30 unsigned int num_verts,
unsigned int num_faces,
int* verts_per_face,
31 int* face_verts,
float* vert_coords)
34 m_is_quad.resize(num_faces);
35 for (
unsigned int i = 0; i < num_faces; i++)
37 if (verts_per_face[i] == 3)
39 else if (verts_per_face[i] == 4)
44 "Incorrect mesh format. It can only be composed of triangles " 50 m_face_verts.resize(num_faces);
51 unsigned int count = 0;
52 for (
unsigned int f = 0; f < num_faces; f++)
54 m_face_verts[f][0] = face_verts[count++];
55 m_face_verts[f][1] = face_verts[count++];
56 m_face_verts[f][2] = face_verts[count++];
58 m_face_verts[f][3] = face_verts[count++];
60 m_face_verts[f][3] = -1;
64 m_vertices.resize(num_verts);
65 for (
unsigned int i = 0; i < num_verts; i++)
67 m_vertices[i][0] = vert_coords[3 * i];
68 m_vertices[i][1] = vert_coords[3 * i + 1];
69 m_vertices[i][2] = vert_coords[3 * i + 2];
75 m_normals.resize(num_faces);
77 for (
unsigned int f = 0; f < num_faces; f++)
79 const unsigned int v1 = m_face_verts[f][3];
80 const unsigned int v2 = m_face_verts[f][2];
81 const unsigned int v3 = m_face_verts[f][1];
82 const unsigned int v4 = m_face_verts[f][0];
86 const float vec1[3] = {m_vertices[v3][0] - m_vertices[v1][0],
87 m_vertices[v3][1] - m_vertices[v1][1],
88 m_vertices[v3][2] - m_vertices[v1][2]};
89 const float vec2[3] = {m_vertices[v4][0] - m_vertices[v2][0],
90 m_vertices[v4][1] - m_vertices[v2][1],
91 m_vertices[v4][2] - m_vertices[v2][2]};
92 m_normals[f][0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
93 m_normals[f][1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
94 m_normals[f][2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
98 const float vec1[3] = {m_vertices[v2][0] - m_vertices[v1][0],
99 m_vertices[v2][1] - m_vertices[v1][1],
100 m_vertices[v2][2] - m_vertices[v1][2]};
101 const float vec2[3] = {m_vertices[v3][0] - m_vertices[v1][0],
102 m_vertices[v3][1] - m_vertices[v1][1],
103 m_vertices[v3][2] - m_vertices[v1][2]};
104 m_normals[f][0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
105 m_normals[f][1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
106 m_normals[f][2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
108 m_normals[f] = m_normals[f].unitarize();
116 unsigned int num_verts,
unsigned int num_faces,
122 m_is_quad.resize(num_faces);
123 for (
unsigned int i = 0; i < num_faces; i++) m_is_quad[i] = is_quad(i, 0);
126 m_face_verts.
resize(num_faces);
127 for (
unsigned int f = 0; f < num_faces; f++)
129 m_face_verts[f][0] = face_verts(0, f);
130 m_face_verts[f][1] = face_verts(1, f);
131 m_face_verts[f][2] = face_verts(2, f);
133 m_face_verts[f][3] = face_verts(3, f);
135 m_face_verts[f][3] = -1;
139 m_vertices.resize(num_verts);
140 for (
unsigned int i = 0; i < num_verts; i++)
142 m_vertices[i][0] = vert_coords(0, i);
143 m_vertices[i][1] = vert_coords(1, i);
144 m_vertices[i][2] = vert_coords(2, i);
148 m_normals.resize(num_faces);
149 if (m_computeNormals)
150 for (
unsigned int f = 0; f < num_faces; f++)
152 const unsigned int v1 = m_face_verts[f][0];
153 const unsigned int v2 = m_face_verts[f][1];
154 const unsigned int v3 = m_face_verts[f][2];
155 const unsigned int v4 = m_face_verts[f][3];
159 const float vec1[3] = {m_vertices[v3][0] - m_vertices[v1][0],
160 m_vertices[v3][1] - m_vertices[v1][1],
161 m_vertices[v3][2] - m_vertices[v1][2]};
162 const float vec2[3] = {m_vertices[v4][0] - m_vertices[v2][0],
163 m_vertices[v4][1] - m_vertices[v2][1],
164 m_vertices[v4][2] - m_vertices[v2][2]};
165 m_normals[f][0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
166 m_normals[f][1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
167 m_normals[f][2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
171 const float vec1[3] = {m_vertices[v2][0] - m_vertices[v1][0],
172 m_vertices[v2][1] - m_vertices[v1][1],
173 m_vertices[v2][2] - m_vertices[v1][2]};
174 const float vec2[3] = {m_vertices[v3][0] - m_vertices[v1][0],
175 m_vertices[v3][1] - m_vertices[v1][1],
176 m_vertices[v3][2] - m_vertices[v1][2]};
177 m_normals[f][0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
178 m_normals[f][1] = vec1[2] * vec2[0] - vec1[0] * vec2[2];
179 m_normals[f][2] = vec1[0] * vec2[1] - vec1[1] * vec2[0];
214 for (
size_t f = 0; f < m_face_verts.size(); f++)
216 const unsigned char num_vert = 3 + m_is_quad[f];
217 for (
int i = 0; i < num_vert - 1; i++)
219 const unsigned int v_0 = m_face_verts[f][i];
220 const unsigned int v_1 = m_face_verts[f][i + 1];
222 vbd.emplace_back(m_vertices[v_0]);
223 vbd.emplace_back(m_vertices[v_1]);
228 const int v_0 = m_face_verts[f][num_vert - 1];
229 const int v_1 = m_face_verts[f][0];
231 vbd.emplace_back(m_vertices[v_0]);
232 vbd.emplace_back(m_vertices[v_1]);
235 cbd.assign(vbd.size(), edge_color.asTColor());
243 for (
size_t f = 0; f < m_is_quad.size(); f++)
246 const auto& normal = m_normals[f];
249 m_vertices[m_face_verts[f][0]], m_vertices[m_face_verts[f][1]],
250 m_vertices[m_face_verts[f][2]], normal, normal, normal);
254 m_vertices[m_face_verts[f][0]], m_vertices[m_face_verts[f][2]],
255 m_vertices[m_face_verts[f][3]], normal, normal, normal);
259 for (
auto& t : tris) t.setColor(face_color);
267 cbd.assign(m_vertices.size(), vert_color.asTColor());
273 writeToStreamRender(
out);
274 out << m_showEdges << m_showFaces << m_showVertices << m_computeNormals;
275 out << m_is_quad << m_vertices << m_normals;
276 out.WriteAs<uint32_t>(m_face_verts.size());
277 if (!m_face_verts.empty())
278 out.WriteBufferFixEndianness<uint32_t>(
279 m_face_verts[0].data(),
280 m_face_verts.size() * m_face_verts[0].size());
285 readFromStreamRender(in);
286 in >> m_showEdges >> m_showFaces >> m_showVertices >> m_computeNormals;
287 in >> m_is_quad >> m_vertices >> m_normals;
288 const auto N = in.
ReadAs<uint32_t>();
289 m_face_verts.resize(N);
290 if (!m_face_verts.empty())
292 m_face_verts[0].data(),
293 m_face_verts.size() * m_face_verts[0].size());
299 if (m_vertices.empty())
306 bb_min.x = std::numeric_limits<double>::max();
307 bb_min.y = std::numeric_limits<double>::max();
308 bb_min.z = std::numeric_limits<double>::max();
309 bb_max.x = -std::numeric_limits<double>::max();
310 bb_max.y = -std::numeric_limits<double>::max();
311 bb_max.z = -std::numeric_limits<double>::max();
313 for (
size_t i = 0; i < m_vertices.size(); i++)
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...
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
void resize(size_t row, size_t col)
void serializeTo(mrpt::serialization::CArchive &out) const override
Pure virtual method for writing (serializing) to an abstract archive.
#define THROW_EXCEPTION(msg)
void serializeFrom(mrpt::serialization::CArchive &in, uint8_t serial_version) override
Pure virtual method for reading (deserializing) from an abstract archive.
#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.
mrpt::opengl::shader_id_t shader_id
The base class of 3D objects that can be directly rendered through OpenGL.
A 3D mesh composed of Triangles and/or Quads.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
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.
Context for calls to render()
std::vector< mrpt::math::TPoint3Df > m_vertex_buffer_data
std::vector< mrpt::img::TColor > m_color_buffer_data
This base provides a set of functions for maths stuff.
void renderUpdateBuffers() const override
Called whenever m_outdatedBuffers is true: used to re-generate OpenGL vertex buffers, etc.
uint8_t serializeGetVersion() const override
Must return the current versioning number of the object.
STORED_TYPE ReadAs()
De-serialize a variable and returns it by value.
static constexpr shader_id_t WIREFRAME
TPoint3D_< double > TPoint3D
Lightweight 3D point.
void loadMesh(unsigned int num_verts, unsigned int num_faces, int *verts_per_face, int *face_verts, float *vert_coords)
Load a 3D mesh.
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
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...
void onUpdateBuffers_Triangles() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
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.
std::vector< mrpt::opengl::TTriangle > m_triangles
List of triangles.
virtual ~CMesh3D() override
mrpt::vision::TStereoCalibResults out
void onUpdateBuffers_Points() 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.
void render(const RenderContext &rc) const override
Implements the rendering of 3D objects in each class derived from CRenderizable.
void onUpdateBuffers_Wireframe() override
Must be implemented in derived classes to update the geometric entities to be drawn in "m_*_buffer" f...
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...
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.
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...
static constexpr shader_id_t POINTS
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.