/* * Copyright 2015 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 #include #include "sdf/sdf.hh" #include "test_config.h" //////////////////////////////////////// // Test parsing a model element that has a frame element TEST(Frame, ModelFrame) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << " 1 1 0 0 0 0" << " " << " 1 0 0 0 0 0" << " " << "" << ""; sdf::SDFPtr sdfParsed(new sdf::SDF()); sdf::init(sdfParsed); ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // Verify correct parsing // 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"); // model frame EXPECT_TRUE(modelElem->HasElement("frame")); sdf::ElementPtr frameElem = modelElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), "mframe"); // model frame pose EXPECT_TRUE(frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), "/world"); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(1, 1, 0, 0, 0, 0)); // model pose EXPECT_TRUE(modelElem->HasElement("pose")); sdf::ElementPtr modelPoseElem = modelElem->GetElement("pose"); EXPECT_TRUE(modelPoseElem->HasAttribute("frame")); EXPECT_EQ(modelPoseElem->Get("frame"), "mframe"); EXPECT_EQ(modelPoseElem->Get(), ignition::math::Pose3d(1, 0, 0, 0, 0, 0)); // link EXPECT_TRUE(modelElem->HasElement("link")); sdf::ElementPtr linkElem = modelElem->GetElement("link"); EXPECT_TRUE(linkElem->HasAttribute("name")); EXPECT_EQ(linkElem->Get("name"), "link"); } //////////////////////////////////////// // Test parsing a model element with an empty frame element TEST(Frame, FrameDefaultPose) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << " " << "" << ""; sdf::SDFPtr sdfParsed(new sdf::SDF()); sdf::init(sdfParsed); ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // Verify correct parsing // 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"); // model frame EXPECT_TRUE(modelElem->HasElement("frame")); sdf::ElementPtr frameElem = modelElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), "mframe"); // model frame pose EXPECT_TRUE(!frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), ""); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(0, 0, 0, 0, 0, 0)); // link EXPECT_TRUE(modelElem->HasElement("link")); sdf::ElementPtr linkElem = modelElem->GetElement("link"); EXPECT_TRUE(linkElem->HasAttribute("name")); EXPECT_EQ(linkElem->Get("name"), "link"); } //////////////////////////////////////// // Test parsing a model element with no frames - for backward compatibility TEST(Frame, NoFrame) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << "" << ""; sdf::SDFPtr sdfParsed(new sdf::SDF()); sdf::init(sdfParsed); ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // Verify correct parsing // 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"); { // model frame EXPECT_TRUE(!modelElem->HasElement("frame")); sdf::ElementPtr frameElem = modelElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), ""); // model frame pose EXPECT_TRUE(!frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), ""); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(0, 0, 0, 0, 0, 0)); } // link EXPECT_TRUE(modelElem->HasElement("link")); sdf::ElementPtr linkElem = modelElem->GetElement("link"); EXPECT_TRUE(linkElem->HasAttribute("name")); EXPECT_EQ(linkElem->Get("name"), "link"); { // link frame EXPECT_TRUE(!linkElem->HasElement("frame")); sdf::ElementPtr frameElem = linkElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), ""); // link frame pose EXPECT_TRUE(!frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), ""); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(0, 0, 0, 0, 0, 0)); } } //////////////////////////////////////// // Test parsing a link element that has a frame element TEST(Frame, LinkFrame) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << " " << " 1 0 2 0 0 0" << " " << " 0 5 0 0 0 0" << " " << "" << ""; sdf::SDFPtr sdfParsed(new sdf::SDF()); sdf::init(sdfParsed); ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // Verify correct parsing // 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"); // link EXPECT_TRUE(modelElem->HasElement("link")); sdf::ElementPtr linkElem = modelElem->GetElement("link"); EXPECT_TRUE(linkElem->HasAttribute("name")); EXPECT_EQ(linkElem->Get("name"), "link"); // link frame EXPECT_TRUE(linkElem->HasElement("frame")); sdf::ElementPtr frameElem = linkElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), "lframe"); // link frame pose EXPECT_TRUE(frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), "model"); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(1, 0, 2, 0, 0, 0)); // link pose EXPECT_TRUE(linkElem->HasElement("pose")); sdf::ElementPtr linkPoseElem = linkElem->GetElement("pose"); EXPECT_TRUE(linkPoseElem->HasAttribute("frame")); EXPECT_EQ(linkPoseElem->Get("frame"), "lframe"); EXPECT_EQ(linkPoseElem->Get(), ignition::math::Pose3d(0, 5, 0, 0, 0, 0)); } //////////////////////////////////////// // Test parsing a joint element that has a frame element TEST(Frame, JointFrame) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << " " << " " << " parent" << " child" << " " << " 1 0 0" << " " << " " << " 0 0 1 0 0 0" << " " << " 0 2 1 0 0 0" << " " << "" << ""; sdf::SDFPtr sdfParsed(new sdf::SDF()); sdf::init(sdfParsed); ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // Verify correct parsing // 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"); // link EXPECT_TRUE(modelElem->HasElement("link")); sdf::ElementPtr linkElem = modelElem->GetElement("link"); EXPECT_TRUE(linkElem->HasAttribute("name")); EXPECT_EQ(linkElem->Get("name"), "parent"); linkElem = linkElem->GetNextElement("link"); EXPECT_TRUE(linkElem != NULL); EXPECT_TRUE(linkElem->HasAttribute("name")); EXPECT_EQ(linkElem->Get("name"), "child"); // joint EXPECT_TRUE(modelElem->HasElement("joint")); sdf::ElementPtr jointElem = modelElem->GetElement("joint"); EXPECT_TRUE(jointElem->HasAttribute("name")); EXPECT_EQ(jointElem->Get("name"), "revjoint"); EXPECT_TRUE(jointElem->HasAttribute("type")); EXPECT_EQ(jointElem->Get("type"), "revolute"); // joint links EXPECT_TRUE(jointElem->HasElement("parent")); EXPECT_EQ(jointElem->Get("parent"), "parent"); EXPECT_TRUE(jointElem->HasElement("child")); EXPECT_EQ(jointElem->Get("child"), "child"); // joint axis EXPECT_TRUE(jointElem->HasElement("axis")); sdf::ElementPtr axisElem = jointElem->GetElement("axis"); EXPECT_TRUE(axisElem->HasElement("xyz")); EXPECT_EQ(axisElem->Get("xyz"), ignition::math::Vector3d(1, 0, 0)); // joint frame EXPECT_TRUE(jointElem->HasElement("frame")); sdf::ElementPtr frameElem = jointElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), "jframe"); // joint frame pose EXPECT_TRUE(frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), "child"); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(0, 0, 1, 0, 0, 0)); // joint pose EXPECT_TRUE(jointElem->HasElement("pose")); sdf::ElementPtr jointPoseElem = jointElem->GetElement("pose"); EXPECT_TRUE(jointPoseElem->HasAttribute("frame")); EXPECT_EQ(jointPoseElem->Get("frame"), "jframe"); EXPECT_EQ(jointPoseElem->Get(), ignition::math::Pose3d(0, 2, 1, 0, 0, 0)); } //////////////////////////////////////// // Test parsing a collision element that has a frame element TEST(Frame, CollisionFrame) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << " " << " " << " 1 3 1 0 0 0" << " " << " 0 2 0 0 0 0" << " " << " " << "" << ""; sdf::SDFPtr sdfParsed(new sdf::SDF()); sdf::init(sdfParsed); ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // Verify correct parsing // 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"); // link EXPECT_TRUE(modelElem->HasElement("link")); sdf::ElementPtr linkElem = modelElem->GetElement("link"); EXPECT_TRUE(linkElem->HasAttribute("name")); EXPECT_EQ(linkElem->Get("name"), "link"); // collision EXPECT_TRUE(linkElem->HasElement("collision")); sdf::ElementPtr collisionElem = linkElem->GetElement("collision"); EXPECT_TRUE(collisionElem->HasAttribute("name")); EXPECT_EQ(collisionElem->Get("name"), "collision"); // collision frame EXPECT_TRUE(collisionElem->HasElement("frame")); sdf::ElementPtr frameElem = collisionElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), "cframe"); // collision frame pose EXPECT_TRUE(frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), "link"); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(1, 3, 1, 0, 0, 0)); // collision pose EXPECT_TRUE(collisionElem->HasElement("pose")); sdf::ElementPtr collisionPoseElem = collisionElem->GetElement("pose"); EXPECT_TRUE(collisionPoseElem->HasAttribute("frame")); EXPECT_EQ(collisionPoseElem->Get("frame"), "cframe"); EXPECT_EQ(collisionPoseElem->Get(), ignition::math::Pose3d(0, 2, 0, 0, 0, 0)); } //////////////////////////////////////// // Test parsing a visual element that has a frame element TEST(Frame, VisualFrame) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << " " << " " << " 1 1 1 0 0 0" << " " << " 2 2 2 0 0 0" << " " << " " << "" << ""; sdf::SDFPtr sdfParsed(new sdf::SDF()); sdf::init(sdfParsed); ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // Verify correct parsing // 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"); // link EXPECT_TRUE(modelElem->HasElement("link")); sdf::ElementPtr linkElem = modelElem->GetElement("link"); EXPECT_TRUE(linkElem->HasAttribute("name")); EXPECT_EQ(linkElem->Get("name"), "link"); // visual EXPECT_TRUE(linkElem->HasElement("visual")); sdf::ElementPtr visualElem = linkElem->GetElement("visual"); EXPECT_TRUE(visualElem->HasAttribute("name")); EXPECT_EQ(visualElem->Get("name"), "visual"); // visual frame EXPECT_TRUE(visualElem->HasElement("frame")); sdf::ElementPtr frameElem = visualElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), "vframe"); // visual frame pose EXPECT_TRUE(frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), "link"); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(1, 1, 1, 0, 0, 0)); // visual pose EXPECT_TRUE(visualElem->HasElement("pose")); sdf::ElementPtr visualPoseElem = visualElem->GetElement("pose"); EXPECT_TRUE(visualPoseElem->HasAttribute("frame")); EXPECT_EQ(visualPoseElem->Get("frame"), "vframe"); EXPECT_EQ(visualPoseElem->Get(), ignition::math::Pose3d(2, 2, 2, 0, 0, 0)); } //////////////////////////////////////// // Test parsing an inertial element that has a frame element TEST(Frame, InertialFrame) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << " " << " " << " 1 2 3 0 0 0" << " " << " 3 2 1 0 0 0" << " " << " " << "" << ""; sdf::SDFPtr sdfParsed(new sdf::SDF()); sdf::init(sdfParsed); ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // Verify correct parsing // 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"); // link EXPECT_TRUE(modelElem->HasElement("link")); sdf::ElementPtr linkElem = modelElem->GetElement("link"); EXPECT_TRUE(linkElem->HasAttribute("name")); EXPECT_EQ(linkElem->Get("name"), "link"); // inertial EXPECT_TRUE(linkElem->HasElement("inertial")); sdf::ElementPtr inertialElem = linkElem->GetElement("inertial"); // inertial frame EXPECT_TRUE(inertialElem->HasElement("frame")); sdf::ElementPtr frameElem = inertialElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), "iframe"); // inertial frame pose EXPECT_TRUE(frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), "link"); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(1, 2, 3, 0, 0, 0)); // inertial pose EXPECT_TRUE(inertialElem->HasElement("pose")); sdf::ElementPtr inertialPoseElem = inertialElem->GetElement("pose"); EXPECT_TRUE(inertialPoseElem->HasAttribute("frame")); EXPECT_EQ(inertialPoseElem->Get("frame"), "iframe"); EXPECT_EQ(inertialPoseElem->Get(), ignition::math::Pose3d(3, 2, 1, 0, 0, 0)); } //////////////////////////////////////// // Test parsing a light element that has a frame element TEST(Frame, LightFrame) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << " 0.1 10 0 0 0 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()); sdf::init(sdfParsed); ASSERT_TRUE(sdf::readString(stream.str(), sdfParsed)); // Verify correct parsing // light EXPECT_TRUE(sdfParsed->Root()->HasElement("light")); sdf::ElementPtr lightElem = sdfParsed->Root()->GetElement("light"); EXPECT_TRUE(lightElem->HasAttribute("name")); EXPECT_EQ(lightElem->Get("name"), "my_light"); EXPECT_TRUE(lightElem->HasAttribute("type")); EXPECT_EQ(lightElem->Get("type"), "directional"); // light frame EXPECT_TRUE(lightElem->HasElement("frame")); sdf::ElementPtr frameElem = lightElem->GetElement("frame"); EXPECT_TRUE(frameElem->HasAttribute("name")); EXPECT_EQ(frameElem->Get("name"), "lframe"); // light frame pose EXPECT_TRUE(frameElem->HasElement("pose")); sdf::ElementPtr poseElem = frameElem->GetElement("pose"); EXPECT_TRUE(poseElem->HasAttribute("frame")); EXPECT_EQ(poseElem->Get("frame"), "/world"); EXPECT_EQ(poseElem->Get(), ignition::math::Pose3d(0.1, 10, 0, 0, 0, 0)); // light pose EXPECT_TRUE(lightElem->HasElement("pose")); sdf::ElementPtr lightPoseElem = lightElem->GetElement("pose"); EXPECT_TRUE(lightPoseElem->HasAttribute("frame")); EXPECT_EQ(lightPoseElem->Get("frame"), "lframe"); EXPECT_EQ(lightPoseElem->Get(), ignition::math::Pose3d(0.1, 0, 0, 0, 0, 0)); // diffuse EXPECT_TRUE(lightElem->HasElement("diffuse")); sdf::ElementPtr diffuseElem = lightElem->GetElement("diffuse"); EXPECT_EQ(diffuseElem->Get(), sdf::Color(0.2f, 0.3f, 0.4f, 1.0f)); // specular EXPECT_TRUE(lightElem->HasElement("specular")); sdf::ElementPtr specularElem = lightElem->GetElement("specular"); EXPECT_EQ(specularElem->Get(), sdf::Color(0.3f, 0.4f, 0.5f, 1.0f)); } //////////////////////////////////////// // Test parsing an actor element that has a frame element TEST(Frame, ActorFrame) { std::ostringstream stream; std::string version = SDF_VERSION; stream << "" << "" << " " << " 1 5 0 0 0 0" << " " << " 0.1 3 0 0 0 0" << " " << " moonwalk.dae" << " " << " " << " walk.dae" << " " << "