File: FaceCollapseOp.cc

package info (click to toggle)
madlib 1.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,196 kB
  • sloc: cpp: 39,851; sh: 10,041; makefile: 473
file content (496 lines) | stat: -rw-r--r-- 16,101 bytes parent folder | download | duplicates (6)
1
2
3
4
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// -------------------------------------------------------------------
// MAdLib - Copyright (C) 2008-2009 Universite catholique de Louvain
//
// See the Copyright.txt and License.txt files for license information. 
// You should have received a copy of these files along with MAdLib. 
// If not, see <http://www.madlib.be/license/>
//
// Please report all bugs and problems to <contrib@madlib.be>
//
// Authors: Olivier Pierard, Gaetan Compere, Jean-Francois Remacle
// -------------------------------------------------------------------

#include "FaceCollapseOp.h"
#include "OperatorTools.h"
#include "MeshSizeBase.h"

namespace MAd {

  // -------------------------------------------------------------------
  bool faceCollapseOp::checkConstraints() const
  {
    // find the opposite vertex
    oppV = F_edOpVt(delFace,delEdge);

    if( EN_constrained((pEntity)delEdge)) return false;
    if( !clpsOnOppV && EN_constrained((pEntity)oppV) ) return false;

    return true;
  }

  // -------------------------------------------------------------------
  bool faceCollapseOp::checkGeometry()
  {
    pGEntity fGE = F_whatIn(delFace);
    if( clpsOnOppV )
      {
        // delEdge and delFace must have the same classification
        if( E_whatIn(delEdge) != fGE ) return false;
      }
    else
      {
        // oppV and delFace must have the same classification
        if( V_whatIn(oppV) != fGE ) return false;
      }
    
    // It leaves us with two possibilities:
    // (a) In case ( clpsOnOppV == true ) :
    //     (1) delFaces and delEdges are classified on a dim 2
    //     (2) delFaces and delEdges are classified on a dim 3
    // (b) In case ( clpsOnOppV == false ) :
    //     (1) delFaces and oppV are classified on a dim 2
    //     (2) delFaces and oppV are classified on a dim 3
    // For both (a) and (b):
    // In case (2) no dimension reduction can occur -> unconditionally valid
    // In case (1): * if 2D mesh, no dim reduc -> unconditionally valid
    //              * if 3D mesh, two things to check:
    //                   A.: check boundaries are not constrained
    //                       (otherwise ok but checkVol=true)
    //                   B.: check dimension reduction

    if ( dim == 3 && GEN_type(fGE) == 2 )
      {
        // check A
        if( constrainBoundary ) return false;
        else checkVol = true;
            
        // check B
        // - Dimension reduction on node (on oppV, case clpsOnOppV=true): as delEdge 
        //   is classified on dim 2, only one surface (fGE) passes 
        //   through it. As it is the same as the geom ent of delFace, 
        //   oppV already touches it -> no node dim reduction.
        // - Dimension reduction on lines and surfaces:
        //   occurs if these 2 conditions hold together:
        //   C1: for one of the regions bounding delFace, the face 
        //       containing delEdge and which is not delFace is 
        //       classified on a surface,
        //   C2: for one of the regions bounding delFace, the edge 
        //       of the region opposite to delEdge is classified 
        //       on a dimension < 3 (line contact).
        //   C2b (surface contact): for one of the regions bounding 
        //       delFace, one of the 2 other faces is classified on 
        //       a surface. This is found by Condition 2.
        // - Dimension reduction occur if these 2 conditions hold together:
        //   C3: the face has only 1 region,
        //   C4: there is a face using delEdge which opposite vertex
        //       is linked to oppV by an edge oppE and oppE is not 
        //       used by the only region.
        bool C1 = false, C2 = false;
        for (int iR=0; iR<2; iR++)
          {
            pRegion reg = F_region(delFace,iR);
            if ( !reg ) continue;
            
            // check C1
            for (int iF=0; iF<4; iF++) {
              pFace pf = R_face(reg,iF);
              if ( pf == delFace ) continue;
              if ( F_inClosure(pf,(pEntity)delEdge) && F_whatInType(pf) == 2 ) C1 = true;
            }

            // check C2
            if ( E_whatInType( R_gtOppEdg(reg, delEdge) ) < 3 ) C2 = true;
          }
        if ( C1 && C2 ) return false;

        // In case C3 holds
        if ( F_numRegions(delFace) == 1 )
          {
            // check C4
            pRegion pr = F_region(delFace,0);
            if ( !pr ) pr = F_region(delFace,1);
            pPList delEdgeFaces = E_faces(delEdge);
            void * temp = NULL;
            pFace pf;
            while ( ( pf = (pFace) PList_next(delEdgeFaces,&temp) ) )
              {
                if ( pf == delFace ) continue;
                if ( R_inClosure(pr,(pEntity)pf) ) continue;
                if ( E_exist(oppV, F_edOpVt(pf,delEdge) ) ) {
                  PList_delete(delEdgeFaces); return false;
                }
              }
            PList_delete(delEdgeFaces);
          }
      }

    return true;
  }

  // -------------------------------------------------------------------
  bool faceCollapseOp::evaluateShapes()
  {
    if( dim != 3 ) return evaluateShapes2D();
  
    double worstShape = mqm.getElementEvaluator()->bestShapeEver();

    pRegion delReg[2];
    delReg[0] = F_region(delFace,0);
    delReg[1] = F_region(delFace,1);

    double rCoords[4][3];   
    pMSize rSizes[4] = {NULL, NULL, NULL, NULL};

    double volIn = 0.;
    double volOut = 0.;
  
    // 1) General case valid for any clpsOnOppV
  
    pPList eRegs = E_regions(delEdge);
    void * temp = NULL;
    while( pRegion region = (pRegion)PList_next( eRegs, &temp ) ) {   
    
      if (checkVol) { volIn += R_volume (region); }
    
      if( region==delReg[0] || region==delReg[1] ) continue;
    
      // check every tet resulting from the split
      for( int iR=0; iR<2; iR++ ) {
      
        pVertex eV = E_vertex(delEdge,iR);
      
        // Loop over all the vertices of region
        pVertex rV;
        for( int iRV=0; iRV < R_numVertices(region); iRV++ ) {        
        
          rV = R_vertex( region, iRV );
      
          if ( rSizes[iRV] ) delete rSizes[iRV];
        
          if( rV == eV ) { // Set oppV size and pos. instead of end of delEdge
            if( clpsOnOppV ) {  // Collapse on oppV
              double xyz[3]; 
              V_coord(oppV,xyz);
              for( int k=0; k<3; k++ ) rCoords[iRV][k] = xyz[k];
              rSizes[iRV] = sizeField->getSize(oppV);  
            }
            else {  // Collapse on newVt
              for( int k=0; k<3; k++ ) rCoords[iRV][k] = newVPos[k];
              rSizes[iRV] = sizeField->getSizeOnEntity((pEntity)delEdge,rCoords[iRV]);
            }
          }
          else {
            rSizes[iRV] = sizeField->getSize(rV);
            V_coord(rV,rCoords[iRV]);
          }
        }
      
        // Some cleaning and exit if new element is not acceptable
        double shape;
        if( ! mqm.getElementEvaluator()->XYZ_R_shape(rCoords,rSizes,&shape) ) {
          for (int i=0; i<4; i++) {
            if ( rSizes[i] ) delete rSizes[i];
          }
          PList_delete(eRegs);
          return false;
        }

        if( worstShape > shape ) worstShape = shape;
      
        if (checkVol) { volOut += R_XYZ_volume(rCoords); }
      }  // End of loop on sub-tetra
    }  // End loop on regions around delEdge
    PList_delete(eRegs);
  
    // 2) Particular case if collapse edge on new vertex; need other checks 
  
    if( !clpsOnOppV ) {     
    
      // loop over the regions of oppV excepted delReg[0-1]
      pPList vRegs = V_regions(oppV);
      void * temp = NULL;
      while( pRegion region = (pRegion)PList_next(vRegs,&temp) ) {
    
        if( region==delReg[0] || region==delReg[1] ) continue;
      
        if (checkVol) { volIn += R_volume(region); }
      
        pVertex rV;
        for(int iRV=0; iRV < R_numVertices(region); iRV++) {
      
          rV = R_vertex(region, iRV);
      
          if ( rSizes[iRV] ) delete rSizes[iRV];
        
          if( rV == oppV ) { // Set size and pos. of new vertex on delEdge
            for(int iC=0; iC<3; iC++) rCoords[iRV][iC] = newVPos[iC];
            rSizes[iRV] = sizeField->getSizeOnEntity((pEntity)delEdge,rCoords[iRV]);
          }
          else {
            rSizes[iRV] = sizeField->getSize(rV);
            V_coord(rV,rCoords[iRV]);
          }
        }
      
        if (checkVol) { volOut += R_XYZ_volume(rCoords); }
      
        // Some cleaning and exit if new element is not acceptable
        double shape;
        if( ! mqm.getElementEvaluator()->XYZ_R_shape(rCoords,rSizes,&shape) ) {
          for (int i=0; i<4; i++) {
            if ( rSizes[i] ) delete rSizes[i];
          }
          PList_delete(vRegs);
          return false;
        }
      
        if( worstShape > shape ) worstShape = shape;
      
      }  // End loop on vRegs
      PList_delete(vRegs);
    }
  
    for (int i=0; i<4; i++) {
      if ( rSizes[i] ) delete rSizes[i];
    }
  
    results->setWorstShape(worstShape); 

    if (checkVol) {
      double dV = fabs( (volIn-volOut) / volIn );
      if( dV > dVTol ) return false;
    }
  
    return true;
  }

  // -------------------------------------------------------------------
  bool faceCollapseOp::evaluateShapes2D()
  {
    double worstShape = mqm.getElementEvaluator()->bestShapeEver();

    // get the normal to the del face
    double fNormal[3];
    F_normal(delFace,fNormal);

    double fCoords[3][3];   
    pMSize fSizes[3] = {NULL, NULL, NULL};
  
    // 1) General case valid for any clpsOnOppV
  
    for( int eF=0; eF < E_numFaces(delEdge); eF++ ) {
    
      pFace face = E_face(delEdge, eF);
    
      if( face == delFace ) continue;

      for( int iSubF=0; iSubF<2; iSubF++ ) {
      
        pVertex eV = E_vertex(delEdge,iSubF);
      
        // Loop over all the vertices of face
        pVertex fV;
        for( int iFV=0; iFV<F_numVertices(face); iFV++ ) {        
        
          fV = F_vertex(face, iFV);
      
          if ( fSizes[iFV] ) delete fSizes[iFV];
        
          if( fV == eV ) { // Set oppV size and pos. instead of end of delEdge
            if( clpsOnOppV ) {
              double xyz[3]; 
              V_coord(oppV,xyz);
              for(int k=0; k<3; k++ ) fCoords[iFV][k] = xyz[k];
              fSizes[iFV] = sizeField->getSize(oppV);  
            }
            else {  // Collapse on newVt
              for(int k=0; k<3; k++) fCoords[iFV][k] = newVPos[k];
              fSizes[iFV] = sizeField->getSizeOnEntity((pEntity)delEdge,fCoords[iFV]);
            }
          }
          else {
            V_coord(fV,fCoords[iFV]);
            fSizes[iFV] = sizeField->getSize(fV);
          }
        }
      
        // Some cleaning and exit if new element is not acceptable
        double shape;
        if( ! mqm.getElementEvaluator()->XYZ_F_shape(fCoords,fSizes,fNormal,&shape) ) {
          for (int i = 0; i<3; i++) {
            if ( fSizes[i] ) delete fSizes[i];
          }
          return false;
        }
      
        if( worstShape > shape ) worstShape = shape;
      }  // End of loop on sub-tetra
    }  // End loop on regions around delEdge
  
  
    // 2) Particular case if collapse edge on new vertex; need other checks 
  
    if( !clpsOnOppV ) {
    
      pPList vFaces = V_faces(oppV);
      void * temp = NULL;
      while( pFace face = (pFace)PList_next( vFaces, &temp ) ) { 
    
        if( face == delFace ) continue;
      
        pVertex fV;       
        for( int k=0; k < F_numVertices(face); k++ ) {
      
          fV = F_vertex(face,k);
      
          if ( fSizes[k] ) delete fSizes[k];
        
          if( fV == oppV ) { // Set size and pos. of new vertex on delEdge
            for( int iC=0; iC<3; iC++ ) fCoords[k][iC] = newVPos[iC];
            fSizes[k] = sizeField->getSizeOnEntity((pEntity)delEdge,fCoords[k]);
          }
          else {
            V_coord(fV,fCoords[k]);
            fSizes[k] = sizeField->getSize(fV);
          }
        }
      
        // Some cleaning and exit if new element is not acceptable
        double shape;
        if( ! mqm.getElementEvaluator()->XYZ_F_shape(fCoords,fSizes,fNormal,&shape) ) {
          for (int i = 0; i<3; i++) {
            if ( fSizes[i] ) delete fSizes[i];
          }
          return false;
        }
      
        if( worstShape > shape ) worstShape = shape;
      }  // End loop on vFaces              
      PList_delete(vFaces);
    }
  
    for (int i = 0; i<3; i++) {
      if ( fSizes[i] ) delete fSizes[i];
    }
  
    results->setWorstShape(worstShape);
    
    return true;
  }

  // -------------------------------------------------------------------
  void faceCollapseOp::evaluateLengths() const
  {
    double minSq = MAdBIG;
    double maxSq = 0.;
  
    // Interpolate the desired size at splitting location (for !clpsOnOppV)
    pMSize sizeNewV = NULL;
    if (!clpsOnOppV) {
      sizeNewV = sizeField->getSizeOnEntity((pEntity)delEdge,newVPos);
    }
  
    // 1) General case, all faces touching delEdge are modified for both types
  
    pFace face;
    for( int i=0; i<E_numFaces(delEdge); i++ ) {
    
      face = E_face(delEdge, i);
    
      if( face == delFace ) continue;
    
      pVertex vertex = F_edOpVt(face,delEdge);
    
      double lenSq;
      if( clpsOnOppV ) {
        lenSq = sizeField->SF_VV_lengthSq(oppV,vertex);
      }
      else {
        double vxyz[3];
        V_coord(vertex,vxyz);
        pMSize vSize = sizeField->findSize(vertex);
        lenSq = sizeField->SF_XYZ_lengthSq( newVPos, vxyz, 
                                            sizeNewV, vSize );
      }

      if( lenSq > maxSq ) maxSq = lenSq;
      if( lenSq < minSq ) minSq = lenSq;
    }
    
    // 2) For type 0 only, all edges touching oppV are affected as well
    
    if( !clpsOnOppV ) {
    
      for( int j=0; j<V_numEdges( oppV ); j++ )
        {
          pVertex vertex = E_otherVertex( V_edge(oppV, j), oppV );
          double vxyz[3];
          V_coord(vertex,vxyz);
          pMSize vSize = sizeField->findSize(vertex);
          double lenSq = sizeField->SF_XYZ_lengthSq( newVPos, vxyz, 
                                                     sizeNewV, vSize );
      
          if( lenSq > maxSq ) maxSq = lenSq;
          if( lenSq < minSq ) minSq = lenSq;
        }          
    }
  
    results->setMaxLenSq(maxSq);
    results->setMinLenSq(minSq);

    if (sizeNewV) delete sizeNewV;
  }

  // -------------------------------------------------------------------
  void faceCollapseOp::getCavity(pPList * cavity) const
  {
    if( clpsOnOppV )  // Delete new vertex
      {
        if ( dim == 3 ) *cavity = E_regions(delEdge);
        else            *cavity = E_faces(delEdge);
      }
    else  // Delete existing vertex
      {
        if ( dim == 3 ) {
          *cavity = V_regions(oppV);
          pPList eRegs = E_regions(delEdge);
          void * temp = 0;
          while ( pRegion region = (pRegion)PList_next(eRegs,&temp) ) {
            if ( !PList_inList(*cavity,(pEntity)region) ) PList_append(*cavity,(pEntity)region);
          }
          PList_delete(eRegs);
        }
        else {
          *cavity = V_faces(oppV);
          pPList eFaces = E_faces(delEdge);
          void * temp = 0;
          while ( pFace face = (pFace)PList_next(eFaces,&temp) ) {
            if ( !PList_inList(*cavity,(pEntity)face) ) PList_append(*cavity,(pEntity)face);
          }
          PList_delete(eFaces);

        }
      }
  }

  // -------------------------------------------------------------------
  void faceCollapseOp::apply()
  {
    pVertex newV = E_split(mesh, delEdge, newVPos);
  


    // determine the edge collapse
    pEdge edgeDel = E_exist(oppV,newV);
    pVertex vDel, vTgt;
    if( clpsOnOppV ) { vDel = newV; vTgt = oppV; } 
    else             { vDel = oppV; vTgt = newV; }

    E_collapse(mesh,edgeDel,vDel,vTgt);

    HistorySgl::instance().add((int)type(),OPERATOR_APPLY,1);
  }

  // -------------------------------------------------------------------

}