Skip to content

Commit

Permalink
Scene: update calls to use sdf::Errors parameters (#1164)
Browse files Browse the repository at this point in the history
Signed-off-by: Marco A. Gutierrez <[email protected]>
Co-authored-by: Addisu Z. Taddese <[email protected]>
  • Loading branch information
Marco A. Gutierrez and azeey authored Mar 31, 2023
1 parent 066f5a2 commit d044d5d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 12 deletions.
8 changes: 8 additions & 0 deletions include/sdf/Scene.hh
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ namespace sdf
/// \return SDF element pointer with updated scene values.
public: sdf::ElementPtr ToElement() const;

/// \brief Create and return an SDF element filled with data from this
/// scene.
/// Note that parameter passing functionality is not captured with this
/// function.
/// \param[out] _errors Vector of errors.
/// \return SDF element pointer with updated scene values.
public: sdf::ElementPtr ToElement(sdf::Errors &_errors) const;

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
35 changes: 23 additions & 12 deletions src/Scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,31 @@ Errors Scene::Load(ElementPtr _sdf, const ParserConfig &_config)
}

// Get the ambient property
this->dataPtr->ambient = _sdf->Get<gz::math::Color>("ambient",
this->dataPtr->ambient = _sdf->Get<gz::math::Color>(errors, "ambient",
this->dataPtr->ambient).first;

// Get the background color property
this->dataPtr->background = _sdf->Get<gz::math::Color>("background",
this->dataPtr->background = _sdf->Get<gz::math::Color>(errors, "background",
this->dataPtr->background).first;

// Get the grid property
this->dataPtr->grid = _sdf->Get<bool>("grid",
this->dataPtr->grid = _sdf->Get<bool>(errors, "grid",
this->dataPtr->grid).first;

// Get the shadows property
this->dataPtr->shadows = _sdf->Get<bool>("shadows",
this->dataPtr->shadows = _sdf->Get<bool>(errors, "shadows",
this->dataPtr->shadows).first;

// Get the origin_visual property
this->dataPtr->originVisual = _sdf->Get<bool>("origin_visual",
this->dataPtr->originVisual = _sdf->Get<bool>(errors, "origin_visual",
this->dataPtr->originVisual).first;

// load sky
if (_sdf->HasElement("sky"))
{
this->dataPtr->sky.emplace();
Errors err = this->dataPtr->sky->Load(_sdf->GetElement("sky"), _config);
Errors err = this->dataPtr->sky->Load(_sdf->GetElement(
"sky", errors), _config);
errors.insert(errors.end(), err.begin(), err.end());
}

Expand Down Expand Up @@ -186,18 +187,28 @@ sdf::ElementPtr Scene::Element() const

/////////////////////////////////////////////////
sdf::ElementPtr Scene::ToElement() const
{
sdf::Errors errors;
auto result = this->ToElement(errors);
sdf::throwOrPrintErrors(errors);
return result;
}

/////////////////////////////////////////////////
sdf::ElementPtr Scene::ToElement(sdf::Errors &_errors) const
{
sdf::ElementPtr elem(new sdf::Element);
sdf::initFile("scene.sdf", elem);

elem->GetElement("ambient")->Set(this->Ambient());
elem->GetElement("background")->Set(this->Background());
elem->GetElement("grid")->Set(this->Grid());
elem->GetElement("origin_visual")->Set(this->OriginVisual());
elem->GetElement("shadows")->Set(this->Shadows());
elem->GetElement("ambient", _errors)->Set(_errors, this->Ambient());
elem->GetElement("background", _errors)->Set(_errors, this->Background());
elem->GetElement("grid", _errors)->Set(_errors, this->Grid());
elem->GetElement("origin_visual", _errors)->Set(
_errors, this->OriginVisual());
elem->GetElement("shadows", _errors)->Set(_errors, this->Shadows());

if (this->dataPtr->sky)
elem->InsertElement(this->dataPtr->sky->ToElement(), true);
elem->InsertElement(this->dataPtr->sky->ToElement(_errors), true);

return elem;
}
50 changes: 50 additions & 0 deletions src/Scene_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <gtest/gtest.h>
#include "sdf/Scene.hh"
#include "test_utils.hh"

/////////////////////////////////////////////////
TEST(DOMScene, Construction)
Expand Down Expand Up @@ -200,3 +201,52 @@ TEST(DOMScene, ToElement)
const sdf::Sky *sky2 = scene2.Sky();
ASSERT_NE(nullptr, sky2);
}

/////////////////////////////////////////////////
TEST(DOMScene, ToElementErrorOutput)
{
std::stringstream buffer;
sdf::testing::RedirectConsoleStream redir(
sdf::Console::Instance()->GetMsgStream(), &buffer);

#ifdef _WIN32
sdf::Console::Instance()->SetQuiet(false);
sdf::testing::ScopeExit revertSetQuiet(
[]
{
sdf::Console::Instance()->SetQuiet(true);
});
#endif

sdf::Scene scene;
sdf::Errors errors;

scene.SetAmbient(gz::math::Color(0.1f, 0.2f, 0.3f, 1.0f));
scene.SetBackground(gz::math::Color(0.2f, 0.3f, 0.4f, 1.0f));
scene.SetGrid(true);
scene.SetOriginVisual(true);
scene.SetShadows(true);

sdf::Sky sky;
scene.SetSky(sky);

sdf::ElementPtr elem = scene.ToElement(errors);
EXPECT_TRUE(errors.empty());
ASSERT_NE(nullptr, elem);

sdf::Scene scene2;
errors = scene2.Load(elem);
EXPECT_TRUE(errors.empty());

EXPECT_EQ(scene.Ambient(), scene2.Ambient());
EXPECT_EQ(scene.Background(), scene2.Background());
EXPECT_EQ(scene.Grid(), scene2.Grid());
EXPECT_EQ(scene.OriginVisual(), scene2.OriginVisual());
EXPECT_EQ(scene.Shadows(), scene2.Shadows());

const sdf::Sky *sky2 = scene2.Sky();
ASSERT_NE(nullptr, sky2);

// Check nothing has been printed
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
}

0 comments on commit d044d5d

Please sign in to comment.