Main MRPT website > C++ reference for MRPT 1.9.9
CVisualizer_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 CVISUALIZER_IMPL_H
10 #define CVISUALIZER_IMPL_H
11 
12 namespace mrpt
13 {
14 namespace graphs
15 {
16 namespace detail
17 {
18 // constructor, destructor
19 ////////////////////////////////////////////////////////////
20 template <class CPOSE, class MAPS_IMPLEMENTATION, class NODE_ANNOTATIONS,
21  class EDGE_ANNOTATIONS>
22 CVisualizer<CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS,
23  EDGE_ANNOTATIONS>::CVisualizer(const GRAPH_T& graph_in)
24  : m_graph(graph_in)
25 {
26  // Is a 2D or 3D graph network?
27  using constraint_t = typename GRAPH_T::constraint_t;
28  m_is_3D_graph = constraint_t::is_3D();
29 }
30 template <class CPOSE, class MAPS_IMPLEMENTATION, class NODE_ANNOTATIONS,
31  class EDGE_ANNOTATIONS>
32 CVisualizer<CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS,
33  EDGE_ANNOTATIONS>::~CVisualizer()
34 {
35 }
36 
37 // methods implementations
38 ////////////////////////////////////////////////////////////
39 template <class CPOSE, class MAPS_IMPLEMENTATION, class NODE_ANNOTATIONS,
40  class EDGE_ANNOTATIONS>
41 void CVisualizer<CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS,
42  EDGE_ANNOTATIONS>::
43  getAs3DObject(
45  mrpt::system::TParametersDouble viz_params) const
46 {
47  using namespace mrpt::opengl;
48 
49  // graph visualization parameters
50  const bool show_ID_labels =
51  0 != viz_params.getWithDefaultVal("show_ID_labels", 0);
52  const bool show_ground_grid =
53  0 != viz_params.getWithDefaultVal("show_ground_grid", 1);
54  const bool show_edges = 0 != viz_params.getWithDefaultVal("show_edges", 1);
55  const bool show_node_corners =
56  0 != viz_params.getWithDefaultVal("show_node_corners", 1);
57  const bool show_edge_rel_poses =
58  0 != viz_params.getWithDefaultVal("show_edge_rel_poses", 0);
59  const double nodes_point_size =
60  viz_params.getWithDefaultVal("nodes_point_size", 0.);
61 
62  if (show_ground_grid)
63  {
64  this->drawGroundGrid(object, &viz_params);
65  }
66 
67  if (nodes_point_size > 0)
68  {
69  this->drawNodePoints(object, &viz_params);
70  }
71 
72  if (show_node_corners || show_ID_labels)
73  {
74  this->drawNodeCorners(object, &viz_params);
75  }
76 
77  if (show_edge_rel_poses)
78  {
79  this->drawEdgeRelPoses(object, &viz_params);
80  }
81 
82  if (show_edges)
83  {
84  this->drawEdges(object, &viz_params);
85  }
86 }
87 
88 template <class CPOSE, class MAPS_IMPLEMENTATION, class NODE_ANNOTATIONS,
89  class EDGE_ANNOTATIONS>
90 void CVisualizer<CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS,
91  EDGE_ANNOTATIONS>::
92  drawGroundGrid(
94  const mrpt::system::TParametersDouble* viz_params /*=NULL*/) const
95 {
96  using namespace mrpt::opengl;
97  ASSERTMSG_(viz_params, "Pointer to viz_params was not provided.");
98 
99  // Estimate bounding box.
100  mrpt::math::TPoint3D BB_min(-10., -10., 0.), BB_max(10., 10., 0.);
101 
102  for (typename GRAPH_T::global_poses_t::const_iterator n_it =
103  m_graph.nodes.begin();
104  n_it != m_graph.nodes.end(); ++n_it)
105  {
106  const CPose3D p = CPose3D(
107  n_it->second); // Convert to 3D from whatever its real type.
108 
109  keep_min(BB_min.x, p.x());
110  keep_min(BB_min.y, p.y());
111  keep_min(BB_min.z, p.z());
112 
113  keep_max(BB_max.x, p.x());
114  keep_max(BB_max.y, p.y());
115  keep_max(BB_max.z, p.z());
116  }
117 
118  // Create ground plane:
119  const double grid_frequency = 5.0;
121  BB_min.x, BB_max.x, BB_min.y, BB_max.y, BB_min.z, grid_frequency);
122  grid->setColor(0.3, 0.3, 0.3);
123  object->insert(grid);
124 }
125 
126 template <class CPOSE, class MAPS_IMPLEMENTATION, class NODE_ANNOTATIONS,
127  class EDGE_ANNOTATIONS>
128 void CVisualizer<CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS,
129  EDGE_ANNOTATIONS>::
130  drawNodePoints(
132  const mrpt::system::TParametersDouble* viz_params /*=NULL*/) const
133 {
134  ASSERTMSG_(viz_params, "Pointer to viz_params was not provided.");
135 
136  using namespace mrpt::opengl;
137  using namespace mrpt::img;
138 
139  const double nodes_point_size =
140  viz_params->getWithDefaultVal("nodes_point_size", 0.);
141  const unsigned int nodes_point_color = viz_params->getWithDefaultVal(
142  "nodes_point_color", (unsigned int)0xA0A0A0);
143 
144  CPointCloud::Ptr pnts = mrpt::make_aligned_shared<CPointCloud>();
145  pnts->setColor(TColorf(TColor(nodes_point_color)));
146  pnts->setPointSize(nodes_point_size);
147 
148  // Add all nodesnodes:
149  for (typename GRAPH_T::global_poses_t::const_iterator n_it =
150  m_graph.nodes.begin();
151  n_it != m_graph.nodes.end(); ++n_it)
152  {
153  const CPose3D p = CPose3D(
154  n_it->second); // Convert to 3D from whatever its real type.
155  pnts->insertPoint(p.x(), p.y(), p.z());
156  }
157 
158  pnts->enablePointSmooth();
159  object->insert(pnts);
160 }
161 
162 template <class CPOSE, class MAPS_IMPLEMENTATION, class NODE_ANNOTATIONS,
163  class EDGE_ANNOTATIONS>
164 void CVisualizer<CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS,
165  EDGE_ANNOTATIONS>::
166  drawNodeCorners(
168  const mrpt::system::TParametersDouble* viz_params /*=NULL*/) const
169 {
170  using namespace mrpt::opengl;
171  using mrpt::poses::CPose3D;
172 
173  ASSERTMSG_(viz_params, "Pointer to viz_params was not provided.");
174 
175  const bool show_node_corners =
176  0 != viz_params->getWithDefaultVal("show_node_corners", 1);
177  const bool show_ID_labels =
178  0 != viz_params->getWithDefaultVal("show_ID_labels", 0);
179  const double nodes_corner_scale =
180  viz_params->getWithDefaultVal("nodes_corner_scale", 0.7);
181 
182  for (typename GRAPH_T::global_poses_t::const_iterator n_it =
183  m_graph.nodes.begin();
184  n_it != m_graph.nodes.end(); ++n_it)
185  {
186  // Convert to 3D from whatever its real type. CSetOfObjects::Ptr
187  // gl_corner = show_node_corners ?
188  const CPose3D p = CPose3D(n_it->second);
189  CSetOfObjects::Ptr gl_corner =
190  show_node_corners
191  ? (m_is_3D_graph ? stock_objects::CornerXYZSimple(
192  nodes_corner_scale, 1.0 /*line width*/)
194  nodes_corner_scale, 1.0 /*line width*/))
195  : mrpt::make_aligned_shared<CSetOfObjects>();
196  gl_corner->setPose(p);
197  if (show_ID_labels)
198  { // don't show IDs twice!
199  gl_corner->setName(
200  format("%u", static_cast<unsigned int>(n_it->first)));
201  gl_corner->enableShowName();
202  }
203  object->insert(gl_corner);
204  }
205 }
206 
207 template <class CPOSE, class MAPS_IMPLEMENTATION, class NODE_ANNOTATIONS,
208  class EDGE_ANNOTATIONS>
209 void CVisualizer<CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS,
210  EDGE_ANNOTATIONS>::
211  drawEdgeRelPoses(
213  const mrpt::system::TParametersDouble* viz_params /*=NULL*/) const
214 {
215  using namespace mrpt::opengl;
216  using namespace mrpt::img;
217  using mrpt::graphs::TNodeID;
218  ASSERTMSG_(viz_params, "Pointer to viz_params was not provided.");
219 
220  const double nodes_edges_corner_scale =
221  viz_params->getWithDefaultVal("nodes_edges_corner_scale", 0.4);
222  const unsigned int edge_rel_poses_color = viz_params->getWithDefaultVal(
223  "edge_rel_poses_color", (unsigned int)0x40FF8000);
224  const TColor col8bit(
225  edge_rel_poses_color & 0xffffff, edge_rel_poses_color >> 24);
226  const double edge_width = viz_params->getWithDefaultVal("edge_width", 2.);
227 
228  for (auto edge_it = m_graph.begin(); edge_it != m_graph.end(); ++edge_it)
229  {
230  // Node ID of the source pose:
231  const TNodeID node_id_start = m_graph.edges_store_inverse_poses
232  ? edge_it->first.second
233  : edge_it->first.first;
234 
235  // Draw only if we have the global coords of starting nodes:
237  m_graph.nodes.find(node_id_start);
238  if (n_it != m_graph.nodes.end())
239  {
240  const CPose3D pSource = CPose3D(n_it->second);
241  // Create a set of objects at that pose and do the rest in relative
242  // coords:
244  mrpt::make_aligned_shared<mrpt::opengl::CSetOfObjects>();
245  gl_rel_edge->setPose(pSource);
246 
247  const typename GRAPH_T::constraint_no_pdf_t& edge_pose =
248  edge_it->second.getPoseMean();
249  const mrpt::poses::CPoint3D edge_pose_pt =
250  mrpt::poses::CPoint3D(edge_pose);
251 
252  mrpt::opengl::CSetOfObjects::Ptr gl_edge_corner =
253  (m_is_3D_graph
255  nodes_edges_corner_scale, 1.0 /*line width*/)
257  nodes_edges_corner_scale, 1.0 /*line width*/));
258 
259  gl_edge_corner->setPose(edge_pose);
260  gl_rel_edge->insert(gl_edge_corner);
261 
264  0, 0, 0, edge_pose_pt.x(), edge_pose_pt.y(),
265  edge_pose_pt.z());
266 
267  gl_line->setColor_u8(col8bit);
268  gl_line->setLineWidth(edge_width);
269  gl_rel_edge->insert(gl_line);
270 
271  object->insert(gl_rel_edge);
272  }
273  }
274 }
275 
276 template <class CPOSE, class MAPS_IMPLEMENTATION, class NODE_ANNOTATIONS,
277  class EDGE_ANNOTATIONS>
278 void CVisualizer<CPOSE, MAPS_IMPLEMENTATION, NODE_ANNOTATIONS,
279  EDGE_ANNOTATIONS>::
280  drawEdges(
282  const mrpt::system::TParametersDouble* viz_params /*=NULL*/) const
283 {
284  using namespace mrpt::opengl;
285  using namespace mrpt::img;
286  using namespace mrpt::poses;
287  ASSERTMSG_(viz_params, "Pointer to viz_params was not provided.");
288 
289  CSetOfLines::Ptr gl_edges = mrpt::make_aligned_shared<CSetOfLines>();
290  const unsigned int edge_color =
291  viz_params->getWithDefaultVal("edge_color", (unsigned int)0x400000FF);
292  const double edge_width = viz_params->getWithDefaultVal("edge_width", 2.);
293 
294  const TColor col8bit(edge_color & 0xffffff, edge_color >> 24);
295 
296  gl_edges->setColor_u8(col8bit);
297  gl_edges->setLineWidth(edge_width);
298 
299  // for all registered edges.
300  for (typename GRAPH_T::const_iterator edge_it = m_graph.begin();
301  edge_it != m_graph.end(); ++edge_it)
302  {
303  const TNodeID id1 = edge_it->first.first;
304  const TNodeID id2 = edge_it->first.second;
305 
306  // Draw only if we have the global coords of both nodes:
308  m_graph.nodes.find(id1);
310  m_graph.nodes.find(id2);
311  if (n_it1 != m_graph.nodes.end() && n_it2 != m_graph.nodes.end())
312  { // both nodes found?
313  const CPose3D p1 = CPose3D(n_it1->second);
314  const CPose3D p2 = CPose3D(n_it2->second);
315  gl_edges->appendLine(
316  mrpt::math::TPoint3D(p1.x(), p1.y(), p1.z()),
317  mrpt::math::TPoint3D(p2.x(), p2.y(), p2.z()));
318  }
319  }
320  object->insert(gl_edges);
321 }
322 }
323 }
324 } // end of namespaces
325 
326 #endif /* end of include guard: CVISUALIZER_IMPL_H */
mrpt::keep_min
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: core/include/mrpt/core/bits_math.h:124
mrpt::opengl::CGridPlaneXY::Ptr
std::shared_ptr< CGridPlaneXY > Ptr
Definition: CGridPlaneXY.h:34
mrpt::opengl::CSetOfLines::Ptr
std::shared_ptr< CSetOfLines > Ptr
Definition: CSetOfLines.h:35
mrpt::opengl::CSimpleLine::Ptr
std::shared_ptr< CSimpleLine > Ptr
Definition: CSimpleLine.h:24
mrpt::math::TPoint3D::z
double z
Definition: lightweight_geom_data.h:385
mrpt::graphs::TNodeID
uint64_t TNodeID
A generic numeric type for unique IDs of nodes or entities.
Definition: TNodeID.h:17
mrpt::graphs::detail::CVisualizer
Base class for C*Visualizer classes.
Definition: CNetworkOfPoses.h:55
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::opengl::stock_objects::CornerXYZSimple
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...
Definition: StockObjects.cpp:419
p
GLfloat GLfloat p
Definition: glext.h:6305
mrpt::poses
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
Definition: CHierarchicalMapMHPartition.h:25
mrpt::graphs::CNetworkOfPoses::constraint_t
CPOSE constraint_t
The type of PDF poses in the contraints (edges) (=CPOSE template argument)
Definition: CNetworkOfPoses.h:136
mrpt::opengl::CPointCloud::Ptr
std::shared_ptr< CPointCloud > Ptr
Definition: CPointCloud.h:50
mrpt::img
Definition: CCanvas.h:17
mrpt::poses::CPoseOrPoint::y
double y() const
Definition: CPoseOrPoint.h:144
mrpt::opengl::CGridPlaneXY::Create
static Ptr Create(Args &&... args)
Definition: CGridPlaneXY.h:34
mrpt::format
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
mrpt::keep_max
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: core/include/mrpt/core/bits_math.h:131
mrpt::system::TParameters< double >
mrpt::poses::CPoseOrPoint::x
double x() const
Common members of all points & poses classes.
Definition: CPoseOrPoint.h:140
mrpt::poses::CPose3D
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:88
mrpt::math::TPoint3D::x
double x
X,Y,Z coordinates.
Definition: lightweight_geom_data.h:385
mrpt::img::TColorf
A RGB color - floats in the range [0,1].
Definition: TColor.h:79
mrpt::img::TColor
A RGB color - 8bit.
Definition: TColor.h:22
mrpt::opengl::CSetOfObjects::Ptr
std::shared_ptr< CSetOfObjects > Ptr
Definition: CSetOfObjects.h:30
ASSERTMSG_
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
mrpt::math::TPoint3D
Lightweight 3D point.
Definition: lightweight_geom_data.h:378
mrpt::math::TPoint3D::y
double y
Definition: lightweight_geom_data.h:385
mrpt::system::TParameters::getWithDefaultVal
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:105
mrpt::graphs::CNetworkOfPoses
A directed graph of pose constraints, with edges being the relative poses between pairs of nodes iden...
Definition: CNetworkOfPoses.h:122
mrpt::opengl::stock_objects::CornerXYSimple
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).
Definition: StockObjects.cpp:447
mrpt::graphs::CDirectedGraph< CPOSE, mrpt::graphs::detail::edge_annotations_empty >::const_iterator
typename edges_map_t::const_iterator const_iterator
Definition: CDirectedGraph.h:94
mrpt::poses::CPoint3D
A class used to store a 3D point.
Definition: CPoint3D.h:33
mrpt::graphs::CNetworkOfPoses::constraint_no_pdf_t
typename CPOSE::type_value constraint_no_pdf_t
The type of edges or their means if they are PDFs (that is, a simple "edge" value)
Definition: CNetworkOfPoses.h:147
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
mrpt::opengl::CSimpleLine::Create
static Ptr Create(Args &&... args)
Definition: CSimpleLine.h:24



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST