From faa3d181cc3685ae0a4d2d380a86e96f65c49907 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ag=C3=BCero?= Date: Mon, 7 Jun 2021 17:49:03 +0200 Subject: [PATCH 1/4] Add ModelSDF serializer. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Carlos Agüero --- include/ignition/gazebo/components/Model.hh | 45 ++++++++++++++++++- test/integration/components.cc | 49 +++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/include/ignition/gazebo/components/Model.hh b/include/ignition/gazebo/components/Model.hh index 7fdc7014ce..5e6e70640f 100644 --- a/include/ignition/gazebo/components/Model.hh +++ b/include/ignition/gazebo/components/Model.hh @@ -17,7 +17,10 @@ #ifndef IGNITION_GAZEBO_COMPONENTS_MODEL_HH_ #define IGNITION_GAZEBO_COMPONENTS_MODEL_HH_ +#include + #include +#include #include #include @@ -29,6 +32,44 @@ namespace gazebo { // Inline bracket to help doxygen filtering. inline namespace IGNITION_GAZEBO_VERSION_NAMESPACE { +namespace serializers +{ + class SdfModelSerializer + { + /// \brief Serialization for `sdf::Model`. + /// \param[in] _out Output stream. + /// \param[in] _time Model to stream + /// \return The stream. + public: static std::ostream &Serialize(std::ostream &_out, + const sdf::Model &_model) + { + _out << _model.Element()->GetParent()->ToString(""); + return _out; + } + + /// \brief Deserialization for `sdf::Model`. + /// \param[in] _in Input stream. + /// \param[out] _model Model to populate + /// \return The stream. + public: static std::istream &Deserialize(std::istream &_in, + sdf::Model &_model) + { + sdf::Root root; + std::string sdf(std::istreambuf_iterator(_in), {}); + + sdf::Errors errors = root.LoadSdfString(sdf); + if (!errors.empty()) + { + ignerr << "Unable to unserialize sdf::Model" << std::endl; + return _in; + } + + _model.Load(root.Element()->GetElement("model")); + return _in; + } + }; +} + namespace components { /// \brief A component that identifies an entity as being a model. @@ -36,7 +77,9 @@ namespace components IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.Model", Model) /// \brief A component that holds the model's SDF DOM - using ModelSdf = Component; + using ModelSdf = Component; IGN_GAZEBO_REGISTER_COMPONENT("ign_gazebo_components.ModelSdf", ModelSdf) } } diff --git a/test/integration/components.cc b/test/integration/components.cc index 71978487c1..acb5583eb0 100644 --- a/test/integration/components.cc +++ b/test/integration/components.cc @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "ignition/gazebo/components/Actor.hh" @@ -1099,6 +1100,54 @@ TEST_F(ComponentsTest, Model) comp3.Deserialize(istr); } +///////////////////////////////////////////////// +TEST_F(ComponentsTest, ModelSdf) +{ + std::ostringstream stream; + std::string version = SDF_VERSION; + stream + << "" + << "" + << "" + << " " + << " " + << " 0.1 0 0 0 0 0" + << " 0.2 0.3 0.4 1" + << " 0.3 0.4 0.5 1" + << " " + << " " + << "" + << ""; + + sdf::SDFPtr sdfParsed(new sdf::SDF()); + sdf::init(sdfParsed); + ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); + + // model + EXPECT_TRUE(sdfParsed->Root()->HasElement("model")); + sdf::ElementPtr modelElem = sdfParsed->Root()->GetElement("model"); + EXPECT_TRUE(modelElem->HasAttribute("name")); + EXPECT_EQ(modelElem->Get("name"), "my_model"); + + sdf::Model model; + model.Load(modelElem); + EXPECT_EQ("my_model", model.Name()); + + // Create components + auto comp1 = components::ModelSdf(model); + components::ModelSdf comp2; + + // Stream operators + std::ostringstream ostr; + comp1.Serialize(ostr); + + std::istringstream istr(ostr.str()); + comp2.Deserialize(istr); + + EXPECT_EQ("my_model", comp2.Data().Name()); + EXPECT_EQ(1u, comp2.Data().LinkCount()); +} + ///////////////////////////////////////////////// TEST_F(ComponentsTest, Name) { From 63323e17458f4816ff2dec0b9ec3a3a86409e8b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ag=C3=BCero?= Date: Fri, 11 Jun 2021 16:42:50 +0200 Subject: [PATCH 2/4] Update Serialize() function and tests. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Carlos Agüero --- include/ignition/gazebo/components/Model.hh | 12 +++++++- test/integration/components.cc | 34 ++++++++++++++------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/include/ignition/gazebo/components/Model.hh b/include/ignition/gazebo/components/Model.hh index 5e6e70640f..651e295ef1 100644 --- a/include/ignition/gazebo/components/Model.hh +++ b/include/ignition/gazebo/components/Model.hh @@ -43,7 +43,17 @@ namespace serializers public: static std::ostream &Serialize(std::ostream &_out, const sdf::Model &_model) { - _out << _model.Element()->GetParent()->ToString(""); + sdf::ElementPtr modelElem = _model.Element(); + if (!modelElem) + { + ignerr << "Unable to serialize sdf::Model" << std::endl; + return _out; + } + + _out << "" + << "" + << modelElem->ToString("") + << ""; return _out; } diff --git a/test/integration/components.cc b/test/integration/components.cc index acb5583eb0..128cde64ca 100644 --- a/test/integration/components.cc +++ b/test/integration/components.cc @@ -1108,15 +1108,25 @@ TEST_F(ComponentsTest, ModelSdf) stream << "" << "" - << "" - << " " - << " " - << " 0.1 0 0 0 0 0" - << " 0.2 0.3 0.4 1" - << " 0.3 0.4 0.5 1" - << " " - << " " - << "" + << " " + << " " + << " 0.001" + << " 1.0" + << " " + << " " + << " " + << " " + << " " + << " " + << " 0.1 0 0 0 0 0" + << " 0.2 0.3 0.4 1" + << " 0.3 0.4 0.5 1" + << " " + << " " + << " " + << " " << ""; sdf::SDFPtr sdfParsed(new sdf::SDF()); @@ -1124,8 +1134,10 @@ TEST_F(ComponentsTest, ModelSdf) ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // model - EXPECT_TRUE(sdfParsed->Root()->HasElement("model")); - sdf::ElementPtr modelElem = sdfParsed->Root()->GetElement("model"); + EXPECT_TRUE(sdfParsed->Root()->HasElement("world")); + sdf::ElementPtr worldElem = sdfParsed->Root()->GetElement("world"); + EXPECT_TRUE(worldElem->HasElement("model")); + sdf::ElementPtr modelElem = worldElem->GetElement("model"); EXPECT_TRUE(modelElem->HasAttribute("name")); EXPECT_EQ(modelElem->Get("name"), "my_model"); From de79f9a78d85a94e854b82dbf3a3c05e19c25ca2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ag=C3=BCero?= Date: Mon, 26 Jul 2021 17:38:47 +0200 Subject: [PATCH 3/4] Tweaks. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Carlos Agüero --- include/ignition/gazebo/components/Model.hh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/ignition/gazebo/components/Model.hh b/include/ignition/gazebo/components/Model.hh index 651e295ef1..47f392adb3 100644 --- a/include/ignition/gazebo/components/Model.hh +++ b/include/ignition/gazebo/components/Model.hh @@ -51,7 +51,7 @@ namespace serializers } _out << "" - << "" + << "" << modelElem->ToString("") << ""; return _out; @@ -74,7 +74,7 @@ namespace serializers return _in; } - _model.Load(root.Element()->GetElement("model")); + _model = *root.Model(); return _in; } }; From c428d200838b0e551fd7f08821cb738b50b717ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ag=C3=BCero?= Date: Thu, 12 Aug 2021 17:00:05 +0200 Subject: [PATCH 4/4] Tweak MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Carlos Agüero --- include/ignition/gazebo/components/Model.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ignition/gazebo/components/Model.hh b/include/ignition/gazebo/components/Model.hh index 47f392adb3..d4d306f69e 100644 --- a/include/ignition/gazebo/components/Model.hh +++ b/include/ignition/gazebo/components/Model.hh @@ -68,7 +68,7 @@ namespace serializers std::string sdf(std::istreambuf_iterator(_in), {}); sdf::Errors errors = root.LoadSdfString(sdf); - if (!errors.empty()) + if (!root.Element()->HasElement("model")) { ignerr << "Unable to unserialize sdf::Model" << std::endl; return _in;