CTriangleArray.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  \version 3.1.1 $Rev: 1940 $
41 */
42 //==============================================================================
43 
44 //------------------------------------------------------------------------------
45 #ifndef CTriangleArrayH
46 #define CTriangleArrayH
47 //------------------------------------------------------------------------------
48 #include "graphics/CGenericArray.h"
49 //------------------------------------------------------------------------------
50 
51 //------------------------------------------------------------------------------
52 namespace chai3d {
53 //------------------------------------------------------------------------------
54 
55 //==============================================================================
62 //==============================================================================
63 
64 //------------------------------------------------------------------------------
66 typedef std::shared_ptr<cTriangleArray> cTriangleArrayPtr;
67 //------------------------------------------------------------------------------
68 
69 //==============================================================================
103 //==============================================================================
105 {
106  //--------------------------------------------------------------------------
107  // CONSTRUCTOR & DESTRUCTOR:
108  //--------------------------------------------------------------------------
109 
110 public:
111 
112  //--------------------------------------------------------------------------
118  //--------------------------------------------------------------------------
119  cTriangleArray(cVertexArrayPtr a_vertexArray) : cGenericArray(a_vertexArray)
120  {
121  // clear all triangles
122  clear();
123 
124  // store pointer to vertex array
125  m_vertices = a_vertexArray;
126 
127  // initialize OpenGL buffer ID
128  m_elementBuffer = 0;
129  }
130 
131 
132  //--------------------------------------------------------------------------
136  //--------------------------------------------------------------------------
138 
139 
141  void clear()
142  {
143  m_allocated.clear();
144  m_indices.clear();
145  m_freeElements.clear();
146  m_flagMarkForUpdate = true;
147  m_flagMarkForResize = true;
148  }
149 
150 
152  static cTriangleArrayPtr create(cVertexArrayPtr a_vertexArray) { return (std::make_shared<cTriangleArray>(a_vertexArray)); }
153 
154 
155  //--------------------------------------------------------------------------
156  // PUBLIC METHODS:
157  //--------------------------------------------------------------------------
158 
159 public:
160 
162  virtual unsigned int getNumVerticesPerElement() { return (3); }
163 
165  cTriangleArrayPtr copy();
166 
168  void compress();
169 
170 
171  //--------------------------------------------------------------------------
181  //--------------------------------------------------------------------------
182  int newTriangle(const unsigned int a_vertexIndex0,
183  const unsigned int a_vertexIndex1,
184  const unsigned int a_vertexIndex2)
185  {
186  int index;
187 
188  // check if there is an available slot on the free triangle list
189  if (m_freeElements.size() > 0)
190  {
191  index = m_freeElements.front();
192  m_allocated[index] = true;
193  m_freeElements.pop_front();
194  setVertices(index, a_vertexIndex0, a_vertexIndex1, a_vertexIndex2);
195  }
196  else
197  {
198  // store new indices
199  m_indices.push_back(a_vertexIndex0);
200  m_indices.push_back(a_vertexIndex1);
201  m_indices.push_back(a_vertexIndex2);
202  m_allocated.push_back(true);
203 
204  m_flagMarkForResize = true;
205 
206  // store index of new triangle
207  index = (int)(m_allocated.size())-1;
208  }
209 
210  // mark for update
211  m_flagMarkForUpdate = true;
212 
213  // return index to new triangle
214  return (index);
215  }
216 
217 
218  //--------------------------------------------------------------------------
226  //--------------------------------------------------------------------------
227  void removeTriangle(const unsigned int a_triangleIndex)
228  {
229  // sanity check
230  if (m_allocated[a_triangleIndex])
231  {
232  // disable triangle
233  m_allocated[a_triangleIndex] = false;
234 
235  // reset indices to vertices
236  setVertices(a_triangleIndex, 0, 0, 0);
237 
238  // add triangle to free list
239  m_freeElements.push_back(a_triangleIndex);
240 
241  // mark for update
242  m_flagMarkForUpdate = true;
243  }
244  }
245 
246 
247  //--------------------------------------------------------------------------
257  //--------------------------------------------------------------------------
258  inline void setVertices(const unsigned int a_triangleIndex,
259  const unsigned int a_vertexIndex0,
260  const unsigned int a_vertexIndex1,
261  const unsigned int a_vertexIndex2)
262  {
263  m_indices[3*a_triangleIndex+0] = a_vertexIndex0;
264  m_indices[3*a_triangleIndex+1] = a_vertexIndex1;
265  m_indices[3*a_triangleIndex+2] = a_vertexIndex2;
266 
267  // mark for update
268  m_flagMarkForUpdate = true;
269  }
270 
271 
272  //--------------------------------------------------------------------------
280  //--------------------------------------------------------------------------
281  inline unsigned int getVertexIndex0(const unsigned int a_triangleIndex) const
282  {
283  return (m_indices[3*a_triangleIndex+0]);
284  };
285 
286 
287  //--------------------------------------------------------------------------
295  //--------------------------------------------------------------------------
296  inline unsigned int getVertexIndex1(const unsigned int a_triangleIndex) const
297  {
298  return (m_indices[3*a_triangleIndex+1]);
299  };
300 
301 
302  //--------------------------------------------------------------------------
310  //--------------------------------------------------------------------------
311  inline unsigned int getVertexIndex2(const unsigned int a_triangleIndex) const
312  {
313  return (m_indices[3*a_triangleIndex+2]);
314  };
315 
316 
317  //--------------------------------------------------------------------------
328  //--------------------------------------------------------------------------
329  virtual unsigned int getVertexIndex(const unsigned int a_elementIndex,
330  const unsigned int a_vertexNumber) const
331  {
332  switch (a_vertexNumber)
333  {
334  case 0: return (m_indices[3*a_elementIndex+0]);
335  case 1: return (m_indices[3*a_elementIndex+1]);
336  case 2: return (m_indices[3*a_elementIndex+2]);
337  default: return (0);
338  }
339  }
340 
341 
342  //--------------------------------------------------------------------------
352  //--------------------------------------------------------------------------
353  cVector3d getTexCoordAtPosition(const unsigned int a_triangleIndex,
354  const cVector3d& a_localPos)
355  {
356  // get vertices of contact triangles
357  cVector3d vertex0 = m_vertices->getLocalPos(getVertexIndex0(a_triangleIndex));
358  cVector3d vertex1 = m_vertices->getLocalPos(getVertexIndex1(a_triangleIndex));
359  cVector3d vertex2 = m_vertices->getLocalPos(getVertexIndex2(a_triangleIndex));
360 
361  // project desired point on triangle
362  double a0 = 0;
363  double a1 = 0;
364  cProjectPointOnPlane(a_localPos, vertex0, vertex1, vertex2, a0, a1);
365  cVector3d vertex = cAdd(vertex0, cMul(a0, cSub(vertex1, vertex0)), cMul(a1, cSub(vertex2, vertex0)));
366 
367  // compute area of triangle
368  double area = cTriangleArea(vertex0, vertex1, vertex2);
369 
370  // compute areas of three sub-triangles formed by the three vertices of the triangle and the contact point.
371  double area0 = cTriangleArea(vertex, vertex1, vertex2);
372  double area1 = cTriangleArea(vertex, vertex0, vertex2);
373  double area2 = cTriangleArea(vertex, vertex0, vertex1);
374 
375  // compute weights based on position of contact point
376  double c0, c1, c2;
377  if (area > 0.0)
378  {
379  c0 = area0/area;
380  c1 = area1/area;
381  c2 = area2/area;
382  }
383  else
384  {
385  c0 = c1 = c2 = (1.0/3.0);
386  }
387 
388  cVector3d texCoord;
389 
390  if (m_vertices->getUseTexCoordData())
391  {
392  // retrieve the texture coordinate for each triangle vertex
393  cVector3d texCoord0 = m_vertices->getTexCoord(getVertexIndex0(a_triangleIndex));
394  cVector3d texCoord1 = m_vertices->getTexCoord(getVertexIndex1(a_triangleIndex));
395  cVector3d texCoord2 = m_vertices->getTexCoord(getVertexIndex2(a_triangleIndex));
396 
397  // compute the exact texture coordinate at the contact point
398  texCoord = cAdd( cMul(c0, texCoord0), cMul(c1, texCoord1), cMul(c2, texCoord2));
399  }
400  else
401  {
402  texCoord.set(0.0, 0.0, 0.0);
403  }
404 
405  // return result
406  return (texCoord);
407  }
408 
409 
410  //--------------------------------------------------------------------------
414  //--------------------------------------------------------------------------
415  inline void computeBTN()
416  {
417  unsigned int numTriangles = getNumElements();
418  for (unsigned i=0; i<numTriangles; i++)
419  {
420  cVector3d T,B,N;
421 
422  unsigned int index0 = getVertexIndex0(i);
423  unsigned int index1 = getVertexIndex1(i);
424  unsigned int index2 = getVertexIndex2(i);
425 
426  // calculate the vectors from the current vertex to the two other vertices in the triangle
427  cVector3d v1v0 = m_vertices->getLocalPos(index1) - m_vertices->getLocalPos(index0);
428  cVector3d v2v0 = m_vertices->getLocalPos(index2) - m_vertices->getLocalPos(index0);
429 
430  cVector3d tex0 = m_vertices->getTexCoord(index0);
431  cVector3d tex1 = m_vertices->getTexCoord(index1);
432  cVector3d tex2 = m_vertices->getTexCoord(index2);
433 
434  // calculate c1c0_T and c1c0_B
435  double c1c0_T = tex1.x() - tex0.x();
436  double c1c0_B = tex1.y() - tex0.y();
437 
438  // calculate c2c0_T and c2c0_B
439  double c2c0_T = tex2.x() - tex0.x();
440  double c2c0_B = tex2.y() - tex0.y();
441 
442  double fDenominator = c1c0_T * c2c0_B - c2c0_T * c1c0_B;
443  if (fabs(fDenominator) < C_TINY)
444  {
445  // we won't risk a divide by zero, so set the tangent matrix to the identity matrix
446  T = cVector3d(1.0, 0.0, 0.0);
447  B = cVector3d(0.0, 1.0, 0.0);
448  }
449  else
450  {
451  // calculate the reciprocal value once and for all (to achieve speed)
452  double fScale1 = 1.0f / fDenominator;
453 
454  // T and B are calculated just as the equation in the article states
455  T = cVector3d((c2c0_B * v1v0.x() - c1c0_B * v2v0.x()) * fScale1,
456  (c2c0_B * v1v0.y() - c1c0_B * v2v0.y()) * fScale1,
457  (c2c0_B * v1v0.z() - c1c0_B * v2v0.z()) * fScale1);
458 
459  B = cVector3d((-c2c0_T * v1v0.x() + c1c0_T * v2v0.x()) * fScale1,
460  (-c2c0_T * v1v0.y() + c1c0_T * v2v0.y()) * fScale1,
461  (-c2c0_T * v1v0.z() + c1c0_T * v2v0.z()) * fScale1);
462 
463  // Calculate the reciprocal value once and for all (to achieve speed)
464  /*
465  double fScale2 = 1.0f / ((T.x() * B.y() * N.z() - T.z() * B.y() * N.x()) +
466  (B.x() * N.y() * T.z() - B.z() * N.y() * T.x()) +
467  (N.x() * T.y() * B.z() - N.z() * T.y() * B.x()));
468  */
469 
470  // flipped variables (to be checked!)
471  m_vertices->m_tangent[i] = cNormalize(B);
472  m_vertices->m_bitangent[i] = cNormalize(T);
473 
474  // mark for update
475  m_vertices->m_flagTangentData = true;
476  m_vertices->m_flagBitangentData = true;
477  }
478  }
479  }
480 
481 
482  //--------------------------------------------------------------------------
486  //--------------------------------------------------------------------------
487  inline void renderInitialize()
488  {
489 #ifdef C_USE_OPENGL
490  unsigned int numtriangles = getNumElements();
491 
492  // allocate object and buffers if needed
493  if (m_elementBuffer == 0)
494  {
495  glGenBuffers(1, &m_elementBuffer);
496  }
497 
498  // bind buffer
499  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBuffer);
500 
501  // update buffer size if needed
503  {
504  glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3 * numtriangles * sizeof(unsigned int), &(m_indices[0]), GL_STATIC_DRAW);
505  m_flagMarkForResize = true;
506  }
507 
508  // update data if needed
510  {
511  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBuffer);
512  glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, 3 * numtriangles * sizeof(unsigned int), &(m_indices[0]));
513 
514  m_flagMarkForUpdate = false;
515  }
516 
517  // initialize rendering of vertices
518  m_vertices->renderInitialize();
519 
520  // render object
521  glEnableClientState(GL_VERTEX_ARRAY);
522  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_elementBuffer);
523  glDrawElements(GL_TRIANGLES, 3 * numtriangles, GL_UNSIGNED_INT, (void*)0);
524  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
525 #endif
526  }
527 
528  //--------------------------------------------------------------------------
532  //--------------------------------------------------------------------------
533  inline void renderFinalize()
534  {
535 #ifdef C_USE_OPENGL
536  // finalize rendering
537  m_vertices->renderFinalize();
538  glDisableClientState(GL_VERTEX_ARRAY);
539 #endif
540  }
541 
543  virtual bool computeCollision(const unsigned int a_elementIndex,
544  cGenericObject* a_object,
545  cVector3d& a_segmentPointA,
546  cVector3d& a_segmentPointB,
547  cCollisionRecorder& a_recorder,
548  cCollisionSettings& a_settings) const;
549 
550 
551  //--------------------------------------------------------------------------
557  //--------------------------------------------------------------------------
558  inline double computeArea(const unsigned int a_triangleIndex)
559  {
560  // A = 0.5 * | u x v |
561  cVector3d u = m_vertices->getLocalPos(getVertexIndex1(a_triangleIndex)) - m_vertices->getLocalPos(getVertexIndex0(a_triangleIndex));
562  cVector3d v = m_vertices->getLocalPos(getVertexIndex2(a_triangleIndex)) - m_vertices->getLocalPos(getVertexIndex0(a_triangleIndex));
563 
564  return (0.5 * (cCross(u,v).length()));
565  }
566 
567 
568  //--------------------------------------------------------------------------
578  //--------------------------------------------------------------------------
579  cVector3d computeNormal(const unsigned int a_triangleIndex,
580  const bool a_applyNormalToVertices)
581  {
582  // get pointer to vertices
583  int vertex0 = getVertexIndex0(a_triangleIndex);
584  int vertex1 = getVertexIndex1(a_triangleIndex);
585  int vertex2 = getVertexIndex2(a_triangleIndex);
586 
587  // retrieve vertex positions
588  cVector3d pos0 = m_vertices->getLocalPos(vertex0);
589  cVector3d pos1 = m_vertices->getLocalPos(vertex1);
590  cVector3d pos2 = m_vertices->getLocalPos(vertex2);
591 
592  // compute normal vector
593  cVector3d normal, v01, v02;
594  pos1.subr(pos0, v01);
595  pos2.subr(pos0, v02);
596  v01.crossr(v02, normal);
597  double length = normal.length();
598  if (length > 0.0)
599  {
600  normal.div(length);
601  if (a_applyNormalToVertices)
602  {
603  m_vertices->setNormal(vertex0, normal);
604  m_vertices->setNormal(vertex1, normal);
605  m_vertices->setNormal(vertex2, normal);
606  m_vertices->m_flagNormalData = true;
607  }
608  return (normal);
609  }
610 
611  normal.zero();
612  return (normal);
613  }
614 };
615 
616 //------------------------------------------------------------------------------
617 } // namespace chai3d
618 //------------------------------------------------------------------------------
619 
620 //------------------------------------------------------------------------------
621 #endif
622 //------------------------------------------------------------------------------
623 
624 
This class implements a 3D vector.
Definition: CVector3d.h:88
unsigned int getVertexIndex0(const unsigned int a_triangleIndex) const
Definition: CTriangleArray.h:281
virtual unsigned int getVertexIndex(const unsigned int a_elementIndex, const unsigned int a_vertexNumber) const
Definition: CTriangleArray.h:329
unsigned int getVertexIndex1(const unsigned int a_triangleIndex) const
Definition: CTriangleArray.h:296
void zero()
This method clears all vector components with zeros.
Definition: CVector3d.h:256
unsigned int getVertexIndex2(const unsigned int a_triangleIndex) const
Definition: CTriangleArray.h:311
void removeTriangle(const unsigned int a_triangleIndex)
Definition: CTriangleArray.h:227
Implements an abstract class for describing elements composed of vertices.
void div(const double &a_scalar)
This method computes the division of this vector with a scalar.
Definition: CVector3d.h:848
void crossr(const cVector3d &a_vector, cVector3d &a_result) const
This method computes the cross product.
Definition: CVector3d.h:975
cVector3d cAdd(const cVector3d &a_vector1, const cVector3d &a_vector2)
This function computes the addition of two vectors.
Definition: CMaths.h:706
std::vector< unsigned int > m_indices
Element indices to vertices.
Definition: CGenericArray.h:230
virtual unsigned int getNumVerticesPerElement()
This method returns the number of vertices that compose a triangle.
Definition: CTriangleArray.h:162
std::vector< bool > m_allocated
Element allocation flags.
Definition: CGenericArray.h:233
~cTriangleArray()
Definition: CTriangleArray.h:137
This class implements an array of 3D triangles.
Definition: CTriangleArray.h:104
double length() const
This method computes the Euclidean norm of this vector.
Definition: CVector3d.h:1015
std::list< unsigned int > m_freeElements
List of free elements.
Definition: CGenericArray.h:259
cVector3d cSub(const cVector3d &a_vector1, const cVector3d &a_vector2)
This function computes the subtraction of two vectors.
Definition: CMaths.h:767
void setVertices(const unsigned int a_triangleIndex, const unsigned int a_vertexIndex0, const unsigned int a_vertexIndex1, const unsigned int a_vertexIndex2)
Definition: CTriangleArray.h:258
double cTriangleArea(const cVector3d &a_vertex0, const cVector3d &a_vertex1, const cVector3d &a_vertex2)
This function computes the area of a triangle.
Definition: CGeometry.h:89
void set(const double &a_x, const double &a_y, const double &a_z)
This method initializes this vector with components x, y, and z passed as arguments.
Definition: CVector3d.h:298
void renderFinalize()
Definition: CTriangleArray.h:533
This class implements a base class for all 2D or 3D objects in CHAI3D.
Definition: CGenericObject.h:112
This class implements a collision detection recorder that stores all collision events that are report...
Definition: CCollisionBasics.h:185
cTriangleArrayPtr copy()
This method copies all triangle data and returns a new triangle array.
Definition: CTriangleArray.cpp:473
cVector3d cMul(const double &a_value, const cVector3d &a_vector)
This function computes the multiplication of a vector by a scalar.
Definition: CMaths.h:822
double computeArea(const unsigned int a_triangleIndex)
Definition: CTriangleArray.h:558
void renderInitialize()
Definition: CTriangleArray.h:487
bool m_flagMarkForUpdate
If true then element data has been modified.
Definition: CGenericArray.h:236
This structure stores the collision settings that are passed to a collision detector when querying fo...
Definition: CCollisionBasics.h:242
cVector3d getTexCoordAtPosition(const unsigned int a_triangleIndex, const cVector3d &a_localPos)
Definition: CTriangleArray.h:353
virtual unsigned int getNumElements()
This method returns the number of allocated elements.
Definition: CGenericArray.h:145
GLuint m_elementBuffer
OpenGL Buffer for storing elements.
Definition: CGenericArray.h:249
virtual bool computeCollision(const unsigned int a_elementIndex, cGenericObject *a_object, cVector3d &a_segmentPointA, cVector3d &a_segmentPointB, cCollisionRecorder &a_recorder, cCollisionSettings &a_settings) const
This method checks if the given line segment intersects a selected triangle from this array...
Definition: CTriangleArray.cpp:73
static cTriangleArrayPtr create(cVertexArrayPtr a_vertexArray)
Shared cTriangleArray allocator.
Definition: CTriangleArray.h:152
const double C_TINY
Smallest value near zero for a double.
Definition: CConstants.h:100
cVector3d cProjectPointOnPlane(const cVector3d &a_point, const cVector3d &a_planePoint, const cVector3d &a_planeNormal)
This function computes the projection of a point onto a plane.
Definition: CGeometry.h:117
cVector3d cNormalize(const cVector3d &a_vector)
This function computes the normalized vector.
Definition: CMaths.h:928
void computeBTN()
Definition: CTriangleArray.h:415
std::shared_ptr< cVertexArray > cVertexArrayPtr
Definition: CVertexArray.h:107
void subr(const cVector3d &a_vector, cVector3d &a_result) const
This method computes the subtraction of this vector with another.
Definition: CVector3d.h:622
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
int newTriangle(const unsigned int a_vertexIndex0, const unsigned int a_vertexIndex1, const unsigned int a_vertexIndex2)
Definition: CTriangleArray.h:182
cTriangleArray(cVertexArrayPtr a_vertexArray)
Definition: CTriangleArray.h:119
void compress()
This method removes all non allocated triangles.
Definition: CTriangleArray.cpp:511
void clear()
Clear all triangles from array.
Definition: CTriangleArray.h:141
cVector3d cCross(const cVector3d &a_vector1, const cVector3d &a_vector2)
This function computes the cross product between two vectors.
Definition: CMaths.h:879
Definition: CAudioBuffer.cpp:56
This class implements an abstract class for describing geometrical elements composed of vertices...
Definition: CGenericArray.h:90
std::shared_ptr< cTriangleArray > cTriangleArrayPtr
Definition: CTriangleArray.h:65
cVector3d computeNormal(const unsigned int a_triangleIndex, const bool a_applyNormalToVertices)
Definition: CTriangleArray.h:579
bool m_flagMarkForResize
If true then element array size has changed.
Definition: CGenericArray.h:239
double z() const
This method returns vector component z.
Definition: CVector3d.h:232
cVertexArrayPtr m_vertices
Vertex array that contains all vertices used to describe the elements of this array.
Definition: CGenericArray.h:227