-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMemorylessDecimator.hpp
434 lines (382 loc) · 13.8 KB
/
MemorylessDecimator.hpp
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
#pragma once
#include "Matrix.hpp"
#include "Optional.hpp"
#include "TriangleMesh.hpp"
namespace Pvl {
static constexpr double ALPHA = 1. * M_PI / 180.; // 1 degree
static double SQR_COS_ALPHA = sqr(std::cos(ALPHA));
static double SQR_SIN_ALPHA = sqr(std::sin(ALPHA));
static constexpr double MAX_PHI = 90. * M_PI / 180.;
static double MIN_COS_PHI = std::cos(MAX_PHI);
template <typename Float>
class LindstromTurkConstraints {
using Vec = Vector<Float, 3>;
using Mat = Matrix<Float, 3, 3>;
Mat A_;
Vec b_;
int n_ = 0;
public:
bool addConstraint(const Vec& p, const Float b) {
if (full()) {
return false;
}
if (isCompatible(p)) {
const Float distSqr = dotProd(p, p);
const Float dist = sqrt(distSqr);
const Vec pn = p / dist;
const Float bn = b / dist;
A_.row(n_) = pn;
b_[n_] = bn;
n_++;
return true;
} else {
// std::cout << "Constraint not compatible" << std::endl;
return false;
}
}
/// Used for problems formulated as quadratic minimization,
/// i.e. finds point where gradient Hv + c = 0.
int addQuadraticConstraint(const Mat& H, const Vec& c) {
int added = 0;
switch (n_) {
case 0:
added += int(addConstraint(H.row(0), -c[0]));
added += int(addConstraint(H.row(1), -c[1]));
added += int(addConstraint(H.row(2), -c[2]));
break;
case 1: {
const Vec r0 = A_.row(0);
std::array<Float, 3> abs_r0 = {
std::abs(r0[0]), std::abs(r0[1]), std::abs(r0[2])
};
Vec q0;
const int maxIdx =
int(std::max_element(abs_r0.begin(), abs_r0.end()) - abs_r0.begin());
switch (maxIdx) {
case 0:
q0 = { -r0[2] / r0[0], 0., 1. };
break;
case 1:
q0 = { 0., -r0[2] / r0[1], 1. };
break;
case 2:
q0 = { 1., 0., -r0[0] / r0[2] };
break;
}
const Vec q1 = crossProd(r0, q0);
const Vec p0 = prod(H, q0);
const Vec p1 = prod(H, q1);
const Float b0 = -dotProd(q0, c);
const Float b1 = -dotProd(q1, c);
added += int(addConstraint(p0, b0));
added += int(addConstraint(p1, b1));
break;
}
case 2: {
const Vec r0 = A_.row(0);
const Vec r1 = A_.row(1);
const Vec n = crossProd(r0, r1);
const Vec p = prod(H, n);
const Float b = -dotProd(n, c);
added += int(addConstraint(p, b));
break;
}
default:
std::cout << "Overconstrained linear system, skipping" << std::endl;
break;
}
return added;
}
Vec getPlacement() const {
const Mat Ainv = invert(A_);
return prod(Ainv, b_);
}
bool full() const {
return n_ == 3;
}
int count() const {
return n_;
}
private:
/// Contraint compatibility rules (see Sec. 4.2 in LT paper)
bool isCompatible(const Vec& p) const {
const Float distSqr = normSqr(p);
if (distSqr < 1.e-20) {
return false;
}
switch (n_) {
case 0:
return true;
case 1: {
const Vec r0 = A_.row(0);
const Float pr0 = dotProd(r0, p);
const Float r0sqr = dotProd(r0, r0);
const Float lhs = sqr(pr0);
const Float rhs = r0sqr * distSqr * SQR_COS_ALPHA;
return lhs < rhs;
}
case 2: {
const Vec r0 = A_.row(0);
const Vec r1 = A_.row(1);
const Vec n = crossProd(r0, r1);
const Float np = dotProd(n, p);
const Float nsqr = dotProd(n, n);
const Float lhs = sqr(np);
const Float rhs = nsqr * distSqr * SQR_SIN_ALPHA;
return lhs > rhs;
}
default:
throw;
}
}
};
template <typename Mesh>
class MemorylessDecimator {
using Float = typename Mesh::Float;
using Vec = Vector<Float, 3>;
using Mat = Matrix<Float, 3, 3>;
using Constraints = LindstromTurkConstraints<Float>;
public:
Optional<float> cost(const Mesh& mesh, const Graph::CollapseContext& context) const {
const Vec p0 = mesh.point(context.remaining);
const Vec p1 = mesh.point(context.removed);
const Float lengthSqr = normSqr(p0 - p1);
std::set<FaceHandle> triangles = getTriangles(mesh, context);
std::set<VertexHandle> ring = getVertexRing(mesh, context);
std::set<HalfEdgeHandle> boundary; //= getBoundary(mesh, context);
Optional<Vec> dp = computePlacement(mesh, p0, triangles, ring, boundary, lengthSqr);
if (dp) {
Vec p = p0 + dp.value();
const Float volumeCost = computeVolumeCost(mesh, triangles, p);
const Float boundaryCost = computeBoundaryCost(mesh, boundary, p);
const Float totalCost = 0.5 * boundaryCost * lengthSqr + 0.5 * volumeCost;
PVL_ASSERT(totalCost >= 0.);
return totalCost;
} else {
return NONE;
}
}
Optional<Vec> placement(const Mesh& mesh, const Graph::CollapseContext& context) const {
const Vec p0 = mesh.point(context.remaining);
const Vec p1 = mesh.point(context.removed);
const Float lengthSqr = normSqr(p0 - p1);
std::set<FaceHandle> faces = getTriangles(mesh, context);
std::set<VertexHandle> ring = getVertexRing(mesh, context);
std::set<HalfEdgeHandle> boundary; //= getBoundary(ci);
Optional<Vec> dp = computePlacement(mesh, p0, faces, ring, boundary, lengthSqr);
if (dp) {
return p0 + dp.value();
} else {
return NONE;
}
}
void postprocess(const Mesh&, const Graph::CollapseContext&) {}
private:
inline Mat crossProductMatrixSqr(const Vec& p) const {
return {
Vec(p[1] * p[1] + p[2] * p[2], -p[0] * p[1], -p[0] * p[2]),
Vec(-p[0] * p[1], p[0] * p[0] + p[2] * p[2], -p[1] * p[2]),
Vec(-p[0] * p[2], -p[1] * p[2], p[0] * p[0] + p[1] * p[1]),
};
}
Vec computePlacement(const Mesh& mesh,
const Vec& pivot,
const std::set<FaceHandle>& triangles,
const std::set<VertexHandle>& ring,
const std::set<HalfEdgeHandle>& boundary,
const Float lengthSqr) const {
Constraints constraints;
addBoundaryConstraint(mesh, constraints, boundary);
addVolumeConstraint(mesh, pivot, constraints, triangles);
addVolumeAndBoundaryOptimizationConstraint(
mesh, pivot, constraints, triangles, boundary, lengthSqr);
addShapeConstraint(mesh, pivot, constraints, ring);
return constraints.getPlacement();
}
void addVolumeConstraint(const Mesh& mesh,
const Vec& pivot,
Constraints& constraints,
const std::set<FaceHandle>& faces) const {
if (constraints.full()) {
return;
}
Vec sumN(0);
Float sumL = 0.;
for (FaceHandle fh : faces) {
/// \todo add pivot to triangle(fh) ?
std::array<Vec, 3> tri = mesh.triangle(fh);
const Vec n = crossProd(tri[1] - tri[0], tri[2] - tri[0]);
const Float l = dotProd(crossProd(tri[0] - pivot, tri[1] - pivot), tri[2] - pivot);
sumN += n;
sumL += l;
}
constraints.addConstraint(sumN, sumL);
}
void addBoundaryConstraint(const Mesh& mesh,
Constraints& constraints,
const std::set<HalfEdgeHandle>& edges) const {
if (edges.empty() || constraints.full()) {
return;
}
Vec e1(0);
Vec e2(0);
for (HalfEdgeHandle eh : edges) {
const Vec from = mesh.point(mesh.from(eh));
const Vec to = mesh.point(mesh.to(eh));
e1 += from - to;
e2 += crossProd(from, to);
}
const Mat H = crossProductMatrixSqr(e1);
const Vec c = crossProd(e1, e2);
constraints.addQuadraticConstraint(H, c);
}
void addShapeConstraint(const Mesh& mesh,
const Vec& pivot,
Constraints& constraints,
const std::set<VertexHandle>& ring) const {
if (constraints.full()) {
return;
}
Mat H = Mat::identity() * Float(ring.size());
Vec c(0);
for (VertexHandle v : ring) {
c -= (mesh.point(v) - pivot);
}
constraints.addQuadraticConstraint(H, c);
}
void addVolumeAndBoundaryOptimizationConstraint(const Mesh& mesh,
const Vec& pivot,
Constraints& constraints,
const std::set<FaceHandle>& faces,
const std::set<HalfEdgeHandle>& boundary,
const Float lengthSqr) const {
if (constraints.full()) {
std::cout << "Skipping optimization contraint" << std::endl;
return;
}
Mat H = Mat::null();
Vec c(0, 0, 0);
for (FaceHandle face : faces) {
std::array<Vec, 3> tri = mesh.triangle(face);
const Vec n = crossProd(tri[1] - tri[0], tri[2] - tri[0]);
const Float l = dotProd(crossProd(tri[0] - pivot, tri[1] - pivot), tri[2] - pivot);
H += outerProd(n, n);
c -= n * l;
}
if (!boundary.empty()) {
Mat Hb = Mat::null();
Vec cb(0, 0, 0);
for (HalfEdgeHandle edge : boundary) {
const Vec from = mesh.point(mesh.from(edge));
const Vec to = mesh.point(mesh.to(edge));
const Vec n = crossProd(from, to);
const Vec dir = from - to;
Hb += crossProductMatrixSqr(dir);
cb += crossProd(dir, n);
}
// 9 * boundary weight * homogenizing factor
const Float volumeWeight = 0.5;
const Float boundaryWeight = 0.5;
const Float w = 9 * boundaryWeight * lengthSqr;
H = H * volumeWeight + Hb * w;
c = c * volumeWeight + cb * w;
}
constraints.addQuadraticConstraint(H, c);
}
std::set<FaceHandle> getTriangles(const Mesh& mesh,
const Graph::CollapseContext& context) const {
std::set<FaceHandle> faces;
for (FaceHandle fh : mesh.faceRing(context.remaining)) {
faces.insert(fh);
}
for (FaceHandle fh : mesh.faceRing(context.removed)) {
faces.insert(fh);
}
faces.erase(context.left);
faces.erase(context.right);
return faces;
}
/*std::set<HalfedgeHandle> getBoundary(const CollapseInfo& ci) const {
std::set<HalfedgeHandle> handles;
for (VertexHandle v : { ci.v0, ci.v1 }) {
// incomming halfedges
for (auto iter = mesh_.vih_iter(v); iter.is_valid(); ++iter) {
HalfedgeHandle eh1 = *iter;
HalfedgeHandle eh2 = mesh_.opposite_halfedge_handle(eh1);
if (mesh_.is_boundary(eh1)) {
handles.insert(eh1);
}
if (mesh_.is_boundary(eh2)) {
handles.insert(eh2);
}
}
}
return handles;
}*/
std::set<VertexHandle> getVertexRing(const Mesh& mesh,
const Graph::CollapseContext& context) const {
std::set<VertexHandle> ring;
for (VertexHandle v1 : { context.removed, context.remaining }) {
// incomming halfedges
for (VertexHandle v2 : mesh.vertexRing(v1)) {
if (v2 != context.removed && v2 != context.remaining) {
ring.insert(v2);
}
}
}
return ring;
}
Float computeVolumeCost(const Mesh& mesh,
const std::set<FaceHandle>& faces,
const Vec& p) const {
Float cost = 0.;
for (FaceHandle face : faces) {
std::array<Vec, 3> tri = mesh.triangle(face);
const Vec v01 = tri[1] - tri[0];
const Vec v02 = tri[2] - tri[0];
const Vec n = crossProd(v01, v02);
const Float l = dotProd(crossProd(tri[0], tri[1]), tri[2]);
cost += sqr(dotProd(n, p) - l);
}
return cost / 36.;
}
Float computeBoundaryCost(const Mesh& mesh,
const std::set<HalfEdgeHandle>& edges,
const Vec& p) const {
Float cost = 0.;
for (HalfEdgeHandle edge : edges) {
Vec from = mesh.point(mesh.from(edge));
Vec to = mesh.point(mesh.to(edge));
const Vec dir = from - to;
const Vec c = crossProd(dir, from - p);
cost += dotProd(c, c);
}
return cost / 4.;
}
/* bool isCollapseAllowed(const CollapseInfo& ci, const Point& p) const {
// simulate collapse
mesh_.set_point(ci.v0, p);
mesh_.set_point(ci.v1, p);
OnScopeExit guard([&] {
// rollback the collapse
mesh_.set_point(ci.v0, ci.p0);
mesh_.set_point(ci.v1, ci.p1);
});
for (VertexHandle vh : { ci.v0, ci.v1 }) {
for (auto iter = mesh_.vf_iter(vh); iter.is_valid(); ++iter) {
FaceHandle face = *iter;
// these faces will be removed, so no need to test them
if (face != ci.fl && face != ci.fr) {
typename MeshT::Normal n1 = mesh_.normal(face);
typename MeshT::Normal n2 = mesh_.calc_face_normal(face);
const Float cosPhi = dot(n1, n2);
if (cosPhi < MIN_COS_PHI) {
return false;
}
}
}
}
return true;
}*/
};
} // namespace Pvl