00001 // Wild Magic Source Code 00002 // David Eberly 00003 // http://www.geometrictools.com 00004 // Copyright (c) 1998-2007 00005 // 00006 // This library is free software; you can redistribute it and/or modify it 00007 // under the terms of the GNU Lesser General Public License as published by 00008 // the Free Software Foundation; either version 2.1 of the License, or (at 00009 // your option) any later version. The license is available for reading at 00010 // either of the locations: 00011 // http://www.gnu.org/copyleft/lgpl.html 00012 // http://www.geometrictools.com/License/WildMagicLicense.pdf 00013 // The license applies to versions 0 through 4 of Wild Magic. 00014 // 00015 // Version: 4.0.0 (2006/06/28) 00016 00017 #ifndef WM4DELAUNAY2_H 00018 #define WM4DELAUNAY2_H 00019 00020 #include "Wm4FoundationLIB.h" 00021 #include "Wm4Delaunay1.h" 00022 #include "Wm4DelTriangle.h" 00023 #include "Wm4Query2.h" 00024 00025 namespace Wm4 00026 { 00027 00028 template <class Real> 00029 class WM4_FOUNDATION_ITEM Delaunay2 : public Delaunay<Real> 00030 { 00031 public: 00032 // The input to the constructor is the array of vertices whose Delaunay 00033 // triangulation is required. If you want Delaunay2 to delete the 00034 // vertices during destruction, set bOwner to 'true'. Otherwise, you 00035 // own the vertices and must delete them yourself. 00036 // 00037 // You have a choice of speed versus accuracy. The fastest choice is 00038 // Query::QT_INT64, but it gives up a lot of precision, scaling the points 00039 // to [0,2^{16}]^3. The choice Query::QT_INTEGER gives up less precision, 00040 // scaling the points to [0,2^{20}]^3. The choice Query::QT_RATIONAL uses 00041 // exact arithmetic, but is the slowest choice. The choice Query::QT_REAL 00042 // uses floating-point arithmetic, but is not robust in all cases. 00043 00044 Delaunay2 (int iVertexQuantity, Vector2<Real>* akVertex, Real fEpsilon, 00045 bool bOwner, Query::Type eQueryType); 00046 virtual ~Delaunay2 (); 00047 00048 // The input vertex array. 00049 const Vector2<Real>* GetVertices () const; 00050 00051 // The number of unique vertices processed. 00052 int GetUniqueVertexQuantity () const; 00053 00054 // If GetDimension() returns 1, then the points lie on a line. You must 00055 // create a Delaunay1 object using the function provided. 00056 const Vector2<Real>& GetLineOrigin () const; 00057 const Vector2<Real>& GetLineDirection () const; 00058 Delaunay1<Real>* GetDelaunay1 () const; 00059 00060 // Locate those triangle edges that do not share other triangles. The 00061 // returned quantity is the number of edges in the hull. The returned 00062 // array has 2*quantity indices, each pair representing an edge. The 00063 // edges are not ordered, but the pair of vertices for an edge is ordered 00064 // so that they conform to a counterclockwise traversal of the hull. The 00065 // return value is 'true' iff the dimension is 2. 00066 bool GetHull (int& riEQuantity, int*& raiIndex); 00067 00068 // Support for searching the triangulation for a triangle that contains 00069 // a point. If there is a containing triangle, the returned value is a 00070 // triangle index i with 0 <= i < riTQuantity. If there is not a 00071 // containing triangle, -1 is returned. 00072 int GetContainingTriangle (const Vector2<Real>& rkP) const; 00073 00074 // If GetContainingTriangle returns a nonnegative value, the path of 00075 // triangles searched for the containing triangles is stored in an array. 00076 // The last index of the array is returned by GetPathLast; it is one 00077 // less than the number of array elements. The array itself is returned 00078 // by GetPath. 00079 int GetPathLast () const; 00080 const int* GetPath () const; 00081 00082 // If GetContainingTriangle returns -1, the path of triangles searched 00083 // may be obtained by GetPathLast and GetPath. The input point is outside 00084 // an edge of the last triangle in the path. This function returns the 00085 // vertex indices <v0,v1> of the edge, listed in counterclockwise order 00086 // relative to the convex hull of the data points. The final output is 00087 // the index of the vertex v2 opposite the edge. The return value of 00088 // the function is the index of the triple of vertex indices; the value 00089 // is 0, 1, or 2. 00090 int GetLastEdge (int& riV0, int& riV1, int& riV2) const; 00091 00092 // Get the vertices for triangle i. The function returns 'true' if i is 00093 // a valid triangle index, in which case the vertices are valid. 00094 // Otherwise, the function returns 'false' and the vertices are invalid. 00095 bool GetVertexSet (int i, Vector2<Real> akV[3]) const; 00096 00097 // Get the vertex indices for triangle i. The function returns 'true' if 00098 // i is a valid triangle index, in which case the vertices are valid. 00099 // Otherwise, the function returns 'false' and the vertices are invalid. 00100 bool GetIndexSet (int i, int aiIndex[3]) const; 00101 00102 // Get the indices for triangles adjacent to triangle i. The function 00103 // returns 'true' if i is a valid triangle index, in which case the 00104 // adjacencies are valid. Otherwise, the function returns 'false' and 00105 // the adjacencies are invalid. 00106 bool GetAdjacentSet (int i, int aiAdjacent[3]) const; 00107 00108 // Compute the barycentric coordinates of P with respect to triangle i. 00109 // The function returns 'true' if i is a valid triangle index, in which 00110 // case the coordinates are valid. Otherwise, the function returns 00111 // 'false' and the coordinate array is invalid. 00112 bool GetBarycentricSet (int i, const Vector2<Real>& rkP, Real afBary[3]) 00113 const; 00114 00115 // Support for streaming to/from disk. 00116 Delaunay2 (const char* acFilename); 00117 bool Load (const char* acFilename); 00118 bool Save (const char* acFilename) const; 00119 00120 private: 00121 using Delaunay<Real>::m_eQueryType; 00122 using Delaunay<Real>::m_iVertexQuantity; 00123 using Delaunay<Real>::m_iDimension; 00124 using Delaunay<Real>::m_iSimplexQuantity; 00125 using Delaunay<Real>::m_aiIndex; 00126 using Delaunay<Real>::m_aiAdjacent; 00127 using Delaunay<Real>::m_fEpsilon; 00128 using Delaunay<Real>::m_bOwner; 00129 00130 void Update (int i); 00131 DelTriangle<Real>* GetContainingTriangle (int i) const; 00132 void RemoveTriangles (); 00133 bool IsSupervertex (int i) const; 00134 00135 // The input vertices. 00136 Vector2<Real>* m_akVertex; 00137 00138 // The number of unique vertices processed. 00139 int m_iUniqueVertexQuantity; 00140 00141 // The scaled input vertices with additional storage for the three 00142 // supertriangle vertices. This array and supporting data structures 00143 // are for robust calculations. 00144 Vector2<Real>* m_akSVertex; 00145 Query2<Real>* m_pkQuery; 00146 Vector2<Real> m_kMin; 00147 Real m_fScale; 00148 00149 // The indices for the three supertriangle vertices. 00150 int m_aiSV[3]; 00151 00152 // The current triangulation. 00153 std::set<DelTriangle<Real>*> m_kTriangle; 00154 00155 // The line of containment if the dimension is 1. 00156 Vector2<Real> m_kLineOrigin, m_kLineDirection; 00157 00158 // Store the path of tetrahedra visited in a GetContainingTetrahedron 00159 // function call. 00160 mutable int m_iPathLast; 00161 mutable int* m_aiPath; 00162 00163 // If a query point is not in the convex hull of the input points, the 00164 // point is outside an edge of the last triangle in the search path. 00165 // These are the vertex indices for that edge. 00166 mutable int m_iLastEdgeV0, m_iLastEdgeV1; 00167 mutable int m_iLastEdgeOpposite, m_iLastEdgeOppositeIndex; 00168 }; 00169 00170 typedef Delaunay2<float> Delaunay2f; 00171 typedef Delaunay2<double> Delaunay2d; 00172 00173 } 00174 00175 #endif