-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathac3d.h
1105 lines (1002 loc) · 37.5 KB
/
ac3d.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* acclint - A tool that detects errors in AC3D files.
* Copyright (C) 2020-2024 Robert Reif
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
#ifndef _AC3D_H_
#define _AC3D_H_
#define _USE_MATH_DEFINES
#include <array>
#include <set>
#include <map>
#include <vector>
#include <cmath>
#include <regex>
#include <chrono>
class AC3D
{
#define CHECK(func, var, state) \
public: \
void func(bool value) { var = value; } \
bool func() const { return var; } \
size_t func##Count() const { return var##_count; } \
private: \
bool var = state; \
size_t var##_count = 0;
// warnings
CHECK(trailingText, m_trailing_text, true)
CHECK(blankLine, m_blank_line, false)
CHECK(duplicateVertices, m_duplicate_vertices, true)
CHECK(unusedVertex, m_unused_vertex, true)
CHECK(invalidNormalLength, m_invalid_normal_length, true)
CHECK(invalidMaterial, m_invalid_material, true)
CHECK(invalidRefCount, m_invalid_ref_count, true)
CHECK(extraUVCoordinates, m_extra_uv_coordinates, true)
CHECK(duplicateMaterials, m_duplicate_materials, true)
CHECK(unusedMaterial, m_unused_material, true)
CHECK(missingSurfaces, m_missing_surfaces, true)
CHECK(duplicateSurfaces, m_duplicate_surfaces, true)
CHECK(duplicateSurfacesOrder, m_duplicate_surfaces_order, true)
CHECK(duplicateSurfacesWinding, m_duplicate_surfaces_winding, true)
CHECK(duplicateSurfaceVertices, m_duplicate_surface_vertices, true)
CHECK(collinearSurfaceVertices, m_collinear_surface_vertices, true)
CHECK(multiplePolygonSurface, m_multiple_polygon_surface, true)
CHECK(surfaceSelfIntersecting, m_surface_self_intersecting, true)
CHECK(surfaceNotCoplanar, m_surface_not_coplanar, true)
CHECK(surfaceNotConvex, m_surface_not_convex, true)
CHECK(surfaceNoTexture, m_surface_no_texture, true)
CHECK(surfaceStripHole, m_surface_strip_hole, false)
CHECK(surfaceStripSize, m_surface_strip_size, false)
CHECK(surfaceStripDegenerate, m_surface_strip_degenerate, false)
CHECK(surfaceStripDuplicateTriangles, m_surface_strip_duplicate_triangles, false)
CHECK(surface2SidedOpaque, m_surface_2_sided_opaque, false)
CHECK(duplicateTriangles, m_duplicate_triangles, false)
CHECK(floatingPoint, m_floating_point, true)
CHECK(emptyObject, m_empty_object, true)
CHECK(extraObject, m_extra_object, true)
CHECK(missingKids, m_missing_kids, true)
CHECK(missingTexture, m_missing_texture, true)
CHECK(duplicateTexture, m_duplicate_texture, true)
CHECK(ambiguousTexture, m_ambiguous_texture, true)
CHECK(multipleCrease, m_multiple_crease, true)
CHECK(multipleFolded, m_multiple_folded, true)
CHECK(multipleHidden, m_multiple_hidden, true)
CHECK(multipleLoc, m_multiple_loc, true)
CHECK(multipleLocked, m_multiple_locked, true)
CHECK(multipleName, m_multiple_name, true)
CHECK(multipleRot, m_multiple_rot, true)
CHECK(multipleSubdiv, m_multiple_subdiv, true)
CHECK(multipleTexoff, m_multiple_texoff, true)
CHECK(multipleTexrep, m_multiple_texrep, true)
CHECK(multipleTexture, m_multiple_texture, true)
CHECK(differentUV, m_different_uv, true)
CHECK(groupWithGeometry, m_group_with_geometry, true)
CHECK(multipleWorld, m_multiple_world, true)
CHECK(differentSURF, m_different_surf, true)
CHECK(differentMat, m_different_mat, true)
CHECK(overlapping2SidedSurface, m_overlapping_2_sided_surface, true)
CHECK(missingNormal, m_missing_normal, true)
CHECK(missingUVCoordinates, m_missing_uv_coordinates, true)
// errors
CHECK(invalidNormal, m_invalid_normal, true)
CHECK(invalidVertexIndex, m_invalid_vertex_index, true)
CHECK(invalidTextureCoordinate, m_invalid_texture_coordinate, true)
CHECK(invalidMaterialIndex, m_invalid_material_index, true)
CHECK(invalidSurfaceType, m_invalid_surface_type, true)
CHECK(invalidToken, m_invalid_token, true)
#undef CHECK
void showLine(std::istringstream &in) const;
void showLine(const std::istringstream &in, const std::streampos &pos) const;
void showLine(std::istream &in, const std::streampos &pos, int offset = 0) const;
public:
enum class DumpType { group, poly, surf};
struct RemoveInfo
{
RemoveInfo(const std::string &type, const std::string &expression)
: object_type(type), regular_expression(expression)
{
}
std::string object_type;
std::regex regular_expression;
};
bool read(const std::string &file);
bool write(const std::string &file, int version = 0);
void dump(DumpType dump_type) const;
size_t warnings() const
{
return m_warnings;
}
size_t errors() const
{
return m_errors;
}
void crlf(bool value)
{
m_crlf = value;
}
bool crlf() const
{
return m_crlf;
}
void notAC3DFile(bool value)
{
m_not_ac3d_file = value;
}
bool notAC3DFile() const
{
return m_not_ac3d_file;
}
void texturePaths(const std::vector<std::string> &paths)
{
m_texture_paths = paths;
}
size_t getWorldKidCount() const
{
return m_objects[0].kids.size();
}
size_t getWorldKidCount(size_t kid) const
{
if (!m_objects.empty() && m_objects[0].kids.size() > kid)
return m_objects[0].kids[kid].kids.size();
return 0;
}
void showTimes(bool value)
{
m_show_times = value;
}
bool showTimes() const
{
return m_show_times;
}
void threads(unsigned int value)
{
m_threads = value;
}
unsigned int threads() const
{
return m_threads;
}
void quite(bool value)
{
m_quite = value;
}
bool quite() const
{
return m_quite;
}
void summary(bool value)
{
m_summary = value;
}
bool summary() const
{
return m_summary;
}
bool clean();
bool cleanObjects();
bool cleanVertices();
bool cleanSurfaces();
bool cleanMaterials();
bool fixMultipleWorlds();
bool splitMultipleSURF();
bool splitMultipleMat();
bool merge(const AC3D& ac3d);
void flatten();
bool splitPolygons();
void removeObjects(const RemoveInfo &remove_info);
void combineTexture();
void fixOverlapping2SidedSurface();
void fixSurface2SidedOpaque();
static std::string getDuration(const std::chrono::time_point<std::chrono::system_clock> &start,
const std::chrono::time_point<std::chrono::system_clock> &end);
static std::string getTime(const std::chrono::time_point<std::chrono::system_clock> &time);
class quoted_string : public std::string
{
friend std::ostream & operator << (std::ostream &out, const quoted_string &s);
friend std::istream & operator >> (std::istream &in, quoted_string &s);
};
private:
class Color : public std::array<double,3>
{
public:
bool clip()
{
bool clipped = false;
for (auto &value : *this)
{
if (value < 0.0)
{
value = 0.0;
clipped = true;
}
else if (value > 1.0)
{
value = 1.0;
clipped = true;
}
}
return clipped;
}
};
class Point2 : public std::array<double,2>
{
public:
double x() const { return (*this)[0]; }
double y() const { return (*this)[1]; }
void x(double value) { (*this)[0] = value; }
void y(double value) { (*this)[1] = value; }
Point2 operator - (const Point2 &other) const
{
return Point2 { x() - other.x(), y() - other.y() };
}
double dot(const Point2 &other) const
{
return x() * other.x() + y() * other.y();
}
double cross(const Point2 &other) const
{
return (x() * other.y()) - (y() * other.x());
}
};
class Point3 : public std::array<double,3>
{
public:
double x() const { return (*this)[0]; }
double y() const { return (*this)[1]; }
double z() const { return (*this)[2]; }
void x(double value) { (*this)[0] = value; }
void y(double value) { (*this)[1] = value; }
void z(double value) { (*this)[2] = value; }
Point3 operator + (const Point3 &other) const
{
return Point3 { x() + other.x(),
y() + other.y(),
z() + other.z() };
}
Point3 operator - (const Point3 &other) const
{
return Point3 { x() - other.x(),
y() - other.y(),
z() - other.z() };
}
Point3 operator * (double value) const
{
return Point3 { x() * value, y() * value, z() * value };
}
Point3 operator - () const
{
return Point3{ -x(), -y(), -z() };
}
Point3 &operator += (const Point3 &other)
{
x(x() + other.x());
y(y() + other.y());
z(z() + other.z());
return *this;
}
double dot(const Point3 &other) const
{
return x() * other.x() + y() * other.y() + z() * other.z();
}
Point3 cross(const Point3 &other) const
{
return Point3 { y() * other.z() - z() * other.y(),
z() * other.x() - x() * other.z(),
x() * other.y() - y() * other.x() };
}
double length() const
{
return std::sqrt(x() * x() + y() * y() + z() * z());
}
void normalize()
{
const double l = length();
if (l != 0.0)
{
x(x() / l);
y(y() / l);
z(z() / l);
}
}
double angleRadians(const Point3 &other) const
{
return acos(dot(other) / (length() * other.length()));
}
double angleDegrees(const Point3 &other) const
{
return angleRadians(other) * 180.0 / M_PI;
}
bool equals(const Point3 &other) const
{
static constexpr double SMALL_NUM = static_cast<double>(std::numeric_limits<double>::epsilon());
return std::abs(x() - other.x()) < SMALL_NUM &&
std::abs(y() - other.y()) < SMALL_NUM &&
std::abs(z() - other.z()) < SMALL_NUM;
}
};
class Matrix : public std::array<std::array<double, 4>, 4>
{
public:
Matrix() // identity matrix
{
(*this)[0][0] = 1; (*this)[0][1] = 0; (*this)[0][2] = 0; (*this)[0][3] = 0;
(*this)[1][0] = 0; (*this)[1][1] = 1; (*this)[1][2] = 0; (*this)[1][3] = 0;
(*this)[2][0] = 0; (*this)[2][1] = 0; (*this)[2][2] = 1; (*this)[2][3] = 0;
(*this)[3][0] = 0; (*this)[3][1] = 0; (*this)[3][2] = 0; (*this)[3][3] = 1;
}
Matrix(double m0, double m1, double m2, double m3,
double m4, double m5, double m6, double m7,
double m8, double m9, double m10, double m11,
double m12, double m13, double m14, double m15)
{
(*this)[0][0] = m0; (*this)[0][1] = m1; (*this)[0][2] = m2; (*this)[0][3] = m3;
(*this)[1][0] = m4; (*this)[1][1] = m5; (*this)[1][2] = m6; (*this)[1][3] = m7;
(*this)[2][0] = m8; (*this)[2][1] = m9; (*this)[2][2] = m10; (*this)[2][3] = m11;
(*this)[3][0] = m12; (*this)[3][1] = m13; (*this)[3][2] = m14; (*this)[3][3] = m15;
}
void setLocation(const Point3 &location)
{
(*this)[3][0] = location[0];
(*this)[3][1] = location[1];
(*this)[3][2] = location[2];
}
void setRotation(const std::array<double, 9> &rotation)
{
(*this)[0][0] = rotation[0]; (*this)[0][1] = rotation[1]; (*this)[0][2] = rotation[2];
(*this)[1][0] = rotation[3]; (*this)[1][1] = rotation[4]; (*this)[1][2] = rotation[5];
(*this)[2][0] = rotation[6]; (*this)[2][1] = rotation[7]; (*this)[2][2] = rotation[8];
}
void transformPoint(Point3 &point) const
{
Point3 dst;
double t0 = point[0];
double t1 = point[1];
double t2 = point[2];
dst[0] = t0 * (*this)[0][0] + t1 * (*this)[1][0] + t2 * (*this)[2][0] + (*this)[3][0];
dst[1] = t0 * (*this)[0][1] + t1 * (*this)[1][1] + t2 * (*this)[2][1] + (*this)[3][1];
dst[2] = t0 * (*this)[0][2] + t1 * (*this)[1][2] + t2 * (*this)[2][2] + (*this)[3][2];
point = dst;
}
Matrix multiply(const Matrix &matrix)
{
Matrix result;
for (int j = 0; j < 4; j++)
{
result[0][j] = matrix[0][0] * (*this)[0][j] +
matrix[0][1] * (*this)[1][j] +
matrix[0][2] * (*this)[2][j] +
matrix[0][3] * (*this)[3][j];
result[1][j] = matrix[1][0] * (*this)[0][j] +
matrix[1][1] * (*this)[1][j] +
matrix[1][2] * (*this)[2][j] +
matrix[1][3] * (*this)[3][j];
result[2][j] = matrix[2][0] * (*this)[0][j] +
matrix[2][1] * (*this)[1][j] +
matrix[2][2] * (*this)[2][j] +
matrix[2][3] * (*this)[3][j];
result[3][j] = matrix[3][0] * (*this)[0][j] +
matrix[3][1] * (*this)[1][j] +
matrix[3][2] * (*this)[2][j] +
matrix[3][3] * (*this)[3][j];
}
return result;
}
void transformNormal(Point3 &normal) const
{
Point3 dst;
double t0 = normal[0];
double t1 = normal[1];
double t2 = normal[2];
dst[0] = t0 * (*this)[0][0] + t1 * (*this)[1][0] + t2 * (*this)[2][0];
dst[1] = t0 * (*this)[0][1] + t1 * (*this)[1][1] + t2 * (*this)[2][1];
dst[2] = t0 * (*this)[0][2] + t1 * (*this)[1][2] + t2 * (*this)[2][2];
normal = dst;
}
};
struct Header
{
std::string version = "AC3Db";
int getVersion() const
{
return version[4] == 'b' ? 11 : version[4] == 'c' ? 12 : 11;
}
};
struct LineInfo
{
LineInfo() = default;
LineInfo(size_t number, const std::streampos &pos) : line_number(number), line_pos(pos) { }
size_t line_number = 0;
std::streampos line_pos;
};
struct Data : public LineInfo
{
std::string data;
};
struct Material : public LineInfo
{
quoted_string name;
Color rgb = {0.0, 0.0, 0.0};
Color amb = {0.0, 0.0, 0.0};
Color emis = {0.0, 0.0, 0.0};
Color spec = {0.0, 0.0, 0.0};
double shi = 0.0;
double trans = 0.0;
std::vector<Data> data;
bool version12 = false;
bool used = false;
};
struct Texture : public LineInfo
{
quoted_string name;
std::string type;
std::string path;
};
struct TexRep : public LineInfo
{
Point2 texrep = {0.0, 0.0};
};
struct TexOff : public LineInfo
{
Point2 texoff = {0.0, 0.0};
};
struct SubDiv : public LineInfo
{
size_t subdiv = 0;
};
struct Ref : public LineInfo
{
size_t index = 0;
std::vector<Point2> coordinates;
bool duplicate = false;
bool collinear = false;
};
class Refs : public LineInfo, public std::vector<Ref>
{
public:
int declared_size = 0;
};
struct Mat : public LineInfo
{
size_t mat = 0;
explicit Mat(size_t index) : LineInfo(), mat(index) { };
Mat(size_t number, const std::streampos& pos, size_t index) : LineInfo(number, pos), mat(index) { }
};
struct Vertex : public LineInfo
{
Point3 vertex = { 0.0, 0.0, 0.0 };
Point3 normal = { 0.0, 0.0, 0.0 };
bool has_normal = false;
bool used = false;
bool operator == (const Vertex& other) const
{
if (!vertex.equals(other.vertex))
return false;
if (has_normal != other.has_normal)
return false;
if (has_normal && !normal.equals(other.normal))
return false;
return true;
}
};
enum Difference { None = 0, Order = 1, Winding = 2 };
struct Surface;
struct Object;
struct Triangle
{
std::array<Vertex,3> vertices;
std::array<Ref,3> refs;
Point3 normal = { 0.0, 0.0, 0.0 };
bool degenerate = false;
Triangle(const Vertex& v0, const Vertex& v1, const Vertex& v2, const Ref &r0, const Ref &r1, const Ref &r2)
{
vertices[0] = v0;
vertices[1] = v1;
vertices[2] = v2;
refs[0] = r0;
refs[1] = r1;
refs[2] = r2;
degenerate = AC3D::degenerate(v0.vertex, v1.vertex, v2.vertex);
if (!degenerate)
normal = AC3D::normalizedNormal(v0.vertex, v1.vertex, v2.vertex);
}
bool sameTriangle(const Triangle &triangle, Difference difference) const;
bool sameTriangle(const Object &object, const Surface &surface, Difference difference) const;
};
enum class WindingType { CCW, CW };
enum class PlaneType { xy, xz, yz };
struct Plane
{
double distance = 0;
Point3 normal = { 0.0, 0.0, 0.0 };
bool valid = false;
explicit Plane(const std::array<Point3, 3> &points)
{
if (!AC3D::degenerate(points))
{
normal = (points[1] - points[0]).cross(points[2] - points[0]);
normal.normalize();
distance = normal.dot(points[0]);
valid = true;
}
}
Plane(const Point3 &p0, const Point3 &p1, const Point3 &p2)
{
if (!AC3D::degenerate(p0, p1, p2))
{
normal = (p1 - p0).cross(p2 - p0);
normal.normalize();
distance = normal.dot(p0);
valid = true;
}
}
Plane(const Point3 &p0, const Point3 &p1, const Point3 &p2, WindingType winding)
{
if (!AC3D::degenerate(p0, p1, p2))
{
if (winding == WindingType::CCW)
normal = (p1 - p0).cross(p2 - p0);
else
normal = (p2 - p0).cross(p1 - p0);
normal.normalize();
distance = normal.dot(p0);
valid = true;
}
}
explicit Plane(const Triangle &triangle)
{
if (!triangle.degenerate)
{
normal = triangle.normal;
distance = normal.dot(triangle.vertices[0].vertex);
valid = true;
}
}
bool isOnPlane(Point3 point) const
{
static constexpr double SMALL_NUM = static_cast<double>(std::numeric_limits<double>::epsilon());
return std::abs(normal.dot(point) - distance) < SMALL_NUM;
}
double distanceToPoint(const Point3 &point) const
{
return normal.dot(point) - distance;
}
bool isAbovePlane(const Point3 &point) const
{
return distanceToPoint(point) >= 0.0;
}
bool isBelowPlane(const Point3 &point) const
{
return !isAbovePlane(point);
}
bool equals(const Plane &other) const
{
static constexpr double SMALL_NUM = static_cast<double>(std::numeric_limits<double>::epsilon());
return valid && other.valid && std::abs(distance - other.distance) < SMALL_NUM && normal.equals(other.normal);
}
};
struct Mats : public std::vector<Mat>
{
bool operator == (const Mats &mats) const
{
if (size() != mats.size())
return false;
for (size_t i = 0; i < size(); i++)
{
if (at(i).mat != mats[i].mat)
return false;
}
return true;
}
bool operator != (const Mats &mats) const
{
return !(mats == *this);
}
};
struct Surface : public LineInfo
{
unsigned int flags = 0;
Mats mats;
Refs refs;
bool coplanar = true; // only for Polygon and ClosedLine
Point3 normal = { 0.0, 0.0, 0.0 }; // only for Polygon
bool concave = false; // only for Polygon
std::vector<Triangle> triangleStrip; // only for triangle strips
enum : unsigned int
{
Polygon = 0, ClosedLine = 1, Line = 2, TriangleStrip = 4, Shaded = 0x10, DoubleSided = 0x20,
TypeMask = 0xf, FaceMask = 0xf0, ShadeMask = 0x10, SideMask = 0x20,
SingleSidedFlat = 0, SingleSidedSmooth = 0x10, DoubleSidedFlat = 0x20, DoubleSidedSmooth = 0x30
};
bool isPolygon() const
{
return (flags & TypeMask) == Polygon;
}
bool isClosedLine() const
{
return (flags & TypeMask) == ClosedLine;
}
bool isLine() const
{
return (flags & TypeMask) == Line;
}
bool isTriangleStrip() const
{
return (flags & TypeMask) == TriangleStrip;
}
bool isFlatShaded() const
{
return (flags & ShadeMask) == 0;
}
bool isSmoothShaded() const
{
return (flags & ShadeMask) == Shaded;
}
bool isSingleSided() const
{
return (flags & SideMask) != DoubleSided;
}
void setSingleSided()
{
flags = flags & ~DoubleSided;
}
bool isDoubleSided() const
{
return (flags & SideMask) == DoubleSided;
}
bool isSingleSidedFlat() const
{
return (flags & FaceMask) == SingleSidedFlat;
}
bool isSingleSidedSmooth() const
{
return (flags & FaceMask) == SingleSidedSmooth;
}
bool isDoubleSidedFlat() const
{
return (flags & FaceMask) == DoubleSidedFlat;
}
bool isDoubleSidedSmooth() const
{
return (flags & FaceMask) == DoubleSidedSmooth;
}
bool isValidFlags(bool is_ac) const
{
if (flags & ~(TypeMask | FaceMask))
return false;
if (!(isPolygon() || isClosedLine() || isLine() || (!is_ac && isTriangleStrip())))
return false;
if (!(isSingleSidedFlat() || isSingleSidedSmooth() || isDoubleSidedFlat() || isDoubleSidedSmooth()))
return false;
return true;
}
bool getIndex(size_t ref, size_t &index) const
{
if (ref >= refs.size())
return false;
index = refs[ref].index;
return true;
}
const std::vector<Triangle> &getTriangleStrip() const
{
return triangleStrip;
}
void setTriangleStrip(const Object &object);
bool isTriangle() const
{
return refs.size() == 3;
}
void dump(DumpType dump_type, size_t count, size_t level) const;
};
struct Location : public LineInfo
{
Point3 location = {0.0, 0.0, 0.0};
};
struct Rotation : public LineInfo
{
std::array<double,9> rotation = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
};
struct Crease : public LineInfo
{
double crease = 0.0;
};
struct Name : public LineInfo
{
quoted_string name;
};
struct Shader : public LineInfo
{
quoted_string name;
};
struct URL : public LineInfo
{
std::string url;
};
struct Type : public LineInfo
{
std::string type;
int type_offset = 0;
};
struct Numvert : public LineInfo
{
int number = 0;
int number_offset = 0;
};
struct Numvsurf : public LineInfo
{
int number = 0;
int number_offset = 0;
};
struct Object : public LineInfo
{
Type type;
std::vector<Name> names;
std::vector<URL> urls;
std::vector<Data> data;
std::vector<Shader> shaders;
std::vector<TexRep> texreps;
std::vector<TexOff> texoffs;
std::vector<SubDiv> subdivs;
std::vector<Location> locations;
std::vector<Rotation> rotations;
std::vector<Crease> creases;
std::vector<LineInfo> hidden;
std::vector<LineInfo> locked;
std::vector<LineInfo> folded;
std::vector<Texture> textures;
Numvert numvert;
std::vector<Vertex> vertices;
Numvsurf numsurf;
std::vector<Surface> surfaces;
std::vector<Object> kids;
Matrix matrix;
bool empty() const
{
return (type.type == "poly" && vertices.empty() && surfaces.empty() && kids.empty());
}
bool getVertex(size_t index, Point3 &vertex) const
{
if (index >= vertices.size())
return false;
vertex = vertices[index].vertex;
return true;
}
bool getSurfaceVertex(const Surface &surface,
size_t ref,
Point3 &vertex) const
{
size_t index;
if (!surface.getIndex(ref, index))
return false;
if (!getVertex(index, vertex))
return false;
return true;
}
bool getVertex(size_t index, Point2 &vertex, PlaneType planeType) const
{
if (index >= vertices.size())
return false;
switch (planeType)
{
case PlaneType::xy:
vertex.x(vertices[index].vertex.x());
vertex.y(vertices[index].vertex.y());
break;
case PlaneType::xz:
vertex.x(vertices[index].vertex.x());
vertex.y(vertices[index].vertex.z());
break;
case PlaneType::yz:
vertex.x(vertices[index].vertex.y());
vertex.y(vertices[index].vertex.z());
break;
}
return true;
}
bool getSurfaceVertex(const Surface &surface,
size_t ref,
Point2 &vertex,
PlaneType planeType) const
{
size_t index;
if (!surface.getIndex(ref, index))
return false;
if (!getVertex(index, vertex, planeType))
return false;
return true;
}
bool sameVertex(size_t index1, size_t index2) const
{
if (index1 == index2)
return true;
// skip invalid vertex
if (index1 >= vertices.size() || index2 >= vertices.size())
return false;
return vertices[index1].vertex == vertices[index2].vertex;
}
size_t getTexturesSize() const
{
int actual_textures = static_cast<int>(textures.size());
if (actual_textures == 0)
return 0;
for (int i = actual_textures - 1; i >= 0; i--)
{
if (textures[i].name == "empty_texture_no_mapping")
actual_textures--;
else
break;
}
return actual_textures;
}
const std::string &getName() const
{
if (!names.empty())
return names[0].name;
static const std::string none("");
return none;
}
void setName(const std::string &name)
{
names.resize(1);
names[0].name = quoted_string(name);
}
const std::string &getTexture() const
{
if (!textures.empty())
return textures[0].name;
static const std::string none("");
return none;
}
bool hasTransparentTexture() const;
bool sameSurface(size_t index1, size_t index2, Difference difference) const;
void dump(DumpType dump_type, size_t count, size_t level) const;
void incrementMaterialIndex(size_t num_materials);
void transform(const Matrix ¤tMatrix);
void removeKids(const RemoveInfo &remove_info);
bool splitPolygons();
bool addObject(const Object &object);
bool sameTextures(const Object &object) const;
};
struct NullStream : public std::ostream
{
public:
NullStream() : std::ostream(nullptr) {}
NullStream(const NullStream &) : std::ostream(nullptr) {}
template<typename T>
NullStream &operator<<(T const &) { return *this; }
};
NullStream m_null_stream;
std::string m_file;
std::string m_line;
size_t m_line_number = 0;
std::streampos m_line_pos;
size_t m_level = 0;
size_t m_errors = 0;
size_t m_warnings = 0;
bool m_is_utf_8 = false;
bool m_is_ac = false;
bool m_crlf = false;
bool m_not_ac3d_file = true;
bool m_quite = false;
bool m_summary = false;
bool m_show_times = false;
unsigned int m_threads = 1;
Header m_header;
std::vector<Material> m_materials;
std::vector<Object> m_objects;
std::vector<std::string> m_texture_paths;
bool m_has_world = false;
std::map<std::string, bool> m_transparent_textures;
bool m_rename_combine_texture = false;