Main MRPT website > C++ reference
MRPT logo
graph_tools_impl.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2014, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef opengl_graph_tools_impl_H
10 #define opengl_graph_tools_impl_H
11 
18 
19 namespace mrpt
20 {
21  namespace opengl
22  {
23  namespace graph_tools
24  {
25  template<class GRAPH_T>
26  CSetOfObjectsPtr graph_visualize(
27  const GRAPH_T &g,
28  const mrpt::utils::TParametersDouble &extra_params)
29  {
31 
33  using mrpt::math::TPose3D;
36 
37  // Is a 2D or 3D graph network?
38  typedef typename GRAPH_T::constraint_t constraint_t;
39 
40  const bool is_3D_graph = constraint_t::is_3D();
41 
42  CSetOfObjectsPtr ret = CSetOfObjects::Create();
43 
44  const bool show_ID_labels = 0!=extra_params.getWithDefaultVal("show_ID_labels", 0);
45  const bool show_ground_grid = 0!=extra_params.getWithDefaultVal("show_ground_grid", 1);
46  const bool show_edges = 0!=extra_params.getWithDefaultVal("show_edges", 1);
47  const bool show_node_corners = 0!=extra_params.getWithDefaultVal("show_node_corners", 1);
48  const bool show_edge_rel_poses = 0!=extra_params.getWithDefaultVal("show_edge_rel_poses", 0);
49  const double nodes_point_size = extra_params.getWithDefaultVal("nodes_point_size", 0. );
50  const double nodes_corner_scale = extra_params.getWithDefaultVal("nodes_corner_scale", 0.7 );
51  const double nodes_edges_corner_scale = extra_params.getWithDefaultVal("nodes_edges_corner_scale", 0.4 );
52  const unsigned int nodes_point_color = extra_params.getWithDefaultVal("nodes_point_color", (unsigned int)0xA0A0A0 );
53  const unsigned int edge_color = extra_params.getWithDefaultVal("edge_color", (unsigned int)0x400000FF );
54  const unsigned int edge_rel_poses_color = extra_params.getWithDefaultVal("edge_rel_poses_color", (unsigned int)0x40FF8000 );
55  const double edge_width = extra_params.getWithDefaultVal("edge_width", 2. );
56 
57  if (show_ground_grid)
58  {
59  // Estimate bounding box.
60  TPoint3D BB_min(-10.,-10.,0.), BB_max(10.,10.,0.);
61 
62  for (typename GRAPH_T::global_poses_t::const_iterator itNod = g.nodes.begin();itNod!=g.nodes.end();++itNod)
63  {
64  const CPose3D p = CPose3D(itNod->second); // Convert to 3D from whatever its real type.
65 
66  keep_min( BB_min.x, p.x() );
67  keep_min( BB_min.y, p.y() );
68  keep_min( BB_min.z, p.z() );
69 
70  keep_max( BB_max.x, p.x() );
71  keep_max( BB_max.y, p.y() );
72  keep_max( BB_max.z, p.z() );
73  }
74 
75  // Create ground plane:
76  const double grid_frequency = 5.0;
77  CGridPlaneXYPtr grid = CGridPlaneXY::Create(BB_min.x, BB_max.x, BB_min.y, BB_max.y, BB_min.z, grid_frequency);
78  grid->setColor(0.3,0.3,0.3);
79  ret->insert( grid );
80  } // end show_ground_grid
81 
82  // Draw nodes as thick points:
83  if (nodes_point_size!=0)
84  {
85  ASSERT_(nodes_point_size>=0);
86 
87  CPointCloudPtr pnts = CPointCloud::Create();
88  pnts->setColor( TColorf(TColor(nodes_point_color)) );
89  pnts->setPointSize(nodes_point_size);
90 
91  // Add nodes:
92  for (typename GRAPH_T::global_poses_t::const_iterator itNod = g.nodes.begin();itNod!=g.nodes.end();++itNod)
93  {
94  const CPose3D p = CPose3D(itNod->second); // Convert to 3D from whatever its real type.
95  pnts->insertPoint(p.x(),p.y(), p.z() );
96  }
97 
98  pnts->enablePointSmooth();
99 
100  ret->insert(pnts);
101  } // end draw node points
102 
103  // Show a 2D corner at each node (or just an empty object with the ID label)
104  if (show_node_corners || show_ID_labels)
105  {
106  for (typename GRAPH_T::global_poses_t::const_iterator itNod = g.nodes.begin();itNod!=g.nodes.end();++itNod)
107  {
108  const CPose3D p = CPose3D(itNod->second); // Convert to 3D from whatever its real type.
109  CSetOfObjectsPtr gl_corner = show_node_corners ?
110  (is_3D_graph ? stock_objects::CornerXYZSimple(nodes_corner_scale, 1.0 /*line width*/ ) : stock_objects::CornerXYSimple(nodes_corner_scale, 1.0 /*line width*/ ))
112  gl_corner->setPose( p );
113  if (show_ID_labels) // don't show IDs twice!
114  {
115  gl_corner->setName(format("%u",static_cast<unsigned int>(itNod->first) ));
116  gl_corner->enableShowName();
117  }
118  ret->insert( gl_corner );
119  }
120  } // end draw node corners
121 
122  if (show_edge_rel_poses)
123  {
124  const TColor col8bit(edge_rel_poses_color & 0xffffff, edge_rel_poses_color >> 24);
125 
126  for (typename GRAPH_T::const_iterator itEd = g.begin();itEd!=g.end();++itEd)
127  {
128  // Node ID of the source pose:
129  const TNodeID node_id_start = g.edges_store_inverse_poses ? itEd->first.second : itEd->first.first;
130 
131  // Draw only if we have the global coords of starting nodes:
132  typename GRAPH_T::global_poses_t::const_iterator itNod = g.nodes.find(node_id_start);
133  if (itNod!=g.nodes.end())
134  {
135  const CPose3D pSource = CPose3D(itNod->second);
136  // Create a set of objects at that pose and do the rest in relative coords:
137  mrpt::opengl::CSetOfObjectsPtr gl_rel_edge = mrpt::opengl::CSetOfObjects::Create();
138  gl_rel_edge->setPose(pSource);
139 
140  const typename GRAPH_T::constraint_no_pdf_t & edge_pose = itEd->second.getPoseMean();
141  const mrpt::poses::CPoint3D edge_pose_pt = mrpt::poses::CPoint3D(edge_pose);
142 
143  mrpt::opengl::CSetOfObjectsPtr gl_edge_corner =
144  (is_3D_graph ? stock_objects::CornerXYZSimple(nodes_edges_corner_scale, 1.0 /*line width*/ ) : stock_objects::CornerXYSimple(nodes_edges_corner_scale, 1.0 /*line width*/ ));
145 
146  gl_edge_corner->setPose(edge_pose);
147  gl_rel_edge->insert(gl_edge_corner);
148 
149  mrpt::opengl::CSimpleLinePtr gl_line = mrpt::opengl::CSimpleLine::Create(0,0,0, edge_pose_pt.x(), edge_pose_pt.y(), edge_pose_pt.z() );
150  gl_line->setColor_u8( col8bit );
151  gl_line->setLineWidth(edge_width);
152  gl_rel_edge->insert(gl_line);
153 
154  ret->insert( gl_rel_edge );
155  }
156  }
157  }
158 
159  if (show_edges)
160  {
161  CSetOfLinesPtr gl_edges = CSetOfLines::Create();
162  const TColor col8bit(edge_color & 0xffffff, edge_color >> 24);
163 
164  gl_edges->setColor_u8( col8bit );
165  gl_edges->setLineWidth( edge_width );
166 
167  for (typename GRAPH_T::const_iterator itEd = g.begin();itEd!=g.end();++itEd)
168  {
169  const TNodeID id1 = itEd->first.first;
170  const TNodeID id2 = itEd->first.second;
171 
172  // Draw only if we have the global coords of both nodes:
173  typename GRAPH_T::global_poses_t::const_iterator itNod1 = g.nodes.find(id1);
174  typename GRAPH_T::global_poses_t::const_iterator itNod2 = g.nodes.find(id2);
175  if (itNod1!=g.nodes.end() && itNod2!=g.nodes.end())
176  {
177  const CPose3D p1 = CPose3D(itNod1->second);
178  const CPose3D p2 = CPose3D(itNod2->second);
179  gl_edges->appendLine( TPoint3D(p1.x(),p1.y(),p1.z()), TPoint3D(p2.x(),p2.y(),p2.z()) );
180  }
181  }
182  ret->insert( gl_edges );
183 
184  } // end draw edges
185 
186  return ret;
187 
189 
190  }
191 
192  }
193  }
194 }
195 
196 #endif
T getWithDefaultVal(const std::string &s, const T &defaultVal) const
A const version of the [] operator and with a default value in case the parameter is not set (for usa...
Definition: TParameters.h:83
CSetOfObjectsPtr graph_visualize(const GRAPH_T &g, const mrpt::utils::TParametersDouble &extra_params=mrpt::utils::TParametersDouble())
Returns an opengl objects representation of an arbitrary graph, as a network of 3D pose frames...
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
uint64_t TNodeID
The type for node IDs in graphs of different types.
Definition: types_simple.h:39
const Scalar * const_iterator
Definition: eigen_plugins.h:24
double z
X,Y,Z coordinates.
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
static CSetOfLinesPtr Create()
#define MRPT_TRY_END
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:115
A RGB color - 8bit.
Definition: TColor.h:25
CSetOfObjectsPtr OPENGL_IMPEXP CornerXYZSimple(float scale=1.0, float lineWidth=1.0)
Returns three arrows representing a X,Y,Z 3D corner (just thick lines instead of complex arrows for f...
A class used to store a 3D point.
Definition: CPoint3D.h:32
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
Definition: bits.h:143
static CGridPlaneXYPtr Create()
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void keep_min(T &var, const K test_val)
If the second argument is below the first one, set the first argument to this lower value...
Definition: bits.h:137
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:69
static CSimpleLinePtr Create()
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
#define MRPT_TRY_START
#define ASSERT_(f)
A RGB color - floats in the range [0,1].
Definition: TColor.h:52
static CPointCloudPtr Create()
Lightweight 3D point.
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
CSetOfObjectsPtr OPENGL_IMPEXP CornerXYSimple(float scale=1.0, float lineWidth=1.0)
Returns two arrows representing a X,Y 2D corner (just thick lines, fast to render).
static CSetOfObjectsPtr Create()



Page generated by Doxygen 1.8.8 for MRPT 1.2.2 SVN:Unversioned directory at Tue Oct 14 02:14:08 UTC 2014