-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathsamples_test.cpp
314 lines (281 loc) · 9.94 KB
/
samples_test.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
// Copyright 2021 The Manifold Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "samples.h"
#ifdef MANIFOLD_CROSS_SECTION
#include "manifold/cross_section.h"
#endif
#include "manifold/polygon.h"
#include "test.h"
using namespace manifold;
std::vector<int> EdgePairs(const MeshGL in) {
const int numHalfedge = 3 * in.NumTri();
std::vector<int> edgePair(numHalfedge);
std::map<std::pair<int, int>, int> halfedgeLink;
for (int i = 0; i < numHalfedge; ++i) {
std::pair<int, int> key = std::make_pair(
in.triVerts[i], in.triVerts[(i + 1) % 3 == 0 ? i - 2 : i + 1]);
if (key.first > key.second) std::swap(key.first, key.second);
const auto result = halfedgeLink.emplace(std::make_pair(key, i));
if (!result.second) {
const int pair = result.first->second;
edgePair[pair] = i;
edgePair[i] = pair;
}
}
return edgePair;
}
#ifdef MANIFOLD_CROSS_SECTION
// If you print this knot (with support), you can snap a half-inch marble into
// it and it'll roll around (dimensions in mm).
TEST(Samples, Knot13) {
Manifold knot13 = TorusKnot(1, 3, 25, 10, 3.75);
#ifdef MANIFOLD_EXPORT
if (options.exportModels) ExportMesh("knot13.glb", knot13.GetMeshGL(), {});
#endif
EXPECT_EQ(knot13.Genus(), 1);
EXPECT_NEAR(knot13.Volume(), 20786, 1);
EXPECT_NEAR(knot13.SurfaceArea(), 11177, 1);
CheckGL(knot13);
}
// This creates two interlinked knots.
TEST(Samples, Knot42) {
Manifold knot42 = TorusKnot(4, 2, 15, 6, 5);
#ifdef MANIFOLD_EXPORT
if (options.exportModels) ExportMesh("knot42.glb", knot42.GetMeshGL(), {});
#endif
std::vector<Manifold> knots = knot42.Decompose();
ASSERT_EQ(knots.size(), 2);
EXPECT_EQ(knots[0].Genus(), 1);
EXPECT_EQ(knots[1].Genus(), 1);
EXPECT_NEAR(knots[0].Volume(), knots[1].Volume(), 1);
EXPECT_NEAR(knots[0].SurfaceArea(), knots[1].SurfaceArea(), 1);
CheckGL(knot42);
}
#endif
TEST(Samples, Scallop) {
Manifold scallop = Scallop();
#ifdef MANIFOLD_EXPORT
if (options.exportModels) {
MeshGL in = scallop.GetMeshGL();
std::vector<int> edgePair = EdgePairs(in);
ExportOptions options;
const int numVert = scallop.NumVert();
const int numHalfedge = 3 * scallop.NumTri();
const int numProp = in.numProp;
for (int i = 0; i < numHalfedge; ++i) {
const int vert = in.triVerts[i];
for (int j : {0, 1, 2}) {
in.vertProperties.push_back(in.vertProperties[numProp * vert + j] +
in.halfedgeTangent[4 * i + j] *
in.halfedgeTangent[4 * i + 3]);
}
const int j = edgePair[i % 3 == 0 ? i + 2 : i - 1];
in.triVerts.push_back(vert);
in.triVerts.push_back(numVert + i);
in.triVerts.push_back(numVert + j);
}
options.faceted = true;
options.mat.roughness = 0.5;
ExportMesh("scallopFacets.glb", scallop.GetMeshGL(), options);
}
#endif
auto colorCurvature = [](double* newProp, vec3 pos, const double* oldProp) {
const double curvature = oldProp[0];
const vec3 red(1, 0, 0);
const vec3 blue(0, 0, 1);
const double limit = 15;
vec3 color = la::lerp(blue, red, smoothstep(-limit, limit, curvature));
for (const int i : {0, 1, 2}) {
newProp[i] = color[i];
}
};
scallop = scallop.Refine(50).CalculateCurvature(-1, 0).SetProperties(
3, colorCurvature);
EXPECT_NEAR(scallop.Volume(), 39.9, 0.1);
EXPECT_NEAR(scallop.SurfaceArea(), 79.3, 0.1);
EXPECT_EQ(scallop.NumVert(), scallop.NumPropVert());
CheckGL(scallop);
#ifdef MANIFOLD_EXPORT
if (options.exportModels) {
MeshGL out = scallop.GetMeshGL();
ExportOptions options2;
// options2.faceted = false;
options2.mat.roughness = 0.1;
options2.mat.metalness = 0;
options2.mat.colorIdx = 0;
ExportMesh("scallop.glb", out, options2);
}
#endif
}
TEST(Samples, TetPuzzle) {
Manifold puzzle = TetPuzzle(50, 0.2, 50);
EXPECT_LE(puzzle.NumDegenerateTris(), 2);
CheckGL(puzzle);
Manifold puzzle2 = puzzle.Rotate(0, 0, 180);
EXPECT_TRUE((puzzle ^ puzzle2).IsEmpty());
quat q = rotation_quat(normalize(vec3(1, -1, -1)), vec3(0, 0, 1));
puzzle = puzzle.Transform({la::qmat(q), vec3()});
#ifdef MANIFOLD_EXPORT
if (options.exportModels) ExportMesh("tetPuzzle.glb", puzzle.GetMeshGL(), {});
#endif
}
TEST(Samples, FrameReduced) {
Manifold frame = RoundedFrame(100, 10, 4);
EXPECT_EQ(frame.NumDegenerateTris(), 0);
EXPECT_EQ(frame.Genus(), 5);
EXPECT_NEAR(frame.Volume(), 227333, 10);
EXPECT_NEAR(frame.SurfaceArea(), 62635, 1);
CheckGL(frame);
#ifdef MANIFOLD_EXPORT
if (options.exportModels)
ExportMesh("roundedFrameReduced.glb", frame.GetMeshGL(), {});
#endif
}
TEST(Samples, Frame) {
Manifold frame = RoundedFrame(100, 10);
EXPECT_EQ(frame.NumDegenerateTris(), 0);
EXPECT_EQ(frame.Genus(), 5);
CheckGL(frame);
#ifdef MANIFOLD_EXPORT
if (options.exportModels)
ExportMesh("roundedFrame.glb", frame.GetMeshGL(), {});
#endif
}
// This creates a bracelet sample which involves many operations between shapes
// that are not in general position, e.g. coplanar faces.
#ifdef MANIFOLD_CROSS_SECTION
TEST(Samples, Bracelet) {
Manifold bracelet = StretchyBracelet();
EXPECT_EQ(bracelet.NumDegenerateTris(), 0);
EXPECT_EQ(bracelet.Genus(), 1);
CheckGL(bracelet);
CrossSection projection(bracelet.Project());
projection = projection.Simplify(bracelet.BoundingBox().Scale() * 1e-8);
Rect rect = projection.Bounds();
Box box = bracelet.BoundingBox();
EXPECT_FLOAT_EQ(rect.min.x, box.min.x);
EXPECT_FLOAT_EQ(rect.min.y, box.min.y);
EXPECT_FLOAT_EQ(rect.max.x, box.max.x);
EXPECT_FLOAT_EQ(rect.max.y, box.max.y);
EXPECT_NEAR(projection.Area(), 649, 1);
EXPECT_EQ(projection.NumContour(), 2);
Manifold extrusion = Manifold::Extrude(projection.ToPolygons(), 1);
EXPECT_EQ(extrusion.NumDegenerateTris(), 0);
EXPECT_EQ(extrusion.Genus(), 1);
CrossSection slice(bracelet.Slice());
EXPECT_EQ(slice.NumContour(), 2);
EXPECT_NEAR(slice.Area(), 230.6, 0.1);
extrusion = Manifold::Extrude(slice.ToPolygons(), 1);
EXPECT_EQ(extrusion.Genus(), 1);
#ifdef MANIFOLD_EXPORT
if (options.exportModels)
ExportMesh("bracelet.glb", bracelet.GetMeshGL(), {});
#endif
}
TEST(Samples, GyroidModule) {
const double size = 20;
Manifold gyroid = GyroidModule(size);
EXPECT_LE(gyroid.NumDegenerateTris(), 4);
EXPECT_EQ(gyroid.Genus(), 15);
CheckGL(gyroid);
const Box bounds = gyroid.BoundingBox();
const double epsilon = gyroid.GetEpsilon();
EXPECT_NEAR(bounds.min.z, 0, epsilon);
EXPECT_NEAR(bounds.max.z, size * std::sqrt(2.0), epsilon);
CrossSection slice(gyroid.Slice(5));
EXPECT_EQ(slice.NumContour(), 4);
EXPECT_NEAR(slice.Area(), 121.9, 0.1);
Manifold extrusion = Manifold::Extrude(slice.ToPolygons(), 1);
EXPECT_EQ(extrusion.Genus(), -3);
#ifdef MANIFOLD_EXPORT
if (options.exportModels)
ExportMesh("gyroidModule.glb", gyroid.GetMeshGL(), {});
#endif
}
#endif
TEST(Samples, Sponge1) {
Manifold sponge = MengerSponge(1);
EXPECT_EQ(sponge.NumDegenerateTris(), 0);
EXPECT_EQ(sponge.NumVert(), 40);
EXPECT_EQ(sponge.Genus(), 5);
CheckGL(sponge);
#ifdef MANIFOLD_EXPORT
if (options.exportModels)
ExportMesh("mengerSponge1.glb", sponge.GetMeshGL(), {});
#endif
}
// This sample needs a lot of memory to run and is therefore disabled for
// emscripten.
#ifndef __EMSCRIPTEN__
// A fractal with many degenerate intersections, which also tests exact 90
// degree rotations.
#ifdef MANIFOLD_CROSS_SECTION
TEST(Samples, Sponge4) {
Manifold sponge = MengerSponge(4);
EXPECT_LE(sponge.NumDegenerateTris(), 8);
EXPECT_EQ(sponge.Genus(), 26433); // should be 1:5, 2:81, 3:1409, 4:26433
CheckGL(sponge);
std::pair<Manifold, Manifold> cutSponge = sponge.SplitByPlane({1, 1, 1}, 0);
EXPECT_EQ(cutSponge.first.Genus(), 13394);
EXPECT_EQ(cutSponge.second.Genus(), 13394);
CrossSection projection(cutSponge.first.Project());
projection = projection.Simplify(cutSponge.first.GetEpsilon());
Rect rect = projection.Bounds();
Box box = cutSponge.first.BoundingBox();
EXPECT_EQ(rect.min.x, box.min.x);
EXPECT_EQ(rect.min.y, box.min.y);
EXPECT_EQ(rect.max.x, box.max.x);
EXPECT_EQ(rect.max.y, box.max.y);
EXPECT_NEAR(projection.Area(), 0.535, 0.001);
Manifold extrusion = Manifold::Extrude(projection.ToPolygons(), 1);
EXPECT_LE(extrusion.NumDegenerateTris(), 32);
EXPECT_EQ(extrusion.Genus(), 502);
#ifdef MANIFOLD_EXPORT
if (options.exportModels) {
ExportMesh("mengerHalf.glb", cutSponge.first.GetMeshGL(), {});
const MeshGL out = sponge
.SetProperties(3,
[](double* newProp, vec3 pos,
const double* oldProp) {
for (const int i : {0, 1, 2})
newProp[i] = 0.5 * (pos[i] + 0.5);
})
.GetMeshGL();
ExportOptions options;
options.faceted = true;
options.mat.roughness = 0.2;
options.mat.metalness = 1.0;
options.mat.colorIdx = 0;
ExportMesh("mengerSponge.glb", out, options);
}
#endif
}
#endif
#endif
TEST(Samples, DISABLED_CondensedMatter16) {
Manifold cm = CondensedMatter(16);
CheckGL(cm);
#ifdef MANIFOLD_EXPORT
if (options.exportModels)
ExportMesh("condensedMatter16.glb", cm.GetMeshGL(), {});
#endif
}
TEST(Samples, CondensedMatter64) {
Manifold cm = CondensedMatter(64);
CheckGL(cm);
#ifdef MANIFOLD_EXPORT
if (options.exportModels)
ExportMesh("condensedMatter64.glb", cm.GetMeshGL(), {});
#endif
}