OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
meshDualT.hh
1 /*===========================================================================*\
2 * *
3 * OpenMesh *
4 * Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen *
5 * www.openmesh.org *
6 * *
7 *---------------------------------------------------------------------------*
8 * This file is part of OpenMesh. *
9 * *
10 * OpenMesh is free software: you can redistribute it and/or modify *
11 * it under the terms of the GNU Lesser General Public License as *
12 * published by the Free Software Foundation, either version 3 of *
13 * the License, or (at your option) any later version with the *
14 * following exceptions: *
15 * *
16 * If other files instantiate templates or use macros *
17 * or inline functions from this file, or you compile this file and *
18 * link it with other files to produce an executable, this file does *
19 * not by itself cause the resulting executable to be covered by the *
20 * GNU Lesser General Public License. This exception does not however *
21 * invalidate any other reasons why the executable file might be *
22 * covered by the GNU Lesser General Public License. *
23 * *
24 * OpenMesh is distributed in the hope that it will be useful, *
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27 * GNU Lesser General Public License for more details. *
28 * *
29 * You should have received a copy of the GNU LesserGeneral Public *
30 * License along with OpenMesh. If not, *
31 * see <http://www.gnu.org/licenses/>. *
32 * *
33 \*===========================================================================*/
34
35 /*===========================================================================*\
36 * *
37 * $Revision: 990 $ *
38 * $Date: 2014-02-05 10:01:07 +0100 (Mi, 05. Feb 2014) $ *
39 * *
40 \*===========================================================================*/
41
42 /*
43 Compute the dual of a mesh:
44 - each face of the original mesh is replaced by a vertex at the center of gravity of the vertices of the face
45 - each vertex of the original mesh is replaced by a face containing the dual vertices of its primal adjacent faces
46
47 Changelog:
48 - 29 mar 2010: initial work
49
50 Programmer:
51 Clement Courbet - clement.courbet@ecp.fr
52
53 (c) Clement Courbet 2010
54 */
55
56 #ifndef OPENMESH_MESH_DUAL_H
57 #define OPENMESH_MESH_DUAL_H
58
59 //== INCLUDES =================================================================
60
61 // -------------------- STL
62 #include <vector>
63 #if defined(OM_CC_MIPS)
64 # include <math.h>
65 #else
66 # include <cmath>
67 #endif
68
69 #include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
70 #include <OpenMesh/Core/Utils/Property.hh>
71
72 //== FORWARDDECLARATIONS ======================================================
73
74 //== NAMESPACES ===============================================================
75
76
77 namespace OpenMesh {
78 namespace Util {
79
80 //== Function DEFINITION =========================================================
81
87 template <typename MeshTraits>
88 PolyMesh_ArrayKernelT<MeshTraits>* MeshDual (PolyMesh_ArrayKernelT<MeshTraits> &primal)
89 {
90 PolyMesh_ArrayKernelT<MeshTraits>* dual = new PolyMesh_ArrayKernelT<MeshTraits>();
91
92 //we will need to reference which vertex in the dual is attached to each face in the primal
93 //and which face of the dual is attached to each vertex in the primal.
94
95 FPropHandleT< typename PolyMesh_ArrayKernelT<MeshTraits>::VertexHandle > primalToDual;
96 primal.add_property(primalToDual);
97
98 //for each face in the primal mesh, add a vertex at the center of gravity of the face
99 for(typename PolyMesh_ArrayKernelT<MeshTraits>::ConstFaceIter fit=primal.faces_begin(); fit!=primal.faces_end(); ++fit)
100 {
101 typename PolyMesh_ArrayKernelT<MeshTraits>::Point centerPoint(0,0,0);
102 typename PolyMesh_ArrayKernelT<MeshTraits>::Scalar degree= 0.0;
103 for(typename PolyMesh_ArrayKernelT<MeshTraits>::ConstFaceVertexIter vit=primal.cfv_iter(*fit); vit.is_valid(); ++vit, ++degree)
104 centerPoint += primal.point(*vit);
105 assert(degree!=0);
106 centerPoint /= degree;
107 primal.property(primalToDual, *fit) = dual->add_vertex(centerPoint);
108 }
109
110 //for each vertex in the primal, add a face in the dual
111 std::vector< typename PolyMesh_ArrayKernelT<MeshTraits>::VertexHandle > face_vhandles;
112 for(typename PolyMesh_ArrayKernelT<MeshTraits>::ConstVertexIter vit=primal.vertices_begin(); vit!=primal.vertices_end(); ++vit)
113 {
114 if(!primal.is_boundary(*vit))
115 {
116 face_vhandles.clear();
117 for(typename PolyMesh_ArrayKernelT<MeshTraits>::ConstVertexFaceIter fit=primal.cvf_iter(*vit); fit.is_valid(); ++fit)
118 face_vhandles.push_back(primal.property(primalToDual, *fit));
119 dual->add_face(face_vhandles);
120 }
121 }
122
123 primal.remove_property(primalToDual);
124
125 return dual;
126
127 }
128
129 //=============================================================================
130 } // namespace Util
131 } // namespace OpenMesh
132 //=============================================================================
133
134 //=============================================================================
135 #endif // OPENMESH_MESH_DUAL_H defined
136 //=============================================================================
137
138