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

Label Component & System #853

Merged
merged 19 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
42 changes: 42 additions & 0 deletions include/ignition/gazebo/components/Label.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2018 Open Source Robotics Foundation
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
*
* 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_LABEL_HH_
#define IGNITION_GAZEBO_COMPONENTS_LABEL_HH_

#include <ignition/gazebo/config.hh>
#include <ignition/gazebo/Export.hh>
#include "ignition/gazebo/components/Factory.hh"
#include "ignition/gazebo/components/Component.hh"
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved

namespace ignition
{
namespace gazebo
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
namespace components
{
/// \brief A component that holds the label of the object used by
/// Segmentation & Bounding box sensors to generate datasets annotations
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
using Label = Component<int, class LabelTag>;
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.Label",
Label)
}
}
}
}
#endif
41 changes: 35 additions & 6 deletions src/rendering/RenderUtil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include "ignition/gazebo/components/DepthCamera.hh"
#include "ignition/gazebo/components/GpuLidar.hh"
#include "ignition/gazebo/components/Geometry.hh"
#include "ignition/gazebo/components/Label.hh"
#include "ignition/gazebo/components/LaserRetro.hh"
#include "ignition/gazebo/components/Light.hh"
#include "ignition/gazebo/components/LightCmd.hh"
Expand Down Expand Up @@ -220,6 +221,9 @@ class ignition::gazebo::RenderUtilPrivate
/// All temperatures are in Kelvin.
public: std::map<Entity, std::tuple<float, float, std::string>> entityTemp;

/// \brief A map of entity ids and label data for datasets annotations
public: std::map<Entity, int> entityLabel;
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved

/// \brief A map of entity ids and wire boxes
public: std::unordered_map<Entity, ignition::rendering::WireBoxPtr> wireBoxes;

Expand Down Expand Up @@ -637,6 +641,7 @@ void RenderUtil::Update()
auto actorTransforms = std::move(this->dataPtr->actorTransforms);
auto actorAnimationData = std::move(this->dataPtr->actorAnimationData);
auto entityTemp = std::move(this->dataPtr->entityTemp);
auto entityLabel = std::move(this->dataPtr->entityLabel);
auto newWireframeVisualLinks =
std::move(this->dataPtr->newWireframeVisualLinks);
auto newCollisionLinks = std::move(this->dataPtr->newCollisionLinks);
Expand All @@ -657,6 +662,7 @@ void RenderUtil::Update()
this->dataPtr->actorTransforms.clear();
this->dataPtr->actorAnimationData.clear();
this->dataPtr->entityTemp.clear();
this->dataPtr->entityLabel.clear();
this->dataPtr->newWireframeVisualLinks.clear();
this->dataPtr->newCollisionLinks.clear();
this->dataPtr->thermalCameraData.clear();
Expand All @@ -680,9 +686,7 @@ void RenderUtil::Update()
if (scene.Grid() && !this->dataPtr->enableSensors)
this->ShowGrid();
if (scene.Sky())
{
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
this->dataPtr->scene->SetSkyEnabled(true);
}

// only one scene so break
break;
Expand Down Expand Up @@ -971,9 +975,7 @@ void RenderUtil::Update()

math::Pose3d globalPose;
if (entityPoses.find(tf.first) != entityPoses.end())
{
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
globalPose = entityPoses[tf.first];
}

math::Pose3d trajPose;
// Trajectory from the ECS
Expand Down Expand Up @@ -1096,8 +1098,7 @@ void RenderUtil::Update()
if (!node)
continue;

auto visual =
std::dynamic_pointer_cast<rendering::Visual>(node);
auto visual = std::dynamic_pointer_cast<rendering::Visual>(node);
if (!visual)
continue;

Expand All @@ -1112,6 +1113,20 @@ void RenderUtil::Update()
}
}

// set visual label
for (const auto &label : entityLabel)
{
auto node = this->dataPtr->sceneManager.NodeById(label.first);
if (!node)
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
continue;

auto visual = std::dynamic_pointer_cast<rendering::Visual>(node);
if (!visual)
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
continue;

visual->SetUserData("label", label.second);
}
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved

// create new wireframe visuals
{
for (const auto &link : newWireframeVisualLinks)
Expand Down Expand Up @@ -1329,6 +1344,13 @@ void RenderUtilPrivate::CreateRenderingEntities(
visual.SetLaserRetro(laserRetro->Data());
}

// set label
auto label = _ecm.Component<components::Label>(_entity);
if (label != nullptr)
{
this->entityLabel[_entity] = label->Data();
}

if (auto temp = _ecm.Component<components::Temperature>(_entity))
{
// get the uniform temperature for the entity
Expand Down Expand Up @@ -1560,6 +1582,13 @@ void RenderUtilPrivate::CreateRenderingEntities(
visual.SetLaserRetro(laserRetro->Data());
}

// set label
auto label = _ecm.Component<components::Label>(_entity);
if (label != nullptr)
{
this->entityLabel[_entity] = label->Data();
}

if (auto temp = _ecm.Component<components::Temperature>(_entity))
{
// get the uniform temperature for the entity
Expand Down
1 change: 1 addition & 0 deletions src/systems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ add_subdirectory(joint_position_controller)
add_subdirectory(joint_state_publisher)
add_subdirectory(joint_traj_control)
add_subdirectory(kinetic_energy_monitor)
add_subdirectory(label)
add_subdirectory(lift_drag)
add_subdirectory(log)
add_subdirectory(log_video_recorder)
Expand Down
4 changes: 4 additions & 0 deletions src/systems/label/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
gz_add_system(label
SOURCES
Label.cc
)
93 changes: 93 additions & 0 deletions src/systems/label/Label.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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 <limits>
#include <string>

#include <ignition/plugin/Register.hh>
#include "ignition/gazebo/components/Label.hh"
#include "ignition/gazebo/components/World.hh"
#include "ignition/gazebo/components/Visual.hh"
#include "ignition/gazebo/components/Link.hh"
#include "ignition/gazebo/EntityComponentManager.hh"
#include "Label.hh"
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved

using namespace ignition;
using namespace gazebo;
using namespace systems;

//////////////////////////////////////////////////
Label::Label() : System()
{
}

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

//////////////////////////////////////////////////
void Label::Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
gazebo::EntityComponentManager &_ecm,
gazebo::EventManager & /*_eventMgr*/)
{
const std::string labelTag = "label";

std::string parentName = _sdf->GetParent()->GetName();
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved

if (!_sdf->HasElement(labelTag))
{
ignerr << "Failed to load Label system, label tag not found";
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
return;
}

int label = _sdf->Get<int>(labelTag);
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved

if (label < 0 || label > 255)
{
ignerr << "Failed to Configure Label system, value " << label
<< " is not in [0-255] range";
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
return;
}

/// Set the component to the visual to set its user data, but
/// if the plugin is inside the <model> tag, get its visual child
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
if (parentName == "visual")
{
_ecm.CreateComponent(_entity, components::Label(label));
}
else if (parentName == "model")
{
// get link child of parent model
auto links = _ecm.ChildrenByComponents<components::Link>(
_entity, components::Link());
if (links.size() > 0)
{
Entity linkEntity = links[0];

// get visual child of parent link
auto visuals = _ecm.ChildrenByComponents<components::Visual>(
linkEntity, components::Visual());
if (visuals.size() > 0)
{
Entity visualEntity = visuals[0];
_ecm.CreateComponent(visualEntity, components::Label(label));
}
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
}
}
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
}

IGNITION_ADD_PLUGIN(Label, System, Label::ISystemConfigure)
IGNITION_ADD_PLUGIN_ALIAS(Label, "ignition::gazebo::systems::Label")
53 changes: 53 additions & 0 deletions src/systems/label/Label.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2018 Open Source Robotics Foundation
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
*
* 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_SYSTEMS_LABEL_HH_
#define IGNITION_GAZEBO_SYSTEMS_LABEL_HH_

#include <memory>
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
#include <ignition/gazebo/config.hh>
#include <ignition/gazebo/System.hh>
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved

namespace ignition
{
namespace gazebo
{
// Inline bracket to help doxygen filtering.
inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
namespace systems
{
/// \brief A label plugin that sets the label for the parent entity
class Label:
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
public System,
public ISystemConfigure
{
/// \brief Constructor
public: explicit Label();

/// \brief Destructor
public: ~Label() override;

/// Documentation inherited
AmrElsersy marked this conversation as resolved.
Show resolved Hide resolved
public: void Configure(const Entity &_entity,
const std::shared_ptr<const sdf::Element> &_sdf,
EntityComponentManager &_ecm,
gazebo::EventManager &_eventMgr) final;
};
}
}
}
}
#endif