Skip to content

Commit

Permalink
Merge branch 'sdf13' into marcoag/sdf_error_element_particleEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco A. Gutierrez authored Mar 29, 2023
2 parents 12e0078 + 0f6ca9a commit af91598
Show file tree
Hide file tree
Showing 16 changed files with 800 additions and 170 deletions.
8 changes: 8 additions & 0 deletions include/sdf/Frame.hh
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,14 @@ namespace sdf
/// \return SDF element pointer with updated frame values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Give a scoped FrameAttachedToGraph to be used for resolving
/// attached bodies. This is private and is intended to be called by
/// Model::Load or World::Load.
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Imu.hh
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,14 @@ namespace sdf
/// \return SDF element pointer with updated sensor values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
10 changes: 10 additions & 0 deletions include/sdf/JointAxis.hh
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ namespace sdf
/// \return SDF element pointer with updated joint values.
public: sdf::ElementPtr ToElement(unsigned int _index = 0u) const;

/// \brief Create and return an SDF element filled with data from this
/// joint axis.
/// Note that parameter passing functionality is not captured with this
/// function.
/// \param[out] _errors Vector of errors.
/// \param[in] _index Index of this joint axis
/// \return SDF element pointer with updated joint values.
public: sdf::ElementPtr ToElement(sdf::Errors &_errors,
unsigned int _index = 0u) 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/Light.hh
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ namespace sdf
/// \return SDF element pointer with updated light values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Allow Link::SetPoseRelativeToGraph or World::Load to call
/// SetXmlParentName and SetPoseRelativeToGraph,
/// but Link::SetPoseRelativeToGraph is a private function, so we need
Expand Down
8 changes: 8 additions & 0 deletions include/sdf/Physics.hh
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ namespace sdf
/// \return SDF element pointer with updated physics values.
public: sdf::ElementPtr ToElement() const;

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

/// \brief Private data pointer.
GZ_UTILS_IMPL_PTR(dataPtr)
};
Expand Down
21 changes: 15 additions & 6 deletions src/Frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Errors Frame::Load(ElementPtr _sdf)
// Read the frame's attached_to attribute
if (_sdf->HasAttribute("attached_to"))
{
auto pair = _sdf->Get<std::string>("attached_to", "");
auto pair = _sdf->Get<std::string>(errors, "attached_to", "");
if (pair.second)
{
this->dataPtr->attachedTo = pair.first;
Expand Down Expand Up @@ -221,23 +221,32 @@ sdf::ElementPtr Frame::Element() const

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

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

elem->GetAttribute("name")->Set(this->dataPtr->name);
elem->GetAttribute("name")->Set(this->dataPtr->name, _errors);

if (!this->dataPtr->attachedTo.empty())
elem->GetAttribute("attached_to")->Set(this->dataPtr->attachedTo);
elem->GetAttribute("attached_to")->Set(this->dataPtr->attachedTo, _errors);

// Set pose
sdf::ElementPtr poseElem = elem->GetElement("pose");
sdf::ElementPtr poseElem = elem->GetElement("pose", _errors);
if (!this->dataPtr->poseRelativeTo.empty())
{
poseElem->GetAttribute("relative_to")->Set<std::string>(
this->dataPtr->poseRelativeTo);
this->dataPtr->poseRelativeTo, _errors);
}
poseElem->Set<gz::math::Pose3d>(this->RawPose());
poseElem->Set<gz::math::Pose3d>(_errors, this->RawPose());

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

/////////////////////////////////////////////////
TEST(DOMframe, Construction)
Expand Down Expand Up @@ -116,3 +117,70 @@ TEST(DOMFrame, ToElement)
EXPECT_TRUE(frame2.AttachedTo().empty());
}
}

/////////////////////////////////////////////////
TEST(DOMFrame, 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::Errors errors;

// With 'attached-to'
{
sdf::Frame frame;

frame.SetName("my-frame");
frame.SetAttachedTo("attached-to-frame");
frame.SetPoseRelativeTo("relative-to-frame");
frame.SetRawPose(gz::math::Pose3d(1, 2, 3, 0.1, 0.2, 0.3));

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

sdf::Frame frame2;
errors = frame2.Load(elem);
EXPECT_TRUE(errors.empty());

EXPECT_EQ(frame.Name(), frame.Name());
EXPECT_EQ(frame.AttachedTo(), frame2.AttachedTo());
EXPECT_EQ(frame.PoseRelativeTo(), frame2.PoseRelativeTo());
EXPECT_EQ(frame.RawPose(), frame2.RawPose());
}

// Without 'attached-to'
{
sdf::Frame frame;

frame.SetName("my-frame");
frame.SetRawPose(gz::math::Pose3d(1, 2, 3, 0.1, 0.2, 0.3));
EXPECT_TRUE(frame.AttachedTo().empty());

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

sdf::Frame frame2;
errors = frame2.Load(elem);
EXPECT_TRUE(errors.empty());

EXPECT_EQ(frame.Name(), frame.Name());
EXPECT_EQ(frame.RawPose(), frame2.RawPose());
EXPECT_TRUE(frame.AttachedTo().empty());
EXPECT_TRUE(frame2.AttachedTo().empty());
}

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

0 comments on commit af91598

Please sign in to comment.