Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_mesh_inset_test.cc « tests « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b1313d536b04d8660470e41a82cf4e2cf89b34e4 (plain)
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
/* SPDX-License-Identifier: Apache-2.0 */

#include "testing/testing.h"

#include <fstream>
#include <iostream>
#include <sstream>

#include "BLI_array.hh"
#include "BLI_mesh_inset.hh"
#include "BLI_vector.hh"

namespace blender::meshinset {

namespace test {

class SpecArrays {
 public:
  Array<float3> vert;
  Array<Vector<int>> face;
  Array<Vector<int>> contour;
};

/* The spec should have the form:
 * #verts #faces #contours
 * <float> <float> <float>  [#verts lines]
 * <int> <int> ... <int>  [#faces lines]
 * <int> <int> ... <int> [#contours lines]
 */
static SpecArrays fill_input_from_string(const char *spec)
{
  SpecArrays ans;
  std::istringstream ss(spec);
  std::string line;
  getline(ss, line);
  std::istringstream hdrss(line);
  int nverts, nfaces, ncontours;
  hdrss >> nverts >> nfaces >> ncontours;
  if (nverts == 0) {
    return SpecArrays();
  }
  ans.vert = Array<float3>(nverts);
  ans.face = Array<Vector<int>>(nfaces);
  ans.contour = Array<Vector<int>>(ncontours);
  int i = 0;
  while (i < nverts && getline(ss, line)) {
    std::istringstream iss(line);
    float x, y, z;
    iss >> x >> y >> z;
    ans.vert[i] = float3(x, y, z);
    i++;
  }
  i = 0;
  while (i < nfaces && getline(ss, line)) {
    std::istringstream fss(line);
    int v;
    while (fss >> v) {
      ans.face[i].append(v);
    }
    i++;
  }
  i = 0;
  while (i < ncontours && getline(ss, line)) {
    std::istringstream css(line);
    int v;
    while (css >> v) {
      ans.contour[i].append(v);
    }
    i++;
  }
  return ans;
}

class InputHolder {
  SpecArrays spec_arrays_;

 public:
  MeshInset_Input input;

  InputHolder(const char *spec, float amount)
  {
    spec_arrays_ = fill_input_from_string(spec);
    input.vert = spec_arrays_.vert.as_span();
    input.face = spec_arrays_.face.as_span();
    input.contour = spec_arrays_.contour.as_span();
    input.inset_amount = amount;
    input.slope = 0.0f;
    input.need_ids = false;
  }
};

TEST(mesh_inset, Tri)
{
  const char *spec = R"(3 1 1
  0.0 0.0 0.0
  1.0 0.0 0.0
  0.5 0.5 0.0
  0 1 2
  0 1 2
  )";

  InputHolder in1(spec, 0.1);
  MeshInset_Result out1 = mesh_inset_calc(in1.input);
  EXPECT_EQ(out1.vert.size(), 6);
  EXPECT_EQ(out1.face.size(), 4);

  InputHolder in2(spec, 0.3);
  MeshInset_Result out2 = mesh_inset_calc(in2.input);
  EXPECT_EQ(out2.vert.size(), 4);
  EXPECT_EQ(out2.face.size(), 3);
}

/* An asymmetrical quadrilateral. */
TEST(mesh_inset, Quad)
{
  const char *spec = R"(4 1 1
  -1.0 -1.0 0.0
  1.1 -1.0 0.0
  0.9 0.9 0.0
  -0.5 1.0 0.0
  0 1 2 3
  0 1 2 3
  )";

  InputHolder in1(spec, 0.3);
  MeshInset_Result out1 = mesh_inset_calc(in1.input);
  EXPECT_EQ(out1.vert.size(), 8);
  EXPECT_EQ(out1.face.size(), 5);

  InputHolder in2(spec, .85);
  MeshInset_Result out2 = mesh_inset_calc(in2.input);
  EXPECT_EQ(out2.vert.size(), 8);
  EXPECT_EQ(out2.face.size(), 5);

  InputHolder in3(spec, .88);
  MeshInset_Result out3 = mesh_inset_calc(in3.input);
  EXPECT_EQ(out3.vert.size(), 6);
  EXPECT_EQ(out3.face.size(), 4);
}

TEST(mesh_inset, Square)
{
  const char *spec = R"(4 1 1
  0.0 0.0 0.0
  1.0 0.0 0.0
  1.0 1.0 0.0
  0.0 1.0 0.0
  0 1 2 3
  0 1 2 3
  )";

  InputHolder in1(spec, 0.4);
  MeshInset_Result out1 = mesh_inset_calc(in1.input);
  EXPECT_EQ(out1.vert.size(), 8);
  EXPECT_EQ(out1.face.size(), 5);

  InputHolder in2(spec, 0.51);
  in2.input.slope = 0.5f;
  MeshInset_Result out2 = mesh_inset_calc(in2.input);
  /* Note: current code wants all 3-valence vertices in
   * straight skeleton, so the center doesn't collapse to
   * a single vertex, but rather two vertices with a zero
   * length edge between them. */
  EXPECT_EQ(out2.vert.size(), 6);
  EXPECT_EQ(out2.face.size(), 4);
  /* The last two verts should be in the center, with height 0.25. */
  const float3 &v4 = out2.vert[4];
  const float3 &v5 = out2.vert[5];
  EXPECT_NEAR(v4.x, 0.5, 1e-5);
  EXPECT_NEAR(v4.y, 0.5, 1e-5);
  EXPECT_NEAR(v4.z, 0.25, 1e-5);
  EXPECT_NEAR(v5.x, 0.5, 1e-5);
  EXPECT_NEAR(v5.y, 0.5, 1e-5);
  EXPECT_NEAR(v5.z, 0.25, 1e-5);
}

TEST(mesh_inset, Pentagon)
{
  const char *spec = R"(5 1 1
  0.0 0.0 0.0
  1.0 0.0 0.0
  1.0 1.0 0.0
  0.5 1.5 0.0
  0.0 1.0 0.0
  0 1 2 3 4
  0 1 2 3 4
  )";

  InputHolder in1(spec, 0.2);
  MeshInset_Result out1 = mesh_inset_calc(in1.input);
  EXPECT_EQ(out1.vert.size(), 10);
  EXPECT_EQ(out1.face.size(), 6);

  InputHolder in2(spec, 1.0);
  MeshInset_Result out2 = mesh_inset_calc(in2.input);
  /* Because code wants all valence-3 vertices in the skeleton,
   * there is a zero-length edge in this output. */
  EXPECT_EQ(out2.vert.size(), 8);
  EXPECT_EQ(out2.face.size(), 5);
}

TEST(mesh_inset, Hexagon)
{
  const char *spec = R"(6 1 1
  0.0 1.0 0.0
  0.125 0.0 0.0
  0.625 -0.75 0.0
  1.5 -1.0 0.0
  2.875 0.0 0.0
  3.0 1.0 0.0
  0 1 2 3 4 5
  0 1 2 3 4 5
  )";

  InputHolder in1(spec, 0.4);
  MeshInset_Result out1 = mesh_inset_calc(in1.input);
  EXPECT_EQ(out1.vert.size(), 12);
  EXPECT_EQ(out1.face.size(), 7);

  InputHolder in2(spec, 0.67);
  MeshInset_Result out2 = mesh_inset_calc(in2.input);
  EXPECT_EQ(out2.vert.size(), 12);
  EXPECT_EQ(out2.face.size(), 7);

  InputHolder in3(spec, 0.85);
  MeshInset_Result out3 = mesh_inset_calc(in3.input);
  EXPECT_EQ(out3.vert.size(), 12);
  EXPECT_EQ(out3.face.size(), 7);

  InputHolder in4(spec, 0.945);
  MeshInset_Result out4 = mesh_inset_calc(in4.input);
  EXPECT_EQ(out4.vert.size(), 12);
  EXPECT_EQ(out4.face.size(), 7);

  InputHolder in5(spec, 0.97);
  MeshInset_Result out5 = mesh_inset_calc(in5.input);
  EXPECT_EQ(out5.vert.size(), 10);
  EXPECT_EQ(out5.face.size(), 6);
}

TEST(mesh_inset, Splitter)
{
  const char *spec = R"(5 1 1
  0.0 0.0 0.0
  1.5 0.1 0.0
  1.75 0.8 0.0
  0.8 0.6 0.0
  0.0 1.0 0.0
  0 1 2 3 4
  0 1 2 3 4
  )";

  InputHolder in1(spec, 0.25);
  MeshInset_Result out1 = mesh_inset_calc(in1.input);
  EXPECT_EQ(out1.vert.size(), 10);
  EXPECT_EQ(out1.face.size(), 6);

  InputHolder in2(spec, 0.29);
  MeshInset_Result out2 = mesh_inset_calc(in2.input);
  EXPECT_EQ(out2.vert.size(), 12);
  EXPECT_EQ(out2.face.size(), 7);

  InputHolder in3(spec, 0.40);
  MeshInset_Result out3 = mesh_inset_calc(in3.input);
  EXPECT_EQ(out3.vert.size(), 8);
  EXPECT_EQ(out3.face.size(), 5);
}

TEST(mesh_inset, Flipper)
{
  const char *spec = R"(20 1 1
  0.0 0.0 0.0
  1.5 0.0 0.0
  1.375 0.025 0.0
  1.25 0.06 0.0
  1.125 0.11 0.0
  1.0 0.2 0.0
  1.0 1.0 0.0
  0.79 1.0 0.0
  0.75 0.95 0.0
  0.71 1.0 0.0
  0.585 1.0 0.0
  0.55 0.9 0.0
  0.515 1.0 0.0
  0.38 1.0 0.0
  0.35 0.85 0.0
  0.32 1.0 0.0
  0.175 1.0 0.0
  0.15 0.8 0.0
  0.125 1.0 0.0
  0.0 1.0 0.0
  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  )";

  InputHolder in1(spec, 0.01);
  MeshInset_Result out1 = mesh_inset_calc(in1.input);
  EXPECT_EQ(out1.vert.size(), 40);
  EXPECT_EQ(out1.face.size(), 21);

  InputHolder in2(spec, 0.06);
  MeshInset_Result out2 = mesh_inset_calc(in2.input);
  EXPECT_EQ(out2.vert.size(), 40);
  EXPECT_EQ(out2.face.size(), 21);

  InputHolder in3(spec, 0.07);
  MeshInset_Result out3 = mesh_inset_calc(in3.input);
  EXPECT_EQ(out3.vert.size(), 40);
  EXPECT_EQ(out3.face.size(), 21);

  InputHolder in4(spec, 0.08);
  MeshInset_Result out4 = mesh_inset_calc(in4.input);
  EXPECT_EQ(out4.vert.size(), 40);
  EXPECT_EQ(out4.face.size(), 21);

  InputHolder in5(spec, 0.087);
  MeshInset_Result out5 = mesh_inset_calc(in5.input);
  EXPECT_EQ(out5.vert.size(), 40);
  EXPECT_EQ(out5.face.size(), 21);

  InputHolder in6(spec, 0.0878);
  MeshInset_Result out6 = mesh_inset_calc(in6.input);
  EXPECT_EQ(out6.vert.size(), 40);
  EXPECT_EQ(out6.face.size(), 21);

  InputHolder in7(spec, 0.11);
  MeshInset_Result out7 = mesh_inset_calc(in7.input);
  EXPECT_EQ(out7.vert.size(), 42);
  EXPECT_EQ(out7.face.size(), 22);

  InputHolder in8(spec, 0.24);
  MeshInset_Result out8 = mesh_inset_calc(in8.input);
  EXPECT_EQ(out8.vert.size(), 42);
  EXPECT_EQ(out8.face.size(), 22);

  InputHolder in9(spec, 0.255);
  MeshInset_Result out9 = mesh_inset_calc(in9.input);
  EXPECT_EQ(out9.vert.size(), 42);
  EXPECT_EQ(out9.face.size(), 22);

  InputHolder in10(spec, 0.30);
  MeshInset_Result out10 = mesh_inset_calc(in10.input);
  EXPECT_EQ(out10.vert.size(), 40);
  EXPECT_EQ(out10.face.size(), 21);

  InputHolder in11(spec, 0.35);
  MeshInset_Result out11 = mesh_inset_calc(in11.input);
  EXPECT_EQ(out11.vert.size(), 38);
  EXPECT_EQ(out11.face.size(), 20);
}

#if 0
TEST(mesh_inset, Grid)
{
  const char *spec = R"(16 9 1
  0.0 0.0 0.0
  1.0 0.0 0.0
  2.0 0.0 0.0
  3.0 0.0 0.0
  0.0 1.0 0.0
  1.0 1.0 0.0
  2.0 1.0 0.0
  3.0 1.0 0.0
  0.0 2.0 0.0
  1.0 2.0 0.0
  2.0 2.0 0.0
  3.0 2.0 0.0
  0.0 3.0 0.0
  1.0 3.0 0.0
  2.0 3.0 0.0
  3.0 3.0 0.0
  0 1 5 4
  1 2 6 5
  2 3 7 6
  4 5 9 8
  5 6 10 9
  6 7 11 10
  8 9 13 12
  9 10 14 13
  10 11 15 14
  0 1 2 3 7 11 15 14 13 12 8 4
  )";

  InputHolder in1(spec, 0.5);
  MeshInset_Result out1 = mesh_inset_calc(in1.input);
  EXPECT_EQ(out1.vert.size(), 28);
  EXPECT_EQ(out1.face.size(), 21);
}
#endif

}  // namespace test

}  // namespace blender::meshinset