-
Notifications
You must be signed in to change notification settings - Fork 0
/
contouring.cpp
159 lines (135 loc) · 4.29 KB
/
contouring.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
#include "stdafx.h"
#pragma hdrstop
#include "QEF.h"
#include "contouring.h"
namespace Contouring
{
QEF_Solver::Input::Input()
{
AABB24_Clear(&bounds);
numPoints = 0;
threshold = 1e-6f;
maxIterations = ~0;
}
mxDEFINE_CLASS(Options);
mxBEGIN_REFLECTION(Options)
mxMEMBER_FIELD( radius ),
mxMEMBER_FIELD( maxDepth ),
mxMEMBER_FIELD( minSubdiv ),
mxMEMBER_FIELD( threshold ),
mxMEMBER_FIELD( qef_threshold ),
mxMEMBER_FIELD( build_threshold ),
mxEND_REFLECTION;
Options::Options()
{
this->SetDefauls();
}
void Options::SetDefauls()
{
radius = 1.0f;
maxDepth = 16;
minSubdiv = 1.0f;
threshold = 1.0f;
qef_threshold = -1.0f; // don't simplify at all
build_threshold = -1.0f;// build to the very last level
}
OctStats::OctStats()
{
mxZERO_OUT( *this );
}
void OctStats::Print( UINT32 elapsedTimeMSec )
{
DBGOUT( "\n=== Octree statistics ========" );
DBGOUT( "Polygons: %u", numPolygons );
DBGOUT( "Bad Nodes: %u", numBadNodes );
DBGOUT( "Leaf Nodes: %u", numLeafNodes );
DBGOUT( "Empty Leaves: %u", numEmptyLeaves );
DBGOUT( "Internal Nodes: %u", numInternalNodes );
DBGOUT( "Max. Tree Depth: %u", maxTreeDepth );
DBGOUT( "Max. Intersections: %u", maxActiveEdges );
DBGOUT( "Nodes Collapsed: %u", nodes_collapsed );
DBGOUT( "Max. QEF error: %f", max_QEF_error );
DBGOUT( "Construction Time: %u msec", construction_time_milliseconds );
DBGOUT( "Contouring Time: %u msec", contouring_time_milliseconds );
DBGOUT( "Total Memory Used: %u bytes", bytesAllocated );
//DBGOUT( "Memory used: %u bytes (leaves: %u, nodes %u)", bytesAllocated );
DBGOUT( "Time elapsed: %u msec", elapsedTimeMSec );
DBGOUT( "==== End ====================" );
}
// Basically, you start the vertex in the centre of the cell.
// Then you average all the vectors taken from the vertex to each plane
// and move the vertex along that resultant, and repeat this step a fixed number of times.
// http://gamedev.stackexchange.com/a/83757
//
void QEF_Solver_ParticleBased::Solve( const Input& input, Output &output )
{
// Center the particle on the masspoint.
Float3 masspoint = Float3_Zero();
for( int i = 0; i < input.numPoints; i++ )
{
masspoint += input.positions[ i ];
}
masspoint /= input.numPoints;
Float3 particlePosition = masspoint;
// find the force that starts moving the particle from the masspoint towards the iso-surface
Float3 force;
Float4 planes[MAX_POINTS];
for( int i = 0; i < input.numPoints; i++ )
{
const Float3& planePoint = input.positions[ i ];
const Float3& planeNormal = input.normals[ i ];
planes[ i ] = Plane_FromPointNormal( planePoint, planeNormal );
}
//const float FORCE_TRESHOLD = 0.00001f;
const float forceRatio = 0.75f;
int iteration = 0;
while( iteration++ < MAX_ITERATIONS )
{
force = Float3_Zero();
// For each intersection point:
for (int i = 0; i < input.numPoints; i++)
{
const Float3& point = input.positions[ i ];
force += Plane_GetNormal( planes[i] ) * Plane_PointDistance( planes[i], point );
}
// Average the force over all the intersection points, and multiply
// with a ratio and some damping to avoid instabilities.
float damping = 1.0f - ((float) iteration) / MAX_ITERATIONS;
force *= forceRatio * damping / input.numPoints;
// Apply the force.
particlePosition += force;
// If the force was almost null, break.
if( Float3_LengthSquared(force) < squaref(input.threshold) )
{
break;
}
}
output.position = particlePosition;
output.error = Float3_Length(force);
}
void QEF_Solver_SVD::Solve( const Input& input, Output &output )
{
svd::QefSolver qef;
for( int i = 0; i < input.numPoints; i++ )
{
const Float3& planePoint = input.positions[ i ];
const Float3& planeNormal = input.normals[ i ];
qef.add(
planePoint.x, planePoint.y, planePoint.z,
planeNormal.x, planeNormal.y, planeNormal.z
);
}
const int QEF_SWEEPS = 4;
svd::Vec3 qefPosition;
const float error = qef.solve( qefPosition, input.threshold, QEF_SWEEPS, input.threshold );
output.position = Float3_Set(qefPosition.x, qefPosition.y, qefPosition.z);
output.error = error;
output.qef = qef.getData();
//NOTE: this is important!
if( !AABB_ContainsPoint( input.bounds, output.position ) )
{
const svd::Vec3& mp = qef.getMassPoint();
output.position = Float3_Set(mp.x, mp.y, mp.z);
}
}
}//namespace Contouring