Wm4MeshSmoother.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 "Wm4MeshSmoother.h"
00019 
00020 namespace Wm4
00021 {
00022 //----------------------------------------------------------------------------
00023 template <class Real>
00024 MeshSmoother<Real>::MeshSmoother ()
00025 {
00026     m_iVQuantity = 0;
00027     m_akVertex = 0;
00028     m_iTQuantity = 0;
00029     m_aiIndex = 0;
00030     m_akNormal = 0;
00031     m_akMean = 0;
00032     m_aiNeighborCount = 0;
00033 }
00034 //----------------------------------------------------------------------------
00035 template <class Real>
00036 MeshSmoother<Real>::MeshSmoother (int iVQuantity, Vector3<Real>* akVertex,
00037     int iTQuantity, const int* aiIndex)
00038 {
00039     m_akVertex = 0;
00040     m_akNormal = 0;
00041     m_aiIndex = 0;
00042     m_akMean = 0;
00043     m_aiNeighborCount = 0;
00044 
00045     Create(iVQuantity,akVertex,iTQuantity,aiIndex);
00046 }
00047 //----------------------------------------------------------------------------
00048 template <class Real>
00049 MeshSmoother<Real>::~MeshSmoother ()
00050 {
00051     Destroy();
00052 }
00053 //----------------------------------------------------------------------------
00054 template <class Real>
00055 void MeshSmoother<Real>::Create (int iVQuantity, Vector3<Real>* akVertex,
00056     int iTQuantity, const int* aiIndex)
00057 {
00058     // remove previously allocated smoother data
00059     Destroy();
00060 
00061     m_iVQuantity = iVQuantity;
00062     m_akVertex = akVertex;
00063     m_iTQuantity = iTQuantity;
00064     m_aiIndex = aiIndex;
00065 
00066     m_akNormal = WM4_NEW Vector3<Real>[m_iVQuantity];
00067     m_akMean = WM4_NEW Vector3<Real>[m_iVQuantity];
00068     m_aiNeighborCount = WM4_NEW int[m_iVQuantity];
00069 
00070     // count the number of vertex neighbors
00071     memset(m_aiNeighborCount,0,m_iVQuantity*sizeof(int));
00072     const int* piIndex = m_aiIndex;
00073     for (int i = 0; i < m_iTQuantity; i++)
00074     {
00075         m_aiNeighborCount[*piIndex++] += 2;
00076         m_aiNeighborCount[*piIndex++] += 2;
00077         m_aiNeighborCount[*piIndex++] += 2;
00078     }
00079 }
00080 //----------------------------------------------------------------------------
00081 template <class Real>
00082 void MeshSmoother<Real>::Destroy ()
00083 {
00084     WM4_DELETE[] m_akNormal;
00085     WM4_DELETE[] m_akMean;
00086     WM4_DELETE[] m_aiNeighborCount;
00087 }
00088 //----------------------------------------------------------------------------
00089 template <class Real>
00090 void MeshSmoother<Real>::Update (Real fTime)
00091 {
00092     memset(m_akNormal,0,m_iVQuantity*sizeof(Vector3<Real>));
00093     memset(m_akMean,0,m_iVQuantity*sizeof(Vector3<Real>));
00094 
00095     const int* piIndex = m_aiIndex;
00096     int i;
00097     for (i = 0; i < m_iTQuantity; i++)
00098     {
00099         int iV0 = *piIndex++;
00100         int iV1 = *piIndex++;
00101         int iV2 = *piIndex++;
00102 
00103         Vector3<Real>& rkV0 = m_akVertex[iV0];
00104         Vector3<Real>& rkV1 = m_akVertex[iV1];
00105         Vector3<Real>& rkV2 = m_akVertex[iV2];
00106 
00107         Vector3<Real> kEdge1 = rkV1 - rkV0;
00108         Vector3<Real> kEdge2 = rkV2 - rkV0;
00109         Vector3<Real> kNormal = kEdge1.Cross(kEdge2);
00110 
00111         m_akNormal[iV0] += kNormal;
00112         m_akNormal[iV1] += kNormal;
00113         m_akNormal[iV2] += kNormal;
00114 
00115         m_akMean[iV0] += rkV1 + rkV2;
00116         m_akMean[iV1] += rkV2 + rkV0;
00117         m_akMean[iV2] += rkV0 + rkV1;
00118     }
00119 
00120     for (i = 0; i < m_iVQuantity; i++)
00121     {
00122         m_akNormal[i].Normalize();
00123         m_akMean[i] /= (Real)m_aiNeighborCount[i];
00124     }
00125 
00126     for (i = 0; i < m_iVQuantity; i++)
00127     {
00128         if (VertexInfluenced(i,fTime))
00129         {
00130             Vector3<Real> kLocalDiff = m_akMean[i] - m_akVertex[i];
00131             Vector3<Real> kSurfaceNormal = kLocalDiff.Dot(m_akNormal[i]) *
00132                 m_akNormal[i];
00133             Vector3<Real> kTangent = kLocalDiff - kSurfaceNormal;
00134 
00135             Real fTWeight = GetTangentWeight(i,fTime);
00136             Real fNWeight = GetNormalWeight(i,fTime);
00137             m_akVertex[i] += fTWeight*kTangent + fNWeight*m_akNormal[i];
00138         }
00139     }
00140 }
00141 //----------------------------------------------------------------------------
00142 template <class Real>
00143 bool MeshSmoother<Real>::VertexInfluenced (int, Real)
00144 {
00145     return true;
00146 }
00147 //----------------------------------------------------------------------------
00148 template <class Real>
00149 Real MeshSmoother<Real>::GetTangentWeight (int, Real)
00150 {
00151     return (Real)0.5;
00152 }
00153 //----------------------------------------------------------------------------
00154 template <class Real>
00155 Real MeshSmoother<Real>::GetNormalWeight (int, Real)
00156 {
00157     return (Real)0.0;
00158 }
00159 //----------------------------------------------------------------------------
00160 template <class Real>
00161 int MeshSmoother<Real>::GetVQuantity () const
00162 {
00163     return m_iVQuantity;
00164 }
00165 //----------------------------------------------------------------------------
00166 template <class Real>
00167 const Vector3<Real>* MeshSmoother<Real>::GetVertices () const
00168 {
00169     return m_akVertex;
00170 }
00171 //----------------------------------------------------------------------------
00172 template <class Real>
00173 int MeshSmoother<Real>::GetTQuantity () const
00174 {
00175     return m_iTQuantity;
00176 }
00177 //----------------------------------------------------------------------------
00178 template <class Real>
00179 const int* MeshSmoother<Real>::GetIndices () const
00180 {
00181     return m_aiIndex;
00182 }
00183 //----------------------------------------------------------------------------
00184 template <class Real>
00185 const Vector3<Real>* MeshSmoother<Real>::GetNormals () const
00186 {
00187     return m_akNormal;
00188 }
00189 //----------------------------------------------------------------------------
00190 template <class Real>
00191 const Vector3<Real>* MeshSmoother<Real>::GetMeans () const
00192 {
00193     return m_akMean;
00194 }
00195 //----------------------------------------------------------------------------
00196 
00197 //----------------------------------------------------------------------------
00198 // explicit instantiation
00199 //----------------------------------------------------------------------------
00200 template WM4_FOUNDATION_ITEM
00201 class MeshSmoother<float>;
00202 
00203 template WM4_FOUNDATION_ITEM
00204 class MeshSmoother<double>;
00205 //----------------------------------------------------------------------------
00206 }

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