urdf.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <rve_render_client/urdf.h>
00031 #include <rve_render_client/mesh_instance.h>
00032 
00033 #include <urdf/model.h>
00034 #include <rve_common/eigen_conversions.h>
00035 
00036 #include <Eigen/Geometry>
00037 
00038 namespace rve_render_client
00039 {
00040 
00041 URDFPtr createURDF(const urdf::Model& model)
00042 {
00043   URDFPtr urdf(new URDF(model), destroySceneObject);
00044   return urdf;
00045 }
00046 
00047 URDFLink::URDFLink(const urdf::Model& model, const urdf::Link& link)
00048 {
00049   name_ = link.name;
00050 
00051   if (link.visual)
00052   {
00053     loadInfo(visual_, model, link, *link.visual->geometry, link.visual->origin);
00054   }
00055 }
00056 
00057 URDFLink::~URDFLink()
00058 {
00059 }
00060 
00061 void URDFLink::loadInfo(VisualInfo& info, const urdf::Model& model, const urdf::Link& link, const urdf::Geometry& geom, const urdf::Pose& origin)
00062 {
00063   rve_msgs::Vector3 p;
00064   p.x = origin.position.x;
00065   p.y = origin.position.y;
00066   p.z = origin.position.z;
00067   rve_msgs::Quaternion q;
00068   q.x = origin.rotation.x;
00069   q.y = origin.rotation.y;
00070   q.z = origin.rotation.z;
00071   q.w = origin.rotation.w;
00072   info.offset_position_ = p;
00073   info.offset_orientation_ = q;
00074 
00075   if (geom.type == urdf::Geometry::MESH)
00076   {
00077     const urdf::Mesh& mesh = static_cast<const urdf::Mesh&>(geom);
00078     if (!mesh.filename.empty())
00079     {
00080       MeshInstancePtr m = createMeshInstance(mesh.filename);
00081       addObject(m);
00082 
00083       // TODO: these functions need to be aggregates
00084       set_position_ = boost::bind(&MeshInstance::setPosition, m.get(), _1);
00085       set_orientation_ = boost::bind(&MeshInstance::setOrientation, m.get(), _1);
00086     }
00087   }
00088 
00089   // TODO: non-mesh objects
00090 }
00091 
00092 void URDFLink::setTransform(const rve_msgs::Vector3& pos, const rve_msgs::Quaternion& orient)
00093 {
00094   if (set_position_)
00095   {
00096     Eigen::Vector3f v(pos.x, pos.y, pos.z);
00097     Eigen::Quaternionf q(orient.w, orient.x, orient.y, orient.z);
00098     v += rve_common::msgToEigen(visual_.offset_position_);
00099     q *= rve_common::msgToEigen(visual_.offset_orientation_);
00100 
00101     set_position_(rve_common::eigenToMsg(v));
00102     set_orientation_(rve_common::eigenToMsg(q));
00103   }
00104 }
00105 
00106 URDF::URDF(const urdf::Model& model)
00107 {
00108   load(model);
00109 }
00110 
00111 URDF::~URDF()
00112 {
00113 }
00114 
00115 void URDF::load(const urdf::Model& model)
00116 {
00117   clear();
00118 
00119   urdf::Model m;
00120   m.initParam("robot_description");
00121   typedef std::vector<boost::shared_ptr<urdf::Link> > V_Link;
00122   V_Link links;
00123   m.getLinks(links);
00124   V_Link::iterator it = links.begin();
00125   V_Link::iterator end = links.end();
00126   for (; it != end; ++it)
00127   {
00128     const boost::shared_ptr<urdf::Link>& l = *it;
00129     URDFLinkPtr link(new URDFLink(model, *l));
00130     addObject(link);
00131     links_.push_back(link);
00132   }
00133 }
00134 
00135 void URDF::clear()
00136 {
00137   LinkIterator it = links_.begin();
00138   LinkIterator end = links_.end();
00139   for (; it != end; ++it)
00140   {
00141     const URDFLinkPtr& link = *it;
00142     removeObject(link);
00143   }
00144 
00145   links_.clear();
00146 }
00147 
00148 } // namespace rve_render_client


rve_render_client
Author(s): Josh Faust
autogenerated on Wed Dec 11 2013 14:31:32