MRPT  1.9.9
CVectorField2D.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "opengl-precomp.h" // Precompiled header
11 
14 
15 using namespace mrpt;
16 using namespace mrpt::opengl;
17 using namespace mrpt::math;
18 using namespace std;
19 
21 
22 /** Constructor */
23 CVectorField2D::CVectorField2D() : xcomp(0, 0), ycomp(0, 0)
24 
25 {
26  m_point_color = m_color;
27  m_field_color = m_color;
28 }
29 
30 /** Constructor with a initial set of lines. */
32  CMatrixFloat Matrix_x, CMatrixFloat Matrix_y, float xmin, float xmax,
33  float ymin, float ymax)
34 {
35  MRPT_UNUSED_PARAM(Matrix_x);
36  MRPT_UNUSED_PARAM(Matrix_y);
37  MRPT_UNUSED_PARAM(xmin);
38  MRPT_UNUSED_PARAM(xmax);
39  MRPT_UNUSED_PARAM(ymin);
40  MRPT_UNUSED_PARAM(ymax);
41  m_point_color = m_color;
42  m_field_color = m_color;
43 }
44 
46 {
47  switch (rc.shader_id)
48  {
51  break;
54  break;
57  break;
58  };
59 }
61 {
65 }
66 
68 {
71  vbd.clear();
72 
73  vbd.reserve(xcomp.size() * 2);
74 
75  const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1);
76  const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1);
77 
78  for (int i = 0; i < xcomp.cols(); i++)
79  for (int j = 0; j < xcomp.rows(); j++)
80  {
81  vbd.emplace_back(xMin + i * x_cell_size, yMin + j * y_cell_size, 0);
82  vbd.emplace_back(
83  xMin + i * x_cell_size + xcomp(j, i),
84  yMin + j * y_cell_size + ycomp(j, i), 0);
85  }
86 
87  cbd.assign(vbd.size(), m_field_color);
88 }
90 {
91  using P3f = mrpt::math::TPoint3Df;
92 
94  tris.clear();
95 
96  tris.reserve(xcomp.size());
97 
98  const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1);
99  const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1);
100 
101  for (int i = 0; i < xcomp.cols(); i++)
102  for (int j = 0; j < xcomp.rows(); j++)
103  {
104  const float tri_side =
105  0.25f *
106  sqrt(xcomp(j, i) * xcomp(j, i) + ycomp(j, i) * ycomp(j, i));
107  const float ang = ::atan2f(ycomp(j, i), xcomp(j, i)) - 1.5708f;
108  tris.emplace_back(
109  P3f(-sin(ang) * 0.866f * tri_side + xMin + i * x_cell_size +
110  xcomp(j, i),
111  cos(ang) * 0.866f * tri_side + yMin + j * y_cell_size +
112  ycomp(j, i),
113  0),
114  P3f(cos(ang) * 0.5f * tri_side + xMin + i * x_cell_size +
115  xcomp(j, i),
116  sin(ang) * 0.5f * tri_side + yMin + j * y_cell_size +
117  ycomp(j, i),
118  0),
119  P3f(-cos(ang) * 0.5f * tri_side + xMin + i * x_cell_size +
120  xcomp(j, i),
121  -sin(ang) * 0.5f * tri_side + yMin + j * y_cell_size +
122  ycomp(j, i),
123  0));
124  }
125 
126  for (auto& t : tris)
127  {
128  t.computeNormals();
129  t.setColor(m_field_color);
130  }
131 }
133 {
136 
137  vbd.clear();
138  vbd.reserve(xcomp.size());
139 
140  const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1);
141  const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1);
142 
143  for (int i = 0; i < xcomp.cols(); i++)
144  for (int j = 0; j < xcomp.rows(); j++)
145  vbd.emplace_back(
146  xMin + i * x_cell_size, yMin + j * y_cell_size, .0f);
147 
148  cbd.assign(vbd.size(), m_point_color);
149 }
150 
151 /*---------------------------------------------------------------
152  Implements the writing to a CStream capability of
153  CSerializable objects
154  ---------------------------------------------------------------*/
155 uint8_t CVectorField2D::serializeGetVersion() const { return 0; }
157 {
158  writeToStreamRender(out);
159 
160  out << xcomp << ycomp;
161  out << xMin << xMax << yMin << yMax;
162  out << m_lineWidth;
163  out << m_pointSize;
164  out << m_antiAliasing;
165  out << m_point_color;
166  out << m_field_color;
167 }
168 
170  mrpt::serialization::CArchive& in, uint8_t version)
171 {
172  switch (version)
173  {
174  case 0:
175  readFromStreamRender(in);
176 
177  in >> xcomp >> ycomp;
178  in >> xMin >> xMax >> yMin >> yMax;
179  in >> m_lineWidth;
180  in >> m_pointSize;
181  in >> m_antiAliasing;
182  in >> m_point_color;
183  in >> m_field_color;
184  break;
185 
186  default:
188  break;
189  };
191 }
192 
195 {
196  bb_min.x = xMin;
197  bb_min.y = yMin;
198  bb_min.z = 0;
199 
200  bb_max.x = xMax;
201  bb_max.y = yMax;
202  bb_max.z = 0;
203 
204  const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1);
205  const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1);
206 
207  for (int i = 0; i < xcomp.cols(); i++)
208  for (int j = 0; j < xcomp.rows(); j++)
209  {
210  const float tri_side =
211  0.25f *
212  sqrt(xcomp(j, i) * xcomp(j, i) + ycomp(j, i) * ycomp(j, i));
213  const float ang = ::atan2f(ycomp(j, i), xcomp(j, i)) - 1.5708f;
214 
215  if (-sin(ang) * 0.866f * tri_side + xMin + i * x_cell_size +
216  xcomp(j, i) <
217  bb_min.x)
218  bb_min.x = -sin(ang) * 0.866f * tri_side + xMin +
219  i * x_cell_size + xcomp(j, i);
220 
221  if (cos(ang) * 0.866f * tri_side + yMin + j * y_cell_size +
222  ycomp(j, i) <
223  bb_min.y)
224  bb_min.y = cos(ang) * 0.866f * tri_side + yMin +
225  j * y_cell_size + ycomp(j, i);
226 
227  if (-sin(ang) * 0.866f * tri_side + xMin + i * x_cell_size +
228  xcomp(j, i) >
229  bb_max.x)
230  bb_max.x = -sin(ang) * 0.866f * tri_side + xMin +
231  i * x_cell_size + xcomp(j, i);
232 
233  if (cos(ang) * 0.866f * tri_side + yMin + j * y_cell_size +
234  ycomp(j, i) >
235  bb_max.y)
236  bb_max.y = cos(ang) * 0.866f * tri_side + yMin +
237  j * y_cell_size + ycomp(j, i);
238  }
239 
240  // Convert to coordinates of my parent:
241  m_pose.composePoint(bb_min, bb_min);
242  m_pose.composePoint(bb_max, bb_max);
243 }
244 
246 {
247  ASSERT_(xcomp.size() > 0);
248 
249  const float ratio_xp =
250  xcomp.maxCoeff() * (xcomp.cols() - 1) / (xMax - xMin);
251  const float ratio_xn =
252  xcomp.minCoeff() * (xcomp.cols() - 1) / (xMax - xMin);
253  const float ratio_yp =
254  ycomp.maxCoeff() * (ycomp.rows() - 1) / (yMax - yMin);
255  const float ratio_yn =
256  ycomp.minCoeff() * (ycomp.rows() - 1) / (yMax - yMin);
257  const float norm_factor = 0.85f / max(max(ratio_xp, std::abs(ratio_xn)),
258  max(ratio_yp, std::abs(ratio_yn)));
259 
260  xcomp *= norm_factor;
261  ycomp *= norm_factor;
263 }
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
STL namespace.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
Context for calls to render()
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
std::vector< mrpt::img::TColor > m_color_buffer_data
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
void onUpdateBuffers_Triangles() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
This base provides a set of functions for maths stuff.
static constexpr shader_id_t WIREFRAME
void onUpdateBuffers_Points() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
A 2D vector field representation, consisting of points and arrows drawn on a plane (invisible grid)...
static constexpr shader_id_t TRIANGLES
void getBoundingBox(mrpt::math::TPoint3D &bb_min, mrpt::math::TPoint3D &bb_max) const override
Evaluates the bounding box of this object (including possible children) in the coordinate frame of th...
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
std::vector< mrpt::img::TColor > m_color_buffer_data
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
std::vector< mrpt::opengl::TTriangle > m_triangles
List of triangles.
mrpt::vision::TStereoCalibResults out
void onUpdateBuffers_Wireframe() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
const auto bb_max
void adjustVectorFieldToGrid()
Adjust the vector field in the scene (vectors magnitude) according to the grid size.
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
const auto bb_min
TPoint3D_< float > TPoint3Df
Definition: TPoint3D.h:269
This template class provides the basic functionality for a general 2D any-size, resizable container o...
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
static constexpr shader_id_t POINTS
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020