CMesh.h
Go to the documentation of this file.
1 //==============================================================================
2 /*
3  Software License Agreement (BSD License)
4  Copyright (c) 2003-2016, 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.2.0 $Rev: 2161 $
43 */
44 //==============================================================================
45 
46 //------------------------------------------------------------------------------
47 #ifdef _MSVC
48 #pragma warning (disable : 4786)
49 #endif
50 //------------------------------------------------------------------------------
51 #ifndef CMeshH
52 #define CMeshH
53 //------------------------------------------------------------------------------
54 #include "world/CGenericObject.h"
55 #include "materials/CMaterial.h"
56 #include "materials/CTexture2d.h"
57 #include "graphics/CColor.h"
58 //------------------------------------------------------------------------------
59 #include <vector>
60 #include <list>
61 //------------------------------------------------------------------------------
62 
63 //------------------------------------------------------------------------------
64 namespace chai3d {
65 //------------------------------------------------------------------------------
66 
67 //------------------------------------------------------------------------------
68 class cWorld;
69 //------------------------------------------------------------------------------
70 
71 //==============================================================================
78 //==============================================================================
79 //------------------------------------------------------------------------------
80 
81 // Array of vertices.
82 class cVertexArray;
83 typedef std::shared_ptr<cVertexArray> cVertexArrayPtr;
84 
85 // Array of triangles.
86 class cTriangleArray;
87 typedef std::shared_ptr<cTriangleArray> cTriangleArrayPtr;
88 
89 // Edges
90 struct cEdge;
91 
92 //------------------------------------------------------------------------------
93 
94 //==============================================================================
107 //==============================================================================
108 class cMesh : public cGenericObject
109 {
110  //--------------------------------------------------------------------------
111  // CONSTRUCTOR & DESTRUCTOR:
112  //--------------------------------------------------------------------------
113 
114 public:
115 
117  cMesh(cMaterialPtr a_material = cMaterialPtr());
118 
120  virtual ~cMesh();
121 
122 
123  //--------------------------------------------------------------------------
124  // PUBLIC METHODS - GENERAL
125  //--------------------------------------------------------------------------
126 
127 public:
128 
130  virtual cMesh* copy(const bool a_duplicateMaterialData = false,
131  const bool a_duplicateTextureData = false,
132  const bool a_duplicateMeshData = false,
133  const bool a_buildCollisionDetector = false);
134 
135 
136  //-----------------------------------------------------------------------
137  // PUBLIC METHODS - GRAPHIC PROPERTIES:
138  //-----------------------------------------------------------------------
139 
140 public:
141 
143  virtual void setTransparencyLevel(const float a_level,
144  const bool a_applyToVertices = false,
145  const bool a_applyToTextures = false,
146  const bool a_affectChildren = false);
147 
148 
149  //-----------------------------------------------------------------------
150  // PUBLIC METHODS - DISPLAY LISTS:
151  //-----------------------------------------------------------------------
152 
153 public:
154 
156  virtual void markForUpdate(const bool a_affectChildren = false);
157 
158 
159  //--------------------------------------------------------------------------
160  // PUBLIC METHODS - VERTICES
161  //--------------------------------------------------------------------------
162 
163 public:
164 
166  unsigned int newVertex(const double a_x = 0.0,
167  const double a_y = 0.0,
168  const double a_z = 0.0,
169  const double a_normalX = 1.0,
170  const double a_normalY = 0.0,
171  const double a_normalZ = 0.0,
172  const double a_textureCoordX = 0.0,
173  const double a_textureCoordY = 0.0,
174  const double a_textureCoordZ = 0.0);
175 
177  unsigned int newVertex(const cVector3d& a_pos,
178  const cVector3d& a_normal = cVector3d(1,0,0),
179  const cVector3d& a_textureCoord = cVector3d(0,0,0),
180  const cColorf& a_color = cColorf(0,0,0,1));
181 
183  inline unsigned int getNumVertices() const { return (unsigned int)(m_vertices->getNumElements()); }
184 
186  void setVertexColor(const cColorf& a_color);
187 
188 
189  //--------------------------------------------------------------------------
190  // PUBLIC METHODS - TRIANGLES
191  //--------------------------------------------------------------------------
192 
193 public:
194 
196  unsigned int newTriangle(const unsigned int a_indexVertex0,
197  const unsigned int a_indexVertex1,
198  const unsigned int a_indexVertex2);
199 
201  unsigned int newTriangle(const cVector3d& a_vertex0 = cVector3d(0,0,0),
202  const cVector3d& a_vertex1 = cVector3d(0,0,0),
203  const cVector3d& a_vertex2 = cVector3d(0,0,0),
204  const cVector3d& a_normal0 = cVector3d(1,0,0),
205  const cVector3d& a_normal1 = cVector3d(1,0,0),
206  const cVector3d& a_normal2 = cVector3d(1,0,0),
207  const cVector3d& a_textureCoord0 = cVector3d(0,0,0),
208  const cVector3d& a_textureCoord1 = cVector3d(0,0,0),
209  const cVector3d& a_textureCoord2 = cVector3d(0,0,0),
210  const cColorf& a_colorVertex0 = cColorf(0,0,0,1),
211  const cColorf& a_colorVertex1 = cColorf(0,0,0,1),
212  const cColorf& a_colorVertex2 = cColorf(0,0,0,1));
213 
215  bool removeTriangle(const unsigned int a_index);
216 
218  unsigned int getNumTriangles();
219 
221  void clear();
222 
224  void setShowTriangles(const bool a_showTriangles) { m_showTriangles = a_showTriangles; }
225 
227  bool getShowTriangles() const { return (m_showTriangles); }
228 
229 
230  //--------------------------------------------------------------------------
231  // PUBLIC METHODS - EDGES
232  //--------------------------------------------------------------------------
233 
234 public:
235 
237  void computeAllEdges(double a_angleThresholdDeg = 40.0);
238 
240  void clearAllEdges();
241 
243  void setShowEdges(const bool a_showEdges) { m_showEdges = a_showEdges; }
244 
246  bool getShowEdges() const { return (m_showEdges); }
247 
249  void setEdgeProperties(const double a_width,
250  const cColorf& a_color);
251 
253  void setEdgeLineWidth(const double a_width) { setEdgeProperties(a_width, m_edgeLineColor); }
254 
255 
256  //--------------------------------------------------------------------------
257  // PUBLIC METHODS - SURFACE NORMALS, TANGENTS, BITANGENTS
258  //--------------------------------------------------------------------------
259 
260 public:
261 
263  void setShowNormals(const bool a_showNormals) { m_showNormals = a_showNormals; }
264 
266  bool getShowNormals() const { return (m_showNormals); }
267 
269  void setNormalsProperties(const double a_length, const cColorf& a_color);
270 
272  void setNormalsLength(const double a_length) { setNormalsProperties(a_length, m_normalsColor); }
273 
275  void computeAllNormals();
276 
278  virtual void reverseAllNormals();
279 
281  void computeBTN();
282 
284  void setShowTangents(const bool a_showTangents) { m_showTangents = a_showTangents; }
285 
287  bool getShowTangents() const { return (m_showTangents); }
288 
289 
290  //--------------------------------------------------------------------------
291  // PUBLIC METHODS - COLLISION DETECTION:
292  //--------------------------------------------------------------------------
293 
294 public:
295 
297  virtual void createBruteForceCollisionDetector();
298 
300  virtual void createAABBCollisionDetector(const double a_radius);
301 
302 
303  //--------------------------------------------------------------------------
304  // PUBLIC METHODS - GEOMETRY:
305  //--------------------------------------------------------------------------
306 
307 public:
308 
310  void scaleXYZ(const double a_scaleX, const double a_scaleY, const double a_scaleZ);
311 
313  virtual void offsetVertices(const cVector3d& a_offset,
314  const bool a_updateCollisionDetector = true);
315 
317  virtual cVector3d getCenterOfMass();
318 
319 
320  //--------------------------------------------------------------------------
321  // PROTECTED METHODS - INTERNAL
322  //--------------------------------------------------------------------------
323 
324 protected:
325 
327  virtual void render(cRenderOptions& a_options);
328 
330  virtual void renderNormals(cRenderOptions& a_options);
331 
333  virtual void renderTangents(cRenderOptions& a_options);
334 
336  virtual void renderEdges(cRenderOptions& a_options);
337 
339  virtual void renderMesh(cRenderOptions& a_options);
340 
342  virtual void updateGlobalPositions(const bool a_frameOnly);
343 
345  virtual void updateBoundaryBox();
346 
348  void copyMeshProperties(cMesh* a_obj,
349  const bool a_duplicateMaterialData,
350  const bool a_duplicateTextureData,
351  const bool a_duplicateMeshData,
352  const bool a_buildCollisionDetector);
353 
355  virtual void scaleObject(const double& a_scaleFactor) { scaleXYZ(a_scaleFactor, a_scaleFactor, a_scaleFactor); }
356 
358  void computeLocalInteraction(const cVector3d& a_toolPos,
359  const cVector3d& a_toolVel,
360  const unsigned int a_IDN);
361 
362 
363  //--------------------------------------------------------------------------
364  // PROTECTED MEMBERS - DISPLAY PROPERTIES:
365  //--------------------------------------------------------------------------
366 
367 protected:
368 
371 
374 
377 
380 
383 
386 
389 
390 
391  //--------------------------------------------------------------------------
392  // PUBLIC MEMBERS - DISPLAY PROPERTIES:
393  //--------------------------------------------------------------------------
394 
395 public:
396 
399 
402 
403 
404  //--------------------------------------------------------------------------
405  // PUBLIC MEMBERS - TRIANGLE AND VERTEX DATA:
406  //--------------------------------------------------------------------------
407 
408 public:
409 
411  cVertexArrayPtr m_vertices;
412 
414  cTriangleArrayPtr m_triangles;
415 
417  std::vector<cEdge> m_edges;
418 };
419 
420 
421 //------------------------------------------------------------------------------
422 #ifndef DOXYGEN_SHOULD_SKIP_THIS
423 //------------------------------------------------------------------------------
424 
425 //==============================================================================
436 //==============================================================================
437 struct cEdge
438 {
439  //--------------------------------------------------------------------------
440  // CONSTRUCTOR & DESTRUCTOR:
441  //--------------------------------------------------------------------------
442 
443 public:
444 
446  cEdge() { m_parent = NULL; m_triangle = 0; m_vertex0 = 0; m_vertex1 = 0; }
447 
449  cEdge(cMesh* a_parent,
450  int a_vertex0,
451  int a_vertex1);
452 
454  ~cEdge() {};
455 
456 
457  //--------------------------------------------------------------------------
458  // PUBLIC METHODS:
459  //--------------------------------------------------------------------------
460 
461 public:
462 
464  void set(cMesh* a_parent,
465  int a_vertex0,
466  int a_vertex1);
467 
468 
469  //--------------------------------------------------------------------------
470  // PUBLIC METHODS:
471  //--------------------------------------------------------------------------
472 
473 public:
475  int m_vertex0;
476 
478  int m_vertex1;
479 
481  int m_triangle;
482 
484  cMesh* m_parent;
485 };
486 
487 
488 //------------------------------------------------------------------------------
489 // OPERTAOR - (IMPLEMENTED FOR EDGE SORTING PURPOSES)
490 //------------------------------------------------------------------------------
491 inline bool operator<(const cEdge& a_edge0, const cEdge& a_edge1)
492 {
493  double tol = 0.0;
494 
495  cVector3d edge0Vertex0 = a_edge0.m_parent->m_vertices->getLocalPos(a_edge0.m_vertex0);
496  cVector3d edge0Vertex1 = a_edge0.m_parent->m_vertices->getLocalPos(a_edge0.m_vertex1);
497  cVector3d edge1Vertex0 = a_edge1.m_parent->m_vertices->getLocalPos(a_edge1.m_vertex0);
498  cVector3d edge1Vertex1 = a_edge1.m_parent->m_vertices->getLocalPos(a_edge1.m_vertex1);
499 
500 
501  if (cAbs(edge0Vertex0.x() - edge1Vertex0.x()) <= tol)
502  {
503  if (cAbs(edge0Vertex0.y() - edge1Vertex0.y()) <= tol)
504  {
505  if (cAbs(edge0Vertex0.z() - edge1Vertex0.z()) <= tol)
506  {
507  if (cAbs(edge0Vertex1.x() - edge1Vertex1.x()) <= tol)
508  {
509  if (cAbs(edge0Vertex1.y() - edge1Vertex1.y()) <= tol)
510  {
511  return (edge0Vertex1.z() < edge1Vertex1.z());
512  }
513  else
514  {
515  return (edge0Vertex1.y() < edge1Vertex1.y());
516  }
517  }
518  else
519  {
520  return (edge0Vertex1.x() < edge1Vertex1.x());
521  }
522  }
523  else
524  {
525  return (edge0Vertex0.z() < edge1Vertex0.z());
526  }
527  }
528  else
529  {
530  return (edge0Vertex0.y() < edge1Vertex0.y());
531  }
532  }
533  else
534  {
535  return (edge0Vertex0.x() < edge1Vertex0.x());
536  }
537 }
538 
539 //------------------------------------------------------------------------------
540 #endif // DOXYGEN_SHOULD_SKIP_THIS
541 //------------------------------------------------------------------------------
542 
543 //------------------------------------------------------------------------------
544 } // namespace chai3d
545 //------------------------------------------------------------------------------
546 
547 //------------------------------------------------------------------------------
548 #endif
549 //------------------------------------------------------------------------------
550 
This class implements a 3D vector.
Definition: CVector3d.h:88
This structures provide a containers for storing rendering options that are passed through the sceneg...
Definition: CRenderOptions.h:82
double m_normalsLength
Length of each normal (for graphic rendering of normals).
Definition: CMesh.h:379
virtual void createBruteForceCollisionDetector()
This method builds a brute force collision detector for this mesh.
Definition: CMesh.cpp:996
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 void scaleObject(const double &a_scaleFactor)
This method scales this object by a scale factor.
Definition: CMesh.h:355
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:1052
virtual void setTransparencyLevel(const float a_level, const bool a_applyToVertices=false, const bool a_applyToTextures=false, const bool a_affectChildren=false)
This method sets the alpha value at each vertex and in all of my material colors. ...
Definition: CMesh.cpp:634
bool m_showNormals
If true, then normals are displayed.
Definition: CMesh.h:373
void setEdgeLineWidth(const double a_width)
This method sets the line width of edges.
Definition: CMesh.h:253
void setNormalsProperties(const double a_length, const cColorf &a_color)
This method sets the graphic properties for normal-rendering.
Definition: CMesh.cpp:739
cDisplayList m_displayListEdges
Display list for edges.
Definition: CMesh.h:388
This class provides support for OpenGL display lists.
Definition: CDisplayList.h:76
void computeAllNormals()
This method computes all triangle normals.
Definition: CMesh.cpp:493
void setNormalsLength(const double a_length)
This method set the length of normals for display purposes.
Definition: CMesh.h:272
virtual void renderMesh(cRenderOptions &a_options)
This method renders all triangles, material and texture properties.
Definition: CMesh.cpp:1385
void setVertexColor(const cColorf &a_color)
This method sets the color of each vertex.
Definition: CMesh.cpp:663
virtual void renderTangents(cRenderOptions &a_options)
This method draws a small line for each tangent and bi-tangent.
Definition: CMesh.cpp:1189
std::vector< cEdge > m_edges
Array of Edges.
Definition: CMesh.h:417
bool m_showTriangles
If true, then triangles are displayed.
Definition: CMesh.h:370
void setShowTangents(const bool a_showTangents)
This method enables or disables the rendering of tangents and bi-tangents.
Definition: CMesh.h:284
cVertexArrayPtr m_vertices
Array of vertices.
Definition: CMesh.h:411
cColorf m_edgeLineColor
Color used to render lines representing edges.
Definition: CMesh.h:401
std::shared_ptr< cMaterial > cMaterialPtr
Definition: CMaterial.h:67
bool m_showEdges
If true, then show edges.
Definition: CMesh.h:382
virtual void render(cRenderOptions &a_options)
This method renders this object graphically using OpenGL.
Definition: CMesh.cpp:1069
void setShowNormals(const bool a_showNormals)
This method enables or disables the rendering of vertex normals.
Definition: CMesh.h:263
This class implements a base class for all 2D or 3D objects in CHAI3D.
Definition: CGenericObject.h:112
bool getShowTriangles() const
This method returns whether rendering of triangles is enabled.
Definition: CMesh.h:227
virtual void createAABBCollisionDetector(const double a_radius)
This method builds an AABB collision detector for this mesh.
Definition: CMesh.cpp:1017
virtual void renderEdges(cRenderOptions &a_options)
This method draws all edges of the mesh.
Definition: CMesh.cpp:1296
cMesh(cMaterialPtr a_material=cMaterialPtr())
Constructor of cMesh.
Definition: CMesh.cpp:76
virtual void updateGlobalPositions(const bool a_frameOnly)
This method updates the global position of each vertex.
Definition: CMesh.cpp:587
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
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
unsigned int getNumVertices() const
This method returns the number of stored vertices.
Definition: CMesh.h:183
Implements a base class for all objects.
bool getShowTangents() const
This method returns whether rendering of tangents and bi-tangents is enabled.
Definition: CMesh.h:287
bool removeTriangle(const unsigned int a_index)
This method removes a triangle from the triangle array.
Definition: CMesh.cpp:451
bool m_showTangents
If true, then tangents and bitangents are displayed.
Definition: CMesh.h:376
virtual void reverseAllNormals()
This method reverses all surface normals.
Definition: CMesh.cpp:719
This class implements a 3D polygonal mesh.
Definition: CMesh.h:108
unsigned int getNumTriangles()
This method returns the number of stored triangles.
Definition: CMesh.cpp:277
cColorf m_normalsColor
Color used to render lines representing normals.
Definition: CMesh.h:398
T cAbs(const T &a_value)
This function computes an absolute value.
Definition: CMaths.h:173
cTriangleArrayPtr m_triangles
Array of triangles.
Definition: CMesh.h:414
std::shared_ptr< cVertexArray > cVertexArrayPtr
Definition: CVertexArray.h:107
virtual void renderNormals(cRenderOptions &a_options)
This method draws a small line for each vertex normal.
Definition: CMesh.cpp:1119
This class defines a color using a GLfloat representation for each component.
Definition: CColor.h:138
Implements color properties.
void computeAllEdges(double a_angleThresholdDeg=40.0)
This method creates a list of edges by providing a threshold angle in degrees.
Definition: CMesh.cpp:831
double y() const
This method returns vector component y.
Definition: CVector3d.h:226
double x() const
This method returns vector component x.
Definition: CVector3d.h:220
Implements material properties.
void clearAllEdges()
This method clears all edges.
Definition: CMesh.cpp:967
void setEdgeProperties(const double a_width, const cColorf &a_color)
This method sets the graphic properties for edge-rendering.
Definition: CMesh.cpp:983
Definition: CAudioBuffer.cpp:56
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
cGenericObject * m_parent
Parent object.
Definition: CGenericObject.h:729
void setShowTriangles(const bool a_showTriangles)
This method enables or disables the rendering of triangles.
Definition: CMesh.h:224
void clear()
This method clears all triangles and vertices of mesh.
Definition: CMesh.cpp:471
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:686
Implements 2D textures.
std::shared_ptr< cTriangleArray > cTriangleArrayPtr
Definition: CTriangleArray.h:65
virtual cVector3d getCenterOfMass()
This method computes the center of mass of this mesh, based on vertex positions.
Definition: CMesh.cpp:251
virtual void markForUpdate(const bool a_affectChildren=false)
This method invalidates any existing display lists and marks the mesh for update. ...
Definition: CMesh.cpp:609
virtual ~cMesh()
Destructor of cMesh.
Definition: CMesh.cpp:134
bool getShowNormals() const
This method returns whether rendering of normals is enabled.
Definition: CMesh.h:266
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
void computeBTN()
This method computes the normal matrix vectors for all triangles.
Definition: CMesh.cpp:547
double m_edgeLineWidth
Width of edge lines.
Definition: CMesh.h:385
void setShowEdges(const bool a_showEdges)
This method enables or disables the rendering of edges.
Definition: CMesh.h:243
bool getShowEdges() const
This method returns whether rendering of edges is enabled.
Definition: CMesh.h:246
virtual void updateBoundaryBox()
This method updates the boundary box dimensions based on the vertices.
Definition: CMesh.cpp:753
double z() const
This method returns vector component z.
Definition: CVector3d.h:232