-
Notifications
You must be signed in to change notification settings - Fork 21
/
octree.cpp
1935 lines (1656 loc) · 50.3 KB
/
octree.cpp
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
/*
Implementations of Octree member functions.
Copyright (C) 2011 Tao Ju
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
(LGPL) as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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 Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <iostream>
#include <cassert>
#include "octree.hpp"
#include "PLYWriter.hpp"
#if _WIN64 || __x86_64__ || __ppc64__
#define ENVIRONMENT64
#else
#define ENVIRONMENT32
#endif
#ifdef ENVIRONMENT64
typedef long int ptr_type;
#else
typedef int ptr_type;
#endif
Octree::Octree( char* fname, double threshold )
{
simplify_threshold = threshold;
// Recognize file format
/*
if ( strstr( fname, ".sog" ) != NULL || strstr( fname, ".SOG" ) != NULL ) {
printf("Reading SOG file format.\n") ;
this->hasQEF = 0 ;
readSOG( fname ) ;
}*/
if ( strstr( fname, ".dcf" ) != NULL || strstr( fname, ".DCF" ) != NULL ) {
printf("Reading DCF file format.\n") ;
this->hasQEF = 1 ;
readDCF( fname ) ;
}
else {
printf("Wrong input format. Must be SOG/DCF.\n");
exit(0) ;
}
}
void Octree::simplify( float thresh ) {
if ( this->hasQEF ) {
int st[3] = {0,0,0} ;
this->root = simplify( this->root, st, this->dimen, thresh ) ;
}
}
// simplify by collapsing nodes where the parent node QEF solution is good enough
OctreeNode* Octree::simplify( OctreeNode* node, int st[3], int len, float thresh ) {
if ( node == NULL )
return NULL ;
//NodeType type = ;
if ( node->getType() == INTERNAL ) {
InternalNode* inode = (InternalNode*)node ;
int simple = 1;
// QEF data
float ata[6] = { 0, 0, 0, 0, 0, 0 };
float atb[3] = { 0, 0, 0 } ;
float pt[3] = { 0, 0, 0 } ;
//float mp[3] = { 0, 0, 0 } ;
float btb = 0 ;
int signs[8] = {-1,-1,-1,-1,-1,-1,-1,-1} ;
int midsign = -1 ;
// child data
int nlen = len / 2 ;
int nst[3] ;
int ec = 0 ;
int ht ;
for ( int i = 0 ; i < 8 ; i ++ ) { // recurse into tree
nst[0] = st[0] + vertMap[i][0] * nlen ;
nst[1] = st[1] + vertMap[i][1] * nlen ;
nst[2] = st[2] + vertMap[i][2] * nlen ;
inode->child[i] = simplify( inode->child[i], nst, nlen, thresh ) ;
if ( inode->child[i] != NULL ) {
if ( inode->child[i]->getType() == INTERNAL ) {
simple = 0 ;
}
else if ( inode->child[i]->getType() == LEAF ) { // sum child leaf QEFs
LeafNode* lnode = (LeafNode *) inode->child[i] ;
ht = lnode->height ;
for ( int j = 0 ; j < 6 ; j ++ )
ata[j] += lnode->ata[j] ;
for ( int j = 0 ; j < 3 ; j ++ ) {
atb[j] += lnode->atb[j] ;
pt[j] += lnode->mp[j] ;
}
if ( lnode->mp[0] == 0 )
printf("%f %f %f, Height: %d\n", lnode->mp[0], lnode->mp[1], lnode->mp[2], ht) ;
btb += lnode->btb ;
ec++ ; // QEF count (?)
midsign = lnode->getSign( 7 - i ) ;
signs[i] = lnode->getSign( i ) ;
}
else { // pseudoleaf
assert( inode->child[i]->getType() == PSEUDOLEAF );
PseudoLeafNode* pnode = (PseudoLeafNode *) inode->child[i];
ht = pnode->height ;
for ( int j = 0 ; j < 6 ; j ++ )
ata[j] += pnode->ata[j] ;
for ( int j = 0 ; j < 3 ; j ++ ) {
atb[j] += pnode->atb[j] ;
pt[j] += pnode->mp[j] ;
}
btb += pnode->btb ;
ec ++ ;
midsign = pnode->getSign( 7 - i ) ;
signs[i] = pnode->getSign( i ) ;
}
}
} // all QEFs summed
if ( simple ) { // one or more child INTERNAL (?)
if ( ec == 0 ) { // no QEFs found/summed above ( all childs INTERNAL ?)
//printf("deleting INTERNAL node because all children INTERNAL\n");
delete node;
return NULL;
}
else {
pt[0] = pt[0] / ec; // average of summed points
pt[1] = pt[1] / ec;
pt[2] = pt[2] / ec;
//if ( pt[0] < st[0] || pt[1] < st[1] || pt[2] < st[2] ||
// pt[0] > st[0] + len || pt[1] > st[1] + len || pt[2] > st[2] + len )
//{ // pt outside node cube
//printf("Out! %f %f %f, Box: (%d %d %d) Len: %d ec: %d\n", pt[0], pt[1], pt[2], st[0],st[1],st[2],len,ec) ;
//}
unsigned char sg = 0 ;
for ( int i = 0 ; i < 8 ; i ++ ) {
if ( signs[i] == 1 )
sg |= ( 1 << i ) ;
else if ( signs[i] == -1 ) { // Undetermined, use center sign instead
if ( midsign == 1 )
sg |= ( 1 << i ) ;
else if ( midsign == -1 )
printf("Wrong!");
}
}
// Solve QEF for parent node
float mat[10];
BoundingBoxf* box = new BoundingBoxf();
box->begin.x = (float) st[0] ;
box->begin.y = (float) st[1] ;
box->begin.z = (float) st[2] ;
box->end.x = (float) st[0] + len ;
box->end.y = (float) st[1] + len ;
box->end.z = (float) st[2] + len ;
// pt is the average of child-nodes
// mp is the new solution point
float mp[3] = { 0, 0, 0 } ;
float error = calcPoint( ata, atb, btb, pt, mp, box, mat ) ;
#ifdef CLAMP
if ( mp[0] < st[0] || mp[1] < st[1] || mp[2] < st[2] || // mp is outside boudning-box
mp[0] > st[0] + len || mp[1] > st[1] + len || mp[2] > st[2] + len ) {
mp[0] = pt[0] ;
mp[1] = pt[1] ;
mp[2] = pt[2] ;
}
#endif
if ( error <= thresh ) { // if parent QEF solution is good enough
PseudoLeafNode* pnode = new PseudoLeafNode( ht+1, sg, ata, atb, btb, mp ) ;
delete inode ;
return pnode ;
}
else { // QEF solution not good enough
return node ;
}
}
} else { // simple == 0
return node ;
}
} else { // type != INTERNAL
return node ;
}
}
void Octree::readDCF( char* fname ) {
FILE* fin = fopen( fname, "rb" ) ;
if ( fin == NULL )
printf("Can not open file %s.\n", fname) ;
// Process header
char version[10] ;
fread( version, sizeof( char ), 10, fin ) ;
if ( strcmp( version, "multisign" ) != 0 ) {
printf("Wrong DCF version.\n") ;
exit(0) ;
}
fread( &(this->dimen), sizeof( int ), 1, fin ) ;
fread( &(this->dimen), sizeof( int ), 1, fin ) ;
fread( &(this->dimen), sizeof( int ), 1, fin ) ;
this->maxDepth = 0 ;
int temp = 1 ;
while ( temp < this->dimen ) {
maxDepth ++ ;
temp <<= 1 ;
}
printf(" dimen: %d maxDepth: %d\n", this->dimen, maxDepth ) ;
// Recursive reader
int st[3] = {0, 0, 0} ;
this->root = readDCF( fin, st, dimen, maxDepth ) ;
int nodecount[3];
countNodes( nodecount );
std::cout << " Read nodes from file: Internal " << nodecount[0] << "\tPseudo " << nodecount[1] << "\tLeaf " << nodecount[2] << "\n";
// optional octree simplification
if (simplify_threshold > 0 ) {
std::cout << "Simplifying with threshold " << simplify_threshold << "\n";
int nodecount1[3],nodecount2[3];
countNodes( nodecount1 );
std::cout << " Before simplify: Internal " << nodecount1[0] << "\tPseudo " << nodecount1[1] << "\tLeaf " << nodecount1[2] << "\n";
simplify( simplify_threshold );
countNodes( nodecount2 );
std::cout << " After simplify: Internal " << nodecount2[0] << "\tPseudo " << nodecount2[1] << "\tLeaf " << nodecount2[2] << "\n";
std::cout << " Nodecount I+P+L reduced from " << nodecount1[0]+nodecount1[1]+nodecount1[2] << " to " << nodecount2[0]+nodecount2[1]+nodecount2[2] << "\n";
}
printf("Done reading.\n") ;
fclose( fin ) ;
}
// only InternalNode and LeafNode returned by this function
// st cube corner (x,y,z)
// len cube side length
// ht node depth (root has 0, leaf has maxDepth)
//
// recursive reader
OctreeNode* Octree::readDCF( FILE* fin, int st[3], int len, int height ) {
OctreeNode* rvalue = NULL ;
int type ;
fread( &type, sizeof( int ), 1, fin ) ; // Get type
//printf("%d %d (%02d, %02d, %02d) NodeType: %d\n", height, len, st[0], st[1], st[2], type);
if ( type == 0 ) { // Internal node
rvalue = new InternalNode() ;
int child_len = len / 2 ; // len of child node is half that of parent
int child_st[3] ;
for ( int i = 0 ; i < 8 ; i ++ ) { // create eight child nodes
child_st[0] = st[0] + vertMap[i][0] * child_len; // child st position
child_st[1] = st[1] + vertMap[i][1] * child_len;
child_st[2] = st[2] + vertMap[i][2] * child_len;
((InternalNode *)rvalue)->child[i] = readDCF( fin, child_st, child_len, height - 1 ) ; // height is one less than parent
}
return rvalue ;
}
else if ( type == 1 ) { // Empty node,
short sg ;
fread( &sg, sizeof( short ), 1, fin ) ; // signs not used??
assert( rvalue == NULL );
return rvalue ;
}
else if ( type == 2 ) { // Leaf node
short rsg[8] ;
fread( rsg, sizeof( short ), 8, fin ) ;
unsigned char sg = 0 ;
for ( int i = 0 ; i < 8 ; i ++ ) { // set signs
if ( rsg[i] != 0 )
sg |= ( 1 << i ) ;
}
// intersections and normals
float inters[12][3], norms[12][3] ;
int numinters = 0;
for ( int i = 0 ; i < 12 ; i ++ ) { // potentially there are 12 intersections, one for each edge of the cube
int num = 0;
fread( &num, sizeof( int ), 1, fin ) ;
//if ( num > 0 ) {
for ( int j = 0 ; j < num ; j ++ ) {
float off ;
fread( &off, sizeof( float ), 1, fin ) ;
fread( norms[numinters], sizeof( float ), 3, fin ) ; // normals
int dir = i / 4 ;
int base = edgevmap[ i ][ 0 ] ;
inters[numinters][0] = st[0] + vertMap[base][0] * len ;
inters[numinters][1] = st[1] + vertMap[base][1] * len ;
inters[numinters][2] = st[2] + vertMap[base][2] * len ;
inters[numinters][dir] += off ;
numinters ++ ;
}
//}
}
if ( numinters > 0 )
rvalue = new LeafNode( height, sg, st, len, numinters, inters, norms ) ;
else
rvalue = NULL ;
return rvalue ;
}
else {
printf("Wrong! Node Type: %d\n", type);
exit(-1);
}
}
// no-intersections algorithm
// fname is the PLY output file
void Octree::genContourNoInter2( char* fname ) {
int numTris = 0 ;
int numVertices = 0 ;
IndexedTriangleList* tlist = new IndexedTriangleList();
VertexList* vlist = new VertexList();
tlist->next = NULL ;
vlist->next = NULL ;
founds = 0 ;
news = 0 ;
// generate triangles
faceVerts = 0 ;
edgeVerts = 0 ;
HashMap* hash = new HashMap();
int st[3] = {0,0,0};
printf("Processing contour...\n") ;
clock_t start = clock( ) ;
// one cellProc call to root processes entire tree
cellProcContourNoInter2( root, st, dimen, hash, tlist, numTris, vlist, numVertices ) ;
clock_t finish = clock( ) ;
printf("Time used: %f seconds.\n", (float) (finish - start) / (float) CLOCKS_PER_SEC ) ;
printf("Face vertices: %d Edge vertices: %d\n", faceVerts, edgeVerts ) ;
printf("New hash entries: %d. Found times: %d\n", news, founds) ;
// Finally, turn into PLY
FILE* fout = fopen ( fname, "wb" ) ;
printf("Vertices counted: %d Triangles counted: %d \n", numVertices, numTris ) ;
PLYWriter::writeHeader( fout, numVertices, numTris ) ;
VertexList* v = vlist->next ;
while ( v != NULL ) {
PLYWriter::writeVertex( fout, v->vt ) ;
v = v->next ;
}
IndexedTriangleList* t = tlist->next ;
for ( int i = 0 ; i < numTris ; i ++ ) {
int inds[] = {numVertices - 1 - t->vt[0], numVertices - 1 - t->vt[1], numVertices - 1 - t->vt[2]} ;
PLYWriter::writeFace( fout, 3, inds ) ;
t = t->next ;
}
fclose( fout ) ;
// Clear up
delete hash ;
v = vlist ;
while ( v != NULL ) {
vlist = v->next ;
delete v ;
v = vlist ;
}
t = tlist ;
while ( t != NULL ) {
tlist = t->next ;
delete t ;
t = tlist ;
}
}
// original algorithm
// may produce intersecting polygons?
void Octree::genContour( char* fname ) {
int numTris = 0 ;
int numVertices = 0 ;
FILE* fout = fopen ( fname, "wb" ) ;
cellProcCount ( root, numVertices, numTris ) ;
printf("numVertices: %d numTriangles: %d \n", numVertices, numTris ) ;
PLYWriter::writeHeader( fout, numVertices, numTris ) ;
int offset = 0; // start of vertex index
clock_t start = clock();
generateVertexIndex( root, offset, fout ); // write vertices to file, populate node->index
printf("Wrote %d vertices to file\n", offset ) ;
actualTris = 0 ;
cellProcContour( this->root, fout ) ; // a single call to root runs algorithm on entire tree
clock_t finish = clock();
printf("Time used: %f seconds.\n", (float) (finish - start) / (float) CLOCKS_PER_SEC ) ;
printf("Actual triangles written: %d\n", actualTris ) ;
fclose( fout ) ;
}
// this writes out octree vertices to the PLY file
// each vertex gets an index, which is stored in node->index
void Octree::generateVertexIndex( OctreeNode* node, int& offset, FILE* fout ) {
NodeType type = node->getType() ;
if ( type == INTERNAL ) { // Internal node, recurse into tree
InternalNode* inode = ( (InternalNode* ) node ) ;
for ( int i = 0 ; i < 8 ; i ++ ) {
if ( inode->child[i] != NULL )
generateVertexIndex( inode->child[i], offset, fout ) ;
}
}
else if ( type == LEAF ) { // Leaf node
LeafNode* lnode = ((LeafNode *) node) ;
PLYWriter::writeVertex( fout, lnode->mp ) ; // write out mp
lnode->index = offset;
offset++;
}
else if ( type == PSEUDOLEAF ) { // Pseudo leaf node
PseudoLeafNode* pnode = ((PseudoLeafNode *) node) ;
PLYWriter::writeVertex( fout, pnode->mp ) ; // write out mp
pnode->index = offset;
offset++;
}
}
// cellProcContour( this->root ) is the entry-point to the entire algorithm
void Octree::cellProcContour( OctreeNode* node, FILE* fout ) {
if ( node == NULL )
return ;
int type = node->getType() ;
if ( type == INTERNAL ) { // internal node
InternalNode* inode = (( InternalNode * ) node );
for ( int i = 0 ; i < 8 ; i ++ ) // 8 Cell calls on children
cellProcContour( inode->child[ i ], fout );
for ( int i = 0 ; i < 12 ; i ++ ) { // 12 face calls, faces between each child node
int c[ 2 ] = { cellProcFaceMask[ i ][ 0 ], cellProcFaceMask[ i ][ 1 ] };
OctreeNode* fcd[2];
fcd[0] = inode->child[ c[0] ] ;
fcd[1] = inode->child[ c[1] ] ;
faceProcContour( fcd, cellProcFaceMask[ i ][ 2 ], fout ) ;
}
for ( int i = 0 ; i < 6 ; i ++ ) { // 6 edge calls
int c[ 4 ] = { cellProcEdgeMask[ i ][ 0 ], cellProcEdgeMask[ i ][ 1 ], cellProcEdgeMask[ i ][ 2 ], cellProcEdgeMask[ i ][ 3 ] };
OctreeNode* ecd[4] ;
for ( int j = 0 ; j < 4 ; j ++ )
ecd[j] = inode->child[ c[j] ] ;
edgeProcContour( ecd, cellProcEdgeMask[ i ][ 4 ], fout ) ;
}
}
};
// node[2] are the two nodes that share a face
// dir comes from cellProcFaceMask[i][2] where i=0..11
void Octree::faceProcContour ( OctreeNode* node[2], int dir, FILE* fout ) {
// printf("I am at a face! %d\n", dir ) ;
if ( ! ( node[0] && node[1] ) ) {
// printf("I am none.\n") ;
return ;
}
NodeType type[2] = { node[0]->getType(), node[1]->getType() } ;
if ( type[0] == INTERNAL || type[1] == INTERNAL ) { // both nodes internal
// 4 face calls
OctreeNode* fcd[2] ;
for ( int i = 0 ; i < 4 ; i ++ ) {
int c[2] = { faceProcFaceMask[ dir ][ i ][ 0 ], faceProcFaceMask[ dir ][ i ][ 1 ] };
for ( int j = 0 ; j < 2 ; j ++ ) {
if ( type[j] > 0 )
fcd[j] = node[j];
else
fcd[j] = ((InternalNode *) node[ j ] )->child[ c[j] ];
}
faceProcContour( fcd, faceProcFaceMask[ dir ][ i ][ 2 ], fout ) ;
}
// 4 edge calls
int orders[2][4] = {{ 0, 0, 1, 1 }, { 0, 1, 0, 1 }} ;
OctreeNode* ecd[4] ;
for ( int i = 0 ; i < 4 ; i ++ ) {
int c[4] = { faceProcEdgeMask[ dir ][ i ][ 1 ], faceProcEdgeMask[ dir ][ i ][ 2 ],
faceProcEdgeMask[ dir ][ i ][ 3 ], faceProcEdgeMask[ dir ][ i ][ 4 ] };
int* order = orders[ faceProcEdgeMask[ dir ][ i ][ 0 ] ] ;
for ( int j = 0 ; j < 4 ; j ++ ) {
if ( type[order[j]] > 0 )
ecd[j] = node[order[j]] ;
else
ecd[j] = ( (InternalNode *) node[ order[ j ] ] )->child[ c[j] ] ;
}
edgeProcContour( ecd, faceProcEdgeMask[ dir ][ i ][ 5 ], fout ) ;
}
// printf("I am done.\n") ;
}
else {
// printf("I don't have any children.\n") ;
}
};
// a common edge between four nodes in node[4]
// "dir" comes from cellProcEdgeMask
void Octree::edgeProcContour ( OctreeNode* node[4], int dir, FILE* fout ) {
if ( ! ( node[0] && node[1] && node[2] && node[3] ) )
return;
NodeType type[4] = { node[0]->getType(), node[1]->getType(), node[2]->getType(), node[3]->getType() } ;
if ( type[0] != INTERNAL && type[1] != INTERNAL && type[2] != INTERNAL && type[3] != INTERNAL ) {
processEdgeWrite( node, dir, fout ) ; // a face (quad?) is output
} else {
// 2 edge calls
OctreeNode* ecd[4] ;
for ( int i = 0 ; i < 2 ; i ++ ) {
int c[ 4 ] = { edgeProcEdgeMask[ dir ][ i ][ 0 ],
edgeProcEdgeMask[ dir ][ i ][ 1 ],
edgeProcEdgeMask[ dir ][ i ][ 2 ],
edgeProcEdgeMask[ dir ][ i ][ 3 ] } ;
for ( int j = 0 ; j < 4 ; j ++ ) {
if ( type[j] > 0 )
ecd[j] = node[j] ;
else
ecd[j] = ((InternalNode *) node[j])->child[ c[j] ] ;
}
edgeProcContour( ecd, edgeProcEdgeMask[ dir ][ i ][ 4 ], fout ) ;
}
}
};
// this writes out a face to the PLY file
// vertices already exist in the file
// so here we write out topology only, i.e. sets of indices that form a face
void Octree::processEdgeWrite ( OctreeNode* node[4], int dir, FILE* fout ) {
// Get minimal cell
int type, ht, minht = this->maxDepth+1, mini = -1 ;
int ind[4], sc[4], flip[4] = {0,0,0,0} ;
int flip2;
for ( int i = 0 ; i < 4 ; i ++ ) {
if ( node[i]->getType() == LEAF ) {
LeafNode* lnode = ((LeafNode *) node[i]) ;
int ed = processEdgeMask[dir][i] ;
int c1 = edgevmap[ed][0] ;
int c2 = edgevmap[ed][1] ;
if ( lnode->height < minht ) {
minht = lnode->height ;
mini = i ;
if ( lnode->getSign(c1) > 0 )
flip2 = 1 ;
else
flip2 = 0 ;
}
ind[i] = lnode->index ;
if ( lnode->getSign( c1 ) == lnode->getSign( c2 ) )
sc[ i ] = 0 ;
else
sc[ i ] = 1 ;
// if ( lnode->getSign(c1) > 0 )
// {
// flip[ i ] = 1 ;
// }
// }
} else {
assert( node[i]->getType() == PSEUDOLEAF );
PseudoLeafNode* pnode = ((PseudoLeafNode *) node[i]) ;
int ed = processEdgeMask[dir][i] ;
int c1 = edgevmap[ed][0] ;
int c2 = edgevmap[ed][1] ;
if ( pnode->height < minht ) {
minht = pnode->height;
mini = i;
if ( pnode->getSign(c1) > 0 )
flip2 = 1 ;
else
flip2 = 0 ;
}
ind[i] = pnode->index ;
if ( pnode->getSign( c1 ) == pnode->getSign( c2 ) )
sc[ i ] = 0 ;
else
sc[ i ] = 1 ;
// if ( pnode->getSign(c1) > 0 )
// {
// flip[ i ] = 1 ;
// flip2 = 1 ;
// }
// }
}
}
if ( sc[ mini ] == 1 ) { // condition for any triangle output?
if ( flip2 == 0 ) {
actualTris ++ ;
if ( ind[0] == ind[1] ) { // two indices same, so output triangle
int tind[] = { ind[0], ind[3], ind[2] } ;
PLYWriter::writeFace( fout, 3, tind ) ;
} else if ( ind[1] == ind[3] ) {
int tind[] = { ind[0], ind[1], ind[2] } ;
PLYWriter::writeFace( fout, 3, tind ) ;
} else if ( ind[3] == ind[2] ) {
int tind[] = { ind[0], ind[1], ind[3] } ;
PLYWriter::writeFace( fout, 3, tind ) ;
} else if ( ind[2] == ind[0] ) {
int tind[] = { ind[1], ind[3], ind[2] } ;
PLYWriter::writeFace( fout, 3, tind ) ;
} else { // all indices unique, so output a quad by outputting two triangles
int tind1[] = { ind[0], ind[1], ind[3] } ;
PLYWriter::writeFace( fout, 3, tind1 ) ;
int tind2[] = { ind[0], ind[3], ind[2] } ;
PLYWriter::writeFace( fout, 3, tind2 ) ;
actualTris ++ ; // two triangles, so add one here also
}
} else {
actualTris ++ ;
if ( ind[0] == ind[1] ) {
int tind[] = { ind[0], ind[2], ind[3] } ;
PLYWriter::writeFace( fout, 3, tind ) ;
} else if ( ind[1] == ind[3] ) {
int tind[] = { ind[0], ind[2], ind[1] } ;
PLYWriter::writeFace( fout, 3, tind ) ;
} else if ( ind[3] == ind[2] ) {
int tind[] = { ind[0], ind[3], ind[1] } ;
PLYWriter::writeFace( fout, 3, tind ) ;
} else if ( ind[2] == ind[0] ) {
int tind[] = { ind[1], ind[2], ind[3] } ;
PLYWriter::writeFace( fout, 3, tind ) ;
} else {
int tind1[] = { ind[0], ind[3], ind[1] } ;
PLYWriter::writeFace( fout, 3, tind1 ) ;
int tind2[] = { ind[0], ind[2], ind[3] } ;
PLYWriter::writeFace( fout, 3, tind2 ) ;
actualTris++; // two triangles, so add one here also
}
}
}
};
// used initially for counting number of vertices
// genContour calls cellProcCount(root) and this is a recursive function
void Octree::cellProcCount( OctreeNode* node, int& nverts, int& nfaces ) {
if ( node == NULL )
return ;
int type = node->getType() ;
if (type != INTERNAL)
nverts ++ ; // !internal, so leaf or pseudoleaf node produces a vertex
else {
// recurse into tree
InternalNode* inode = (( InternalNode * ) node ) ;
for ( int i = 0 ; i < 8 ; i ++ ) // 8 recursive calls to child nodes
cellProcCount( inode->child[ i ], nverts, nfaces ) ;
OctreeNode* fcd[2];
for ( int i = 0 ; i < 12 ; i ++ ) { // 12 face calls. among the 8 child-nodes there are 12 common faces
int c[ 2 ] = { cellProcFaceMask[ i ][ 0 ], cellProcFaceMask[ i ][ 1 ] };
fcd[0] = inode->child[ c[0] ] ;
fcd[1] = inode->child[ c[1] ] ;
faceProcCount( fcd, cellProcFaceMask[ i ][ 2 ], nverts, nfaces ); // 2nd argument is "dir"
}
OctreeNode* ecd[4] ;
for ( int i = 0 ; i < 6 ; i ++ ) { // 6 edge calls
int c[ 4 ] = { cellProcEdgeMask[ i ][ 0 ], cellProcEdgeMask[ i ][ 1 ], cellProcEdgeMask[ i ][ 2 ], cellProcEdgeMask[ i ][ 3 ] };
for ( int j = 0 ; j < 4 ; j ++ )
ecd[j] = inode->child[ c[j] ] ;
edgeProcCount( ecd, cellProcEdgeMask[ i ][ 4 ], nverts, nfaces ) ;
}
}
};
void Octree::faceProcCount ( OctreeNode* node[2], int dir, int& nverts, int& nfaces ) {
if ( ! ( node[0] && node[1] ) )
return ;
int type[2] = { node[0]->getType(), node[1]->getType() } ;
if ( type[0] == INTERNAL || type[1] == INTERNAL ) {
OctreeNode* fcd[2] ;
for ( int i = 0 ; i < 4 ; i ++ ) { // 4 face calls, recursive!
int c[2] = { faceProcFaceMask[ dir ][ i ][ 0 ], faceProcFaceMask[ dir ][ i ][ 1 ] };
for ( int j = 0 ; j < 2 ; j ++ ) {
if ( type[j] != INTERNAL )
fcd[j] = node[j] ;
else
fcd[j] = ((InternalNode *) node[ j ] )->child[ c[j] ] ;
}
faceProcCount( fcd, faceProcFaceMask[ dir ][ i ][ 2 ], nverts, nfaces ) ;
}
int orders[2][4] = {{ 0, 0, 1, 1 }, { 0, 1, 0, 1 }} ;
OctreeNode* ecd[4] ;
for ( int i = 0 ; i < 4 ; i ++ ) { // 4 edge calls
int c[4] = { faceProcEdgeMask[ dir ][ i ][ 1 ], faceProcEdgeMask[ dir ][ i ][ 2 ],
faceProcEdgeMask[ dir ][ i ][ 3 ], faceProcEdgeMask[ dir ][ i ][ 4 ] };
int* order = orders[ faceProcEdgeMask[ dir ][ i ][ 0 ] ] ;
for ( int j = 0 ; j < 4 ; j ++ ) {
if ( type[order[j]] != INTERNAL )
ecd[j] = node[order[j]] ;
else
ecd[j] = ( (InternalNode *) node[ order[ j ] ] )->child[ c[j] ] ;
}
edgeProcCount( ecd, faceProcEdgeMask[ dir ][ i ][ 5 ], nverts, nfaces ) ;
}
}
};
void Octree::edgeProcCount ( OctreeNode* node[4], int dir, int& nverts, int& nfaces ) {
if ( ! ( node[0] && node[1] && node[2] && node[3] ) )
return ;
int type[4] = { node[0]->getType(), node[1]->getType(), node[2]->getType(), node[3]->getType() } ;
if ( type[0] != INTERNAL && type[1] != INTERNAL && type[2] != INTERNAL && type[3] != INTERNAL )
processEdgeCount( node, dir, nverts, nfaces ) ;
else {
// 2 edge calls
OctreeNode* ecd[4] ;
for ( int i = 0 ; i < 2 ; i ++ ) {
int c[ 4 ] = { edgeProcEdgeMask[ dir ][ i ][ 0 ],
edgeProcEdgeMask[ dir ][ i ][ 1 ],
edgeProcEdgeMask[ dir ][ i ][ 2 ],
edgeProcEdgeMask[ dir ][ i ][ 3 ] } ;
for ( int j = 0 ; j < 4 ; j ++ ) {
if ( type[j] != INTERNAL )
ecd[j] = node[j] ;
else
ecd[j] = ((InternalNode *) node[j])->child[ c[j] ] ;
}
edgeProcCount( ecd, edgeProcEdgeMask[ dir ][ i ][ 4 ], nverts, nfaces ) ;
}
}
};
void Octree::processEdgeCount ( OctreeNode* node[4], int dir, int& nverts, int& nfaces ) {
// Get minimal cell
int i, type, ht, minht = maxDepth+1, mini = -1 ;
int ind[4], sc[4], flip[4] = {0,0,0,0} ;
for ( i = 0 ; i < 4 ; i ++ ) {
if ( node[i]->getType() == 1 ) {
LeafNode* lnode = ((LeafNode *) node[i]) ;
if ( lnode->height < minht ) {
minht = lnode->height ;
mini = i ;
}
ind[i] = lnode->index ;
int ed = processEdgeMask[dir][i] ;
int c1 = edgevmap[ed][0] ;
int c2 = edgevmap[ed][1] ;
if ( lnode->getSign( c1 ) == lnode->getSign( c2 ) ) {
sc[ i ] = 0 ;
}
else {
sc[ i ] = 1 ;
if ( lnode->getSign(c1) > 0 )
flip[ i ] = 1 ;
}
}
else {
PseudoLeafNode* pnode = ((PseudoLeafNode *) node[i]) ;
if ( pnode->height < minht ) {
minht = pnode->height ;
mini = i ;
}
ind[i] = pnode->index ;
int ed = processEdgeMask[dir][i] ;
int c1 = edgevmap[ed][0] ;
int c2 = edgevmap[ed][1] ;
if ( pnode->getSign( c1 ) == pnode->getSign( c2 ) ) {
sc[ i ] = 0 ;
}
else {
sc[ i ] = 1 ;
if ( pnode->getSign(c1) > 0 )
flip[ i ] = 1 ;
}
}
}
if ( sc[ mini ] == 1 ) {
nfaces ++ ; // triangle
if ( node[0] != node[1] && node[1] != node[3] && node[3] != node[2] && node[2] != node[0] )
nfaces ++ ; // quad, so two triangles
}
};
/************************************************************************/
/* Start intersection free algorithm */
/************************************************************************/
int Octree::testFace( int st[3], int len, int dir, float v1[3], float v2[3] )
{
#ifdef TESS_UNIFORM
return 0 ;
#endif
#ifdef TESS_NONE
return 1 ;
#endif
float vec[3] = { v2[0]-v1[0], v2[1]-v1[1], v2[2]-v1[2] } ;
float ax1[3], ax2[3] ;
float ed1[3]={0,0,0}, ed2[3] = {0,0,0};
ed1[(dir+1)%3]=1;
ed2[(dir+2)%3]=1;
Intersection::cross( ed1, vec, ax1 ) ;
Intersection::cross( ed2, vec, ax2 ) ;
Triangle* t1 = new Triangle ;
Triangle* t2 = new Triangle ;
for ( int i = 0 ; i < 3 ; i ++ )
{
t1->vt[0][i] = v1[i] ;
t1->vt[1][i] = v2[i] ;
t1->vt[2][i] = v2[i] ;
t2->vt[0][i] = st[i] ;
t2->vt[1][i] = st[i] ;
t2->vt[2][i] = st[i] ;
}
t2->vt[1][(dir+1)%3] += len ;
t2->vt[2][(dir+2)%3] += len ;
if ( Intersection::separating( ax1, t1, t2 ) || Intersection::separating( ax2, t1, t2 ) )
{
faceVerts ++ ;
/*
printf("\n{{{%d, %d, %d},%d,%d},{{%f, %f, %f},{%f, %f, %f}}}\n",
st[0],st[1], st[2],
len, dir+1,
v1[0], v1[1], v1[2],
v2[0], v2[1], v2[2]) ;
*/
return 0 ;
}
else
{
return 1 ;
}
};
int Octree::testEdge( int st[3], int len, int dir, OctreeNode* node[4], float v[4][3] )
{
#ifdef TESS_UNIFORM
return 0 ;
#endif
#ifdef TESS_NONE
return 1 ;
#endif
if ( node[0] == node[1] || node[1] == node[3] || node[3] == node[2] || node[2] == node[0] )
{
// return 1 ;
}
float p1[3] ={st[0], st[1], st[2]};
float p2[3] ={st[0], st[1], st[2]};
p2[dir] += len ;
int nbr[]={0,1,3,2,0} ;
int nbr2[]={3,2,0,1,3} ;
float nm[3], vec1[4][3], vec2[4][3] ;
float d1, d2 ;
for ( int i = 0 ; i < 4 ; i ++ )
{
for ( int j = 0 ; j < 3 ; j ++ )
{
vec1[i][j] = v[i][j] - p1[j] ;
vec2[i][j] = v[i][j] - p2[j] ;
}
}
#ifdef EDGE_TEST_CONVEXITY
for ( i = 0 ; i < 4 ; i ++ )
{
int a = nbr[i] ;
int b = nbr[i+1] ;
int c = nbr2[i] ;
int d = nbr2[i+1] ;
if ( node[a] == node[b] )
{
continue ;
}
Intersection::cross( vec1[a], vec1[b], nm ) ;
d1 = Intersection::dot( vec1[c], nm ) ;
d2 = Intersection::dot( vec1[d], nm ) ;
if ( d1 * d2 < 0 )
{
/*
printf("1\n{{{%f, %f, %f},{%f, %f, %f}},{{%f, %f, %f},{%f, %f, %f},{%f, %f, %f},{%f, %f, %f}}}\n",
p1[0], p1[1], p1[2],
p2[0], p2[1], p2[2],
v[0][0], v[0][1], v[0][2],
v[1][0], v[1][1], v[1][2],
v[2][0], v[2][1], v[2][2],
v[3][0], v[3][1], v[3][2]) ;
*/
return 0 ;
}
Intersection::cross( vec2[a], vec2[b], nm ) ;
d1 = Intersection::dot( vec2[c], nm ) ;
d2 = Intersection::dot( vec2[d], nm ) ;
if ( d1 * d2 < 0 )
{
/*
printf("2\n{{{%f, %f, %f},{%f, %f, %f}},{{%f, %f, %f},{%f, %f, %f},{%f, %f, %f},{%f, %f, %f}}}\n",
p1[0], p1[1], p1[2],
p2[0], p2[1], p2[2],
v[0][0], v[0][1], v[0][2],
v[1][0], v[1][1], v[1][2],
v[2][0], v[2][1], v[2][2],
v[3][0], v[3][1], v[3][2]) ;
*/
return 0 ;
}
}
#else
#ifdef EDGE_TEST_FLIPDIAGONAL
Triangle* t1 = new Triangle ;
Triangle* t2 = new Triangle ;
int tri[2][2][4] = {{{0,1,3,2},{3,2,0,1}},{{2,0,1,3},{1,3,2,0}}} ;
for ( i = 0 ; i < 2 ; i ++ )
{
int good = 1 ;
for ( int j = 0 ; j < 2 ; j ++ )