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  [[maybe_unused]] CMatrixFloat Matrix_x,
33  [[maybe_unused]] CMatrixFloat Matrix_y, [[maybe_unused]] float xmin,
34  [[maybe_unused]] float xmax, [[maybe_unused]] float ymin,
35  [[maybe_unused]] float ymax)
36 {
37  m_point_color = m_color;
38  m_field_color = m_color;
39 }
40 
42 {
43  switch (rc.shader_id)
44  {
47  break;
50  break;
53  break;
54  };
55 }
57 {
61 }
62 
64 {
67  vbd.clear();
68 
69  vbd.reserve(xcomp.size() * 2);
70 
71  const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1);
72  const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1);
73 
74  for (int i = 0; i < xcomp.cols(); i++)
75  for (int j = 0; j < xcomp.rows(); j++)
76  {
77  vbd.emplace_back(xMin + i * x_cell_size, yMin + j * y_cell_size, 0);
78  vbd.emplace_back(
79  xMin + i * x_cell_size + xcomp(j, i),
80  yMin + j * y_cell_size + ycomp(j, i), 0);
81  }
82 
83  cbd.assign(vbd.size(), m_field_color);
84 }
86 {
87  using P3f = mrpt::math::TPoint3Df;
88 
90  tris.clear();
91 
92  tris.reserve(xcomp.size());
93 
94  const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1);
95  const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1);
96 
97  for (int i = 0; i < xcomp.cols(); i++)
98  for (int j = 0; j < xcomp.rows(); j++)
99  {
100  const float tri_side =
101  0.25f *
102  sqrt(xcomp(j, i) * xcomp(j, i) + ycomp(j, i) * ycomp(j, i));
103  const float ang = ::atan2f(ycomp(j, i), xcomp(j, i)) - 1.5708f;
104  tris.emplace_back(
105  P3f(-sin(ang) * 0.866f * tri_side + xMin + i * x_cell_size +
106  xcomp(j, i),
107  cos(ang) * 0.866f * tri_side + yMin + j * y_cell_size +
108  ycomp(j, i),
109  0),
110  P3f(cos(ang) * 0.5f * tri_side + xMin + i * x_cell_size +
111  xcomp(j, i),
112  sin(ang) * 0.5f * tri_side + yMin + j * y_cell_size +
113  ycomp(j, i),
114  0),
115  P3f(-cos(ang) * 0.5f * tri_side + xMin + i * x_cell_size +
116  xcomp(j, i),
117  -sin(ang) * 0.5f * tri_side + yMin + j * y_cell_size +
118  ycomp(j, i),
119  0));
120  }
121 
122  for (auto& t : tris)
123  {
124  t.computeNormals();
125  t.setColor(m_field_color);
126  }
127 }
129 {
132 
133  vbd.clear();
134  vbd.reserve(xcomp.size());
135 
136  const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1);
137  const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1);
138 
139  for (int i = 0; i < xcomp.cols(); i++)
140  for (int j = 0; j < xcomp.rows(); j++)
141  vbd.emplace_back(
142  xMin + i * x_cell_size, yMin + j * y_cell_size, .0f);
143 
144  cbd.assign(vbd.size(), m_point_color);
145 }
146 
147 /*---------------------------------------------------------------
148  Implements the writing to a CStream capability of
149  CSerializable objects
150  ---------------------------------------------------------------*/
151 uint8_t CVectorField2D::serializeGetVersion() const { return 0; }
153 {
154  writeToStreamRender(out);
155 
156  out << xcomp << ycomp;
157  out << xMin << xMax << yMin << yMax;
158  out << m_lineWidth;
159  out << m_pointSize;
160  out << m_antiAliasing;
161  out << m_point_color;
162  out << m_field_color;
163 }
164 
166  mrpt::serialization::CArchive& in, uint8_t version)
167 {
168  switch (version)
169  {
170  case 0:
171  readFromStreamRender(in);
172 
173  in >> xcomp >> ycomp;
174  in >> xMin >> xMax >> yMin >> yMax;
175  in >> m_lineWidth;
176  in >> m_pointSize;
177  in >> m_antiAliasing;
178  in >> m_point_color;
179  in >> m_field_color;
180  break;
181 
182  default:
184  break;
185  };
187 }
188 
191 {
192  bb_min.x = xMin;
193  bb_min.y = yMin;
194  bb_min.z = 0;
195 
196  bb_max.x = xMax;
197  bb_max.y = yMax;
198  bb_max.z = 0;
199 
200  const float x_cell_size = (xMax - xMin) / (xcomp.cols() - 1);
201  const float y_cell_size = (yMax - yMin) / (ycomp.rows() - 1);
202 
203  for (int i = 0; i < xcomp.cols(); i++)
204  for (int j = 0; j < xcomp.rows(); j++)
205  {
206  const float tri_side =
207  0.25f *
208  sqrt(xcomp(j, i) * xcomp(j, i) + ycomp(j, i) * ycomp(j, i));
209  const float ang = ::atan2f(ycomp(j, i), xcomp(j, i)) - 1.5708f;
210 
211  if (-sin(ang) * 0.866f * tri_side + xMin + i * x_cell_size +
212  xcomp(j, i) <
213  bb_min.x)
214  bb_min.x = -sin(ang) * 0.866f * tri_side + xMin +
215  i * x_cell_size + xcomp(j, i);
216 
217  if (cos(ang) * 0.866f * tri_side + yMin + j * y_cell_size +
218  ycomp(j, i) <
219  bb_min.y)
220  bb_min.y = cos(ang) * 0.866f * tri_side + yMin +
221  j * y_cell_size + ycomp(j, i);
222 
223  if (-sin(ang) * 0.866f * tri_side + xMin + i * x_cell_size +
224  xcomp(j, i) >
225  bb_max.x)
226  bb_max.x = -sin(ang) * 0.866f * tri_side + xMin +
227  i * x_cell_size + xcomp(j, i);
228 
229  if (cos(ang) * 0.866f * tri_side + yMin + j * y_cell_size +
230  ycomp(j, i) >
231  bb_max.y)
232  bb_max.y = cos(ang) * 0.866f * tri_side + yMin +
233  j * y_cell_size + ycomp(j, i);
234  }
235 
236  // Convert to coordinates of my parent:
237  m_pose.composePoint(bb_min, bb_min);
238  m_pose.composePoint(bb_max, bb_max);
239 }
240 
242 {
243  ASSERT_(xcomp.size() > 0);
244 
245  const float ratio_xp =
246  xcomp.maxCoeff() * (xcomp.cols() - 1) / (xMax - xMin);
247  const float ratio_xn =
248  xcomp.minCoeff() * (xcomp.cols() - 1) / (xMax - xMin);
249  const float ratio_yp =
250  ycomp.maxCoeff() * (ycomp.rows() - 1) / (yMax - yMin);
251  const float ratio_yn =
252  ycomp.minCoeff() * (ycomp.rows() - 1) / (yMax - yMin);
253  const float norm_factor = 0.85f / max(max(ratio_xp, std::abs(ratio_xn)),
254  max(ratio_yp, std::abs(ratio_yn)));
255 
256  xcomp *= norm_factor;
257  ycomp *= norm_factor;
259 }
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.
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: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020