59 #ifndef OPENMESH_SUBDIVIDER_UNIFORM_LOOPT_HH
60 #define OPENMESH_SUBDIVIDER_UNIFORM_LOOPT_HH
65 #include <OpenMesh/Core/System/config.hh>
67 #include <OpenMesh/Core/Utils/vector_cast.hh>
68 #include <OpenMesh/Core/Utils/Property.hh>
71 #if defined(OM_CC_MIPS)
81 namespace Subdivider {
95 template <
typename MeshType,
typename RealType =
float>
100 typedef RealType real_t;
101 typedef MeshType mesh_t;
104 typedef std::pair< real_t, real_t > weight_t;
105 typedef std::vector< std::pair<real_t,real_t> > weights_t;
110 LoopT(
void) : parent_t(), _1over8( 1.0/8.0 ), _3over8( 3.0/8.0 )
114 LoopT( mesh_t& _m ) : parent_t(_m), _1over8( 1.0/8.0 ), _3over8( 3.0/8.0 )
124 const char *
name()
const {
return "Uniform Loop"; }
130 weights_.resize(_max_valence);
131 std::generate(weights_.begin(), weights_.end(),
compute_weight());
140 _m.add_property( vp_pos_ );
141 _m.add_property( ep_pos_ );
148 _m.remove_property( vp_pos_ );
149 _m.remove_property( ep_pos_ );
154 bool subdivide( mesh_t& _m,
size_t _n,
const bool _update_points =
true)
159 typename mesh_t::FaceIter fit, f_end;
160 typename mesh_t::EdgeIter eit, e_end;
161 typename mesh_t::VertexIter vit;
164 for (
size_t i=0; i < _n; ++i)
169 for (vit = _m.vertices_begin(); vit != _m.vertices_end(); ++vit) {
175 for (eit=_m.edges_begin(); eit != _m.edges_end(); ++eit)
176 compute_midpoint( _m, *eit );
182 e_end = _m.edges_end();
183 for (eit=_m.edges_begin(); eit != e_end; ++eit)
184 split_edge(_m, *eit );
190 f_end = _m.faces_end();
191 for (fit = _m.faces_begin(); fit != f_end; ++fit)
192 split_face(_m, *fit );
196 for ( vit = _m.vertices_begin();
197 vit != _m.vertices_end(); ++vit) {
198 _m.set_point(*vit, _m.property( vp_pos_, *vit ) );
203 #if defined(_DEBUG) || defined(DEBUG)
219 weight_t operator() (
void)
221 #if !defined(OM_CC_MIPS)
230 double inv_v = 1.0/double(valence);
231 double t = (3.0 + 2.0 * cos( 2.0 * M_PI * inv_v) );
232 double alpha = (40.0 - t * t)/64.0;
234 return weight_t( 1.0-alpha, inv_v*alpha);
236 return weight_t(0.0, 0.0);
243 void split_face(mesh_t& _m,
const typename mesh_t::FaceHandle& _fh)
245 typename mesh_t::HalfedgeHandle
246 heh1(_m.halfedge_handle(_fh)),
247 heh2(_m.next_halfedge_handle(_m.next_halfedge_handle(heh1))),
248 heh3(_m.next_halfedge_handle(_m.next_halfedge_handle(heh2)));
251 corner_cutting( _m, heh1 );
252 corner_cutting( _m, heh2 );
253 corner_cutting( _m, heh3 );
257 void corner_cutting(mesh_t& _m,
const typename mesh_t::HalfedgeHandle& _he)
260 typename mesh_t::HalfedgeHandle
263 heh6(_m.next_halfedge_handle(heh1));
266 for (; _m.next_halfedge_handle(_m.next_halfedge_handle(heh5)) != heh1;
267 heh5 = _m.next_halfedge_handle(heh5))
270 typename mesh_t::VertexHandle
271 vh1 = _m.to_vertex_handle(heh1),
272 vh2 = _m.to_vertex_handle(heh5);
274 typename mesh_t::HalfedgeHandle
275 heh2(_m.next_halfedge_handle(heh5)),
276 heh3(_m.new_edge( vh1, vh2)),
277 heh4(_m.opposite_halfedge_handle(heh3));
293 typename mesh_t::FaceHandle fh_old(_m.face_handle(heh6));
294 typename mesh_t::FaceHandle fh_new(_m.new_face());
298 _m.set_next_halfedge_handle(heh4, heh6);
299 _m.set_next_halfedge_handle(heh5, heh4);
301 _m.set_face_handle(heh4, fh_old);
302 _m.set_face_handle(heh5, fh_old);
303 _m.set_face_handle(heh6, fh_old);
304 _m.set_halfedge_handle(fh_old, heh4);
307 _m.set_next_halfedge_handle(heh1, heh3);
308 _m.set_next_halfedge_handle(heh3, heh2);
310 _m.set_face_handle(heh1, fh_new);
311 _m.set_face_handle(heh2, fh_new);
312 _m.set_face_handle(heh3, fh_new);
314 _m.set_halfedge_handle(fh_new, heh1);
318 void split_edge(mesh_t& _m,
const typename mesh_t::EdgeHandle& _eh)
320 typename mesh_t::HalfedgeHandle
321 heh = _m.halfedge_handle(_eh, 0),
322 opp_heh = _m.halfedge_handle(_eh, 1);
324 typename mesh_t::HalfedgeHandle new_heh, opp_new_heh, t_heh;
325 typename mesh_t::VertexHandle vh;
326 typename mesh_t::VertexHandle vh1(_m.to_vertex_handle(heh));
327 typename mesh_t::Point midP(_m.point(_m.to_vertex_handle(heh)));
328 midP += _m.point(_m.to_vertex_handle(opp_heh));
332 vh = _m.new_vertex( midP );
335 _m.property( vp_pos_, vh ) = _m.property( ep_pos_, _eh );
339 if (_m.is_boundary(_eh))
342 _m.next_halfedge_handle(t_heh) != opp_heh;
343 t_heh = _m.opposite_halfedge_handle(_m.next_halfedge_handle(t_heh)))
348 for (t_heh = _m.next_halfedge_handle(opp_heh);
349 _m.next_halfedge_handle(t_heh) != opp_heh;
350 t_heh = _m.next_halfedge_handle(t_heh) )
354 new_heh = _m.new_edge(vh, vh1);
355 opp_new_heh = _m.opposite_halfedge_handle(new_heh);
356 _m.set_vertex_handle( heh, vh );
358 _m.set_next_halfedge_handle(t_heh, opp_new_heh);
359 _m.set_next_halfedge_handle(new_heh, _m.next_halfedge_handle(heh));
360 _m.set_next_halfedge_handle(heh, new_heh);
361 _m.set_next_halfedge_handle(opp_new_heh, opp_heh);
363 if (_m.face_handle(opp_heh).is_valid())
365 _m.set_face_handle(opp_new_heh, _m.face_handle(opp_heh));
366 _m.set_halfedge_handle(_m.face_handle(opp_new_heh), opp_new_heh);
369 _m.set_face_handle( new_heh, _m.face_handle(heh) );
370 _m.set_halfedge_handle( vh, new_heh);
371 _m.set_halfedge_handle( _m.face_handle(heh), heh );
372 _m.set_halfedge_handle( vh1, opp_new_heh );
375 _m.adjust_outgoing_halfedge( vh );
376 _m.adjust_outgoing_halfedge( vh1 );
381 void compute_midpoint(mesh_t& _m,
const typename mesh_t::EdgeHandle& _eh)
383 #define V( X ) vector_cast< typename mesh_t::Normal >( X )
384 typename mesh_t::HalfedgeHandle heh, opp_heh;
386 heh = _m.halfedge_handle( _eh, 0);
387 opp_heh = _m.halfedge_handle( _eh, 1);
389 typename mesh_t::Point
390 pos(_m.point(_m.to_vertex_handle(heh)));
392 pos += V( _m.point(_m.to_vertex_handle(opp_heh)) );
395 if (_m.is_boundary(_eh) )
402 pos += V(_m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh))));
403 pos += V(_m.point(_m.to_vertex_handle(_m.next_halfedge_handle(opp_heh))));
406 _m.property( ep_pos_, _eh ) = pos;
410 void smooth(mesh_t& _m,
const typename mesh_t::VertexHandle& _vh)
412 typename mesh_t::Point pos(0.0,0.0,0.0);
414 if (_m.is_boundary(_vh) )
416 typename mesh_t::HalfedgeHandle heh, prev_heh;
417 heh = _m.halfedge_handle( _vh );
419 if ( heh.is_valid() )
421 assert( _m.is_boundary( _m.edge_handle( heh ) ) );
423 prev_heh = _m.prev_halfedge_handle( heh );
425 typename mesh_t::VertexHandle
426 to_vh = _m.to_vertex_handle( heh ),
427 from_vh = _m.from_vertex_handle( prev_heh );
430 pos = _m.point( _vh );
443 typename mesh_t::VertexVertexIter vvit;
447 for (vvit=_m.vv_iter(_vh); vvit.is_valid(); ++vvit) {
451 pos *= weights_[valence].second;
452 pos += weights_[valence].first
456 _m.property( vp_pos_, _vh ) = pos;
466 const real_t _1over8;
467 const real_t _3over8;
477 #endif // OPENMESH_SUBDIVIDER_UNIFORM_COMPOSITELOOPT_HH defined
bool prepare(mesh_t &_m)
Prepare mesh, e.g. add properties.
bool cleanup(mesh_t &_m)
Cleanup mesh after usage, e.g. remove added properties.
void vector_cast(const src_t &_src, dst_t &_dst, GenProg::Int2Type< n >)
Cast vector type to another vector type by copying the vector elements.
bool subdivide(mesh_t &_m, size_t _n, const bool _update_points=true)
Subdivide mesh _m _n times.
Add normals to mesh item (vertices/faces)
const char * name() const
Return name of subdivision algorithm.
void init_weights(size_t _max_valence=50)
Pre-compute weights.