Developer Documentation
MeshInfoT_impl.hh
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 
43 
44 
45 
46 
47 //=============================================================================
48 //
49 // IMPLEMENTATION
50 //
51 //=============================================================================
52 
53 #define MESHINFOT_C
54 
55 //== INCLUDES =================================================================
56 
57 #include "MeshInfoT.hh"
58 
59 #include <iostream>
60 #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
61 #include <OpenMesh/Core/Geometry/MathDefs.hh>
62 
63 
64 //== NAMESPACES ===============================================================
65 
66 namespace MeshInfo {
67 
68 //== IMPLEMENTATION ===========================================================
69 
70 template < typename MeshT >
71 inline
72 int boundaryCount(MeshT* _mesh ) {
73 
74  int numOfBounds = 0;
75 
77  _mesh->add_property(visited);
78 
79  typename MeshT::VertexHandle last;
80  const typename MeshT::VertexHandle begin;
81 
82  //Iteration �ber Halfedges
83  for (typename MeshT::HalfedgeIter he_it=_mesh->halfedges_begin(); he_it!=_mesh->halfedges_end() ; ++he_it )
84  _mesh->property(visited,*he_it) = false;
85 
86  for (typename MeshT::HalfedgeIter he_it=_mesh->halfedges_begin(); he_it!=_mesh->halfedges_end() ; ++he_it ) {
87  if ( _mesh->property(visited,*he_it) )
88  continue;
89 
90  if( !_mesh->is_boundary(*he_it ) )
91  continue;
92 
93  _mesh->property(visited,*he_it) = true;
94  typename MeshT::HalfedgeHandle he = _mesh->next_halfedge_handle(*he_it);
95 
96  while( _mesh->is_boundary(*he_it) && ! _mesh->property(visited,he) ) {
97  _mesh->property(visited,he) = true;
98  he = _mesh->next_halfedge_handle(he);
99  }
100 
101  ++numOfBounds;
102 
103  }
104 
105  _mesh->remove_property(visited);
106 
107  return numOfBounds;
108 }
109 
110 //=============================================================================
111 
112 template < typename MeshT >
113 inline
114 int componentCount(MeshT* _mesh ) {
115 
116  int numOfComps = 0;
117 
119  _mesh->add_property(visited);
120 
121  typename MeshT::VertexIter v_it;
122  typename MeshT::VertexIter v_end = _mesh->vertices_end();
123 
124  //iterate over all vertices
125  for (v_it = _mesh->vertices_begin(); v_it != v_end; ++v_it)
126  _mesh->property(visited, *v_it) = false;
127 
128  typename MeshT::VertexHandle vh;
129  typename MeshT::VertexIter current_pos = _mesh->vertices_begin();
130 
131  while( true ){
132  //find an unvisited vertex
133  bool found = false;
134  for (v_it = current_pos ; v_it != v_end; ++v_it)
135  if ( !_mesh->property(visited, *v_it) ){
136  found = true;
137  vh = *v_it;
138  _mesh->property(visited, *v_it) = true;
139  current_pos = v_it;
140  break;
141  }
142 
143  //if none was found -> finished
144  if (!found) break;
145 
146  numOfComps++;
147 
148  std::vector< typename MeshT::VertexHandle > handles;
149  handles.push_back( vh );
150 
151  //grow from found vertex
152  while( ! handles.empty() ){
153  typename MeshT::VertexHandle current = handles.back();
154  handles.pop_back();
155 
156  typename MeshT::VertexVertexIter vv_it;
157 
158  for (vv_it=_mesh->vv_iter( current ); vv_it.is_valid(); ++vv_it)
159  if ( !_mesh->property(visited, *vv_it) ){
160  _mesh->property(visited, *vv_it) = true;
161  handles.push_back( *vv_it );
162  }
163  }
164  }
165 
166  _mesh->remove_property(visited);
167 
168  return numOfComps;
169 }
170 
171 //=============================================================================
172 
173 template < typename MeshT >
174 inline
175 void getBoundingBox( MeshT* _mesh,
176  typename MeshT::Point& _min ,
177  typename MeshT::Point& _max) {
178  if ( _mesh->n_vertices() == 0 ) {
179  std::cerr << "Unable to compute Bounding Box: No points in Mesh!" << std::endl;
180  }
181  // Use any point as initial value
182  _min = _mesh->point(_mesh->vertex_handle(0));
183  _max = _mesh->point(_mesh->vertex_handle(0));
184 
185  for (typename MeshT::VertexIter v_it = _mesh->vertices_begin() ; v_it != _mesh->vertices_end() ; ++v_it ) {
186  _min.minimize( _mesh->point(*v_it) );
187  _max.maximize( _mesh->point(*v_it) );
188  }
189 
190 }
191 
192 //=============================================================================
193 
194 template < typename MeshT >
195 inline
196 typename MeshT::Point
197 cog ( const MeshT* _mesh ) {
198  typename MeshT::ConstVertexIter v_it, v_end=_mesh->vertices_end();
199  typename MeshT::Point cog(0.0,0.0,0.0);
200 
201  for (v_it = _mesh->vertices_begin(); v_it != v_end ; ++v_it)
202  cog += _mesh->point(*v_it);
203  cog = 1.0 / (typename MeshT::Scalar)_mesh->n_vertices() * cog;
204 
205  return cog;
206 }
207 
208 //=============================================================================
209 } // MeshInfo Namespace
210 //=============================================================================