64 #ifndef OPENMESH_SUBDIVIDER_UNIFORM_INTERP_SQRT3T_LABSIK_GREINER_HH
65 #define OPENMESH_SUBDIVIDER_UNIFORM_INTERP_SQRT3T_LABSIK_GREINER_HH
70 #include <OpenMesh/Core/Mesh/Handles.hh>
71 #include <OpenMesh/Core/System/config.hh>
74 #if defined(_DEBUG) || defined(DEBUG)
77 # include <OpenMesh/Tools/Utils/MeshCheckerT.hh>
78 # define ASSERT_CONSISTENCY( T, m ) \
79 assert(OpenMesh::Utils::MeshCheckerT<T>(m).check())
81 # define ASSERT_CONSISTENCY( T, m )
85 #if defined(OM_CC_MIPS)
97 namespace Subdivider {
112 template <
typename MeshType,
typename RealType =
float>
117 typedef RealType real_t;
118 typedef MeshType mesh_t;
121 typedef std::vector< std::vector<real_t> > weights_t;
138 const char *
name()
const {
return "Uniform Interpolating Sqrt3"; }
143 weights_.resize(_max_valence);
145 weights_[3].resize(4);
146 weights_[3][0] = real_t(+4.0/27);
147 weights_[3][1] = real_t(-5.0/27);
148 weights_[3][2] = real_t(+4.0/27);
149 weights_[3][3] = real_t(+8.0/9);
151 weights_[4].resize(5);
152 weights_[4][0] = real_t(+2.0/9);
153 weights_[4][1] = real_t(-1.0/9);
154 weights_[4][2] = real_t(-1.0/9);
155 weights_[4][3] = real_t(+2.0/9);
156 weights_[4][4] = real_t(+7.0/9);
158 for(
unsigned int K=5; K<_max_valence; ++K)
160 weights_[K].resize(K+1);
161 real_t aH = 2.0*cos(M_PI/K)/3.0;
162 weights_[K][K] = 1.0 - aH*aH;
163 for(
unsigned int i=0; i<K; ++i)
165 weights_[K][i] = (aH*aH + 2.0*aH*cos(2.0*i*M_PI/K + M_PI/K) + 2.0*aH*aH*cos(4.0*i*M_PI/K + 2.0*M_PI/K))/K;
170 weights_[6].resize(0);
180 _m.request_edge_status();
181 _m.add_property( fp_pos_ );
182 _m.add_property( ep_nv_ );
183 _m.add_property( mp_gen_ );
184 _m.property( mp_gen_ ) = 0;
186 return _m.has_edge_status()
187 && ep_nv_.is_valid() && mp_gen_.
is_valid();
193 _m.release_edge_status();
194 _m.remove_property( fp_pos_ );
195 _m.remove_property( ep_nv_ );
196 _m.remove_property( mp_gen_ );
201 bool subdivide( MeshType& _m,
size_t _n ,
const bool _update_points =
true)
206 typename MeshType::VertexIter vit;
207 typename MeshType::VertexVertexIter vvit;
208 typename MeshType::EdgeIter eit;
209 typename MeshType::FaceIter fit;
210 typename MeshType::FaceVertexIter fvit;
211 typename MeshType::FaceHalfedgeIter fheit;
212 typename MeshType::VertexHandle vh;
213 typename MeshType::HalfedgeHandle heh;
214 typename MeshType::Point pos(0,0,0), zero(0,0,0);
215 size_t &gen = _m.property( mp_gen_ );
217 for (
size_t l=0; l<_n; ++l)
220 for (eit=_m.edges_begin(); eit != _m.edges_end();++eit)
222 _m.status( *eit ).set_tagged(
true );
223 if ( (gen%2) && _m.is_boundary(*eit) )
224 compute_new_boundary_points( _m, *eit );
228 typename MeshType::FaceIter fend = _m.faces_end();
229 for (fit = _m.faces_begin();fit != fend; ++fit)
231 if (_m.is_boundary(*fit))
234 _m.property(fp_pos_, *fit).invalidate();
238 for( heh = _m.halfedge_handle(*fit); !_m.is_boundary( _m.opposite_halfedge_handle(heh) ); heh = _m.next_halfedge_handle(heh) )
240 assert(_m.is_boundary( _m.opposite_halfedge_handle(heh) ));
243 if( _m.is_boundary(_m.next_halfedge_handle(heh)) || _m.is_boundary(_m.prev_halfedge_handle(heh)) )
245 if(_m.is_boundary(_m.prev_halfedge_handle(heh)))
246 heh = _m.prev_halfedge_handle(heh);
248 if(_m.is_boundary(_m.next_halfedge_handle(_m.next_halfedge_handle(heh))))
251 pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(heh));
252 pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
253 pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(_m.prev_halfedge_handle(heh)));
257 #ifdef MIRROR_TRIANGLES
259 pos += real_t(2.0/9) * _m.point(_m.to_vertex_handle(heh));
260 pos += real_t(4.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
261 pos += real_t(4.0/9) * _m.point(_m.to_vertex_handle(_m.prev_halfedge_handle(heh)));
262 pos += real_t(-1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
264 pos += real_t(7.0/24) * _m.point(_m.to_vertex_handle(heh));
265 pos += real_t(3.0/8) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
266 pos += real_t(3.0/8) * _m.point(_m.to_vertex_handle(_m.prev_halfedge_handle(heh)));
267 pos += real_t(-1.0/24) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
273 vh = _m.to_vertex_handle(_m.next_halfedge_handle(heh));
275 if((_m.valence(vh) == 6) || _m.is_boundary(vh))
277 #ifdef MIRROR_TRIANGLES
279 pos += real_t(5.0/9) * _m.point(vh);
280 pos += real_t(3.0/9) * _m.point(_m.to_vertex_handle(heh));
281 pos += real_t(3.0/9) * _m.point(_m.to_vertex_handle(_m.opposite_halfedge_handle(heh)));
282 pos += real_t(-1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.next_halfedge_handle(heh)))));
283 pos += real_t(-1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
286 pos += real_t(1.0/9) * _m.point(vh);
287 pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(heh));
288 pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(_m.opposite_halfedge_handle(heh)));
289 pos += real_t(1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.next_halfedge_handle(heh)))));
290 pos += real_t(1.0/9) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
292 pos += real_t(1.0/2) * _m.point(vh);
293 pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(heh));
294 pos += real_t(1.0/3) * _m.point(_m.to_vertex_handle(_m.opposite_halfedge_handle(heh)));
295 pos += real_t(-1.0/12) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.next_halfedge_handle(heh)))));
296 pos += real_t(-1.0/12) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(_m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)))));
303 unsigned int K = _m.valence(vh);
304 pos += weights_[K][K]*_m.point(vh);
305 heh = _m.opposite_halfedge_handle( _m.next_halfedge_handle(heh) );
306 for(
unsigned int i = 0; i<K; ++i, heh = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)) )
308 pos += weights_[K][i]*_m.point(_m.to_vertex_handle(heh));
312 vh = _m.add_vertex( pos );
313 _m.property(fp_pos_, *fit) = vh;
322 for(fvit = _m.fv_iter( *fit ); fvit.is_valid(); ++fvit)
323 if( (_m.valence(*fvit)) == 6 || _m.is_boundary(*fvit) )
328 for(fheit = _m.fh_iter( *fit ); fheit.is_valid(); ++fheit)
332 assert(_m.to_vertex_handle(heh).is_valid());
333 pos += real_t(32.0/81) * _m.point(_m.to_vertex_handle(heh));
335 heh = _m.opposite_halfedge_handle(heh);
336 assert(heh.is_valid());
337 assert(_m.next_halfedge_handle(heh).is_valid());
338 assert(_m.to_vertex_handle(_m.next_halfedge_handle(heh)).is_valid());
339 pos -= real_t(1.0/81) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
341 heh = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh));
342 assert(heh.is_valid());
343 assert(_m.next_halfedge_handle(heh).is_valid());
344 assert(_m.to_vertex_handle(_m.next_halfedge_handle(heh)).is_valid());
345 pos -= real_t(2.0/81) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
346 heh = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh));
347 assert(heh.is_valid());
348 assert(_m.next_halfedge_handle(heh).is_valid());
349 assert(_m.to_vertex_handle(_m.next_halfedge_handle(heh)).is_valid());
350 pos -= real_t(2.0/81) * _m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh)));
356 for(fheit = _m.fh_iter( *fit ); fheit.is_valid(); ++fheit)
358 vh = _m.to_vertex_handle(*fheit);
359 if( (_m.valence(vh) != 6) && (!_m.is_boundary(vh)) )
361 unsigned int K = _m.valence(vh);
362 pos += weights_[K][K]*_m.point(vh);
363 heh = _m.opposite_halfedge_handle( *fheit );
364 for(
unsigned int i = 0; i<K; ++i, heh = _m.opposite_halfedge_handle(_m.prev_halfedge_handle(heh)) )
366 pos += weights_[K][i]*_m.point(_m.to_vertex_handle(heh));
370 pos *= real_t(1.0/(3-nOrdinary));
373 vh = _m.add_vertex( pos );
374 _m.property(fp_pos_, *fit) = vh;
379 for (fit = _m.faces_begin();fit != fend; ++fit)
381 if ( _m.is_boundary(*fit) && (gen%2))
383 boundary_split( _m, *fit );
387 assert(_m.property(fp_pos_, *fit).is_valid());
388 _m.split( *fit, _m.property(fp_pos_, *fit) );
393 for (eit=_m.edges_begin(); eit != _m.edges_end(); ++eit)
394 if ( _m.status( *eit ).tagged() && !_m.is_boundary( *eit ) )
398 ASSERT_CONSISTENCY( MeshType, _m );
410 void compute_new_boundary_points( MeshType& _m,
411 const typename MeshType::EdgeHandle& _eh)
413 assert( _m.is_boundary(_eh) );
415 typename MeshType::HalfedgeHandle heh;
416 typename MeshType::VertexHandle vh1, vh2, vh3, vh4, vhl, vhr;
417 typename MeshType::Point zero(0,0,0), P1, P2, P3, P4;
432 heh = _m.halfedge_handle(_eh,
433 _m.is_boundary(_m.halfedge_handle(_eh,1)));
435 assert( _m.is_boundary( _m.next_halfedge_handle( heh ) ) );
436 assert( _m.is_boundary( _m.prev_halfedge_handle( heh ) ) );
438 vh1 = _m.to_vertex_handle( _m.next_halfedge_handle( heh ) );
439 vh2 = _m.to_vertex_handle( heh );
440 vh3 = _m.from_vertex_handle( heh );
441 vh4 = _m.from_vertex_handle( _m.prev_halfedge_handle( heh ));
448 vhl = _m.add_vertex(real_t(-5.0/81)*P1 + real_t(20.0/27)*P2 + real_t(10.0/27)*P3 + real_t(-4.0/81)*P4);
449 vhr = _m.add_vertex(real_t(-5.0/81)*P4 + real_t(20.0/27)*P3 + real_t(10.0/27)*P2 + real_t(-4.0/81)*P1);
451 _m.property(ep_nv_, _eh).first = vhl;
452 _m.property(ep_nv_, _eh).second = vhr;
456 void boundary_split( MeshType& _m,
const typename MeshType::FaceHandle& _fh )
458 assert( _m.is_boundary(_fh) );
460 typename MeshType::VertexHandle vhl, vhr;
461 typename MeshType::FaceEdgeIter fe_it;
462 typename MeshType::HalfedgeHandle heh;
465 for( fe_it=_m.fe_iter( _fh ); fe_it.is_valid() && !_m.is_boundary( *fe_it ); ++fe_it ) {};
468 vhl = _m.property(ep_nv_, *fe_it).first;
469 vhr = _m.property(ep_nv_, *fe_it).second;
484 heh = _m.halfedge_handle(*fe_it, _m.is_boundary(_m.halfedge_handle(*fe_it,0)));
486 typename MeshType::HalfedgeHandle pl_P3;
489 boundary_split( _m, heh, vhl );
490 pl_P3 = _m.next_halfedge_handle( heh );
491 boundary_split( _m, heh );
494 boundary_split( _m, pl_P3, vhr );
495 boundary_split( _m, pl_P3 );
497 assert( _m.is_boundary( vhl ) && _m.halfedge_handle(vhl).is_valid() );
498 assert( _m.is_boundary( vhr ) && _m.halfedge_handle(vhr).is_valid() );
501 void boundary_split(MeshType& _m,
502 const typename MeshType::HalfedgeHandle& _heh,
503 const typename MeshType::VertexHandle& _vh)
505 assert( _m.is_boundary( _m.edge_handle(_heh) ) );
507 typename MeshType::HalfedgeHandle
509 opp_heh( _m.opposite_halfedge_handle(_heh) ),
510 new_heh, opp_new_heh;
511 typename MeshType::VertexHandle to_vh(_m.to_vertex_handle(heh));
512 typename MeshType::HalfedgeHandle t_heh;
533 _m.next_halfedge_handle(t_heh) != opp_heh;
534 t_heh = _m.opposite_halfedge_handle(_m.next_halfedge_handle(t_heh)))
537 assert( _m.is_boundary( t_heh ) );
539 new_heh = _m.new_edge( _vh, to_vh );
540 opp_new_heh = _m.opposite_halfedge_handle(new_heh);
543 _m.set_next_halfedge_handle(t_heh, opp_new_heh);
544 _m.set_next_halfedge_handle(new_heh, _m.next_halfedge_handle(heh));
545 _m.set_next_halfedge_handle(heh, new_heh);
546 _m.set_next_halfedge_handle(opp_new_heh, opp_heh);
549 _m.set_face_handle(opp_new_heh, _m.face_handle(opp_heh));
552 _m.set_vertex_handle(heh, _vh);
555 _m.set_face_handle(new_heh, _m.face_handle(heh));
559 _m.set_halfedge_handle( to_vh, opp_new_heh );
562 _m.set_halfedge_handle( _vh, opp_heh );
565 void boundary_split( MeshType& _m,
566 const typename MeshType::HalfedgeHandle& _heh)
568 assert( _m.is_boundary( _m.opposite_halfedge_handle( _heh ) ) );
570 typename MeshType::HalfedgeHandle
572 n_heh(_m.next_halfedge_handle(heh));
574 typename MeshType::VertexHandle
575 to_vh(_m.to_vertex_handle(heh));
577 typename MeshType::HalfedgeHandle
578 heh2(_m.new_edge(to_vh,
579 _m.to_vertex_handle(_m.next_halfedge_handle(n_heh)))),
580 heh3(_m.opposite_halfedge_handle(heh2));
582 typename MeshType::FaceHandle
583 new_fh(_m.new_face()),
584 fh(_m.face_handle(heh));
587 _m.set_face_handle(heh, new_fh);
588 _m.set_face_handle(heh2, new_fh);
589 _m.set_next_halfedge_handle(heh2, _m.next_halfedge_handle(_m.next_halfedge_handle(n_heh)));
590 _m.set_next_halfedge_handle(heh, heh2);
591 _m.set_face_handle( _m.next_halfedge_handle(heh2), new_fh);
593 _m.set_next_halfedge_handle(heh3, n_heh);
594 _m.set_next_halfedge_handle(_m.next_halfedge_handle(n_heh), heh3);
595 _m.set_face_handle(heh3, fh);
597 _m.set_halfedge_handle( fh, n_heh);
598 _m.set_halfedge_handle(new_fh, heh);
608 typename MeshType::VertexHandle> > ep_nv_;
618 #endif // OPENMESH_SUBDIVIDER_UNIFORM_SQRT3T_HH
bool cleanup(MeshType &_m)
Cleanup mesh after usage, e.g. remove added properties.
const char * name() const
Return name of subdivision algorithm.
bool is_valid() const
The handle is valid iff the index is not equal to -1.
bool subdivide(MeshType &_m, size_t _n, const bool _update_points=true)
Subdivide mesh _m _n times.
bool prepare(MeshType &_m)
Prepare mesh, e.g. add properties.
void init_weights(size_t _max_valence=50)
Pre-compute weights.