-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathincg_smesh.cpp
More file actions
1388 lines (1199 loc) · 41.7 KB
/
incg_smesh.cpp
File metadata and controls
1388 lines (1199 loc) · 41.7 KB
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include "incg_smesh.h"
#include "incg_smesh_uid_factory.h"
#ifdef __cplusplus
extern "C" {
#endif
//
// A global variable to handle a singleton-style factory
//
sMesh_uid_factory* global_UID_factory = NULL;
//----------------------------------------------------------------------------
//
// Function to set the UID of an edge given node indices
//
int set_edge_uid( edgeid_t & euid,
unsigned long i, unsigned long j )
{
if( i < j ) {
euid.ids.i = i;
euid.ids.j = j;
return 1;
} else {
euid.ids.i = j;
euid.ids.j = i;
return 2;
}
}
//----------------------------------------------------------------------------
//
// Methods for the node object
//
sMesh_Node::sMesh_Node()
{
uid = global_UID_factory->getNewNodeUID( this );
flags = 0x00;
x = -9.9e+99;
y = -9.9e+99;
z = -9.9e+99;
}
sMesh_Node::~sMesh_Node()
{
}
long sMesh_Node::getUID() const
{
return uid;
}
int sMesh_Node::addRef( void *p )
{
refs.push_back( p );
return 0;
}
int sMesh_Node::removeRef( void *p )
{
refs.remove( p ); // ERROR TRAP
return 0;
}
unsigned int sMesh_Node::getNumRefs() const
{
return( (unsigned int) refs.size() );
}
//
// Methods for the edge object
//
sMesh_Edge::sMesh_Edge( sMesh_Node * np1_, sMesh_Node * np2_ )
{
uid = global_UID_factory->getNewEdgeUID( this );
flags = 0x00;
if( np1_->getUID() < np2_->getUID() ) {
np1 = np1_;
np2 = np2_;
} else {
np1 = np2_;
np2 = np1_;
}
np1->addRef( (void*) this );
np2->addRef( (void*) this );
}
sMesh_Edge::~sMesh_Edge()
{
np1->removeRef( (void*) this );
np2->removeRef( (void*) this );
}
long sMesh_Edge::getUID() const
{
return uid;
}
int sMesh_Edge::addRef( void *p )
{
refs.push_back( p );
return 0;
}
int sMesh_Edge::removeRef( void *p )
{
refs.remove( p ); // ERROR TRAP
return 0;
}
unsigned int sMesh_Edge::getNumRefs() const
{
return( (unsigned int) refs.size() );
}
sMesh_Node* sMesh_Edge::getNodePtr( int which_one ) const
{
if( which_one == 1 ) return np1;
else if( which_one == 2 ) return np2;
else return NULL;
}
int sMesh_Edge::isBoundary() const
{
// Bitwise-AND of [____ __X_] and [____ ___X] is 1 when both elements exist.
unsigned int ui = (flags & 0x01)
& ( (flags & 0x02) >> 1);
// Subtracting from 1 gives 0 if it is an interior edge or 1 if it is trace.
return 1 - ui;
}
int sMesh_Edge::computeLength()
{
// REQUIRES ERROR-TRAPPING in theory
double d = np1->x - np2->x;
length = d*d;
d = np1->y - np2->y;
length += d*d;
d = np1->z - np2->z;
length += d*d;
length = sqrt( length );
return 0;
}
double sMesh_Edge::getLength() const
{
return length;
}
sMesh_Edge* sMesh_Edge::getChildPtr( int which_one ) const
{
if( which_one == 1 ) return cp1;
else if( which_one == 2 ) return cp2;
else return NULL;
}
int sMesh_Edge::isSplit() const
{
if( cp1 == NULL ) return 0;
return 1;
}
int sMesh_Edge::split( uidmap_t* node_uid, ptrmap_t* node_ptr,
uidmap_t* edge_uid, ptrmap_t* edge_ptr )
{
if( isSplit() ) return 1;
sMesh_Node* mnp = new sMesh_Node();
if( mnp == NULL ) {
FPRINTF( stdout, " [Error] Could not create node object \n" );
return -1;
}
if( node_uid != NULL ) (*node_uid)[ mnp->getUID() ] = mnp;
if( node_ptr != NULL ) (*node_ptr)[ mnp ] = mnp->getUID();
mnp->flags |= (0x01 << 7);
// (The node order should be with the new node's UID being largest.)
if( np1->getUID() > mnp->getUID() || np1->getUID() > mnp->getUID() ) {
FPRINTF( stdout, " [Error] Node UID error in splitting edge\n" );
return 999;
}
mnp->x = 0.5*( np1->x + np2->x );
mnp->y = 0.5*( np1->y + np2->y );
mnp->z = 0.5*( np1->z + np2->z );
// form two new edges (the order is 1st edge shares node 1)
sMesh_Edge* sep1 = new sMesh_Edge( np1, mnp );
if( sep1 == NULL ) {
FPRINTF( stdout, " [Error] Could not create edge object \n" );
return -2;
}
if( edge_uid != NULL ) (*edge_uid)[ sep1->getUID() ] = sep1;
if( edge_ptr != NULL ) (*edge_ptr)[ sep1 ] = sep1->getUID();
sMesh_Edge* sep2 = new sMesh_Edge( np2, mnp );
if( sep2 == NULL ) {
FPRINTF( stdout, " [Error] Could not create edge object \n" );
return -3;
}
if( edge_uid != NULL ) (*edge_uid)[ sep2->getUID() ] = sep2;
if( edge_ptr != NULL ) (*edge_ptr)[ sep2 ] = sep2->getUID();
// assign splitting edge child pointers; convention is N1 -CE1- M -CE2- N2
cp1 = sep1;
cp2 = sep2;
return 0;
}
//
// Methods for the quadrilateral object
//
// Constructor requires pointers to edge objects and 4 bits indicating the
// direction of each edge compared to the counter-clockwise convention of the
// quadrilateral.
sMesh_Quad::sMesh_Quad( sMesh_Edge * ne1_, sMesh_Edge * ne2_,
sMesh_Edge * ne3_, sMesh_Edge * ne4_,
unsigned char dirs_ )
{
uid = global_UID_factory->getNewQuadUID( this );
eptr[0] = ne1_;
eptr[1] = ne2_;
eptr[2] = ne3_;
eptr[3] = ne4_;
eattr = dirs_ & 0xF0; // copy the first 4 bits [XXXX ____] as directions
// for the faces indexed [1234 ____]
for(int k=0;k<4;++k) eptr[k]->addRef( (void*) this );
}
sMesh_Quad::~sMesh_Quad()
{
for(int k=0;k<4;++k) eptr[k]->removeRef( (void*) this );
}
long sMesh_Quad::getUID() const
{
return uid;
}
unsigned char sMesh_Quad::getEdgeAttr() const
{
return eattr;
}
sMesh_Edge* sMesh_Quad::getEdgePtr( int which_one ) const
{
if( which_one < 0 || 3 < which_one ) return NULL;
return eptr[ which_one ];
}
int sMesh_Quad::needsSubdivision() const
{
if( sattr ) return 0;
int idiv=0;
if( eptr[0]->isSplit() || eptr[2]->isSplit() ) idiv = 1;
if( eptr[1]->isSplit() || eptr[3]->isSplit() ) idiv += 2;
return idiv;
}
int sMesh_Quad::setChildren( int index, sMesh_Quad* p, ChildSetToken& token )
{
if( index < 0 || 3 < index ) return 1;
child[ index ] = p;
if( index == 0 || index == 2 ) {
sattr |= 0x01;
} else {
sattr |= 0x02;
}
return 0;
}
sMesh_Quad* sMesh_Quad::getChildPtr( int index ) const
{
if( index < 0 || 3 < index ) return NULL;
return child[ index ];
}
//
// Methods for the triangle object
//
// Constructor requires pointers to edge objects and 3 bits indicating the
// direction of each edge compared to the counter-clockwise convention of the
// triangle.
sMesh_Tri::sMesh_Tri( sMesh_Edge * ne1_, sMesh_Edge * ne2_, sMesh_Edge * ne3_,
unsigned char dirs_ )
{
uid = global_UID_factory->getNewTriUID( this );
eptr[0] = ne1_;
eptr[1] = ne2_;
eptr[2] = ne3_;
eattr = dirs_ & 0xE0; // copy the first 3 bits [XXX_ ____] as directions
// for the faces indexed [123__ ___]
for(int k=0;k<3;++k) eptr[k]->addRef( (void*) this );
}
sMesh_Tri::~sMesh_Tri()
{
for(int k=0;k<3;++k) eptr[k]->removeRef( (void*) this );
}
long sMesh_Tri::getUID() const
{
return uid;
}
unsigned char sMesh_Tri::getEdgeAttr() const
{
return eattr;
}
sMesh_Edge* sMesh_Tri::getEdgePtr( int which_one ) const
{
if( which_one < 0 || 2 < which_one ) return NULL;
return eptr[ which_one ];
}
int sMesh_Tri::computeHeuristics( unsigned char & attr_, double & len_max )
{
const unsigned char bit7 = 0x01 << 7;
const unsigned char bit6 = 0x01 << 6;
const unsigned char bit5 = 0x01 << 5;
const unsigned char bit4 = 0x01 << 4;
len_max=0.0;
double edge_len[3];
for(int k=0;k<3;++k) {
edge_len[k] = eptr[k]->getLength();
if( edge_len[k] > len_max ) len_max = edge_len[k];
}
double edge_rat[3];
edge_rat[0] = edge_len[1] / edge_len[0]; // ratio of 1 on 0
edge_rat[1] = edge_len[2] / edge_len[1]; // ratio of 2 on 1
edge_rat[2] = edge_len[0] / edge_len[2]; // ratio of 0 on 2
const double ratio=0.15; // thresshold for declaring isosceles at angle
if( fabs( edge_rat[0] - 1.0/edge_rat[2] ) <= ratio ) hattr |= bit6;
if( fabs( edge_rat[1] - 1.0/edge_rat[0] ) <= ratio ) hattr |= bit5;
if( fabs( edge_rat[2] - 1.0/edge_rat[1] ) <= ratio ) hattr |= bit4;
sMesh_Node* node_ptr[3];
if( eattr & bit7 ) { node_ptr[0] = eptr[0]->getNodePtr(2);
node_ptr[1] = eptr[0]->getNodePtr(1); }
else { node_ptr[0] = eptr[0]->getNodePtr(1);
node_ptr[1] = eptr[0]->getNodePtr(2); }
if( eattr & bit6 ) { node_ptr[2] = eptr[1]->getNodePtr(1); }
else { node_ptr[2] = eptr[1]->getNodePtr(2); }
double angle_cos[3]; // angle opposite of an edge
angle_cos[0] = (node_ptr[0]->x - node_ptr[2]->x) * // nodes 0-2-1
(node_ptr[1]->x - node_ptr[2]->x)
+ (node_ptr[0]->y - node_ptr[2]->y) *
(node_ptr[1]->y - node_ptr[2]->y)
+ (node_ptr[0]->z - node_ptr[2]->z) *
(node_ptr[1]->z - node_ptr[2]->z);
angle_cos[1] = (node_ptr[1]->x - node_ptr[0]->x) * // nodes 1-0-2
(node_ptr[2]->x - node_ptr[0]->x)
+ (node_ptr[1]->y - node_ptr[0]->y) *
(node_ptr[2]->y - node_ptr[0]->y)
+ (node_ptr[1]->z - node_ptr[0]->z) *
(node_ptr[2]->z - node_ptr[0]->z);
angle_cos[2] = (node_ptr[2]->x - node_ptr[1]->x) * // nodes 2-1-0
(node_ptr[0]->x - node_ptr[1]->x)
+ (node_ptr[2]->y - node_ptr[1]->y) *
(node_ptr[0]->y - node_ptr[1]->y)
+ (node_ptr[2]->z - node_ptr[1]->z) *
(node_ptr[0]->z - node_ptr[1]->z);
// bits for angles: [____ _123] (set for obtuse angles)
if( angle_cos[0] < 0.0 ) hattr |= 0x04;
if( angle_cos[1] < 0.0 ) hattr |= 0x02;
if( angle_cos[2] < 0.0 ) hattr |= 0x01;
attr_ = hattr;
return 0;
}
int sMesh_Tri::setChildren( int index, void* p, ChildSetToken& token )
{
if( index < 0 || 2 < index ) return 1;
child[ index ] = p;
if( index == 0 ) sattr |= 0x04;
else if( index == 1 ) sattr |= 0x02;
else if( index == 2 ) sattr |= 0x01;
return 0;
}
void* sMesh_Tri::getChildPtr( int index ) const
{
if( index < 0 || 2 < index ) return NULL;
return child[ index ];
}
int sMesh_Tri::setSubdivision( unsigned char n, ChildSetToken& token )
{
unsigned char tmp = (sattr & 0xF0) >> 4;
if( tmp ) return 1;
if( n > 4 ) return 2;
sattr |= (n << 4);
return 0;
}
//
// Methods for the mesh object
//
sMesh_Core::sMesh_Core()
{
}
sMesh_Core::~sMesh_Core()
{
for(ptrmap_it pit = tri_ptr_map.begin(); pit != tri_ptr_map.end(); ++pit) {
delete ((sMesh_Tri*) pit->first);
}
for(ptrmap_it pit = quad_ptr_map.begin(); pit != quad_ptr_map.end(); ++pit) {
delete ((sMesh_Quad*) pit->first);
}
for(ptrmap_it pit = edge_ptr_map.begin(); pit != edge_ptr_map.end(); ++pit) {
delete ((sMesh_Edge*) pit->first);
}
for(ptrmap_it pit = node_ptr_map.begin(); pit != node_ptr_map.end(); ++pit) {
delete ((sMesh_Node*) pit->first);
}
}
//
// Public method to receive arrays of node and element data (that is provided
// in a conventional sparse format) and generate the internals of a mesh
// object.
// NOTE: Quadrilaterals need to have a proper winding order. The order can be
// clockwise or counter-clockwise, but there has to be a winding order.
//
int sMesh_Core::loadData( int nno, const node_t nodes[],
int nel, const face_t faces[] )
{
if( nno <= 0 || nel <= 0 ) {
FPRINTF( stdout, " [Error] Sizes (%d,%d) cannot be zero\n", nno, nel );
return 1;
}
if( nodes == NULL || faces == NULL ) {
FPRINTF( stdout, " [Error] Pointers (%p,%p) cannot be null\n", nodes, faces );
return 2;
}
// count triangles and quadrilaterals
int ntri=0, nquad=0;
for(int n=0;n<nel;++n) {
if( faces[n].nodes[3] < 0 ) ++ntri;
else ++nquad;
}
// continuation needs to trap individual object allocations
int ierr=0;
node_uid_map.clear();
node_ptr_map.clear();
for(int n=0;n<nno;++n) {
sMesh_Node* np = new sMesh_Node();
if( np == NULL ) { ierr=-111; break; }
np->x = nodes[n].x;
np->y = nodes[n].y;
np->z = nodes[n].z;
node_uid_map[ np->getUID() ] = np;
node_ptr_map[ np ] = np->getUID();
}
// allocate the edges (derived from the faces)
edge_uid_map.clear();
edge_ptr_map.clear();
nquad=0; ntri=0;
for(int n=0;n<nel && ierr==0;++n) {
char ic=4;
if( faces[n].nodes[3] < 0 ) ic=3;
long node_pairs[4][2] = {{-2,-2},{-2,-2},{-2,-2},{-2,-2}};
for(int k=0;k<4;++k) {
node_pairs[k][0] = faces[n].nodes[k];
node_pairs[k][1] = faces[n].nodes[0];
if( ic == 4 ) {
if( k < 3 ) node_pairs[k][1] = faces[n].nodes[k+1];
} else {
if( k < 2 ) node_pairs[k][1] = faces[n].nodes[k+1];
}
}
// allocate edges individually
sMesh_Edge* edges_ptr[4] = {NULL, NULL, NULL, NULL};
unsigned char edges_dir[4] = {9,9,9,9};
for(int k=0;k<ic && ierr==0;++k) {
edgeid_t eid;
int idir = set_edge_uid( eid, (unsigned long) node_pairs[k][0],
(unsigned long) node_pairs[k][1] );
edges_dir[k] = (unsigned char) idir;
edgeid_map_it eit;
if( (eit = edgeid_map.find( eid )) == edgeid_map.end() ) {
long nid1 = node_pairs[k][0];
long nid2 = node_pairs[k][1];
if( nid1 > nid2 ) {
nid2 = node_pairs[k][0];
nid1 = node_pairs[k][1];
}
sMesh_Node* np1 = (sMesh_Node*) node_uid_map[ nid1 ];
sMesh_Node* np2 = (sMesh_Node*) node_uid_map[ nid2 ];
sMesh_Edge* ep = new sMesh_Edge( np1, np2 );
if( ep == NULL ) { ierr=-121; break; }
ep->computeLength();
edge_uid_map[ ep->getUID() ] = ep;
edge_ptr_map[ ep ] = ep->getUID();
edgeid_map[ eid ] = ep;
edges_ptr[k] = ep;
} else {
edges_ptr[k] = (sMesh_Edge*) eit->second;
}
// set side bits
if( idir == 1 ) edges_ptr[k]->flags |= 0x01;
else edges_ptr[k]->flags |= 0x02;
}
// create the face object
unsigned dirs_=0x00;
for(int k=0;k<ic;++k) if( edges_dir[k] == 2 ) dirs_ |= 0x01 << (3 - k);
if( ic==4 ) {
sMesh_Quad* qp = new sMesh_Quad( edges_ptr[0], edges_ptr[1],
edges_ptr[2], edges_ptr[3],
dirs_ << 4 );
if( qp == NULL ) { ierr=-131; break; }
quad_uid_map[ qp->getUID() ] = qp;
quad_ptr_map[ qp ] = qp->getUID();
} else {
sMesh_Tri* tp = new sMesh_Tri( edges_ptr[0], edges_ptr[1],
edges_ptr[2], dirs_ << 4 );
if( tp == NULL ) { ierr=-131; break; }
tri_uid_map[ tp->getUID() ] = tp;
tri_ptr_map[ tp ] = tp->getUID();
}
}
if( ierr ) {
FPRINTF( stdout, " [Error] Something went really wrong... \n" );
return ierr;
}
return 0;
}
//
// Public method to receive arrays of node and element data (that is provided
// in a conventional sparse format) and generate the internals of a mesh
// object.
//
int sMesh_Core::quadify()
{
struct trih_s { unsigned char attr; double s; };
std::map< sMesh_Tri*, struct trih_s > tri_map_rules;
for(ptrmap_it it=tri_ptr_map.begin(); it!=tri_ptr_map.end();++it) {
sMesh_Tri* tp = (sMesh_Tri*) it->first;
unsigned char attr;
double tmp=0.0;
tp->computeHeuristics( attr, tmp );
// when any of the angles is obtuse
if( attr & 0x07 ) {
tri_map_rules[ tp ] = { attr, tmp };
}
}
int ierr=0;
std::map< sMesh_Tri*, struct trih_s > :: const_iterator it;
for( it=tri_map_rules.begin(); it!=tri_map_rules.end(); ++it) {
// find out which edge needs to be subdivided
unsigned char attr = it->second.attr;
int index=-2;
if( attr & 0x04 ) index=0;
if( attr & 0x02 ) { if( index==-2 ) { index=1; } else { index=-1; } }
if( attr & 0x01 ) { if( index==-2 ) { index=2; } else { index=-1; } }
// Should return an index {0,1,2}
// "-1" is an error of duplicate obtuse angle!
if( index < 0 ) {
FPRINTF( stdout, " [Error] Invalid Rule 3 index %d \n", ierr );
ierr=999;
break;
} else {
ierr=subdivideByRule3( it->first, index );
}
}
if( ierr ) {
FPRINTF( stdout, " [Error] Something went really wrong... \n" );
return 1;
}
tri_map_rules.clear();
for(ptrmap_it it=tri_ptr_map.begin(); it!=tri_ptr_map.end();++it) {
sMesh_Tri* tp = (sMesh_Tri*) it->first;
unsigned char sattr = tp->getSubdivAttr();
if( (sattr & 0x0F) == 0 ) {
tri_map_rules[ tp ] = { sattr, 0.0 };
}
}
for( it=tri_map_rules.begin(); it!=tri_map_rules.end(); ++it) {
ierr=subdivideByRule1( it->first );
if( ierr ) break;
}
if( ierr ) {
FPRINTF( stdout, " [Error] Something went really wrong... \n" );
return 1;
}
tri_map_rules.clear();
char ic=1; // iteration
while( ic && ierr==0 ) {
std::map< sMesh_Quad*, unsigned char > quad_map_rules;
for(ptrmap_it it=quad_ptr_map.begin(); it!=quad_ptr_map.end();++it) {
sMesh_Quad* qp = (sMesh_Quad*) it->first;
unsigned char uc = (unsigned char) qp->needsSubdivision();
if( uc ) quad_map_rules[ qp ] = uc;
}
if( quad_map_rules.size() == 0 ) ic=0; // iteration termination
for(std::map< sMesh_Quad*, unsigned char > :: const_iterator
it=quad_map_rules.begin(); it!=quad_map_rules.end();++it) {
sMesh_Quad* qp = (sMesh_Quad*) it->first;
if( it->second == 1 ) {
ierr=subdivideByRule2u( qp );
} else if( it->second == 2 ) {
ierr=subdivideByRule2v( qp );
} else if( it->second == 3 ) {
// We will force a "u-subdivision" and allow the iteration to pick
// up the subdivisions of the child quads in the next epoch.
ierr=subdivideByRule2u( qp );
} else {
ierr=999;
}
if( ierr ) {
ic=0;
break;
}
}
#ifdef _DEBUG_
dumpEdges( "edges.dat", 1 );
#endif
}
if( ierr ) {
FPRINTF( stdout, " [Error] Something went really wrong... \n" );
return 1;
}
return 0;
}
//
// Function that performs subdivision by "rule 3" given an angle index
//
int sMesh_Core::subdivideByRule3( sMesh_Tri* p, int index )
{
if( p == NULL ) {
FPRINTF( stdout, " [Error] Triangle pointer is null \n" );
return 1;
}
if( index < 0 || 3 < index ) {
FPRINTF( stdout, " [Error] Bad index (%d) in R3 subdivision\n",
index );
return 2;
}
const unsigned char bit7 = 0x01 << 7; // picks flags for face 0
const unsigned char bit6 = 0x01 << 6; // picks flags for face 1
const unsigned char bit5 = 0x01 << 5; // picks flags for face 2
// get all the things we know about this triangle that we will need
unsigned char eattr = p->getEdgeAttr();
unsigned char sdir=0; // direction of split edge
sMesh_Edge* sep = p->getEdgePtr( index ); // edge to split
sMesh_Edge* oep1=NULL,* oep2=NULL; // lateral edges of big triangle
sMesh_Node* np=NULL; // opposite node
unsigned char dirs[2] = { 0x00, 0x00 }; // edge directions of new tris
if( index == 0 ) {
if( eattr & bit7 ) sdir=1;
oep1 = p->getEdgePtr(2); // /| Conventions
oep2 = p->getEdgePtr(1); // / | 0: 2 & 1
if( eattr & bit5 ) dirs[0] |= bit7; // 0 / | 2
if( eattr & bit6 ) dirs[1] |= bit5; // / | 1: 0 & 2
if( eattr & bit5 ) { // /____|
np = oep1->getNodePtr(2); // 1 2: 1 & 0
} else {
np = oep1->getNodePtr(1);
}
{ sMesh_Node* tmp=NULL; // sanity check block
if( eattr & bit6 ) tmp = oep2->getNodePtr(1);
else tmp = oep2->getNodePtr(2);
if( tmp == np ) {
} else {
FPRINTF( stdout, " [Error] Opposite node: %ld \n", np->getUID() );
return 999;
} } // endof sanity check block
} else if( index == 1 ) {
if( eattr & bit6 ) sdir=1;
oep1 = p->getEdgePtr(0);
oep2 = p->getEdgePtr(2);
if( eattr & bit7 ) dirs[0] |= bit7;
if( eattr & bit5 ) dirs[1] |= bit5;
if( eattr & bit7 ) {
np = oep1->getNodePtr(2);
} else {
np = oep1->getNodePtr(1);
}
{ sMesh_Node* tmp=NULL; // sanity check block
if( eattr & bit5 ) tmp = oep2->getNodePtr(1);
else tmp = oep2->getNodePtr(2);
if( tmp == np ) {
} else {
FPRINTF( stdout, " [Error] Opposite node: %ld \n", np->getUID() );
return 999;
} } // endof sanity check block
} else {
if( eattr & bit5 ) sdir=1;
oep1 = p->getEdgePtr(1);
oep2 = p->getEdgePtr(0);
if( eattr & bit6 ) dirs[0] |= bit7;
if( eattr & bit7 ) dirs[1] |= bit5;
if( eattr & bit6 ) {
np = oep1->getNodePtr(2);
} else {
np = oep1->getNodePtr(1);
}
{ sMesh_Node* tmp=NULL; // sanity check block
if( eattr & bit7 ) tmp = oep2->getNodePtr(1);
else tmp = oep2->getNodePtr(2);
if( tmp == np ) {
} else {
FPRINTF( stdout, " [Error] Opposite node: %ld \n", np->getUID() );
return 999;
} } // endof sanity check block
}
// spliting edge
sMesh_Node* mnp=NULL; // new or existing node at midpoint
sMesh_Edge *sep1=NULL,*sep2=NULL; // new edges splitting the original one
if( sep->isSplit() ) {
} else {
int iret = sep->split( &node_uid_map, &node_ptr_map,
&edge_uid_map, &edge_ptr_map );
if( iret ) {
FPRINTF( stdout, " [Error] Fatal in splitting edge attempt \n" );
return 4;
}
}
// get pointers to the two edges
sep1 = sep->getChildPtr(1);
sep2 = sep->getChildPtr(2);
// orientations of split edges in the new triangles
if( sdir ) {
if( sep2->getNodePtr(1) != sep->getNodePtr(2) ) dirs[0] |= bit6;
if( sep1->getNodePtr(2) != sep->getNodePtr(1) ) dirs[1] |= bit6;
} else {
if( sep1->getNodePtr(1) != sep->getNodePtr(1) ) dirs[0] |= bit6;
if( sep2->getNodePtr(2) != sep->getNodePtr(2) ) dirs[1] |= bit6;
}
// locate middle node
if( sep1->getNodePtr(1) == sep->getNodePtr(1) ) {
mnp = sep1->getNodePtr(2);
} else {
mnp = sep1->getNodePtr(1);
}
if( mnp == sep->getNodePtr(1) || mnp == sep->getNodePtr(2) ) {
FPRINTF( stdout, " [Error] Midpoint node pointer error \n" );
return 999;
}
// create bisecting edge
sMesh_Edge* bep = new sMesh_Edge( np, mnp ); // new bisecting edge
if( mnp == NULL ) {
FPRINTF( stdout, " [Error] Could not create edge object \n" );
return -4;
}
edge_uid_map[ bep->getUID() ] = bep;
edge_ptr_map[ bep ] = bep->getUID();
// setting edge attribute of this edge for the triangles
if( bep->getNodePtr(2) == np ) {
dirs[1] |= bit7;
} else {
dirs[0] |= bit5;
}
// create two triangles
sMesh_Tri *tp1=NULL, *tp2=NULL;
if( sdir ) {
tp1 = new sMesh_Tri( oep1, sep2, bep, dirs[0] );
tp2 = new sMesh_Tri( bep, sep1, oep2, dirs[1] );
} else {
tp1 = new sMesh_Tri( oep1, sep1, bep, dirs[0] );
tp2 = new sMesh_Tri( bep, sep2, oep2, dirs[1] );
}
if( tp1 == NULL || tp2 == NULL ) {
FPRINTF( stdout, " [Error] Could not create triangle object \n" );
if( tp1 != NULL ) delete tp1;
if( tp2 != NULL ) delete tp2;
return -5;
}
tri_uid_map[ tp1->getUID() ] = tp1;
tri_ptr_map[ tp1 ] = tp1->getUID();
tri_uid_map[ tp2->getUID() ] = tp2;
tri_ptr_map[ tp2 ] = tp2->getUID();
// Area elements pointer assignments
unsigned char sa = 1;
if( index == 1 ) sa = 2;
else if( index == 2 ) sa = 3;
ChildSetToken token;
p->setSubdivision( sa, token );
p->setChildren( 0, tp1, token );
p->setChildren( 1, tp2, token );
return 0;
}
//
// Function that performs subdivision by "rule 1"
//
int sMesh_Core::subdivideByRule1( sMesh_Tri* p )
{
if( p == NULL ) {
FPRINTF( stdout, " [Error] Triangle pointer is null \n" );
return 1;
}
const unsigned char bit7 = 0x01 << 7; // picks flags for face 0
const unsigned char bit6 = 0x01 << 6; // picks flags for face 1
const unsigned char bit5 = 0x01 << 5; // picks flags for face 2
const unsigned char bit4 = 0x01 << 4; // picks flags for face 3
sMesh_Node* cnp = new sMesh_Node(); // "centroid" node
if( cnp == NULL ) {
FPRINTF( stdout, " [Error] Could not create node object \n" );
return -1;
}
node_uid_map[ cnp->getUID() ] = cnp;
node_ptr_map[ cnp ] = cnp->getUID();
cnp->x = 0.0; cnp->y = 0.0; cnp->z = 0.0;
cnp->flags |= bit7;
// get all the things we know about this triangle that we will need
unsigned char eattr = p->getEdgeAttr();
unsigned char dirs[3] = {0x00, 0x00, 0x00}; // edge directions of 3 new quads
sMesh_Edge* qep[3][4] = {{ NULL, NULL, NULL, NULL }, // edges of quads
{ NULL, NULL, NULL, NULL },
{ NULL, NULL, NULL, NULL }};
for(int k=0;k<3;++k) { // sweep over edges
sMesh_Edge* sep = p->getEdgePtr(k);
unsigned char sdir=0, eqA=4, eqB=4;
if( k==0 ) {
sdir = (eattr & bit7) >> 7;
eqA=0; eqB=1;
} else if( k==1 ) {
sdir = (eattr & bit6) >> 6;
eqA=1; eqB=2;
} else {
sdir = (eattr & bit5) >> 5;
eqA=2; eqB=0;
}
cnp->x += (sep->getNodePtr(1)->x + sep->getNodePtr(2)->x)/6.0;
cnp->y += (sep->getNodePtr(1)->y + sep->getNodePtr(2)->y)/6.0;
cnp->z += (sep->getNodePtr(1)->z + sep->getNodePtr(2)->z)/6.0;
if( sep->isSplit() ) {
} else {
int iret = sep->split( &node_uid_map, &node_ptr_map,
&edge_uid_map, &edge_ptr_map );
if( iret ) {
FPRINTF( stdout, " [Error] Fatal in splitting edge attempt \n" );
return 2;
}
}
// get pointers to the two edges
sMesh_Edge* sep1 = sep->getChildPtr(1);
sMesh_Edge* sep2 = sep->getChildPtr(2);
// orientations and pointers of split edges in the new triangles
if( sdir ) {
if( sep2->getNodePtr(1) != sep->getNodePtr(2) ) dirs[eqA] |= bit7;
if( sep1->getNodePtr(2) != sep->getNodePtr(1) ) dirs[eqB] |= bit4;
qep[eqA][0] = sep2;
qep[eqB][3] = sep1;
} else {
if( sep1->getNodePtr(1) != sep->getNodePtr(1) ) dirs[eqA] |= bit7;
if( sep2->getNodePtr(2) != sep->getNodePtr(2) ) dirs[eqB] |= bit4;
qep[eqA][0] = sep1;
qep[eqB][3] = sep2;
}
// locate midpoint node
sMesh_Node* mnp=NULL;
if( sep1->getNodePtr(1) == sep->getNodePtr(1) ) {
mnp = sep1->getNodePtr(2);
} else {
mnp = sep1->getNodePtr(1);
}
// create mid-point connecting edge
sMesh_Edge* tmp = new sMesh_Edge( cnp, mnp );
if( tmp == NULL ) {
FPRINTF( stdout, " [Error] Could not create edge object \n" );
return -2;
}
edge_uid_map[ tmp->getUID() ] = tmp;
edge_ptr_map[ tmp ] = tmp->getUID();
// store pointers to new edge
qep[eqA][1] = tmp;
qep[eqB][2] = tmp;
if( mnp->getUID() > cnp->getUID() ) {
dirs[eqA] |= bit6;
} else {
dirs[eqB] |= bit5;
}
}
// create three quadrilaterals
for(int k=0;k<3;++k) {
sMesh_Quad* tmp = new sMesh_Quad( qep[k][0], qep[k][1],
qep[k][2], qep[k][3], dirs[k] );
if( tmp == NULL ) {
FPRINTF( stdout, " [Error] Could not create area object \n" );
return -3;
}
quad_uid_map[ tmp->getUID() ] = tmp;
quad_ptr_map[ tmp ] = tmp->getUID();
}
return 0;
}
//
// Function that performs subdivision by "rule 2" in the "u" direction
//
int sMesh_Core::subdivideByRule2u( sMesh_Quad* p )
{
if( p == NULL ) {
FPRINTF( stdout, " [Error] Quadrilateral pointer is null \n" );
return 1;
}