TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MeshAlg.h
Go to the documentation of this file.
1 
12 #ifndef G3D_MeshAlg_h
13 #define G3D_MeshAlg_h
14 
15 #include "G3D/platform.h"
16 #include "G3D/Array.h"
17 #include "G3D/Vector3.h"
18 #include "G3D/CoordinateFrame.h"
19 #include "G3D/SmallArray.h"
20 #include "G3D/constants.h"
21 #include "G3D/Image1.h"
22 
23 #ifdef G3D_WINDOWS
24 // Turn off "conditional expression is constant" warning; MSVC generates this
25 // for debug assertions in inlined methods.
26 #pragma warning( push )
27 #pragma warning (disable : 4127)
28 #endif
29 
30 namespace G3D {
31 
44 class MeshAlg {
45 public:
46 
49 
57  class Vertex {
58  public:
59  Vertex() {}
60 
71 
75  inline bool inEdge(int e) const {
76  return edgeIndex.contains(~e) || edgeIndex.contains(e);
77  }
78 
84 
85  inline bool inFace(int f) const {
86  debugAssert(f >= 0);
87  return faceIndex.contains(f);
88  }
89  };
90 
91 
95  class Face {
96  public:
97  Face();
98 
103  static const int NONE;
104 
105 
110  int vertexIndex[3];
111 
112  inline bool containsVertex(int v) const {
113  return contains(vertexIndex, 3, v);
114  }
115 
139  // Temporarily takes on the value Face::NONE during adjacency
140  // computation to indicate an edge that has not yet been assigned.
141  int edgeIndex[3];
142 
143  inline bool containsEdge(int e) const {
144  if (e < 0) {
145  e = ~e;
146  }
147  return contains(edgeIndex, 3, e) || contains(edgeIndex, 3, ~e);
148  }
149 
152  inline bool containsDirectedEdge(int e) const {
153  return contains(edgeIndex, 3, e);
154  }
155  };
156 
157 
159  class Edge {
160  public:
161  Edge();
162 
164  int vertexIndex[2];
165 
166  inline bool containsVertex(int v) const {
167  return contains(vertexIndex, 2, v);
168  }
169 
175  int faceIndex[2];
176 
179  inline bool inFace(int f) const {
180  return contains(faceIndex, 2, f);
181  }
182 
186  inline bool boundary() const {
187  return (faceIndex[0] == Face::NONE) ||
188  (faceIndex[1] == Face::NONE);
189  }
190 
194  inline Edge reverse() const {
195  Edge e;
196  e.vertexIndex[0] = vertexIndex[1];
197  e.vertexIndex[1] = vertexIndex[0];
198  e.faceIndex[0] = faceIndex[1];
199  e.faceIndex[1] = faceIndex[0];
200  return e;
201  }
202  };
203 
204 
210  class Geometry {
211  public:
214 
217 
221  Geometry& operator=(const Geometry& src);
222 
223  void clear() {
224  vertexArray.clear();
225  normalArray.clear();
226  }
227  };
228 
254  static void computeAdjacency(
255  const Array<Vector3>& vertexGeometry,
256  const Array<int>& indexArray,
257  Array<Face>& faceArray,
258  Array<Edge>& edgeArray,
259  Array<Vertex>& vertexArray);
260 
266  static void computeAdjacency(
267  const Array<Vector3>& vertexArray,
268  const Array<int>& indexArray,
269  Array<Face>& faceArray,
270  Array<Edge>& edgeArray,
271  Array< Array<int> >& facesAdjacentToVertex);
272 
288  static void computeAreaStatistics(
289  const Array<Vector3>& vertexArray,
290  const Array<int>& indexArray,
291  double& minEdgeLength,
292  double& meanEdgeLength,
293  double& medianEdgeLength,
294  double& maxEdgeLength,
295  double& minFaceArea,
296  double& meanFaceArea,
297  double& medianFaceArea,
298  double& maxFaceArea);
299 
300 private:
301 
303  static void weldBoundaryEdges(
304  Array<Face>& faceArray,
305  Array<Edge>& edgeArray,
306  Array<Vertex>& vertexArray);
307 
308 public:
309 
328  static void computeTangentSpaceBasis(
329  const Array<Vector3>& vertexArray,
330  const Array<Vector2>& texCoordArray,
331  const Array<Vector3>& vertexNormalArray,
332  const Array<Face>& faceArray,
334  Array<Vector3>& binormal);
335 
337  static void computeNormals(
338  const Array<Vector3>& vertexArray,
339  const Array<Face>& faceArray,
340  const Array< Array<int> >& adjacentFaceArray,
341  Array<Vector3>& vertexNormalArray,
342  Array<Vector3>& faceNormalArray);
343 
353  static void computeNormals(
354  const Array<Vector3>& vertexGeometry,
355  const Array<Face>& faceArray,
356  const Array<Vertex>& vertexArray,
357  Array<Vector3>& vertexNormalArray,
358  Array<Vector3>& faceNormalArray);
359 
363  static void computeNormals(
364  Geometry& geometry,
365  const Array<int>& indexArray);
366 
372  static void computeFaceNormals(
373  const Array<Vector3>& vertexArray,
374  const Array<Face>& faceArray,
375  Array<Vector3>& faceNormals,
376  bool normalize = true);
377 
384  static void identifyBackfaces(
385  const Array<Vector3>& vertexArray,
386  const Array<Face>& faceArray,
387  const Vector4& P,
388  Array<bool>& backface);
389 
392  static void identifyBackfaces(
393  const Array<Vector3>& vertexArray,
394  const Array<Face>& faceArray,
395  const Vector4& P,
396  Array<bool>& backface,
397  const Array<Vector3>& faceNormals);
398 
443  static void computeWeld(
444  const Array<Vector3>& oldVertexPositions,
445  Array<Vector3>& newVertexPositions,
446  Array<int>& toNew,
447  Array<int>& toOld,
448  float radius = fuzzyEpsilon32);
449 
478  static void weldAdjacency(
479  const Array<Vector3>& originalGeometry,
480  Array<Face>& faceArray,
481  Array<Edge>& edgeArray,
482  Array<Vertex>& vertexArray,
483  float radius = fuzzyEpsilon32);
484 
485 
490  static int countBoundaryEdges(const Array<Edge>& edgeArray);
491 
492 
510  static void createIndexArray(
511  int n,
512  Array<int>& array,
513  int start = 0,
514  int run = 1,
515  int skip = 0);
516 
523  static void computeBounds(const Array<Vector3>& vertex, class AABox& box, class Sphere& sphere);
524 
526  static void computeBounds(const Array<Vector3>& vertex, const Array<int>& index, class AABox& box, class Sphere& sphere);
527 
532  static void debugCheckConsistency(
533  const Array<Face>& faceArray,
534  const Array<Edge>& edgeArray,
535  const Array<Vertex>& vertexArray);
536 
549  static void generateGrid
550  (Array<Vector3>& vertex,
551  Array<Vector2>& texCoord,
552  Array<int>& index,
553  int wCells = 10,
554  int hCells = 10,
555  const Vector2& textureScale = Vector2(1,1),
556  bool spaceCentered = true,
557  bool twoSided = true,
558  const CoordinateFrame& xform = CoordinateFrame(),
559  const Image1::Ref& elevation = Image1::Ref());
560 
565  template<class IndexType>
566  static void toIndexedTriList(
567  const Array<IndexType>& inIndices,
568  MeshAlg::Primitive inType,
569  Array<IndexType>& outIndices) {
570 
571  debugAssert(
572  inType == PrimitiveType::TRIANGLE_STRIP ||
573  inType == PrimitiveType::TRIANGLE_FAN ||
574  inType == PrimitiveType::QUADS ||
575  inType == PrimitiveType::QUAD_STRIP);
576 
577  const int inSize = inIndices.size();
578 
579  switch(inType) {
581  {
582  debugAssert(inSize >= 3);
583 
584  int N = outIndices.size();
585  outIndices.resize(N + (inSize - 2) * 3);
586 
587  for (IndexType i = 1, outIndex = N; i <= (inSize - 2); ++i, outIndex += 3) {
588  outIndices[outIndex] = inIndices[0];
589  outIndices[outIndex + 1] = inIndices[i];
590  outIndices[outIndex + 2] = inIndices[i + 1];
591  }
592 
593  break;
594  }
595 
597  {
598  debugAssert(inSize >= 3);
599 
600  int N = outIndices.size();
601  outIndices.resize(N + (inSize - 2) * 3);
602 
603  bool atEven = false;
604  for (IndexType i = 0, outIndex = N; i < (inSize - 2); ++i, outIndex += 3) {
605  if (atEven) {
606  outIndices[outIndex] = inIndices[i + 1];
607  outIndices[outIndex + 1] = inIndices[i];
608  outIndices[outIndex + 2] = inIndices[i + 2];
609  atEven = false;
610  } else {
611  outIndices[outIndex] = inIndices[i];
612  outIndices[outIndex + 1] = inIndices[i + 1];
613  outIndices[outIndex + 2] = inIndices[i + 2];
614  atEven = true;
615  }
616  }
617 
618  break;
619  }
620 
622  {
623  debugAssert(inIndices.size() >= 4);
624 
625  int N = outIndices.size();
626  outIndices.resize(N + (inSize / 4) * 3);
627 
628  for (IndexType i = 0, outIndex = N; i <= (inSize - 4); i += 4, outIndex += 6) {
629  outIndices[outIndex] = inIndices[i];
630  outIndices[outIndex + 1] = inIndices[i + 1];
631  outIndices[outIndex + 2] = inIndices[i + 3];
632  outIndices[outIndex + 3] = inIndices[i + 1];
633  outIndices[outIndex + 4] = inIndices[i + 2];
634  outIndices[outIndex + 5] = inIndices[i + 3];
635  }
636 
637  break;
638  }
639 
641  {
642  debugAssert(inIndices.size() >= 4);
643 
644  int N = outIndices.size();
645  outIndices.resize(N + (inSize - 2) * 3);
646 
647  for (IndexType i = 0, outIndex = N; i <= (inSize - 2); i += 2, outIndex += 6) {
648  outIndices[outIndex] = inIndices[i];
649  outIndices[outIndex + 1] = inIndices[i + 1];
650  outIndices[outIndex + 2] = inIndices[i + 2];
651  outIndices[outIndex + 3] = inIndices[i + 2];
652  outIndices[outIndex + 4] = inIndices[i + 1];
653  outIndices[outIndex + 5] = inIndices[i + 3];
654  }
655  break;
656  }
657  default:
658  alwaysAssertM(false, "Illegal argument");
659  }
660  }
661 
662 protected:
663 
677  static int findEdgeIndex(
678  const Array<Vector3>& vertexArray,
679  Array<Edge>& geometricEdgeArray,
680  int i0, int i1, int f, double area);
681 };
682 }
683 
684 
685 #ifdef G3D_WINDOWS
686 #pragma warning( pop )
687 #endif
688 
689 #endif
690 
PrimitiveType Primitive
Definition: MeshAlg.h:48
Face()
Definition: MeshAlg.cpp:100
Definition: Vector2.h:40
static void toIndexedTriList(const Array< IndexType > &inIndices, MeshAlg::Primitive inType, Array< IndexType > &outIndices)
Definition: MeshAlg.h:566
static void computeAdjacency(const Array< Vector3 > &vertexGeometry, const Array< int > &indexArray, Array< Face > &faceArray, Array< Edge > &edgeArray, Array< Vertex > &vertexArray)
Definition: MeshAlgAdjacency.cpp:193
Definition: constants.h:28
bool inFace(int f) const
Definition: MeshAlg.h:179
void resize(size_t n, bool shrinkIfNecessary=true)
Definition: Array.h:490
int vertexIndex[2]
Definition: MeshAlg.h:164
bool inEdge(int e) const
Definition: MeshAlg.h:75
Definition: constants.h:26
A rigid body RT (rotation-translation) transformation.
Definition: CoordinateFrame.h:59
static void generateGrid(Array< Vector3 > &vertex, Array< Vector2 > &texCoord, Array< int > &index, int wCells=10, int hCells=10, const Vector2 &textureScale=Vector2(1, 1), bool spaceCentered=true, bool twoSided=true, const CoordinateFrame &xform=CoordinateFrame(), const Image1::Ref &elevation=Image1::Ref())
Definition: MeshAlg.cpp:28
Definition: constants.h:29
bool contains(const T &value) const
Definition: SmallArray.h:162
static void computeWeld(const Array< Vector3 > &oldVertexPositions, Array< Vector3 > &newVertexPositions, Array< int > &toNew, Array< int > &toOld, float radius=fuzzyEpsilon32)
Definition: MeshAlgWeld.cpp:203
Vertex()
Definition: MeshAlg.h:59
shared_ptr< class Image1 > Ref
Definition: Image1.h:31
bool containsVertex(int v) const
Definition: MeshAlg.h:166
bool boundary() const
Definition: MeshAlg.h:186
Definition: AABox.h:25
static void weldBoundaryEdges(Array< Face > &faceArray, Array< Edge > &edgeArray, Array< Vertex > &vertexArray)
Definition: MeshAlgAdjacency.cpp:430
Dynamic 1D array tuned for performance.
Definition: Array.h:95
Definition: MeshAlg.h:210
int vertexIndex[3]
Definition: MeshAlg.h:110
static void computeNormals(const Array< Vector3 > &vertexArray, const Array< Face > &faceArray, const Array< Array< int > > &adjacentFaceArray, Array< Vector3 > &vertexNormalArray, Array< Vector3 > &faceNormalArray)
Definition: MeshAlg.cpp:144
static const int NONE
Definition: MeshAlg.h:103
SmallArray< int, 6 > faceIndex
Definition: MeshAlg.h:83
int faceIndex[2]
Definition: MeshAlg.h:175
bool contains(const T *array, int len, const T &e)
Definition: Array.h:1451
Definition: constants.h:19
static void computeBounds(const Array< Vector3 > &vertex, class AABox &box, class Sphere &sphere)
Definition: MeshAlg.cpp:430
float normalize(float v)
Definition: g3dmath.h:438
Definition: Sphere.h:24
static void identifyBackfaces(const Array< Vector3 > &vertexArray, const Array< Face > &faceArray, const Vector4 &P, Array< bool > &backface)
Definition: MeshAlg.cpp:246
#define debugAssert(exp)
Definition: debugAssert.h:160
static void computeAreaStatistics(const Array< Vector3 > &vertexArray, const Array< int > &indexArray, double &minEdgeLength, double &meanEdgeLength, double &medianEdgeLength, double &maxEdgeLength, double &minFaceArea, double &meanFaceArea, double &medianFaceArea, double &maxFaceArea)
Definition: MeshAlg.cpp:343
void clear()
Definition: MeshAlg.h:223
static void computeFaceNormals(const Array< Vector3 > &vertexArray, const Array< Face > &faceArray, Array< Vector3 > &faceNormals, bool normalize=true)
Definition: MeshAlg.cpp:220
Definition: Vector4.h:39
Definition: MeshAlg.h:95
void clear(bool shrink=true)
Definition: Array.h:407
Definition: constants.h:27
bool containsEdge(int e) const
Definition: MeshAlg.h:143
static int countBoundaryEdges(const Array< Edge > &edgeArray)
Definition: MeshAlg.cpp:401
int size() const
Definition: Array.h:430
#define fuzzyEpsilon32
Definition: g3dmath.h:132
Definition: AABox.h:32
Definition: MeshAlg.h:159
int edgeIndex[3]
Definition: MeshAlg.h:141
Definition: MeshAlg.h:44
Array< Vector3 > normalArray
Definition: MeshAlg.h:216
Edge reverse() const
Definition: MeshAlg.h:194
bool inFace(int f) const
Definition: MeshAlg.h:85
static int findEdgeIndex(const Array< Vector3 > &vertexArray, Array< Edge > &geometricEdgeArray, int i0, int i1, int f, double area)
bool containsVertex(int v) const
Definition: MeshAlg.h:112
Edge()
Definition: MeshAlg.cpp:108
static void computeTangentSpaceBasis(const Array< Vector3 > &vertexArray, const Array< Vector2 > &texCoordArray, const Array< Vector3 > &vertexNormalArray, const Array< Face > &faceArray, Array< Vector3 > &tangent, Array< Vector3 > &binormal)
Definition: MeshAlg.cpp:552
Geometry & operator=(const Geometry &src)
Definition: MeshAlg.cpp:117
bool containsDirectedEdge(int e) const
Definition: MeshAlg.h:152
static void weldAdjacency(const Array< Vector3 > &originalGeometry, Array< Face > &faceArray, Array< Edge > &edgeArray, Array< Vertex > &vertexArray, float radius=fuzzyEpsilon32)
Definition: MeshAlgAdjacency.cpp:623
SmallArray< int, 6 > edgeIndex
Definition: MeshAlg.h:70
float tangent(float x)
Definition: Spell.cpp:1502
uint8 const P[]
Definition: AuthenticationPackets.cpp:225
#define alwaysAssertM(exp, message)
Definition: debugAssert.h:165
Definition: MeshAlg.h:57
static void createIndexArray(int n, Array< int > &array, int start=0, int run=1, int skip=0)
Definition: MeshAlg.cpp:316
static void debugCheckConsistency(const Array< Face > &faceArray, const Array< Edge > &edgeArray, const Array< Vertex > &vertexArray)
Definition: MeshAlgAdjacency.cpp:693
Array< Vector3 > vertexArray
Definition: MeshAlg.h:213