-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdual_contouring2.cpp
1380 lines (1216 loc) · 40.7 KB
/
dual_contouring2.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
#include "stdafx.h"
#pragma hdrstop
#include "QEF.h"
#include "dual_contouring2.h"
namespace DualContouring
{
void InitNode( Node2& _node )
{
mxUINT_LOOP_i(8) {
_node.kids[i] = NIL_NODE;
}
}
// empty nodes (i.e. internal nodes with no children) should be collapsed
bool IsBadNode( const Node2& _node )
{
return
_node.kids[0] == NIL_NODE && _node.kids[1] == NIL_NODE && _node.kids[2] == NIL_NODE && _node.kids[3] == NIL_NODE &&
_node.kids[4] == NIL_NODE && _node.kids[5] == NIL_NODE && _node.kids[6] == NIL_NODE && _node.kids[7] == NIL_NODE;
}
Octree_DC2::Octree_DC2()
{
}
Octree_DC2::~Octree_DC2()
{
m_nodes.Empty();
m_leaves.Empty();
m_QEFs.Empty();
}
void Octree_DC2::Build(
const AVolume* _volume,
QEF_Solver* _qef_solver,
OctStats &_octree_stats,
const Options& options
)
{
mxPROFILE_SCOPE("Build Octree");
DBGOUT("Building octree (radius: %f, min.cell: %f, QEF threshold: %f)",
options.radius, options.minSubdiv, options.qef_threshold);
const UINT32 startTimeMSec = mxGetTimeInMilliseconds();
m_nodes.Empty();
m_leaves.Empty();
m_QEFs.Empty();
OctCubeF worldBounds;
worldBounds.x = 0;
worldBounds.y = 0;
worldBounds.z = 0;
worldBounds.radius = options.radius;
NodeID rootNodeID = 0;
rootNodeID = BuildOctreeRecursive( worldBounds, 0, _qef_solver, _volume, options, _octree_stats );
#if USE_QEF_SIMPLIFICATION
rootNodeID = Simplify( rootNodeID, worldBounds, 0, _qef_solver, _volume, options, _octree_stats );
#endif
_octree_stats.bytesAllocated = m_nodes.GetDataSize() + m_leaves.GetDataSize();
const UINT32 currentTimeMSec = mxGetTimeInMilliseconds();
_octree_stats.construction_time_milliseconds = currentTimeMSec - startTimeMSec;
}
// may return NULL
NodeID Octree_DC2::TryCreateLeaf(
const OctCubeF& _bounds,
const UINT32 _treeLevel,
QEF_Solver* _qef_solver,
const AVolume* _volume,
float _error_threshold,
OctStats &stats
)
{
Float3 corners[8];
OCT_GetCorners( _bounds, corners );
// Find corner signs and determine zero crossing edges.
int cornerSigns = 0;
for( int i = 0; i < 8; i++ )
{
const float distance = _volume->GetDistanceAt(corners[i]);
if( AVolume::IsSolid( distance ) ) {
cornerSigns |= (1 << i);
}
}
if( cornerSigns == 0xFF || cornerSigns == 0 )
{
// voxel is fully inside or outside the volume
stats.numEmptyLeaves++;
return NIL_NODE;
}
// Calculate the so-called 'Hermite data':
// intersection points together with their normals
// (of the contour with the edges of the cube).
QEF_Solver::Input solverInput;
solverInput.bounds.min_point = corners[0];
solverInput.bounds.max_point = corners[CHILD_MASK_X|CHILD_MASK_Y|CHILD_MASK_Z];
// Calculate intersection points on zero crossing edges.
const UINT8* edges = CUBE_GetEdgeIndices();
for( int iEdge = 0; iEdge < 12; iEdge++ )
{
int iPointA = edges[ iEdge*2 + 0 ];
int iPointB = edges[ iEdge*2 + 1 ];
int signA = (cornerSigns >> iPointA) & 1;
int signB = (cornerSigns >> iPointB) & 1;
if( signA == signB ) {
continue;
}
const Float3& pointA = corners[ iPointA ];
const Float3& pointB = corners[ iPointB ];
Float3 intersectionPoint;
Float3 intersectionNormal;
if( _volume->IntersectsLine( pointA, pointB, intersectionPoint, intersectionNormal ) )
{
solverInput.positions[ solverInput.numPoints ] = intersectionPoint;
solverInput.normals[ solverInput.numPoints ] = intersectionNormal;
solverInput.numPoints++;
}
}
mxASSERT2(solverInput.numPoints > 0, "No intersection points, but differing signs at corners - should happen!");
mxASSERT(solverInput.numPoints <= 8);
stats.maxActiveEdges = largest(stats.maxActiveEdges, solverInput.numPoints);
QEF_Solver::Output solverOutput;
_qef_solver->Solve( solverInput, solverOutput );
stats.max_QEF_error = largest( stats.max_QEF_error, solverOutput.error );
if( solverOutput.error < _error_threshold ) {
//DBGOUT("solverOutput.error: %f", solverOutput.error);
return NIL_NODE;
}
Float3 centerP, centerN;
centerP = solverOutput.position;
centerN = Float3_Normalized(_volume->SampleAt(centerP).normal);
const UINT16 leafIndex = m_leaves.Alloc();
Leaf2& leaf = m_leaves[ leafIndex ];
QuantizePosition( _bounds, centerP, leaf.xyz );
QuantizeNormal( centerN, leaf.N );
leaf.signs = cornerSigns;
#if USE_QEF_SIMPLIFICATION
mxASSERT(solverOutput.qef.numPoints > 0);
leaf.qefID = m_QEFs.Alloc();
QEF_t& qef = m_QEFs[ leaf.qefID ];
(svd::QefData&)qef = solverOutput.qef;
#else
leaf.qefID = (UINT16)~0;
#endif
stats.numLeafNodes++;
stats.maxTreeDepth = largest(stats.maxTreeDepth, _treeLevel);
return MAKE_LEAF_ID(leafIndex);
}
// may return NULL
NodeID Octree_DC2::TryCreateLeaf2(
const OctCubeF& _bounds,
const UINT32 _treeLevel,
QEF_Solver* _qef_solver,
const AVolume* _volume,
float _error_threshold,
OctStats &stats
)
{
Float3 corners[8];
OCT_GetCorners( _bounds, corners );
// Calculate the so-called 'Hermite data':
// intersection points together with their normals
// (of the contour with the edges of the cube).
QEF_Solver::Input solverInput;
solverInput.bounds.min_point = corners[0];
solverInput.bounds.max_point = corners[CHILD_MASK_X|CHILD_MASK_Y|CHILD_MASK_Z];
// Calculate intersection points on zero crossing edges.
const UINT8* edges = CUBE_GetEdgeIndices();
for( int iEdge = 0; iEdge < 12; iEdge++ )
{
int iPointA = edges[ iEdge*2 + 0 ];
int iPointB = edges[ iEdge*2 + 1 ];
const Float3& pointA = corners[ iPointA ];
const Float3& pointB = corners[ iPointB ];
Float3 intersectionPoint;
Float3 intersectionNormal;
if( _volume->IntersectsLine( pointA, pointB, intersectionPoint, intersectionNormal ) )
{
solverInput.positions[ solverInput.numPoints ] = intersectionPoint;
solverInput.normals[ solverInput.numPoints ] = intersectionNormal;
solverInput.numPoints++;
}
}
if( solverInput.numPoints == 0 )
{
// voxel is fully inside or outside the volume
stats.numEmptyLeaves++;
return NIL_NODE;
}
stats.maxActiveEdges = largest(stats.maxActiveEdges, solverInput.numPoints);
QEF_Solver::Output solverOutput;
_qef_solver->Solve( solverInput, solverOutput );
stats.max_QEF_error = largest( stats.max_QEF_error, solverOutput.error );
if( solverOutput.error < _error_threshold ) {
//DBGOUT("solverOutput.error: %f", solverOutput.error);
return NIL_NODE;
}
Float3 centerP, centerN;
centerP = solverOutput.position;
centerN = Float3_Normalized(_volume->SampleAt(centerP).normal);
const UINT16 leafIndex = m_leaves.Alloc();
Leaf2& leaf = m_leaves[ leafIndex ];
QuantizePosition( _bounds, centerP, leaf.xyz );
QuantizeNormal( centerN, leaf.N );
int cornerSigns = 0;
for( int i = 0; i < 8; i++ )
{
const float distance = _volume->GetDistanceAt(corners[i]);
if( AVolume::IsSolid( distance ) ) {
cornerSigns |= (1 << i);
}
}
leaf.signs = cornerSigns;
#if USE_QEF_SIMPLIFICATION
mxASSERT(solverOutput.qef.numPoints > 0);
leaf.qefID = m_QEFs.Alloc();
QEF_t& qef = m_QEFs[ leaf.qefID ];
(svd::QefData&)qef = solverOutput.qef;
#else
leaf.qefID = (UINT16)~0;
#endif
stats.numLeafNodes++;
stats.maxTreeDepth = largest(stats.maxTreeDepth, _treeLevel);
return MAKE_LEAF_ID(leafIndex);
}
NodeID Octree_DC2::BuildOctreeRecursive(
const OctCubeF& _bounds,
const UINT32 _treeLevel,
QEF_Solver* _qef_solver,
const AVolume* _volume,
const Options& options,
OctStats &stats
)
{
mxPROFILE_SCOPE("BuildOctreeRecursive");
const bool isLeaf = (_treeLevel >= options.maxDepth)
|| (_bounds.radius <= options.minSubdiv);
if( isLeaf )
{
const float fake_threshold = -1.0; // don't throw away leaves with error less than this
return TryCreateLeaf( _bounds, _treeLevel, _qef_solver, _volume, fake_threshold, stats );
}
else
{
//#if !USE_QEF_SIMPLIFICATION
// if( _treeLevel > 0 )// don't simplify octree down to a single node-point
// {
// NodeID leafID = TryCreateLeaf2( _bounds, _treeLevel, _qef_solver, _volume, options.build_threshold, stats );
// if( leafID != NIL_NODE )
// {
// return leafID;
// }
// }
//#endif
OctCubeF octants[8];
GetChildOctants(_bounds,octants);
const UINT32 nextLevel = _treeLevel + 1;
NodeID nodeID = m_nodes.Alloc();
for( int i = 0; i < 8; i++ )
{
NodeID childID = BuildOctreeRecursive( octants[i], nextLevel, _qef_solver, _volume, options, stats );
Node2& node = m_nodes[ nodeID ];
node.kids[i] = childID;
}
// collapse empty nodes
if( IsBadNode( m_nodes[ nodeID ] ) )
{
stats.numBadNodes++;
m_nodes.Free( nodeID );
nodeID = NIL_NODE;
}
else
{
stats.numInternalNodes++;
}
return nodeID;
}
}
#if USE_QEF_SIMPLIFICATION
NodeID Octree_DC2::Simplify(
const NodeID _nodeIndex,
const OctCubeF& _bounds,
const UINT32 _treeLevel,
QEF_Solver* _qef_solver,
const AVolume* _volume,
const Options& options,
OctStats &stats
)
{
NodeID result = _nodeIndex;
//// don't simplify octree down to a single node-point
//if( _treeLevel == 1 ) {
// return _nodeIndex;
//}
if( _nodeIndex != NIL_NODE && !IS_LEAF_ID( _nodeIndex ) )
{
OctCubeF octants[8];
GetChildOctants( _bounds, octants );
bool isCollapsible = true;
int signs[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
int midsign = -1;
Float3 corners[8];
OCT_GetCorners( _bounds, corners );
svd::QefSolver solver;
int edgeCount = 0;
Float3 averageNormal = Float3_Zero();
const int nextLevel = _treeLevel + 1;
for( int i = 0; i < 8; i++ )
{
Node2& node = m_nodes[_nodeIndex];
if( node.kids[i] != NIL_NODE )
{
NodeID simplified = Simplify(
node.kids[i],
octants[i],
nextLevel,
_qef_solver,
_volume,
options,
stats
);
mxASSERT( simplified != NIL_NODE );
if( node.kids[i] != simplified ) {
FreeNode( node.kids[i] );
node.kids[i] = simplified;
stats.nodes_collapsed++;
}
if( IS_LEAF_ID(simplified) )
{
const Leaf2& leaf = m_leaves[ GET_ID(simplified) ];
const QEF_t& qef = m_QEFs[ leaf.qefID ];
solver.add( qef );
//midsign = (leaf.signs >> (7 - i)) & 1;
//midsign = (leaf.signs >> i) & 1;
signs[i] = (leaf.signs >> i) & 1;
averageNormal += DequantizeNormal( leaf.N );
edgeCount++;
}
else
{
// at least one child is an internal node, can't collapse
isCollapsible = false;
}
}
}
midsign = _volume->GetDistanceAt(_bounds.XYZ()) >= 0.0f;
Node2& node = m_nodes[_nodeIndex];
if( isCollapsible )
{
const float QEF_ERROR = 1e-6f;
const int QEF_SWEEPS = 4;
svd::Vec3 qefPosition;
solver.solve(qefPosition, QEF_ERROR, QEF_SWEEPS, QEF_ERROR);
float error = solver.getError();
// collapse the node if the residual is less than the given tolerance
if( error <= options.qef_threshold )
{
Float3 position = Float3_Set(qefPosition.x,qefPosition.y,qefPosition.z);
AABB24 aabb;
aabb.min_point = corners[0];
aabb.max_point = corners[CHILD_MASK_X|CHILD_MASK_Y|CHILD_MASK_Z];
if( !AABB_ContainsPoint( aabb, position ) )
{
const svd::Vec3& mp = solver.getMassPoint();
position = Float3_Set(mp.x, mp.y, mp.z);
}
const UINT16 leafIndex = m_leaves.Alloc();
Leaf2& newLeaf = m_leaves[ leafIndex ];
result = MAKE_LEAF_ID( leafIndex );
for (int i = 0; i < 8; i++)
{
if (signs[i] == -1)
{
// Undetermined, use center sign instead
newLeaf.signs |= (midsign << i);
}
else
{
newLeaf.signs |= (signs[i] << i);
}
}
QuantizePosition( _bounds, position, newLeaf.xyz );
QuantizeNormal( Float3_Normalized(averageNormal), newLeaf.N );
newLeaf.qefID = m_QEFs.Alloc();
QEF_t& newQEF = m_QEFs[ newLeaf.qefID ];
(svd::QefData&)newQEF = solver.getData();
}
}
}
return result;
}
#endif
void Octree_DC2::FreeNode( NodeID nodeID )
{
const int nodeIndex = GET_ID( nodeID );
if( IS_LEAF_ID( nodeID ) )
{
Leaf2& leaf = m_leaves[ nodeIndex ];
if( leaf.qefID != (UINT16)~0 ) {
m_QEFs.Free( leaf.qefID );
}
m_leaves.Free( nodeIndex );
}
else
{
m_nodes.Free( nodeIndex );
}
}
static void CollectVertices(
const Octree_DC2& _tree,
const OctCubeF& _bounds,
const UINT32 _treeLevel,
const Options& _options,
const NodeID _parentID,
AMeshBuilder & _mesh,
MeshCtx & _meshCtx,
OctStats & _stats
)
{
mxPROFILE_SCOPE("CollectVertices");
const bool isLeaf = IS_LEAF_ID(_parentID);
const UINT32 nodeIndex = GET_ID(_parentID);
if(isLeaf)
{
const Leaf2& leaf = _tree.m_leaves[nodeIndex];
DrawVertex vertex;
DequantizePosition( _bounds, leaf.xyz, &vertex.xyz );
vertex.N.v = (int&)leaf.N;
const int vertexIndex = _mesh.AddVertex( vertex );
_meshCtx.vertexIDs[nodeIndex] = vertexIndex;
}
else//if(!isLeaf)
{
OctCubeF octants[8];
GetChildOctants(_bounds,octants);
const UINT32 nextLevel = _treeLevel + 1;
mxASSERT(nextLevel <= _options.maxDepth);
const Node2& node = _tree.m_nodes[nodeIndex];
for( int i = 0; i < 8; i++ )
{
if( node.kids[i] != NIL_NODE ) {
CollectVertices( _tree, octants[i], nextLevel, _options, node.kids[i], _mesh, _meshCtx, _stats );
}
}
}
}
ERet Octree_DC2::Triangulate(
AMeshBuilder & mesh,
const Options& options,
OctStats &stats
)
{
mxPROFILE_SCOPE("Triangulate");
mxDO(mesh.Begin());
const UINT32 startTimeMSec = mxGetTimeInMilliseconds();
OctCubeF worldBounds;
worldBounds.x = 0;
worldBounds.y = 0;
worldBounds.z = 0;
worldBounds.radius = options.radius;
const UINT32 zeroLevel = 0;
const NodeID rootNodeID = 0;
// allocate temporary storage
const UINT32 maxVertices = m_leaves.Num();
mxASSERT( maxVertices < MAX_UINT16 );
MeshCtx meshCtx;
ScopedStackAlloc tempAlloc( gCore.frameAlloc );
meshCtx.vertexIDs = tempAlloc.AllocMany< UINT16 >( maxVertices );
// create vertices
CollectVertices( *this, worldBounds, zeroLevel, options, rootNodeID, mesh, meshCtx, stats );
// create quadrilaterals
NodeCtx rootCtx = { 0, 0 };
ProcessNode( rootCtx, mesh, meshCtx );
int verts, tris;
mxDO(mesh.End( verts, tris ));
const UINT32 currentTimeMSec = mxGetTimeInMilliseconds();
const UINT32 elapsedTimeMSec = currentTimeMSec - startTimeMSec;
stats.numPolygons += tris;
stats.contouring_time_milliseconds = elapsedTimeMSec;
DBGOUT("Octree_DC2::Triangulate(): %d verts, %d tris in %u msec\n", verts, tris, elapsedTimeMSec);
return ALL_OK;
}
// recursively enumerates each edge of the octree and emits quads at minimal edges when needed
/*
.-----.-----.
/ 6 / 7 /| Z
.-----.-----.7| | /Y
/ 4 / 5 /|/| | /
.-----+-----|5|3. |/_____X
| 4 | 5 |/|/ (0,0,0)
|-----|-----|1/
| 0 | 1 |/
.-----+-----.
*/
void Octree_DC2::ProcessNode(
const NodeCtx& n,
AMeshBuilder& mesh, MeshCtx & ctx
)
{
mxPROFILE_SCOPE("ProcessNode");
if( n.id != NIL_NODE && !IS_LEAF_ID(n.id) )
{
//const Node2& node = m_nodes[ n.id ];
const NodeCtx c0 = CreateChildCtx( n, 0 );
const NodeCtx c1 = CreateChildCtx( n, 1 );
const NodeCtx c2 = CreateChildCtx( n, 2 );
const NodeCtx c3 = CreateChildCtx( n, 3 );
const NodeCtx c4 = CreateChildCtx( n, 4 );
const NodeCtx c5 = CreateChildCtx( n, 5 );
const NodeCtx c6 = CreateChildCtx( n, 6 );
const NodeCtx c7 = CreateChildCtx( n, 7 );
// Call ProcessNode() for each child (8 cell calls).
{
ProcessNode( c0, mesh, ctx );
ProcessNode( c1, mesh, ctx );
ProcessNode( c2, mesh, ctx );
ProcessNode( c3, mesh, ctx );
ProcessNode( c4, mesh, ctx );
ProcessNode( c5, mesh, ctx );
ProcessNode( c6, mesh, ctx );
ProcessNode( c7, mesh, ctx );
}
// Call ProcessFace() for each pair of children that share a face (12 face calls).
{
// call ProcessFace_X() for children sharing a face in the ZY plane ('vertical' plane with normal pointing left to right)
ProcessFaces_X( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 4 ProcessFace_X() calls
// call ProcessFace_Y() for children sharing a face in the XZ plane ('front' plane with normal pointing front to back)
ProcessFaces_Y( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 4 ProcessFace_Y() calls
// call ProcessFace_Z() for children sharing a face in the XY plane ('ground' plane with normal pointing upwards)
ProcessFaces_Z( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 4 ProcessFace_Z() calls
}
// Call ProcessEdge() for each of the 6 interior edges (6 edge calls).
{
// call ProcessEdge_X for children sharing an edge along the X-axis
ProcessEdges_X_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_X() calls
// call ProcessEdge_Y for children sharing an edge along the Y-axis
ProcessEdges_Y_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_Y() calls
// call ProcessEdge_Z for children sharing an edge along the Z-axis
ProcessEdges_Z_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_Z() calls
}
}
}
/*
Processes the two nodes sharing a (vertical) face along the X axis:
..Left node | Right node..
---.-----.-----.---
/ 7 / 6 /| Z
--.-----.-----.6|-- | /Y
/ 5 / 4 /|/| | /
---.-----+-----|4|2.--- |/_____X*
| 5 | 4 |/|/ (0,0,0)
|-----+-----|0/
| 1 | 0 |/
---.-----+-----.---
*/
void Octree_DC2::ProcessFace_X(
const NodeCtx& n1, //<= 'left' node
const NodeCtx& n2, //<= 'right' node
AMeshBuilder& mesh, MeshCtx & ctx
)
{
// When at least one of the two nodes sharing a face is subdivided,
// we get four sub-faces and four edges meeting at a single vertex
// at the center of the face.
const bool n1Leaf = IS_LEAF_ID(n1.id);
const bool n2Leaf = IS_LEAF_ID(n2.id);
// If all nodes are leaves, or one is empty, bail out.
if( n1.id != NIL_NODE && n2.id != NIL_NODE && (!n1Leaf || !n2Leaf) )
{
// left
const NodeCtx c0 = n1Leaf ? n1 : CreateChildCtx( n1, 1 );
const NodeCtx c2 = n1Leaf ? n1 : CreateChildCtx( n1, 3 );
const NodeCtx c6 = n1Leaf ? n1 : CreateChildCtx( n1, 7 );
const NodeCtx c4 = n1Leaf ? n1 : CreateChildCtx( n1, 5 );
// right
const NodeCtx c1 = n2Leaf ? n2 : CreateChildCtx( n2, 0 );
const NodeCtx c3 = n2Leaf ? n2 : CreateChildCtx( n2, 2 );
const NodeCtx c7 = n2Leaf ? n2 : CreateChildCtx( n2, 6 );
const NodeCtx c5 = n2Leaf ? n2 : CreateChildCtx( n2, 4 );
// call ProcessFace_YZ for children sharing a face in the ZY plane
ProcessFaces_X( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 4 ProcessFace_X() calls
// call ProcessEdge_Y for nodes sharing an edge along the Y-axis
ProcessEdges_Y_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_Y() calls
// call ProcessEdge_Z for children sharing an edge along the Z-axis
ProcessEdges_Z_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_Z() calls
}
}
/*
Processes the two nodes sharing a (vertical) face along the Y axis:
Back node
.-----.-----.
/ 4 / 5 /| Z
.-----.-----.5| | /Y*
/ 6 / 7 /|/| | /
.-----+-----|7|1. |/_____X
| 6 | 7 |/|/ (0,0,0)
|-----+-----|3/
| 2 | 3 |/
.-----+-----.
Front node
*/
void Octree_DC2::ProcessFace_Y(
const NodeCtx& n1, //<= 'front' node
const NodeCtx& n2, //<= 'back' node
AMeshBuilder& mesh, MeshCtx & ctx
)
{
const bool n1Leaf = IS_LEAF_ID(n1.id);
const bool n2Leaf = IS_LEAF_ID(n2.id);
// If all nodes are leaves, or one is empty, bail out.
if( n1.id != NIL_NODE && n2.id != NIL_NODE && (!n1Leaf || !n2Leaf) )
{
// front
const NodeCtx c0 = n1Leaf ? n1 : CreateChildCtx( n1, 2 );
const NodeCtx c4 = n1Leaf ? n1 : CreateChildCtx( n1, 6 );
const NodeCtx c5 = n1Leaf ? n1 : CreateChildCtx( n1, 7 );
const NodeCtx c1 = n1Leaf ? n1 : CreateChildCtx( n1, 3 );
// back
const NodeCtx c2 = n2Leaf ? n2 : CreateChildCtx( n2, 0 );
const NodeCtx c6 = n2Leaf ? n2 : CreateChildCtx( n2, 4 );
const NodeCtx c7 = n2Leaf ? n2 : CreateChildCtx( n2, 5 );
const NodeCtx c3 = n2Leaf ? n2 : CreateChildCtx( n2, 1 );
// call ProcessFace_XZ for nodes sharing sub-faces
ProcessFaces_Y( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 4 ProcessFace_Y() calls
// call ProcessEdge_X for nodes sharing an edge along the X-axis
ProcessEdges_X_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_X() calls
// call ProcessEdge_Z for children sharing an edge along the Z-axis
ProcessEdges_Z_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_Z() calls
}
}
/*
Processes the two nodes sharing a (horizontal) face along the Z axis:
Upper node
.-----.-----.
/ 2 / 3 /| Z*
.-----.-----.3| | /Y
/ 0 / 1 /|/| | /
.-----+-----|1|7. |/_____X
| 0 | 1 |/|/ (0,0,0)
|-----+-----|5/
| 4 | 5 |/
.-----+-----.
Lower node
*/
void Octree_DC2::ProcessFace_Z(
const NodeCtx& n1, //<= lower node
const NodeCtx& n2, //<= upper node
AMeshBuilder& mesh, MeshCtx & ctx
)
{
const bool n1Leaf = IS_LEAF_ID(n1.id);
const bool n2Leaf = IS_LEAF_ID(n2.id);
// If all nodes are leaves, or one is empty, bail out.
if( n1.id != NIL_NODE && n2.id != NIL_NODE && (!n1Leaf || !n2Leaf) )
{
// bottom part
const NodeCtx c0 = n1Leaf ? n1 : CreateChildCtx( n1, 4 );
const NodeCtx c1 = n1Leaf ? n1 : CreateChildCtx( n1, 5 );
const NodeCtx c2 = n1Leaf ? n1 : CreateChildCtx( n1, 6 );
const NodeCtx c3 = n1Leaf ? n1 : CreateChildCtx( n1, 7 );
// upper part
const NodeCtx c4 = n2Leaf ? n2 : CreateChildCtx( n2, 0 );
const NodeCtx c5 = n2Leaf ? n2 : CreateChildCtx( n2, 1 );
const NodeCtx c6 = n2Leaf ? n2 : CreateChildCtx( n2, 2 );
const NodeCtx c7 = n2Leaf ? n2 : CreateChildCtx( n2, 3 );
// call ProcessFace_Z() for nodes sharing sub-faces on the XY plane
ProcessFaces_Z( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx );
// call ProcessEdge_X for nodes sharing an edge along the X-axis
ProcessEdges_X_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_X() calls
// call ProcessEdge_Y for nodes sharing an edge along the Y-axis
ProcessEdges_Y_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_Y() calls
}
}
/*
Processes the 4 subtrees sharing the edge along the X axis:
.-----. Z
/ 2 /| | Y
/-----/2| | /
/ 3 /|/| |/
.-----|3|1| O-------X*
| 3 |/|/
|-----|0/
| 0 |/
.-----.
*/
void Octree_DC2::ProcessEdge_X(
// 'right-hand rule' order, around X axis, starting from the lowest octant index
const NodeCtx& n0,const NodeCtx& n1,const NodeCtx& n2,const NodeCtx& n3,
AMeshBuilder& mesh, MeshCtx & ctx
)
{
// If one of the nodes is null, bail out.
if( n0.id != NIL_NODE && n1.id != NIL_NODE && n2.id != NIL_NODE && n3.id != NIL_NODE )
{
const bool n0Leaf = IS_LEAF_ID(n0.id);
const bool n1Leaf = IS_LEAF_ID(n1.id);
const bool n2Leaf = IS_LEAF_ID(n2.id);
const bool n3Leaf = IS_LEAF_ID(n3.id);
// If all cubes are leaves, stop recursion and emit a quad.
if( n0Leaf && n1Leaf && n2Leaf && n3Leaf )
{
// All nodes are surface leaves - determine the minimal cell/edge (containing no smaller edge).
const Leaf2& leaf0 = m_leaves[ GET_ID(n0.id) ];
const Leaf2& leaf1 = m_leaves[ GET_ID(n1.id) ];
const Leaf2& leaf2 = m_leaves[ GET_ID(n2.id) ];
const Leaf2& leaf3 = m_leaves[ GET_ID(n3.id) ];
// Determine the cell with the greatest subdivision
// and test the corner signs of its edge to determine
// if a polygon should be generated.
const NodeCtx* mc;
mc = (n0.depth >= n1.depth) ? &n0 : &n1;
mc = (mc->depth >= n2.depth) ? mc : &n2;
mc = (mc->depth >= n3.depth) ? mc : &n3;
// left -> right
int sign1, sign2; // 0 = outside, 1 = inside surface
if(mc == &n0) {
sign1 = (leaf0.signs >> 6) & 1;
sign2 = (leaf0.signs >> 7) & 1;
} else if(mc == &n1) {
sign1 = (leaf1.signs >> 4) & 1;
sign2 = (leaf1.signs >> 5) & 1;
} else if(mc == &n2) {
sign1 = (leaf2.signs >> 0) & 1;
sign2 = (leaf2.signs >> 1) & 1;
} else {
sign1 = (leaf3.signs >> 2) & 1;
sign2 = (leaf3.signs >> 3) & 1;
}
if(sign1 > sign2) {
/* The first corner is inside the surface, the second is outside */
EmitQuad( n0, n1, n2, n3, mesh, ctx );
} else if( sign1 < sign2 ) {
/* The second corner is inside the surface, the first is outside */
EmitQuad( n3, n2, n1, n0, mesh, ctx );
}
}
else//if( !n0Leaf || !n1Leaf || !n2Leaf || !n3Leaf )
{
const NodeCtx c0 = n0Leaf ? n0 : CreateChildCtx( n0, 6 );
const NodeCtx c1 = n0Leaf ? n0 : CreateChildCtx( n0, 7 );
const NodeCtx c2 = n1Leaf ? n1 : CreateChildCtx( n1, 4 );
const NodeCtx c3 = n1Leaf ? n1 : CreateChildCtx( n1, 5 );
const NodeCtx c4 = n3Leaf ? n3 : CreateChildCtx( n3, 2 );
const NodeCtx c5 = n3Leaf ? n3 : CreateChildCtx( n3, 3 );
const NodeCtx c6 = n2Leaf ? n2 : CreateChildCtx( n2, 0 );
const NodeCtx c7 = n2Leaf ? n2 : CreateChildCtx( n2, 1 );
// ...call ProcessEdge_X for nodes sharing smaller edges:
ProcessEdges_X_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_X() calls
}
}
}
/*
Processes the 4 subtrees sharing the edge along the Y axis:
.-----.-----. Z
/ 1 / 2 /| | Y*
.-----+-----|2| | /
| 1 | 2 |/| |/
|-----X-----|3/ O-------X
| 0 | 3 |/
.-----.-----.
*/
void Octree_DC2::ProcessEdge_Y(
const NodeCtx& n0,const NodeCtx& n1,const NodeCtx& n2,const NodeCtx& n3,
AMeshBuilder& mesh, MeshCtx & ctx
)
{
// If one of the nodes is null, bail out.
if( n0.id != NIL_NODE && n1.id != NIL_NODE && n2.id != NIL_NODE && n3.id != NIL_NODE )
{
const bool n0Leaf = IS_LEAF_ID(n0.id);
const bool n1Leaf = IS_LEAF_ID(n1.id);
const bool n2Leaf = IS_LEAF_ID(n2.id);
const bool n3Leaf = IS_LEAF_ID(n3.id);
// If all cubes are leaves, stop recursion and emit a quad.
if( n0Leaf && n1Leaf && n2Leaf && n3Leaf )
{
// All nodes are surface leaves - determine the minimal cell/edge (containing no smaller edge).
const Leaf2& leaf0 = m_leaves[ GET_ID(n0.id) ];
const Leaf2& leaf1 = m_leaves[ GET_ID(n1.id) ];
const Leaf2& leaf2 = m_leaves[ GET_ID(n2.id) ];
const Leaf2& leaf3 = m_leaves[ GET_ID(n3.id) ];
const NodeCtx* mc;
mc = (n0.depth >= n1.depth) ? &n0 : &n1;
mc = (mc->depth >= n2.depth) ? mc : &n2;
mc = (mc->depth >= n3.depth) ? mc : &n3;
// front -> back
int sign1, sign2; // 0 = outside, 1 = inside surface
if(mc == &n0) {
sign1 = (leaf0.signs >> 5) & 1;
sign2 = (leaf0.signs >> 7) & 1;
} else if(mc == &n1) {
sign1 = (leaf1.signs >> 1) & 1;
sign2 = (leaf1.signs >> 3) & 1;
} else if(mc == &n2) {
sign1 = (leaf2.signs >> 0) & 1;
sign2 = (leaf2.signs >> 2) & 1;
} else {
sign1 = (leaf3.signs >> 4) & 1;
sign2 = (leaf3.signs >> 6) & 1;
}
if( sign1 > sign2 ) {
// The first corner is inside the surface, the second is outside
EmitQuad( n0, n1, n2, n3, mesh, ctx );
} else if( sign1 < sign2 ) {
// The second corner is inside the surface, the first is outside
EmitQuad( n3, n2, n1, n0, mesh, ctx );
}
}
else
{
const NodeCtx c0 = n0Leaf ? n0 : CreateChildCtx( n0, 5 );
const NodeCtx c1 = n3Leaf ? n3 : CreateChildCtx( n3, 4 );
const NodeCtx c2 = n0Leaf ? n0 : CreateChildCtx( n0, 7 );
const NodeCtx c3 = n3Leaf ? n3 : CreateChildCtx( n3, 6 );
const NodeCtx c4 = n1Leaf ? n1 : CreateChildCtx( n1, 1 );
const NodeCtx c5 = n2Leaf ? n2 : CreateChildCtx( n2, 0 );
const NodeCtx c6 = n1Leaf ? n1 : CreateChildCtx( n1, 3 );
const NodeCtx c7 = n2Leaf ? n2 : CreateChildCtx( n2, 2 );
// call ProcessEdge_Y for children sharing an edge along the Y-axis
ProcessEdges_Y_Axis( c0, c1, c2, c3, c4, c5, c6, c7, mesh, ctx ); // 2 ProcessEdge_Y() calls
}
}
}
/*
Processes the 4 subtrees sharing the edge along the Z axis:
Z*
.-----.-----. | Y
/ 3 / 2 /| | /
.-----.-----.2| |/
/ 0 / 1 /|/ O-------X
.-----+-----|1/
| 0 | 1 |/
.-----.-----.
*/
void Octree_DC2::ProcessEdge_Z(
const NodeCtx& n0,const NodeCtx& n1,const NodeCtx& n2,const NodeCtx& n3,
AMeshBuilder& mesh, MeshCtx & ctx
)
{
// If one of the nodes is null, bail out.
if( n0.id != NIL_NODE && n1.id != NIL_NODE && n2.id != NIL_NODE && n3.id != NIL_NODE )
{
const bool n0Leaf = IS_LEAF_ID(n0.id);
const bool n1Leaf = IS_LEAF_ID(n1.id);
const bool n2Leaf = IS_LEAF_ID(n2.id);
const bool n3Leaf = IS_LEAF_ID(n3.id);
// If all cubes are leaves, stop recursion and emit a quad.
if( n0Leaf && n1Leaf && n2Leaf && n3Leaf )
{
// All nodes are surface leaves - determine the minimal cell/edge (containing no smaller edge).
const Leaf2& leaf0 = m_leaves[ GET_ID(n0.id) ];
const Leaf2& leaf1 = m_leaves[ GET_ID(n1.id) ];
const Leaf2& leaf2 = m_leaves[ GET_ID(n2.id) ];
const Leaf2& leaf3 = m_leaves[ GET_ID(n3.id) ];
const NodeCtx* mc;
mc = (n0.depth >= n1.depth) ? &n0 : &n1;
mc = (mc->depth >= n2.depth) ? mc : &n2;
mc = (mc->depth >= n3.depth) ? mc : &n3;
// bottom -> top
int sign1, sign2; // 0 = outside, 1 = inside surface
if(mc == &n0) {
sign1 = (leaf0.signs >> 3) & 1;
sign2 = (leaf0.signs >> 7) & 1;
} else if(mc == &n1) {
sign1 = (leaf1.signs >> 2) & 1;
sign2 = (leaf1.signs >> 6) & 1;
} else if(mc == &n2) {
sign1 = (leaf2.signs >> 0) & 1;
sign2 = (leaf2.signs >> 4) & 1;
} else {
sign1 = (leaf3.signs >> 1) & 1;
sign2 = (leaf3.signs >> 5) & 1;
}
if( sign1 > sign2 ) {
// The first corner is inside the surface, the second is outside
EmitQuad( n0, n1, n2, n3, mesh, ctx );
} else if( sign1 < sign2 ) {
// The second corner is inside the surface, the first is outside
EmitQuad( n3, n2, n1, n0, mesh, ctx );
}
}
else
{