Wm4UniqueVerticesTriangles.inl

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 namespace Wm4
00018 {
00019 //----------------------------------------------------------------------------
00020 template <int N, class Real>
00021 UniqueVerticesTriangles<N,Real>::UniqueVerticesTriangles (int iTQuantity,
00022     const TTuple<N,Real>* akInVertex, int& riOutVQuantity,
00023     TTuple<N,Real>*& rakOutVertex, int*& raiOutIndex)
00024 {
00025     assert(iTQuantity > 0 && akInVertex);
00026 
00027     ConstructUniqueVertices(3*iTQuantity,akInVertex,riOutVQuantity,
00028         rakOutVertex);
00029 
00030     // The input index array is implicitly {<0,1,2>,<3,4,5>,...,<n-3,n-2,n-1>}
00031     // where n is the number of vertices.  The output index array is the same
00032     // as the mapping array.
00033     int iIQuantity = 3*iTQuantity;
00034     raiOutIndex = WM4_NEW int[iIQuantity];
00035     size_t uiSize = iIQuantity*sizeof(int);
00036     System::Memcpy(raiOutIndex,uiSize,m_aiInToOutMapping,uiSize);
00037 }
00038 //----------------------------------------------------------------------------
00039 template <int N, class Real>
00040 UniqueVerticesTriangles<N,Real>::UniqueVerticesTriangles (int iInVQuantity,
00041     const TTuple<N,Real>* akInVertex, int iTQuantity, const int* aiInIndex,
00042     int& riOutVQuantity, TTuple<N,Real>*& rakOutVertex, int*& raiOutIndex)
00043 {
00044     assert(iInVQuantity > 0 && akInVertex && iTQuantity > 0 && aiInIndex);
00045 
00046     ConstructUniqueVertices(iInVQuantity,akInVertex,riOutVQuantity,
00047         rakOutVertex);
00048 
00049     // The input index array needs it indices mapped to the unique vertex
00050     // indices.
00051     int iIQuantity = 3*iTQuantity;
00052     raiOutIndex = WM4_NEW int[iIQuantity];
00053     for (int i = 0; i < iIQuantity; i++)
00054     {
00055         assert(0 <= aiInIndex[i] && aiInIndex[i] < iInVQuantity);
00056         raiOutIndex[i] = m_aiInToOutMapping[aiInIndex[i]];
00057         assert(0 <= raiOutIndex[i] && raiOutIndex[i] < riOutVQuantity);
00058     }
00059 }
00060 //----------------------------------------------------------------------------
00061 template <int N, class Real>
00062 UniqueVerticesTriangles<N,Real>::~UniqueVerticesTriangles ()
00063 {
00064     WM4_DELETE[] m_aiInToOutMapping;
00065 }
00066 //----------------------------------------------------------------------------
00067 template <int N, class Real>
00068 int UniqueVerticesTriangles<N,Real>::GetOutputIndexFor (int iInputIndex) const
00069 {
00070     assert(0 <= iInputIndex && iInputIndex < m_iInVQuantity);
00071     return m_aiInToOutMapping[iInputIndex];
00072 }
00073 //----------------------------------------------------------------------------
00074 template <int N, class Real>
00075 void UniqueVerticesTriangles<N,Real>::ConstructUniqueVertices (
00076     int iInVQuantity, const TTuple<N,Real>* akInVertex, int& riOutVQuantity,
00077     TTuple<N,Real>*& rakOutVertex)
00078 {
00079     // Construct the unique vertices.
00080     m_iInVQuantity = iInVQuantity;
00081     m_aiInToOutMapping = WM4_NEW int[m_iInVQuantity];
00082     std::map<TTuple<N,Real>,int> kTable;
00083     m_iOutVQuantity = 0;
00084     typename std::map<TTuple<N,Real>,int>::iterator pkIter;
00085     for (int i = 0; i < m_iInVQuantity; i++)
00086     {
00087         pkIter = kTable.find(akInVertex[i]);
00088         if (pkIter != kTable.end())
00089         {
00090             // Vertex i is a duplicate of one inserted earlier into the
00091             // table.  Map vertex i to the first-found copy.
00092             m_aiInToOutMapping[i] = pkIter->second;
00093         }
00094         else
00095         {
00096             // Vertex i is the first occurrence of such a point.
00097             kTable.insert(std::make_pair(akInVertex[i],m_iOutVQuantity));
00098             m_aiInToOutMapping[i] = m_iOutVQuantity;
00099             m_iOutVQuantity++;
00100         }
00101     }
00102 
00103     // Pack the unique vertices into an array in the correct order.
00104     riOutVQuantity = m_iOutVQuantity;
00105     rakOutVertex = WM4_NEW TTuple<N,Real>[m_iOutVQuantity];
00106     for (pkIter = kTable.begin(); pkIter != kTable.end(); pkIter++)
00107     {
00108         assert(0 <= pkIter->second && pkIter->second < m_iOutVQuantity);
00109         rakOutVertex[pkIter->second] = pkIter->first;
00110     }
00111 }
00112 //----------------------------------------------------------------------------
00113 } //namespace Wm4

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