54 #ifndef OPENMESH_SUBDIVIDER_UNIFORM_LOOPT_HH 55 #define OPENMESH_SUBDIVIDER_UNIFORM_LOOPT_HH 60 #include <OpenMesh/Core/System/config.hh> 62 #include <OpenMesh/Core/Utils/vector_cast.hh> 63 #include <OpenMesh/Core/Utils/Property.hh> 66 #if defined(OM_CC_MIPS) 76 namespace Subdivider {
90 template <
typename MeshType,
typename RealType =
double>
95 typedef RealType real_t;
96 typedef MeshType mesh_t;
99 typedef std::pair< real_t, real_t > weight_t;
100 typedef std::vector< std::pair<real_t,real_t> > weights_t;
105 LoopT(
void) : parent_t(), _1over8( 1.0/8.0 ), _3over8( 3.0/8.0 )
109 LoopT( mesh_t& _m ) : parent_t(_m), _1over8( 1.0/8.0 ), _3over8( 3.0/8.0 )
119 const char *
name()
const {
return "Uniform Loop"; }
125 weights_.resize(_max_valence);
126 std::generate(weights_.begin(), weights_.end(),
compute_weight());
135 _m.add_property( vp_pos_ );
136 _m.add_property( ep_pos_ );
143 _m.remove_property( vp_pos_ );
144 _m.remove_property( ep_pos_ );
149 bool subdivide( mesh_t& _m,
size_t _n,
const bool _update_points =
true)
154 typename mesh_t::FaceIter fit, f_end;
155 typename mesh_t::EdgeIter eit, e_end;
156 typename mesh_t::VertexIter vit;
159 for (
size_t i=0; i < _n; ++i)
164 for (vit = _m.vertices_begin(); vit != _m.vertices_end(); ++vit) {
170 for (eit=_m.edges_begin(); eit != _m.edges_end(); ++eit)
171 compute_midpoint( _m, *eit );
177 e_end = _m.edges_end();
178 for (eit=_m.edges_begin(); eit != e_end; ++eit)
179 split_edge(_m, *eit );
185 f_end = _m.faces_end();
186 for (fit = _m.faces_begin(); fit != f_end; ++fit)
187 split_face(_m, *fit );
191 for ( vit = _m.vertices_begin();
192 vit != _m.vertices_end(); ++vit) {
193 _m.set_point(*vit, _m.property( vp_pos_, *vit ) );
198 #if defined(_DEBUG) || defined(DEBUG) 214 weight_t operator() (
void)
216 #if !defined(OM_CC_MIPS) 225 double inv_v = 1.0/double(valence);
226 double t = (3.0 + 2.0 * cos( 2.0 * M_PI * inv_v) );
227 double alpha = (40.0 - t * t)/64.0;
229 return weight_t( static_cast<real_t>(1.0-alpha), static_cast<real_t>(inv_v*alpha) );
231 return weight_t(static_cast<real_t>(0.0), static_cast<real_t>(0.0));
238 void split_face(mesh_t& _m,
const typename mesh_t::FaceHandle& _fh)
240 typename mesh_t::HalfedgeHandle
241 heh1(_m.halfedge_handle(_fh)),
242 heh2(_m.next_halfedge_handle(_m.next_halfedge_handle(heh1))),
243 heh3(_m.next_halfedge_handle(_m.next_halfedge_handle(heh2)));
246 corner_cutting( _m, heh1 );
247 corner_cutting( _m, heh2 );
248 corner_cutting( _m, heh3 );
252 void corner_cutting(mesh_t& _m,
const typename mesh_t::HalfedgeHandle& _he)
255 typename mesh_t::HalfedgeHandle
258 heh6(_m.next_halfedge_handle(heh1));
261 for (; _m.next_halfedge_handle(_m.next_halfedge_handle(heh5)) != heh1;
262 heh5 = _m.next_halfedge_handle(heh5))
265 typename mesh_t::VertexHandle
266 vh1 = _m.to_vertex_handle(heh1),
267 vh2 = _m.to_vertex_handle(heh5);
269 typename mesh_t::HalfedgeHandle
270 heh2(_m.next_halfedge_handle(heh5)),
271 heh3(_m.new_edge( vh1, vh2)),
272 heh4(_m.opposite_halfedge_handle(heh3));
288 typename mesh_t::FaceHandle fh_old(_m.face_handle(heh6));
289 typename mesh_t::FaceHandle fh_new(_m.new_face());
293 _m.set_next_halfedge_handle(heh4, heh6);
294 _m.set_next_halfedge_handle(heh5, heh4);
296 _m.set_face_handle(heh4, fh_old);
297 _m.set_face_handle(heh5, fh_old);
298 _m.set_face_handle(heh6, fh_old);
299 _m.set_halfedge_handle(fh_old, heh4);
302 _m.set_next_halfedge_handle(heh1, heh3);
303 _m.set_next_halfedge_handle(heh3, heh2);
305 _m.set_face_handle(heh1, fh_new);
306 _m.set_face_handle(heh2, fh_new);
307 _m.set_face_handle(heh3, fh_new);
309 _m.set_halfedge_handle(fh_new, heh1);
313 void split_edge(mesh_t& _m,
const typename mesh_t::EdgeHandle& _eh)
315 typename mesh_t::HalfedgeHandle
316 heh = _m.halfedge_handle(_eh, 0),
317 opp_heh = _m.halfedge_handle(_eh, 1);
319 typename mesh_t::HalfedgeHandle new_heh, opp_new_heh, t_heh;
320 typename mesh_t::VertexHandle vh;
321 typename mesh_t::VertexHandle vh1(_m.to_vertex_handle(heh));
322 typename mesh_t::Point midP(_m.point(_m.to_vertex_handle(heh)));
323 midP += _m.point(_m.to_vertex_handle(opp_heh));
324 midP *=
static_cast<typename mesh_t::Point::value_type
>(0.5);
327 vh = _m.new_vertex( midP );
330 _m.property( vp_pos_, vh ) = _m.property( ep_pos_, _eh );
334 if (_m.is_boundary(_eh))
337 _m.next_halfedge_handle(t_heh) != opp_heh;
338 t_heh = _m.opposite_halfedge_handle(_m.next_halfedge_handle(t_heh)))
343 for (t_heh = _m.next_halfedge_handle(opp_heh);
344 _m.next_halfedge_handle(t_heh) != opp_heh;
345 t_heh = _m.next_halfedge_handle(t_heh) )
349 new_heh = _m.new_edge(vh, vh1);
350 opp_new_heh = _m.opposite_halfedge_handle(new_heh);
351 _m.set_vertex_handle( heh, vh );
353 _m.set_next_halfedge_handle(t_heh, opp_new_heh);
354 _m.set_next_halfedge_handle(new_heh, _m.next_halfedge_handle(heh));
355 _m.set_next_halfedge_handle(heh, new_heh);
356 _m.set_next_halfedge_handle(opp_new_heh, opp_heh);
358 if (_m.face_handle(opp_heh).is_valid())
360 _m.set_face_handle(opp_new_heh, _m.face_handle(opp_heh));
361 _m.set_halfedge_handle(_m.face_handle(opp_new_heh), opp_new_heh);
364 _m.set_face_handle( new_heh, _m.face_handle(heh) );
365 _m.set_halfedge_handle( vh, new_heh);
366 _m.set_halfedge_handle( _m.face_handle(heh), heh );
367 _m.set_halfedge_handle( vh1, opp_new_heh );
370 _m.adjust_outgoing_halfedge( vh );
371 _m.adjust_outgoing_halfedge( vh1 );
376 void compute_midpoint(mesh_t& _m,
const typename mesh_t::EdgeHandle& _eh)
378 #define V( X ) vector_cast< typename mesh_t::Normal >( X ) 379 typename mesh_t::HalfedgeHandle heh, opp_heh;
381 heh = _m.halfedge_handle( _eh, 0);
382 opp_heh = _m.halfedge_handle( _eh, 1);
384 typename mesh_t::Point
385 pos(_m.point(_m.to_vertex_handle(heh)));
387 pos += V( _m.point(_m.to_vertex_handle(opp_heh)) );
390 if (_m.is_boundary(_eh) )
392 pos *=
static_cast<typename MeshType::Point::value_type
>(0.5);
397 pos += V(_m.point(_m.to_vertex_handle(_m.next_halfedge_handle(heh))));
398 pos += V(_m.point(_m.to_vertex_handle(_m.next_halfedge_handle(opp_heh))));
401 _m.property( ep_pos_, _eh ) = pos;
405 void smooth(mesh_t& _m,
const typename mesh_t::VertexHandle& _vh)
407 typename mesh_t::Point pos(0.0,0.0,0.0);
409 if (_m.is_boundary(_vh) )
411 typename mesh_t::HalfedgeHandle heh, prev_heh;
412 heh = _m.halfedge_handle( _vh );
414 if ( heh.is_valid() )
416 assert( _m.is_boundary( _m.edge_handle( heh ) ) );
418 prev_heh = _m.prev_halfedge_handle( heh );
420 typename mesh_t::VertexHandle
421 to_vh = _m.to_vertex_handle( heh ),
422 from_vh = _m.from_vertex_handle( prev_heh );
425 pos = _m.point( _vh );
427 pos +=
vector_cast<
typename mesh_t::Normal >( _m.point( to_vh ) );
428 pos +=
vector_cast<
typename mesh_t::Normal >( _m.point( from_vh ) );
437 typedef typename mesh_t::Normal Vec;
438 typename mesh_t::VertexVertexIter vvit;
442 for (vvit=_m.vv_iter(_vh); vvit.is_valid(); ++vvit) {
446 pos *= weights_[valence].second;
447 pos += weights_[valence].first
451 _m.property( vp_pos_, _vh ) = pos;
461 const real_t _1over8;
462 const real_t _3over8;
472 #endif // OPENMESH_SUBDIVIDER_UNIFORM_COMPOSITELOOPT_HH defined 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.
const char * name() const
Return name of subdivision algorithm.
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 init_weights(size_t _max_valence=50)
Pre-compute weights.
bool subdivide(mesh_t &_m, size_t _n, const bool _update_points=true)
Subdivide mesh _m _n times.