Main MRPT website > C++ reference for MRPT 1.9.9
ClearanceDiagram.cpp
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 
10 #include "nav-precomp.h" // Precomp header
11 
14 #include <mrpt/opengl/CMesh.h>
15 #include <mrpt/utils/CStream.h>
16 #include <mrpt/utils/round.h>
18 #include <limits>
19 
20 using namespace mrpt::nav;
21 
23  : m_actual_num_paths(0), m_k_a2d(.0), m_k_d2a(.0)
24 {
25 }
26 
28  mrpt::opengl::CMesh& mesh, double min_x, double max_x, double min_y,
29  double max_y, double cell_res, bool integrate_over_path) const
30 {
31  ASSERT_(cell_res > 0.0);
32  ASSERT_(max_x > min_x);
33  ASSERT_(max_y > min_y);
34 
35  mesh.setXBounds(min_x, max_x);
36  mesh.setYBounds(min_y, max_y);
37  const int nX = (int)::ceil((max_x - min_x) / cell_res);
38  const int nY = (int)::ceil((max_y - min_y) / cell_res);
39  const double dx = (max_x - min_x) / nX;
40  const double dy = (max_y - min_y) / nY;
41 
42  mrpt::math::CMatrixFloat Z(nX, nY);
43 
44  if (m_raw_clearances.empty()) return; // Nothing to do: empty structure!
45 
46  for (int iX = 0; iX < nX; iX++)
47  {
48  const double x = min_x + dx * (0.5 + iX);
49  for (int iY = 0; iY < nY; iY++)
50  {
51  const double y = min_y + dy * (0.5 + iY);
52 
53  double clear_val = .0;
54  if (x != 0 || y != 0)
55  {
56  const double alpha = ::atan2(y, x);
57  const uint16_t actual_k =
60  const double dist = std::hypot(x, y);
61  clear_val =
62  this->getClearance(actual_k, dist, integrate_over_path);
63  }
64  Z(iX, iY) = clear_val;
65  }
66  }
67 
68  mesh.setZ(Z);
69  mesh.enableColorFromZ(true);
70  mesh.enableTransparency(true);
71  mesh.setColorA_u8(0x50);
72  mesh.enableWireFrame(false);
73 }
74 
76 {
77  uint8_t version;
78  in >> version;
79  switch (version)
80  {
81  case 0:
82  uint32_t decim_num;
83  in.ReadAsAndCastTo<uint32_t, size_t>(m_actual_num_paths);
84  in >> decim_num;
85  this->resize(m_actual_num_paths, decim_num);
86  in >> m_raw_clearances;
87  break;
88  default:
90  };
91 }
92 
94 {
95  const uint8_t version = 0;
96  out << version;
97 
98  out << uint32_t(m_actual_num_paths) << uint32_t(m_raw_clearances.size());
99  out << m_raw_clearances;
100 }
101 
103  size_t actual_k)
104 {
105  return m_raw_clearances[real_k_to_decimated_k(actual_k)];
106 }
107 
109  size_t actual_k) const
110 {
111  return m_raw_clearances[real_k_to_decimated_k(actual_k)];
112 }
113 
115 {
116  ASSERT_(m_actual_num_paths > 0 && !m_raw_clearances.empty());
117  const size_t ret = mrpt::utils::round(k * m_k_a2d);
118  ASSERT_(ret < m_raw_clearances.size());
119  return ret;
120 }
121 
123 {
124  ASSERT_(m_actual_num_paths > 0 && !m_raw_clearances.empty());
125  const size_t ret = mrpt::utils::round(k * m_k_d2a);
126  ASSERT_(ret < m_actual_num_paths);
127  return ret;
128 }
129 
131  uint16_t actual_k, double dist, bool integrate_over_path) const
132 {
133  if (this->empty()) // If we are not using clearance values, just return a
134  // fixed value:
135  return 0.0;
136 
138 
139  const size_t k = real_k_to_decimated_k(actual_k);
140 
141  const auto& rc_k = m_raw_clearances[k];
142 
143  double res = 0;
144  int avr_count = 0; // weighted avrg: closer to query points weight more
145  // than at path start.
146  for (const auto& e : rc_k)
147  {
148  if (!integrate_over_path)
149  {
150  // dont integrate: forget past part:
151  res = 0;
152  avr_count = 0;
153  }
154  // Keep min clearance along straight path:
155  res += e.second;
156  avr_count++;
157 
158  if (e.first > dist) break; // target dist reached.
159  }
160 
161  if (!avr_count)
162  {
163  res = rc_k.begin()->second;
164  }
165  else
166  {
167  res = res / avr_count;
168  }
169  return res;
170 }
171 
173 {
174  m_actual_num_paths = 0;
175  m_raw_clearances.clear();
176  m_k_a2d = m_k_d2a = .0;
177 }
178 
180  size_t actual_num_paths, size_t decimated_num_paths)
181 {
182  if (decimated_num_paths == 0)
183  {
184  this->clear();
185  return;
186  }
187  ASSERT_ABOVEEQ_(actual_num_paths, decimated_num_paths);
188 
189  m_actual_num_paths = actual_num_paths;
190  m_raw_clearances.resize(decimated_num_paths);
191 
192  m_k_d2a = double(m_actual_num_paths - 1) / (m_raw_clearances.size() - 1);
193  m_k_a2d = double(m_raw_clearances.size() - 1) / (m_actual_num_paths - 1);
194 }
GLclampf GLclampf GLclampf alpha
Definition: glext.h:3525
unsigned __int16 uint16_t
Definition: rptypes.h:44
CRenderizable & setColorA_u8(const uint8_t a) override
Color components in the range [0,255].
void setZ(const mrpt::math::CMatrixTemplateNumeric< float > &in_Z)
This method sets the matrix of heights for each position (cell) in the mesh grid. ...
Definition: CMesh.cpp:538
void enableTransparency(bool v)
Definition: CMesh.h:122
#define ASSERT_BELOW_(__A, __B)
void clear()
Reset to default, empty state.
std::vector< dist2clearance_t > m_raw_clearances
Container: [decimated_path_k][TPS_distance] => normalized_clearance_for_exactly_that_robot_pose.
void enableWireFrame(bool v)
Definition: CMesh.h:127
void clear()
Clear the contents of this container.
Definition: ts_hash_map.h:189
void writeToStream(mrpt::utils::CStream &out) const
dist2clearance_t & get_path_clearance(size_t actual_k)
unsigned char uint8_t
Definition: rptypes.h:41
double getClearance(uint16_t k, double TPS_query_distance, bool integrate_over_path) const
Gets the clearance for path k and distance TPS_query_distance in one of two modes: ...
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
size_t decimated_k_to_real_k(size_t k) const
std::map< double, double > dist2clearance_t
[TPS_distance] => normalized_clearance_for_exactly_that_robot_pose
size_t real_k_to_decimated_k(size_t k) const
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
void setYBounds(const float min, const float max)
Definition: CMesh.h:204
void resize(size_t actual_num_paths, size_t decimated_num_paths)
Initializes the container to allocate decimated_num_paths entries, as a decimated subset of a total o...
void readFromStream(mrpt::utils::CStream &in)
#define ASSERT_ABOVEEQ_(__A, __B)
void renderAs3DObject(mrpt::opengl::CMesh &mesh, double min_x, double max_x, double min_y, double max_y, double cell_res, bool integrate_over_path) const
void enableColorFromZ(bool v, mrpt::utils::TColormap colorMap=mrpt::utils::cmHOT)
Definition: CMesh.h:132
GLuint in
Definition: glext.h:7274
#define ASSERT_(f)
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:25
GLenum GLint GLint y
Definition: glext.h:3538
void setXBounds(const float min, const float max)
Definition: CMesh.h:197
A planar (XY) grid where each cell has an associated height and, optionally, a texture map...
Definition: CMesh.h:39
GLuint res
Definition: glext.h:7268
GLenum GLint x
Definition: glext.h:3538
unsigned __int32 uint32_t
Definition: rptypes.h:47
uint16_t alpha2index(double alpha) const
Discrete index value for the corresponding alpha value.



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