MRPT  1.9.9
CFrustum.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 
12 #include <mrpt/opengl/CFrustum.h>
14 
15 using namespace mrpt;
16 using namespace mrpt::opengl;
17 using namespace mrpt::math;
18 using namespace std;
19 
21 
22 void CFrustum::render(const RenderContext& rc) const
23 {
24  switch (rc.shader_id)
25  {
27  if (m_draw_planes) CRenderizableShaderTriangles::render(rc);
28  break;
30  if (m_draw_lines) CRenderizableShaderWireFrame::render(rc);
31  break;
32  };
33 }
35 {
38 }
39 
40 std::array<mrpt::math::TPoint3Df, 8> CFrustum::computeFrustumCorners() const
41 {
42  std::array<mrpt::math::TPoint3Df, 8> pts;
43  for (size_t j = 0; j < 2; j++)
44  {
45  const float r = j == 0 ? m_min_distance : m_max_distance;
46  for (size_t i = 0; i < 4; i++) pts[4 * j + i].x = r;
47  pts[4 * j + 0].y = -r * tan(m_fov_horz_left);
48  pts[4 * j + 1].y = -r * tan(m_fov_horz_left);
49  pts[4 * j + 2].y = r * tan(m_fov_horz_right);
50  pts[4 * j + 3].y = r * tan(m_fov_horz_right);
51  pts[4 * j + 0].z = -r * tan(m_fov_vert_down);
52  pts[4 * j + 1].z = r * tan(m_fov_vert_up);
53  pts[4 * j + 2].z = -r * tan(m_fov_vert_down);
54  pts[4 * j + 3].z = r * tan(m_fov_vert_up);
55  }
56  return pts;
57 }
58 
60 {
63  vbd.clear();
64 
65  const std::array<mrpt::math::TPoint3Df, 8> pts = computeFrustumCorners();
66 
67  const std::array<int, 16> draw_path = {0, 1, 3, 2, 0, 4, 6, 2,
68  3, 7, 6, 4, 5, 7, 5, 1};
69 
70  // GL_LINE_STRIP:
71  for (size_t idx = 0; idx < draw_path.size(); idx++)
72  {
73  const unsigned int idx_next = (idx + 1) % draw_path.size();
74  vbd.emplace_back(pts[draw_path[idx]]);
75  vbd.emplace_back(pts[draw_path[idx_next]]);
76  }
77 
78  cbd.assign(vbd.size(), m_color);
79 }
81 {
83  tris.clear();
84 
85  const std::array<mrpt::math::TPoint3Df, 8> pts = computeFrustumCorners();
86 
87  tris.emplace_back(pts[0], pts[2], pts[6]);
88  tris.emplace_back(pts[6], pts[4], pts[0]);
89 
90  tris.emplace_back(pts[2], pts[3], pts[7]);
91  tris.emplace_back(pts[7], pts[6], pts[2]);
92 
93  tris.emplace_back(pts[4], pts[6], pts[7]);
94  tris.emplace_back(pts[7], pts[5], pts[4]);
95 
96  tris.emplace_back(pts[1], pts[5], pts[7]);
97  tris.emplace_back(pts[7], pts[3], pts[1]);
98 
99  tris.emplace_back(pts[1], pts[5], pts[7]);
100  tris.emplace_back(pts[7], pts[3], pts[1]);
101 
102  tris.emplace_back(pts[4], pts[5], pts[1]);
103  tris.emplace_back(pts[1], pts[0], pts[4]);
104 
105  // All faces, all vertices, same color:
106  for (auto& t : tris) t.setColor(m_planes_color);
107 }
108 
109 // Ctors
111  : m_fov_horz_left(mrpt::DEG2RAD(45.0f)),
112  m_fov_horz_right(mrpt::DEG2RAD(45.0f)),
113  m_fov_vert_down(mrpt::DEG2RAD(30.0f)),
114  m_fov_vert_up(mrpt::DEG2RAD(30.0f)),
115  m_planes_color(0xE0, 0x00, 0x00, 0x50) // RGBA
116 {
123  keep_min(m_fov_vert_up, DEG2RAD(89.9f));
125 }
126 
128  float near_distance, float far_distance, float horz_FOV_degrees,
129  float vert_FOV_degrees, float lineWidth, bool draw_lines, bool draw_planes)
130  : m_min_distance(near_distance),
131  m_max_distance(far_distance),
132  m_fov_horz_left(mrpt::DEG2RAD(.5f * horz_FOV_degrees)),
133  m_fov_horz_right(mrpt::DEG2RAD(.5f * horz_FOV_degrees)),
134  m_fov_vert_down(mrpt::DEG2RAD(.5f * vert_FOV_degrees)),
135  m_fov_vert_up(mrpt::DEG2RAD(.5f * vert_FOV_degrees)),
136  m_draw_lines(draw_lines),
137  m_draw_planes(draw_planes),
138  m_planes_color(0xE0, 0x00, 0x00, 0x50) // RGBA
139 {
140  this->setLineWidth(lineWidth);
141 }
142 
143 uint8_t CFrustum::serializeGetVersion() const { return 0; }
145 {
147  // version 0
152 }
153 
155 {
156  switch (version)
157  {
158  case 0:
165  break;
166  default:
168  };
170 }
171 
173  [[maybe_unused]] const mrpt::poses::CPose3D& o,
174  [[maybe_unused]] double& dist) const
175 {
176  THROW_EXCEPTION("TO DO");
177 }
178 
179 // setters:
181  const float near_distance, const float far_distance)
182 {
183  m_min_distance = near_distance;
184  m_max_distance = far_distance;
186 }
187 void CFrustum::setHorzFOV(const float fov_horz_degrees)
188 {
189  m_fov_horz_right = m_fov_horz_left = 0.5f * mrpt::DEG2RAD(fov_horz_degrees);
195 }
196 void CFrustum::setVertFOV(const float fov_vert_degrees)
197 {
198  m_fov_vert_down = m_fov_vert_up = 0.5f * mrpt::DEG2RAD(fov_vert_degrees);
201  keep_min(m_fov_vert_up, DEG2RAD(89.9f));
204 }
206  const float fov_horz_left_degrees, const float fov_horz_right_degrees)
207 {
208  m_fov_horz_left = mrpt::DEG2RAD(fov_horz_left_degrees);
209  m_fov_horz_right = mrpt::DEG2RAD(fov_horz_right_degrees);
215 }
217  const float fov_vert_down_degrees, const float fov_vert_up_degrees)
218 {
219  m_fov_vert_down = mrpt::DEG2RAD(fov_vert_down_degrees);
220  m_fov_vert_up = mrpt::DEG2RAD(fov_vert_up_degrees);
223  keep_min(m_fov_vert_up, DEG2RAD(89.9f));
226 }
227 
230 {
231  // Compute the 8 corners of the frustum:
232  TPoint3Df pts[8];
233  for (int j = 0; j < 2; j++)
234  {
235  const float r = j == 0 ? m_min_distance : m_max_distance;
236  for (int i = 0; i < 4; i++) pts[4 * j + i].x = r;
237  pts[4 * j + 0].y = -r * tan(m_fov_horz_left);
238  pts[4 * j + 1].y = -r * tan(m_fov_horz_left);
239  pts[4 * j + 2].y = r * tan(m_fov_horz_right);
240  pts[4 * j + 3].y = r * tan(m_fov_horz_right);
241  pts[4 * j + 0].z = -r * tan(m_fov_vert_down);
242  pts[4 * j + 1].z = r * tan(m_fov_vert_up);
243  pts[4 * j + 2].z = -r * tan(m_fov_vert_down);
244  pts[4 * j + 3].z = r * tan(m_fov_vert_up);
245  }
246 
247  bb_min = TPoint3D(
248  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
249  std::numeric_limits<double>::max());
250  bb_max = TPoint3D(
251  -std::numeric_limits<double>::max(),
252  -std::numeric_limits<double>::max(),
253  -std::numeric_limits<double>::max());
254  for (auto& pt : pts)
255  {
256  keep_min(bb_min.x, pt.x);
257  keep_min(bb_min.y, pt.y);
258  keep_min(bb_min.z, pt.z);
259 
260  keep_max(bb_max.x, pt.x);
261  keep_max(bb_max.y, pt.y);
262  keep_max(bb_max.z, pt.z);
263  }
264 
265  // Convert to coordinates of my parent:
268 }
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...
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Simulation of ray-trace, given a pose.
Definition: CFrustum.cpp:172
void onUpdateBuffers_Triangles() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CFrustum.cpp:80
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
#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.
void onUpdateBuffers_Wireframe() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
Definition: CFrustum.cpp:59
float m_min_distance
Near and far planes.
Definition: CFrustum.h:136
void setVertFOV(const float fov_vert_degrees)
Changes vertical FOV (symmetric)
Definition: CFrustum.cpp:196
STL namespace.
uint8_t B
Definition: TColor.h:51
Renderizable generic renderer for objects using the triangles shader.
uint8_t G
Definition: TColor.h:51
std::array< mrpt::math::TPoint3Df, 8 > computeFrustumCorners() const
Definition: CFrustum.cpp:40
void setHorzFOV(const float fov_horz_degrees)
Changes horizontal FOV (symmetric)
Definition: CFrustum.cpp:187
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
Definition: CFrustum.cpp:34
mrpt::poses::CPose3D m_pose
6D pose wrt the parent coordinate reference.
Definition: CRenderizable.h:62
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
#define MRPT_THROW_UNKNOWN_SERIALIZATION_VERSION(__V)
For use in CSerializable implementations.
Definition: exceptions.h:97
This base provides a set of functions for maths stuff.
void writeToStreamRender(mrpt::serialization::CArchive &out) const
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
Definition: CFrustum.cpp:154
constexpr double DEG2RAD(const double x)
Degrees to radians.
CFrustum()
Basic empty constructor.
Definition: CFrustum.cpp:110
static constexpr shader_id_t WIREFRAME
TPoint3D_< double > TPoint3D
Lightweight 3D point.
Definition: TPoint3D.h:268
uint8_t R
Definition: TColor.h:51
static constexpr shader_id_t TRIANGLES
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...
std::vector< mrpt::img::TColor > m_color_buffer_data
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
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::img::TColor m_planes_color
Definition: CFrustum.h:142
float m_fov_horz_left
Semi FOVs (in radians)
Definition: CFrustum.h:138
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
void setNearFarPlanes(const float near_distance, const float far_distance)
Changes distance of near & far planes.
Definition: CFrustum.cpp:180
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
Definition: CFrustum.cpp:143
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:13
void setVertFOVAsymmetric(const float fov_vert_down_degrees, const float fov_vert_up_degrees)
Changes vertical FOV (asymmetric)
Definition: CFrustum.cpp:216
const auto bb_max
A solid or wireframe frustum in 3D (a rectangular truncated pyramid), with arbitrary (possibly assyme...
Definition: CFrustum.h:52
void readFromStreamRender(mrpt::serialization::CArchive &in)
void composePoint(double lx, double ly, double lz, double &gx, double &gy, double &gz, mrpt::optional_ref< mrpt::math::CMatrixDouble33 > out_jacobian_df_dpoint=std::nullopt, mrpt::optional_ref< mrpt::math::CMatrixDouble36 > out_jacobian_df_dpose=std::nullopt, mrpt::optional_ref< mrpt::math::CMatrixDouble36 > out_jacobian_df_dse3=std::nullopt, bool use_small_rot_approx=false) const
An alternative, slightly more efficient way of doing with G and L being 3D points and P this 6D pose...
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: CFrustum.cpp:228
const auto bb_min
void setHorzFOVAsymmetric(const float fov_horz_left_degrees, const float fov_horz_right_degrees)
Changes horizontal FOV (asymmetric)
Definition: CFrustum.cpp:205
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
Definition: CFrustum.cpp:144
uint8_t A
Definition: TColor.h:51
float m_fov_vert_down
Semi FOVs (in radians)
Definition: CFrustum.h:140
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