Skip to content

Commit

Permalink
intends, default case, invalid color
Browse files Browse the repository at this point in the history
Signed-off-by: Dharini Dutia <[email protected]>
  • Loading branch information
quarkytale committed Jan 4, 2024
1 parent 7b08ebf commit 7621715
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/SdfEntityCreator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,11 @@ Entity SdfEntityCreator::CreateEntities(const sdf::Visual *_visual)
MaterialParser::MaterialValues parsed =
this->dataPtr->materialParser.GetMaterialValues(scriptName);

if(!parsed.ambient.has_value()) {
gzwarn << "Material " << scriptName <<
" not recognized, using default."<< std::endl;
}

visualMaterial.SetAmbient
(parsed.ambient.value_or(visualMaterial.Ambient()));
visualMaterial.SetDiffuse
Expand Down
3 changes: 3 additions & 0 deletions src/rendering/MaterialParser/ConfigLoader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ void ConfigLoader::_parseNodes(std::ifstream & stream, ConfigNode * parent)
case TOKEN_NewLine:
_nextToken(stream);
break;

default:
break;

Check warning on line 272 in src/rendering/MaterialParser/ConfigLoader.cc

View check run for this annotation

Codecov / codecov/patch

src/rendering/MaterialParser/ConfigLoader.cc#L271-L272

Added lines #L271 - L272 were not covered by tests
}
}
}
Expand Down
28 changes: 14 additions & 14 deletions src/rendering/MaterialParser/ConfigLoader.hh
Original file line number Diff line number Diff line change
Expand Up @@ -90,40 +90,40 @@ public:

inline void setName(const std::string & name)
{
this->m_name = name;
this->m_name = name;
}

inline std::string & getName()
{
return m_name;
return m_name;
}

inline void addValue(const std::string & value)
{
m_values.push_back(value);
m_values.push_back(value);
}

inline void clearValues()
{
m_values.clear();
m_values.clear();
}

inline std::vector<std::string> & getValues()
{
return m_values;
return m_values;
}

inline void getValuesInFloat(std::vector<float> & floatValues)
{
for (const auto & str : m_values) {
floatValues.push_back(std::stof(str));
}
for (const auto & str : m_values) {
floatValues.push_back(std::stof(str));
}
}

inline const std::string & getValue(unsigned int index = 0)
{
assert(index < m_values.size());
return m_values[index];
assert(index < m_values.size());
return m_values[index];
}

ConfigNode * addChild(
Expand All @@ -133,20 +133,20 @@ public:

inline std::vector<ConfigNode *> & getChildren()
{
return m_children;
return m_children;
}

inline ConfigNode * getChild(unsigned int index = 0)
{
assert(index < m_children.size());
return m_children[index];
assert(index < m_children.size());
return m_children[index];
}

void setParent(ConfigNode * newParent);

inline ConfigNode * getParent()
{
return m_parent;
return m_parent;
}

private:
Expand Down
51 changes: 51 additions & 0 deletions test/integration/material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class MaterialTest : public InternalFixture<::testing::Test>
public: std::unique_ptr<SdfEntityCreator> creator;
};

// Check for blue color parsed
TEST_F(MaterialTest, SolidColor)
{
const std::string modelSdf = R"sdf(
Expand Down Expand Up @@ -152,6 +153,7 @@ TEST_F(MaterialTest, SolidColor)
boxVisualComp->Data().Specular());
}

// Other than solid colors parsed black by default
TEST_F(MaterialTest, OtherColor)
{
const std::string modelSdf = R"sdf(
Expand Down Expand Up @@ -199,6 +201,7 @@ TEST_F(MaterialTest, OtherColor)
boxVisualComp->Data().Specular());
}

// Warning for custom scripts not supported, default to black
TEST_F(MaterialTest, CustomScript)
{
const std::string modelSdf = R"sdf(
Expand Down Expand Up @@ -245,3 +248,51 @@ TEST_F(MaterialTest, CustomScript)
EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f),
boxVisualComp->Data().Specular());
}

// Warning for invalid name, default to black
TEST_F(MaterialTest, InvalidColor)
{
const std::string modelSdf = R"sdf(
<sdf version="1.11">
<model name="material_shapes">
<pose>0 0 0.5 0 0 0</pose>
<link name="box">
<pose>0 -1.5 0 0 0 0</pose>
<visual name="box_visual">
<geometry>
<box>
<size>1 1 1</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Invalid</name>
</script>
</material>
</visual>
</link>
</model>
</sdf>
)sdf";

ASSERT_TRUE(this->StartServer());
this->SpawnModelSDF(modelSdf);

auto model = this->GetModel("material_shapes");
ASSERT_TRUE(model.Valid(*this->ecm));

auto boxVisualEntity =
this->ecm->EntityByComponents(components::Name("box_visual"));
ASSERT_NE(kNullEntity, boxVisualEntity);

// Default to black color
auto boxVisualComp =
this->ecm->Component<components::Material>(boxVisualEntity);
EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f),
boxVisualComp->Data().Ambient());
EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f),
boxVisualComp->Data().Diffuse());
EXPECT_EQ(math::Color(0.0f, 0.0f, 0.0f, 1.0f),
boxVisualComp->Data().Specular());
}

0 comments on commit 7621715

Please sign in to comment.