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

updated loading //material colors #519

Merged
merged 6 commits into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
90 changes: 82 additions & 8 deletions src/Material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*
*/
#include <sstream>
#include <string>
#include <optional>
#include <vector>
Expand Down Expand Up @@ -69,6 +70,18 @@ class sdf::Material::Implementation

/// \brief The path to the file where this material was defined.
public: std::string filePath = "";

/// \brief Get a valid color. This checks the color components specified in
/// this->sdf are r,g,b or r,g,b,a (expects 3 or 4 values) and each value is
/// between [0,1]. When only 3 values are present (r,g,b), then alpha is set
/// to 1. If the checks do not pass, the default color 0,0,0,1 is returned.
/// \param[in] _colorType The color type (i.e., ambient, diffuse, specular,
/// or emissive), which is case sensitive and expects a lowercase string
/// \param[out] _errors Parsing errors will be appended to this variable
/// \return A color for _colorType. If no parsing errors occurred then the
/// specified color component from this->sdf is returned, otherwise 0,0,0,1
public: ignition::math::Color GetColor(const std::string &_colorType,
Errors &_errors);
};

/////////////////////////////////////////////////
Expand Down Expand Up @@ -168,17 +181,13 @@ Errors Material::Load(sdf::ElementPtr _sdf)
this->dataPtr->renderOrder = _sdf->Get<float>("render_order",
this->dataPtr->renderOrder).first;

this->dataPtr->ambient = _sdf->Get<ignition::math::Color>("ambient",
this->dataPtr->ambient).first;
this->dataPtr->ambient = this->dataPtr->GetColor("ambient", errors);

this->dataPtr->diffuse = _sdf->Get<ignition::math::Color>("diffuse",
this->dataPtr->diffuse).first;
this->dataPtr->diffuse = this->dataPtr->GetColor("diffuse", errors);

this->dataPtr->specular = _sdf->Get<ignition::math::Color>("specular",
this->dataPtr->specular).first;
this->dataPtr->specular = this->dataPtr->GetColor("specular", errors);

this->dataPtr->emissive = _sdf->Get<ignition::math::Color>("emissive",
this->dataPtr->emissive).first;
this->dataPtr->emissive = this->dataPtr->GetColor("emissive", errors);

this->dataPtr->lighting = _sdf->Get<bool>("lighting",
this->dataPtr->lighting).first;
Expand Down Expand Up @@ -358,3 +367,68 @@ void Material::SetFilePath(const std::string &_filePath)
{
this->dataPtr->filePath = _filePath;
}

//////////////////////////////////////////////////
ignition::math::Color Material::Implementation::GetColor(
const std::string &_colorType, Errors &_errors)
{
ignition::math::Color color(0, 0, 0, 1);

if (!this->sdf->HasElement(_colorType))
return color;
adlarkin marked this conversation as resolved.
Show resolved Hide resolved

std::string colorStr = this->sdf->GetElement(_colorType)
->GetValue()
->GetAsString();
if (colorStr.empty())
{
_errors.push_back({ErrorCode::ELEMENT_INVALID, "No values found inside <" +
_colorType + "> element. Using default values 0 0 0 1."});
return color;
}

std::stringstream ss(colorStr);
std::string token;
std::vector<float> colors;
float c; // r,g,b,a values
bool isValidColor = true;
while (ss >> token)
{
try
{
c = std::stof(token);
colors.push_back(c);
}
catch(const std::exception &/*e*/)
{
std::cerr << "Error converting color value: can not convert ["
<< token << "] to a float" << std::endl;
isValidColor = false;
break;
}

if (c < 0.0f || c > 1.0f)
{
isValidColor = false;
break;
}
}

size_t colorSize = colors.size();
if (colorSize == 3u)
colors.push_back(1.0f);
else if (colorSize != 4u)
isValidColor = false;

if (!isValidColor)
{
_errors.push_back({ErrorCode::ELEMENT_INVALID, "The value <" + _colorType +
">" + colorStr + "</" + _colorType + "> is invalid. " +
"Using default values 0 0 0 1 for <" + _colorType + "> element."});
return color;
}

color.Set(colors[0], colors[1], colors[2], colors[3]);

return color;
}
101 changes: 101 additions & 0 deletions src/Material_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,104 @@ TEST(DOMMaterial, InvalidSdf)
sdf::Errors errors = material.Load(elem);
EXPECT_EQ(sdf::ErrorCode::ELEMENT_INCORRECT_TYPE, errors[0].Code());
}

void PrintErrors(sdf::Errors &_errors)
{
for (const auto &e : _errors)
std::cout << e << std::endl;
}

/////////////////////////////////////////////////
TEST(DOMMaterial, Colors)
{
sdf::ElementPtr elem(new sdf::Element());
elem->SetName("material");

// invalid diffuse (empty)
sdf::ElementPtr elemDiffuse(new sdf::Element());
elemDiffuse->SetName("diffuse");
elemDiffuse->AddValue("string", "", false, "description");
elem->InsertElement(elemDiffuse);

sdf::Material material;
sdf::Errors errors = material.Load(elem);
PrintErrors(errors);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tip: You should be able to do std::cout << errors << std::endl;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!


ASSERT_EQ(errors.size(), 1u);
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::ELEMENT_INVALID);
EXPECT_EQ(material.Ambient(), ignition::math::Color(0, 0, 0, 1));
EXPECT_EQ(material.Diffuse(), ignition::math::Color(0, 0, 0, 1));
EXPECT_EQ(material.Specular(), ignition::math::Color(0, 0, 0, 1));
EXPECT_EQ(material.Emissive(), ignition::math::Color(0, 0, 0, 1));

// another invalid diffuse (value is > 1)
elemDiffuse->Set<std::string>("1.5 0 0 1");
errors.clear();
errors = material.Load(elem);
PrintErrors(errors);

ASSERT_EQ(errors.size(), 1u);
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::ELEMENT_INVALID);
EXPECT_EQ(material.Diffuse(), ignition::math::Color(0, 0, 0, 1));

// another invalid diffuse (only 2 values)
elemDiffuse->Set<std::string>("0.1 0.2");
errors.clear();
errors = material.Load(elem);
PrintErrors(errors);

ASSERT_EQ(errors.size(), 1u);
EXPECT_EQ(errors[0].Code(), sdf::ErrorCode::ELEMENT_INVALID);
EXPECT_EQ(material.Diffuse(), ignition::math::Color(0, 0, 0, 1));

// invalid ambient
sdf::ElementPtr elemAmbient(new sdf::Element());
elemAmbient->AddValue("string", "", false, "description");
elemAmbient->SetName("ambient");
elemAmbient->Set<std::string>("0.1 0.2 test");
elem->InsertElement(elemAmbient);

// invalid specular
sdf::ElementPtr elemSpecular(new sdf::Element());
elemSpecular->AddValue("string", "", false, "description");
elemSpecular->SetName("specular");
elemSpecular->Set<std::string>("0.1 0.2 0.3 0.4 0.5");
elem->InsertElement(elemSpecular);

// invalid emissive
sdf::ElementPtr elemEmissive(new sdf::Element());
elemEmissive->AddValue("string", "", false, "description");
elemEmissive->SetName("emissive");
elemEmissive->Set<std::string>("-0.1 0.2 0.3 0.4");
elem->InsertElement(elemEmissive);

errors.clear();
errors = material.Load(elem);
PrintErrors(errors);

EXPECT_EQ(errors.size(), 4u);
for (const auto &e : errors)
EXPECT_EQ(e.Code(), sdf::ErrorCode::ELEMENT_INVALID);
adlarkin marked this conversation as resolved.
Show resolved Hide resolved

EXPECT_EQ(material.Ambient(), ignition::math::Color(0, 0, 0, 1));
EXPECT_EQ(material.Diffuse(), ignition::math::Color(0, 0, 0, 1));
EXPECT_EQ(material.Specular(), ignition::math::Color(0, 0, 0, 1));
EXPECT_EQ(material.Emissive(), ignition::math::Color(0, 0, 0, 1));

// valid diffuse, ambient, specular, emissive
elemDiffuse->Set<std::string>("0 0.1 0.2");
elemAmbient->Set<std::string>("0.3 0.4 0.55 1");
elemSpecular->Set<std::string>("0 0.1 0.2 0.3");
elemEmissive->Set<std::string>("0.12 0.23 0.34 0.56");

errors.clear();
errors = material.Load(elem);
PrintErrors(errors);

EXPECT_EQ(errors.size(), 0u);
EXPECT_EQ(material.Diffuse(), ignition::math::Color(0, 0.1f, 0.2f, 1));
EXPECT_EQ(material.Ambient(), ignition::math::Color(0.3f, 0.4f, 0.55f, 1));
EXPECT_EQ(material.Specular(), ignition::math::Color(0, 0.1f, 0.2f, 0.3f));
EXPECT_EQ(material.Emissive(),
ignition::math::Color(0.12f, 0.23f, 0.34f, 0.56f));
}