OpenMesh
OpenMesh/Tools/Dualizer/meshDualT.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 /*
00043   Compute the dual of a mesh:
00044   - each face of the original mesh is replaced by a vertex at the center of gravity of the vertices of the face
00045   - each vertex of the original mesh is replaced by a face containing the dual vertices of its primal adjacent faces
00046 
00047   Changelog:
00048     - 29 mar 2010: initial work
00049 
00050   Programmer: 
00051     Clement Courbet - clement.courbet@ecp.fr
00052 
00053   (c) Clement Courbet 2010
00054 */
00055 
00056 #ifndef OPENMESH_MESH_DUAL_H
00057 #define OPENMESH_MESH_DUAL_H
00058 
00059 //== INCLUDES =================================================================
00060 
00061 // -------------------- STL
00062 #include <vector>
00063 #if defined(OM_CC_MIPS)
00064 #  include <math.h>
00065 #else
00066 #  include <cmath>
00067 #endif
00068 
00069 #include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
00070 #include <OpenMesh/Core/Utils/Property.hh>
00071 
00072 //== FORWARDDECLARATIONS ======================================================
00073 
00074 //== NAMESPACES ===============================================================
00075 
00076 
00077 namespace OpenMesh   { 
00078 namespace Util { 
00079 
00080 //== Function DEFINITION =========================================================
00081 
00087 template <typename MeshTraits>
00088 PolyMesh_ArrayKernelT<MeshTraits>* MeshDual (PolyMesh_ArrayKernelT<MeshTraits> &primal)
00089 {
00090   PolyMesh_ArrayKernelT<MeshTraits>* dual = new PolyMesh_ArrayKernelT<MeshTraits>();
00091 
00092   //we will need to reference which vertex in the dual is attached to each face in the primal
00093   //and which face of the dual is attached to each vertex in the primal.
00094 
00095   FPropHandleT< typename PolyMesh_ArrayKernelT<MeshTraits>::VertexHandle > primalToDual;
00096   primal.add_property(primalToDual);
00097 
00098   //for each face in the primal mesh, add a vertex at the center of gravity of the face
00099   for(typename PolyMesh_ArrayKernelT<MeshTraits>::ConstFaceIter fit=primal.faces_begin(); fit!=primal.faces_end(); ++fit)
00100   {
00101       typename PolyMesh_ArrayKernelT<MeshTraits>::Point centerPoint(0,0,0);
00102       unsigned int degree= 0;
00103       for(typename PolyMesh_ArrayKernelT<MeshTraits>::ConstFaceVertexIter vit=primal.cfv_iter(fit); vit; ++vit, ++degree)
00104           centerPoint += primal.point(vit.handle());
00105       assert(degree!=0);
00106       centerPoint /= degree;
00107       primal.property(primalToDual, fit) = dual->add_vertex(centerPoint);
00108   }
00109 
00110   //for each vertex in the primal, add a face in the dual
00111   std::vector< typename PolyMesh_ArrayKernelT<MeshTraits>::VertexHandle >  face_vhandles;
00112   for(typename PolyMesh_ArrayKernelT<MeshTraits>::ConstVertexIter vit=primal.vertices_begin(); vit!=primal.vertices_end(); ++vit)
00113   {
00114       if(!primal.is_boundary(vit.handle()))
00115       {
00116           face_vhandles.clear();
00117           for(typename PolyMesh_ArrayKernelT<MeshTraits>::ConstVertexFaceIter fit=primal.cvf_iter(vit); fit; ++fit)
00118               face_vhandles.push_back(primal.property(primalToDual, fit));
00119           dual->add_face(face_vhandles);
00120       }
00121   }
00122 
00123   primal.remove_property(primalToDual);
00124 
00125   return dual;
00126 
00127 };
00128 
00129 //=============================================================================
00130 } // namespace Util
00131 } // namespace OpenMesh
00132 //=============================================================================
00133 
00134 //=============================================================================
00135 #endif // OPENMESH_MESH_DUAL_H defined
00136 //=============================================================================
00137 
00138 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines