59 #ifndef OPENMESH_SUBDIVIDER_UNIFORM_SQRT3T_HH 60 #define OPENMESH_SUBDIVIDER_UNIFORM_SQRT3T_HH 65 #include <OpenMesh/Core/Mesh/Handles.hh> 66 #include <OpenMesh/Core/System/config.hh> 68 #if defined(_DEBUG) || defined(DEBUG) 71 # include <OpenMesh/Tools/Utils/MeshCheckerT.hh> 72 # define ASSERT_CONSISTENCY( T, m ) \ 73 assert(OpenMesh::Utils::MeshCheckerT<T>(m).check()) 75 # define ASSERT_CONSISTENCY( T, m ) 79 #if defined(OM_CC_MIPS) 89 namespace Subdivider {
102 template <
typename MeshType,
typename RealType =
float>
107 typedef RealType real_t;
108 typedef MeshType mesh_t;
111 typedef std::pair< real_t, real_t > weight_t;
112 typedef std::vector< std::pair<real_t,real_t> > weights_t;
117 Sqrt3T(
void) : parent_t(), _1over3( real_t(1.0/3.0) ), _1over27( real_t(1.0/27.0) )
120 Sqrt3T(MeshType &_m) : parent_t(_m), _1over3( real_t(1.0/3.0) ), _1over27( real_t(1.0/27.0) )
129 const char *
name()
const {
return "Uniform Sqrt3"; }
135 weights_.resize(_max_valence);
136 std::generate(weights_.begin(), weights_.end(),
compute_weight());
145 _m.request_edge_status();
146 _m.add_property( vp_pos_ );
147 _m.add_property( ep_nv_ );
148 _m.add_property( mp_gen_ );
149 _m.property( mp_gen_ ) = 0;
151 return _m.has_edge_status() && vp_pos_.
is_valid()
152 && ep_nv_.is_valid() && mp_gen_.
is_valid();
158 _m.release_edge_status();
159 _m.remove_property( vp_pos_ );
160 _m.remove_property( ep_nv_ );
161 _m.remove_property( mp_gen_ );
165 bool subdivide( MeshType& _m,
size_t _n ,
const bool _update_points =
true)
170 typename MeshType::VertexIter vit;
171 typename MeshType::VertexVertexIter vvit;
172 typename MeshType::EdgeIter eit;
173 typename MeshType::FaceIter fit;
174 typename MeshType::FaceVertexIter fvit;
175 typename MeshType::VertexHandle vh;
176 typename MeshType::HalfedgeHandle heh;
177 typename MeshType::Point pos(0,0,0), zero(0,0,0);
178 size_t &gen = _m.property( mp_gen_ );
180 for (
size_t l=0; l<_n; ++l)
183 for (eit=_m.edges_begin(); eit != _m.edges_end();++eit)
185 _m.status( *eit ).set_tagged(
true );
186 if ( (gen%2) && _m.is_boundary(*eit) )
187 compute_new_boundary_points( _m, *eit );
192 for (vit=_m.vertices_begin(); vit!=_m.vertices_end(); ++vit)
194 if ( _m.is_boundary(*vit) )
198 heh = _m.halfedge_handle(*vit);
202 prev_heh = _m.prev_halfedge_handle(heh);
204 assert( _m.is_boundary(heh ) );
205 assert( _m.is_boundary(prev_heh) );
207 pos = _m.point(_m.to_vertex_handle(heh));
208 pos += _m.point(_m.from_vertex_handle(prev_heh));
211 pos += real_t(19.0) * _m.point( *vit );
214 _m.property( vp_pos_, *vit ) = pos;
218 _m.property( vp_pos_, *vit ) = _m.point( *vit );
225 for ( vvit = _m.vv_iter(*vit); vvit.is_valid(); ++vvit)
227 pos += _m.point( *vvit );
230 pos *= weights_[ valence ].second;
231 pos += weights_[ valence ].first * _m.point(*vit);
232 _m.property( vp_pos_, *vit ) = pos;
237 typename MeshType::FaceIter fend = _m.faces_end();
238 for (fit = _m.faces_begin();fit != fend; ++fit)
240 if ( (gen%2) && _m.is_boundary(*fit))
242 boundary_split( _m, *fit );
246 fvit = _m.fv_iter( *fit );
247 pos = _m.point( *fvit);
248 pos += _m.point(*(++fvit));
249 pos += _m.point(*(++fvit));
251 vh = _m.add_vertex( zero );
252 _m.property( vp_pos_, vh ) = pos;
253 _m.split( *fit, vh );
258 for (vit=_m.vertices_begin();vit != _m.vertices_end(); ++vit)
259 _m.set_point(*vit, _m.property( vp_pos_, *vit ) );
262 for (eit=_m.edges_begin(); eit != _m.edges_end(); ++eit)
263 if ( _m.status( *eit ).tagged() && !_m.is_boundary( *eit ) )
267 ASSERT_CONSISTENCY( MeshType, _m );
282 weight_t operator() (
void)
284 #if !defined(OM_CC_MIPS) 289 real_t alpha = real_t( (4.0-2.0*cos(2.0*M_PI / real_t(valence)) )/9.0 );
290 return weight_t( real_t(1)-alpha, alpha/real_t(valence) );
292 return weight_t(real_t(0.0), real_t(0.0) );
301 void compute_new_boundary_points( MeshType& _m,
302 const typename MeshType::EdgeHandle& _eh)
304 assert( _m.is_boundary(_eh) );
306 typename MeshType::HalfedgeHandle heh;
307 typename MeshType::VertexHandle vh1, vh2, vh3, vh4, vhl, vhr;
308 typename MeshType::Point zero(0,0,0), P1, P2, P3, P4;
323 heh = _m.halfedge_handle(_eh,
324 _m.is_boundary(_m.halfedge_handle(_eh,1)));
326 assert( _m.is_boundary( _m.next_halfedge_handle( heh ) ) );
327 assert( _m.is_boundary( _m.prev_halfedge_handle( heh ) ) );
329 vh1 = _m.to_vertex_handle( _m.next_halfedge_handle( heh ) );
330 vh2 = _m.to_vertex_handle( heh );
331 vh3 = _m.from_vertex_handle( heh );
332 vh4 = _m.from_vertex_handle( _m.prev_halfedge_handle( heh ));
339 vhl = _m.add_vertex(zero);
340 vhr = _m.add_vertex(zero);
342 _m.property(vp_pos_, vhl ) = (P1 + real_t(16.0f) * P2 + real_t(10.0f) * P3) * _1over27;
343 _m.property(vp_pos_, vhr ) = ( real_t(10.0f) * P2 + real_t(16.0f) * P3 + P4) * _1over27;
344 _m.property(ep_nv_, _eh).first = vhl;
345 _m.property(ep_nv_, _eh).second = vhr;
349 void boundary_split( MeshType& _m,
const typename MeshType::FaceHandle& _fh )
351 assert( _m.is_boundary(_fh) );
353 typename MeshType::VertexHandle vhl, vhr;
354 typename MeshType::FaceEdgeIter fe_it;
355 typename MeshType::HalfedgeHandle heh;
358 for( fe_it=_m.fe_iter( _fh ); fe_it.is_valid() && !_m.is_boundary( *fe_it ); ++fe_it ) {};
361 vhl = _m.property(ep_nv_, *fe_it).first;
362 vhr = _m.property(ep_nv_, *fe_it).second;
377 heh = _m.halfedge_handle(*fe_it,
378 _m.is_boundary(_m.halfedge_handle(*fe_it,0)));
380 typename MeshType::HalfedgeHandle pl_P3;
383 boundary_split( _m, heh, vhl );
384 pl_P3 = _m.next_halfedge_handle( heh );
385 boundary_split( _m, heh );
388 boundary_split( _m, pl_P3, vhr );
389 boundary_split( _m, pl_P3 );
391 assert( _m.is_boundary( vhl ) && _m.halfedge_handle(vhl).is_valid() );
392 assert( _m.is_boundary( vhr ) && _m.halfedge_handle(vhr).is_valid() );
395 void boundary_split(MeshType& _m,
396 const typename MeshType::HalfedgeHandle& _heh,
397 const typename MeshType::VertexHandle& _vh)
399 assert( _m.is_boundary( _m.edge_handle(_heh) ) );
401 typename MeshType::HalfedgeHandle
403 opp_heh( _m.opposite_halfedge_handle(_heh) ),
404 new_heh, opp_new_heh;
405 typename MeshType::VertexHandle to_vh(_m.to_vertex_handle(heh));
406 typename MeshType::HalfedgeHandle t_heh;
427 _m.next_halfedge_handle(t_heh) != opp_heh;
428 t_heh = _m.opposite_halfedge_handle(_m.next_halfedge_handle(t_heh)))
431 assert( _m.is_boundary( t_heh ) );
433 new_heh = _m.new_edge( _vh, to_vh );
434 opp_new_heh = _m.opposite_halfedge_handle(new_heh);
438 _m.set_next_halfedge_handle(t_heh, opp_new_heh);
440 _m.set_next_halfedge_handle(new_heh, _m.next_halfedge_handle(heh));
441 _m.set_next_halfedge_handle(heh, new_heh);
442 _m.set_next_halfedge_handle(opp_new_heh, opp_heh);
445 _m.set_face_handle(opp_new_heh, _m.face_handle(opp_heh));
448 _m.set_vertex_handle(heh, _vh);
451 _m.set_face_handle(new_heh, _m.face_handle(heh));
455 _m.set_halfedge_handle( to_vh, opp_new_heh );
458 _m.set_halfedge_handle( _vh, opp_heh );
461 void boundary_split( MeshType& _m,
462 const typename MeshType::HalfedgeHandle& _heh)
464 assert( _m.is_boundary( _m.opposite_halfedge_handle( _heh ) ) );
466 typename MeshType::HalfedgeHandle
468 n_heh(_m.next_halfedge_handle(heh));
470 typename MeshType::VertexHandle
471 to_vh(_m.to_vertex_handle(heh));
473 typename MeshType::HalfedgeHandle
474 heh2(_m.new_edge(to_vh,
475 _m.to_vertex_handle(_m.next_halfedge_handle(n_heh)))),
476 heh3(_m.opposite_halfedge_handle(heh2));
478 typename MeshType::FaceHandle
479 new_fh(_m.new_face()),
480 fh(_m.face_handle(heh));
484 #define set_next_heh set_next_halfedge_handle 485 #define next_heh next_halfedge_handle 487 _m.set_face_handle(heh, new_fh);
488 _m.set_face_handle(heh2, new_fh);
489 _m.set_next_heh(heh2, _m.next_heh(_m.next_heh(n_heh)));
490 _m.set_next_heh(heh, heh2);
491 _m.set_face_handle( _m.next_heh(heh2), new_fh);
495 _m.set_next_heh(heh3, n_heh);
496 _m.set_next_heh(_m.next_halfedge_handle(n_heh), heh3);
497 _m.set_face_handle(heh3, fh);
500 _m.set_halfedge_handle( fh, n_heh);
501 _m.set_halfedge_handle(new_fh, heh);
503 #undef set_next_halfedge_handle 504 #undef next_halfedge_handle 513 typename MeshType::VertexHandle> > ep_nv_;
516 const real_t _1over3;
517 const real_t _1over27;
526 #endif // OPENMESH_SUBDIVIDER_UNIFORM_SQRT3T_HH
bool cleanup(MeshType &_m)
Cleanup mesh after usage, e.g. remove added properties.
bool subdivide(MeshType &_m, size_t _n, const bool _update_points=true)
Subdivide mesh _m _n times.
bool is_valid() const
The handle is valid iff the index is not equal to -1.
void init_weights(size_t _max_valence=50)
Pre-compute weights.
Handle for a halfedge entity.
bool prepare(MeshType &_m)
Prepare mesh, e.g. add properties.
const char * name() const
Return name of subdivision algorithm.