File: EdgeCollapseOp.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 (401 lines) | stat: -rw-r--r-- 12,216 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
// -------------------------------------------------------------------
// 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: Gaetan Compere, Jean-Francois Remacle
// -------------------------------------------------------------------

#include "EdgeCollapseOp.h"
#include "OperatorTools.h"
#include <map>
#include <cmath>

using std::map;

namespace MAd {

  // -------------------------------------------------------------------
  bool edgeCollapseOp::checkConstraints() const
  {
    if ( EN_constrained((pEntity)edgeDel) ) return false;
    if ( EN_constrained((pEntity)vDel   ) ) return false;
    return true;
  }

  // -------------------------------------------------------------------
  bool edgeCollapseOp::checkGeometry()
  {
    int typeD = V_whatInType(vDel);
    if ( typeD == 0 ) return false;

    if ( constrainBoundary ) {
      switch( typeD ) {
      case 3: break;
      case 2:
        {
          bool ok = true;
          pPList eRgns = E_regions(edgeDel);
          if( PList_size(eRgns) != 0 )  ok = false;
          PList_delete(eRgns);
          if (!ok) return false;
          break;
        }
      case 1: return false;
      case 0: return false;
      }
    }
    else {
      // check in edgeDel
      if ( V_whatIn(vDel) != E_whatIn(edgeDel) ) return false;
      
      if ( V_whatInType(vDel) <= 2 )
        {
          // check in every tet around edgeDel (= to be collapsed):
          //   - no dimension reduction surface on surface
          pEdge oppE;
          pFace newSurfaceFace;
          pPList vDelFaces = V_faces(vDel);
          void * temp = NULL;
          pFace pf;
          while ( ( pf = (pFace)PList_next(vDelFaces,&temp) ) ) {
            if ( F_inClosure(pf,(pEntity)edgeDel) ) continue;
            if ( F_whatInType(pf) == 2 ) {
              oppE           = F_vtOpEd(pf,vDel);
              newSurfaceFace = F_exist(oppE,vTgt);
              if ( newSurfaceFace && F_whatInType(newSurfaceFace) == 2 ) {
                PList_delete(vDelFaces);
                return false;
              }
            }
          }
          PList_delete(vDelFaces);
      
          
          // check in every face around edge del (= to be collapsed):
          //   - the face exists (that's why we loop over edges, not faces)
          //   - no new contact between surface and line
          //   - no new contact between surface and surface
          //   - no dimension reduction line on line
          //
          //                     vTgt 
          //
          //                   /  |
          //                  /   |
          //                 /    |
          //                /     |  oppE
          //     edgeDel   /      |
          //              /  pf   |
          //             /        |
          //            /         |
          //
          //          vDel ----- oppV
          //
          //                 pe
          //

          pVertex oppV;
          pEdge pe;
          pPList vDelEdges = V_edges(vDel);
          temp = NULL;
          while ( ( pe = (pEdge)PList_next(vDelEdges,&temp) ) ) {
            if ( pe == edgeDel ) continue;
            int d1 = E_whatInType(pe);
            if ( d1 <= 2 ) {
              oppV = E_otherVertex(pe,vDel);
              oppE = E_exist(oppV,vTgt);
              if ( oppE ) {
                int d2 = E_whatInType(oppE);
                if ( d2 <= 2 ) {
                  pf = F_exist(oppE,vDel);

                  // check if the face exists
                  if ( !pf ) {
                    PList_delete(vDelEdges); return false;
                  }

                  // check for line on line dimension reduction
                  if ( d1 == 1 && d2 == 1 ) {
                    PList_delete(vDelEdges); return false;
                  }

                  pGEntity fge = F_whatIn(pf);

                  // check for new contacts between a line and a surface
                  if ( d1 == 1 && d2 == 2 && fge != E_whatIn(oppE) ) {
                    PList_delete(vDelEdges); return false;
                  }
                  if ( d1 == 2 && d2 == 1 && fge != E_whatIn(pe) ) {
                    PList_delete(vDelEdges); return false;
                  }

                  // check for new contacts between 2 surfaces
                  if ( d1 == 2 && d2 == 2 ) {
                    if ( fge != E_whatIn(pe) || fge != E_whatIn(oppE) ) {
                      PList_delete(vDelEdges); return false;
                    }
                  }
                }
              }
            }
          }
          PList_delete(vDelEdges);
          
        }
    }

    return true;
  }

  // -------------------------------------------------------------------
  bool edgeCollapseOp::evaluateShapes2D()
  {
    pPList vDelFaces = V_faces(vDel);

    double worstShape = mqm.getElementEvaluator()->bestShapeEver();

    map<pGEntity,double> area_after;

    void * temp1 = 0;
    while ( pFace face = (pFace)PList_next(vDelFaces,&temp1) ) {

      // Faces surrounding edgeDel will be deleted
      if (F_inClosure(face,(pEntity)edgeDel)) continue;

      // Gather the coordinates and sizes of the new face
      double xyz[3][3];
      pMSize fSizes[3] = {NULL,NULL,NULL};

      pPList fVerts = F_vertices(face,1);
      void * temp2 = 0;
      int iV = 0;
      while ( pVertex vertex = (pVertex)PList_next(fVerts, &temp2) ) {
        if (vertex == vDel) {
          fSizes[iV] = sizeField->findSize(vTgt);
          V_coord(vTgt,xyz[iV]);
        }
        else {
          fSizes[iV] = sizeField->findSize(vertex);
          V_coord(vertex,xyz[iV]);
        }
        iV++;
      }
      PList_delete(fVerts);

      // Check if the shape is acceptable and compare it to the worst shape
      double shape = 0.;
      double normalBefore[3];
      F_normal(face,normalBefore);
      if ( !mqm.getElementEvaluator()->XYZ_F_shape(xyz, fSizes, normalBefore, &shape) ) {
        PList_delete(vDelFaces); 
        return false;
      }
      else {
        if ( shape < worstShape ) worstShape = shape;
      }

      // Compute the area of the new face
      if( !constrainBoundary && V_whatInType(vDel) < 2 ) {

        pGEntity g = F_whatIn(face);
        std::map<pGEntity,double>::iterator it = area_after.find( g );
        if( it != area_after.end() )
         (*it).second += XYZ_F_area(xyz,normalBefore);
        else
         area_after[g] = XYZ_F_area(xyz,normalBefore);
      }
    }

    // If the edge is on a boundary, check that the area of
    // the old and the new cavities are the same for each Geo entities
    if( !constrainBoundary && V_whatInType(vDel) < 2 ) {
      map< pGEntity,double>  area_before;
      map<pGEntity,double>::iterator it, it2;

      void * temp3 = 0;
      while ( pFace face = (pFace)PList_next(vDelFaces,&temp3) ) {
        double normal[3];
        F_normal(face,normal);
        pGEntity g = F_whatIn(face);
        it = area_before.find( g );
        if( it != area_before.end() )
          (*it).second += F_area(face,normal);
        else
          area_before[g] = F_area(face,normal);
      }

      for( it=area_before.begin(); it!=area_before.end(); ++it  )
      {
        double before = (*it).second;
        double after( 0.0 );
        it2 = area_after.find( (*it).first );
        if( it2 != area_after.end() )
          after = (*it2).second;

        // Check area change
        double dA = fabs( (before-after) / before );
        if( dA > dATol ) {
          PList_delete(vDelFaces);
          return false;
        }
      }
    }
    results->setWorstShape(worstShape);

    PList_delete(vDelFaces);

    return true;
  }

  // -------------------------------------------------------------------
  bool edgeCollapseOp::evaluateShapes()
  {
    pPList vDelRgns = V_regions(vDel);

    // 2D case
    if( PList_size(vDelRgns)==0 ) {
      bool flag = evaluateShapes2D();
      PList_delete(vDelRgns);
      return flag;
    }

    // 3D case
    else {

      double worstShape = mqm.getElementEvaluator()->bestShapeEver();
      double volAfter = 0.;

      void * temp1 = 0;
      while ( pRegion region = (pRegion)PList_next(vDelRgns,&temp1) ) {
      
        // Regions surrounding edgeDel will be deleted
        if (R_inClosure(region,(pEntity)edgeDel)) continue;

        // Gather the coordinates and sizes of the new region
        double xyz[4][3];
        pMSize rSizes[4] = {NULL,NULL,NULL,NULL};

        pPList rVerts = R_vertices(region);
        void * temp2 = 0;
        int iV = 0;
        while ( pVertex vertex = (pVertex)PList_next(rVerts, &temp2) ) {
          if (vertex == vDel) {
            rSizes[iV] = sizeField->findSize(vTgt);
            V_coord(vTgt,xyz[iV]);
          }
          else {
            rSizes[iV] = sizeField->findSize(vertex);
            V_coord(vertex,xyz[iV]);
          }
          iV++;
        }
        PList_delete(rVerts);

        // Check if the shape is acceptable and compare it to the worst shape
        double shape = 0.;
        if ( !mqm.getElementEvaluator()->XYZ_R_shape(xyz, rSizes, &shape) ) {
          PList_delete(vDelRgns); 
          return false;
        }
        else {
          if ( shape < worstShape ) worstShape = shape;
        }

        // Compute the volume of the new region
        if( !constrainBoundary && V_whatInType(vDel)!=3 ) {
          volAfter += R_XYZ_volume (xyz);
        }
      }

      // If the edge is on a boundary, check that the volume of 
      // the old and the new cavities are the same
      if( !constrainBoundary && V_whatInType(vDel)!=3 ) {

        double volBefore = 0.;
      
        void * temp3 = 0;
        while ( pRegion region = (pRegion)PList_next(vDelRgns,&temp3) ) {
          volBefore += R_volume (region);
        }

        double dV = fabs( (volBefore-volAfter) / volBefore );
        if( dV > dVTol ) {
          PList_delete(vDelRgns);
          return false;
        }
      }
    
      results->setWorstShape(worstShape);
    }
  
    PList_delete(vDelRgns); 
  
    return true;
  }

  // -------------------------------------------------------------------
  void edgeCollapseOp::evaluateLengths() const
  {
    double minSq = MAdBIG, maxSq = 0.;

    double xyzTgt[3];
    V_coord(vTgt,xyzTgt);
    pMSize sizeTgt = sizeField->findSize(vTgt);

    // list nodes linked to target vertex by an edge
    std::set<pVertex> vTgtLinked;
    pPList vTgtEdges = V_edges(vTgt);
    void * temp1 = 0;
    while ( pEdge edge = (pEdge) PList_next(vTgtEdges,&temp1) ) {
      vTgtLinked.insert(E_vertex(edge,0));
      vTgtLinked.insert(E_vertex(edge,1));
    }
    PList_delete(vTgtEdges);

    // find nodes linked to vDel and not linked to vTgt
    pPList vDelEdges = V_edges(vDel);
    void * temp2 = 0;
    while ( pEdge edge = (pEdge) PList_next(vDelEdges,&temp2) ) {
      for (int iV=0; iV<2; iV++) {
        pVertex pV = E_vertex(edge,iV);
        if ( pV == vDel ) continue;
        if ( vTgtLinked.find(pV) != vTgtLinked.end() ) continue;
      
        double xyzV[3];
        V_coord(pV,xyzV);
        pMSize sizeV = sizeField->findSize(pV);
      
        double newSq = sizeField->SF_XYZ_lengthSq(xyzTgt,xyzV,sizeTgt,sizeV);
        if( newSq < minSq ) { minSq = newSq; }
        if( newSq > maxSq ) { maxSq = newSq; }
      }
    }
    PList_delete(vDelEdges);

    results->setMaxLenSq(maxSq);
    results->setMinLenSq(minSq);
  }

  // -------------------------------------------------------------------
  void edgeCollapseOp::getCavity(pPList * cavity) const
  {
    if ( dim == 3 ) *cavity = V_regions(vDel);
    else            *cavity = V_faces(vDel);
  }

  // -------------------------------------------------------------------
  void edgeCollapseOp::apply()
  {
    E_collapse(mesh, edgeDel, vDel, vTgt);

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

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

}