Skip to content

Commit

Permalink
MaterialColor test world and line fix.
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Perseghetti <bperseghetti@rudislabs.com>
  • Loading branch information
bperseghetti committed Jan 20, 2024
1 parent f8f23f9 commit 0e23d42
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/systems/user_commands/UserCommands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ void UserCommands::Configure(const Entity &_entity,
this->dataPtr->node.Subscribe(lightTopic, &UserCommandsPrivate::OnCmdLight,
this->dataPtr.get());

std::string materialColorTopic{"/world/" + validWorldName +"/material_color"};
std::string materialColorTopic{
"/world/" + validWorldName + "/material_color"};
this->dataPtr->node.Subscribe(materialColorTopic,
&UserCommandsPrivate::OnCmdMaterialColor, this->dataPtr.get());

Expand Down
40 changes: 22 additions & 18 deletions test/integration/user_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ TEST_F(UserCommandsTest, GZ_UTILS_TEST_ENABLED_ONLY_ON_LINUX(MaterialColor))
// Start server
ServerConfig serverConfig;
const auto sdfFile = gz::common::joinPaths(
std::string(PROJECT_SOURCE_PATH), "test", "worlds", "shapes.sdf");
std::string(PROJECT_SOURCE_PATH), "test", "worlds", "material_color.sdf");
serverConfig.SetSdfFile(sdfFile);

Server server(serverConfig);
Expand All @@ -1078,39 +1078,43 @@ TEST_F(UserCommandsTest, GZ_UTILS_TEST_ENABLED_ONLY_ON_LINUX(MaterialColor))

transport::Node node;

// Camera Ball
auto boxVisualEntity =
ecm->EntityByComponents(components::Name("box_visual"));
ASSERT_NE(kNullEntity, boxVisualEntity);
// box
auto sphereVisualEntity =
ecm->EntityByComponents(components::Name("sphere_visual"));
ASSERT_NE(kNullEntity, sphereVisualEntity);

// check box visual's initial values
auto boxVisualComp = ecm->Component<components::Material>(boxVisualEntity);
ASSERT_NE(nullptr, boxVisualComp);
EXPECT_EQ(math::Color(1.0f, 0.0f, 0.0f, 1.0f),
boxVisualComp->Data().Diffuse());
auto sphereVisualComp =
ecm->Component<components::Material>(sphereVisualEntity);
ASSERT_NE(nullptr, sphereVisualComp);
EXPECT_EQ(math::Color(0.3f, 0.3f, 0.3f, 1.0f),
sphereVisualComp->Data().Diffuse());

// Test material_color topic
const std::string materialColorTopic = "/world/material_color/material_color";
const std::string materialColorTopic =
"/world/material_color/material_color";

msgs::MaterialColor materialColorMsg;
materialColorMsg.mutable_entity()->set_name("box::box_link::box_visual");
materialColorMsg.mutable_entity()->set_name("sphere_visual");
materialColorMsg.set_entity_match(
gz::msgs::MaterialColor::EntityMatch::MaterialColor_EntityMatch_ALL);
gz::msgs::Set(materialColorMsg.mutable_diffuse(),
gz::math::Color(1.0f, 1.0f, 1.0f, 1.0f));

// Publish material color
auto pub = node.Advertise<msgs::MaterialColor>(materialColorTopic);
pub.Publish(materialColorMsg);
GZ_SLEEP_MS(100);
pub.Publish(materialColorMsg);

server.Run(true, 100, false);
// Sleep for a small duration to allow Run thread to start
GZ_SLEEP_MS(10);
GZ_SLEEP_MS(100);

auto boxVisCmdComp = ecm->Component<components::VisualCmd>(boxVisualEntity);
ASSERT_NE(nullptr, boxVisualComp);
EXPECT_FLOAT_EQ(0.0f, boxVisCmdComp->Data().material().diffuse().r());
EXPECT_FLOAT_EQ(0.0f, boxVisCmdComp->Data().material().diffuse().g());
EXPECT_FLOAT_EQ(1.0f, boxVisCmdComp->Data().material().diffuse().b());
EXPECT_FLOAT_EQ(1.0f, boxVisCmdComp->Data().material().diffuse().a());
auto updatedVisual =
ecm->Component<components::Material>(sphereVisualEntity);
EXPECT_EQ(math::Color(1.0f, 1.0f, 1.0f, 1.0f),
updatedVisual->Data().Diffuse());
}

/////////////////////////////////////////////////
Expand Down
76 changes: 76 additions & 0 deletions test/worlds/material_color.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" ?>
<sdf version="1.10">
<world name="material_color">
<physics name="1ms" type="ode">
<max_step_size>0.001</max_step_size>
<real_time_factor>0</real_time_factor>
</physics>
<plugin
filename="gz-sim-scene-broadcaster-system"
name="gz::sim::systems::SceneBroadcaster">
</plugin>
<plugin
filename="gz-sim-user-commands-system"
name="gz::sim::systems::UserCommands">
</plugin>
<plugin
filename="gz-sim-sensors-system"
name="gz::sim::systems::Sensors">
<render_engine>ogre2</render_engine>
</plugin>

<model name="sphere">
<pose>0 0.0 0.0 0 0 0</pose>
<link name="sphere_link">
<!-- Added a render sensor to trigger RenderUtil:Update -->
<sensor name="camera" type="camera">
<pose>1 0 1.3 0 0 0</pose>
<camera>
<horizontal_fov>1.047</horizontal_fov>
<image>
<width>320</width>
<height>240</height>
</image>
<clip>
<near>0.1</near>
<far>100</far>
</clip>
</camera>
<always_on>1</always_on>
<update_rate>30</update_rate>
<visualize>false</visualize>
<topic>camera</topic>
</sensor>
<visual name="sphere_visual">
<geometry>
<sphere>
<radius>0.5</radius>
</sphere>
</geometry>
<material>
<ambient>0.3 0.3 0.3 1</ambient>
<diffuse>0.3 0.3 0.3 1</diffuse>
<specular>0.3 0.3 0.3 1</specular>
</material>
</visual>
</link>
</model>
<model name="sphere_1">
<pose>0.5 1.0 0.0 0 0 0</pose>
<link name="sphere_link">
<visual name="sphere_visual">
<geometry>
<sphere>
<radius>0.5</radius>
</sphere>
</geometry>
<material>
<ambient>0.3 0.3 0.3 1</ambient>
<diffuse>0.3 0.3 0.3 1</diffuse>
<specular>0.3 0.3 0.3 1</specular>
</material>
</visual>
</link>
</model>
</world>
</sdf>

0 comments on commit 0e23d42

Please sign in to comment.