-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_write_utils.h
574 lines (490 loc) · 20.7 KB
/
read_write_utils.h
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Copyright(C) 2018 Tommy Hinks <tommy.hinks@gmail.com>
// This file is subject to the license terms in the LICENSE file
// found in the top-level directory of this distribution.
#pragma once
#include <array>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>
#include "thinks/obj_io/obj_io.h"
#include "mesh_types.h"
struct WriteResult {
thinks::ObjWriteResult write_result;
std::string mesh_str;
};
template <typename MeshT>
struct ReadResult {
thinks::ObjReadResult read_result;
MeshT mesh;
};
namespace read_write_utils_internal {
template <typename ObjT>
struct ObjTypeMaker;
template <typename FloatT>
struct ObjTypeMaker<thinks::ObjPosition<FloatT, 3>> {
template <typename VecFloatT>
static constexpr thinks::ObjPosition<FloatT, 3> Make(
const Vec3<VecFloatT>& v) noexcept {
return {v.x, v.y, v.z};
}
};
template <typename FloatT>
struct ObjTypeMaker<thinks::ObjPosition<FloatT, 4>> {
template <typename VecFloatT>
static constexpr thinks::ObjPosition<FloatT, 4> Make(
const Vec4<VecFloatT>& v) noexcept {
return {v.x, v.y, v.z, v.w};
}
};
template <typename FloatT>
struct ObjTypeMaker<thinks::ObjTexCoord<FloatT, 2>> {
template <typename VecFloatT>
static constexpr thinks::ObjTexCoord<FloatT, 2> Make(
const Vec2<VecFloatT>& v) noexcept {
return {v.x, v.y};
}
};
template <typename FloatT>
struct ObjTypeMaker<thinks::ObjTexCoord<FloatT, 3>> {
template <typename VecFloatT>
static constexpr thinks::ObjTexCoord<FloatT, 3> Make(
const Vec3<VecFloatT>& v) noexcept {
return {v.x, v.y, v.z};
}
};
template <typename FloatT>
struct ObjTypeMaker<thinks::ObjNormal<FloatT>> {
template <typename VecFloatT>
static constexpr thinks::ObjNormal<FloatT> Make(
const Vec3<VecFloatT>& v) noexcept {
return {v.x, v.y, v.z};
}
};
template <typename IndexT>
struct ObjTypeMaker<thinks::ObjTriangleFace<IndexT>> {
static constexpr thinks::ObjTriangleFace<IndexT> Make(
const std::array<IndexT, 3>& a) noexcept {
return {a[0], a[1], a[2]};
}
};
template <typename IndexT>
struct ObjTypeMaker<thinks::ObjQuadFace<IndexT>> {
static constexpr thinks::ObjQuadFace<IndexT> Make(
const std::array<IndexT, 4>& a) noexcept {
return {a[0], a[1], a[2], a[3]};
}
};
template <typename IndexT>
struct ObjTypeMaker<thinks::ObjPolygonFace<IndexT>> {
template <std::size_t N>
static constexpr thinks::ObjPolygonFace<IndexT> Make(
const std::array<IndexT, N>& a) noexcept {
auto face = thinks::ObjPolygonFace<IndexT>{};
// Heap allocation!
face.values.resize(std::tuple_size<std::array<IndexT, N>>::value);
for (std::size_t i = 0; i < face.values.size(); ++i) {
face.values[i] = a[i];
}
return face;
}
};
template <std::size_t IndicesPerFaceT, typename IndexT>
struct FaceSelector {
using Type = thinks::ObjPolygonFace<IndexT>;
};
template <typename IndexT>
struct FaceSelector<3, IndexT> {
using Type = thinks::ObjTriangleFace<IndexT>;
};
template <typename IndexT>
struct FaceSelector<4, IndexT> {
using Type = thinks::ObjQuadFace<IndexT>;
};
template <typename AddPositionFuncT, typename AddFaceFuncT,
typename AddTexCoordFuncT, typename AddNormalFuncT>
thinks::ObjReadResult ReadHelper(
std::istream& is, AddPositionFuncT&& add_position, AddFaceFuncT&& add_face,
AddTexCoordFuncT&& add_tex_coord, AddNormalFuncT&& add_normal,
const bool read_tex_coords, const bool read_normals) {
using thinks::ReadObj;
auto result = thinks::ObjReadResult{};
if (!read_tex_coords && !read_normals) {
result = ReadObj(is, std::forward<AddPositionFuncT>(add_position),
std::forward<AddFaceFuncT>(add_face));
} else if (read_tex_coords && !read_normals) {
result = ReadObj(is, std::forward<AddPositionFuncT>(add_position),
std::forward<AddFaceFuncT>(add_face), add_tex_coord);
} else if (!read_tex_coords && read_normals) {
result = ReadObj(is, std::forward<AddPositionFuncT>(add_position),
std::forward<AddFaceFuncT>(add_face),
nullptr /* add_tex_coord */,
std::forward<AddNormalFuncT>(add_normal));
} else {
result = ReadObj(is, std::forward<AddPositionFuncT>(add_position),
std::forward<AddFaceFuncT>(add_face),
std::forward<AddTexCoordFuncT>(add_tex_coord),
std::forward<AddNormalFuncT>(add_normal));
}
return result;
}
template <typename PosMapperT, typename FaceMapperT, typename TexMapperT,
typename NmlMapperT>
WriteResult WriteHelper(PosMapperT&& pos_mapper, FaceMapperT&& face_mapper,
TexMapperT&& tex_mapper, NmlMapperT&& nml_mapper,
const bool write_tex_coords, const bool write_normals) {
using thinks::WriteObj;
auto result = thinks::ObjWriteResult{};
auto oss = std::ostringstream{};
if (!write_tex_coords && !write_normals) {
result = WriteObj(oss, std::forward<PosMapperT>(pos_mapper),
std::forward<FaceMapperT>(face_mapper));
} else if (write_tex_coords && !write_normals) {
result = WriteObj(oss, std::forward<PosMapperT>(pos_mapper),
std::forward<FaceMapperT>(face_mapper),
std::forward<TexMapperT>(tex_mapper));
} else if (!write_tex_coords && write_normals) {
result = WriteObj(oss, std::forward<PosMapperT>(pos_mapper),
std::forward<FaceMapperT>(face_mapper),
nullptr, // No texture coordinates!
std::forward<NmlMapperT>(nml_mapper));
} else {
result = WriteObj(oss, std::forward<PosMapperT>(pos_mapper),
std::forward<FaceMapperT>(face_mapper),
std::forward<TexMapperT>(tex_mapper),
std::forward<NmlMapperT>(nml_mapper));
}
return {result, oss.str()};
}
} // namespace read_write_utils_internal
template <typename MeshT>
ReadResult<MeshT> ReadMesh(std::istream& is, const bool read_tex_coords,
const bool read_normals) {
using thinks::MakeObjAddFunc;
using MeshType = MeshT;
using VertexType = MeshType::VertexType;
auto mesh = MeshType{};
auto pos_count = uint32_t{0};
auto tex_count = uint32_t{0};
auto nml_count = uint32_t{0};
// Positions.
using PositionType = VertexType::PositionType;
using ObjPositionType = thinks::ObjPosition<PositionType::ValueType,
VecSize<PositionType>::value>;
auto add_position =
MakeObjAddFunc<ObjPositionType>([&mesh, &pos_count](const auto& pos) {
if (mesh.vertices.size() <= pos_count) {
mesh.vertices.push_back(VertexType{});
}
mesh.vertices[pos_count++].pos =
VecMaker<PositionType>::Make(pos.values);
});
// Faces.
using ObjFaceType = read_write_utils_internal::FaceSelector<
MeshType::IndicesPerFace, thinks::ObjIndex<MeshType::IndexType>>::Type;
auto add_face = MakeObjAddFunc<ObjFaceType>([&mesh](const auto& face) {
if (face.values.size() != MeshType::IndicesPerFace) {
throw std::runtime_error("unexpected face index count");
}
for (const auto idx : face.values) {
mesh.indices.push_back(idx.value);
}
});
// Texture coordinates [optional].
using TexCoordType = VertexType::TexCoordType;
using ObjTexCoordType = thinks::ObjTexCoord<TexCoordType::ValueType,
VecSize<TexCoordType>::value>;
auto add_tex_coord =
MakeObjAddFunc<ObjTexCoordType>([&mesh, &tex_count](const auto& tex) {
if (mesh.vertices.size() <= tex_count) {
mesh.vertices.push_back(VertexType{});
}
mesh.vertices[tex_count++].tex =
VecMaker<TexCoordType>::Make(tex.values);
});
// Normals [optional].
using NormalType = VertexType::NormalType;
using ObjNormalType = thinks::ObjNormal<NormalType::ValueType>;
auto add_normal =
MakeObjAddFunc<ObjNormalType>([&mesh, &nml_count](const auto& nml) {
if (mesh.vertices.size() <= nml_count) {
mesh.vertices.push_back(VertexType{});
}
mesh.vertices[nml_count++].normal =
VecMaker<NormalType>::Make(nml.values);
});
const auto result = read_write_utils_internal::ReadHelper(
is, add_position, add_face, add_tex_coord, add_normal, read_tex_coords,
read_normals);
// Some sanity checks...
if (read_tex_coords && pos_count != tex_count) {
throw std::runtime_error("tex coord count must match position count");
}
if (read_normals && pos_count != nml_count) {
throw std::runtime_error("normal count must match position count");
}
if (result.position_count != mesh.vertices.size()) {
throw std::runtime_error("bad position count");
}
if (read_tex_coords && result.tex_coord_count != mesh.vertices.size()) {
throw std::runtime_error("bad tex coord count");
}
if (read_normals && result.normal_count != mesh.vertices.size()) {
throw std::runtime_error("bad normal count");
}
if (result.face_count != mesh.indices.size() / MeshType::IndicesPerFace) {
throw std::runtime_error("bad face count");
}
return {result, mesh};
}
template <typename IndexedMeshT>
ReadResult<IndexedMeshT> ReadIndexGroupMesh(std::istream& is,
const bool read_tex_coords,
const bool read_normals) {
using thinks::MakeObjAddFunc;
using MeshType = IndexedMeshT;
auto mesh = MeshType{};
// Positions.
using PositionType = MeshType::PositionType;
using ObjPositionType = thinks::ObjPosition<PositionType::ValueType,
VecSize<PositionType>::value>;
auto add_position = MakeObjAddFunc<ObjPositionType>([&mesh](const auto& pos) {
mesh.positions.push_back(VecMaker<PositionType>::Make(pos.values));
});
// Faces.
using ObjFaceType = read_write_utils_internal::FaceSelector<
MeshType::IndicesPerFace,
thinks::ObjIndexGroup<MeshType::IndexType>>::Type;
auto add_face = MakeObjAddFunc<ObjFaceType>(
[&mesh, read_tex_coords, read_normals](const auto& face) {
if (face.values.size() != MeshType::IndicesPerFace) {
throw std::runtime_error("unexpected face index count");
}
for (const auto idx : face.values) {
mesh.position_indices.push_back(idx.position_index.value);
if (read_tex_coords && idx.tex_coord_index.second) {
mesh.tex_coord_indices.push_back(idx.tex_coord_index.first.value);
}
if (read_normals && idx.normal_index.second) {
mesh.normal_indices.push_back(idx.normal_index.first.value);
}
}
});
// Texture coordinates [optional.
using TexCoordType = MeshType::TexCoordType;
using ObjTexCoordType = thinks::ObjTexCoord<TexCoordType::ValueType,
VecSize<TexCoordType>::value>;
auto add_tex_coord =
MakeObjAddFunc<ObjTexCoordType>([&mesh](const auto& tex) {
mesh.tex_coords.push_back(VecMaker<TexCoordType>::Make(tex.values));
});
// Normals [optional].
using NormalType = MeshType::NormalType;
using ObjNormalType = thinks::ObjNormal<NormalType::ValueType>;
auto add_normal = MakeObjAddFunc<ObjNormalType>([&mesh](const auto& nml) {
mesh.normals.push_back(VecMaker<NormalType>::Make(nml.values));
});
const auto result = read_write_utils_internal::ReadHelper(
is, add_position, add_face, add_tex_coord, add_normal, read_tex_coords,
read_normals);
// Some sanity checks...
if (result.position_count != mesh.positions.size()) {
throw std::runtime_error("bad position count");
}
if (read_tex_coords && result.tex_coord_count != mesh.tex_coords.size()) {
throw std::runtime_error("bad tex coord count");
}
if (read_normals && result.normal_count != mesh.normals.size()) {
throw std::runtime_error("bad normal count");
}
if (result.face_count !=
mesh.position_indices.size() / MeshType::IndicesPerFace) {
throw std::runtime_error("bad face count");
}
if (read_tex_coords && result.face_count != mesh.tex_coord_indices.size() /
MeshType::IndicesPerFace) {
throw std::runtime_error("bad face count");
}
if (read_normals && result.face_count != mesh.normal_indices.size() /
MeshType::IndicesPerFace) {
throw std::runtime_error("bad face count");
}
return {result, mesh};
}
template <typename MeshT>
WriteResult WriteMesh(const MeshT& mesh, const bool write_tex_coords,
const bool write_normals) {
using thinks::ObjEnd;
using thinks::ObjMap;
using read_write_utils_internal::FaceSelector;
using read_write_utils_internal::ObjTypeMaker;
using read_write_utils_internal::WriteHelper;
using MeshType = MeshT;
using VertexType = MeshType::VertexType;
const auto vtx_iend = std::end(mesh.vertices);
// Positions.
auto pos_vtx_iter = begin(mesh.vertices);
auto pos_mapper = [&pos_vtx_iter, vtx_iend]() {
using PositionType = VertexType::PositionType;
using ObjPositionType = thinks::ObjPosition<PositionType::ValueType,
VecSize<PositionType>::value>;
return pos_vtx_iter == vtx_iend
? ObjEnd<ObjPositionType>()
: ObjMap(ObjTypeMaker<ObjPositionType>::Make(
(*pos_vtx_iter++).pos));
};
// Texture coordinates.
auto tex_vtx_iter = begin(mesh.vertices);
auto tex_mapper = [&tex_vtx_iter, vtx_iend]() {
using TexCoordType = VertexType::TexCoordType;
using ObjTexCoordType = thinks::ObjTexCoord<TexCoordType::ValueType,
VecSize<TexCoordType>::value>;
return tex_vtx_iter == vtx_iend
? ObjEnd<ObjTexCoordType>()
: ObjMap(ObjTypeMaker<ObjTexCoordType>::Make(
(*tex_vtx_iter++).tex));
};
// Normals.
auto nml_vtx_iter = begin(mesh.vertices);
auto nml_mapper = [&nml_vtx_iter, vtx_iend]() {
using NormalType = VertexType::NormalType;
using ObjNormalType = thinks::ObjNormal<NormalType::ValueType>;
return nml_vtx_iter == vtx_iend ? ObjEnd<ObjNormalType>()
: ObjMap(ObjTypeMaker<ObjNormalType>::Make(
(*nml_vtx_iter++).normal));
};
// Faces.
auto idx_iter = mesh.indices.begin();
const auto idx_iend = mesh.indices.end();
auto face_mapper = [&idx_iter, idx_iend]() {
using MeshIndexType = MeshType::IndexType;
using ObjIndexType = thinks::ObjIndex<MeshIndexType>;
using ObjFaceType =
FaceSelector<MeshType::IndicesPerFace, ObjIndexType>::Type;
if (std::distance(idx_iter, idx_iend) < MeshType::IndicesPerFace) {
return ObjEnd<ObjFaceType>();
}
auto idx_buf = std::array<ObjIndexType, MeshType::IndicesPerFace>{};
for (auto& idx : idx_buf) {
idx = ObjIndexType(*idx_iter++);
}
return ObjMap(ObjTypeMaker<ObjFaceType>::Make(idx_buf));
};
const auto result = read_write_utils_internal::WriteHelper(
pos_mapper, face_mapper, tex_mapper, nml_mapper, write_tex_coords,
write_normals);
const auto wr = result.write_result;
// Some sanity checks...
if (wr.position_count != mesh.vertices.size()) {
throw std::runtime_error("bad position count");
}
if (write_tex_coords && wr.tex_coord_count != mesh.vertices.size()) {
throw std::runtime_error("bad tex coord count");
}
if (write_normals && wr.normal_count != mesh.vertices.size()) {
throw std::runtime_error("bad normal count");
}
if (wr.face_count != mesh.indices.size() / MeshType::IndicesPerFace) {
throw std::runtime_error("bad index count");
}
return result;
}
template <typename IndexedMeshT>
WriteResult WriteIndexGroupMesh(const IndexedMeshT& imesh,
const bool write_tex_coords,
const bool write_normals) {
using thinks::ObjEnd;
using thinks::ObjMap;
using read_write_utils_internal::FaceSelector;
using read_write_utils_internal::ObjTypeMaker;
using read_write_utils_internal::WriteHelper;
using MeshType = IndexedMeshT;
// Positions.
auto pos_iter = std::begin(imesh.positions);
const auto pos_iend = std::end(imesh.positions);
auto pos_mapper = [&pos_iter, pos_iend]() {
using PositionType = MeshType::PositionType;
using ObjPositionType = thinks::ObjPosition<PositionType::ValueType,
VecSize<PositionType>::value>;
return pos_iter == pos_iend
? ObjEnd<ObjPositionType>()
: ObjMap(ObjTypeMaker<ObjPositionType>::Make(*pos_iter++));
};
// Texture coordinates.
auto tex_iter = std::begin(imesh.tex_coords);
const auto tex_iend = std::end(imesh.tex_coords);
auto tex_mapper = [&tex_iter, tex_iend]() {
using TexCoordType = MeshType::TexCoordType;
using ObjTexCoordType = thinks::ObjTexCoord<TexCoordType::ValueType,
VecSize<TexCoordType>::value>;
return tex_iter == tex_iend
? ObjEnd<ObjTexCoordType>()
: ObjMap(ObjTypeMaker<ObjTexCoordType>::Make(*tex_iter++));
};
// Normals.
auto nml_iter = std::begin(imesh.normals);
const auto nml_iend = std::end(imesh.normals);
auto nml_mapper = [&nml_iter, nml_iend]() {
using NormalType = MeshType::NormalType;
using ObjNormalType = thinks::ObjNormal<NormalType::ValueType>;
return nml_iter == nml_iend
? ObjEnd<ObjNormalType>()
: ObjMap(ObjTypeMaker<ObjNormalType>::Make(*nml_iter++));
};
// Faces.
auto pos_idx_iter = std::begin(imesh.position_indices);
auto pos_idx_iend = std::end(imesh.position_indices);
auto tex_idx_iter = std::begin(imesh.tex_coord_indices);
auto tex_idx_iend = std::end(imesh.tex_coord_indices);
auto nml_idx_iter = std::begin(imesh.normal_indices);
auto nml_idx_iend = std::end(imesh.normal_indices);
auto face_mapper = [&pos_idx_iter, &tex_idx_iter, &nml_idx_iter, pos_idx_iend,
tex_idx_iend, nml_idx_iend, write_tex_coords,
write_normals]() {
using MeshIndexType = MeshType::IndexType;
using ObjIndexGroupType = thinks::ObjIndexGroup<MeshIndexType>;
using ObjFaceType =
FaceSelector<MeshType::IndicesPerFace, ObjIndexGroupType>::Type;
if (std::distance(pos_idx_iter, pos_idx_iend) < MeshType::IndicesPerFace ||
std::distance(tex_idx_iter, tex_idx_iend) < MeshType::IndicesPerFace ||
std::distance(nml_idx_iter, nml_idx_iend) < MeshType::IndicesPerFace) {
return ObjEnd<ObjFaceType>();
}
auto idx_group_buf =
std::array<ObjIndexGroupType, MeshType::IndicesPerFace>{};
for (auto& idx_group : idx_group_buf) {
idx_group =
ObjIndexGroupType(*pos_idx_iter++, *tex_idx_iter++, *nml_idx_iter++);
idx_group.tex_coord_index.second = write_tex_coords;
idx_group.normal_index.second = write_normals;
}
return ObjMap(ObjTypeMaker<ObjFaceType>::Make(idx_group_buf));
};
const auto result = read_write_utils_internal::WriteHelper(
pos_mapper, face_mapper, tex_mapper, nml_mapper, write_tex_coords,
write_normals);
const auto wr = result.write_result;
// Some sanity checks...
if (wr.position_count != imesh.positions.size()) {
throw std::runtime_error("bad position count");
}
if (write_tex_coords && wr.tex_coord_count != imesh.tex_coords.size()) {
throw std::runtime_error("bad tex coord count");
}
if (write_normals && wr.normal_count != imesh.normals.size()) {
throw std::runtime_error("bad normal count");
}
if (wr.face_count !=
imesh.position_indices.size() / MeshType::IndicesPerFace) {
throw std::runtime_error("bad position index count");
}
if (write_tex_coords && wr.face_count != imesh.tex_coord_indices.size() /
MeshType::IndicesPerFace) {
throw std::runtime_error("bad tex coord index count");
}
if (write_normals && wr.face_count != imesh.position_indices.size() /
MeshType::IndicesPerFace) {
throw std::runtime_error("bad normal index count");
}
return result;
}