Wm4Delaunay1.cpp

Go to the documentation of this file.
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 #include "Wm4FoundationPCH.h"
00018 #include "Wm4Delaunay1.h"
00019 
00020 namespace Wm4
00021 {
00022 //----------------------------------------------------------------------------
00023 template <class Real>
00024 Delaunay1<Real>::Delaunay1 (int iVertexQuantity, Real* afVertex,
00025     Real fEpsilon, bool bOwner, Query::Type eQueryType)
00026     :
00027     Delaunay<Real>(iVertexQuantity,fEpsilon,bOwner,eQueryType)
00028 {
00029     assert(afVertex);
00030     m_afVertex = afVertex;
00031 
00032     std::vector<SortedVertex> kArray(m_iVertexQuantity);
00033     int i;
00034     for (i = 0; i < m_iVertexQuantity; i++)
00035     {
00036         kArray[i].Value = m_afVertex[i];
00037         kArray[i].Index = i;
00038     }
00039     std::sort(kArray.begin(),kArray.end());
00040 
00041     Real fRange = kArray[m_iVertexQuantity-1].Value - kArray[0].Value;
00042     if (fRange >= m_fEpsilon)
00043     {
00044         m_iDimension = 1;
00045         m_iSimplexQuantity = m_iVertexQuantity - 1;
00046         m_aiIndex = WM4_NEW int[2*m_iSimplexQuantity];
00047         for (i = 0; i < m_iSimplexQuantity; i++)
00048         {
00049             m_aiIndex[2*i] = kArray[i].Index;
00050             m_aiIndex[2*i+1] = kArray[i+1].Index;
00051         }
00052 
00053         m_aiAdjacent = WM4_NEW int[2*m_iSimplexQuantity];
00054         for (i = 0; i < m_iSimplexQuantity; i++)
00055         {
00056             m_aiAdjacent[2*i] = i-1;
00057             m_aiAdjacent[2*i+1] = i+1;
00058         }
00059         m_aiAdjacent[2*m_iSimplexQuantity-1] = -1;
00060     }
00061 }
00062 //----------------------------------------------------------------------------
00063 template <class Real>
00064 Delaunay1<Real>::~Delaunay1 ()
00065 {
00066     if (m_bOwner)
00067     {
00068         WM4_DELETE[] m_afVertex;
00069     }
00070 }
00071 //----------------------------------------------------------------------------
00072 template <class Real>
00073 const Real* Delaunay1<Real>::GetVertices () const
00074 {
00075     return m_afVertex;
00076 }
00077 //----------------------------------------------------------------------------
00078 template <class Real>
00079 bool Delaunay1<Real>::GetHull (int aiIndex[2])
00080 {
00081     assert(m_iDimension == 1);
00082     if (m_iDimension != 1)
00083     {
00084         return false;
00085     }
00086 
00087     aiIndex[0] = m_aiIndex[0];
00088     aiIndex[1] = m_aiIndex[2*m_iSimplexQuantity-1];
00089     return true;
00090 }
00091 //----------------------------------------------------------------------------
00092 template <class Real>
00093 int Delaunay1<Real>::GetContainingSegment (const Real fP) const
00094 {
00095     assert(m_iDimension == 1);
00096     if (m_iDimension != 1)
00097     {
00098         return -1;
00099     }
00100 
00101     if (fP < m_afVertex[m_aiIndex[0]])
00102     {
00103         return -1;
00104     }
00105 
00106     if (fP > m_afVertex[m_aiIndex[2*m_iSimplexQuantity-1]])
00107     {
00108         return -1;
00109     }
00110 
00111     int i;
00112     for (i = 0; i < m_iSimplexQuantity; i++)
00113     {
00114         if (fP < m_afVertex[m_aiIndex[2*i+1]])
00115         {
00116             break;
00117         }
00118     }
00119 
00120     assert(i < m_iSimplexQuantity);
00121     return i;
00122 }
00123 //----------------------------------------------------------------------------
00124 template <class Real>
00125 bool Delaunay1<Real>::GetVertexSet (int i, Real afV[2]) const
00126 {
00127     assert(m_iDimension == 1);
00128     if (m_iDimension != 1)
00129     {
00130         return false;
00131     }
00132 
00133     if (0 <= i && i < m_iSimplexQuantity)
00134     {
00135         afV[0] = m_afVertex[m_aiIndex[2*i]];
00136         afV[1] = m_afVertex[m_aiIndex[2*i+1]];
00137         return true;
00138     }
00139     return false;
00140 }
00141 //----------------------------------------------------------------------------
00142 template <class Real>
00143 bool Delaunay1<Real>::GetIndexSet (int i, int aiIndex[2]) const
00144 {
00145     assert(m_iDimension == 1);
00146     if (m_iDimension != 1)
00147     {
00148         return false;
00149     }
00150 
00151     if (0 <= i && i < m_iSimplexQuantity)
00152     {
00153         aiIndex[0] = m_aiIndex[2*i];
00154         aiIndex[1] = m_aiIndex[2*i+1];
00155         return true;
00156     }
00157     return false;
00158 }
00159 //----------------------------------------------------------------------------
00160 template <class Real>
00161 bool Delaunay1<Real>::GetAdjacentSet (int i, int aiAdjacent[2]) const
00162 {
00163     assert(m_iDimension == 1);
00164     if (m_iDimension != 1)
00165     {
00166         return false;
00167     }
00168 
00169     if (0 <= i && i < m_iSimplexQuantity)
00170     {
00171         aiAdjacent[0] = m_aiAdjacent[2*i];
00172         aiAdjacent[1] = m_aiAdjacent[2*i+1];
00173         return true;
00174     }
00175     return false;
00176 }
00177 //----------------------------------------------------------------------------
00178 template <class Real>
00179 bool Delaunay1<Real>::GetBarycentricSet (int i, const Real fP, Real afBary[2])
00180     const
00181 {
00182     assert(m_iDimension == 1);
00183     if (m_iDimension != 1)
00184     {
00185         return false;
00186     }
00187 
00188     if (0 <= i && i < m_iSimplexQuantity)
00189     {
00190         Real fV0 = m_afVertex[m_aiIndex[2*i]];
00191         Real fV1 = m_afVertex[m_aiIndex[2*i+1]];
00192         Real fDenom = fV1 - fV0;
00193         if (fDenom > m_fEpsilon)
00194         {
00195             afBary[0] = (fV1 - fP)/fDenom;
00196         }
00197         else
00198         {
00199             afBary[0] = (Real)1.0;
00200         }
00201 
00202         afBary[1] = (Real)1.0 - afBary[0];
00203         return true;
00204     }
00205     return false;
00206 }
00207 //----------------------------------------------------------------------------
00208 template <class Real>
00209 Delaunay1<Real>::Delaunay1 (const char* acFilename)
00210     :
00211     Delaunay<Real>(0,(Real)0.0,false,Query::QT_REAL)
00212 {
00213     m_afVertex = 0;
00214     bool bLoaded = Load(acFilename);
00215     assert(bLoaded);
00216     (void)bLoaded;  // avoid warning in Release build
00217 }
00218 //----------------------------------------------------------------------------
00219 template <class Real>
00220 bool Delaunay1<Real>::Load (const char* acFilename)
00221 {
00222     FILE* pkIFile = System::Fopen(acFilename,"rb");
00223     if (!pkIFile)
00224     {
00225         return false;
00226     }
00227 
00228     Delaunay<Real>::Load(pkIFile);
00229 
00230     if (m_bOwner)
00231     {
00232         WM4_DELETE[] m_afVertex;
00233     }
00234 
00235     m_bOwner = true;
00236     m_afVertex = WM4_NEW Real[m_iVertexQuantity];
00237 
00238     size_t uiSize = sizeof(Real);
00239     if (uiSize == 4)
00240     {
00241         System::Read4le(pkIFile,m_iVertexQuantity,m_afVertex);
00242     }
00243     else // uiSize == 8
00244     {
00245         System::Read8le(pkIFile,m_iVertexQuantity,m_afVertex);
00246     }
00247 
00248     System::Fclose(pkIFile);
00249     return true;
00250 }
00251 //----------------------------------------------------------------------------
00252 template <class Real>
00253 bool Delaunay1<Real>::Save (const char* acFilename) const
00254 {
00255     FILE* pkOFile = System::Fopen(acFilename,"wb");
00256     if (!pkOFile)
00257     {
00258         return false;
00259     }
00260 
00261     Delaunay<Real>::Save(pkOFile);
00262 
00263     size_t uiSize = sizeof(Real);
00264     if (uiSize == 4)
00265     {
00266         System::Write4le(pkOFile,m_iVertexQuantity,m_afVertex);
00267     }
00268     else // uiSize == 8
00269     {
00270         System::Write8le(pkOFile,m_iVertexQuantity,m_afVertex);
00271     }
00272 
00273     System::Fclose(pkOFile);
00274     return true;
00275 }
00276 //----------------------------------------------------------------------------
00277 
00278 //----------------------------------------------------------------------------
00279 // explicit instantiation
00280 //----------------------------------------------------------------------------
00281 template WM4_FOUNDATION_ITEM
00282 class Delaunay1<float>;
00283 
00284 template WM4_FOUNDATION_ITEM
00285 class Delaunay1<double>;
00286 //----------------------------------------------------------------------------
00287 }

Generated on Wed Nov 23 19:01:03 2011 for FreeCAD by  doxygen 1.6.1