51 #define ISOTROPICREMESHER_C 53 #include "IsotropicRemesherT.hh" 55 #include <ACG/Geometry/Algorithms.hh> 58 #include <ACG/Geometry/bsp/TriangleBSPT.hh> 61 template<
class MeshT >
64 const double low = (4.0 / 5.0) * _targetEdgeLength;
65 const double high = (4.0 / 3.0) * _targetEdgeLength;
67 MeshT meshCopy = _mesh;
70 for (
int i=0; i < 10; i++){
72 prgEmt_->sendProgressSignal(10*i + 0);
75 splitLongEdges(_mesh, high);
77 prgEmt_->sendProgressSignal(10*i + 2);
80 collapseShortEdges(_mesh, low, high);
82 prgEmt_->sendProgressSignal(10*i + 4);
85 equalizeValences(_mesh);
87 prgEmt_->sendProgressSignal(10*i + 6);
90 tangentialRelaxation(_mesh);
92 prgEmt_->sendProgressSignal(10*i + 8);
95 projectToSurface(_mesh, meshCopy, triangleBSP);
97 prgEmt_->sendProgressSignal(10*i + 10);
103 template<
class MeshT >
110 triangle_bsp->
reserve(_mesh.n_faces());
112 typename MeshT::FIter f_it = _mesh.faces_begin();
113 typename MeshT::FIter f_end = _mesh.faces_end();
115 for (; f_it!=f_end; ++f_it)
118 triangle_bsp->
build(10, 100);
125 template<
class MeshT >
129 const double _maxEdgeLengthSqr = _maxEdgeLength * _maxEdgeLength;
131 typename MeshT::EdgeIter e_it;
132 typename MeshT::EdgeIter e_end = _mesh.edges_end();
135 for (e_it = _mesh.edges_begin(); e_it != e_end; ++e_it){
136 const typename MeshT::HalfedgeHandle & hh = _mesh.halfedge_handle( *e_it, 0 );
138 const typename MeshT::VertexHandle & v0 = _mesh.from_vertex_handle(hh);
139 const typename MeshT::VertexHandle & v1 = _mesh.to_vertex_handle(hh);
141 typename MeshT::Point vec = _mesh.point(v1) - _mesh.point(v0);
144 if ( vec.sqrnorm() > _maxEdgeLengthSqr ){
146 const typename MeshT::Point midPoint = _mesh.point(v0) + ( 0.5 * vec );
149 typename MeshT::VertexHandle vh = _mesh.add_vertex( midPoint );
151 bool hadFeature = _mesh.status(*e_it).feature();
153 _mesh.split(*e_it, vh);
157 typename MeshT::VOHIter vh_it;
158 for (vh_it = _mesh.voh_iter(vh); vh_it.is_valid(); ++vh_it)
159 if ( _mesh.to_vertex_handle(*vh_it) == v0 || _mesh.to_vertex_handle(*vh_it) == v1 )
160 _mesh.status( _mesh.edge_handle( *vh_it ) ).set_feature(
true );
167 template<
class MeshT >
170 const double _minEdgeLengthSqr = _minEdgeLength * _minEdgeLength;
171 const double _maxEdgeLengthSqr = _maxEdgeLength * _maxEdgeLength;
175 if ( !_mesh.get_property_handle(checked,
"Checked Property") )
176 _mesh.add_property(checked,
"Checked Property" );
179 typename MeshT::ConstEdgeIter e_it;
180 typename MeshT::ConstEdgeIter e_end = _mesh.edges_end();
182 for (e_it = _mesh.edges_begin(); e_it != e_end; ++e_it)
183 _mesh.property(checked, *e_it) =
false;
186 bool finished =
false;
192 for (e_it = _mesh.edges_begin(); e_it != _mesh.edges_end() ; ++e_it){
194 if ( _mesh.property(checked, *e_it) )
197 _mesh.property(checked, *e_it) =
true;
199 const typename MeshT::HalfedgeHandle & hh = _mesh.halfedge_handle( *e_it, 0 );
201 const typename MeshT::VertexHandle & v0 = _mesh.from_vertex_handle(hh);
202 const typename MeshT::VertexHandle & v1 = _mesh.to_vertex_handle(hh);
204 const typename MeshT::Point vec = _mesh.point(v1) - _mesh.point(v0);
206 const double edgeLength = vec.sqrnorm();
209 if ( (edgeLength < _minEdgeLengthSqr) && (edgeLength > DBL_EPSILON) ){
212 const typename MeshT::Point & B = _mesh.point(v1);
214 bool collapse_ok =
true;
216 for(
typename MeshT::VOHIter vh_it = _mesh.voh_iter(v0); vh_it.is_valid(); ++vh_it)
217 if ( (( B - _mesh.point( _mesh.to_vertex_handle(*vh_it) ) ).sqrnorm() > _maxEdgeLengthSqr )
218 || ( _mesh.status( _mesh.edge_handle( *vh_it ) ).feature())
219 || ( _mesh.is_boundary( _mesh.edge_handle( *vh_it ) ) )){
224 if( collapse_ok && _mesh.is_collapse_ok(hh) ) {
225 _mesh.collapse( hh );
235 _mesh.remove_property(checked);
237 _mesh.garbage_collection();
240 template<
class MeshT >
244 typename MeshT::EdgeIter e_it;
245 typename MeshT::EdgeIter e_end = _mesh.edges_end();
247 for (e_it = _mesh.edges_sbegin(); e_it != e_end; ++e_it){
249 if ( !_mesh.is_flip_ok(*e_it) )
continue;
250 if ( _mesh.status( *e_it ).feature() )
continue;
253 const typename MeshT::HalfedgeHandle & h0 = _mesh.halfedge_handle( *e_it, 0 );
254 const typename MeshT::HalfedgeHandle & h1 = _mesh.halfedge_handle( *e_it, 1 );
256 if (h0.is_valid() && h1.is_valid())
258 if (_mesh.face_handle(h0).is_valid() && _mesh.face_handle(h1).is_valid()){
260 const typename MeshT::VertexHandle & a = _mesh.to_vertex_handle(h0);
261 const typename MeshT::VertexHandle & b = _mesh.to_vertex_handle(h1);
262 const typename MeshT::VertexHandle & c = _mesh.to_vertex_handle(_mesh.next_halfedge_handle(h0));
263 const typename MeshT::VertexHandle & d = _mesh.to_vertex_handle(_mesh.next_halfedge_handle(h1));
265 const int deviation_pre = abs((
int)(_mesh.valence(a) - targetValence(_mesh, a)))
266 +abs((
int)(_mesh.valence(b) - targetValence(_mesh, b)))
267 +abs((
int)(_mesh.valence(c) - targetValence(_mesh, c)))
268 +abs((
int)(_mesh.valence(d) - targetValence(_mesh, d)));
271 const int deviation_post = abs((
int)(_mesh.valence(a) - targetValence(_mesh, a)))
272 +abs((
int)(_mesh.valence(b) - targetValence(_mesh, b)))
273 +abs((
int)(_mesh.valence(c) - targetValence(_mesh, c)))
274 +abs((
int)(_mesh.valence(d) - targetValence(_mesh, d)));
276 if (deviation_pre <= deviation_post)
283 template<
class MeshT >
287 if (isBoundary(_mesh,_vh))
293 template<
class MeshT >
297 typename MeshT::ConstVertexOHalfedgeIter voh_it;
299 for (voh_it = _mesh.voh_iter( _vh ); voh_it.is_valid(); ++voh_it )
300 if ( _mesh.is_boundary( _mesh.edge_handle( *voh_it ) ) )
306 template<
class MeshT >
310 typename MeshT::ConstVertexOHalfedgeIter voh_it;
312 for (voh_it = _mesh.voh_iter( _vh ); voh_it.is_valid(); ++voh_it )
313 if ( _mesh.status( _mesh.edge_handle( *voh_it ) ).feature() )
319 template<
class MeshT >
322 _mesh.update_normals();
326 if ( !_mesh.get_property_handle(q,
"q Property") )
327 _mesh.add_property(q,
"q Property" );
329 typename MeshT::ConstVertexIter v_it;
330 typename MeshT::ConstVertexIter v_end = _mesh.vertices_end();
333 for (v_it = _mesh.vertices_sbegin(); v_it != v_end; ++v_it){
335 typename MeshT::Point tmp(0.0, 0.0, 0.0);
338 typename MeshT::VVIter vv_it;
339 for (vv_it = _mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it){
340 tmp += _mesh.point(*vv_it);
347 _mesh.property(q, *v_it) = tmp;
351 for (v_it = _mesh.vertices_sbegin(); v_it != v_end; ++v_it){
352 if ( !isBoundary(_mesh, *v_it) && !isFeature(_mesh, *v_it) )
353 _mesh.set_point(*v_it, _mesh.property(q, *v_it) + (_mesh.normal(*v_it)| (_mesh.point(*v_it) - _mesh.property(q, *v_it) ) ) * _mesh.normal(*v_it));
356 _mesh.remove_property(q);
359 template <
class MeshT>
360 template <
class SpatialSearchT>
361 typename MeshT::Point
363 const typename MeshT::Point& _point,
364 typename MeshT::FaceHandle& _fh,
365 SpatialSearchT* _ssearch,
369 typename MeshT::Point p_best = _mesh.point(_mesh.vertex_handle(0));
370 typename MeshT::Scalar d_best = (_point-p_best).sqrnorm();
372 typename MeshT::FaceHandle fh_best;
377 typename MeshT::ConstFaceIter cf_it = _mesh.faces_begin();
378 typename MeshT::ConstFaceIter cf_end = _mesh.faces_end();
380 for(; cf_it != cf_end; ++cf_it)
382 typename MeshT::ConstFaceVertexIter cfv_it = _mesh.cfv_iter(*cf_it);
384 const typename MeshT::Point& pt0 = _mesh.point( *cfv_it);
385 const typename MeshT::Point& pt1 = _mesh.point( *(++cfv_it));
386 const typename MeshT::Point& pt2 = _mesh.point( *(++cfv_it));
388 typename MeshT::Point ptn;
390 typename MeshT::Scalar d = ACG::Geometry::distPointTriangleSquared( _point,
396 if( d < d_best && d >= 0.0)
410 *_dbest = sqrt(d_best);
417 typename MeshT::FaceHandle fh = _ssearch->nearest(_point).handle;
418 typename MeshT::CFVIter fv_it = _mesh.cfv_iter(fh);
420 const typename MeshT::Point& pt0 = _mesh.point( *( fv_it));
421 const typename MeshT::Point& pt1 = _mesh.point( *(++fv_it));
422 const typename MeshT::Point& pt2 = _mesh.point( *(++fv_it));
425 d_best = ACG::Geometry::distPointTriangleSquared(_point, pt0, pt1, pt2, p_best);
432 *_dbest = sqrt(d_best);
439 template<
class MeshT >
440 template<
class SpatialSearchT >
444 typename MeshT::VertexIter v_it;
445 typename MeshT::VertexIter v_end = _mesh.vertices_end();
447 for (v_it = _mesh.vertices_begin(); v_it != v_end; ++v_it){
449 if (isBoundary(_mesh, *v_it))
continue;
450 if ( isFeature(_mesh, *v_it))
continue;
452 typename MeshT::Point p = _mesh.point(*v_it);
453 typename MeshT::FaceHandle fhNear;
456 typename MeshT::Point pNear = findNearestPoint(_original, p, fhNear, _ssearch, &distance);
458 _mesh.set_point(*v_it, pNear);
int targetValence(MeshT &_mesh, const typename MeshT::VertexHandle &_vh)
returns 4 for boundary vertices and 6 otherwise
void reserve(size_t _n)
Reserve memory for _n entries.
void push_back(Handle _h)
Add a handle to the BSP.
void collapseShortEdges(MeshT &_mesh, const double _minEdgeLength, const double _maxEdgeLength)
collapse edges shorter than minEdgeLength if collapsing doesn't result in new edge longer than maxEdg...
void build(unsigned int _max_handles, unsigned int _max_depth)
void splitLongEdges(MeshT &_mesh, const double _maxEdgeLength)
performs edge splits until all edges are shorter than the threshold
void remesh(MeshT &_mesh, const double _targetEdgeLength)
do the remeshing