MRPT  2.0.4
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 }
mrpt::opengl::CVectorField2D::CVectorField2D
CVectorField2D()
Constructor.
Definition: CVectorField2D.cpp:23
mrpt::opengl::CRenderizable::notifyChange
void notifyChange() const
Call to enable calling renderUpdateBuffers() before the next render() rendering iteration.
Definition: CRenderizable.h:315
mrpt::opengl::CVectorField2D::serializeFrom
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CVectorField2D.cpp:165
mrpt::math::TPoint3D_< double >
mrpt::opengl::CVectorField2D::adjustVectorFieldToGrid
void adjustVectorFieldToGrid()
Adjust the vector field in the scene (vectors magnitude) according to the grid size.
Definition: CVectorField2D.cpp:241
mrpt::opengl::CRenderizableShaderTriangles::render
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
Definition: CRenderizableShaderTriangles.cpp:45
mrpt::opengl::CVectorField2D::getBoundingBox
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...
Definition: CVectorField2D.cpp:189
mrpt::opengl::CVectorField2D::serializeGetVersion
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CVectorField2D.cpp:151
mrpt::opengl::CRenderizable
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:48
mrpt::opengl::CRenderizableShaderWireFrame::m_color_buffer_data
std::vector< mrpt::img::TColor > m_color_buffer_data
Definition: CRenderizableShaderWireFrame.h:80
mrpt::opengl::CRenderizableShaderTriangles::m_triangles
std::vector< mrpt::opengl::TTriangle > m_triangles
List of triangles.
Definition: CRenderizableShaderTriangles.h:58
mrpt::opengl::CVectorField2D
A 2D vector field representation, consisting of points and arrows drawn on a plane (invisible grid).
Definition: CVectorField2D.h:35
out
mrpt::vision::TStereoCalibResults out
Definition: chessboard_stereo_camera_calib_unittest.cpp:25
mrpt::math::TPoint3Df
TPoint3D_< float > TPoint3Df
Definition: TPoint3D.h:269
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
ASSERT_
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
mrpt::serialization::CArchive
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
bb_max
const auto bb_max
Definition: CPose3DPDFGrid_unittest.cpp:25
mrpt::opengl::DefaultShaderID::WIREFRAME
static constexpr shader_id_t WIREFRAME
Definition: DefaultShaders.h:25
bb_min
const auto bb_min
Definition: CPose3DPDFGrid_unittest.cpp:23
mrpt::opengl::DefaultShaderID::TRIANGLES
static constexpr shader_id_t TRIANGLES
Definition: DefaultShaders.h:27
mrpt::opengl::CRenderizableShaderWireFrame::m_vertex_buffer_data
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
Definition: CRenderizableShaderWireFrame.h:79
mrpt::opengl::CRenderizableShaderPoints::renderUpdateBuffers
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers,...
mrpt::opengl::CVectorField2D::onUpdateBuffers_Wireframe
void onUpdateBuffers_Wireframe() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CVectorField2D.cpp:63
mrpt::opengl::CRenderizableShaderWireFrame::render
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
Definition: CRenderizableShaderWireFrame.cpp:52
mrpt::opengl::CRenderizable::RenderContext
Context for calls to render()
Definition: CRenderizable.h:266
IMPLEMENTS_SERIALIZABLE
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
Definition: CSerializable.h:166
mrpt::opengl::CRenderizable::RenderContext::shader_id
mrpt::opengl::shader_id_t shader_id
Definition: CRenderizable.h:272
opengl-precomp.h
mrpt::opengl::CRenderizableShaderTriangles::renderUpdateBuffers
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers,...
mrpt::opengl::CVectorField2D::renderUpdateBuffers
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers,...
Definition: CVectorField2D.cpp:56
CVectorField2D.h
mrpt::math
This base provides a set of functions for maths stuff.
Definition: math/include/mrpt/math/bits_math.h:11
mrpt::opengl::CVectorField2D::serializeTo
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CVectorField2D.cpp:152
mrpt::opengl::CRenderizableShaderPoints::m_vertex_buffer_data
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
Definition: CRenderizableShaderPoints.h:106
CArchive.h
MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
mrpt::opengl::CVectorField2D::render
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
Definition: CVectorField2D.cpp:41
mrpt::opengl::CRenderizableShaderWireFrame::renderUpdateBuffers
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers,...
mrpt::opengl::CRenderizableShaderPoints::m_color_buffer_data
std::vector< mrpt::img::TColor > m_color_buffer_data
Definition: CRenderizableShaderPoints.h:107
mrpt::opengl
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
mrpt::opengl::DefaultShaderID::POINTS
static constexpr shader_id_t POINTS
Definition: DefaultShaders.h:24
mrpt::math::CMatrixDynamic
This template class provides the basic functionality for a general 2D any-size, resizable container o...
Definition: CMatrixDynamic.h:39
mrpt::opengl::CVectorField2D::onUpdateBuffers_Triangles
void onUpdateBuffers_Triangles() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CVectorField2D.cpp:85
mrpt::opengl::CVectorField2D::onUpdateBuffers_Points
void onUpdateBuffers_Points() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CVectorField2D.cpp:128



Page generated by Doxygen 1.8.17 for MRPT 2.0.4 at Fri Jul 17 08:43:33 UTC 2020