Repository navigation
Expand file tree
/
Copy pathobj_oracle.cpp
More file actions
512 lines (470 loc) · 18.6 KB
/
Copy pathobj_oracle.cpp
File metadata and controls
512 lines (470 loc) · 18.6 KB
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#include <trueform/core/blocked_buffer.hpp>
#include <trueform/core/complete.hpp>
#include <trueform/core/offset_block_buffer.hpp>
#include <trueform/core/points_buffer.hpp>
#include <trueform/core/polygons_buffer.hpp>
#include <trueform/core/range.hpp>
#include <trueform/core/static_size.hpp>
#include <trueform/io/obj_file.hpp>
#include <trueform/io/obj_reader.hpp>
#include <trueform/io/read_obj.hpp>
#include <tbb/task_arena.h>
#include <tbb/task_group.h>
#include <array>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <ios>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
namespace {
class oracle_output {
public:
explicit oracle_output(const std::filesystem::path &path)
: _stream(path, std::ios::binary) {}
explicit operator bool() const {
return _valid && static_cast<bool>(_stream);
}
template <typename T> auto scalar(const T &value) -> void {
static_assert(std::is_trivially_copyable_v<T>);
_stream.write(reinterpret_cast<const char *>(&value), sizeof(value));
_valid &= static_cast<bool>(_stream);
}
template <typename T> auto buffer(const tf::buffer<T> &values) -> void {
scalar(values.size());
_stream.write(reinterpret_cast<const char *>(values.data()),
static_cast<std::streamsize>(values.size() * sizeof(T)));
_valid &= static_cast<bool>(_stream);
}
auto strings(const std::vector<std::string> &values) -> void {
scalar(values.size());
for (const auto &value : values) {
scalar(value.size());
_stream.write(value.data(), static_cast<std::streamsize>(value.size()));
_valid &= static_cast<bool>(_stream);
}
}
auto finish() -> bool {
_stream.flush();
_valid &= static_cast<bool>(_stream);
_stream.close();
_valid &= !_stream.fail();
return _valid;
}
private:
std::ofstream _stream;
bool _valid = true;
};
template <typename Index, typename RealT, std::size_t Dims, std::size_t Ngon>
auto write(oracle_output &output,
const tf::polygons_buffer<Index, RealT, Dims, Ngon> &polygons)
-> void {
output.buffer(polygons.points_buffer().data_buffer());
output.buffer(polygons.faces_buffer().data_buffer());
}
template <typename Index, typename RealT, std::size_t Dims>
auto write(
oracle_output &output,
const tf::polygons_buffer<Index, RealT, Dims, tf::dynamic_size> &polygons)
-> void {
output.buffer(polygons.points_buffer().data_buffer());
output.buffer(polygons.faces_buffer().offsets_buffer());
output.buffer(polygons.faces_buffer().data_buffer());
}
template <typename Index, typename RealT>
auto write(oracle_output &output, const tf::obj_file<Index, RealT> &obj_file)
-> void {
write(output, obj_file.polygons);
output.buffer(obj_file.normals.data_buffer());
output.buffer(obj_file.textures.data_buffer());
output.buffer(obj_file.face_groups);
output.strings(obj_file.group_names);
output.buffer(obj_file.face_objects);
output.strings(obj_file.object_names);
}
auto make_fixed_obj(std::size_t face_count, std::size_t ngon,
std::string_view line_end, bool final_line_end,
std::string_view face_suffix) -> std::string {
std::string data;
data.reserve(256 + face_count * (ngon * 8 + line_end.size() + 2));
data += "# fixed fixture";
data += line_end;
data += " v 1.0000000000000002 0 0";
data += line_end;
data += "v 0 2.0000000000000004 0";
data += line_end;
data += "v 0 0 3.0000000000000007";
data += line_end;
if (ngon == 4) {
data += "v 4.000000000000001 0 0";
data += line_end;
}
for (std::size_t face = 0; face < face_count; ++face) {
data += "f";
for (std::size_t corner = 0; corner < ngon; ++corner) {
data += " ";
data += std::to_string(corner + 1);
data += face_suffix;
}
if ((face % 17) == 0)
data += " # face";
if (face + 1 != face_count || final_line_end)
data += line_end;
}
return data;
}
auto make_mixed_obj() -> std::string {
return "v 0 0 0\n"
"v 1 0 0\n"
"v 1 1 0\n"
"v 0 1 0\n"
"v -1 0 0\n"
"f 1 2 3\n"
"f 1 3 4 5\n";
}
auto make_complete_obj(std::string_view face) -> std::string {
return "v 0 0 0\n"
"v 1 0 0\n"
"v 0 1 0\n"
"vt 0 0\n"
"vt 1 0\n"
"vt 0 1\n"
"vn 0 0 1\n"
"f 1/1/1 2/2/1 3/3/1\n"
"g front\n"
"o shape\n" +
std::string(face);
}
auto write_fixture(const std::filesystem::path &path, const std::string &data)
-> bool {
std::ofstream output(path, std::ios::binary);
if (!output)
return false;
output.write(data.data(), static_cast<std::streamsize>(data.size()));
output.flush();
if (!output)
return false;
output.close();
if (output.fail())
return false;
std::error_code error;
return std::filesystem::file_size(path, error) == data.size() && !error;
}
auto remove_file(const std::filesystem::path &path) -> bool {
std::error_code error;
const auto removed = std::filesystem::remove(path, error);
return !error && removed;
}
auto ensure_absent(const std::filesystem::path &path) -> bool {
std::error_code error;
std::filesystem::remove(path, error);
if (error)
return false;
return !std::filesystem::exists(path, error) && !error;
}
template <typename Index, typename RealT, std::size_t Dims, std::size_t Ngon>
auto valid_fixed(const tf::polygons_buffer<Index, RealT, Dims, Ngon> &polygons,
std::size_t point_count, std::size_t face_count) -> bool {
if (polygons.points_buffer().size() != point_count ||
static_cast<std::size_t>(polygons.faces_buffer().size()) != face_count)
return false;
if (point_count == 0 || face_count == 0)
return point_count == 0 && face_count == 0;
if (polygons.points_buffer()[0][0] != static_cast<RealT>(1.0000000000000002))
return false;
const auto face = polygons.faces_buffer()[0];
for (std::size_t corner = 0; corner < Ngon; ++corner) {
if (face[corner] != static_cast<Index>(corner))
return false;
}
return true;
}
template <typename Index, typename RealT, std::size_t Dims>
auto valid_dynamic(
const tf::polygons_buffer<Index, RealT, Dims, tf::dynamic_size> &polygons,
std::size_t point_count, std::size_t face_count, std::size_t index_count)
-> bool {
const auto &faces = polygons.faces_buffer();
if (polygons.points_buffer().size() != point_count ||
faces.offsets_buffer().size() != (face_count == 0 ? 0 : face_count + 1) ||
faces.data_buffer().size() != index_count)
return false;
if (face_count == 0)
return point_count == 0 && index_count == 0;
return faces.offsets_buffer().front() == 0 &&
faces.offsets_buffer().back() == static_cast<Index>(index_count) &&
faces.data_buffer().front() == Index(0);
}
template <typename Index, typename RealT>
auto valid_complete(const tf::obj_file<Index, RealT> &obj_file,
std::size_t point_count, std::size_t face_count,
bool has_textures, bool has_normals, bool has_labels)
-> bool {
const auto &faces = obj_file.polygons.faces_buffer();
if (obj_file.polygons.points_buffer().size() != point_count ||
faces.offsets_buffer().size() != (face_count == 0 ? 0 : face_count + 1))
return false;
if (face_count == 0)
return point_count == 0 && obj_file.normals.data_buffer().size() == 0 &&
obj_file.textures.data_buffer().size() == 0;
if (faces.data_buffer().size() != face_count * 3 ||
obj_file.normals.data_buffer().size() !=
(has_normals ? point_count * 3 : 0) ||
obj_file.textures.data_buffer().size() !=
(has_textures ? point_count * 2 : 0))
return false;
if (!has_labels)
return obj_file.group_names.empty() && obj_file.object_names.empty() &&
obj_file.face_groups.size() == 0 &&
obj_file.face_objects.size() == 0;
return obj_file.group_names == std::vector<std::string>{"default", "front"} &&
obj_file.object_names ==
std::vector<std::string>{"default", "shape"} &&
obj_file.face_groups.size() == face_count &&
obj_file.face_objects.size() == face_count &&
obj_file.face_groups.front() == Index(0) &&
obj_file.face_groups.back() == Index(1) &&
obj_file.face_objects.front() == Index(0) &&
obj_file.face_objects.back() == Index(1);
}
auto const_range(const std::string &data) {
return tf::make_range(static_cast<const char *>(data.data()), data.size());
}
template <typename Function>
auto in_arena(int concurrency, Function &&function) {
tbb::task_arena arena(concurrency);
return arena.execute(std::forward<Function>(function));
}
template <typename Index, typename RealT, std::size_t Dims, std::size_t Ngon>
auto write_fixed_append(oracle_output &output, const std::string &data,
int concurrency, bool expected_valid,
std::size_t expected_points, std::size_t expected_faces)
-> bool {
tf::points_buffer<RealT, Dims> points;
points.allocate(1);
for (std::size_t dim = 0; dim < Dims; ++dim)
points[0][dim] = static_cast<RealT>(17 + dim);
tf::blocked_buffer<Index, Ngon> faces;
std::array<Index, Ngon> seed{};
for (std::size_t corner = 0; corner < Ngon; ++corner)
seed[corner] = static_cast<Index>(corner + 9);
faces.push_back(seed);
tf::io::obj_reader reader;
const auto valid = in_arena(concurrency, [&] {
return reader.read(const_range(data), points, faces);
});
output.scalar(valid);
output.buffer(points.data_buffer());
output.buffer(faces.data_buffer());
return valid == expected_valid && points.size() == expected_points &&
static_cast<std::size_t>(faces.size()) == expected_faces;
}
template <typename Index, typename RealT, std::size_t Dims, std::size_t Ngon>
auto write_cancelled_fixed_append(oracle_output &output,
const std::string &data,
std::size_t expected_points,
std::size_t expected_faces) -> bool {
tf::points_buffer<RealT, Dims> points;
tf::blocked_buffer<Index, Ngon> faces;
tf::io::obj_reader reader;
bool called = false;
bool valid = false;
tbb::task_group_context context;
tbb::task_group group(context);
group.run([&] {
called = true;
context.cancel_group_execution();
valid = reader.read(const_range(data), points, faces);
});
group.wait();
output.scalar(called);
output.scalar(valid);
output.buffer(points.data_buffer());
output.buffer(faces.data_buffer());
return called && valid && points.size() == expected_points &&
static_cast<std::size_t>(faces.size()) == expected_faces;
}
template <typename Index, typename RealT, std::size_t Dims>
auto write_dynamic_append(oracle_output &output, const std::string &data,
bool expected_valid, std::size_t expected_points,
std::size_t expected_faces,
std::size_t expected_indices) -> bool {
tf::points_buffer<RealT, Dims> points;
points.allocate(1);
for (std::size_t dim = 0; dim < Dims; ++dim)
points[0][dim] = static_cast<RealT>(31 + dim);
tf::offset_block_buffer<Index, Index> faces;
faces.offsets_buffer().push_back(0);
faces.offsets_buffer().push_back(3);
faces.data_buffer().push_back(4);
faces.data_buffer().push_back(5);
faces.data_buffer().push_back(6);
tf::io::obj_reader reader;
const auto valid = reader.read(const_range(data), points, faces);
output.scalar(valid);
output.buffer(points.data_buffer());
output.buffer(faces.offsets_buffer());
output.buffer(faces.data_buffer());
return valid == expected_valid && points.size() == expected_points &&
faces.offsets_buffer().size() == expected_faces + 1 &&
faces.data_buffer().size() == expected_indices;
}
auto run(const std::filesystem::path &output_path, bool corrupt) -> bool {
static_assert(
std::is_same_v<decltype(tf::read_obj(std::string_view{})),
tf::polygons_buffer<int, float, 3, tf::dynamic_size>>);
static_assert(std::is_same_v<decltype(tf::read_obj<3>(std::string_view{})),
tf::polygons_buffer<int, float, 3, 3>>);
static_assert(
std::is_same_v<decltype(tf::read_obj<std::int64_t, 4, double>(
std::string_view{})),
tf::polygons_buffer<std::int64_t, double, 3, 4>>);
static_assert(
std::is_same_v<decltype(tf::read_obj<int, tf::dynamic_size, double>(
std::string_view{})),
tf::polygons_buffer<int, double, 3, tf::dynamic_size>>);
oracle_output output(output_path);
if (!output)
return false;
const auto triangles = make_fixed_obj(24000, 3, "\n", false, "/7/11");
const auto normal_triangles = make_fixed_obj(24000, 3, "\n", true, "//11");
const auto quads = make_fixed_obj(18000, 4, "\r\n", false, "");
for (const auto concurrency : {1, 2, 4}) {
auto polygons = in_arena(concurrency, [&] {
return tf::read_obj<int, 3, float>(const_range(triangles));
});
if (!valid_fixed(polygons, 3, 24000))
return false;
write(output, polygons);
if (!write_fixed_append<int, float, 3, 3>(output, normal_triangles,
concurrency, true, 4, 24001))
return false;
}
if (!write_cancelled_fixed_append<int, float, 3, 3>(output, triangles, 3,
24000))
return false;
auto quad_polygons =
tf::read_obj<std::int64_t, 4, double>(const_range(quads));
if (!valid_fixed(quad_polygons, 4, 18000))
return false;
write(output, quad_polygons);
if (!write_fixed_append<std::int64_t, double, 2, 4>(output, quads, 4, true, 5,
18001))
return false;
auto mutable_triangles =
std::vector<char>(triangles.begin(), triangles.end());
auto mutable_polygons = tf::read_obj<3>(
tf::make_range(mutable_triangles.data(), mutable_triangles.size()));
if (!valid_fixed(mutable_polygons, 3, 24000))
return false;
write(output, mutable_polygons);
const auto mixed = make_mixed_obj();
auto mixed_polygons = tf::read_obj<int, double>(const_range(mixed));
if (!valid_dynamic(mixed_polygons, 5, 2, 7))
return false;
write(output, mixed_polygons);
auto explicit_dynamic =
tf::read_obj<int, tf::dynamic_size, double>(const_range(mixed));
if (!valid_dynamic(explicit_dynamic, 5, 2, 7))
return false;
write(output, explicit_dynamic);
if (!write_dynamic_append<std::int64_t, double, 2>(output, mixed, true, 6, 3,
10))
return false;
const auto complete = make_complete_obj("f 1/1/1 3/3/1 2/2/1\n");
const auto complete_normals = std::string("v 0 0 0\nv 1 0 0\nv 0 1 0\n") +
"vn 0 0 1\nf 1//1 2//1 3//1\n";
auto complete_file = tf::read_obj(const_range(complete), tf::complete);
if (!valid_complete(complete_file, 3, 2, true, true, true))
return false;
write(output, complete_file);
auto normals_file = tf::read_obj<std::int64_t, double>(
const_range(complete_normals), tf::complete);
if (!valid_complete(normals_file, 3, 1, false, true, false))
return false;
write(output, normals_file);
auto mutable_complete = std::vector<char>(complete.begin(), complete.end());
auto mutable_complete_file = tf::read_obj(
tf::make_range(mutable_complete.data(), mutable_complete.size()),
tf::complete);
if (!valid_complete(mutable_complete_file, 3, 2, true, true, true))
return false;
write(output, mutable_complete_file);
const auto malformed_fixed = triangles + "f 1 2 3 1\n";
if (!write_fixed_append<int, float, 3, 3>(output, malformed_fixed, 4, false,
0, 0))
return false;
const auto malformed_dynamic = mixed + "f 1 bad 3\n";
if (!write_dynamic_append<int, float, 3>(output, malformed_dynamic, false, 6,
3, 11))
return false;
auto empty_dynamic = tf::read_obj(const_range(std::string{}));
if (empty_dynamic.points_buffer().size() != 0 ||
empty_dynamic.faces_buffer().offsets_buffer().size() != 1 ||
empty_dynamic.faces_buffer().data_buffer().size() != 0)
return false;
write(output, empty_dynamic);
auto empty_fixed = tf::read_obj<3>(const_range(std::string{}));
if (!valid_fixed(empty_fixed, 0, 0))
return false;
write(output, empty_fixed);
auto empty_complete = tf::read_obj(const_range(std::string{}), tf::complete);
if (!valid_complete(empty_complete, 0, 0, false, false, false))
return false;
write(output, empty_complete);
const auto fixture_path = output_path.string() + ".fixture.obj";
if (!ensure_absent(fixture_path) || !write_fixture(fixture_path, triangles))
return false;
auto fixed_from_file = tf::read_obj<int, 3, float>(fixture_path);
auto dynamic_from_file = tf::read_obj(fixture_path);
if (!valid_fixed(fixed_from_file, 3, 24000) ||
!valid_dynamic(dynamic_from_file, 3, 24000, 72000))
return false;
write(output, fixed_from_file);
write(output, dynamic_from_file);
if (!remove_file(fixture_path))
return false;
const auto complete_fixture_path = output_path.string() + ".complete.obj";
if (!ensure_absent(complete_fixture_path) ||
!write_fixture(complete_fixture_path, complete))
return false;
auto complete_from_file = tf::read_obj(complete_fixture_path, tf::complete);
if (!valid_complete(complete_from_file, 3, 2, true, true, true))
return false;
write(output, complete_from_file);
if (!remove_file(complete_fixture_path))
return false;
const auto missing_path = output_path.string() + ".missing.obj";
if (!ensure_absent(missing_path))
return false;
auto missing_fixed = tf::read_obj<3>(missing_path);
auto missing_dynamic = tf::read_obj(missing_path);
auto missing_complete = tf::read_obj(missing_path, tf::complete);
if (!valid_fixed(missing_fixed, 0, 0) ||
missing_dynamic.points_buffer().size() != 0 ||
missing_dynamic.faces_buffer().offsets_buffer().size() != 0 ||
missing_dynamic.faces_buffer().data_buffer().size() != 0 ||
!valid_complete(missing_complete, 0, 0, false, false, false))
return false;
write(output, missing_fixed);
write(output, missing_dynamic);
write(output, missing_complete);
if (!ensure_absent(missing_path))
return false;
if (corrupt)
output.scalar(std::uint8_t{0x5a});
return output.finish();
}
} // namespace
int main(int argc, char **argv) {
if (argc < 2 || argc > 3)
return 2;
const auto corrupt = argc == 3;
if (corrupt && std::string_view(argv[2]) != "--corrupt")
return 2;
return run(argv[1], corrupt) ? 0 : 1;
}