From 5c1b631153ab53eed964e83a92d38bdbbd2fc91e Mon Sep 17 00:00:00 2001 From: Martin Pecka Date: Thu, 22 Apr 2021 12:12:37 +0200 Subject: [PATCH 1/2] Fixed light preview parameters in GUI. Signed-off-by: Martin Pecka --- gazebo/gui/LightMaker.cc | 2 ++ gazebo/gui/LightMaker_TEST.cc | 6 ++++++ gazebo/rendering/Light.cc | 14 ++++++++++---- gazebo/rendering/Light.hh | 4 ++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/gazebo/gui/LightMaker.cc b/gazebo/gui/LightMaker.cc index 57cd76fd92..84e55d137f 100644 --- a/gazebo/gui/LightMaker.cc +++ b/gazebo/gui/LightMaker.cc @@ -115,6 +115,8 @@ bool LightMaker::Init() this->dataPtr->light->Load(); scene->AddLight(this->dataPtr->light); + this->msg.clear_name(); + this->dataPtr->light->UpdateFromMsg(this->msg); this->dataPtr->light->SetLightType(this->lightTypename); this->dataPtr->light->SetPosition(ignition::math::Vector3d(0, 0, 1)); if (this->lightTypename == "directional") diff --git a/gazebo/gui/LightMaker_TEST.cc b/gazebo/gui/LightMaker_TEST.cc index 33dcb0b6a5..fff41d72bc 100644 --- a/gazebo/gui/LightMaker_TEST.cc +++ b/gazebo/gui/LightMaker_TEST.cc @@ -62,6 +62,7 @@ void LightMaker_TEST::PointLight() // Check there's a light in the scene -- this is the preview light = scene->LightByName("__default__"); QVERIFY(light != NULL); + const auto previewLight = light; // Check that the light appeared in the center of the screen ignition::math::Vector3d startPos = pointLightMaker->EntityPosition(); @@ -98,6 +99,11 @@ void LightMaker_TEST::PointLight() light = scene->LightByName("user_point_light_0"); QVERIFY(light != NULL); + QVERIFY(light->LightType() == previewLight->LightType()); + QVERIFY(light->DiffuseColor() == previewLight->DiffuseColor()); + QVERIFY(light->SpecularColor() == previewLight->SpecularColor()); + QVERIFY(light->Direction() == previewLight->Direction()); + // Terminate mainWindow->close(); delete mainWindow; diff --git a/gazebo/rendering/Light.cc b/gazebo/rendering/Light.cc index 4aa059217f..b0e18c8285 100644 --- a/gazebo/rendering/Light.cc +++ b/gazebo/rendering/Light.cc @@ -156,14 +156,20 @@ void Light::UpdateSDFFromMsg(const msgs::Light &_msg) ////////////////////////////////////////////////// void Light::UpdateFromMsg(ConstLightPtr &_msg) { - this->UpdateSDFFromMsg(*_msg); + this->UpdateFromMsg(*_msg); +} + +////////////////////////////////////////////////// +void Light::UpdateFromMsg(const msgs::Light &_msg) +{ + this->UpdateSDFFromMsg(_msg); this->Update(); - if (_msg->has_pose()) + if (_msg.has_pose()) { - this->SetPosition(msgs::ConvertIgn(_msg->pose().position())); - this->SetRotation(msgs::ConvertIgn(_msg->pose().orientation())); + this->SetPosition(msgs::ConvertIgn(_msg.pose().position())); + this->SetRotation(msgs::ConvertIgn(_msg.pose().orientation())); } } diff --git a/gazebo/rendering/Light.hh b/gazebo/rendering/Light.hh index 1c4b52c4c1..d7cf12131a 100644 --- a/gazebo/rendering/Light.hh +++ b/gazebo/rendering/Light.hh @@ -214,6 +214,10 @@ namespace gazebo /// \param[in] _msg Light message to update from public: void UpdateFromMsg(ConstLightPtr &_msg); + /// \brief Update a light source from a message. + /// \param[in] _msg Light message to update from + public: void UpdateFromMsg(const msgs::Light &_msg); + /// \brief Clone the light with a new name /// \param[in] _name Name of the cloned light. /// \param[in] _scene Scene to contain the light. From 9b8769b00a097c7e3e95266c456eaefd5c0e6d1d Mon Sep 17 00:00:00 2001 From: Martin Pecka Date: Tue, 29 Jun 2021 12:37:09 +0200 Subject: [PATCH 2/2] Added Light getters for spotlight parameters, added test for spotlight GUI creation. Signed-off-by: Martin Pecka --- gazebo/gui/LightMaker_TEST.cc | 100 ++++++++++++++++++++++++++++++++++ gazebo/gui/LightMaker_TEST.hh | 3 + gazebo/rendering/Light.cc | 30 +++++++++- gazebo/rendering/Light.hh | 12 ++++ 4 files changed, 144 insertions(+), 1 deletion(-) diff --git a/gazebo/gui/LightMaker_TEST.cc b/gazebo/gui/LightMaker_TEST.cc index fff41d72bc..62eb6e0e97 100644 --- a/gazebo/gui/LightMaker_TEST.cc +++ b/gazebo/gui/LightMaker_TEST.cc @@ -24,6 +24,18 @@ #include "gazebo/gui/LightMaker.hh" #include "gazebo/gui/LightMaker_TEST.hh" +// from https://stackoverflow.com/a/65954026/1076564 +#define COMPARE_DBL(actual, expected, epsilon) \ +do {\ + if (!QTest::compare_helper((qAbs(actual - expected) <= epsilon), \ + QString{"Compared values are not the same in respect to epsilon %1"} \ + .arg(epsilon).toLocal8Bit().constData(), \ + QTest::toString(actual), \ + QTest::toString(expected), \ + #actual, #expected, __FILE__, __LINE__)) \ + return;\ +} while (false) + ///////////////////////////////////////////////// void LightMaker_TEST::PointLight() { @@ -109,6 +121,94 @@ void LightMaker_TEST::PointLight() delete mainWindow; } +///////////////////////////////////////////////// +void LightMaker_TEST::SpotLight() +{ + this->resMaxPercentChange = 5.0; + this->shareMaxPercentChange = 2.0; + + this->Load("worlds/empty.world", false, false, false); + + // Create the main window. + gazebo::gui::MainWindow *mainWindow = new gazebo::gui::MainWindow(); + QVERIFY(mainWindow != NULL); + mainWindow->Load(); + mainWindow->Init(); + mainWindow->show(); + + this->ProcessEventsAndDraw(mainWindow); + + // Get scene + gazebo::rendering::ScenePtr scene = gazebo::rendering::get_scene(); + QVERIFY(scene != NULL); + + // Check there's no light in the scene yet + gazebo::rendering::LightPtr light = scene->LightByName("__default__"); + QVERIFY(light == NULL); + light = scene->LightByName("user_spot_light_0"); + QVERIFY(light == NULL); + + // Create a spotLight maker + gazebo::gui::SpotLightMaker *spotLightMaker = + new gazebo::gui::SpotLightMaker(); + QVERIFY(spotLightMaker != NULL); + + // Start the maker to make a light + spotLightMaker->Start(); + + // Check there's a light in the scene -- this is the preview + light = scene->LightByName("__default__"); + QVERIFY(light != NULL); + const auto previewLight = light; + + // Check that the light appeared in the center of the screen + ignition::math::Vector3d startPos = spotLightMaker->EntityPosition(); + QVERIFY(startPos == ignition::math::Vector3d::UnitZ); + QVERIFY(light->Position() == startPos); + + // Mouse move + gazebo::common::MouseEvent mouseEvent; + mouseEvent.SetType(gazebo::common::MouseEvent::MOVE); + spotLightMaker->OnMouseMove(mouseEvent); + + // Check that entity moved + ignition::math::Vector3d pos = spotLightMaker->EntityPosition(); + QVERIFY(pos != startPos); + QVERIFY(light->Position() == pos); + + // Mouse release + mouseEvent.SetType(gazebo::common::MouseEvent::RELEASE); + mouseEvent.SetButton(gazebo::common::MouseEvent::LEFT); + mouseEvent.SetDragging(false); + mouseEvent.SetPressPos(0, 0); + mouseEvent.SetPos(0, 0); + spotLightMaker->OnMouseRelease(mouseEvent); + + // Check there's no light in the scene -- the preview is gone + light = scene->LightByName("__default__"); + QVERIFY(light == NULL); + light = scene->LightByName("user_spot_light"); + QVERIFY(light == NULL); + + this->ProcessEventsAndDraw(mainWindow); + + // Check there's a light in the scene -- this is the final pointLight + light = scene->LightByName("user_spot_light_0"); + QVERIFY(light != NULL); + + QVERIFY(light->LightType() == previewLight->LightType()); + QVERIFY(light->DiffuseColor() == previewLight->DiffuseColor()); + QVERIFY(light->SpecularColor() == previewLight->SpecularColor()); + QVERIFY(light->Direction() == previewLight->Direction()); + COMPARE_DBL(light->SpotInnerAngle(), previewLight->SpotInnerAngle(), 1e-3); + COMPARE_DBL(light->SpotOuterAngle(), previewLight->SpotOuterAngle(), 1e-3); + COMPARE_DBL(light->SpotFalloff(), previewLight->SpotFalloff(), 1e-3); + + // Terminate + mainWindow->close(); + delete mainWindow; +} + ///////////////////////////////////////////////// void LightMaker_TEST::CopyLight() { diff --git a/gazebo/gui/LightMaker_TEST.hh b/gazebo/gui/LightMaker_TEST.hh index 26accfb4f4..9484cba35c 100644 --- a/gazebo/gui/LightMaker_TEST.hh +++ b/gazebo/gui/LightMaker_TEST.hh @@ -28,6 +28,9 @@ class LightMaker_TEST : public QTestFixture /// \brief Test creating a point light. private slots: void PointLight(); + /// \brief Test creating a spot light. + private slots: void SpotLight(); + /// \brief Test creating a point light as a copy. private slots: void CopyLight(); }; diff --git a/gazebo/rendering/Light.cc b/gazebo/rendering/Light.cc index b0e18c8285..2f0b44ba03 100644 --- a/gazebo/rendering/Light.cc +++ b/gazebo/rendering/Light.cc @@ -156,7 +156,8 @@ void Light::UpdateSDFFromMsg(const msgs::Light &_msg) ////////////////////////////////////////////////// void Light::UpdateFromMsg(ConstLightPtr &_msg) { - this->UpdateFromMsg(*_msg); + if (_msg) + this->UpdateFromMsg(*_msg); } ////////////////////////////////////////////////// @@ -627,6 +628,15 @@ bool Light::CastShadows() const return false; } +////////////////////////////////////////////////// +double Light::SpotInnerAngle() const +{ + if (this->dataPtr->light->getType() != Ogre::Light::LT_SPOTLIGHT) + return std::numeric_limits::quiet_NaN(); + + return this->dataPtr->light->getSpotlightInnerAngle().valueRadians(); +} + ////////////////////////////////////////////////// void Light::SetSpotInnerAngle(const double _angle) { @@ -642,6 +652,15 @@ void Light::SetSpotInnerAngle(const double _angle) } } +////////////////////////////////////////////////// +double Light::SpotOuterAngle() const +{ + if (this->dataPtr->light->getType() != Ogre::Light::LT_SPOTLIGHT) + return std::numeric_limits::quiet_NaN(); + + return this->dataPtr->light->getSpotlightOuterAngle().valueRadians(); +} + ////////////////////////////////////////////////// void Light::SetSpotOuterAngle(const double _angle) { @@ -657,6 +676,15 @@ void Light::SetSpotOuterAngle(const double _angle) } } +////////////////////////////////////////////////// +double Light::SpotFalloff() const +{ + if (this->dataPtr->light->getType() != Ogre::Light::LT_SPOTLIGHT) + return std::numeric_limits::quiet_NaN(); + + return this->dataPtr->light->getSpotlightFalloff(); +} + ////////////////////////////////////////////////// void Light::SetSpotFalloff(const double _angle) { diff --git a/gazebo/rendering/Light.hh b/gazebo/rendering/Light.hh index d7cf12131a..d56b5a83a6 100644 --- a/gazebo/rendering/Light.hh +++ b/gazebo/rendering/Light.hh @@ -182,14 +182,26 @@ namespace gazebo public: void SetAttenuation(double _constant, double _linear, double _quadratic); + /// \brief Get the spot light inner angle + /// \return The inner angle in radians (or NaN if the light is not spot). + public: double SpotInnerAngle() const; + /// \brief Set the spot light inner angle /// \param[in] _angle Inner angle in radians public: void SetSpotInnerAngle(const double _angle); + /// \brief Get the spot light outer angle + /// \return The outer angle in radians (or NaN if the light is not spot). + public: double SpotOuterAngle() const; + /// \brief Set the spot light outer angle /// \param[in] _angle Outer angle in radians public: void SetSpotOuterAngle(const double _angle); + /// \brief Get the spot light falloff + /// \return The falloff value (or NaN if the light is not spot). + public: double SpotFalloff() const; + /// \brief Set the spot light falloff /// \param[in] _value Falloff value public: void SetSpotFalloff(const double _value);