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.1.1 $Rev: 1869 $
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_applyToTextures = false,
145  const bool a_affectChildren = false);
146 
147 
148  //-----------------------------------------------------------------------
149  // PUBLIC METHODS - DISPLAY LISTS:
150  //-----------------------------------------------------------------------
151 
152 public:
153 
155  virtual void markForUpdate(const bool a_affectChildren = false);
156 
157 
158  //--------------------------------------------------------------------------
159  // PUBLIC METHODS - VERTICES
160  //--------------------------------------------------------------------------
161 
162 public:
163 
165  unsigned int newVertex(const double a_x = 0.0,
166  const double a_y = 0.0,
167  const double a_z = 0.0,
168  const double a_normalX = 1.0,
169  const double a_normalY = 0.0,
170  const double a_normalZ = 0.0,
171  const double a_textureCoordX = 0.0,
172  const double a_textureCoordY = 0.0,
173  const double a_textureCoordZ = 0.0);
174 
176  unsigned int newVertex(const cVector3d& a_pos,
177  const cVector3d& a_normal = cVector3d(1,0,0),
178  const cVector3d& a_textureCoord = cVector3d(0,0,0),
179  const cColorf& a_color = cColorf(0,0,0,1));
180 
182  inline unsigned int getNumVertices() const { return (unsigned int)(m_vertices->getNumVertices()); }
183 
185  void setVertexColor(const cColorf& a_color);
186 
187 
188  //--------------------------------------------------------------------------
189  // PUBLIC METHODS - TRIANGLES
190  //--------------------------------------------------------------------------
191 
192 public:
193 
195  unsigned int newTriangle(const unsigned int a_indexVertex0,
196  const unsigned int a_indexVertex1,
197  const unsigned int a_indexVertex2);
198 
200  unsigned int newTriangle(const cVector3d& a_vertex0 = cVector3d(0,0,0),
201  const cVector3d& a_vertex1 = cVector3d(0,0,0),
202  const cVector3d& a_vertex2 = cVector3d(0,0,0),
203  const cVector3d& a_normal0 = cVector3d(1,0,0),
204  const cVector3d& a_normal1 = cVector3d(1,0,0),
205  const cVector3d& a_normal2 = cVector3d(1,0,0),
206  const cVector3d& a_textureCoord0 = cVector3d(0,0,0),
207  const cVector3d& a_textureCoord1 = cVector3d(0,0,0),
208  const cVector3d& a_textureCoord2 = cVector3d(0,0,0),
209  const cColorf& a_colorVertex0 = cColorf(0,0,0,1),
210  const cColorf& a_colorVertex1 = cColorf(0,0,0,1),
211  const cColorf& a_colorVertex2 = cColorf(0,0,0,1));
212 
214  bool removeTriangle(const unsigned int a_index);
215 
217  unsigned int getNumTriangles();
218 
220  void clear();
221 
223  void setShowTriangles(const bool a_showTriangles) { m_showTriangles = a_showTriangles; }
224 
226  bool getShowTriangles() const { return (m_showTriangles); }
227 
228 
229  //--------------------------------------------------------------------------
230  // PUBLIC METHODS - EDGES
231  //--------------------------------------------------------------------------
232 
233 public:
234 
236  void computeAllEdges(double a_angleThresholdDeg = 40.0);
237 
239  void clearAllEdges();
240 
242  void setShowEdges(const bool a_showEdges) { m_showEdges = a_showEdges; }
243 
245  bool getShowEdges() const { return (m_showEdges); }
246 
248  void setEdgeProperties(const double a_width,
249  const cColorf& a_color);
250 
252  void setEdgeLineWidth(const double a_width) { setEdgeProperties(a_width, m_edgeLineColor); }
253 
254 
255  //--------------------------------------------------------------------------
256  // PUBLIC METHODS - SURFACE NORMALS
257  //--------------------------------------------------------------------------
258 
259 public:
260 
262  void setShowNormals(const bool a_showNormals) { m_showNormals = a_showNormals; }
263 
265  bool getShowNormals() const { return (m_showNormals); }
266 
268  void setNormalsProperties(const double a_length, const cColorf& a_color);
269 
271  void setNormalsLength(const double a_length) { setNormalsProperties(a_length, m_normalsColor); }
272 
274  void computeAllNormals();
275 
277  virtual void reverseAllNormals();
278 
279 
280  //--------------------------------------------------------------------------
281  // PUBLIC METHODS - COLLISION DETECTION:
282  //--------------------------------------------------------------------------
283 
284 public:
285 
287  virtual void createBruteForceCollisionDetector();
288 
290  virtual void createAABBCollisionDetector(const double a_radius);
291 
292 
293  //--------------------------------------------------------------------------
294  // PUBLIC METHODS - GEOMETRY:
295  //--------------------------------------------------------------------------
296 
297 public:
298 
300  void scaleXYZ(const double a_scaleX, const double a_scaleY, const double a_scaleZ);
301 
303  virtual void offsetVertices(const cVector3d& a_offset,
304  const bool a_updateCollisionDetector = true);
305 
307  virtual cVector3d getCenterOfMass();
308 
309 
310  //--------------------------------------------------------------------------
311  // PROTECTED METHODS - INTERNAL
312  //--------------------------------------------------------------------------
313 
314 protected:
315 
317  virtual void render(cRenderOptions& a_options);
318 
320  virtual void renderNormals(cRenderOptions& a_options);
321 
323  virtual void renderEdges(cRenderOptions& a_options);
324 
326  virtual void renderMesh(cRenderOptions& a_options);
327 
329  virtual void updateGlobalPositions(const bool a_frameOnly);
330 
332  virtual void updateBoundaryBox();
333 
335  void copyMeshProperties(cMesh* a_obj,
336  const bool a_duplicateMaterialData,
337  const bool a_duplicateTextureData,
338  const bool a_duplicateMeshData,
339  const bool a_buildCollisionDetector);
340 
342  virtual void scaleObject(const double& a_scaleFactor) { scaleXYZ(a_scaleFactor, a_scaleFactor, a_scaleFactor); }
343 
345  void computeLocalInteraction(const cVector3d& a_toolPos,
346  const cVector3d& a_toolVel,
347  const unsigned int a_IDN);
348 
349 
350  //--------------------------------------------------------------------------
351  // PROTECTED MEMBERS - DISPLAY PROPERTIES:
352  //--------------------------------------------------------------------------
353 
354 protected:
355 
358 
361 
364 
367 
370 
373 
374 
375  //--------------------------------------------------------------------------
376  // PUBLIC MEMBERS - DISPLAY PROPERTIES:
377  //--------------------------------------------------------------------------
378 
379 public:
380 
383 
386 
387 
388  //--------------------------------------------------------------------------
389  // PUBLIC MEMBERS - TRIANGLE AND VERTEX DATA:
390  //--------------------------------------------------------------------------
391 
392 public:
393 
395  cVertexArrayPtr m_vertices;
396 
398  cTriangleArrayPtr m_triangles;
399 
401  std::vector<cEdge> m_edges;
402 };
403 
404 
405 //------------------------------------------------------------------------------
406 #ifndef DOXYGEN_SHOULD_SKIP_THIS
407 //------------------------------------------------------------------------------
408 
409 //==============================================================================
420 //==============================================================================
421 struct cEdge
422 {
423  //--------------------------------------------------------------------------
424  // CONSTRUCTOR & DESTRUCTOR:
425  //--------------------------------------------------------------------------
426 
427 public:
428 
430  cEdge() { m_parent = NULL; m_triangle = 0; m_vertex0 = 0; m_vertex1 = 0; }
431 
433  cEdge(cMesh* a_parent,
434  int a_vertex0,
435  int a_vertex1);
436 
438  ~cEdge() {};
439 
440 
441  //--------------------------------------------------------------------------
442  // PUBLIC METHODS:
443  //--------------------------------------------------------------------------
444 
445 public:
446 
448  void set(cMesh* a_parent,
449  int a_vertex0,
450  int a_vertex1);
451 
452 
453  //--------------------------------------------------------------------------
454  // PUBLIC METHODS:
455  //--------------------------------------------------------------------------
456 
457 public:
459  int m_vertex0;
460 
462  int m_vertex1;
463 
465  int m_triangle;
466 
468  cMesh* m_parent;
469 };
470 
471 
472 //------------------------------------------------------------------------------
473 // OPERTAOR - (IMPLEMENTED FOR EDGE SORTING PURPOSES)
474 //------------------------------------------------------------------------------
475 inline bool operator<(const cEdge& a_edge0, const cEdge& a_edge1)
476 {
477  double tol = 0.0;
478 
479  cVector3d edge0Vertex0 = a_edge0.m_parent->m_vertices->getLocalPos(a_edge0.m_vertex0);
480  cVector3d edge0Vertex1 = a_edge0.m_parent->m_vertices->getLocalPos(a_edge0.m_vertex1);
481  cVector3d edge1Vertex0 = a_edge1.m_parent->m_vertices->getLocalPos(a_edge1.m_vertex0);
482  cVector3d edge1Vertex1 = a_edge1.m_parent->m_vertices->getLocalPos(a_edge1.m_vertex1);
483 
484 
485  if (cAbs(edge0Vertex0.x() - edge1Vertex0.x()) <= tol)
486  {
487  if (cAbs(edge0Vertex0.y() - edge1Vertex0.y()) <= tol)
488  {
489  if (cAbs(edge0Vertex0.z() - edge1Vertex0.z()) <= tol)
490  {
491  if (cAbs(edge0Vertex1.x() - edge1Vertex1.x()) <= tol)
492  {
493  if (cAbs(edge0Vertex1.y() - edge1Vertex1.y()) <= tol)
494  {
495  return (edge0Vertex1.z() < edge1Vertex1.z());
496  }
497  else
498  {
499  return (edge0Vertex1.y() < edge1Vertex1.y());
500  }
501  }
502  else
503  {
504  return (edge0Vertex1.x() < edge1Vertex1.x());
505  }
506  }
507  else
508  {
509  return (edge0Vertex0.z() < edge1Vertex0.z());
510  }
511  }
512  else
513  {
514  return (edge0Vertex0.y() < edge1Vertex0.y());
515  }
516  }
517  else
518  {
519  return (edge0Vertex0.x() < edge1Vertex0.x());
520  }
521 }
522 
523 //------------------------------------------------------------------------------
524 #endif // DOXYGEN_SHOULD_SKIP_THIS
525 //------------------------------------------------------------------------------
526 
527 //------------------------------------------------------------------------------
528 } // namespace chai3d
529 //------------------------------------------------------------------------------
530 
531 //------------------------------------------------------------------------------
532 #endif
533 //------------------------------------------------------------------------------
534 
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:363
virtual void createBruteForceCollisionDetector()
This method builds a brute force collision detector for this mesh.
Definition: CMesh.cpp:976
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:546
virtual void scaleObject(const double &a_scaleFactor)
This method scales this object by a scale factor.
Definition: CMesh.h:342
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:1036
bool m_showNormals
If true, then normals are displayed.
Definition: CMesh.h:360
void setEdgeLineWidth(const double a_width)
This method sets the line width of edges.
Definition: CMesh.h:252
void setNormalsProperties(const double a_length, const cColorf &a_color)
This method sets the graphic properties for normal-rendering.
Definition: CMesh.cpp:718
cDisplayList m_displayListEdges
Display list for edges.
Definition: CMesh.h:372
This class provides support for OpenGL display lists.
Definition: CDisplayList.h:76
void computeAllNormals()
This method computes all triangle normals, optionally propagating the operation to my children...
Definition: CMesh.cpp:487
void setNormalsLength(const double a_length)
This method set the length of normals for display purposes.
Definition: CMesh.h:271
virtual void renderMesh(cRenderOptions &a_options)
This method renders all triangles, material and texture properties.
Definition: CMesh.cpp:1254
void setVertexColor(const cColorf &a_color)
This method sets the color of each vertex.
Definition: CMesh.cpp:642
std::vector< cEdge > m_edges
Array of Edges.
Definition: CMesh.h:401
bool m_showTriangles
If true, then triangles are displayed.
Definition: CMesh.h:357
cVertexArrayPtr m_vertices
Array of vertices.
Definition: CMesh.h:395
cColorf m_edgeLineColor
Color used to render lines representing edges.
Definition: CMesh.h:385
std::shared_ptr< cMaterial > cMaterialPtr
Definition: CMaterial.h:67
bool m_showEdges
If true, then show edges.
Definition: CMesh.h:366
virtual void render(cRenderOptions &a_options)
This method renders this object graphically using OpenGL.
Definition: CMesh.cpp:1053
void setShowNormals(const bool a_showNormals)
This method enables or disables the rendering of vertex normals.
Definition: CMesh.h:262
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:226
virtual void createAABBCollisionDetector(const double a_radius)
This method builds an AABB collision detector for this mesh.
Definition: CMesh.cpp:1001
virtual void renderEdges(cRenderOptions &a_options)
This method draws all edges of the mesh.
Definition: CMesh.cpp:1165
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:570
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:151
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:360
unsigned int getNumVertices() const
This method returns the number of stored vertices.
Definition: CMesh.h:182
Implements a base class for all objects.
bool removeTriangle(const unsigned int a_index)
This method removes a triangle from the triangle array.
Definition: CMesh.cpp:448
virtual void reverseAllNormals()
This method reverses all surface normals.
Definition: CMesh.cpp:698
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:274
cColorf m_normalsColor
Color used to render lines representing normals.
Definition: CMesh.h:382
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:398
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:1095
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:810
Implements material properties.
void clearAllEdges()
This method clears all edges.
Definition: CMesh.cpp:947
void setEdgeProperties(const double a_width, const cColorf &a_color)
This method sets the graphic properties for edge-rendering.
Definition: CMesh.cpp:963
virtual void setTransparencyLevel(const float a_level, 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:616
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:297
void setShowTriangles(const bool a_showTriangles)
This method enables or disables the rendering of triangles.
Definition: CMesh.h:223
void clear()
This method clears all triangles and vertices of mesh.
Definition: CMesh.cpp:468
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:665
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:248
virtual void markForUpdate(const bool a_affectChildren=false)
This method invalidates any existing display lists and marks the mesh for update. ...
Definition: CMesh.cpp:592
virtual ~cMesh()
Destructor of cMesh.
Definition: CMesh.cpp:131
bool getShowNormals() const
This method returns whether rendering of normals is enabled.
Definition: CMesh.h:265
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:182
double m_edgeLineWidth
Width of edge lines.
Definition: CMesh.h:369
void setShowEdges(const bool a_showEdges)
This method enables or disables the rendering of edges.
Definition: CMesh.h:242
bool getShowEdges() const
This method returns whether rendering of edges is enabled.
Definition: CMesh.h:245
virtual void updateBoundaryBox()
This method updates the boundary box dimensions based on the vertices.
Definition: CMesh.cpp:732