-
Notifications
You must be signed in to change notification settings - Fork 98
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
+277
−17
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8359bbf
updated loading //material colors
jennuine 17f13cb
Merge branch 'master' into jennuine/material_colors
jennuine f4633ca
added error when color element is empty
jennuine 74ab6b4
Merge branch 'master' into jennuine/material_colors
jennuine 39c05a5
moved color parsing to Param
jennuine dbf7614
specified exceptions
jennuine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* Copyright 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 <string> | ||
#include <gtest/gtest.h> | ||
|
||
#include <ignition/math/Color.hh> | ||
|
||
#include "sdf/sdf.hh" | ||
#include "test_config.h" | ||
|
||
void ExpectInvalidWithMessage(sdf::Errors &_errors, std::string _compType) | ||
{ | ||
for (const auto &e : _errors) | ||
{ | ||
if (e.Message().find(_compType) != std::string::npos) | ||
{ | ||
EXPECT_EQ(e.Code(), sdf::ErrorCode::ELEMENT_INVALID); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
////////////////////////////////////////////////// | ||
TEST(Material, InvalidColors) | ||
{ | ||
std::string testFile = | ||
sdf::testing::TestFile("sdf", "material_invalid.sdf"); | ||
|
||
sdf::Root root; | ||
sdf::Errors errors = root.Load(testFile); | ||
std::cout << errors << std::endl; | ||
|
||
ASSERT_FALSE(errors.empty()); | ||
ExpectInvalidWithMessage(errors, "ambient"); | ||
|
||
// since the above test will break at //ambient, it doesn't test the other | ||
// invalid cases. Below tests these other invalid cases | ||
|
||
// less than 3 values | ||
std::string testSDF = R"( | ||
<?xml version="1.0" ?> | ||
<sdf version="1.8"> | ||
<model name="model"> | ||
<link name="link"> | ||
<visual name="material"> | ||
<material> | ||
<specular>0 0.1</specular> | ||
</material> | ||
</visual> | ||
</link> | ||
</model> | ||
</sdf>)"; | ||
|
||
errors.clear(); | ||
errors = root.LoadSdfString(testSDF); | ||
std::cout << errors << std::endl; | ||
|
||
ASSERT_FALSE(errors.empty()); | ||
ExpectInvalidWithMessage(errors, "specular"); | ||
|
||
// negative value | ||
testSDF = R"( | ||
<?xml version="1.0" ?> | ||
<sdf version="1.8"> | ||
<model name="model"> | ||
<link name="link"> | ||
<visual name="material"> | ||
<material> | ||
<emissive>0.1 0.2 -1</emissive> | ||
</material> | ||
</visual> | ||
</link> | ||
</model> | ||
</sdf>)"; | ||
|
||
errors.clear(); | ||
errors = root.LoadSdfString(testSDF); | ||
std::cout << errors << std::endl; | ||
|
||
ASSERT_FALSE(errors.empty()); | ||
ExpectInvalidWithMessage(errors, "emissive"); | ||
|
||
// more than 4 values | ||
testSDF = R"( | ||
<?xml version="1.0" ?> | ||
<sdf version="1.8"> | ||
<model name="model"> | ||
<link name="link"> | ||
<visual name="material"> | ||
<material> | ||
<diffuse>0.1 0.2 0.3 0.4 0.5</diffuse> | ||
</material> | ||
</visual> | ||
</link> | ||
</model> | ||
</sdf>)"; | ||
|
||
errors.clear(); | ||
errors = root.LoadSdfString(testSDF); | ||
std::cout << errors << std::endl; | ||
|
||
ASSERT_FALSE(errors.empty()); | ||
ExpectInvalidWithMessage(errors, "diffuse"); | ||
|
||
// invalid string value | ||
testSDF = R"( | ||
<?xml version="1.0" ?> | ||
<sdf version="1.8"> | ||
<model name="model"> | ||
<link name="link"> | ||
<visual name="material"> | ||
<material> | ||
<ambient>0.1 0.2 test</ambient> | ||
</material> | ||
</visual> | ||
</link> | ||
</model> | ||
</sdf>)"; | ||
|
||
errors.clear(); | ||
errors = root.LoadSdfString(testSDF); | ||
std::cout << errors << std::endl; | ||
|
||
ASSERT_FALSE(errors.empty()); | ||
ExpectInvalidWithMessage(errors, "ambient"); | ||
} | ||
|
||
////////////////////////////////////////////////// | ||
TEST(Material, ValidColors) | ||
{ | ||
std::string testFile = | ||
sdf::testing::TestFile("sdf", "material_valid.sdf"); | ||
|
||
sdf::Root root; | ||
sdf::Errors errors = root.Load(testFile); | ||
std::cout << errors << std::endl; | ||
|
||
ASSERT_TRUE(errors.empty()); | ||
|
||
sdf::ElementPtr elem = root.Element()->GetElement("model") | ||
->GetElement("link") | ||
->GetElement("visual") | ||
->GetElement("material"); | ||
ASSERT_NE(elem, nullptr); | ||
|
||
EXPECT_EQ(elem->Get<ignition::math::Color>("diffuse"), | ||
ignition::math::Color(0, 0.1f, 0.2f, 1)); | ||
EXPECT_EQ(elem->Get<ignition::math::Color>("specular"), | ||
ignition::math::Color(0, 0.1f, 0.2f, 0.3f)); | ||
EXPECT_EQ(elem->Get<ignition::math::Color>("emissive"), | ||
ignition::math::Color(0.12f, 0.23f, 0.34f, 0.56f)); | ||
EXPECT_EQ(elem->Get<ignition::math::Color>("ambient"), | ||
ignition::math::Color(0, 0, 0, 1)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" ?> | ||
<sdf version="1.8"> | ||
<model name="model"> | ||
<link name="link"> | ||
<visual name="material"> | ||
<material> | ||
<ambient>1.5 0 0 1</ambient> | ||
<specular>0 0.1</specular> | ||
<emissive>0.1 0.2 -1</emissive> | ||
<diffuse>0.1 0.2 0.3 0.4 0.5</diffuse> | ||
</material> | ||
</visual> | ||
</link> | ||
</model> | ||
</sdf> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" ?> | ||
<sdf version="1.8"> | ||
<model name="model"> | ||
<link name="link"> | ||
<visual name="material"> | ||
<material> | ||
<diffuse>0 0.1 0.2</diffuse> | ||
<specular>0 0.1 0.2 0.3</specular> | ||
<emissive>0.12 0.23 0.34 0.56</emissive> | ||
</material> | ||
</visual> | ||
</link> | ||
</model> | ||
</sdf> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the caught exception be more specific like the ones in
ValueFromString
https://github.com/osrf/sdformat/blob/deb142130c6fb96b24a2e0f35e481e34ac014834/src/Param.cc#L509-L520
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup! dbf7614