-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathldrawloader.cpp
More file actions
4861 lines (4025 loc) · 153 KB
/
ldrawloader.cpp
File metadata and controls
4861 lines (4025 loc) · 153 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
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
/**
* Copyright (c) 2019-2025 Christoph Kubisch
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ldrawloader.hpp"
#include <immintrin.h>
#include <cmath>
#include <cstdlib>
#include <cfloat>
#include <algorithm>
#include <filesystem>
#define LDR_DEBUG_FLAG_FILELOAD 1
#define LDR_DEBUG_FLAG 0
#define LDR_DEBUG_PRINT_NON_MANIFOLDS 0
namespace ldr {
// ignores projective
inline LdrMatrix mat_identity()
{
LdrMatrix out = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
return out;
}
inline LdrMatrix mat_mul(const LdrMatrix& a, const LdrMatrix& b)
{
LdrMatrix out;
out.values[0] = a.values[0] * b.values[0] + a.values[4] * b.values[1] + a.values[8] * b.values[2];
out.values[1] = a.values[1] * b.values[0] + a.values[5] * b.values[1] + a.values[9] * b.values[2];
out.values[2] = a.values[2] * b.values[0] + a.values[6] * b.values[1] + a.values[10] * b.values[2];
out.values[3] = 0;
out.values[4] = a.values[0] * b.values[4] + a.values[4] * b.values[5] + a.values[8] * b.values[6];
out.values[5] = a.values[1] * b.values[4] + a.values[5] * b.values[5] + a.values[9] * b.values[6];
out.values[6] = a.values[2] * b.values[4] + a.values[6] * b.values[5] + a.values[10] * b.values[6];
out.values[7] = 0;
out.values[8] = a.values[0] * b.values[8] + a.values[4] * b.values[9] + a.values[8] * b.values[10];
out.values[9] = a.values[1] * b.values[8] + a.values[5] * b.values[9] + a.values[9] * b.values[10];
out.values[10] = a.values[2] * b.values[8] + a.values[6] * b.values[9] + a.values[10] * b.values[10];
out.values[11] = 0;
out.values[12] = a.values[0] * b.values[12] + a.values[4] * b.values[13] + a.values[8] * b.values[14] + a.values[12];
out.values[13] = a.values[1] * b.values[12] + a.values[5] * b.values[13] + a.values[9] * b.values[14] + a.values[13];
out.values[14] = a.values[2] * b.values[12] + a.values[6] * b.values[13] + a.values[10] * b.values[14] + a.values[14];
out.values[15] = 1;
return out;
}
inline LdrVector transform_point(const LdrMatrix& transform, const LdrVector& vec)
{
LdrVector out;
const float* mat = transform.values;
out.x = vec.x * (mat)[0] + vec.y * (mat)[4] + vec.z * (mat)[8] + (mat)[12];
out.y = vec.x * (mat)[1] + vec.y * (mat)[5] + vec.z * (mat)[9] + (mat)[13];
out.z = vec.x * (mat)[2] + vec.y * (mat)[6] + vec.z * (mat)[10] + (mat)[14];
return out;
}
inline LdrVector transform_vec(const LdrMatrix& transform, const LdrVector& vec)
{
LdrVector out;
const float* mat = transform.values;
out.x = vec.x * (mat)[0] + vec.y * (mat)[4] + vec.z * (mat)[8];
out.y = vec.x * (mat)[1] + vec.y * (mat)[5] + vec.z * (mat)[9];
out.z = vec.x * (mat)[2] + vec.y * (mat)[6] + vec.z * (mat)[10];
return out;
}
inline float mat_determinant(const LdrMatrix& transform)
{
// Sarrus rule
return transform.col[0][0] * transform.col[1][1] * transform.col[2][2]
+ transform.col[1][0] * transform.col[2][1] * transform.col[0][2]
+ transform.col[2][0] * transform.col[0][1] * transform.col[1][2]
- transform.col[2][0] * transform.col[1][1] * transform.col[0][2]
- transform.col[0][0] * transform.col[2][1] * transform.col[1][2]
- transform.col[1][0] * transform.col[0][1] * transform.col[2][2];
}
inline LdrVector make_vec(const float* v)
{
return {v[0], v[1], v[2]};
}
inline LdrVector vec_min(const LdrVector a, const LdrVector b)
{
return {std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z)};
}
inline LdrVector vec_max(const LdrVector a, const LdrVector b)
{
return {std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z)};
}
inline LdrVector vec_add(const LdrVector a, const LdrVector b)
{
return {a.x + b.x, a.y + b.y, a.z + b.z};
}
inline LdrVector vec_sub(const LdrVector a, const LdrVector b)
{
return {a.x - b.x, a.y - b.y, a.z - b.z};
}
inline LdrVector vec_div(const LdrVector a, const LdrVector b)
{
return {a.x / b.x, a.y / b.y, a.z / b.z};
}
inline LdrVector vec_mul(const LdrVector a, const LdrVector b)
{
return {a.x * b.x, a.y * b.y, a.z * b.z};
}
inline LdrVector vec_mul(const LdrVector a, const float b)
{
return {a.x * b, a.y * b, a.z * b};
}
inline LdrVector vec_ceil(const LdrVector a)
{
return {ceilf(a.x), ceilf(a.y), ceilf(a.z)};
}
inline LdrVector vec_floor(const LdrVector a)
{
return {floorf(a.x), floorf(a.y), floorf(a.z)};
}
inline LdrVector vec_clamp(const LdrVector a, const float lowerV, const float upperV)
{
return {std::max(std::min(upperV, a.x), lowerV), std::max(std::min(upperV, a.y), lowerV), std::max(std::min(upperV, a.z), lowerV)};
}
inline LdrVector vec_cross(const LdrVector a, const LdrVector b)
{
return {a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x};
}
inline float vec_dot(const LdrVector a, const LdrVector b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
inline float vec_sq_length(const LdrVector a)
{
return vec_dot(a, a);
}
inline float vec_length(const LdrVector a)
{
return sqrt(vec_dot(a, a));
}
inline LdrVector vec_normalize(const LdrVector a)
{
float len = vec_length(a);
return vec_mul(a, (1.0f / len));
}
inline LdrVector vec_normalize_length(const LdrVector a, float& length)
{
float len = vec_length(a);
length = len;
return vec_mul(a, (1.0f / len));
}
inline LdrVector vec_neg(const LdrVector a)
{
return {-a.x, -a.y, -a.z};
}
inline void bbox_merge(LdrBbox& bbox, const LdrVector vec)
{
bbox.min = vec_min(bbox.min, vec);
bbox.max = vec_max(bbox.max, vec);
}
inline void bbox_merge(LdrBbox& bbox, const LdrMatrix& transform, const LdrBbox other)
{
const float* values = &other.min.x;
uint32_t min = 0;
uint32_t max = uint32_t(offsetof(LdrBbox, max) / sizeof(float));
for(int i = 0; i < 8; i++) {
LdrVector corner;
corner.x = values[(i & 1 ? max : min) + 0];
corner.y = values[(i & 2 ? max : min) + 1];
corner.z = values[(i & 4 ? max : min) + 2];
LdrVector point = transform_point(transform, corner);
bbox_merge(bbox, point);
}
}
//////////////////////////////////////////////////////////////////////////
const float Loader::COPLANAR_TRIANGLE_DOT = 0.998f; // around 3 degrees
const float Loader::NO_AREA_TRIANGLE_DOT = 0.99999f;
const float Loader::FORCED_HARD_EDGE_DOT = 0.2f;
const float Loader::CHAMFER_PARALLEL_DOT = 0.999f;
const float Loader::ANGLE_45_DOT = 0.7071f;
const float Loader::MIN_MERGE_EPSILON = 0.015f; // 1 LDU ~ 0.4mm
static const uint32_t EDGE_HARD_BIT = 1 << 0;
static const uint32_t EDGE_OPTIONAL_BIT = 1 << 1;
static const uint32_t EDGE_HARD_FLOATER_BIT = 1 << 2;
static const uint32_t EDGE_MATERIAL_BIT = 1 << 3;
static const uint32_t EDGE_ANGLE_BIT = 1 << 4;
static const uint32_t EDGE_OVERLAP_BIT = 1 << 5;
static_assert(LDR_INVALID_ID == LDR_INVALID_IDX);
class Mesh
{
public:
static const uint32_t VTX_BITS = 31;
static const uint32_t INVALID = uint32_t(~0);
static_assert(INVALID == LDR_INVALID_IDX);
typedef uint64_t edgeHash_t;
struct VertexPair
{
uint32_t a;
uint32_t b;
};
static edgeHash_t make_edgeHash(uint32_t vtxA, uint32_t vtxB)
{
union
{
uint64_t u64;
VertexPair pair;
};
if(vtxA < vtxB) {
pair.a = vtxA;
pair.b = vtxB;
}
else {
pair.a = vtxB;
pair.b = vtxA;
}
return u64;
}
struct Connectivity
{
static const uint32_t GROWTH = 16;
struct Info
{
uint16_t count = 0;
uint16_t allocated = 0;
uint32_t offset = INVALID;
};
Loader::TVector<Info> infos;
Loader::TVector<uint32_t> content;
Loader* loader = nullptr; // mostly for debugging
void resize(uint32_t num)
{
infos.resize(num);
content.reserve(num * GROWTH);
}
// pointer only valid as long as no edits are made
const uint32_t* getConnected(uint32_t v, uint32_t& count) const
{
count = infos[v].count;
return content.data() + infos[v].offset;
}
void add(uint32_t v, uint32_t element)
{
uint32_t old = infos[v].count++;
uint32_t offset = infos[v].offset;
if(infos[v].allocated < infos[v].count) {
uint32_t newoffset = (uint32_t)content.size();
infos[v].allocated += GROWTH;
infos[v].offset = newoffset;
content.reserve(std::max(content.size(), (content.size() * 3) / 2));
content.resize(content.size() + infos[v].allocated);
if(old) {
memcpy(content.data() + newoffset, content.data() + offset, sizeof(uint32_t) * old);
}
offset = newoffset;
}
content[offset + old] = element;
}
void remove(uint32_t v, uint32_t element)
{
uint32_t* data = content.data() + infos[v].offset;
uint32_t count = infos[v].count;
for(uint32_t i = 0; i < count; i++) {
if(data[i] == element) {
data[i] = data[count - 1];
infos[v].count--;
}
}
}
};
struct TriAdjacency
{
// other triangle per edge
uint32_t edgeSmoothTri[3] = {INVALID, INVALID, INVALID};
uint32_t edgeManifoldTri[3] = {INVALID, INVALID, INVALID};
};
// additional left/right for non-manifold overflow
// linked list
struct EdgeNM
{
uint32_t tri = INVALID;
uint32_t nextNM = INVALID;
bool isLeft = true;
float angle = 0;
};
struct Edge
{
// left triangle has its edge running from A to B
// right triangle in opposite direction.
uint32_t vtxA;
uint32_t vtxB;
uint32_t triLeft;
uint32_t triRight;
float angleRight;
uint32_t flags;
uint32_t nmList;
bool isNonManifold() const { return (nmList != INVALID); }
bool isDead() const { return triLeft == INVALID; }
bool isOpen() const { return triRight == INVALID; }
bool hasFace(uint32_t idx) const { return triLeft == idx || triRight == idx; }
uint32_t otherVertex(uint32_t idx) const { return idx != vtxA ? vtxA : vtxB; }
uint32_t otherTri(uint32_t idx) const { return idx != triLeft ? triLeft : triRight; }
uint32_t getTri(uint32_t right) const { return right ? triRight : triLeft; }
uint32_t getVertex(uint32_t b) const { return b ? vtxB : vtxA; }
VertexPair getVertexPair() const { return {vtxA, vtxB}; }
};
uint32_t numVertices = 0;
uint32_t numTriangles = 0;
uint32_t numEdges = 0;
uint32_t freeNM = INVALID;
bool requiresVertexTriangles = false;
bool nonManifoldEdges = false;
bool nonManifoldNeedsOrdering = false;
bool hasOverlap = false;
uint32_t* triangles = nullptr;
Connectivity vtxTriangles;
Connectivity vtxEdges;
Loader::TVector<TriAdjacency> triAdjacencies;
Loader::BitArray triAlive;
Loader::TVector<Edge> edges;
Loader::TVector<EdgeNM> edgesNM;
std::unordered_map<edgeHash_t, uint32_t> lookupEdge;
Loader* loader = nullptr; // for debugging
inline bool resizeVertices(uint32_t num)
{
numVertices = num;
vtxEdges.resize(numVertices);
if(requiresVertexTriangles) {
vtxTriangles.resize(numVertices);
}
if(num > (1 << VTX_BITS)) {
// FIXME check against this error
fprintf(stderr, "Mesh VTX_BITS too small - %d > %d\n", num, (1 << VTX_BITS));
exit(-1);
return true;
}
return false;
}
inline bool getVertexClosable(uint32_t vertex)
{
uint32_t open = 0;
uint32_t count;
const uint32_t* curEdges = vtxEdges.getConnected(vertex, count);
for(uint32_t e = 0; e < count; e++) {
open += edges[curEdges[e]].isOpen() && !edges[curEdges[e]].isDead();
}
return open == 2;
}
inline Edge* getEdge(uint32_t vtxA, uint32_t vtxB)
{
const auto it = lookupEdge.find(make_edgeHash(vtxA, vtxB));
if(it != lookupEdge.cend()) {
return &edges[it->second];
}
return nullptr;
}
inline const Edge* getEdge(uint32_t vtxA, uint32_t vtxB) const
{
const auto it = lookupEdge.find(make_edgeHash(vtxA, vtxB));
if(it != lookupEdge.cend()) {
return &edges[it->second];
}
return nullptr;
}
inline uint32_t getEdgeIdx(uint32_t vtxA, uint32_t vtxB) const
{
const auto it = lookupEdge.find(make_edgeHash(vtxA, vtxB));
if(it != lookupEdge.cend()) {
return it->second;
}
return INVALID;
}
inline uint32_t* getTriangle(uint32_t t) { return &triangles[t * 3]; }
inline const uint32_t* getTriangle(uint32_t t) const { return &triangles[t * 3]; }
inline VertexPair getTriangleEdgeVertices(uint32_t t, uint32_t e) const
{
return {triangles[t * 3 + e], triangles[t * 3 + (e + 1) % 3]};
}
inline void setTriSmoothAdjacency(uint32_t t, uint32_t e, uint32_t tOther)
{
assert(t != tOther);
assert(triAdjacencies[t].edgeSmoothTri[e] == INVALID || triAdjacencies[t].edgeSmoothTri[e] == tOther);
triAdjacencies[t].edgeSmoothTri[e] = tOther;
}
inline void setTriManifoldAdjacency(uint32_t t, uint32_t e, uint32_t tOther)
{
assert(t != tOther);
assert(triAdjacencies[t].edgeManifoldTri[e] == INVALID || triAdjacencies[t].edgeManifoldTri[e] == tOther);
triAdjacencies[t].edgeManifoldTri[e] = tOther;
}
bool areTriManifoldAdjacent(uint32_t ta, uint32_t tb) const
{
const TriAdjacency triA = triAdjacencies[ta];
return (triA.edgeManifoldTri[0] == tb || triA.edgeManifoldTri[1] == tb || triA.edgeManifoldTri[2] == tb);
}
bool areTriSmoothAdjacent(uint32_t ta, uint32_t tb) const
{
const TriAdjacency triA = triAdjacencies[ta];
return (triA.edgeSmoothTri[0] == tb || triA.edgeSmoothTri[1] == tb || triA.edgeSmoothTri[2] == tb);
}
static inline uint32_t findLowest(const uint32_t* indices)
{
uint32_t lowestIdx = indices[0];
uint32_t lowest = 0;
if(indices[1] < lowestIdx) {
lowestIdx = indices[1];
lowest = 1;
}
if(indices[2] < lowestIdx) {
lowestIdx = indices[2];
lowest = 2;
}
return lowest;
}
inline bool areTrianglesSame(uint32_t t1, uint32_t t2) const
{
const uint32_t* indices1 = &triangles[t1 * 3];
const uint32_t* indices2 = &triangles[t2 * 3];
uint32_t lowest1 = findLowest(indices1);
uint32_t lowest2 = findLowest(indices2);
for(uint32_t i = 0; i < 3; i++) {
if(indices1[(lowest1 + i) % 3] != indices2[(lowest2 + i) % 3])
return false;
}
return true;
}
// find cross product of vectors with widest angle
inline LdrVector getTriangleNormal(uint32_t t, const LdrVector* positions) const
{
uint32_t idxA = triangles[t * 3 + 0];
uint32_t idxB = triangles[t * 3 + 1];
uint32_t idxC = triangles[t * 3 + 2];
LdrVector sides[3];
float minAngle = FLT_MAX;
uint32_t corner = 0;
for(uint32_t i = 0; i < 3; i++) {
sides[i] = vec_normalize(vec_sub(positions[triangles[t * 3 + i]], positions[triangles[t * 3 + (i + 1) % 3]]));
}
for(uint32_t i = 0; i < 3; i++) {
float angle = std::fabs(vec_dot(vec_neg(sides[i]), sides[(i + 1) % 3]));
if(angle < minAngle) {
minAngle = angle;
corner = i;
}
}
return vec_normalize(vec_cross((sides[corner]), (sides[(corner + 1) % 3])));
//return vec_normalize(vec_cross(sides[0], vec_neg(sides[2])));
}
inline void getTriangleBasis(uint32_t t, const LdrVector* positions, LdrVector basis[3]) const
{
LdrVector a = positions[triangles[t * 3 + 0]];
LdrVector b = positions[triangles[t * 3 + 1]];
LdrVector c = positions[triangles[t * 3 + 2]];
basis[0] = vec_normalize(vec_sub(b, a));
basis[2] = getTriangleNormal(t, positions);
basis[1] = vec_normalize(vec_cross(basis[2], basis[0]));
}
// average of the 3 normals
inline LdrVector getTriangleCross(uint32_t t, const LdrVector* positions) const
{
uint32_t idxA = triangles[t * 3 + 0];
uint32_t idxB = triangles[t * 3 + 1];
uint32_t idxC = triangles[t * 3 + 2];
LdrVector sides[3];
LdrVector cross = {0.0f, 0.0f, 0.0f};
for(uint32_t i = 0; i < 3; i++) {
sides[i] = (vec_sub(positions[triangles[t * 3 + i]], positions[triangles[t * 3 + (i + 1) % 3]]));
}
for(uint32_t i = 0; i < 3; i++) {
cross = vec_add(cross, vec_cross((sides[i]), (sides[(i + 1) % 3])));
}
return vec_mul(cross, 1.0f / 3.0f);
}
inline const uint32_t getTriangleOtherVertex(uint32_t t, const Edge& edge) const
{
uint32_t idxA = triangles[t * 3 + 0];
uint32_t idxB = triangles[t * 3 + 1];
uint32_t idxC = triangles[t * 3 + 2];
if(idxA != edge.vtxA && idxA != edge.vtxB)
return idxA;
if(idxB != edge.vtxA && idxB != edge.vtxB)
return idxB;
if(idxC != edge.vtxA && idxC != edge.vtxB)
return idxC;
return INVALID;
}
inline uint32_t findTriangleVertex(uint32_t t, uint32_t vtx) const
{
const uint32_t* indices = getTriangle(t);
if(indices[0] == vtx)
return 0;
if(indices[1] == vtx)
return 1;
if(indices[2] == vtx)
return 2;
return INVALID;
}
inline void replaceTriangleVertex(uint32_t t, uint32_t vtx, uint32_t newVtx)
{
uint32_t* indices = getTriangle(t);
if(indices[0] == vtx)
indices[0] = newVtx;
if(indices[1] == vtx)
indices[1] = newVtx;
if(indices[2] == vtx)
indices[2] = newVtx;
}
inline uint32_t findTriangleConnectingVertices(uint32_t tA, uint32_t tB, uint32_t* vertsA, uint32_t* vertsB) const
{
const uint32_t* indices = getTriangle(tA);
uint32_t num = 0;
uint32_t idxB;
idxB = findTriangleVertex(tB, indices[0]);
if(idxB != INVALID) {
vertsA[num] = 0;
vertsB[num] = idxB;
num++;
}
idxB = findTriangleVertex(tB, indices[1]);
if(idxB != INVALID) {
vertsA[num] = 1;
vertsB[num] = idxB;
num++;
}
idxB = findTriangleVertex(tB, indices[2]);
if(idxB != INVALID) {
vertsA[num] = 2;
vertsB[num] = idxB;
num++;
}
return num;
}
uint32_t addEdgeNM(uint32_t startNM, uint32_t tri, bool isLeft, bool& identicalNeighbor)
{
uint32_t nextNM = startNM;
while(nextNM != INVALID) {
EdgeNM& edgeNM = edgesNM[nextNM];
if(edgeNM.tri == tri)
return startNM;
if(areTrianglesSame(edgeNM.tri, tri)) {
identicalNeighbor = true;
return startNM;
}
nextNM = edgeNM.nextNM;
}
uint32_t outNextNM;
if(freeNM != INVALID) {
outNextNM = freeNM;
freeNM = edgesNM[freeNM].nextNM;
edgesNM[outNextNM].tri = tri;
edgesNM[outNextNM].nextNM = startNM;
edgesNM[outNextNM].isLeft = isLeft;
}
else {
outNextNM = edgesNM.size();
edgesNM.push_back({tri, startNM, isLeft, 0});
}
return outNextNM;
}
uint32_t findFirstEdgeNM(uint32_t nextNM, bool isLeft) const
{
while(nextNM != INVALID) {
const EdgeNM& edgeNM = edgesNM[nextNM];
if(edgeNM.isLeft == isLeft) {
return edgeNM.tri;
}
nextNM = edgeNM.nextNM;
}
return INVALID;
}
const EdgeNM* iterateEdgeNM(uint32_t& startNM) const
{
if(startNM != INVALID) {
uint32_t current = startNM;
startNM = edgesNM[startNM].nextNM;
return &edgesNM[current];
}
return nullptr;
}
EdgeNM* iterateEdgeNM(uint32_t& startNM)
{
if(startNM != INVALID) {
uint32_t current = startNM;
startNM = edgesNM[startNM].nextNM;
return &edgesNM[current];
}
return nullptr;
}
bool removeEdgeNM(uint32_t& startNM, uint32_t tri)
{
uint32_t nextNM = startNM;
uint32_t prevNM = INVALID;
while(nextNM != INVALID) {
EdgeNM& edgeNM = edgesNM[nextNM];
if(edgeNM.tri == tri) {
uint32_t oldNextNM = edgeNM.nextNM;
// insert into freelist
edgeNM.nextNM = freeNM;
edgeNM.tri = INVALID;
freeNM = nextNM;
if(prevNM != INVALID) {
edgesNM[prevNM].nextNM = oldNextNM;
}
else {
// change list head
startNM = oldNextNM;
}
return true;
}
prevNM = nextNM;
nextNM = edgeNM.nextNM;
}
return false;
}
void addEdge(uint32_t vtxA, uint32_t vtxB, uint32_t tri, uint32_t& nonManifold, bool& identicalNeighbor)
{
if(identicalNeighbor)
return;
edgeHash_t edgeHash = make_edgeHash(vtxA, vtxB);
auto it = lookupEdge.find(edgeHash);
if(it != lookupEdge.end()) {
Edge& edge = edges[it->second];
if(edge.triLeft == tri || edge.triRight == tri) {
it = it;
}
else if(vtxA == edge.vtxB && edge.triRight == INVALID) {
if(areTrianglesSame(edge.triLeft, tri)) {
identicalNeighbor = true;
return;
}
edge.triRight = tri;
}
else {
edge.nmList = addEdgeNM(edge.nmList, tri, edge.vtxA == vtxA, identicalNeighbor);
if(identicalNeighbor)
return;
nonManifold = it->second;
nonManifoldNeedsOrdering = true;
}
return;
}
else {
uint32_t edgeIdx = numEdges;
Edge edge;
edge.vtxA = vtxA;
edge.vtxB = vtxB;
edge.triLeft = tri;
edge.triRight = INVALID;
edge.angleRight = 0;
edge.flags = 0;
edge.nmList = INVALID;
edges.push_back(edge);
lookupEdge.insert({edgeHash, edgeIdx});
vtxEdges.add(vtxA, edgeIdx);
vtxEdges.add(vtxB, edgeIdx);
numEdges++;
return;
}
}
inline void removeEdge(uint32_t vtxA, uint32_t vtxB, uint32_t tri)
{
edgeHash_t edgeHash = make_edgeHash(vtxA, vtxB);
auto it = lookupEdge.find(edgeHash);
if(it == lookupEdge.end())
return;
Edge& edge = edges[it->second];
if(!edge.isNonManifold()) {
if(edge.triRight == tri) {
edge.triRight = INVALID;
}
else if(edge.triRight != INVALID) {
// if only right left, migrate right to left
edge.triLeft = edge.triRight;
uint32_t temp = edge.vtxB;
edge.vtxB = edge.vtxA;
edge.vtxA = temp;
edge.triRight = INVALID;
}
else {
edge.triLeft = INVALID;
vtxEdges.remove(edge.vtxA, it->second);
vtxEdges.remove(edge.vtxB, it->second);
lookupEdge.erase(edgeHash);
}
}
else {
bool isLeft = edge.vtxA == vtxA;
bool popFirst = edge.triLeft == tri || edge.triRight == tri;
uint32_t first = popFirst ? findFirstEdgeNM(edge.nmList, isLeft) : 0;
uint32_t removeTri = tri;
// find first with matching state
if(edge.triLeft == tri) {
edge.triLeft = first;
removeTri = first;
nonManifoldNeedsOrdering = true;
}
else if(edge.triRight == tri) {
edge.triRight = first;
removeTri = first;
}
removeEdgeNM(edge.nmList, removeTri);
// if only right exist, migrate right to left
if(edge.triLeft == INVALID && edge.triRight != INVALID) {
edge.triLeft = edge.triRight;
uint32_t temp = edge.vtxB;
edge.vtxB = edge.vtxA;
edge.vtxA = temp;
edge.triRight = INVALID;
// flip left/right in edgeNM list
uint32_t nextNM = edge.nmList;
while(nextNM != INVALID) {
iterateEdgeNM(nextNM)->isLeft ^= true;
}
}
else if(edge.triLeft == INVALID) {
vtxEdges.remove(edge.vtxA, it->second);
vtxEdges.remove(edge.vtxB, it->second);
lookupEdge.erase(edgeHash);
}
}
}
inline uint32_t addTriangle(uint32_t t)
{
uint32_t nonManifold = INVALID;
uint32_t idxA = triangles[t * 3 + 0];
uint32_t idxB = triangles[t * 3 + 1];
uint32_t idxC = triangles[t * 3 + 2];
if(idxA == idxB || idxB == idxC || idxA == idxC)
return INVALID;
bool identicalNeighbor = false;
addEdge(idxA, idxB, t, nonManifold, identicalNeighbor);
addEdge(idxB, idxC, t, nonManifold, identicalNeighbor);
addEdge(idxC, idxA, t, nonManifold, identicalNeighbor);
if(requiresVertexTriangles && !identicalNeighbor) {
vtxTriangles.add(idxA, t);
vtxTriangles.add(idxB, t);
vtxTriangles.add(idxC, t);
}
if(triAlive.size() <= t) {
triAlive.resize(t + 1, false);
numTriangles = t + 1;
}
assert(!triAlive.getBit(t));
triAlive.setBit(t, !identicalNeighbor);
return nonManifold;
}
inline void removeTriangle(uint32_t t, bool flagDead = true)
{
if(!triAlive.getBit(t))
return;
uint32_t idxA = triangles[t * 3 + 0];
uint32_t idxB = triangles[t * 3 + 1];
uint32_t idxC = triangles[t * 3 + 2];
removeEdge(idxA, idxB, t);
removeEdge(idxB, idxC, t);
removeEdge(idxC, idxA, t);
if(requiresVertexTriangles) {
vtxTriangles.remove(idxA, t);
vtxTriangles.remove(idxB, t);
vtxTriangles.remove(idxC, t);
}
if(flagDead) {
triAlive.setBit(t, false);
}
}
void initBasics(uint32_t numV, uint32_t numT, uint32_t* tris, Loader* _loader)
{
loader = _loader;
vtxEdges.loader = _loader;
vtxTriangles.loader = _loader;
numEdges = 0;
numTriangles = numT;
triangles = tris;
edges.reserve(numT * 3);
lookupEdge.reserve(numT * 3);
triAlive.resize(numT, false);
triAdjacencies.resize(numT, TriAdjacency());
resizeVertices(numV);
}
bool initFull(uint32_t numV, uint32_t numT, uint32_t* tris, const LdrVector* positions, Loader* _loader)
{
requiresVertexTriangles = true;
bool nonManifold = false;
initBasics(numV, numT, tris, _loader);
for(uint32_t t = 0; t < numT; t++) {
nonManifold = (addTriangle(t) != INVALID) || nonManifold;
}
if(nonManifold) {
orderNonManifoldByAngle(positions);
}
return nonManifold;
}
struct SortBasis
{
LdrVector posA;
LdrVector vecTriNormal; // perpendicular to triangle plane
LdrVector vecTriOpenSide; // in triangle plane, parallel to edge
float getAngle(LdrVector posC) const
{
LdrVector vecCA = vec_normalize(vec_sub(posC, posA));
float y = -vec_dot(vecTriNormal, vecCA);
float x = vec_dot(vecTriOpenSide, vecCA);
return atan2f(y, x);
}
void init(const LdrVector* positions, uint32_t a, uint32_t b, uint32_t c)
{
posA = positions[a];
LdrVector posB = positions[b];
LdrVector posC = positions[c];
LdrVector vecBA = vec_normalize(vec_sub(posB, posA));
LdrVector vecCA = vec_normalize(vec_sub(posC, posA));
vecTriNormal = vec_normalize(vec_cross(vecBA, vecCA));
vecTriOpenSide = vec_normalize(vec_cross(vecBA, vecTriNormal));
}
};
struct SortEdge
{
float angle;
uint32_t tri;
bool isLeft;
static bool comparator(const SortEdge& a, const SortEdge& b) { return a.angle < b.angle; }
};
void orderNonManifoldByAngle(const LdrVector* positions)