Developer Documentation
HoleInfoT.cc
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 * $Revision: 11127 $ *
45 * $LastChangedBy: moebius $ *
46 * $Date: 2011-03-15 16:18:28 +0100 (Di, 15 Mär 2011) $ *
47 * *
48 \*===========================================================================*/
49 
50 #define HOLEINFO_C
51 
52 #include "HoleInfoT.hh"
53 
55 
56 #include <algorithm>
57 
59 template< class MeshT >
61  : mesh_(_mesh), filler_(0)
62 {
63 }
64 
66 template< class MeshT >
68 {
69  if (filler_ != 0)
70  delete filler_;
71 }
72 
74 template< class MeshT >
76 {
77 
78  // Property for the active mesh to mark already visited edges
79  OpenMesh::EPropHandleT< bool > boundary_search;
80 
81  // Add a property to the mesh to store original vertex positions
82  mesh_->add_property( boundary_search, "Boundary search" );
83 
84  // Initialize Property
85  typename MeshT::EdgeIter e_it, e_end=mesh_->edges_end();
86  for (e_it=mesh_->edges_begin(); e_it!=e_end; ++e_it) {
87  mesh_->property( boundary_search , *e_it ) = false;
88  }
89 
90  holes_.clear();
91 
92  for (e_it=mesh_->edges_begin(); e_it!=e_end; ++e_it) {
93 
94  // Skip already visited edges
95  if ( mesh_->property( boundary_search , *e_it ) )
96  continue;
97 
98  // Check only boundary edges
99  if ( !mesh_->is_boundary(*e_it))
100  continue;
101 
102  // Get boundary halfedge
103  typename MeshT::HalfedgeHandle hh = mesh_->halfedge_handle( *e_it, 0 );
104  if ( ! mesh_->is_boundary( hh ) )
105  hh = mesh_->opposite_halfedge_handle( hh );
106 
107 
108  typename MeshT::Point center(0,0,0);
109 
110  Hole currentHole;
111 
112  // Collect boundary edges
113  typename MeshT::HalfedgeHandle ch = hh;
114  do {
115  currentHole.push_back( mesh_->edge_handle(ch) );
116 
117  center += mesh_->point( mesh_->from_vertex_handle(ch) );
118 
119  mesh_->property( boundary_search , mesh_->edge_handle(ch) ) = true;
120 
121  //check number of outgoing boundary HEH's at Vertex
122  int c = 0;
123  typename MeshT::VertexHandle vh = mesh_->to_vertex_handle(ch);
124 
125  for ( typename MeshT::VertexOHalfedgeIter voh_it(*mesh_,vh); voh_it.is_valid(); ++voh_it)
126  if ( mesh_->is_boundary( *voh_it ) )
127  c++;
128 
129  if ( c >= 2){
130  typename MeshT::HalfedgeHandle op = mesh_->opposite_halfedge_handle( ch );
131  typename MeshT::VertexOHalfedgeIter voh_it(*mesh_,op);
132 
133  ch = *(++voh_it);
134 
135  } else
136  ch = mesh_->next_halfedge_handle( ch );
137 
138  } while ( ch != hh );
139 
140 
141  center /= currentHole.size();
142 
143  bool isHole = true;
144 
145  int err = 0;
146 
147  for (unsigned int i=0; i < currentHole.size(); i++){
148  typename MeshT::HalfedgeHandle hh = mesh_->halfedge_handle( currentHole[i], 0 );
149 
150  if ( ! mesh_->is_boundary( hh ) )
151  hh = mesh_->opposite_halfedge_handle( hh );
152 
153  typename MeshT::VertexHandle vh = mesh_->from_vertex_handle(hh);
154 
155  typename MeshT::Normal n = mesh_->normal( vh );
156 
157  typename MeshT::Point p = mesh_->point( vh );
158 
159  if ( (p - center).norm() < (p + n - center).norm() ){
160 // isHole = false;
161 // break;
162  err++;
163  }
164  }
165 
166 // std::cerr << "Errors " << err << " Size " << hole.count << std::endl;
167  if (isHole)
168  holes_.push_back(currentHole);
169  }
170 
171  mesh_->remove_property( boundary_search);
172 
173 }
174 
176 template< class MeshT >
177 void HoleInfo< MeshT >::fillHole(int _index, int _stages)
178 {
179 
180  if ( (uint) _index > holes_.size()){
181  std::cerr << "Cannot fill hole. Index invalid." << std::endl;
182  return;
183  }
184 
185  if (filler_ == 0)
186  filler_ = new HoleFiller< MeshT >(*mesh_);
187 
188  filler_->fill_hole( holes_[_index][0], _stages );
189 
190  mesh_->garbage_collection();
191 
192  MeshSelection::clearEdgeSelection(mesh_);
193 
194  mesh_->update_normals();
195 }
196 
198 template< class MeshT >
199 void HoleInfo< MeshT >::fillHole(typename MeshT::EdgeHandle _eh, int _stages)
200 {
201  if (filler_ == 0)
202  filler_ = new HoleFiller< MeshT >(*mesh_);
203 
204  filler_->fill_hole( _eh, _stages );
205 
206  mesh_->garbage_collection();
207 
208  MeshSelection::clearEdgeSelection(mesh_);
209 
210  mesh_->update_normals();
211 }
212 
214 template< class MeshT >
216 {
217 
218  if (filler_ == 0)
219  filler_ = new HoleFiller< MeshT >(*mesh_);
220 
221  filler_->fill_all_holes( _stages );
222 
223 }
224 
226 template< class MeshT >
228 {
229 
230  if ( (uint) _index > holes_.size()){
231  std::cerr << "Cannot select hole. Index invalid." << std::endl;
232  return;
233  }
234 
235  for ( uint i = 0 ; i < (holes_[_index]).size() ; ++i ) {
236  mesh_->status( (holes_[_index])[i] ).set_selected(true);
237  }
238 
239 }
240 
241 template< class MeshT >
242 void HoleInfo< MeshT >::getHolePostitionInfo(const int _index, typename MeshT::Normal& _holeNormal, typename MeshT::Point& _holeCenter) const
243 {
244 
245  _holeCenter = typename MeshT::Point(0.0,0.0,0.0);
246  _holeNormal = typename MeshT::Normal(0.0,0.0,0.0);
247 
248  // Center of gravity of hole and an average normal at the hole boundary
249  for ( size_t i = 0 ; i < holes_[_index].size() ; ++i ) {
250  const typename MeshT::HalfedgeHandle he = mesh_->halfedge_handle(holes_[_index][i],0);
251  const typename MeshT::VertexHandle vh_to = mesh_->to_vertex_handle(he);
252 
253  _holeCenter += mesh_->point(vh_to);
254  _holeNormal += mesh_->normal(vh_to);
255  }
256 
257  _holeCenter /= typename MeshT::Scalar(holes_[_index].size());
258  _holeNormal /= typename MeshT::Scalar(holes_[_index].size());
259  _holeNormal.normalize();
260 
261 }
262 
263 template< class MeshT >
264 void HoleInfo< MeshT >::getHoleInfo(const unsigned int _index, size_t& _edges, typename MeshT::Scalar& _diagonal, typename MeshT::Scalar& _boundaryLength) const
265 {
266 
267  if ( _index >= holes_.size() ) {
268  std::cerr << "Invalid hole index " << _index << std::endl;
269  return;
270  }
271 
272  _boundaryLength = 0.0;
273 
274  typename MeshT::Point minCoord = typename MeshT::Point(std::numeric_limits<typename MeshT::Scalar>::max(),std::numeric_limits<typename MeshT::Scalar>::max(),std::numeric_limits<typename MeshT::Scalar>::max());
275  typename MeshT::Point maxCoord = typename MeshT::Point(-std::numeric_limits<typename MeshT::Scalar>::max(),-std::numeric_limits<typename MeshT::Scalar>::max(),-std::numeric_limits<typename MeshT::Scalar>::max());
276 
277  for (size_t i = 0 ; i < holes_[_index].size() ; ++i) {
278  _boundaryLength += mesh_->calc_edge_length(holes_[_index][i]);
279 
280  typename MeshT::Point pos = mesh_->point(mesh_->from_vertex_handle(mesh_->halfedge_handle(holes_[_index][i],0)));
281  minCoord[0] = std::min(minCoord[0],pos[0]);
282  minCoord[1] = std::min(minCoord[1],pos[1]);
283  minCoord[2] = std::min(minCoord[2],pos[2]);
284 
285  maxCoord[0] = std::max(maxCoord[0],pos[0]);
286  maxCoord[1] = std::max(maxCoord[1],pos[1]);
287  maxCoord[2] = std::max(maxCoord[2],pos[2]);
288  }
289 
290  _edges = holes_[_index].size();
291  _diagonal = (maxCoord - minCoord).length();
292 
293 }
294 
295 
297 template< class MeshT >
298 std::vector< std::vector< typename MeshT::EdgeHandle > >* HoleInfo< MeshT >::holes()
299 {
300  return &holes_;
301 }
302 
303 
Add normals to mesh item (vertices/faces)
Definition: Attributes.hh:87
void fillAllHoles(int _stages=3)
fill all holes
Definition: HoleInfoT.cc:215
HoleInfo(MeshT *_mesh)
Konstruktor.
Definition: HoleInfoT.cc:60
void getHoleInfo(const unsigned int _index, size_t &_edges, typename MeshT::Scalar &_diagonal, typename MeshT::Scalar &_boundaryLength) const
Definition: HoleInfoT.cc:264
Functions for selection on a mesh.
void getHolePostitionInfo(const int _index, typename MeshT::Normal &_holeNormal, typename MeshT::Point &_holeCenter) const
Collect information to fly to a hole.
Definition: HoleInfoT.cc:242
void fillHole(int _index, int _stages=3)
fill hole with given index
Definition: HoleInfoT.cc:177
~HoleInfo()
Destruktor.
Definition: HoleInfoT.cc:67
void getHoles()
get all holes and store them internally
Definition: HoleInfoT.cc:75
std::vector< std::vector< typename MeshT::EdgeHandle > > * holes()
get the holes vector
Definition: HoleInfoT.cc:298
void selectHole(int _index)
select a hole with given index
Definition: HoleInfoT.cc:227