diff --git a/src/experimental/studio/app.cc b/src/experimental/studio/app.cc index 87cb8c46..a214a092 100644 --- a/src/experimental/studio/app.cc +++ b/src/experimental/studio/app.cc @@ -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); } } diff --git a/src/experimental/toolbox/interaction.cc b/src/experimental/toolbox/interaction.cc index 54355d8a..b1425935 100644 --- a/src/experimental/toolbox/interaction.cc +++ b/src/experimental/toolbox/interaction.cc @@ -13,45 +13,39 @@ // limitations under the License. #include "experimental/toolbox/interaction.h" + #include -#include "experimental/toolbox/physics.h" -#include "experimental/toolbox/renderer.h" -#include "experimental/toolbox/window.h" #include namespace mujoco::toolbox { -PickResult Pick(float x, float y, Window* window, Renderer* renderer, - Physics* physics, const mjvOption& vis_options) { - const float w = static_cast(window->GetWidth()); - const float h = static_cast(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; diff --git a/src/experimental/toolbox/interaction.h b/src/experimental/toolbox/interaction.h index 15b486b3..3da9039f 100644 --- a/src/experimental/toolbox/interaction.h +++ b/src/experimental/toolbox/interaction.h @@ -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 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 diff --git a/src/experimental/toolbox/window.h b/src/experimental/toolbox/window.h index adf6d2b3..9a52851c 100644 --- a/src/experimental/toolbox/window.h +++ b/src/experimental/toolbox/window.h @@ -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(width_) / static_cast(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.