MRPT  1.9.9
CSetOfTriangles.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-2019, 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 
15 #include "opengl_internals.h"
16 
17 using namespace mrpt;
18 using namespace mrpt::opengl;
19 using namespace mrpt::poses;
20 
21 using namespace mrpt::math;
22 using namespace std;
23 
25 
26 /*---------------------------------------------------------------
27  render
28  ---------------------------------------------------------------*/
29 void CSetOfTriangles::render_dl() const
30 {
31 #if MRPT_HAS_OPENGL_GLUT
32 
33  if (m_enableTransparency)
34  {
35  // glDisable(GL_DEPTH_TEST);
36  glEnable(GL_BLEND);
37  glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
38  }
39  else
40  {
41  glEnable(GL_DEPTH_TEST);
42  glDisable(GL_BLEND);
43  }
44 
45  vector<TTriangle>::const_iterator it;
46 
47  glEnable(GL_NORMALIZE); // Normalize normals
48  glBegin(GL_TRIANGLES);
49 
50  for (it = m_triangles.begin(); it != m_triangles.end(); ++it)
51  {
52  // Compute the normal vector:
53  // ---------------------------------
54  float ax = it->x[1] - it->x[0];
55  float ay = it->y[1] - it->y[0];
56  float az = it->z[1] - it->z[0];
57 
58  float bx = it->x[2] - it->x[0];
59  float by = it->y[2] - it->y[0];
60  float bz = it->z[2] - it->z[0];
61 
62  glNormal3f(ay * bz - az * by, -ax * bz + az * bx, ax * by - ay * bx);
63 
64  glColor4f(it->r[0], it->g[0], it->b[0], it->a[0]);
65  glVertex3f(it->x[0], it->y[0], it->z[0]);
66 
67  glColor4f(it->r[1], it->g[1], it->b[1], it->a[1]);
68  glVertex3f(it->x[1], it->y[1], it->z[1]);
69 
70  glColor4f(it->r[2], it->g[2], it->b[2], it->a[2]);
71  glVertex3f(it->x[2], it->y[2], it->z[2]);
72  }
73 
74  glEnd();
75  glDisable(GL_NORMALIZE);
76 
77  glDisable(GL_BLEND);
78 #endif
79 }
80 
83 {
87 
92 }
95 {
96  i.ReadBufferFixEndianness(t.x, 3);
97  i.ReadBufferFixEndianness(t.y, 3);
98  i.ReadBufferFixEndianness(t.z, 3);
99 
100  i.ReadBufferFixEndianness(t.r, 3);
101  i.ReadBufferFixEndianness(t.g, 3);
102  i.ReadBufferFixEndianness(t.b, 3);
103  i.ReadBufferFixEndianness(t.a, 3);
104 }
105 
106 uint8_t CSetOfTriangles::serializeGetVersion() const { return 1; }
108 {
109  writeToStreamRender(out);
110  auto n = (uint32_t)m_triangles.size();
111  out << n;
112  for (size_t i = 0; i < n; i++) triangle_writeToStream(out, m_triangles[i]);
113 
114  // Version 1:
115  out << m_enableTransparency;
116 }
118  mrpt::serialization::CArchive& in, uint8_t version)
119 {
120  switch (version)
121  {
122  case 0:
123  case 1:
124  {
125  readFromStreamRender(in);
126  uint32_t n;
127  in >> n;
128  m_triangles.assign(n, TTriangle());
129  for (size_t i = 0; i < n; i++)
130  triangle_readFromStream(in, m_triangles[i]);
131 
132  if (version >= 1)
133  in >> m_enableTransparency;
134  else
135  m_enableTransparency = true;
136  }
137  break;
138  default:
140  };
141  polygonsUpToDate = false;
143 }
144 
146  const mrpt::poses::CPose3D& o, double& dist) const
147 {
148  if (!polygonsUpToDate) updatePolygons();
149  return mrpt::math::traceRay(
150  tmpPolygons, (o - this->m_pose).asTPose(), dist);
151 }
153 {
155  m_color = c;
156  mrpt::img::TColorf col(c);
157  for (auto& m_triangle : m_triangles)
158  for (size_t i = 0; i < 3; i++)
159  {
160  m_triangle.r[i] = col.R;
161  m_triangle.g[i] = col.G;
162  m_triangle.b[i] = col.B;
163  m_triangle.a[i] = col.A;
164  }
165  return *this;
166 }
167 
169 {
171  m_color.R = r;
172  const float col = r / 255.f;
173  for (auto& m_triangle : m_triangles)
174  for (size_t i = 0; i < 3; i++) m_triangle.r[i] = col;
175  return *this;
176 }
177 
179 {
181  m_color.G = g;
182  const float col = g / 255.f;
183  for (auto& m_triangle : m_triangles)
184  for (size_t i = 0; i < 3; i++) m_triangle.g[i] = col;
185  return *this;
186 }
187 
189 {
191  m_color.B = b;
192  const float col = b / 255.f;
193  for (auto& m_triangle : m_triangles)
194  for (size_t i = 0; i < 3; i++) m_triangle.b[i] = col;
195  return *this;
196 }
197 
199 {
201  m_color.A = a;
202  const float col = a / 255.f;
203  for (auto& m_triangle : m_triangles)
204  for (size_t i = 0; i < 3; i++) m_triangle.a[i] = col;
205  return *this;
206 }
207 
209  std::vector<mrpt::math::TPolygon3D>& polys) const
210 {
211  if (!polygonsUpToDate) updatePolygons();
212  size_t N = tmpPolygons.size();
213  for (size_t i = 0; i < N; i++) polys[i] = tmpPolygons[i].poly;
214 }
215 
217 {
218  TPolygon3D tmp(3);
219  size_t N = m_triangles.size();
220  tmpPolygons.resize(N);
221  for (size_t i = 0; i < N; i++)
222  for (size_t j = 0; j < 3; j++)
223  {
224  const TTriangle& t = m_triangles[i];
225  tmp[j].x = t.x[j];
226  tmp[j].y = t.y[j];
227  tmp[j].z = t.z[j];
228  tmpPolygons[i] = tmp;
229  }
230  polygonsUpToDate = true;
232 }
233 
236 {
238  std::numeric_limits<double>::max(), std::numeric_limits<double>::max(),
239  std::numeric_limits<double>::max());
241  -std::numeric_limits<double>::max(),
242  -std::numeric_limits<double>::max(),
243  -std::numeric_limits<double>::max());
244 
245  for (const auto& t : m_triangles)
246  {
247  keep_min(bb_min.x, t.x[0]);
248  keep_max(bb_max.x, t.x[0]);
249  keep_min(bb_min.y, t.y[0]);
250  keep_max(bb_max.y, t.y[0]);
251  keep_min(bb_min.z, t.z[0]);
252  keep_max(bb_max.z, t.z[0]);
253 
254  keep_min(bb_min.x, t.x[1]);
255  keep_max(bb_max.x, t.x[1]);
256  keep_min(bb_min.y, t.y[1]);
257  keep_max(bb_max.y, t.y[1]);
258  keep_min(bb_min.z, t.z[1]);
259  keep_max(bb_max.z, t.z[1]);
260 
261  keep_min(bb_min.x, t.x[2]);
262  keep_max(bb_max.x, t.x[2]);
263  keep_min(bb_min.y, t.y[2]);
264  keep_max(bb_max.y, t.y[2]);
265  keep_min(bb_min.z, t.z[2]);
266  keep_max(bb_max.z, t.z[2]);
267  }
268 
269  // Convert to coordinates of my parent:
270  m_pose.composePoint(bb_min, bb_min);
271  m_pose.composePoint(bb_max, bb_max);
272 }
273 
275 {
276  reserve(m_triangles.size() + p->m_triangles.size());
277  m_triangles.insert(
278  m_triangles.end(), p->m_triangles.begin(), p->m_triangles.end());
279  polygonsUpToDate = false;
281 }
void notifyChange() const
Must be called to notify that the object has changed (so, the display list must be updated) ...
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...
CRenderizable & setColorG_u8(const uint8_t g) override
Color components in the range [0,255].
bool traceRay(const mrpt::poses::CPose3D &o, double &dist) const override
Ray tracing.
void getPolygons(std::vector< mrpt::math::TPolygon3D > &polys) const
Gets the polygon cache.
bool traceRay(const std::vector< TPolygonWithPlane > &vec, const mrpt::math::TPose3D &pose, double &dist)
Fast ray tracing method using polygons&#39; properties.
Definition: geometry.cpp:2565
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
#define IMPLEMENTS_SERIALIZABLE(class_name, base, NameSpace)
To be added to all CSerializable-classes implementation files.
void WriteBufferFixEndianness(const T *ptr, size_t ElementCount)
Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running arch...
Definition: CArchive.h:133
The base class of 3D objects that can be directly rendered through OpenGL.
Definition: CRenderizable.h:40
STL namespace.
CRenderizable & setColor_u8(const mrpt::img::TColor &c) override
Changes the default object color.
static void triangle_writeToStream(mrpt::serialization::CArchive &o, const CSetOfTriangles::TTriangle &t)
CRenderizable & setColorR_u8(const uint8_t r) override
Color components in the range [0,255].
A renderizable object suitable for rendering with OpenGL&#39;s display lists.
#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 serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
TPoint3D_< double > TPoint3D
Lightweight 3D point.
Definition: TPoint3D.h:242
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...
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
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...
void updatePolygons() const
Polygon cache updating.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
static void triangle_readFromStream(mrpt::serialization::CArchive &i, CSetOfTriangles::TTriangle &t)
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
Definition: CPose3D.h:85
mrpt::vision::TStereoCalibResults out
A RGB color - floats in the range [0,1].
Definition: TColor.h:78
The namespace for 3D scene representation and rendering.
Definition: CGlCanvasBase.h:15
const auto bb_max
A set of colored triangles.
const auto bb_min
A RGB color - 8bit.
Definition: TColor.h:20
size_t ReadBufferFixEndianness(T *ptr, size_t ElementCount)
Reads a sequence of elemental datatypes, taking care of reordering their bytes from the MRPT stream s...
Definition: CArchive.h:94
CRenderizable & setColorB_u8(const uint8_t b) override
Color components in the range [0,255].
CRenderizable & setColorA_u8(const uint8_t a) override
Color components in the range [0,255].
void insertTriangles(const InputIterator &begin, const InputIterator &end)
Inserts a set of triangles, bounded by iterators, into this set.
3D polygon, inheriting from std::vector<TPoint3D>
Definition: TPolygon3D.h:19



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 24b95e159 Thu Jan 23 01:15:46 2020 +0100 at jue ene 23 01:30:10 CET 2020