Main MRPT website > C++ reference for MRPT 1.9.9
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-2017, 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>
27  const GRAPH_T& g, const mrpt::utils::TParametersDouble& extra_params)
28 {
30 
32  using mrpt::math::TPose3D;
33  using namespace mrpt::utils;
34 
35  // Is a 2D or 3D graph network?
36  typedef typename GRAPH_T::constraint_t constraint_t;
37 
38  const bool is_3D_graph = constraint_t::is_3D();
39 
40  // create opengl obejct to be filled.
41  CSetOfObjects::Ptr ret = mrpt::make_aligned_shared<CSetOfObjects>();
42 
43  // graph visualization parameters
44  const bool show_ID_labels =
45  0 != extra_params.getWithDefaultVal("show_ID_labels", 0);
46  const bool show_ground_grid =
47  0 != extra_params.getWithDefaultVal("show_ground_grid", 1);
48  const bool show_edges =
49  0 != extra_params.getWithDefaultVal("show_edges", 1);
50  const bool show_node_corners =
51  0 != extra_params.getWithDefaultVal("show_node_corners", 1);
52  const bool show_edge_rel_poses =
53  0 != extra_params.getWithDefaultVal("show_edge_rel_poses", 0);
54  const double nodes_point_size =
55  extra_params.getWithDefaultVal("nodes_point_size", 0.);
56  const double nodes_corner_scale =
57  extra_params.getWithDefaultVal("nodes_corner_scale", 0.7);
58  const double nodes_edges_corner_scale =
59  extra_params.getWithDefaultVal("nodes_edges_corner_scale", 0.4);
60  const unsigned int nodes_point_color = extra_params.getWithDefaultVal(
61  "nodes_point_color", (unsigned int)0xA0A0A0);
62  const unsigned int edge_color =
63  extra_params.getWithDefaultVal("edge_color", (unsigned int)0x400000FF);
64  const unsigned int edge_rel_poses_color = extra_params.getWithDefaultVal(
65  "edge_rel_poses_color", (unsigned int)0x40FF8000);
66  const double edge_width = extra_params.getWithDefaultVal("edge_width", 2.);
67 
68  if (show_ground_grid)
69  {
70  // Estimate bounding box.
71  mrpt::math::TPoint3D BB_min(-10., -10., 0.), BB_max(10., 10., 0.);
72 
73  for (typename GRAPH_T::global_poses_t::const_iterator itNod =
74  g.nodes.begin();
75  itNod != g.nodes.end(); ++itNod)
76  {
77  const CPose3D p = CPose3D(
78  itNod->second); // Convert to 3D from whatever its real type.
79 
80  keep_min(BB_min.x, p.x());
81  keep_min(BB_min.y, p.y());
82  keep_min(BB_min.z, p.z());
83 
84  keep_max(BB_max.x, p.x());
85  keep_max(BB_max.y, p.y());
86  keep_max(BB_max.z, p.z());
87  }
88 
89  // Create ground plane:
90  const double grid_frequency = 5.0;
91  CGridPlaneXY::Ptr grid = mrpt::make_aligned_shared<CGridPlaneXY>(
92  BB_min.x, BB_max.x, BB_min.y, BB_max.y, BB_min.z, grid_frequency);
93  grid->setColor(0.3, 0.3, 0.3);
94  ret->insert(grid);
95  } // end show_ground_grid
96 
97  // Draw nodes as thick points:
98  if (nodes_point_size > 0)
99  {
100  CPointCloud::Ptr pnts = mrpt::make_aligned_shared<CPointCloud>();
101  pnts->setColor(TColorf(TColor(nodes_point_color)));
102  pnts->setPointSize(nodes_point_size);
103 
104  // Add nodes:
105  for (typename GRAPH_T::global_poses_t::const_iterator itNod =
106  g.nodes.begin();
107  itNod != g.nodes.end(); ++itNod)
108  {
109  const CPose3D p = CPose3D(
110  itNod->second); // Convert to 3D from whatever its real type.
111  pnts->insertPoint(p.x(), p.y(), p.z());
112  }
113 
114  pnts->enablePointSmooth();
115 
116  ret->insert(pnts);
117  } // end draw node points
118 
119  // Show a 2D corner at each node (or just an empty object with the ID label)
120  if (show_node_corners || show_ID_labels)
121  {
122  for (typename GRAPH_T::global_poses_t::const_iterator itNod =
123  g.nodes.begin();
124  itNod != g.nodes.end(); ++itNod)
125  {
126  const CPose3D p = CPose3D(
127  itNod->second); // Convert to 3D from whatever its real type.
128  CSetOfObjects::Ptr gl_corner =
129  show_node_corners
130  ? (is_3D_graph
132  nodes_corner_scale, 1.0 /*line width*/)
134  nodes_corner_scale, 1.0 /*line width*/))
135  : mrpt::make_aligned_shared<CSetOfObjects>();
136  gl_corner->setPose(p);
137  if (show_ID_labels) // don't show IDs twice!
138  {
139  gl_corner->setName(
140  format("%u", static_cast<unsigned int>(itNod->first)));
141  gl_corner->enableShowName();
142  }
143  ret->insert(gl_corner);
144  }
145  } // end draw node corners
146 
147  if (show_edge_rel_poses)
148  {
149  const TColor col8bit(
150  edge_rel_poses_color & 0xffffff, edge_rel_poses_color >> 24);
151 
152  for (typename GRAPH_T::const_iterator itEd = g.begin(); itEd != g.end();
153  ++itEd)
154  {
155  // Node ID of the source pose:
156  const TNodeID node_id_start = g.edges_store_inverse_poses
157  ? itEd->first.second
158  : itEd->first.first;
159 
160  // Draw only if we have the global coords of starting nodes:
162  g.nodes.find(node_id_start);
163  if (itNod != g.nodes.end())
164  {
165  const CPose3D pSource = CPose3D(itNod->second);
166  // Create a set of objects at that pose and do the rest in
167  // relative coords:
169  mrpt::make_aligned_shared<mrpt::opengl::CSetOfObjects>();
170  gl_rel_edge->setPose(pSource);
171 
172  const typename GRAPH_T::constraint_no_pdf_t& edge_pose =
173  itEd->second.getPoseMean();
174  const mrpt::poses::CPoint3D edge_pose_pt =
175  mrpt::poses::CPoint3D(edge_pose);
176 
177  mrpt::opengl::CSetOfObjects::Ptr gl_edge_corner =
178  (is_3D_graph
180  nodes_edges_corner_scale, 1.0 /*line width*/)
182  nodes_edges_corner_scale, 1.0 /*line width*/));
183 
184  gl_edge_corner->setPose(edge_pose);
185  gl_rel_edge->insert(gl_edge_corner);
186 
188  mrpt::make_aligned_shared<mrpt::opengl::CSimpleLine>(
189  0, 0, 0, edge_pose_pt.x(), edge_pose_pt.y(),
190  edge_pose_pt.z());
191  gl_line->setColor_u8(col8bit);
192  gl_line->setLineWidth(edge_width);
193  gl_rel_edge->insert(gl_line);
194 
195  ret->insert(gl_rel_edge);
196  }
197  }
198  }
199 
200  if (show_edges)
201  {
202  CSetOfLines::Ptr gl_edges = mrpt::make_aligned_shared<CSetOfLines>();
203  const TColor col8bit(edge_color & 0xffffff, edge_color >> 24);
204 
205  gl_edges->setColor_u8(col8bit);
206  gl_edges->setLineWidth(edge_width);
207 
208  for (typename GRAPH_T::const_iterator itEd = g.begin(); itEd != g.end();
209  ++itEd)
210  {
211  const TNodeID id1 = itEd->first.first;
212  const TNodeID id2 = itEd->first.second;
213 
214  // Draw only if we have the global coords of both nodes:
216  g.nodes.find(id1);
218  g.nodes.find(id2);
219  if (itNod1 != g.nodes.end() && itNod2 != g.nodes.end())
220  {
221  const CPose3D p1 = CPose3D(itNod1->second);
222  const CPose3D p2 = CPose3D(itNod2->second);
223  gl_edges->appendLine(
224  mrpt::math::TPoint3D(p1.x(), p1.y(), p1.z()),
225  mrpt::math::TPoint3D(p2.x(), p2.y(), p2.z()));
226  }
227  }
228  ret->insert(gl_edges);
229 
230  } // end draw edges
231 
232  return ret;
233 
235 }
236 
237 } // end of graph_tools namespace
238 } // end of opengl namespace
239 } // end of mrpt namespace
240 
241 #endif
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:135
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
CSetOfObjects::Ptr CornerXYSimple(float scale=1.0, float lineWidth=1.0)
Returns two arrows representing a X,Y 2D corner (just thick lines, fast to render).
CSetOfObjects::Ptr 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...
const Scalar * const_iterator
Definition: eigen_plugins.h:27
#define MRPT_TRY_END
std::shared_ptr< CSetOfObjects > Ptr
Definition: CSetOfObjects.h:32
GLubyte g
Definition: glext.h:6279
A RGB color - 8bit.
Definition: TColor.h:25
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
double x
X,Y,Z coordinates.
uint64_t TNodeID
The type for node IDs in graphs of different types.
Definition: types_simple.h:49
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:227
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:220
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
CSetOfObjects::Ptr 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...
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:106
Lightweight 3D pose (three spatial coordinates, plus three angular coordinates).
#define MRPT_TRY_START
A RGB color - floats in the range [0,1].
Definition: TColor.h:78
std::shared_ptr< CSetOfLines > Ptr
Definition: CSetOfLines.h:37
std::shared_ptr< CPointCloud > Ptr
Definition: CPointCloud.h:52
Lightweight 3D point.
std::shared_ptr< CSimpleLine > Ptr
Definition: CSimpleLine.h:24
GLfloat GLfloat p
Definition: glext.h:6305
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019