My Project
Loading...
Searching...
No Matches
CMesh.h
Go to the documentation of this file.
1//==============================================================================
2/*
3 Software License Agreement (BSD License)
4 Copyright (c) 2003-2024, CHAI3D
5 (www.chai3d.org)
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted provided that the following conditions
11 are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the following
18 disclaimer in the documentation and/or other materials provided
19 with the distribution.
20
21 * Neither the name of CHAI3D nor the names of its contributors may
22 be used to endorse or promote products derived from this software
23 without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28 FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
37
38 \author <http://www.chai3d.org>
39 \author Francois Conti
40 \author Dan Morris
41 \author Chris Sewell
42 \version 3.3.0
43*/
44//==============================================================================
45
46//------------------------------------------------------------------------------
47#ifdef _MSVC
48#pragma warning (disable : 4786)
49#endif
50//------------------------------------------------------------------------------
51#ifndef CMeshH
52#define CMeshH
53//------------------------------------------------------------------------------
55#include "materials/CMaterial.h"
57#include "graphics/CColor.h"
58//------------------------------------------------------------------------------
59#include <vector>
60#include <list>
61//------------------------------------------------------------------------------
62
63//------------------------------------------------------------------------------
64namespace chai3d {
65//------------------------------------------------------------------------------
66
67//------------------------------------------------------------------------------
68class cWorld;
69//------------------------------------------------------------------------------
70
71//==============================================================================
78//==============================================================================
79//------------------------------------------------------------------------------
80
81// Array of vertices.
82class cVertexArray;
83typedef std::shared_ptr<cVertexArray> cVertexArrayPtr;
84
85// Array of triangles.
86class cTriangleArray;
87typedef std::shared_ptr<cTriangleArray> cTriangleArrayPtr;
88
89// Edges
90struct cEdge;
91
92//------------------------------------------------------------------------------
93
94//==============================================================================
107//==============================================================================
108class cMesh : public cGenericObject
109{
110 //--------------------------------------------------------------------------
111 // CONSTRUCTOR & DESTRUCTOR:
112 //--------------------------------------------------------------------------
113
114public:
115
117 cMesh(cMaterialPtr a_material = cMaterialPtr());
118
120 virtual ~cMesh();
121
122
123 //--------------------------------------------------------------------------
124 // PUBLIC METHODS - GENERAL
125 //--------------------------------------------------------------------------
126
127public:
128
130 virtual std::string getClassName() { return ("Mesh"); }
131
133 virtual cMesh* copy(const bool a_duplicateMaterialData = false,
134 const bool a_duplicateTextureData = false,
135 const bool a_duplicateMeshData = false,
136 const bool a_buildCollisionDetector = false);
137
138
139 //-----------------------------------------------------------------------
140 // PUBLIC METHODS - GRAPHIC PROPERTIES:
141 //-----------------------------------------------------------------------
142
143public:
144
146 virtual void setTransparencyLevel(const float a_level,
147 const bool a_applyToVertices = false,
148 const bool a_applyToTextures = false,
149 const bool a_affectChildren = false,
150 const bool a_affectComponents = true);
151
152
153 //-----------------------------------------------------------------------
154 // PUBLIC METHODS - DISPLAY LISTS:
155 //-----------------------------------------------------------------------
156
157public:
158
160 virtual void markForUpdate(const bool a_affectChildren = false, const bool a_affectComponents = true);
161
162
163 //--------------------------------------------------------------------------
164 // PUBLIC METHODS - VERTICES
165 //--------------------------------------------------------------------------
166
167public:
168
170 unsigned int newVertex(const double a_x = 0.0,
171 const double a_y = 0.0,
172 const double a_z = 0.0,
173 const double a_normalX = 1.0,
174 const double a_normalY = 0.0,
175 const double a_normalZ = 0.0,
176 const double a_textureCoordX = 0.0,
177 const double a_textureCoordY = 0.0,
178 const double a_textureCoordZ = 0.0);
179
181 unsigned int newVertex(const cVector3d& a_pos,
182 const cVector3d& a_normal = cVector3d(1,0,0),
183 const cVector3d& a_textureCoord = cVector3d(0,0,0),
184 const cColorf& a_color = cColorf(0,0,0,1));
185
187 inline unsigned int getNumVertices() const { return (unsigned int)(m_vertices->getNumElements()); }
188
190 void setVertexColor(const cColorf& a_color);
191
192
193 //--------------------------------------------------------------------------
194 // PUBLIC METHODS - TRIANGLES
195 //--------------------------------------------------------------------------
196
197public:
198
200 unsigned int newTriangle(const unsigned int a_indexVertex0,
201 const unsigned int a_indexVertex1,
202 const unsigned int a_indexVertex2);
203
205 unsigned int newTriangle(const cVector3d& a_vertex0 = cVector3d(0,0,0),
206 const cVector3d& a_vertex1 = cVector3d(0,0,0),
207 const cVector3d& a_vertex2 = cVector3d(0,0,0),
208 const cVector3d& a_normal0 = cVector3d(1,0,0),
209 const cVector3d& a_normal1 = cVector3d(1,0,0),
210 const cVector3d& a_normal2 = cVector3d(1,0,0),
211 const cVector3d& a_textureCoord0 = cVector3d(0,0,0),
212 const cVector3d& a_textureCoord1 = cVector3d(0,0,0),
213 const cVector3d& a_textureCoord2 = cVector3d(0,0,0),
214 const cColorf& a_colorVertex0 = cColorf(0,0,0,1),
215 const cColorf& a_colorVertex1 = cColorf(0,0,0,1),
216 const cColorf& a_colorVertex2 = cColorf(0,0,0,1));
217
219 bool removeTriangle(const unsigned int a_index);
220
222 unsigned int getNumTriangles();
223
225 void clear();
226
228 void setShowTriangles(const bool a_showTriangles) { m_showTriangles = a_showTriangles; }
229
231 bool getShowTriangles() const { return (m_showTriangles); }
232
233
234 //--------------------------------------------------------------------------
235 // PUBLIC METHODS - EDGES
236 //--------------------------------------------------------------------------
237
238public:
239
241 void computeAllEdges(double a_angleThresholdDeg = 40.0);
242
244 void clearAllEdges();
245
247 void setShowEdges(const bool a_showEdges) { m_showEdges = a_showEdges; }
248
250 bool getShowEdges() const { return (m_showEdges); }
251
253 void setEdgeProperties(const double a_width,
254 const cColorf& a_color);
255
257 void setEdgeLineWidth(const double a_width) { setEdgeProperties(a_width, m_edgeLineColor); }
258
259
260 //--------------------------------------------------------------------------
261 // PUBLIC METHODS - SURFACE NORMALS, TANGENTS, BITANGENTS
262 //--------------------------------------------------------------------------
263
264public:
265
267 void setShowNormals(const bool a_showNormals) { m_showNormals = a_showNormals; }
268
270 bool getShowNormals() const { return (m_showNormals); }
271
273 void setNormalsProperties(const double a_length, const cColorf& a_color);
274
276 void setNormalsLength(const double a_length) { setNormalsProperties(a_length, m_normalsColor); }
277
279 void computeAllNormals();
280
282 virtual void reverseAllNormals();
283
285 void computeBTN();
286
288 void setShowTangents(const bool a_showTangents) { m_showTangents = a_showTangents; }
289
291 bool getShowTangents() const { return (m_showTangents); }
292
293
294 //--------------------------------------------------------------------------
295 // PUBLIC METHODS - COLLISION DETECTION:
296 //--------------------------------------------------------------------------
297
298public:
299
302
304 virtual void createAABBCollisionDetector(const double a_radius);
305
306
307 //--------------------------------------------------------------------------
308 // PUBLIC METHODS - GEOMETRY:
309 //--------------------------------------------------------------------------
310
311public:
312
314 void scaleXYZ(const double a_scaleX, const double a_scaleY, const double a_scaleZ);
315
317 virtual void offsetVertices(const cVector3d& a_offset,
318 const bool a_updateCollisionDetector = true);
319
321 virtual void rotateVertices(const cMatrix3d& a_rotation,
322 const bool a_updateCollisionDetector = true);
323
325 virtual cVector3d getCenterOfMass();
326
327
328 //--------------------------------------------------------------------------
329 // PROTECTED METHODS - INTERNAL
330 //--------------------------------------------------------------------------
331
332protected:
333
335 virtual void render(cRenderOptions& a_options);
336
338 virtual void renderNormals(cRenderOptions& a_options);
339
341 virtual void renderTangents(cRenderOptions& a_options);
342
344 virtual void renderEdges(cRenderOptions& a_options);
345
347 virtual void renderMesh(cRenderOptions& a_options);
348
350 virtual void updateGlobalPositions(const bool a_frameOnly);
351
353 virtual void updateBoundaryBox();
354
356 void copyMeshProperties(cMesh* a_obj,
357 const bool a_duplicateMaterialData,
358 const bool a_duplicateTextureData,
359 const bool a_duplicateMeshData,
360 const bool a_buildCollisionDetector);
361
363 virtual void scaleObject(const double& a_scaleFactor) { scaleXYZ(a_scaleFactor, a_scaleFactor, a_scaleFactor); }
364
366 void computeLocalInteraction(const cVector3d& a_toolPos,
367 const cVector3d& a_toolVel,
368 const unsigned int a_IDN);
369
370
371 //--------------------------------------------------------------------------
372 // PROTECTED MEMBERS - DISPLAY PROPERTIES:
373 //--------------------------------------------------------------------------
374
375protected:
376
379
382
385
388
391
394
397
398
399 //--------------------------------------------------------------------------
400 // PUBLIC MEMBERS - DISPLAY PROPERTIES:
401 //--------------------------------------------------------------------------
402
403public:
404
407
410
411
412 //--------------------------------------------------------------------------
413 // PUBLIC MEMBERS - TRIANGLE AND VERTEX DATA:
414 //--------------------------------------------------------------------------
415
416public:
417
420
423
425 std::vector<cEdge> m_edges;
426};
427
428
429//------------------------------------------------------------------------------
430#ifndef DOXYGEN_SHOULD_SKIP_THIS
431//------------------------------------------------------------------------------
432
433//==============================================================================
444//==============================================================================
445struct cEdge
446{
447 //--------------------------------------------------------------------------
448 // CONSTRUCTOR & DESTRUCTOR:
449 //--------------------------------------------------------------------------
450
451public:
452
454 cEdge() { m_parent = NULL; m_triangle = 0; m_vertex0 = 0; m_vertex1 = 0; }
455
457 cEdge(cMesh* a_parent,
458 int a_vertex0,
459 int a_vertex1);
460
462 virtual ~cEdge() {};
463
464
465 //--------------------------------------------------------------------------
466 // PUBLIC METHODS:
467 //--------------------------------------------------------------------------
468
469public:
470
472 void set(cMesh* a_parent,
473 int a_vertex0,
474 int a_vertex1);
475
476
477 //--------------------------------------------------------------------------
478 // PUBLIC METHODS:
479 //--------------------------------------------------------------------------
480
481public:
483 int m_vertex0;
484
486 int m_vertex1;
487
489 int m_triangle;
490
492 cMesh* m_parent;
493};
494
495
496//------------------------------------------------------------------------------
497// OPERTAOR - (IMPLEMENTED FOR EDGE SORTING PURPOSES)
498//------------------------------------------------------------------------------
499inline bool operator<(const cEdge& a_edge0, const cEdge& a_edge1)
500{
501 double tol = 0.0;
502
503 cVector3d edge0Vertex0 = a_edge0.m_parent->m_vertices->getLocalPos(a_edge0.m_vertex0);
504 cVector3d edge0Vertex1 = a_edge0.m_parent->m_vertices->getLocalPos(a_edge0.m_vertex1);
505 cVector3d edge1Vertex0 = a_edge1.m_parent->m_vertices->getLocalPos(a_edge1.m_vertex0);
506 cVector3d edge1Vertex1 = a_edge1.m_parent->m_vertices->getLocalPos(a_edge1.m_vertex1);
507
508
509 if (cAbs(edge0Vertex0.x() - edge1Vertex0.x()) <= tol)
510 {
511 if (cAbs(edge0Vertex0.y() - edge1Vertex0.y()) <= tol)
512 {
513 if (cAbs(edge0Vertex0.z() - edge1Vertex0.z()) <= tol)
514 {
515 if (cAbs(edge0Vertex1.x() - edge1Vertex1.x()) <= tol)
516 {
517 if (cAbs(edge0Vertex1.y() - edge1Vertex1.y()) <= tol)
518 {
519 return (edge0Vertex1.z() < edge1Vertex1.z());
520 }
521 else
522 {
523 return (edge0Vertex1.y() < edge1Vertex1.y());
524 }
525 }
526 else
527 {
528 return (edge0Vertex1.x() < edge1Vertex1.x());
529 }
530 }
531 else
532 {
533 return (edge0Vertex0.z() < edge1Vertex0.z());
534 }
535 }
536 else
537 {
538 return (edge0Vertex0.y() < edge1Vertex0.y());
539 }
540 }
541 else
542 {
543 return (edge0Vertex0.x() < edge1Vertex0.x());
544 }
545}
546
547//------------------------------------------------------------------------------
548#endif // DOXYGEN_SHOULD_SKIP_THIS
549//------------------------------------------------------------------------------
550
551//------------------------------------------------------------------------------
552} // namespace chai3d
553//------------------------------------------------------------------------------
554
555//------------------------------------------------------------------------------
556#endif
557//------------------------------------------------------------------------------
558
Implements color properties.
Implements a base class for all objects.
Implements material properties.
Implements 2D textures.
This class provides support for OpenGL display lists.
Definition CDisplayList.h:77
This class implements a base class for all 2D or 3D objects in CHAI3D.
Definition CGenericObject.h:113
This class implements a 3D polygonal mesh.
Definition CMesh.h:109
cColorf m_normalsColor
Color used to render lines representing normals.
Definition CMesh.h:406
bool m_showTangents
If true, then tangents and bitangents are displayed.
Definition CMesh.h:384
bool removeTriangle(const unsigned int a_index)
This method removes a triangle from the triangle array.
Definition CMesh.cpp:451
virtual void renderTangents(cRenderOptions &a_options)
This method draws a small line for each tangent and bi-tangent.
Definition CMesh.cpp:1249
virtual void setTransparencyLevel(const float a_level, const bool a_applyToVertices=false, const bool a_applyToTextures=false, const bool a_affectChildren=false, const bool a_affectComponents=true)
This method sets the alpha value at each vertex and in all of my material colors.
Definition CMesh.cpp:635
unsigned int newTriangle(const unsigned int a_indexVertex0, const unsigned int a_indexVertex1, const unsigned int a_indexVertex2)
This method creates a new triangle by passing vertex indices.
Definition CMesh.cpp:363
void setShowTriangles(const bool a_showTriangles)
This method enables or disables the rendering of triangles.
Definition CMesh.h:228
unsigned int newVertex(const double a_x=0.0, const double a_y=0.0, const double a_z=0.0, const double a_normalX=1.0, const double a_normalY=0.0, const double a_normalZ=0.0, const double a_textureCoordX=0.0, const double a_textureCoordY=0.0, const double a_textureCoordZ=0.0)
This method creates a new vertex and adds it to the vertex list.
Definition CMesh.cpp:300
bool getShowNormals() const
This method returns whether rendering of normals is enabled.
Definition CMesh.h:270
virtual std::string getClassName()
This method returns the name of the object class.
Definition CMesh.h:130
virtual void renderMesh(cRenderOptions &a_options)
This method renders all triangles, material and texture properties.
Definition CMesh.cpp:1445
virtual void rotateVertices(const cMatrix3d &a_rotation, const bool a_updateCollisionDetector=true)
This method rotates all vertex positions by applying a local rotation matrix.
Definition CMesh.cpp:722
void computeLocalInteraction(const cVector3d &a_toolPos, const cVector3d &a_toolVel, const unsigned int a_IDN)
This method updates the relationship between the tool and the current object.
Definition CMesh.cpp:1088
virtual void offsetVertices(const cVector3d &a_offset, const bool a_updateCollisionDetector=true)
This method shifts all vertex positions by the specified amount.
Definition CMesh.cpp:688
unsigned int getNumTriangles()
This method returns the number of stored triangles.
Definition CMesh.cpp:277
void setEdgeLineWidth(const double a_width)
This method sets the line width of edges.
Definition CMesh.h:257
void setNormalsProperties(const double a_length, const cColorf &a_color)
This method sets the graphic properties for normal-rendering.
Definition CMesh.cpp:775
std::vector< cEdge > m_edges
Array of Edges.
Definition CMesh.h:425
void computeAllEdges(double a_angleThresholdDeg=40.0)
This method creates a list of edges by providing a threshold angle in degrees.
Definition CMesh.cpp:867
virtual void updateBoundaryBox()
This method updates the boundary box dimensions based on the vertices.
Definition CMesh.cpp:789
cDisplayList m_displayListEdges
Display list for edges.
Definition CMesh.h:396
void setNormalsLength(const double a_length)
This method set the length of normals for display purposes.
Definition CMesh.h:276
void clearAllEdges()
This method clears all edges.
Definition CMesh.cpp:1003
bool m_showNormals
If true, then normals are displayed.
Definition CMesh.h:381
virtual void updateGlobalPositions(const bool a_frameOnly)
This method updates the global position of each vertex.
Definition CMesh.cpp:587
void scaleXYZ(const double a_scaleX, const double a_scaleY, const double a_scaleZ)
This method scale this mesh by using different scale factors along X, Y and Z axes.
Definition CMesh.cpp:563
virtual cVector3d getCenterOfMass()
This method computes the center of mass of this mesh, based on vertex positions.
Definition CMesh.cpp:251
cVertexArrayPtr m_vertices
Array of vertices.
Definition CMesh.h:419
unsigned int getNumVertices() const
This method returns the number of stored vertices.
Definition CMesh.h:187
bool getShowTriangles() const
This method returns whether rendering of triangles is enabled.
Definition CMesh.h:231
cColorf m_edgeLineColor
Color used to render lines representing edges.
Definition CMesh.h:409
cTriangleArrayPtr m_triangles
Array of triangles.
Definition CMesh.h:422
void copyMeshProperties(cMesh *a_obj, const bool a_duplicateMaterialData, const bool a_duplicateTextureData, const bool a_duplicateMeshData, const bool a_buildCollisionDetector)
This method copies all properties of this mesh to another.
Definition CMesh.cpp:185
virtual void createBruteForceCollisionDetector()
This method builds a brute force collision detector for this mesh.
Definition CMesh.cpp:1032
void setShowEdges(const bool a_showEdges)
This method enables or disables the rendering of edges.
Definition CMesh.h:247
virtual void renderNormals(cRenderOptions &a_options)
This method draws a small line for each vertex normal.
Definition CMesh.cpp:1155
void setVertexColor(const cColorf &a_color)
This method sets the color of each vertex.
Definition CMesh.cpp:665
void setEdgeProperties(const double a_width, const cColorf &a_color)
This method sets the graphic properties for edge-rendering.
Definition CMesh.cpp:1019
virtual ~cMesh()
Destructor of cMesh.
Definition CMesh.cpp:134
virtual void render(cRenderOptions &a_options)
This method renders this object graphically using OpenGL.
Definition CMesh.cpp:1105
bool m_showEdges
If true, then show edges.
Definition CMesh.h:390
double m_edgeLineWidth
Width of edge lines.
Definition CMesh.h:393
void computeAllNormals()
This method computes all triangle normals.
Definition CMesh.cpp:493
virtual void createAABBCollisionDetector(const double a_radius)
This method builds an AABB collision detector for this mesh.
Definition CMesh.cpp:1053
void clear()
This method clears all triangles and vertices of mesh.
Definition CMesh.cpp:471
virtual void scaleObject(const double &a_scaleFactor)
This method scales this object by a scale factor.
Definition CMesh.h:363
bool getShowTangents() const
This method returns whether rendering of tangents and bi-tangents is enabled.
Definition CMesh.h:291
void setShowTangents(const bool a_showTangents)
This method enables or disables the rendering of tangents and bi-tangents.
Definition CMesh.h:288
virtual void renderEdges(cRenderOptions &a_options)
This method draws all edges of the mesh.
Definition CMesh.cpp:1356
double m_normalsLength
Length of each normal (for graphic rendering of normals).
Definition CMesh.h:387
virtual cMesh * copy(const bool a_duplicateMaterialData=false, const bool a_duplicateTextureData=false, const bool a_duplicateMeshData=false, const bool a_buildCollisionDetector=false)
This method creates a copy of itself.
Definition CMesh.cpp:154
bool getShowEdges() const
This method returns whether rendering of edges is enabled.
Definition CMesh.h:250
void computeBTN()
This method computes the normal matrix vectors for all triangles.
Definition CMesh.cpp:547
virtual void reverseAllNormals()
This method reverses all surface normals.
Definition CMesh.cpp:755
bool m_showTriangles
If true, then triangles are displayed.
Definition CMesh.h:378
virtual void markForUpdate(const bool a_affectChildren=false, const bool a_affectComponents=true)
This method invalidates any existing display lists and marks the mesh for update.
Definition CMesh.cpp:610
void setShowNormals(const bool a_showNormals)
This method enables or disables the rendering of vertex normals.
Definition CMesh.h:267
Definition CAudioBuffer.cpp:57
T cAbs(const T &a_value)
This function computes an absolute value.
Definition CMaths.h:198
std::shared_ptr< cMaterial > cMaterialPtr
Definition CMaterial.h:69
std::shared_ptr< cVertexArray > cVertexArrayPtr
Definition CVertexArray.h:108
std::shared_ptr< cTriangleArray > cTriangleArrayPtr
Definition CTriangleArray.h:66
This class defines a color using a GLfloat representation for each component.
Definition CColor.h:139
This class implements a 3D matrix.
Definition CMatrix3d.h:98
This structures provide a containers for storing rendering options that are passed through the sceneg...
Definition CRenderOptions.h:83
This class implements a 3D vector.
Definition CVector3d.h:89