forked from gradientspace/geometry3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LaplacianMeshSmoother.cs
444 lines (357 loc) · 15.4 KB
/
LaplacianMeshSmoother.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace g3
{
public class LaplacianMeshSmoother
{
public DMesh3 Mesh;
// info that is fixed based on mesh
PackedSparseMatrix PackedM;
int N;
int[] ToMeshV, ToIndex;
double[] Px, Py, Pz;
int[] nbr_counts;
double[] MLx, MLy, MLz;
// constraints
public struct SoftConstraintV
{
public Vector3d Position;
public double Weight;
public bool PostFix;
}
Dictionary<int, SoftConstraintV> SoftConstraints = new Dictionary<int, SoftConstraintV>();
bool HavePostFixedConstraints = false;
// needs to be updated after constraints
bool need_solve_update;
DiagonalMatrix WeightsM;
double[] Cx, Cy, Cz;
double[] Bx, By, Bz;
DiagonalMatrix Preconditioner;
// Appendix C from http://sites.fas.harvard.edu/~cs277/papers/deformation_survey.pdf
public bool UseSoftConstraintNormalEquations = true;
// result
double[] Sx, Sy, Sz;
public LaplacianMeshSmoother(DMesh3 mesh)
{
Mesh = mesh;
Util.gDevAssert(mesh.IsCompact);
}
public void SetConstraint(int vID, Vector3d targetPos, double weight, bool bForceToFixedPos = false)
{
SoftConstraints[vID] = new SoftConstraintV() { Position = targetPos, Weight = weight, PostFix = bForceToFixedPos };
HavePostFixedConstraints = HavePostFixedConstraints || bForceToFixedPos;
need_solve_update = true;
}
public bool IsConstrained(int vID) {
return SoftConstraints.ContainsKey(vID);
}
public void ClearConstraints()
{
SoftConstraints.Clear();
HavePostFixedConstraints = false;
need_solve_update = true;
}
public void Initialize()
{
ToMeshV = new int[Mesh.MaxVertexID];
ToIndex = new int[Mesh.MaxVertexID];
N = 0;
foreach (int vid in Mesh.VertexIndices()) {
ToMeshV[N] = vid;
ToIndex[vid] = N;
N++;
}
Px = new double[N];
Py = new double[N];
Pz = new double[N];
nbr_counts = new int[N];
SymmetricSparseMatrix M = new SymmetricSparseMatrix();
for (int i = 0; i < N; ++i) {
int vid = ToMeshV[i];
Vector3d v = Mesh.GetVertex(vid);
Px[i] = v.x; Py[i] = v.y; Pz[i] = v.z;
nbr_counts[i] = Mesh.GetVtxEdgeCount(vid);
}
// construct laplacian matrix
for (int i = 0; i < N; ++i) {
int vid = ToMeshV[i];
int n = nbr_counts[i];
double sum_w = 0;
foreach (int nbrvid in Mesh.VtxVerticesItr(vid)) {
int j = ToIndex[nbrvid];
int n2 = nbr_counts[j];
// weight options
//double w = -1;
double w = -1.0 / Math.Sqrt(n + n2);
//double w = -1.0 / n;
M.Set(i, j, w);
sum_w += w;
}
sum_w = -sum_w;
M.Set(vid, vid, sum_w);
}
// transpose(L) * L, but matrix is symmetric...
if (UseSoftConstraintNormalEquations) {
//M = M.Multiply(M);
// only works if M is symmetric!!
PackedM = M.SquarePackedParallel();
} else {
PackedM = new PackedSparseMatrix(M);
}
// compute laplacian vectors of initial mesh positions
MLx = new double[N];
MLy = new double[N];
MLz = new double[N];
PackedM.Multiply(Px, MLx);
PackedM.Multiply(Py, MLy);
PackedM.Multiply(Pz, MLz);
// zero out...this is the smoothing bit!
for (int i = 0; i < Px.Length; ++i) {
MLx[i] = 0;
MLy[i] = 0;
MLz[i] = 0;
}
// allocate memory for internal buffers
Preconditioner = new DiagonalMatrix(N);
WeightsM = new DiagonalMatrix(N);
Cx = new double[N]; Cy = new double[N]; Cz = new double[N];
Bx = new double[N]; By = new double[N]; Bz = new double[N];
Sx = new double[N]; Sy = new double[N]; Sz = new double[N];
need_solve_update = true;
UpdateForSolve();
}
void UpdateForSolve()
{
if (need_solve_update == false)
return;
// construct constraints matrix and RHS
WeightsM.Clear();
Array.Clear(Cx, 0, N);
Array.Clear(Cy, 0, N);
Array.Clear(Cz, 0, N);
foreach ( var constraint in SoftConstraints ) {
int vid = constraint.Key;
int i = ToIndex[vid];
double w = constraint.Value.Weight;
if ( UseSoftConstraintNormalEquations )
w = w * w;
WeightsM.Set(i, i, w);
Vector3d pos = constraint.Value.Position;
Cx[i] = w * pos.x;
Cy[i] = w * pos.y;
Cz[i] = w * pos.z;
}
// add RHS vectors
for (int i = 0; i < N; ++i) {
Bx[i] = MLx[i] + Cx[i];
By[i] = MLy[i] + Cy[i];
Bz[i] = MLz[i] + Cz[i];
}
// update basic Jacobi preconditioner
for ( int i = 0; i < N; i++ ) {
double diag_value = PackedM[i, i] + WeightsM[i, i];
Preconditioner.Set(i, i, 1.0 / diag_value);
}
need_solve_update = false;
}
// Result must be as large as Mesh.MaxVertexID
public bool SolveMultipleCG(Vector3d[] Result)
{
if (WeightsM == null)
Initialize(); // force initialize...
UpdateForSolve();
// use initial positions as initial solution.
Array.Copy(Px, Sx, N);
Array.Copy(Py, Sy, N);
Array.Copy(Pz, Sz, N);
Action<double[], double[]> CombinedMultiply = (X, B) => {
//PackedM.Multiply(X, B);
PackedM.Multiply_Parallel(X, B);
for (int i = 0; i < N; ++i)
B[i] += WeightsM.D[i] * X[i];
};
SparseSymmetricCG SolverX = new SparseSymmetricCG() { B = Bx, X = Sx,
MultiplyF = CombinedMultiply, PreconditionMultiplyF = Preconditioner.Multiply,
UseXAsInitialGuess = true };
SparseSymmetricCG SolverY = new SparseSymmetricCG() { B = By, X = Sy,
MultiplyF = CombinedMultiply, PreconditionMultiplyF = Preconditioner.Multiply,
UseXAsInitialGuess = true };
SparseSymmetricCG SolverZ = new SparseSymmetricCG() { B = Bz, X = Sz,
MultiplyF = CombinedMultiply, PreconditionMultiplyF = Preconditioner.Multiply,
UseXAsInitialGuess = true };
SparseSymmetricCG[] solvers = new SparseSymmetricCG[3] { SolverX, SolverY, SolverZ };
bool[] ok = new bool[3];
int[] indices = new int[3] { 0, 1, 2 };
// preconditioned solve is marginally faster
Action<int> SolveF = (i) => { ok[i] = solvers[i].SolvePreconditioned(); };
//Action<int> SolveF = (i) => { ok[i] = solvers[i].Solve(); };
gParallel.ForEach(indices, SolveF);
if (ok[0] == false || ok[1] == false || ok[2] == false)
return false;
for ( int i = 0; i < N; ++i ) {
int vid = ToMeshV[i];
Result[vid] = new Vector3d(Sx[i], Sy[i], Sz[i]);
}
// apply post-fixed constraints
if (HavePostFixedConstraints) {
foreach (var constraint in SoftConstraints) {
if (constraint.Value.PostFix) {
int vid = constraint.Key;
Result[vid] = constraint.Value.Position;
}
}
}
return true;
}
// Result must be as large as Mesh.MaxVertexID
public bool SolveMultipleRHS(Vector3d[] Result)
{
if (WeightsM == null)
Initialize(); // force initialize...
UpdateForSolve();
// use initial positions as initial solution.
double[][] B = BufferUtil.InitNxM(3, N, new double[][] { Bx, By, Bz } );
double[][] X = BufferUtil.InitNxM(3, N, new double[][] { Px, Py, Pz } );
Action<double[][], double[][]> CombinedMultiply = (Xt, Bt) => {
PackedM.Multiply_Parallel_3(Xt, Bt);
gParallel.ForEach(Interval1i.Range(3), (j) => {
BufferUtil.MultiplyAdd(Bt[j], WeightsM.D, Xt[j]);
});
};
Action<double[][], double[][]> CombinedPreconditionerMultiply = (Xt, Bt) => {
gParallel.ForEach(Interval1i.Range(3), (j) => {
Preconditioner.Multiply(Xt[j], Bt[j]);
});
};
SparseSymmetricCGMultipleRHS Solver = new SparseSymmetricCGMultipleRHS() {
B = B, X = X,
MultiplyF = CombinedMultiply, PreconditionMultiplyF = CombinedPreconditionerMultiply,
UseXAsInitialGuess = true
};
// preconditioned solve is marginally faster
//bool ok = Solver.Solve();
bool ok = Solver.SolvePreconditioned();
if (ok == false)
return false;
for (int i = 0; i < N; ++i) {
int vid = ToMeshV[i];
Result[vid] = new Vector3d(X[0][i], X[1][i], X[2][i]);
}
// apply post-fixed constraints
if (HavePostFixedConstraints) {
foreach (var constraint in SoftConstraints) {
if (constraint.Value.PostFix) {
int vid = constraint.Key;
Result[vid] = constraint.Value.Position;
}
}
}
return true;
}
public bool Solve(Vector3d[] Result)
{
// for small problems, faster to use separate CGs?
if ( Mesh.VertexCount < 10000 )
return SolveMultipleCG(Result);
else
return SolveMultipleRHS(Result);
}
public bool SolveAndUpdateMesh()
{
int N = Mesh.MaxVertexID;
Vector3d[] Result = new Vector3d[N];
if ( Solve(Result) == false )
return false;
for (int i = 0; i < N; ++i ) {
if ( Mesh.IsVertex(i) ) {
Mesh.SetVertex(i, Result[i]);
}
}
return true;
}
/// <summary>
/// Apply LaplacianMeshSmoother to subset of mesh triangles.
/// border of subset always has soft constraint with borderWeight,
/// but is then snapped back to original vtx pos after solve.
/// nConstrainLoops inner loops are also soft-constrained, with weight falloff via square roots (defines continuity)
/// interiorWeight is soft constraint added to all vertices
/// </summary>
public static void RegionSmooth(DMesh3 mesh, IEnumerable<int> triangles,
int nConstrainLoops,
int nIncludeExteriorRings,
bool bPreserveExteriorRings,
double borderWeight = 10.0, double interiorWeight = 0.0)
{
HashSet<int> fixedVerts = new HashSet<int>();
if ( nIncludeExteriorRings > 0 ) {
MeshFaceSelection expandTris = new MeshFaceSelection(mesh);
expandTris.Select(triangles);
if (bPreserveExteriorRings) {
MeshEdgeSelection bdryEdges = new MeshEdgeSelection(mesh);
bdryEdges.SelectBoundaryTriEdges(expandTris);
expandTris.ExpandToOneRingNeighbours(nIncludeExteriorRings);
MeshVertexSelection startVerts = new MeshVertexSelection(mesh);
startVerts.SelectTriangleVertices(triangles);
startVerts.DeselectEdges(bdryEdges);
MeshVertexSelection expandVerts = new MeshVertexSelection(mesh, expandTris);
foreach (int vid in expandVerts) {
if (startVerts.IsSelected(vid) == false)
fixedVerts.Add(vid);
}
} else {
expandTris.ExpandToOneRingNeighbours(nIncludeExteriorRings);
}
triangles = expandTris;
}
RegionOperator region = new RegionOperator(mesh, triangles);
DSubmesh3 submesh = region.Region;
DMesh3 smoothMesh = submesh.SubMesh;
LaplacianMeshSmoother smoother = new LaplacianMeshSmoother(smoothMesh);
// map fixed verts to submesh
HashSet<int> subFixedVerts = new HashSet<int>();
foreach (int base_vid in fixedVerts)
subFixedVerts.Add(submesh.MapVertexToSubmesh(base_vid));
// constrain borders
double w = borderWeight;
HashSet<int> constrained = (submesh.BaseBorderV.Count > 0) ? new HashSet<int>() : null;
foreach (int base_vid in submesh.BaseBorderV) {
int sub_vid = submesh.BaseToSubV[base_vid];
smoother.SetConstraint(sub_vid, smoothMesh.GetVertex(sub_vid), w, true);
if (constrained != null)
constrained.Add(sub_vid);
}
if (constrained.Count > 0) {
w = Math.Sqrt(w);
for (int k = 0; k < nConstrainLoops; ++k) {
HashSet<int> next_layer = new HashSet<int>();
foreach (int sub_vid in constrained) {
foreach (int nbr_vid in smoothMesh.VtxVerticesItr(sub_vid)) {
if (constrained.Contains(nbr_vid) == false) {
if ( smoother.IsConstrained(nbr_vid) == false )
smoother.SetConstraint(nbr_vid, smoothMesh.GetVertex(nbr_vid), w, subFixedVerts.Contains(nbr_vid));
next_layer.Add(nbr_vid);
}
}
}
constrained.UnionWith(next_layer);
w = Math.Sqrt(w);
}
}
// soft constraint on all interior vertices, if requested
if (interiorWeight > 0) {
foreach (int vid in smoothMesh.VertexIndices()) {
if ( smoother.IsConstrained(vid) == false )
smoother.SetConstraint(vid, smoothMesh.GetVertex(vid), interiorWeight, subFixedVerts.Contains(vid));
}
} else if ( subFixedVerts.Count > 0 ) {
foreach (int vid in subFixedVerts) {
if (smoother.IsConstrained(vid) == false)
smoother.SetConstraint(vid, smoothMesh.GetVertex(vid), 0, true);
}
}
smoother.SolveAndUpdateMesh();
region.BackPropropagateVertices(true);
}
}
}