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

3 ➡️ 4 #1181

Merged
merged 9 commits into from
Nov 10, 2021
1 change: 0 additions & 1 deletion include/ignition/gazebo/Util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ namespace ignition
/// \brief Environment variable holding paths to custom rendering engine
/// plugins.
const std::string kRenderPluginPathEnv{"IGN_GAZEBO_RENDER_ENGINE_PATH"};

}
}
}
Expand Down
6 changes: 4 additions & 2 deletions include/ignition/gazebo/rendering/RenderUtil.hh
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,13 @@ inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE {
/// \return Name of the rendering scene.
public: std::string SceneName() const;

/// \brief Set background color of render window
/// \brief Set background color of render window. This will override
/// other sources, such as from SDF.
/// \param[in] _color Color of render window background
public: void SetBackgroundColor(const math::Color &_color);

/// \brief Set ambient light of render window
/// \brief Set ambient light of render window. This will override
/// other sources, such as from SDF.
/// \param[in] _ambient Color of ambient light
public: void SetAmbientLight(const math::Color &_ambient);

Expand Down
49 changes: 17 additions & 32 deletions src/EntityComponentManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <map>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
Expand Down Expand Up @@ -1313,53 +1315,36 @@ void EntityComponentManager::SetState(
continue;
}

// Create component
auto newComp = components::Factory::Instance()->New(compMsg.type());

if (nullptr == newComp)
{
ignerr << "Failed to deserialize component of type [" << compMsg.type()
<< "]" << std::endl;
continue;
}

std::istringstream istr(compMsg.component());
newComp->Deserialize(istr);

// Get type id
auto typeId = newComp->TypeId();

// TODO(louise) Move into if, see TODO below
this->RemoveComponent(entity, typeId);

// Remove component
if (compMsg.remove())
{
this->RemoveComponent(entity, type);
continue;
}

// Get Component
auto comp = this->ComponentImplementation(entity, typeId);
auto comp = this->ComponentImplementation(entity, type);

std::istringstream istr(compMsg.component());

// Create if new
if (nullptr == comp)
{
this->CreateComponentImplementation(entity, typeId, newComp.get());
auto newComp = components::Factory::Instance()->New(type);
if (nullptr == newComp)
{
ignerr << "Failed to create component type ["
<< compMsg.type() << "]" << std::endl;
continue;
}
newComp->Deserialize(istr);
this->CreateComponentImplementation(entity, type, newComp.get());
}
// Update component value
else
{
ignerr << "Internal error" << std::endl;
// TODO(louise) We're shortcutting above and always removing the
// component so that we don't get here, gotta figure out why this
// doesn't update the component. The following line prints the correct
// values.
// igndbg << *comp << " " << *newComp.get() << std::endl;
// *comp = *newComp.get();

// When above TODO is addressed, uncomment AddModifiedComponent below
// unless calling SetChanged (which already calls AddModifiedComponent)
// this->dataPtr->AddModifiedComponent(entity);
comp->Deserialize(istr);
this->dataPtr->AddModifiedComponent(entity);
chapulina marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
96 changes: 96 additions & 0 deletions src/EntityComponentManager_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2629,6 +2629,102 @@ TEST_P(EntityComponentManagerFixture, PinnedEntity)
EXPECT_EQ(0u, manager.EntityCount());
}

//////////////////////////////////////////////////
/// \brief Test using msgs::SerializedStateMap and msgs::SerializedState
/// to update existing component data between multiple ECMs
TEST_P(EntityComponentManagerFixture, StateMsgUpdateComponent)
{
// create 2 ECMs: one will be modified directly, and the other should be
// updated to match the first via msgs::SerializedStateMap
EntityComponentManager originalECMStateMap;
EntityComponentManager otherECMStateMap;

// create an entity and component
auto entity = originalECMStateMap.CreateEntity();
originalECMStateMap.CreateComponent(entity, components::IntComponent(1));

int foundEntities = 0;
otherECMStateMap.Each<components::IntComponent>(
[&](const Entity &, const components::IntComponent *)
{
foundEntities++;
return true;
});
EXPECT_EQ(0, foundEntities);

// update the other ECM to have the new entity and component
msgs::SerializedStateMap stateMapMsg;
originalECMStateMap.State(stateMapMsg);
otherECMStateMap.SetState(stateMapMsg);
foundEntities = 0;
otherECMStateMap.Each<components::IntComponent>(
[&](const Entity &, const components::IntComponent *_intComp)
{
foundEntities++;
EXPECT_EQ(1, _intComp->Data());
return true;
});
EXPECT_EQ(1, foundEntities);

// modify a component and then share the update with the other ECM
stateMapMsg.Clear();
originalECMStateMap.SetComponentData<components::IntComponent>(entity, 2);
originalECMStateMap.State(stateMapMsg);
otherECMStateMap.SetState(stateMapMsg);
foundEntities = 0;
otherECMStateMap.Each<components::IntComponent>(
[&](const Entity &, const components::IntComponent *_intComp)
{
foundEntities++;
EXPECT_EQ(2, _intComp->Data());
return true;
});
EXPECT_EQ(1, foundEntities);

// Run the same test as above, but this time, use a msgs::SerializedState
// instead of a msgs::SerializedStateMap
EntityComponentManager originalECMState;
EntityComponentManager otherECMState;

foundEntities = 0;
otherECMState.Each<components::IntComponent>(
[&](const Entity &, const components::IntComponent *)
{
foundEntities++;
return true;
});
EXPECT_EQ(0, foundEntities);

entity = originalECMState.CreateEntity();
originalECMState.CreateComponent(entity, components::IntComponent(1));

auto stateMsg = originalECMState.State();
otherECMState.SetState(stateMsg);
foundEntities = 0;
otherECMState.Each<components::IntComponent>(
[&](const Entity &, const components::IntComponent *_intComp)
{
foundEntities++;
EXPECT_EQ(1, _intComp->Data());
return true;
});
EXPECT_EQ(1, foundEntities);

stateMsg.Clear();
originalECMState.SetComponentData<components::IntComponent>(entity, 2);
stateMsg = originalECMState.State();
otherECMState.SetState(stateMsg);
foundEntities = 0;
otherECMState.Each<components::IntComponent>(
[&](const Entity &, const components::IntComponent *_intComp)
{
foundEntities++;
EXPECT_EQ(2, _intComp->Data());
return true;
});
EXPECT_EQ(1, foundEntities);
}

// Run multiple times. We want to make sure that static globals don't cause
// problems.
INSTANTIATE_TEST_SUITE_P(EntityComponentManagerRepeat,
Expand Down
78 changes: 76 additions & 2 deletions src/gui/plugins/component_inspector/ComponentInspector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@
#include "ignition/gazebo/components/Actor.hh"
#include "ignition/gazebo/components/AngularAcceleration.hh"
#include "ignition/gazebo/components/AngularVelocity.hh"
#include "ignition/gazebo/components/BatterySoC.hh"
#include "ignition/gazebo/components/CastShadows.hh"
#include "ignition/gazebo/components/CenterOfVolume.hh"
#include "ignition/gazebo/components/ChildLinkName.hh"
#include "ignition/gazebo/components/Collision.hh"
#include "ignition/gazebo/components/Factory.hh"
#include "ignition/gazebo/components/Gravity.hh"
#include "ignition/gazebo/components/Joint.hh"
#include "ignition/gazebo/components/LaserRetro.hh"
#include "ignition/gazebo/components/Level.hh"
#include "ignition/gazebo/components/Light.hh"
#include "ignition/gazebo/components/LightCmd.hh"
Expand All @@ -56,7 +59,10 @@
#include "ignition/gazebo/components/Sensor.hh"
#include "ignition/gazebo/components/SourceFilePath.hh"
#include "ignition/gazebo/components/Static.hh"
#include "ignition/gazebo/components/ThreadPitch.hh"
#include "ignition/gazebo/components/Transparency.hh"
#include "ignition/gazebo/components/Visual.hh"
#include "ignition/gazebo/components/Volume.hh"
#include "ignition/gazebo/components/WindMode.hh"
#include "ignition/gazebo/components/World.hh"
#include "ignition/gazebo/EntityComponentManager.hh"
Expand Down Expand Up @@ -234,6 +240,13 @@ void ignition::gazebo::setData(QStandardItem *_item, const int &_data)
_item->setData(_data, ComponentsModel::RoleNames().key("data"));
}

//////////////////////////////////////////////////
template<>
void ignition::gazebo::setData(QStandardItem *_item, const Entity &_data)
{
setData(_item, static_cast<int>(_data));
}

//////////////////////////////////////////////////
template<>
void ignition::gazebo::setData(QStandardItem *_item, const double &_data)
Expand Down Expand Up @@ -393,6 +406,7 @@ ComponentInspector::ComponentInspector()
: GuiSystem(), dataPtr(std::make_unique<ComponentInspectorPrivate>())
{
qRegisterMetaType<ignition::gazebo::ComponentTypeId>();
qRegisterMetaType<Entity>("Entity");
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -550,13 +564,30 @@ void ComponentInspector::Update(const UpdateInfo &,
if (comp)
setData(item, comp->Data());
}
else if (typeId == components::BatterySoC::typeId)
{
auto comp = _ecm.Component<components::BatterySoC>(
this->dataPtr->entity);
if (comp)
setData(item, comp->Data());
}
else if (typeId == components::CastShadows::typeId)
{
auto comp = _ecm.Component<components::CastShadows>(
this->dataPtr->entity);
if (comp)
setData(item, comp->Data());
}
else if (typeId == components::CenterOfVolume::typeId)
{
auto comp = _ecm.Component<components::CenterOfVolume>(
this->dataPtr->entity);
if (comp)
{
setData(item, comp->Data());
setUnit(item, "m");
}
}
else if (typeId == components::ChildLinkName::typeId)
{
auto comp = _ecm.Component<components::ChildLinkName>(
Expand All @@ -573,6 +604,12 @@ void ComponentInspector::Update(const UpdateInfo &,
setUnit(item, "m/s\u00B2");
}
}
else if (typeId == components::LaserRetro::typeId)
{
auto comp = _ecm.Component<components::LaserRetro>(this->dataPtr->entity);
if (comp)
setData(item, comp->Data());
}
else if (typeId == components::LinearAcceleration::typeId)
{
auto comp = _ecm.Component<components::LinearAcceleration>(
Expand Down Expand Up @@ -683,6 +720,33 @@ void ComponentInspector::Update(const UpdateInfo &,
if (comp)
setData(item, comp->Data());
}
else if (typeId == components::ThreadPitch::typeId)
{
auto comp = _ecm.Component<components::ThreadPitch>(
this->dataPtr->entity);
if (comp)
{
setData(item, comp->Data());
setUnit(item, "m");
}
}
else if (typeId == components::Transparency::typeId)
{
auto comp = _ecm.Component<components::Transparency>(
this->dataPtr->entity);
if (comp)
setData(item, comp->Data());
}
else if (typeId == components::Volume::typeId)
{
auto comp = _ecm.Component<components::Volume>(
this->dataPtr->entity);
if (comp)
{
setData(item, comp->Data());
setUnit(item, "m\u00B3");
}
}
else if (typeId == components::WindMode::typeId)
{
auto comp = _ecm.Component<components::WindMode>(this->dataPtr->entity);
Expand All @@ -699,6 +763,16 @@ void ComponentInspector::Update(const UpdateInfo &,
setUnit(item, "rad/s\u00B2");
}
}
else if (typeId == components::WorldAngularVelocity::typeId)
{
auto comp = _ecm.Component<components::WorldAngularVelocity>(
this->dataPtr->entity);
if (comp)
{
setData(item, comp->Data());
setUnit(item, "rad/s");
}
}
else if (typeId == components::WorldLinearVelocity::typeId)
{
auto comp = _ecm.Component<components::WorldLinearVelocity>(
Expand Down Expand Up @@ -787,13 +861,13 @@ bool ComponentInspector::eventFilter(QObject *_obj, QEvent *_event)
}

/////////////////////////////////////////////////
int ComponentInspector::Entity() const
Entity ComponentInspector::GetEntity() const
{
return this->dataPtr->entity;
}

/////////////////////////////////////////////////
void ComponentInspector::SetEntity(const int &_entity)
void ComponentInspector::SetEntity(const Entity &_entity)
{
// If nothing is selected, display world properties
if (_entity == kNullEntity)
Expand Down
8 changes: 4 additions & 4 deletions src/gui/plugins/component_inspector/ComponentInspector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ namespace gazebo

/// \brief Entity
Q_PROPERTY(
int entity
READ Entity
Entity entity
READ GetEntity
WRITE SetEntity
NOTIFY EntityChanged
)
Expand Down Expand Up @@ -307,11 +307,11 @@ namespace gazebo

/// \brief Get the entity currently inspected.
/// \return Entity ID.
public: Q_INVOKABLE int Entity() const;
public: Q_INVOKABLE Entity GetEntity() const;

/// \brief Set the entity currently inspected.
/// \param[in] _entity Entity ID.
public: Q_INVOKABLE void SetEntity(const int &_entity);
public: Q_INVOKABLE void SetEntity(const Entity &_entity);

/// \brief Notify that entity has changed.
signals: void EntityChanged();
Expand Down
2 changes: 2 additions & 0 deletions src/gui/plugins/component_inspector/ComponentInspector.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<qresource prefix="ComponentInspector/">
<file>Boolean.qml</file>
<file>ComponentInspector.qml</file>
<file>Float.qml</file>
<file>Integer.qml</file>
<file>Light.qml</file>
<file>NoData.qml</file>
<file>Material.qml</file>
Expand Down
Loading