54 #ifndef OPENMESH_SUBDIVIDER_UNIFORM_SQRT3T_HH 55 #define OPENMESH_SUBDIVIDER_UNIFORM_SQRT3T_HH 60 #include <OpenMesh/Core/Mesh/Handles.hh> 61 #include <OpenMesh/Core/System/config.hh> 63 #if defined(_DEBUG) || defined(DEBUG) 66 # include <OpenMesh/Tools/Utils/MeshCheckerT.hh> 67 # define ASSERT_CONSISTENCY( T, m ) \ 68 assert(OpenMesh::Utils::MeshCheckerT<T>(m).check()) 70 # define ASSERT_CONSISTENCY( T, m ) 74 #if defined(OM_CC_MIPS) 84 namespace Subdivider {
97 template <
typename MeshType,
typename RealType =
double>
102 typedef RealType real_t;
103 typedef MeshType mesh_t;
106 typedef std::pair< real_t, real_t > weight_t;
107 typedef std::vector< std::pair<real_t,real_t> > weights_t;
112 Sqrt3T(
void) : parent_t(), _1over3( real_t(1.0/3.0) ), _1over27( real_t(1.0/27.0) )
115 Sqrt3T(MeshType &_m) : parent_t(_m), _1over3( real_t(1.0/3.0) ), _1over27( real_t(1.0/27.0) )
124 const char *
name()
const {
return "Uniform Sqrt3"; }
130 weights_.resize(_max_valence);
131 std::generate(weights_.begin(), weights_.end(), compute_weight());
140 _m.request_edge_status();
141 _m.add_property( vp_pos_ );
142 _m.add_property( ep_nv_ );
143 _m.add_property( mp_gen_ );
144 _m.property( mp_gen_ ) = 0;
146 return _m.has_edge_status() && vp_pos_.
is_valid()
153 _m.release_edge_status();
154 _m.remove_property( vp_pos_ );
155 _m.remove_property( ep_nv_ );
156 _m.remove_property( mp_gen_ );
160 bool subdivide( MeshType& _m,
size_t _n ,
const bool _update_points =
true)
165 typename MeshType::VertexIter vit;
166 typename MeshType::VertexVertexIter vvit;
167 typename MeshType::EdgeIter eit;
168 typename MeshType::FaceIter fit;
169 typename MeshType::FaceVertexIter fvit;
170 typename MeshType::VertexHandle vh;
171 typename MeshType::HalfedgeHandle heh;
172 typename MeshType::Point pos(0,0,0), zero(0,0,0);
173 size_t &gen = _m.property( mp_gen_ );
175 for (
size_t l=0; l<_n; ++l)
178 for (eit=_m.edges_begin(); eit != _m.edges_end();++eit)
180 _m.status( *eit ).set_tagged(
true );
181 if ( (gen%2) && _m.is_boundary(*eit) )
182 compute_new_boundary_points( _m, *eit );
187 for (vit=_m.vertices_begin(); vit!=_m.vertices_end(); ++vit)
189 if ( _m.is_boundary(*vit) )
193 heh = _m.halfedge_handle(*vit);
197 prev_heh = _m.prev_halfedge_handle(heh);
199 assert( _m.is_boundary(heh ) );
200 assert( _m.is_boundary(prev_heh) );
202 pos = _m.point(_m.to_vertex_handle(heh));
203 pos += _m.point(_m.from_vertex_handle(prev_heh));
206 pos += real_t(19.0) * _m.point( *vit );
209 _m.property( vp_pos_, *vit ) = pos;
213 _m.property( vp_pos_, *vit ) = _m.point( *vit );
220 for ( vvit = _m.vv_iter(*vit); vvit.is_valid(); ++vvit)
222 pos += _m.point( *vvit );
225 pos *= weights_[ valence ].second;
226 pos += weights_[ valence ].first * _m.point(*vit);
227 _m.property( vp_pos_, *vit ) = pos;
232 typename MeshType::FaceIter fend = _m.faces_end();
233 for (fit = _m.faces_begin();fit != fend; ++fit)
235 if ( (gen%2) && _m.is_boundary(*fit))
237 boundary_split( _m, *fit );
241 fvit = _m.fv_iter( *fit );
242 pos = _m.point( *fvit);
243 pos += _m.point(*(++fvit));
244 pos += _m.point(*(++fvit));
246 vh = _m.add_vertex( zero );
247 _m.property( vp_pos_, vh ) = pos;
248 _m.split( *fit, vh );
253 for (vit=_m.vertices_begin();vit != _m.vertices_end(); ++vit)
254 _m.set_point(*vit, _m.property( vp_pos_, *vit ) );
257 for (eit=_m.edges_begin(); eit != _m.edges_end(); ++eit)
258 if ( _m.status( *eit ).tagged() && !_m.is_boundary( *eit ) )
262 ASSERT_CONSISTENCY( MeshType, _m );
274 struct compute_weight
276 compute_weight() : valence(-1) { }
279 #if !defined(OM_CC_MIPS) 284 real_t alpha = real_t( (4.0-2.0*cos(2.0*M_PI / real_t(valence)) )/9.0 );
285 return weight_t( real_t(1)-alpha, alpha/real_t(valence) );
287 return weight_t(real_t(0.0), real_t(0.0) );
296 void compute_new_boundary_points( MeshType& _m,
297 const typename MeshType::EdgeHandle& _eh)
299 assert( _m.is_boundary(_eh) );
301 typename MeshType::HalfedgeHandle heh;
302 typename MeshType::VertexHandle vh1, vh2, vh3, vh4, vhl, vhr;
303 typename MeshType::Point zero(0,0,0), P1, P2, P3, P4;
318 heh = _m.halfedge_handle(_eh,
319 _m.is_boundary(_m.halfedge_handle(_eh,1)));
321 assert( _m.is_boundary( _m.next_halfedge_handle( heh ) ) );
322 assert( _m.is_boundary( _m.prev_halfedge_handle( heh ) ) );
324 vh1 = _m.to_vertex_handle( _m.next_halfedge_handle( heh ) );
325 vh2 = _m.to_vertex_handle( heh );
326 vh3 = _m.from_vertex_handle( heh );
327 vh4 = _m.from_vertex_handle( _m.prev_halfedge_handle( heh ));
334 vhl = _m.add_vertex(zero);
335 vhr = _m.add_vertex(zero);
337 _m.property(vp_pos_, vhl ) = (P1 + real_t(16.0f) * P2 + real_t(10.0f) * P3) * _1over27;
338 _m.property(vp_pos_, vhr ) = ( real_t(10.0f) * P2 + real_t(16.0f) * P3 + P4) * _1over27;
339 _m.property(ep_nv_, _eh).first = vhl;
340 _m.property(ep_nv_, _eh).second = vhr;
344 void boundary_split( MeshType& _m,
const typename MeshType::FaceHandle& _fh )
346 assert( _m.is_boundary(_fh) );
348 typename MeshType::VertexHandle vhl, vhr;
349 typename MeshType::FaceEdgeIter fe_it;
350 typename MeshType::HalfedgeHandle heh;
353 for( fe_it=_m.fe_iter( _fh ); fe_it.is_valid() && !_m.is_boundary( *fe_it ); ++fe_it ) {};
356 vhl = _m.property(ep_nv_, *fe_it).first;
357 vhr = _m.property(ep_nv_, *fe_it).second;
372 heh = _m.halfedge_handle(*fe_it,
373 _m.is_boundary(_m.halfedge_handle(*fe_it,0)));
375 typename MeshType::HalfedgeHandle pl_P3;
378 boundary_split( _m, heh, vhl );
379 pl_P3 = _m.next_halfedge_handle( heh );
380 boundary_split( _m, heh );
383 boundary_split( _m, pl_P3, vhr );
384 boundary_split( _m, pl_P3 );
386 assert( _m.is_boundary( vhl ) && _m.halfedge_handle(vhl).is_valid() );
387 assert( _m.is_boundary( vhr ) && _m.halfedge_handle(vhr).is_valid() );
390 void boundary_split(MeshType& _m,
391 const typename MeshType::HalfedgeHandle& _heh,
392 const typename MeshType::VertexHandle& _vh)
394 assert( _m.is_boundary( _m.edge_handle(_heh) ) );
396 typename MeshType::HalfedgeHandle
398 opp_heh( _m.opposite_halfedge_handle(_heh) ),
399 new_heh, opp_new_heh;
400 typename MeshType::VertexHandle to_vh(_m.to_vertex_handle(heh));
401 typename MeshType::HalfedgeHandle t_heh;
422 _m.next_halfedge_handle(t_heh) != opp_heh;
423 t_heh = _m.opposite_halfedge_handle(_m.next_halfedge_handle(t_heh)))
426 assert( _m.is_boundary( t_heh ) );
428 new_heh = _m.new_edge( _vh, to_vh );
429 opp_new_heh = _m.opposite_halfedge_handle(new_heh);
433 _m.set_next_halfedge_handle(t_heh, opp_new_heh);
435 _m.set_next_halfedge_handle(new_heh, _m.next_halfedge_handle(heh));
436 _m.set_next_halfedge_handle(heh, new_heh);
437 _m.set_next_halfedge_handle(opp_new_heh, opp_heh);
440 _m.set_face_handle(opp_new_heh, _m.face_handle(opp_heh));
443 _m.set_vertex_handle(heh, _vh);
446 _m.set_face_handle(new_heh, _m.face_handle(heh));
450 _m.set_halfedge_handle( to_vh, opp_new_heh );
453 _m.set_halfedge_handle( _vh, opp_heh );
456 void boundary_split( MeshType& _m,
457 const typename MeshType::HalfedgeHandle& _heh)
459 assert( _m.is_boundary( _m.opposite_halfedge_handle( _heh ) ) );
461 typename MeshType::HalfedgeHandle
463 n_heh(_m.next_halfedge_handle(heh));
465 typename MeshType::VertexHandle
466 to_vh(_m.to_vertex_handle(heh));
468 typename MeshType::HalfedgeHandle
469 heh2(_m.new_edge(to_vh,
470 _m.to_vertex_handle(_m.next_halfedge_handle(n_heh)))),
471 heh3(_m.opposite_halfedge_handle(heh2));
473 typename MeshType::FaceHandle
474 new_fh(_m.new_face()),
475 fh(_m.face_handle(heh));
479 #define set_next_heh set_next_halfedge_handle 480 #define next_heh next_halfedge_handle 482 _m.set_face_handle(heh, new_fh);
483 _m.set_face_handle(heh2, new_fh);
484 _m.set_next_heh(heh2, _m.next_heh(_m.next_heh(n_heh)));
485 _m.set_next_heh(heh, heh2);
486 _m.set_face_handle( _m.next_heh(heh2), new_fh);
490 _m.set_next_heh(heh3, n_heh);
491 _m.set_next_heh(_m.next_halfedge_handle(n_heh), heh3);
492 _m.set_face_handle(heh3, fh);
495 _m.set_halfedge_handle( fh, n_heh);
496 _m.set_halfedge_handle(new_fh, heh);
498 #undef set_next_halfedge_handle 499 #undef next_halfedge_handle 508 typename MeshType::VertexHandle> > ep_nv_;
511 const real_t _1over3;
512 const real_t _1over27;
521 #endif // OPENMESH_SUBDIVIDER_UNIFORM_SQRT3T_HH Handle for a halfedge entity.
Definition: Handles.hh:127
bool prepare(MeshType &_m)
Prepare mesh, e.g. add properties.
Definition: Sqrt3T.hh:138
void init_weights(size_t _max_valence=50)
Pre-compute weights.
Definition: Sqrt3T.hh:128
bool subdivide(MeshType &_m, size_t _n, const bool _update_points=true)
Subdivide mesh _m _n times.
Definition: Sqrt3T.hh:160
Uniform Sqrt3 subdivision algorithm
Definition: Sqrt3T.hh:98
bool operator()(MeshType &_m, size_t _n, const bool _update_points=true)
Subdivide the mesh _m _n times.
Definition: SubdividerT.hh:122
bool cleanup(MeshType &_m)
Cleanup mesh after usage, e.g. remove added properties.
Definition: Sqrt3T.hh:151
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:59
Handle representing an edge property.
Definition: Property.hh:510
Abstract base class for uniform subdivision algorithms.
Definition: SubdividerT.hh:88
const char * name() const
Return name of subdivision algorithm.
Definition: Sqrt3T.hh:124
bool is_valid() const
The handle is valid iff the index is not negative.
Definition: Handles.hh:72