Remove dependency on Window, Physics, and Renderer classes.

PiperOrigin-RevId: 825137441
Change-Id: I099a518ca9618698401d0330a6ef08278168f88e
This commit is contained in:
Haroon Qureshi
2025-10-28 11:41:28 -07:00
committed by Copybara-Service
parent 4620b9f23a
commit 80eb511620
4 changed files with 35 additions and 38 deletions
+10 -10
View File
@@ -284,9 +284,9 @@ void App::HandleMouseEvents() {
// Left double click.
if (data && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
toolbox::PickResult picked =
toolbox::Pick(mouse_x, mouse_y, window_.get(), renderer_.get(),
physics_.get(), vis_options_);
toolbox::PickResult picked = toolbox::Pick(
model, data, &camera_, mouse_x, mouse_y, window_->GetAspectRatio(),
&renderer_->GetScene(), &vis_options_);
if (picked.body >= 0) {
perturb_.select = picked.body;
perturb_.flexselect = picked.flex;
@@ -305,9 +305,9 @@ void App::HandleMouseEvents() {
// Right double click.
if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Right)) {
toolbox::PickResult picked =
toolbox::Pick(mouse_x, mouse_y, window_.get(), renderer_.get(),
physics_.get(), vis_options_);
toolbox::PickResult picked = toolbox::Pick(
model, data, &camera_, mouse_x, mouse_y, window_->GetAspectRatio(),
&renderer_->GetScene(), &vis_options_);
mju_copy3(camera_.lookat, picked.point);
if (picked.body > 0 && io.KeyCtrl) {
camera_.type = mjCAMERA_TRACKING;
@@ -457,11 +457,11 @@ void App::HandleKeyboardEvents() {
// Camera shortcuts.
if (model) {
if (ImGui_IsChordJustPressed(ImGuiKey_Escape)) {
ui_.camera_idx = toolbox::SetCamera(*model, camera_, 0);
ui_.camera_idx = toolbox::SetCamera(model, &camera_, 0);
} else if (ImGui_IsChordJustPressed(ImGuiKey_LeftBracket)) {
ui_.camera_idx = toolbox::SetCamera(*model, camera_,ui_.camera_idx - 1);
ui_.camera_idx = toolbox::SetCamera(model, &camera_,ui_.camera_idx - 1);
} else if (ImGui_IsChordJustPressed(ImGuiKey_RightBracket)) {
ui_.camera_idx = toolbox::SetCamera(*model, camera_, ui_.camera_idx + 1);
ui_.camera_idx = toolbox::SetCamera(model, &camera_, ui_.camera_idx + 1);
}
}
@@ -570,7 +570,7 @@ void App::SaveSettings() {
void App::SetCamera(int idx) {
if (Model()) {
ui_.camera_idx = ::mujoco::toolbox::SetCamera(*Model(), camera_, idx);
ui_.camera_idx = ::mujoco::toolbox::SetCamera(Model(), &camera_, idx);
}
}
+15 -21
View File
@@ -13,45 +13,39 @@
// limitations under the License.
#include "experimental/toolbox/interaction.h"
#include <algorithm>
#include "experimental/toolbox/physics.h"
#include "experimental/toolbox/renderer.h"
#include "experimental/toolbox/window.h"
#include <mujoco/mujoco.h>
namespace mujoco::toolbox {
PickResult Pick(float x, float y, Window* window, Renderer* renderer,
Physics* physics, const mjvOption& vis_options) {
const float w = static_cast<float>(window->GetWidth());
const float h = static_cast<float>(window->GetHeight());
const float aspect_ratio = w / h;
PickResult Pick(const mjModel* m, const mjData* d, const mjvCamera* camera,
float x, float y, float aspect_ratio, const mjvScene* scene,
const mjvOption* vis_options) {
PickResult result;
result.body =
mjv_select(physics->GetModel(), physics->GetData(), &vis_options,
aspect_ratio, x, 1.0f - y, &renderer->GetScene(), result.point,
&result.geom, &result.flex, &result.skin);
mjv_select(m, d, vis_options, aspect_ratio, x, 1.0f - y, scene,
result.point, &result.geom, &result.flex, &result.skin);
return result;
}
int SetCamera(const mjModel& model, mjvCamera& camera, int request_idx) {
int SetCamera(const mjModel* m, mjvCamera* camera, int request_idx) {
// 0 = free, 1 = tracking, 2+ = fixed
int camera_idx = std::clamp(request_idx, 0, std::max(model.ncam + 1, 0));
int camera_idx = std::clamp(request_idx, 0, std::max(m->ncam + 1, 0));
if (camera_idx == 0) {
camera.type = mjCAMERA_FREE;
camera->type = mjCAMERA_FREE;
} else if (camera_idx == 1) {
if (camera.trackbodyid >= 0) {
camera.type = mjCAMERA_TRACKING;
camera.fixedcamid = -1;
if (camera->trackbodyid >= 0) {
camera->type = mjCAMERA_TRACKING;
camera->fixedcamid = -1;
} else {
camera.type = mjCAMERA_FREE;
camera->type = mjCAMERA_FREE;
camera_idx = 0;
}
} else {
camera.type = mjCAMERA_FIXED;
camera.fixedcamid = camera_idx - 2;
camera->type = mjCAMERA_FIXED;
camera->fixedcamid = camera_idx - 2;
}
return camera_idx;
+4 -6
View File
@@ -15,9 +15,6 @@
#ifndef MUJOCO_SRC_EXPERIMENTAL_TOOLBOX_INTERACTION_H_
#define MUJOCO_SRC_EXPERIMENTAL_TOOLBOX_INTERACTION_H_
#include "experimental/toolbox/physics.h"
#include "experimental/toolbox/renderer.h"
#include "experimental/toolbox/window.h"
#include <mujoco/mujoco.h>
namespace mujoco::toolbox {
@@ -32,8 +29,9 @@ struct PickResult {
};
// Returns information about the object (if any) under the mouse cursor.
PickResult Pick(float x, float y, Window* window, Renderer* renderer,
Physics* physics, const mjvOption& vis_options);
PickResult Pick(const mjModel* m, const mjData* d, const mjvCamera* camera,
float x, float y, float aspect_ratio, const mjvScene* scene,
const mjvOption* vis_options);
// Updates the camera according to the requested index using this convention:
//
@@ -45,7 +43,7 @@ PickResult Pick(float x, float y, Window* window, Renderer* renderer,
// convention. Note the returned index may differ from the request if the
// request was invalid (index was out of range or tracking camera was not
// available).
int SetCamera(const mjModel& model, mjvCamera& camera, int request_idx);
int SetCamera(const mjModel* model, mjvCamera* camera, int request_idx);
} // namespace mujoco::toolbox
+6 -1
View File
@@ -68,10 +68,15 @@ class Window {
// Sets the title of the window.
void SetTitle(std::string_view title);
// Returns the current size of the window.
// Returns information related to the current size of the window.
int GetWidth() const { return width_; }
int GetHeight() const { return height_; }
float GetScale() const { return scale_; }
float GetAspectRatio() const {
return height_ > 0
? static_cast<float>(width_) / static_cast<float>(height_)
: 1.0f;
}
// Returns the path to a file that was dropped on the window. Once called,
// the value will be cleared until the next time a file is dropped.