Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin to spawn lights #587

Merged
merged 26 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
139e552
Plugin to spawn lights
ahcorde Jan 27, 2021
2e9f8ef
Improved implementation
ahcorde Jan 28, 2021
d447b36
make linters happy
ahcorde Jan 28, 2021
a1a9448
quick fix
ahcorde Jan 29, 2021
221e0a8
clean code
ahcorde Jan 29, 2021
e4fb065
Merge branch 'main' into ahcorde/spawn/ligths
ahcorde Jan 29, 2021
c83db28
updated light enum names
ahcorde Feb 1, 2021
fa95c6b
Added feedback
ahcorde Feb 1, 2021
0284bcd
make linters happy
ahcorde Feb 1, 2021
50696fb
Create light visual as child of light (#608)
iche033 Feb 3, 2021
6638fef
Added feedback
ahcorde Feb 3, 2021
a8539cd
Merge branch 'main' into ahcorde/spawn/ligths
ahcorde Feb 3, 2021
1f6a60b
Merge branch 'main' into ahcorde/spawn/ligths
iche033 Feb 4, 2021
2a79677
Light types as lower case
ahcorde Feb 5, 2021
e986ade
Merge branch 'main' into ahcorde/spawn/ligths
chapulina Feb 8, 2021
85477e9
Removed macos warning
ahcorde Feb 9, 2021
e39925f
Merge branch 'ahcorde/spawn/ligths' of https://github.com/ignitionrob…
ahcorde Feb 9, 2021
f436409
Merge branch 'main' into ahcorde/spawn/ligths
ahcorde Feb 9, 2021
a6c96bf
Merge branch 'main' into ahcorde/spawn/ligths
ahcorde Feb 9, 2021
2c17afb
Removed warning
ahcorde Feb 9, 2021
ce6ab44
Removed lights visuals, added light spawner to gui.config
ahcorde Feb 9, 2021
8c4ce9a
Merge branch 'main' into ahcorde/spawn/ligths
ahcorde Feb 11, 2021
d75a3b4
Merge branch 'main' into ahcorde/spawn/ligths
ahcorde Feb 12, 2021
89d8256
make linters happy
ahcorde Feb 12, 2021
a984f2e
spawn directional light at the same height as the other light types
ahcorde Feb 17, 2021
73eb8eb
Fix gui.config
ahcorde Feb 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/ignition/gazebo/Conversions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,12 @@ namespace ignition
template<>
msgs::Light convert(const sdf::Light &_in);

/// \brief Generic conversion from a SDF light type to string.
/// \param[in] _in SDF light type.
/// \return Conversion result.
/// \tparam Out Output type.
std::string IGNITION_GAZEBO_VISIBLE
convert(const sdf::LightType &_in);

/// \brief Generic conversion from a light message to another type.
/// \param[in] _in Light message.
Expand All @@ -234,6 +240,12 @@ namespace ignition
template<>
sdf::Light convert(const msgs::Light &_in);

/// \brief Specialized conversion from a string to a sdf light type
/// \param[in] _in String with the light type.
/// \return Light type emun SDF object.
sdf::LightType IGNITION_GAZEBO_VISIBLE
convert(const std::string &_in);

/// \brief Generic conversion from an SDF gui to another type.
/// \param[in] _in SDF gui.
/// \return Conversion result.
Expand Down
46 changes: 46 additions & 0 deletions include/ignition/gazebo/components/LightType.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef IGNITION_GAZEBO_COMPONENTS_LIGHTTYPE_HH_
#define IGNITION_GAZEBO_COMPONENTS_LIGHTTYPE_HH_

#include <string>
#include <memory>
#include <sdf/Light.hh>
#include <ignition/gazebo/components/Factory.hh>
#include <ignition/gazebo/components/Component.hh>
#include <ignition/gazebo/components/Serialization.hh>
#include <ignition/gazebo/config.hh>

namespace ignition
{
namespace gazebo
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
namespace components
{
/// \brief A component that contains the light type. This is a simple wrapper
/// around std::string
using LightType = Component<std::string, class LightTypeTag,
serializers::StringSerializer>;
IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.LightType", LightType)
}
}
}
}

#endif
8 changes: 8 additions & 0 deletions include/ignition/gazebo/rendering/SceneManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
public: rendering::LightPtr CreateLight(Entity _id,
const sdf::Light &_light, Entity _parentId);

/// \brief Create a light
/// \param[in] _id Unique light id
/// \param[in] _light Light sdf dom
/// \param[in] _parentId Parent id
/// \return Light object created from the sdf dom
public: rendering::VisualPtr CreateLightVisual(Entity _id,
const sdf::Light &_light, Entity _parentId);

/// \brief Ignition sensors is the one responsible for adding sensors
/// to the scene. Here we just keep track of it and make sure it has
/// the correct parent.
Expand Down
39 changes: 39 additions & 0 deletions src/Conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include <sdf/Plane.hh>
#include <sdf/Sphere.hh>

#include <algorithm>
#include <string>

#include "ignition/gazebo/Conversions.hh"
Expand Down Expand Up @@ -865,6 +866,44 @@ void ignition::gazebo::set(msgs::SensorNoise *_msg, const sdf::Noise &_sdf)
_msg->set_dynamic_bias_correlation_time(_sdf.DynamicBiasCorrelationTime());
}

//////////////////////////////////////////////////
std::string ignition::gazebo::convert(const sdf::LightType &_in)
{
if (_in == sdf::LightType::POINT)
{
return std::string("point");
}
else if (_in == sdf::LightType::DIRECTIONAL)
{
return std::string("directional");
}
else if (_in == sdf::LightType::SPOT)
{
return std::string("spot");
}
return std::string("");
}

//////////////////////////////////////////////////
sdf::LightType ignition::gazebo::convert(const std::string &_in)
{
std::string inLowerCase = _in;
std::transform(_in.begin(), _in.end(), inLowerCase.begin(), ::tolower);
if (inLowerCase == "point")
{
return sdf::LightType::POINT;
}
else if (inLowerCase == "directional")
{
return sdf::LightType::DIRECTIONAL;
}
else if (inLowerCase == "spot")
{
return sdf::LightType::SPOT;
}
return sdf::LightType::INVALID;
}

//////////////////////////////////////////////////
template<>
IGNITION_GAZEBO_VISIBLE
Expand Down
10 changes: 10 additions & 0 deletions src/Conversions_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ TEST(Conversions, Light)
EXPECT_EQ(math::Angle(3.3), newLight.SpotOuterAngle());
EXPECT_FLOAT_EQ(0.9, newLight.SpotFalloff());
EXPECT_FLOAT_EQ(1.7, newLight.Intensity());

EXPECT_EQ("", convert(sdf::LightType::INVALID));
EXPECT_EQ("point", convert(sdf::LightType::POINT));
EXPECT_EQ("directional", convert(sdf::LightType::DIRECTIONAL));
EXPECT_EQ("spot", convert(sdf::LightType::SPOT));

EXPECT_EQ(sdf::LightType::POINT, convert("POINT"));
EXPECT_EQ(sdf::LightType::DIRECTIONAL, convert("DireCtiOnal"));
EXPECT_EQ(sdf::LightType::SPOT, convert("spot"));
EXPECT_EQ(sdf::LightType::INVALID, convert("gazebo"));
}

/////////////////////////////////////////////////
Expand Down
4 changes: 4 additions & 0 deletions src/SdfEntityCreator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "ignition/gazebo/components/JointType.hh"
#include "ignition/gazebo/components/Lidar.hh"
#include "ignition/gazebo/components/Light.hh"
#include "ignition/gazebo/components/LightType.hh"
#include "ignition/gazebo/components/LinearAcceleration.hh"
#include "ignition/gazebo/components/LinearVelocity.hh"
#include "ignition/gazebo/components/Link.hh"
Expand Down Expand Up @@ -379,6 +380,9 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Light *_light)
this->dataPtr->ecm->CreateComponent(lightEntity,
components::Name(_light->Name()));

this->dataPtr->ecm->CreateComponent(lightEntity,
components::LightType(convert(_light->Type())));

return lightEntity;
}

Expand Down
16 changes: 16 additions & 0 deletions src/gui/gui.config
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@
</ignition-gui>
</plugin>

<!-- Insert lights -->
<plugin filename="Lights" name="Lights">
<ignition-gui>
<anchors target="Shapes">
<line own="left" target="right"/>
<line own="top" target="top"/>
</anchors>
<property key="resizable" type="bool">false</property>
<property key="width" type="double">250</property>
<property key="height" type="double">50</property>
<property key="state" type="string">floating</property>
<property key="showTitleBar" type="bool">false</property>
<property key="cardBackground" type="string">#666666</property>
</ignition-gui>
</plugin>

<!-- Inspector -->
<plugin filename="ComponentInspector" name="Component inspector">
<ignition-gui>
Expand Down
1 change: 1 addition & 0 deletions src/gui/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ add_subdirectory(align_tool)
add_subdirectory(component_inspector)
add_subdirectory(entity_tree)
add_subdirectory(grid_config)
add_subdirectory(lights)
add_subdirectory(playback_scrubber)
add_subdirectory(plotting)
add_subdirectory(resource_spawner)
Expand Down
7 changes: 7 additions & 0 deletions src/gui/plugins/component_inspector/ComponentInspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "ignition/gazebo/components/Joint.hh"
#include "ignition/gazebo/components/Level.hh"
#include "ignition/gazebo/components/Light.hh"
#include "ignition/gazebo/components/LightType.hh"
#include "ignition/gazebo/components/LinearAcceleration.hh"
#include "ignition/gazebo/components/LinearVelocity.hh"
#include "ignition/gazebo/components/LinearVelocitySeed.hh"
Expand Down Expand Up @@ -500,6 +501,12 @@ void ComponentInspector::Update(const UpdateInfo &,
if (this->dataPtr->entity == this->dataPtr->worldEntity)
this->dataPtr->worldName = comp->Data();
}
else if (typeId == components::LightType::typeId)
{
auto comp = _ecm.Component<components::LightType>(this->dataPtr->entity);
if (comp)
setData(item, comp->Data());
}
else if (typeId == components::ParentEntity::typeId)
{
auto comp = _ecm.Component<components::ParentEntity>(
Expand Down
4 changes: 4 additions & 0 deletions src/gui/plugins/lights/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gz_add_gui_plugin(Lights
SOURCES Lights.cc
QT_HEADERS Lights.hh
)
152 changes: 152 additions & 0 deletions src/gui/plugins/lights/Lights.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

#include "Lights.hh"

#include <ignition/msgs/boolean.pb.h>
#include <ignition/msgs/stringmsg.pb.h>

#include <algorithm>
#include <iostream>
#include <string>

#include <ignition/common/Console.hh>
#include <ignition/gui/Application.hh>
#include <ignition/gui/GuiEvents.hh>
#include <ignition/gui/MainWindow.hh>
#include <ignition/plugin/Register.hh>
#include <ignition/transport/Node.hh>
#include <ignition/transport/Publisher.hh>

#include "ignition/gazebo/EntityComponentManager.hh"
#include "ignition/gazebo/gui/GuiEvents.hh"

namespace ignition::gazebo
{
class LightsPrivate
{
};
}

using namespace ignition;
using namespace gazebo;

/////////////////////////////////////////////////
Lights::Lights()
: ignition::gui::Plugin(),
dataPtr(std::make_unique<LightsPrivate>())
{
}

/////////////////////////////////////////////////
Lights::~Lights() = default;

/////////////////////////////////////////////////
void Lights::LoadConfig(const tinyxml2::XMLElement *)
{
if (this->title.empty())
this->title = "Lights";
}

/////////////////////////////////////////////////
void Lights::OnNewLightClicked(const QString &_sdfString)
{
std::string modelSdfString = _sdfString.toStdString();
std::transform(modelSdfString.begin(), modelSdfString.end(),
modelSdfString.begin(), ::tolower);

if (modelSdfString == "point")
{
modelSdfString = std::string("<?xml version=\"1.0\"?>"
"<sdf version=\"1.6\">"
"<light type='point' name='pointlight'>"
"<pose>0 0 2 0 0 0</pose>"
"<cast_shadows>false</cast_shadows>"
"<diffuse>0.5 0.5 0.5 1</diffuse>"
"<specular>0.5 0.5 0.5 1</specular>"
"<attenuation>"
"<range>4</range>"
"<constant>0.2</constant>"
"<linear>0.5</linear>"
"<quadratic>0.01</quadratic>"
"</attenuation>"
"</light>"
"</sdf>");
}
else if (modelSdfString == "directional")
{
modelSdfString = std::string("<?xml version=\"1.0\"?>"
"<sdf version=\"1.6\">"
"<light type='directional'"
"name='directionallight'>"
"<pose>0 0 5 0 0 0</pose>"
"<cast_shadows>true</cast_shadows>"
"<diffuse>0.8 0.8 0.8 1</diffuse>"
"<specular>0.2 0.2 0.2 1</specular>"
"<attenuation>"
"<range>1000</range>"
"<constant>0.9</constant>"
"<linear>0.01</linear>"
"<quadratic>0.001</quadratic>"
"</attenuation>"
"<direction>0 0 -1</direction>"
"</light>"
"</sdf>");
}
else if (modelSdfString == "spot")
{
modelSdfString = std::string("<?xml version=\"1.0\"?>"
"<sdf version=\"1.6\">"
"<light type='spot' name='spotlight'>"
"<pose>0 0 2 0 0 0</pose>"
"<cast_shadows>true</cast_shadows>"
"<diffuse>0.5 0.5 0.5 1</diffuse>"
"<specular>0.5 0.5 0.5 1</specular>"
"<attenuation>"
"<range>4</range>"
"<constant>0.2</constant>"
"<linear>0.5</linear>"
"<quadratic>0.01</quadratic>"
"</attenuation>"
"<direction>0 0 -1</direction>"
"<spot>"
"<inner_angle>0.1</inner_angle>"
"<outer_angle>0.5</outer_angle>"
"<falloff>0.8</falloff>"
"</spot>"
"</light>"
"</sdf>");
}
else
{
ignwarn << "Invalid model string " << modelSdfString << "\n";
ignwarn << "The valid options are:\n";
ignwarn << " - point\n";
ignwarn << " - directional\n";
ignwarn << " - spot\n";
return;
}

ignition::gui::events::SpawnFromDescription event(modelSdfString);
ignition::gui::App()->sendEvent(
ignition::gui::App()->findChild<ignition::gui::MainWindow *>(),
&event);
}

// Register this plugin
IGNITION_ADD_PLUGIN(ignition::gazebo::Lights,
ignition::gui::Plugin)
Loading