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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020