Skip to content

Commit

Permalink
Geometry and others: update calls to use sdf::Errors parameters (#1153)
Browse files Browse the repository at this point in the history
Signed-off-by: Marco A. Gutierrez <marco@openrobotics.org>
  • Loading branch information
Marco A. Gutierrez committed May 2, 2023
1 parent a5da9a3 commit fd877b6
Show file tree
Hide file tree
Showing 36 changed files with 1,137 additions and 126 deletions.
8 changes: 8 additions & 0 deletions include/sdf/Box.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ namespace sdf
/// \return SDF element pointer with updated box values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Capsule.hh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ namespace sdf
/// \return SDF element pointer with updated capsule values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Collision.hh
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ namespace sdf
/// \return SDF element pointer with updated collision values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Give the name of the xml parent of this object, to be used
/// for resolving poses. This is private and is intended to be called by
/// Link::SetPoseRelativeToGraph.
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Cylinder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ namespace sdf
/// \return SDF element pointer with updated cylinder values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Ellipsoid.hh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ namespace sdf
/// \return SDF element pointer with updated ellipsoid values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Geometry.hh
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ namespace sdf
/// \return SDF element pointer with updated geometry values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Heightmap.hh
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ namespace sdf
/// \return SDF element pointer with updated heightmap values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Mesh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ namespace sdf
/// \return SDF element pointer with updated mesh values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Plane.hh
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ namespace sdf
/// \return SDF element pointer with updated plane values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Polyline.hh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ namespace sdf
/// \return SDF element pointer with updated polyline values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Sphere.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ namespace sdf
/// \return SDF element pointer with updated sphere values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Surface.hh
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,14 @@ namespace sdf
/// \return SDF element pointer with updated surface values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
16 changes: 13 additions & 3 deletions src/Box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <gz/math/Vector3.hh>
#include "sdf/Box.hh"
#include "sdf/parser.hh"
#include "Utils.hh"

using namespace sdf;

Expand Down Expand Up @@ -64,7 +65,7 @@ Errors Box::Load(ElementPtr _sdf)
if (_sdf->HasElement("size"))
{
std::pair<gz::math::Vector3d, bool> pair =
_sdf->Get<gz::math::Vector3d>("size", this->dataPtr->box.Size());
_sdf->Get<gz::math::Vector3d>(errors, "size", this->dataPtr->box.Size());

if (!pair.second)
{
Expand Down Expand Up @@ -116,12 +117,21 @@ gz::math::Boxd &Box::Shape()

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

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

sdf::ElementPtr sizeElem = elem->GetElement("size");
sizeElem->Set(this->Size());
sdf::ElementPtr sizeElem = elem->GetElement("size", _errors);
sizeElem->Set(_errors, this->Size());

return elem;
}
37 changes: 37 additions & 0 deletions src/Box_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <gtest/gtest.h>
#include "sdf/Box.hh"
#include "sdf/Element.hh"
#include "test_utils.hh"

/////////////////////////////////////////////////
TEST(DOMBox, Construction)
Expand Down Expand Up @@ -159,3 +160,39 @@ TEST(DOMBox, ToElement)

EXPECT_EQ(box.Size(), box2.Size());
}

/////////////////////////////////////////////////
TEST(DOMBox, 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::Box box;
sdf::Errors errors;

box.SetSize(gz::math::Vector3d(1, 2, 3));

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

sdf::Box box2;
errors = box2.Load(elem);
EXPECT_TRUE(errors.empty());

EXPECT_EQ(box.Size(), box2.Size());

// Check nothing has been printed
EXPECT_TRUE(buffer.str().empty()) << buffer.str();
}
22 changes: 16 additions & 6 deletions src/Capsule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <sstream>
#include "sdf/Capsule.hh"
#include "sdf/parser.hh"
#include "Utils.hh"

using namespace sdf;

Expand Down Expand Up @@ -62,7 +63,7 @@ Errors Capsule::Load(ElementPtr _sdf)
}

{
std::pair<double, bool> pair = _sdf->Get<double>("radius",
std::pair<double, bool> pair = _sdf->Get<double>(errors, "radius",
this->dataPtr->capsule.Radius());

if (!pair.second)
Expand All @@ -77,7 +78,7 @@ Errors Capsule::Load(ElementPtr _sdf)
}

{
std::pair<double, bool> pair = _sdf->Get<double>("length",
std::pair<double, bool> pair = _sdf->Get<double>(errors, "length",
this->dataPtr->capsule.Length());

if (!pair.second)
Expand Down Expand Up @@ -138,15 +139,24 @@ gz::math::Capsuled &Capsule::Shape()

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

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

sdf::ElementPtr radiusElem = elem->GetElement("radius");
radiusElem->Set(this->Radius());
sdf::ElementPtr radiusElem = elem->GetElement("radius", _errors);
radiusElem->Set(_errors, this->Radius());

sdf::ElementPtr lengthElem = elem->GetElement("length");
lengthElem->Set(this->Length());
sdf::ElementPtr lengthElem = elem->GetElement("length", _errors);
lengthElem->Set(_errors, this->Length());

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

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

/////////////////////////////////////////////////
TEST(DOMCapsule, Construction)
Expand Down Expand Up @@ -197,3 +198,40 @@ TEST(DOMCapsule, ToElement)
EXPECT_DOUBLE_EQ(capsule.Radius(), capsule2.Radius());
EXPECT_DOUBLE_EQ(capsule.Length(), capsule2.Length());
}

/////////////////////////////////////////////////
TEST(DOMCapsule, 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::Capsule capsule;
sdf::Errors errors;

capsule.SetRadius(1.2);
capsule.SetLength(0.5);

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

sdf::Capsule capsule2;
errors = capsule2.Load(elem);
EXPECT_TRUE(errors.empty());

EXPECT_DOUBLE_EQ(capsule.Radius(), capsule2.Radius());
EXPECT_DOUBLE_EQ(capsule.Length(), capsule2.Length());

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

0 comments on commit fd877b6

Please sign in to comment.