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. // Left double click.
if (data && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) { if (data && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
toolbox::PickResult picked = toolbox::PickResult picked = toolbox::Pick(
toolbox::Pick(mouse_x, mouse_y, window_.get(), renderer_.get(), model, data, &camera_, mouse_x, mouse_y, window_->GetAspectRatio(),
physics_.get(), vis_options_); &renderer_->GetScene(), &vis_options_);
if (picked.body >= 0) { if (picked.body >= 0) {
perturb_.select = picked.body; perturb_.select = picked.body;
perturb_.flexselect = picked.flex; perturb_.flexselect = picked.flex;
@@ -305,9 +305,9 @@ void App::HandleMouseEvents() {
// Right double click. // Right double click.
if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Right)) { if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Right)) {
toolbox::PickResult picked = toolbox::PickResult picked = toolbox::Pick(
toolbox::Pick(mouse_x, mouse_y, window_.get(), renderer_.get(), model, data, &camera_, mouse_x, mouse_y, window_->GetAspectRatio(),
physics_.get(), vis_options_); &renderer_->GetScene(), &vis_options_);
mju_copy3(camera_.lookat, picked.point); mju_copy3(camera_.lookat, picked.point);
if (picked.body > 0 && io.KeyCtrl) { if (picked.body > 0 && io.KeyCtrl) {
camera_.type = mjCAMERA_TRACKING; camera_.type = mjCAMERA_TRACKING;
@@ -457,11 +457,11 @@ void App::HandleKeyboardEvents() {
// Camera shortcuts. // Camera shortcuts.
if (model) { if (model) {
if (ImGui_IsChordJustPressed(ImGuiKey_Escape)) { 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)) { } 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)) { } 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) { void App::SetCamera(int idx) {
if (Model()) { 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. // limitations under the License.
#include "experimental/toolbox/interaction.h" #include "experimental/toolbox/interaction.h"
#include <algorithm> #include <algorithm>
#include "experimental/toolbox/physics.h"
#include "experimental/toolbox/renderer.h"
#include "experimental/toolbox/window.h"
#include <mujoco/mujoco.h> #include <mujoco/mujoco.h>
namespace mujoco::toolbox { namespace mujoco::toolbox {
PickResult Pick(float x, float y, Window* window, Renderer* renderer, PickResult Pick(const mjModel* m, const mjData* d, const mjvCamera* camera,
Physics* physics, const mjvOption& vis_options) { float x, float y, float aspect_ratio, const mjvScene* scene,
const float w = static_cast<float>(window->GetWidth()); const mjvOption* vis_options) {
const float h = static_cast<float>(window->GetHeight());
const float aspect_ratio = w / h;
PickResult result; PickResult result;
result.body = result.body =
mjv_select(physics->GetModel(), physics->GetData(), &vis_options, mjv_select(m, d, vis_options, aspect_ratio, x, 1.0f - y, scene,
aspect_ratio, x, 1.0f - y, &renderer->GetScene(), result.point, result.point, &result.geom, &result.flex, &result.skin);
&result.geom, &result.flex, &result.skin);
return result; 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 // 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) { if (camera_idx == 0) {
camera.type = mjCAMERA_FREE; camera->type = mjCAMERA_FREE;
} else if (camera_idx == 1) { } else if (camera_idx == 1) {
if (camera.trackbodyid >= 0) { if (camera->trackbodyid >= 0) {
camera.type = mjCAMERA_TRACKING; camera->type = mjCAMERA_TRACKING;
camera.fixedcamid = -1; camera->fixedcamid = -1;
} else { } else {
camera.type = mjCAMERA_FREE; camera->type = mjCAMERA_FREE;
camera_idx = 0; camera_idx = 0;
} }
} else { } else {
camera.type = mjCAMERA_FIXED; camera->type = mjCAMERA_FIXED;
camera.fixedcamid = camera_idx - 2; camera->fixedcamid = camera_idx - 2;
} }
return camera_idx; return camera_idx;
+4 -6
View File
@@ -15,9 +15,6 @@
#ifndef MUJOCO_SRC_EXPERIMENTAL_TOOLBOX_INTERACTION_H_ #ifndef MUJOCO_SRC_EXPERIMENTAL_TOOLBOX_INTERACTION_H_
#define 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> #include <mujoco/mujoco.h>
namespace mujoco::toolbox { namespace mujoco::toolbox {
@@ -32,8 +29,9 @@ struct PickResult {
}; };
// Returns information about the object (if any) under the mouse cursor. // Returns information about the object (if any) under the mouse cursor.
PickResult Pick(float x, float y, Window* window, Renderer* renderer, PickResult Pick(const mjModel* m, const mjData* d, const mjvCamera* camera,
Physics* physics, const mjvOption& vis_options); 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: // 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 // 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 // request was invalid (index was out of range or tracking camera was not
// available). // available).
int SetCamera(const mjModel& model, mjvCamera& camera, int request_idx); int SetCamera(const mjModel* model, mjvCamera* camera, int request_idx);
} // namespace mujoco::toolbox } // namespace mujoco::toolbox
+6 -1
View File
@@ -68,10 +68,15 @@ class Window {
// Sets the title of the window. // Sets the title of the window.
void SetTitle(std::string_view title); 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 GetWidth() const { return width_; }
int GetHeight() const { return height_; } int GetHeight() const { return height_; }
float GetScale() const { return scale_; } 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, // 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. // the value will be cleared until the next time a file is dropped.