Main MRPT website > C++ reference
MRPT logo
graph_tools_impl.h
Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                       http://www.mrpt.org/                                |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef opengl_graph_tools_impl_H
00029 #define opengl_graph_tools_impl_H
00030 
00031 namespace mrpt
00032 {
00033         namespace opengl
00034         {
00035                 namespace graph_tools
00036                 {
00037                         template<class GRAPH_T>
00038                         CSetOfObjectsPtr graph_visualize(
00039                                 const GRAPH_T &g,
00040                                 const mrpt::utils::TParametersDouble &extra_params)
00041                         {
00042                                 MRPT_TRY_START
00043 
00044                                 // Is a 2D or 3D graph network?
00045                                 typedef typename GRAPH_T::constraint_t constraint_t;
00046 
00047                                 const bool is_3D_graph = constraint_t::is_3D();
00048 
00049                                 CSetOfObjectsPtr ret = CSetOfObjects::Create();
00050 
00051                                 const bool   show_ID_labels           = 0!=extra_params.getWithDefaultVal("show_ID_labels", 0);
00052                                 const bool   show_ground_grid         = 0!=extra_params.getWithDefaultVal("show_ground_grid", 1);
00053                                 const bool   show_edges               = 0!=extra_params.getWithDefaultVal("show_edges", 1);
00054                                 const bool   show_node_corners        = 0!=extra_params.getWithDefaultVal("show_node_corners", 1);
00055                                 const double nodes_point_size         = extra_params.getWithDefaultVal("nodes_point_size", 0. );
00056                                 const double nodes_corner_scale       = extra_params.getWithDefaultVal("nodes_corner_scale", 0.7 );
00057                                 const unsigned int nodes_point_color  = extra_params.getWithDefaultVal("nodes_point_color", (unsigned int)0xA0A0A0 );
00058                                 const unsigned int edge_color         = extra_params.getWithDefaultVal("edge_color", (unsigned int)0x400000FF );
00059                                 const double edge_width               = extra_params.getWithDefaultVal("edge_width", 2. );
00060 
00061                                 if (show_ground_grid)
00062                                 {
00063                                         // Estimate bounding box.
00064                                         TPoint3D  BB_min(-10.,-10.,0.), BB_max(10.,10.,0.);
00065 
00066                                         for (typename GRAPH_T::global_poses_t::const_iterator itNod = g.nodes.begin();itNod!=g.nodes.end();++itNod)
00067                                         {
00068                                                 const CPose3D p = CPose3D(itNod->second); // Convert to 3D from whatever its real type.
00069 
00070                                                 keep_min( BB_min.x, p.x() );
00071                                                 keep_min( BB_min.y, p.y() );
00072                                                 keep_min( BB_min.z, p.z() );
00073 
00074                                                 keep_max( BB_max.x, p.x() );
00075                                                 keep_max( BB_max.y, p.y() );
00076                                                 keep_max( BB_max.z, p.z() );
00077                                         }
00078 
00079                                         // Create ground plane:
00080                                         const double grid_frequency = 5.0;
00081                                         CGridPlaneXYPtr grid = CGridPlaneXY::Create(BB_min.x, BB_max.x, BB_min.y, BB_max.y, BB_min.z, grid_frequency);
00082                                         grid->setColor(0.3,0.3,0.3);
00083                                         ret->insert( grid );
00084                                 } // end show_ground_grid
00085 
00086                                 // Draw nodes as thick points:
00087                                 if (nodes_point_size!=0)
00088                                 {
00089                                         ASSERT_(nodes_point_size>=0);
00090 
00091                                         CPointCloudPtr pnts = CPointCloud::Create();
00092                                         pnts->setColor( TColorf(TColor(nodes_point_color)) );
00093                                         pnts->setPointSize(nodes_point_size);
00094 
00095                                         // Add nodes:
00096                                         for (typename GRAPH_T::global_poses_t::const_iterator itNod = g.nodes.begin();itNod!=g.nodes.end();++itNod)
00097                                         {
00098                                                 const CPose3D p = CPose3D(itNod->second); // Convert to 3D from whatever its real type.
00099                                                 pnts->insertPoint(p.x(),p.y(), p.z() );
00100                                         }
00101 
00102                                         pnts->enablePointSmooth();
00103 
00104                                         ret->insert(pnts);
00105                                 } // end draw node points
00106 
00107                                 // Show a 2D corner at each node (or just an empty object with the ID label)
00108                                 if (show_node_corners || show_ID_labels)
00109                                 {
00110                                         for (typename GRAPH_T::global_poses_t::const_iterator itNod = g.nodes.begin();itNod!=g.nodes.end();++itNod)
00111                                         {
00112                                                 const CPose3D p = CPose3D(itNod->second); // Convert to 3D from whatever its real type.
00113                                                 CSetOfObjectsPtr gl_corner = show_node_corners ?
00114                                                         (is_3D_graph ? stock_objects::CornerXYZSimple(nodes_corner_scale, 1.0 /*line width*/ ) : stock_objects::CornerXYSimple(nodes_corner_scale, 1.0 /*line width*/ ))
00115                                                         : CSetOfObjects::Create();
00116                                                 gl_corner->setPose( p );
00117                                                 if (show_ID_labels) // don't show IDs twice!
00118                                                 {
00119                                                         gl_corner->setName(format("%u",static_cast<unsigned int>(itNod->first) ));
00120                                                         gl_corner->enableShowName();
00121                                                 }
00122                                                 ret->insert( gl_corner );
00123                                         }
00124                                 } // end draw node corners
00125 
00126                                 if (show_edges)
00127                                 {
00128                                         CSetOfLinesPtr  gl_edges = CSetOfLines::Create();
00129                                         const TColor col8bit(edge_color & 0xffffff);
00130                                         const uint8_t col_alpha = edge_color >> 24;
00131 
00132                                         gl_edges->setColor( col8bit.R*(1./255), col8bit.G*(1./255), col8bit.B*(1./255), col_alpha*(1./255) );
00133                                         gl_edges->setLineWidth( edge_width );
00134 
00135                                         for (typename GRAPH_T::const_iterator itEd = g.begin();itEd!=g.end();++itEd)
00136                                         {
00137                                                 const TNodeID id1 = itEd->first.first;
00138                                                 const TNodeID id2 = itEd->first.second;
00139 
00140                                                 // Draw only if we have the global coords of both nodes:
00141                                                 typename GRAPH_T::global_poses_t::const_iterator itNod1 = g.nodes.find(id1);
00142                                                 typename GRAPH_T::global_poses_t::const_iterator itNod2 = g.nodes.find(id2);
00143                                                 if (itNod1!=g.nodes.end() && itNod2!=g.nodes.end())
00144                                                 {
00145                                                         const CPose3D p1 = CPose3D(itNod1->second);
00146                                                         const CPose3D p2 = CPose3D(itNod2->second);
00147                                                         gl_edges->appendLine( TPoint3D(p1.x(),p1.y(),p1.z()), TPoint3D(p2.x(),p2.y(),p2.z()) );
00148                                                 }
00149                                         }
00150                                         ret->insert( gl_edges );
00151 
00152                                 } // end draw edges
00153 
00154                                 return ret;
00155 
00156                                 MRPT_TRY_END
00157 
00158                         }
00159 
00160                 }
00161         }
00162 }
00163 
00164 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011