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
#include "meshkit/SwapEdges.hpp"

#include <iostream>
#include <iomanip>

using namespace Jaal;

//#############################################################################

int SwapTriEdges:: one_sweep(int entity, int rule)
{
     size_t total_count = 0;
     size_t numnodes = mesh->getSize(0);
     size_t numfaces = mesh->getSize(2);

     // Sweep over nodes in the mesh...
     if( entity == 0 ) {
          while(1) {
               size_t curr_count = 0;
               for (size_t i = 0; i < numnodes; i++) {
                    int err = atomicOp( mesh->getNodeAt(i), rule);
                    if (!err) curr_count++;
               }
               if( curr_count == 0) break;
               total_count += curr_count;
          }
     }

     // Sweep over faces in the mesh...
     if( entity == 2 ) {
          while(1) {
               size_t curr_count = 0;
               for (size_t i = 0; i < numfaces; i++) {
                    int err = atomicOp( mesh->getFaceAt(i), rule);
                    if (!err) curr_count++;
               }
               if( curr_count == 0) break;
               total_count += curr_count;
          }
     }
 
     assert( numnodes ==  mesh->getSize(0) );
     assert( numfaces ==  mesh->getSize(2) );

     mesh->make_consistently_oriented();

     return total_count;
}

//#############################################################################

int SwapTriEdges::apply_rule(int rule )
{
     int relexist2 = mesh->build_relations(0, 2);

     mesh->search_boundary();

     assert(mesh->getAdjTable(0, 2));

     num_edges_flipped = 0;
     Jaal::MeshOptimization mopt;

     int curr_count;<--- The scope of the variable 'curr_count' can be reduced.
     while(1) {
          curr_count = one_sweep( 2, rule);
          if( curr_count == 0) {
               mopt.shape_optimize(mesh);
               curr_count = one_sweep( 2, rule );
               if( curr_count == 0) break;
               num_edges_flipped+= curr_count;
          } else
               num_edges_flipped+= curr_count;
     }

     if (!relexist2) mesh->clear_relations(0, 2);

     cout << "# of edges swapped  " << num_edges_flipped << endl;

     return num_edges_flipped;
}

//#############################################################################

int SwapTriEdges::atomicOp(const Face *face, int rule)
{
     if( !face->isActive() )  return 1;

     for (int i = 0; i < 3; i++) {
          Vertex *v1 = face->getNodeAt(i + 1);
          Vertex *v2 = face->getNodeAt(i + 2);
          FlipEdge edge(v1, v2);
          if (is_edge_flip_allowed(edge, rule)) {
               int err = commit(edge);
               if( !err) return 0;
          }
     }
     return 2;
}

//#############################################################################

void SwapTriEdges::FlipEdge::process(Vertex *v1, Vertex *v2)
{
     assert(v1 != v2);

     this->setNodes(v1, v2);

     faces[0] = NULL;
     faces[1] = NULL;
     opposite_nodes[0] = NULL;
     opposite_nodes[1] = NULL;

     if (v1->isBoundary() && v2->isBoundary()) return;

     FaceSequence neighs;
     Mesh::getRelations112(v1, v2, neighs);

     assert(neighs.size() == 2);

     Vertex *ov1 = Face::opposite_node(neighs[0], v1, v2);
     Vertex *ov2 = Face::opposite_node(neighs[1], v1, v2);
     assert(ov1 && ov2);

     faces[0] = neighs[0];
     faces[1] = neighs[1];
     opposite_nodes[0] = ov1;
     opposite_nodes[1] = ov2;
}

//#############################################################################

bool SwapTriEdges::FlipEdge::isSharp(double featureAngle) const
{
     Vertex *v1 = getNodeAt(0);
     Vertex *v2 = getNodeAt(1);
     Vertex *ov1 = opposite_nodes[0];
     Vertex *ov2 = opposite_nodes[1];

     Vec3D A, B;
     A = Face::normal(ov1, v1, v2);
     B = Face::normal(ov2, v2, v1);

     double angle = Math::getVectorAngle(A, B, ANGLE_IN_DEGREES);
     if (angle > featureAngle && fabs(180 - angle) > 1.0E-06) return 1;

     return 0;
}

//#############################################################################

bool SwapTriEdges::FlipEdge::isConcave() const
{
     Vertex *v1 = getNodeAt(0);
     Vertex *v2 = getNodeAt(1);
     Vertex *ov1 = opposite_nodes[0];
     Vertex *ov2 = opposite_nodes[1];

     assert( v1 && v2  );
     assert( ov1 && ov2  );

     bool convex = Face::is_convex_quad(v1->getXYZCoords(),
                                        ov1->getXYZCoords(),
                                        v2->getXYZCoords(),
                                        ov2->getXYZCoords());
     if (!convex) return 1;

     return 0;
}


//*****************************************************************************

bool SwapTriEdges::is_edge_flip_allowed(const FlipEdge &edge, int rule) const
{
     if (!edge.isValid())   return 0;
     if (edge.isConcave()) return 0;

     Vertex *v1  = edge.getNodeAt(0);
     Vertex *v2  = edge.getNodeAt(1);
     Vertex *ov1 = edge.opposite_nodes[0];
     Vertex *ov2 = edge.opposite_nodes[1];

     if (v1->isBoundary()  && v2->isBoundary()  ) return 0;
     if (ov1->isBoundary() || ov2->isBoundary() ) return 0;

     int d1 = v1->getNumRelations(2);
     int d2 = v2->getNumRelations(2);
     int d3 = ov1->getNumRelations(2);
     int d4 = ov2->getNumRelations(2);

     if( rule == DELAUNAY_RULE) {
          double len1 = Vertex::length2(v1, v2);
          double len2 = Vertex::length2(ov1, ov2);
          if (len1 <= len2) return 0;

          if (edge.isSharp(creaseAngle)) return 0;
          return 1;
     }

     if( rule == ADVANCE_FRONT_RULE ) {
          int ideal_v1 =  v1->get_ideal_face_degree(3);
          int ideal_v2 =  v2->get_ideal_face_degree(3);
          int ideal_v3 =  ov1->get_ideal_face_degree(3);
          int ideal_v4 =  ov2->get_ideal_face_degree(3);

          int l1 = 0;
          v1->getAttribute("Layer",  l1);

          int l2 = 0;
          v2->getAttribute("Layer",  l2);

          int l3 = 0;
          ov1->getAttribute("Layer", l3);

          int l4 = 0;
          ov2->getAttribute("Layer", l4);

          // Decrease the vertex degree
          if( (d1  > ideal_v1) && (l2 > l1) && (l3 > l1) && (l4 > l1) ) return 1;
          if( (d2  > ideal_v2) && (l1 > l2) && (l3 > l2) && (l4 > l2) ) return 1;

          // Increase the vertex degree ...
          if( (d3  < ideal_v3) && (l1 > l3) && (l2 > l3) && (l4 > l3) ) return 1;
          if( (d4  < ideal_v4) && (l1 > l4) && (l2 > l4) && (l3 > l4) ) return 1;

          return 0;
     }

     if( rule == DEGREE_REDUCTION_RULE) {
          int ideal_v1 =  v1->get_ideal_face_degree(3);
          int ideal_v2 =  v2->get_ideal_face_degree(3);

          if( v1->isBoundary() &&  (d1 > ideal_v1) && (d2 >3) )  return 1;
          if( v2->isBoundary() &&  (d2 > ideal_v2) && (d1 >3) )  return 1;

          int relaxation_index = d1 + d2 - d3 - d4;
          if (relaxation_index < 3) return 0;
          return 1;
     }

     return 0;
}

//#############################################################################

int SwapTriEdges::commit(const FlipEdge &edge)
{
     Face *t1 = edge.faces[0];
     Face *t2 = edge.faces[1];
     Vertex *v1 = edge.getNodeAt(0);
     Vertex *v2 = edge.getNodeAt(1);
     Vertex *ov1 = edge.opposite_nodes[0];
     Vertex *ov2 = edge.opposite_nodes[1];

     int pos1 = t1->getPosOf(v1);
     int pos2 = t2->getPosOf(v1);

     v1->removeRelation(t1);
     v1->removeRelation(t2);
     v2->removeRelation(t1);
     v2->removeRelation(t2);
     v1->removeRelation(v2);
     v2->removeRelation(v1);

     NodeSequence vconn(3);

     if( t1->getNodeAt(pos1+1) == ov1 && t2->getNodeAt(pos2+2) == ov2 ) {
          vconn[0] = v1;
          vconn[1] = ov1;
          vconn[2] = ov2;
          t1->setNodes(vconn);

          vconn[0] = v2;
          vconn[1] = ov2;
          vconn[2] = ov1;
          t2->setNodes(vconn);

          mesh->reactivate(t1);
          mesh->reactivate(t2);
          return 0;
     }

     if( t1->getNodeAt(pos1+2) == ov1 && t2->getNodeAt(pos2+1) == ov2 ) {
          vconn[0] = v1;
          vconn[1] = ov2;
          vconn[2] = ov1;
          t1->setNodes(vconn);

          vconn[0] = v2;
          vconn[1] = ov1;
          vconn[2] = ov2;
          t2->setNodes(vconn);

          mesh->reactivate(t1);
          mesh->reactivate(t2);
          return 0;
     }

     cout << "Fatal Error: Orientation of faces unknown " << endl;
     exit(0);

     return 0;
}

//#############################################################################

int SwapTriEdges ::atomicOp(Vertex *apexVertex, int rule)
{
     FaceSequence vneighs;
     apexVertex->getRelations( vneighs );
     int numNeighs = vneighs.size();

     for( int i = 0; i < numNeighs; i++) {
          if( unchecked(vneighs[i] ) ) {
               int err = atomicOp( vneighs[i], rule);
               if( err  == 0) return 0;
          }
     }

     return 1;
}
///////////////////////////////////////////////////////////////////////////
int SwapTriEdges :: apply_advance_front_rule()
{
     int relexist2 = mesh->build_relations(0, 2);
     int relexist0 = mesh->build_relations(0, 0);

     mesh->search_boundary();

     size_t numNodes = mesh->getSize(0);
     NodeSequence currlayer, vneighs;
     NodeSet   nextlayer;

     for(size_t i = 0; i < numNodes; i++) {
          Vertex *v = mesh->getNodeAt(i);
          if( v->isBoundary() ) {
               v->setAttribute("Layer", 0);
               currlayer.push_back(v);
          } else
	    v->setAttribute("Layer",std::numeric_limits< int >::max());
     }

     Jaal::MeshOptimization mopt;
     size_t ncount, nSize, num_edges_flipped = 0;<--- The scope of the variable 'nSize' can be reduced.
     mesh->make_consistently_oriented();
     mopt.shape_optimize(mesh);

     LaplaceNoWeight lw;
     LaplaceSmoothing lapsmooth(mesh);
     lapsmooth.setWeight(&lw);
     lapsmooth.setNumIterations(100);

     int curr_layer_id = 0;
     int nirregular0, nirregular1;<--- The scope of the variable 'nirregular0' can be reduced.

     while(1) {
          nSize = currlayer.size();
          nirregular0 = 0;
          for( size_t i = 0; i < nSize; i++) {
               Vertex *v = currlayer[i];
               if( !v->isRemoved() && !isIdeal(v) ) nirregular0++;
          }

          for( int k = 0; k < 3; k++) {  // Three attempts for single layer
               while(1) {
                    nSize = currlayer.size();
                    ncount = 0;
                    for( size_t i = 0; i < nSize; i++) {
                         Vertex *v = currlayer[i];
                         if( !v->isRemoved() &&  !isIdeal(v)) {
                              int err  = atomicOp( v, ADVANCE_FRONT_RULE );
                              if( !err ) ncount++;
                         }
                    }
                    if( ncount == 0) break;
                    num_edges_flipped += ncount;
               }

               nSize = currlayer.size();
               nirregular1 = 0;
               for( size_t i = 0; i < nSize; i++) {
                    Vertex *v = currlayer[i];
                    if( !v->isRemoved() &&  !isIdeal(v) ) nirregular1++;
               }
               assert( nirregular1 <= nirregular0);
               if( nirregular1 == 0) break;
          }
          lapsmooth.execute();

//        mopt.shape_optimize(mesh);

          cout << "Layer : " << curr_layer_id << endl;
          cout << "# of Irregular nodes before swapping : " << nirregular0 << endl;
          cout << "# of Irregular nodes after swapping  : " << nirregular1 << endl;

          int lid = 0;

          nextlayer.clear();
          nSize = currlayer.size();
          for( size_t i = 0; i < nSize; i++) {
               Vertex *v = currlayer[i];
               v->getRelations( vneighs );
               for( size_t k = 0; k < vneighs.size(); k++) {
                    vneighs[k]->getAttribute( "Layer", lid);
                    if( lid > curr_layer_id ) {
                         vneighs[k]->setAttribute("Layer", curr_layer_id+1 );
                         nextlayer.insert( vneighs[k] );
                    }
               }
          }
          if( nextlayer.empty() ) break;

          NodeSet::const_iterator it;
          currlayer.resize(nextlayer.size() );
          int index = 0;
          for( it = nextlayer.begin(); it != nextlayer.end(); ++it)
               currlayer[index++] = *it;
          curr_layer_id++;
     }
     cout << "# of Edges Swapped " << num_edges_flipped << endl;

     vector<int>  less_than_ideal, more_than_ideal, total_ideal;

     int numLayers = curr_layer_id;
     less_than_ideal.resize( numLayers );
     more_than_ideal.resize( numLayers );
     total_ideal.resize( numLayers );

     for( int i = 0; i < numLayers; i++) {
          less_than_ideal[i] = 0;
          more_than_ideal[i] = 0;
          total_ideal[i] = 0;
     }

     int lid = 0;

     numNodes = mesh->getSize(0);
     int final_irregular = 0;
     for( size_t i = 0; i < numNodes; i++) {
          Vertex *v = mesh->getNodeAt(i);
          if( !v->isRemoved()) {
               v->getAttribute("Layer", lid );
               int curr_degree  = v->getNumRelations(2);
               int ideal_degree = v->get_ideal_face_degree(3);
               if( curr_degree != ideal_degree ) {
                    final_irregular++;
                    if( curr_degree < ideal_degree) less_than_ideal[lid]++;
                    if( curr_degree > ideal_degree) more_than_ideal[lid]++;
               } else
                    total_ideal[lid]++;
          }
     }

     cout << " Layer   Less   More  Ideal " << endl;
     for( int i = 0; i < numLayers; i++)
          cout << i << setw(10) <<  less_than_ideal[i]
               << setw(10) <<  more_than_ideal[i]
               << setw(10) <<  total_ideal[i] << endl;
     cout << " Final # of irregular nodes : " << final_irregular << endl;

     mopt.shape_optimize(mesh);

     if (!relexist2) mesh->clear_relations(0, 2);
     if (!relexist0) mesh->clear_relations(0, 0);

     return num_edges_flipped;
}