Skip to content

Commit

Permalink
Add service for configuring view control sensitivity (#504)
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Chen <[email protected]>
  • Loading branch information
iche033 authored Nov 17, 2022
1 parent e6d45da commit 0e4a198
Showing 1 changed file with 58 additions and 7 deletions.
65 changes: 58 additions & 7 deletions src/plugins/interactive_view_control/InteractiveViewControl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,23 @@ class ignition::gui::plugins::InteractiveViewControlPrivate
public: bool OnViewControl(const msgs::StringMsg &_msg,
msgs::Boolean &_res);


/// \brief Callback for camera reference visual request
/// \param[in] _msg Request message to enable/disable the reference visual
/// \param[out] _res Response data
/// \return True if the request is received
public: bool OnReferenceVisual(const msgs::Boolean &_msg,
msgs::Boolean &_res);

/// \brief Callback for camera view control sensitivity request
/// \param[in] _msg Request message to set the camera view controller
/// sensitivity. Value must be greater than zero. The higher the number
/// the more sensitive camera control is to mouse movements. Affects all
/// camera movements (pan, orbit, zoom)
/// \param[out] _res Response data
/// \return True if the request is received
public: bool OnViewControlSensitivity(const msgs::Double &_msg,
msgs::Boolean &_res);

/// \brief Update the reference visual. Adjust scale based on distance from
/// camera to target point so it remains the same size on screen.
public: void UpdateReferenceVisual();
Expand Down Expand Up @@ -112,6 +121,9 @@ class ignition::gui::plugins::InteractiveViewControlPrivate
/// \brief Camera reference visual service
public: std::string cameraRefVisualService;

/// \brief Camera view control sensitivity service
public: std::string cameraViewControlSensitivityService;

/// \brief Ray query for mouse clicks
public: rendering::RayQueryPtr rayQuery{nullptr};

Expand All @@ -123,6 +135,9 @@ class ignition::gui::plugins::InteractiveViewControlPrivate

/// \brief Transport node for making transform control requests
public: transport::Node node;

/// \brief View control sensitivity value. Must be greater than 0.
public: double viewControlSensitivity = 1.0;
};

using namespace ignition;
Expand Down Expand Up @@ -246,7 +261,9 @@ void InteractiveViewControlPrivate::OnRender()
this->viewControl->SetTarget(this->target);
double distance = this->camera->WorldPosition().Distance(
this->target);
double amount = -this->drag.Y() * distance / 5.0;

math::Vector2d newDrag = this->drag * this->viewControlSensitivity;
double amount = -newDrag.Y() * distance / 5.0;
this->viewControl->Zoom(amount);
this->UpdateReferenceVisual();
}
Expand All @@ -261,19 +278,20 @@ void InteractiveViewControlPrivate::OnRender()
}
else
{
math::Vector2d newDrag = this->drag * this->viewControlSensitivity;
// Pan with left button
if (this->mouseEvent.Buttons() & common::MouseEvent::LEFT)
{
if (Qt::ShiftModifier == QGuiApplication::queryKeyboardModifiers())
this->viewControl->Orbit(this->drag);
this->viewControl->Orbit(newDrag);
else
this->viewControl->Pan(this->drag);
this->viewControl->Pan(newDrag);
this->UpdateReferenceVisual();
}
// Orbit with middle button
else if (this->mouseEvent.Buttons() & common::MouseEvent::MIDDLE)
{
this->viewControl->Orbit(this->drag);
this->viewControl->Orbit(newDrag);
this->UpdateReferenceVisual();
}
// Zoom with right button
Expand All @@ -282,7 +300,7 @@ void InteractiveViewControlPrivate::OnRender()
double hfov = this->camera->HFOV().Radian();
double vfov = 2.0f * atan(tan(hfov / 2.0f) / this->camera->AspectRatio());
double distance = this->camera->WorldPosition().Distance(this->target);
double amount = ((-this->drag.Y() /
double amount = ((-newDrag.Y() /
static_cast<double>(this->camera->ImageHeight()))
* distance * tan(vfov/2.0) * 6.0);
this->viewControl->Zoom(amount);
Expand Down Expand Up @@ -345,6 +363,26 @@ bool InteractiveViewControlPrivate::OnReferenceVisual(const msgs::Boolean &_msg,
return true;
}

/////////////////////////////////////////////////
bool InteractiveViewControlPrivate::OnViewControlSensitivity(
const msgs::Double &_msg, msgs::Boolean &_res)
{
std::lock_guard<std::mutex> lock(this->mutex);

if (_msg.data() <= 0.0)
{
ignwarn << "View controller sensitivity must be greater than zero ["
<< _msg.data() << "]" << std::endl;
_res.set_data(false);
return true;
}

this->viewControlSensitivity = _msg.data();

_res.set_data(true);
return true;
}

/////////////////////////////////////////////////
InteractiveViewControl::InteractiveViewControl()
: Plugin(), dataPtr(std::make_unique<InteractiveViewControlPrivate>())
Expand All @@ -368,12 +406,25 @@ void InteractiveViewControl::LoadConfig(
ignmsg << "Camera view controller topic advertised on ["
<< this->dataPtr->cameraViewControlService << "]" << std::endl;

this->dataPtr->cameraRefVisualService = "/gui/camera/reference_visual";
// camera reference visual
this->dataPtr->cameraRefVisualService =
"/gui/camera/view_control/reference_visual";
this->dataPtr->node.Advertise(this->dataPtr->cameraRefVisualService,
&InteractiveViewControlPrivate::OnReferenceVisual, this->dataPtr.get());
ignmsg << "Camera reference visual topic advertised on ["
<< this->dataPtr->cameraRefVisualService << "]" << std::endl;

// camera view control sensitivity
this->dataPtr->cameraViewControlSensitivityService =
"/gui/camera/view_control/sensitivity";
this->dataPtr->node.Advertise(
this->dataPtr->cameraViewControlSensitivityService,
&InteractiveViewControlPrivate::OnViewControlSensitivity,
this->dataPtr.get());
ignmsg << "Camera view control sensitivity advertised on ["
<< this->dataPtr->cameraViewControlSensitivityService << "]"
<< std::endl;

ignition::gui::App()->findChild<
ignition::gui::MainWindow *>()->installEventFilter(this);
}
Expand Down

0 comments on commit 0e4a198

Please sign in to comment.