urdf_renderer.cpp
Go to the documentation of this file.
00001 /* 
00002  * Copyright (c) 2011, Nico Blodow <blodow@cs.tum.edu>
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 Intelligent Autonomous Systems Group/
00014  *       Technische Universitaet Muenchen nor the names of its contributors 
00015  *       may be used to endorse or promote products derived from this software 
00016  *       without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028  * POSSIBILITY OF SUCH DAMAGE.
00029  */
00030 
00031 
00032 #include <GL/glew.h>
00033 #include <GL3/gl3.h>
00034 #include <GL/glu.h>
00035 #include <GL/glx.h>
00036 #undef Success  // <---- Screw Xlib for this
00037 
00038 #include <ros/node_handle.h>
00039 
00040 #include <realtime_urdf_filter/urdf_renderer.h>
00041 
00042 namespace realtime_urdf_filter
00043 {
00044   URDFRenderer::URDFRenderer (std::string model_description, 
00045                               std::string tf_prefix,
00046                               std::string cam_frame,
00047                               std::string fixed_frame,
00048                               tf::TransformListener &tf)
00049     : model_description_(model_description)
00050     , tf_prefix_(tf_prefix)
00051     , camera_frame_ (cam_frame)
00052     , fixed_frame_(fixed_frame)
00053     , tf_(tf)
00054   {
00055     initURDFModel ();
00056     tf_.setExtrapolationLimit (ros::Duration (5.0));
00057   }
00058 
00060 
00061   void
00062     URDFRenderer::initURDFModel ()
00063   {
00064     urdf::Model model;
00065     if (!model.initString(model_description_))
00066     {
00067       ROS_ERROR ("URDF failed Model parse");
00068       return;
00069     }
00070 
00071     ROS_INFO ("URDF parsed OK");
00072     loadURDFModel (model);
00073     ROS_INFO ("URDF loaded OK");
00074   }
00075 
00076 
00079   void URDFRenderer::loadURDFModel
00080     (urdf::Model &model)
00081   {
00082     typedef std::vector<boost::shared_ptr<urdf::Link> > V_Link;
00083     V_Link links;
00084     model.getLinks(links);
00085 
00086     V_Link::iterator it = links.begin();
00087     V_Link::iterator end = links.end();
00088 
00089     for (; it != end; ++it)
00090       process_link (*it);
00091   }
00092 
00094 
00095   void URDFRenderer::process_link (boost::shared_ptr<urdf::Link> link)
00096   {
00097     if (link->visual.get() == NULL || link->visual->geometry.get() == NULL)
00098       return;
00099 
00100     boost::shared_ptr<Renderable> r;
00101     if (link->visual->geometry->type == urdf::Geometry::BOX)
00102     {
00103       boost::shared_ptr<urdf::Box> box = boost::dynamic_pointer_cast<urdf::Box> (link->visual->geometry);
00104       r.reset (new RenderableBox (box->dim.x, box->dim.y, box->dim.z));
00105     }
00106     else if (link->visual->geometry->type == urdf::Geometry::CYLINDER)
00107     {
00108       boost::shared_ptr<urdf::Cylinder> cylinder = boost::dynamic_pointer_cast<urdf::Cylinder> (link->visual->geometry);
00109       r.reset (new RenderableCylinder (cylinder->radius, cylinder->length));
00110     }
00111     else if (link->visual->geometry->type == urdf::Geometry::SPHERE)
00112     {
00113       boost::shared_ptr<urdf::Sphere> sphere = boost::dynamic_pointer_cast<urdf::Sphere> (link->visual->geometry);
00114       r.reset (new RenderableSphere (sphere->radius));
00115     }
00116     else if (link->visual->geometry->type == urdf::Geometry::MESH)
00117     {
00118       boost::shared_ptr<urdf::Mesh> mesh = boost::dynamic_pointer_cast<urdf::Mesh> (link->visual->geometry);
00119       std::string meshname (mesh->filename);
00120       RenderableMesh* rm = new RenderableMesh (meshname);
00121       rm->setScale (mesh->scale.x, mesh->scale.y, mesh->scale.z);
00122       r.reset (rm);
00123     }
00124     r->setLinkName (tf_prefix_+ "/" + link->name);
00125     urdf::Vector3 origin = link->visual->origin.position;
00126     urdf::Rotation rotation = link->visual->origin.rotation;
00127     r->link_offset = tf::Transform (
00128         tf::Quaternion (rotation.x, rotation.y, rotation.z, rotation.w).normalize (),
00129         tf::Vector3 (origin.x, origin.y, origin.z));
00130     if (link->visual && 
00131         (link->visual->material))
00132       r->color  = link->visual->material->color;
00133     renderables_.push_back (r); 
00134   }
00135 
00137 
00138   void URDFRenderer::update_link_transforms ()
00139   {
00140     tf::StampedTransform t;
00141 
00142     std::vector<boost::shared_ptr<Renderable> >::const_iterator it = renderables_.begin ();
00143     for (; it != renderables_.end (); it++)
00144     {
00145       try
00146       {
00147         tf_.lookupTransform (fixed_frame_, (*it)->name, ros::Time (), t);
00148       }
00149       catch (tf::TransformException ex)
00150       {
00151         ROS_ERROR("%s",ex.what());
00152       }
00153       (*it)->link_to_fixed = tf::Transform (t.getRotation (), t.getOrigin ());
00154     }
00155   }
00156 
00158 
00159   void URDFRenderer::render ()
00160   {
00161     update_link_transforms ();
00162       
00163     std::vector<boost::shared_ptr<Renderable> >::const_iterator it = renderables_.begin ();
00164     for (; it != renderables_.end (); it++)
00165       (*it)->render ();
00166   }
00167 
00168 }
00169 
00170 // REGEX BASED LINK / SEARCH OPERATIONS / TARGET FRAMES SETUP
00171 //    for (sop_it = search_operations_.begin (); sop_it != search_operations_.end (); sop_it++)
00172 //    {
00173 //      boost::smatch res;
00174 //      if (boost::regex_match(link->name, res, sop_it->re))
00175 //      {
00176 //        if (!sop_it->pub_topic_re.empty ())
00177 //        {
00178 //          std::string publish_topic = res.format (sop_it->pub_topic_re);
00179 //          sop_it->pub_topic = publish_topic;
00180 //          ROS_INFO ("Regex expanded publisher topic to: %s", publish_topic.c_str());
00181 //          publishers_ [publish_topic] = pcl_ros::Publisher<pcl::PointXYZ>
00182 //                                          (private_nh, publish_topic, max_queue_size_);
00183 //        }
00184 //        ROS_INFO ("Match: %s (%s)", link->name.c_str(), sop_it->re.str().c_str());
00185 //        double r,p,y;
00186 //
00187 //        // handle special case of fixed links of drawers
00188 //        boost::shared_ptr<urdf::Collision> c;
00189 //        boost::regex re (".*_fixed_link");
00190 //        if (boost::regex_match(link->name, re))
00191 //        {
00192 //          c = link->child_links[0]->collision;
00193 //
00194 //          // handle special case of search_expand* params for drawers
00195 //          std::map <std::string, XmlRpc::XmlRpcValue>::const_iterator op_it = sop_it->params.find ("operation");
00196 //          if (op_it != sop_it->params.end())
00197 //          {
00198 //            if (op_it->second == std::string ("fit_drawer"))
00199 //            {
00200 //              ROS_ERROR ("dealing with special case: %s, %s", link->name.c_str(), link->child_joints[0]->name.c_str());
00201 //              sop_it->params["search_expand_distance"] = link->child_joints[0]->limits->upper
00202 //                                                       - link->child_joints[0]->limits->lower;
00203 //              sop_it->params["search_expand_axis"][0] = link->child_joints[0]->axis.x;
00204 //              sop_it->params["search_expand_axis"][1] = link->child_joints[0]->axis.y;
00205 //              sop_it->params["search_expand_axis"][2] = link->child_joints[0]->axis.z;
00206 //              if (sop_it->params.count ("min_drawer_inliers") == 0)
00207 //                sop_it->params["min_drawer_inliers"] = 50;
00208 //            }
00209 //            else if (op_it->second == std::string ("fit_door"))
00210 //            {
00211 //              ROS_ERROR ("dealing with special case: %s, %s", link->name.c_str(), link->child_joints[0]->name.c_str());
00212 //              if (sop_it->params.count ("min_drawer_inliers") == 0)
00213 //                sop_it->params["min_drawer_inliers"] = 50;
00214 //            }
00215 //          }
00216 //        }
00217 //        else
00218 //          c = link->collision;
00219 //
00220 //        c->origin.rotation.getRPY (r,p,y);
00221 //        if (r != 0.0 || p != 0.0 || y != 0.0)
00222 //        {
00223 //          // we have a rotation here, so we need to add this frame to target_frames_
00224 //          TargetFrames tfr;
00225 //          tfr.frame = tf_prefix_ + "/" + link->name;
00226 //          tfr.op = (*sop_it);
00227 //          tfr.translation = (urdf::Vector3());
00228 //          tfr.link = (link);
00229 //          target_frames_.push_back (tfr);
00230 //        }
00231 //        else
00232 //        {
00233 //          // regex matches this link... so let's walk it up...
00234 //          std::string found_link;
00235 //          urdf::Vector3 null_offset;
00236 //          if (walk_back_links (link, link, *sop_it, stop_link_, null_offset))
00237 //          {
00238 //            // do something ?
00239 //          }
00240 //        }
00241 //
00242 //      }
00243 //      else
00244 //      {
00245 //        ROS_INFO ("No match: %s (%s)", link->name.c_str(), sop_it->re.str().c_str());
00246 //      }
00247 //    }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines


realtime_urdf_filter
Author(s): Nico Blodow
autogenerated on Thu May 23 2013 16:50:36