IsotropicRemesherT.cc 15.4 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
/*===========================================================================*\
*                                                                            *
*                              OpenFlipper                                   *
Jan Möbius's avatar
Jan Möbius committed
4
*      Copyright (C) 2001-2014 by Computer Graphics Group, RWTH Aachen       *
Jan Möbius's avatar
Jan Möbius committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
*                                                                            *
\*===========================================================================*/

Jan Möbius's avatar
 
Jan Möbius committed
43
44
45
46
47
48
49
50

#define ISOTROPICREMESHER_C

#include "IsotropicRemesherT.hh"

#include <ACG/Geometry/Algorithms.hh>

// -------------------- BSP
51
#include <ACG/Geometry/bsp/TriangleBSPT.hh>
Jan Möbius's avatar
 
Jan Möbius committed
52
53
54
55
56
57
58
59
60
61
62
63

/// do the remeshing
template< class MeshT >
void IsotropicRemesher< MeshT >::remesh( MeshT& _mesh, const double _targetEdgeLength )
{
  const double low  = (4.0 / 5.0) * _targetEdgeLength;
  const double high = (4.0 / 3.0) * _targetEdgeLength;

  MeshT meshCopy = _mesh;
  OpenMeshTriangleBSPT< MeshT >* triangleBSP = getTriangleBSP(meshCopy);

  for (int i=0; i < 10; i++){
64
65
    if (prgEmt_)
      prgEmt_->sendProgressSignal(10*i + 0);
Jan Möbius's avatar
 
Jan Möbius committed
66
// std::cerr << "Iteration = " << i << std::endl;
Anne Kathrein's avatar
Anne Kathrein committed
67
68
    for(int j = 0; j<15; j++)
        splitLongEdges(_mesh, high);
69
70
71
    if (prgEmt_)
      prgEmt_->sendProgressSignal(10*i + 2);
    
Jan Möbius's avatar
 
Jan Möbius committed
72
73
// std::cerr << "collapse" << std::endl;
    collapseShortEdges(_mesh, low, high);
74
75
76
    if (prgEmt_)
      prgEmt_->sendProgressSignal(10*i + 4);
    
Jan Möbius's avatar
 
Jan Möbius committed
77
78
// std::cerr << "equal" << std::endl;
    equalizeValences(_mesh);
79
80
81
    if (prgEmt_)
      prgEmt_->sendProgressSignal(10*i + 6);
    
Jan Möbius's avatar
 
Jan Möbius committed
82
83
// std::cerr << "relax" << std::endl;
    tangentialRelaxation(_mesh);
84
85
86
    if (prgEmt_)
      prgEmt_->sendProgressSignal(10*i + 8);
    
Jan Möbius's avatar
 
Jan Möbius committed
87
88
// std::cerr << "project" << std::endl;
    projectToSurface(_mesh, meshCopy, triangleBSP);
89
90
    if (prgEmt_)
      prgEmt_->sendProgressSignal(10*i + 10);
Jan Möbius's avatar
 
Jan Möbius committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  }

  delete triangleBSP;
}

template< class MeshT >
OpenMeshTriangleBSPT< MeshT >* IsotropicRemesher< MeshT >::getTriangleBSP(MeshT& _mesh)
{
  // create Triangle BSP
  OpenMeshTriangleBSPT< MeshT >* triangle_bsp = new OpenMeshTriangleBSPT< MeshT >( _mesh );

  // build Triangle BSP
  triangle_bsp->reserve(_mesh.n_faces());

  typename MeshT::FIter f_it  = _mesh.faces_begin();
  typename MeshT::FIter f_end = _mesh.faces_end();

  for (; f_it!=f_end; ++f_it)
Jan Möbius's avatar
Jan Möbius committed
109
    triangle_bsp->push_back( *f_it );
Jan Möbius's avatar
 
Jan Möbius committed
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

  triangle_bsp->build(10, 100);

  // return pointer to triangle bsp
  return triangle_bsp;
}

/// performs edge splits until all edges are shorter than the threshold
template< class MeshT >
void IsotropicRemesher< MeshT >::splitLongEdges( MeshT& _mesh, const double _maxEdgeLength )
{

  const double _maxEdgeLengthSqr = _maxEdgeLength * _maxEdgeLength;

  typename MeshT::EdgeIter e_it;
  typename MeshT::EdgeIter e_end = _mesh.edges_end();

  //iterate over all edges
  for (e_it = _mesh.edges_begin(); e_it != e_end; ++e_it){
Jan Möbius's avatar
Jan Möbius committed
129
    const typename MeshT::HalfedgeHandle & hh = _mesh.halfedge_handle( *e_it, 0 );
Jan Möbius's avatar
 
Jan Möbius committed
130
131
132
133
134
135
136
137
138
139
140
141

    const typename MeshT::VertexHandle & v0 = _mesh.from_vertex_handle(hh);
    const typename MeshT::VertexHandle & v1 = _mesh.to_vertex_handle(hh);

    typename MeshT::Point vec = _mesh.point(v1) - _mesh.point(v0);

    //edge to long?
    if ( vec.sqrnorm() > _maxEdgeLengthSqr ){

      const typename MeshT::Point midPoint = _mesh.point(v0) + ( 0.5 * vec );

      //split at midpoint
142
143
      typename MeshT::VertexHandle vh = _mesh.add_vertex( midPoint );
      
Jan Möbius's avatar
Jan Möbius committed
144
      bool hadFeature = _mesh.status(*e_it).feature();
145
      
Jan Möbius's avatar
Jan Möbius committed
146
      _mesh.split(*e_it, vh);
147
148
149
150
      
      if ( hadFeature ){
        
        typename MeshT::VOHIter vh_it;
Jan Möbius's avatar
Jan Möbius committed
151
152
153
        for (vh_it = _mesh.voh_iter(vh); vh_it.is_valid(); ++vh_it)
          if ( _mesh.to_vertex_handle(*vh_it) == v0 || _mesh.to_vertex_handle(*vh_it) == v1 )
            _mesh.status( _mesh.edge_handle( *vh_it ) ).set_feature( true );
154
      }
Jan Möbius's avatar
 
Jan Möbius committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    }
  }
}

/// collapse edges shorter than minEdgeLength if collapsing doesn't result in new edge longer than maxEdgeLength
template< class MeshT >
void IsotropicRemesher< MeshT >::collapseShortEdges( MeshT& _mesh, const double _minEdgeLength, const double _maxEdgeLength )
{
  const double _minEdgeLengthSqr = _minEdgeLength * _minEdgeLength;
  const double _maxEdgeLengthSqr = _maxEdgeLength * _maxEdgeLength;

  //add checked property
  OpenMesh::EPropHandleT< bool > checked;
  if ( !_mesh.get_property_handle(checked, "Checked Property") )
    _mesh.add_property(checked,"Checked Property" );

  //init property
  typename MeshT::ConstEdgeIter e_it;
  typename MeshT::ConstEdgeIter e_end = _mesh.edges_end();

  for (e_it = _mesh.edges_begin(); e_it != e_end; ++e_it)
Jan Möbius's avatar
Jan Möbius committed
176
    _mesh.property(checked, *e_it) = false;
Jan Möbius's avatar
 
Jan Möbius committed
177
178
179
180
181
182
183
184
185
186


  bool finished = false;

  while( !finished ){

    finished = true;

    for (e_it = _mesh.edges_begin(); e_it != _mesh.edges_end() ; ++e_it){
    
Jan Möbius's avatar
Jan Möbius committed
187
        if ( _mesh.property(checked, *e_it) )
Jan Möbius's avatar
 
Jan Möbius committed
188
189
          continue;
      
Jan Möbius's avatar
Jan Möbius committed
190
        _mesh.property(checked, *e_it) = true;
Jan Möbius's avatar
 
Jan Möbius committed
191

Jan Möbius's avatar
Jan Möbius committed
192
        const typename MeshT::HalfedgeHandle & hh = _mesh.halfedge_handle( *e_it, 0 );
Jan Möbius's avatar
 
Jan Möbius committed
193
194
195
      
        const typename MeshT::VertexHandle & v0 = _mesh.from_vertex_handle(hh);
        const typename MeshT::VertexHandle & v1 = _mesh.to_vertex_handle(hh);
196

Jan Möbius's avatar
 
Jan Möbius committed
197
        const typename MeshT::Point vec = _mesh.point(v1) - _mesh.point(v0);
198
199
200

        const double edgeLength = vec.sqrnorm();

Jan Möbius's avatar
Jan Möbius committed
201
202
        // edge too short but don't try to collapse edges that have length 0
        if ( (edgeLength < _minEdgeLengthSqr) && (edgeLength > DBL_EPSILON) ){
Jan Möbius's avatar
 
Jan Möbius committed
203
204
205
    
          //check if the collapse is ok
          const typename MeshT::Point & B = _mesh.point(v1);
206

Jan Möbius's avatar
 
Jan Möbius committed
207
208
          bool collapse_ok = true;
    
Jan Möbius's avatar
Jan Möbius committed
209
210
211
212
          for(typename MeshT::VOHIter vh_it = _mesh.voh_iter(v0); vh_it.is_valid(); ++vh_it)
            if ( (( B - _mesh.point( _mesh.to_vertex_handle(*vh_it) ) ).sqrnorm() > _maxEdgeLengthSqr )
                 || ( _mesh.status( _mesh.edge_handle( *vh_it ) ).feature())
                 || ( _mesh.is_boundary( _mesh.edge_handle( *vh_it ) ) )){
Jan Möbius's avatar
 
Jan Möbius committed
213
214
215
216
217
218
219
220
221
              collapse_ok = false;
              break;
            }
  
          if( collapse_ok && _mesh.is_collapse_ok(hh) ) {
            _mesh.collapse( hh );

            finished = false;
          }
222

Jan Möbius's avatar
 
Jan Möbius committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
        } 
    }

  }

  _mesh.remove_property(checked);

  _mesh.garbage_collection();
}

template< class MeshT >
void IsotropicRemesher< MeshT >::equalizeValences( MeshT& _mesh )
{

  typename MeshT::EdgeIter e_it;
  typename MeshT::EdgeIter e_end = _mesh.edges_end();

  for (e_it = _mesh.edges_sbegin(); e_it != e_end; ++e_it){

Jan Möbius's avatar
Jan Möbius committed
242
243
    if ( !_mesh.is_flip_ok(*e_it) ) continue;
    if ( _mesh.status( *e_it ).feature() ) continue;
244
    
Jan Möbius's avatar
 
Jan Möbius committed
245

Jan Möbius's avatar
Jan Möbius committed
246
247
    const typename MeshT::HalfedgeHandle & h0 = _mesh.halfedge_handle( *e_it, 0 );
    const typename MeshT::HalfedgeHandle & h1 = _mesh.halfedge_handle( *e_it, 1 );
Jan Möbius's avatar
 
Jan Möbius committed
248
249
250
251
252
253
254
255
256
257
258
259
260
261

    if (h0.is_valid() && h1.is_valid())

      if (_mesh.face_handle(h0).is_valid() && _mesh.face_handle(h1).is_valid()){
        //get vertices of corresponding faces
        const typename MeshT::VertexHandle & a = _mesh.to_vertex_handle(h0);
        const typename MeshT::VertexHandle & b = _mesh.to_vertex_handle(h1);
        const typename MeshT::VertexHandle & c = _mesh.to_vertex_handle(_mesh.next_halfedge_handle(h0));
        const typename MeshT::VertexHandle & d = _mesh.to_vertex_handle(_mesh.next_halfedge_handle(h1));

        const int deviation_pre =  abs((int)(_mesh.valence(a) - targetValence(_mesh, a)))
                                  +abs((int)(_mesh.valence(b) - targetValence(_mesh, b)))
                                  +abs((int)(_mesh.valence(c) - targetValence(_mesh, c)))
                                  +abs((int)(_mesh.valence(d) - targetValence(_mesh, d)));
Jan Möbius's avatar
Jan Möbius committed
262
        _mesh.flip(*e_it);
Jan Möbius's avatar
 
Jan Möbius committed
263
264
265
266
267
268
269

        const int deviation_post = abs((int)(_mesh.valence(a) - targetValence(_mesh, a)))
                                  +abs((int)(_mesh.valence(b) - targetValence(_mesh, b)))
                                  +abs((int)(_mesh.valence(c) - targetValence(_mesh, c)))
                                  +abs((int)(_mesh.valence(d) - targetValence(_mesh, d)));

        if (deviation_pre <= deviation_post)
Jan Möbius's avatar
Jan Möbius committed
270
          _mesh.flip(*e_it);
Jan Möbius's avatar
 
Jan Möbius committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
      }
  }
}

///returns 4 for boundary vertices and 6 otherwise
template< class MeshT > 
inline
int IsotropicRemesher< MeshT >::targetValence( MeshT& _mesh, const typename MeshT::VertexHandle& _vh ){

  if (isBoundary(_mesh,_vh))
    return 4;
  else
    return 6;
}

template< class MeshT > 
inline
bool IsotropicRemesher< MeshT >::isBoundary( MeshT& _mesh, const typename MeshT::VertexHandle& _vh ){

  typename MeshT::ConstVertexOHalfedgeIter voh_it;

Jan Möbius's avatar
Jan Möbius committed
292
293
  for (voh_it = _mesh.voh_iter( _vh ); voh_it.is_valid(); ++voh_it )
    if ( _mesh.is_boundary( _mesh.edge_handle( *voh_it ) ) )
Jan Möbius's avatar
 
Jan Möbius committed
294
295
296
297
298
      return true;

  return false;
}

299
300
301
302
303
304
template< class MeshT > 
inline
bool IsotropicRemesher< MeshT >::isFeature( MeshT& _mesh, const typename MeshT::VertexHandle& _vh ){

  typename MeshT::ConstVertexOHalfedgeIter voh_it;

Jan Möbius's avatar
Jan Möbius committed
305
306
  for (voh_it = _mesh.voh_iter( _vh ); voh_it.is_valid(); ++voh_it )
    if ( _mesh.status( _mesh.edge_handle( *voh_it ) ).feature() )
307
308
309
310
311
      return true;

  return false;
}

Jan Möbius's avatar
 
Jan Möbius committed
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
template< class MeshT >
void IsotropicRemesher< MeshT >::tangentialRelaxation( MeshT& _mesh )
{
  _mesh.update_normals();

  //add checked property
  OpenMesh::VPropHandleT< typename MeshT::Point > q;
  if ( !_mesh.get_property_handle(q, "q Property") )
    _mesh.add_property(q,"q Property" );

  typename MeshT::ConstVertexIter v_it;
  typename MeshT::ConstVertexIter v_end = _mesh.vertices_end();

  //first compute barycenters
  for (v_it = _mesh.vertices_sbegin(); v_it != v_end; ++v_it){

    typename MeshT::Point tmp(0.0, 0.0, 0.0);
    uint N = 0;

    typename MeshT::VVIter vv_it;
Jan Möbius's avatar
Jan Möbius committed
332
333
    for (vv_it = _mesh.vv_iter(*v_it); vv_it.is_valid(); ++vv_it){
      tmp += _mesh.point(*vv_it);
Jan Möbius's avatar
 
Jan Möbius committed
334
335
336
337
338
339
      N++;
    }

    if (N > 0)
      tmp /= (double) N;

Jan Möbius's avatar
Jan Möbius committed
340
    _mesh.property(q, *v_it) = tmp;
Jan Möbius's avatar
 
Jan Möbius committed
341
342
343
344
  }

  //move to new position
  for (v_it = _mesh.vertices_sbegin(); v_it != v_end; ++v_it){
Jan Möbius's avatar
Jan Möbius committed
345
346
    if ( !isBoundary(_mesh, *v_it) && !isFeature(_mesh, *v_it) )
      _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));
Jan Möbius's avatar
 
Jan Möbius committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
  }

  _mesh.remove_property(q);
}

template <class MeshT>
template <class SpatialSearchT>
typename MeshT::Point
IsotropicRemesher< MeshT >::findNearestPoint(const MeshT&                   _mesh,
                                          const typename MeshT::Point&   _point,
                                          typename MeshT::FaceHandle&    _fh,
                                          SpatialSearchT*                _ssearch,
                                          double*                        _dbest)
{

  typename MeshT::Point  p_best = _mesh.point(_mesh.vertex_handle(0));
  typename MeshT::Scalar d_best = (_point-p_best).sqrnorm();

  typename MeshT::FaceHandle fh_best;

  if( _ssearch == 0 )
  {
    // exhaustive search
    typename MeshT::ConstFaceIter cf_it  = _mesh.faces_begin();
    typename MeshT::ConstFaceIter cf_end = _mesh.faces_end();

    for(; cf_it != cf_end; ++cf_it)
    {
Jan Möbius's avatar
Jan Möbius committed
375
      typename MeshT::ConstFaceVertexIter cfv_it = _mesh.cfv_iter(*cf_it);
Jan Möbius's avatar
 
Jan Möbius committed
376

Jan Möbius's avatar
Jan Möbius committed
377
378
379
      const typename MeshT::Point& pt0 = _mesh.point(   *cfv_it);
      const typename MeshT::Point& pt1 = _mesh.point( *(++cfv_it));
      const typename MeshT::Point& pt2 = _mesh.point( *(++cfv_it));
Jan Möbius's avatar
 
Jan Möbius committed
380
381
382
383
384
385
386
387
388

      typename MeshT::Point ptn;

      typename MeshT::Scalar d = ACG::Geometry::distPointTriangleSquared( _point,
                     pt0,
                     pt1,
                     pt2,
                     ptn );

Anne Kathrein's avatar
Anne Kathrein committed
389
      if( d < d_best && d >= 0.0)
Jan Möbius's avatar
 
Jan Möbius committed
390
391
392
393
      {
        d_best = d;
        p_best = ptn;

Jan Möbius's avatar
Jan Möbius committed
394
        fh_best = *cf_it;
Jan Möbius's avatar
 
Jan Möbius committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
      }
    }

    // return facehandle
    _fh = fh_best;

    // return distance
    if(_dbest)
      *_dbest = sqrt(d_best);


    return p_best;
  }
  else
  {
    typename MeshT::FaceHandle     fh = _ssearch->nearest(_point).handle;
    typename MeshT::CFVIter        fv_it = _mesh.cfv_iter(fh);

Jan Möbius's avatar
Jan Möbius committed
413
414
415
    const typename MeshT::Point&   pt0 = _mesh.point( *(  fv_it));
    const typename MeshT::Point&   pt1 = _mesh.point( *(++fv_it));
    const typename MeshT::Point&   pt2 = _mesh.point( *(++fv_it));
Jan Möbius's avatar
 
Jan Möbius committed
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441

    // project
    d_best = ACG::Geometry::distPointTriangleSquared(_point, pt0, pt1, pt2, p_best);

    // return facehandle
    _fh = fh;

    // return distance
    if(_dbest)
      *_dbest = sqrt(d_best);

    return p_best;
  }
}


template< class MeshT >
template< class SpatialSearchT >
void IsotropicRemesher< MeshT >::projectToSurface( MeshT& _mesh, MeshT& _original, SpatialSearchT* _ssearch )
{

  typename MeshT::VertexIter v_it;
  typename MeshT::VertexIter v_end = _mesh.vertices_end();

  for (v_it = _mesh.vertices_begin(); v_it != v_end; ++v_it){

Jan Möbius's avatar
Jan Möbius committed
442
443
    if (isBoundary(_mesh, *v_it)) continue;
    if ( isFeature(_mesh, *v_it)) continue;
Jan Möbius's avatar
 
Jan Möbius committed
444

Jan Möbius's avatar
Jan Möbius committed
445
    typename MeshT::Point p = _mesh.point(*v_it);
Jan Möbius's avatar
 
Jan Möbius committed
446
447
448
449
450
    typename MeshT::FaceHandle fhNear;
    double distance;

    typename MeshT::Point pNear = findNearestPoint(_original, p, fhNear, _ssearch, &distance);

Jan Möbius's avatar
Jan Möbius committed
451
    _mesh.set_point(*v_it, pNear);
Jan Möbius's avatar
 
Jan Möbius committed
452
453
454
  }
}