-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNvTriStripObjects.cpp
More file actions
1722 lines (1427 loc) · 45.8 KB
/
NvTriStripObjects.cpp
File metadata and controls
1722 lines (1427 loc) · 45.8 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
#include "NvTriStripObjects.h"
#include "VertexCache.h"
#include <cassert>
#include <cstddef>
#include <cstdio>
#include <algorithm>
#include <iterator>
#include <memory>
#include <numeric>
#include <set>
namespace nv::tristrip::internal {
constexpr inline int CACHE_INEFFICIENCY{6};
NvStripifier::NvStripifier()
{
cacheSize = 0;
minStripLength = 0;
meshJump = 0;
bFirstTimeResetPoint = false;
}
NvStripifier::~NvStripifier() = default;
///////////////////////////////////////////////////////////////////////////////////////////
// FindEdgeInfo()
//
// find the edge info for these two indices
//
NvEdgeInfo * NvStripifier::FindEdgeInfo(const NvEdgeInfoVec &edgeInfos, int v0, int v1){
// we can get to it through either array
// because the edge infos have a v0 and v1
// and there is no order except how it was
// first created.
NvEdgeInfo *infoIter = edgeInfos[v0];
while (infoIter != nullptr){
if (infoIter->m_v0 == v0){
if (infoIter->m_v1 == v1)
return infoIter;
infoIter = infoIter->m_nextV0;
}
else {
assert(infoIter->m_v1 == v0);
if (infoIter->m_v0 == v1)
return infoIter;
infoIter = infoIter->m_nextV1;
}
}
return nullptr;
}
///////////////////////////////////////////////////////////////////////////////////////////
// FindOtherFace
//
// find the other face sharing these vertices
// exactly like the edge info above
//
NvFaceInfo * NvStripifier::FindOtherFace(NvEdgeInfoVec &edgeInfos, int v0, int v1, const NvFaceInfo *faceInfo){
NvEdgeInfo *edgeInfo = FindEdgeInfo(edgeInfos, v0, v1);
if( (edgeInfo == nullptr) && (v0 == v1))
{
//we've hit a degenerate
return nullptr;
}
assert(edgeInfo != nullptr);
return (edgeInfo->m_face0 == faceInfo ? edgeInfo->m_face1 : edgeInfo->m_face0);
}
bool NvStripifier::AlreadyExists(NvFaceInfo* faceInfo, const NvFaceInfoVec& faceInfos)
{
return std::any_of(std::begin(faceInfos),
std::end(faceInfos),
[faceInfo](const NvFaceInfo* f) noexcept {
return
(f->m_v0 == faceInfo->m_v0) &&
(f->m_v1 == faceInfo->m_v1) &&
(f->m_v2 == faceInfo->m_v2);
});
}
///////////////////////////////////////////////////////////////////////////////////////////
// BuildStripifyInfo()
//
// Builds the list of all face and edge infos
//
void NvStripifier::BuildStripifyInfo(NvFaceInfoVec &faceInfos, NvEdgeInfoVec &edgeInfos,
const size_t maxIndex)
{
// reserve space for the face infos, but do not resize them.
size_t numIndices = indices.size();
faceInfos.reserve(numIndices / 3);
// we actually resize the edge infos, so we must initialize to nullptr
edgeInfos.resize(maxIndex + 1);
for (size_t i = 0; i < maxIndex + 1; i++)
edgeInfos[i] = nullptr;
// iterate through the triangles of the triangle list
size_t numTriangles = numIndices / 3;
size_t index = 0;
bool bFaceUpdated[3];
for (size_t i = 0; i < numTriangles; i++)
{
bool bMightAlreadyExist = true;
bFaceUpdated[0] = false;
bFaceUpdated[1] = false;
bFaceUpdated[2] = false;
// grab the indices
unsigned int v0 = indices[index++];
unsigned int v1 = indices[index++];
unsigned int v2 = indices[index++];
//we disregard degenerates
if(IsDegenerate(v0, v1, v2))
continue;
// create the face info and add it to the list of faces, but only if this exact face doesn't already
// exist in the list
NvFaceInfo *faceInfo = new NvFaceInfo(v0, v1, v2);
// grab the edge infos, creating them if they do not already exist
NvEdgeInfo *edgeInfo01 = FindEdgeInfo(edgeInfos, v0, v1);
if (edgeInfo01 == nullptr)
{
//since one of it's edges isn't in the edge data structure, it can't already exist in the face structure
bMightAlreadyExist = false;
// create the info
edgeInfo01 = new NvEdgeInfo(v0, v1);
// update the linked list on both
edgeInfo01->m_nextV0 = edgeInfos[v0];
edgeInfo01->m_nextV1 = edgeInfos[v1];
edgeInfos[v0] = edgeInfo01;
edgeInfos[v1] = edgeInfo01;
// set face 0
edgeInfo01->m_face0 = faceInfo;
}
else
{
if (edgeInfo01->m_face1 != nullptr)
{
fprintf(stderr, "BuildStripifyInfo: > 2 triangles on an edge... uncertain consequences\n");
}
else
{
edgeInfo01->m_face1 = faceInfo;
bFaceUpdated[0] = true;
}
}
// grab the edge infos, creating them if they do not already exist
NvEdgeInfo *edgeInfo12 = FindEdgeInfo(edgeInfos, v1, v2);
if (edgeInfo12 == nullptr)
{
bMightAlreadyExist = false;
// create the info
edgeInfo12 = new NvEdgeInfo(v1, v2);
// update the linked list on both
edgeInfo12->m_nextV0 = edgeInfos[v1];
edgeInfo12->m_nextV1 = edgeInfos[v2];
edgeInfos[v1] = edgeInfo12;
edgeInfos[v2] = edgeInfo12;
// set face 0
edgeInfo12->m_face0 = faceInfo;
}
else
{
if (edgeInfo12->m_face1 != nullptr)
{
fprintf(stderr, "BuildStripifyInfo: > 2 triangles on an edge... uncertain consequences\n");
}
else
{
edgeInfo12->m_face1 = faceInfo;
bFaceUpdated[1] = true;
}
}
// grab the edge infos, creating them if they do not already exist
NvEdgeInfo *edgeInfo20 = FindEdgeInfo(edgeInfos, v2, v0);
if (edgeInfo20 == nullptr)
{
bMightAlreadyExist = false;
// create the info
edgeInfo20 = new NvEdgeInfo(v2, v0);
// update the linked list on both
edgeInfo20->m_nextV0 = edgeInfos[v2];
edgeInfo20->m_nextV1 = edgeInfos[v0];
edgeInfos[v2] = edgeInfo20;
edgeInfos[v0] = edgeInfo20;
// set face 0
edgeInfo20->m_face0 = faceInfo;
}
else
{
if (edgeInfo20->m_face1 != nullptr)
{
fprintf(stderr, "BuildStripifyInfo: > 2 triangles on an edge... uncertain consequences\n");
}
else
{
edgeInfo20->m_face1 = faceInfo;
bFaceUpdated[2] = true;
}
}
if(bMightAlreadyExist)
{
if(!AlreadyExists(faceInfo, faceInfos))
faceInfos.emplace_back(faceInfo);
else
{
delete faceInfo;
//cleanup pointers that point to this deleted face
if(bFaceUpdated[0])
edgeInfo01->m_face1 = nullptr;
if(bFaceUpdated[1])
edgeInfo12->m_face1 = nullptr;
if(bFaceUpdated[2])
edgeInfo20->m_face1 = nullptr;
}
}
else
{
faceInfos.emplace_back(faceInfo);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// FindStartPoint()
//
// Finds a good starting point, namely one which has only one neighbor
//
std::ptrdiff_t NvStripifier::FindStartPoint(const NvFaceInfoVec &faceInfos, NvEdgeInfoVec &edgeInfos)
{
int bestCtr = -1;
std::ptrdiff_t bestIndex = -1, i = 0;
for(auto &f : faceInfos)
{
int ctr = 0;
if(FindOtherFace(edgeInfos, f->m_v0, f->m_v1, f) == nullptr)
ctr++;
if(FindOtherFace(edgeInfos, f->m_v1, f->m_v2, f) == nullptr)
ctr++;
if(FindOtherFace(edgeInfos, f->m_v2, f->m_v0, f) == nullptr)
ctr++;
if(ctr > bestCtr)
{
bestCtr = ctr;
bestIndex = i;
}
++i;
}
if(bestCtr == 0)
return -1;
else
return bestIndex;
}
///////////////////////////////////////////////////////////////////////////////////////////
// FindGoodResetPoint()
//
// A good reset point is one near other commited areas so that
// we know that when we've made the longest strips its because
// we're stripifying in the same general orientation.
//
NvFaceInfo* NvStripifier::FindGoodResetPoint(NvFaceInfoVec &faceInfos, NvEdgeInfoVec &edgeInfos){
// we hop into different areas of the mesh to try to get
// other large open spans done. Areas of small strips can
// just be left to triangle lists added at the end.
NvFaceInfo *result = nullptr;
{
size_t numFaces = faceInfos.size();
std::ptrdiff_t startPoint;
if(bFirstTimeResetPoint)
{
//first time, find a face with few neighbors (look for an edge of the mesh)
startPoint = FindStartPoint(faceInfos, edgeInfos);
bFirstTimeResetPoint = false;
}
else
// START EPIC MOD: Fix incorrect bracket placement
startPoint = (std::ptrdiff_t)(float( numFaces - 1 ) * meshJump );
// END EPIC MOD: Fix incorrect bracket placement
if(startPoint == -1)
{
// START EPIC MOD: Fix incorrect bracket placement
startPoint = (std::ptrdiff_t)(float( numFaces - 1 ) * meshJump );
// END EPIC MOD: Fix incorrect bracket placement
//meshJump += 0.1f;
//if (meshJump > 1.0f)
// meshJump = .05f;
}
std::ptrdiff_t i = startPoint;
do {
// if this guy isn't visited, try him
if (faceInfos[i]->m_stripId < 0){
result = faceInfos[i];
break;
}
// update the index and clamp to 0-(numFaces-1)
if (static_cast<size_t>(++i) >= numFaces)
i = 0;
} while (i != startPoint);
// update the meshJump
meshJump += 0.1f;
if (meshJump > 1.0f)
meshJump = .05f;
}
// return the best face we found
return result;
}
///////////////////////////////////////////////////////////////////////////////////////////
// GetUniqueVertexInB()
//
// Returns the vertex unique to faceB
//
int NvStripifier::GetUniqueVertexInB(NvFaceInfo *faceA, NvFaceInfo *faceB){
int facev0 = faceB->m_v0;
if (facev0 != faceA->m_v0 &&
facev0 != faceA->m_v1 &&
facev0 != faceA->m_v2)
return facev0;
int facev1 = faceB->m_v1;
if (facev1 != faceA->m_v0 &&
facev1 != faceA->m_v1 &&
facev1 != faceA->m_v2)
return facev1;
int facev2 = faceB->m_v2;
if (facev2 != faceA->m_v0 &&
facev2 != faceA->m_v1 &&
facev2 != faceA->m_v2)
return facev2;
// nothing is different
return -1;
}
///////////////////////////////////////////////////////////////////////////////////////////
// GetSharedVertices()
//
// Returns the (at most) two vertices shared between the two faces
//
void NvStripifier::GetSharedVertices(NvFaceInfo *faceA, NvFaceInfo *faceB, int* vertex0, int* vertex1)
{
*vertex0 = -1;
*vertex1 = -1;
int facev0 = faceB->m_v0;
if (facev0 == faceA->m_v0 ||
facev0 == faceA->m_v1 ||
facev0 == faceA->m_v2)
{
if(*vertex0 == -1)
*vertex0 = facev0;
}
int facev1 = faceB->m_v1;
if (facev1 == faceA->m_v0 ||
facev1 == faceA->m_v1 ||
facev1 == faceA->m_v2)
{
if(*vertex0 == -1)
*vertex0 = facev1;
else
{
*vertex1 = facev1;
return;
}
}
int facev2 = faceB->m_v2;
if (facev2 == faceA->m_v0 ||
facev2 == faceA->m_v1 ||
facev2 == faceA->m_v2)
{
if(*vertex0 == -1)
*vertex0 = facev2;
else
{
*vertex1 = facev2;
return;
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// GetNextIndex()
//
// Returns vertex of the input face which is "next" in the input index list
//
inline int NvStripifier::GetNextIndex(const UIntVec &indices, NvFaceInfo *face){
size_t numIndices = indices.size();
assert(numIndices >= 2);
int v0 = indices[numIndices-2];
int v1 = indices[numIndices-1];
int fv0 = face->m_v0;
int fv1 = face->m_v1;
int fv2 = face->m_v2;
if (fv0 != v0 && fv0 != v1){
if ((fv1 != v0 && fv1 != v1) || (fv2 != v0 && fv2 != v1)){
fprintf(stderr, "GetNextIndex: Triangle doesn't have all of its vertices\n");
fprintf(stderr, "GetNextIndex: Duplicate triangle probably got us derailed\n");
}
return fv0;
}
if (fv1 != v0 && fv1 != v1){
if ((fv0 != v0 && fv0 != v1) || (fv2 != v0 && fv2 != v1)){
fprintf(stderr, "GetNextIndex: Triangle doesn't have all of its vertices\n");
fprintf(stderr, "GetNextIndex: Duplicate triangle probably got us derailed\n");
}
return fv1;
}
if (fv2 != v0 && fv2 != v1){
if ((fv0 != v0 && fv0 != v1) || (fv1 != v0 && fv1 != v1)){
fprintf(stderr, "GetNextIndex: Triangle doesn't have all of its vertices\n");
fprintf(stderr, "GetNextIndex: Duplicate triangle probably got us derailed\n");
}
return fv2;
}
// shouldn't get here, but let's try and fail gracefully
if( (fv0 == fv1) || (fv0 == fv2) )
return fv0;
else if( (fv1 == fv0) || (fv1 == fv2) )
return fv1;
else if( (fv2 == fv0) || (fv2 == fv1) )
return fv2;
else
return -1;
}
///////////////////////////////////////////////////////////////////////////////////////////
// IsMarked()
//
// If either the faceInfo has a real strip index because it is
// already assign to a committed strip OR it is assigned in an
// experiment and the experiment index is the one we are building
// for, then it is marked and unavailable
inline bool NvStripInfo::IsMarked(NvFaceInfo *faceInfo) const {
return (faceInfo->m_stripId >= 0) || (IsExperiment() && faceInfo->m_experimentId == m_experimentId);
}
///////////////////////////////////////////////////////////////////////////////////////////
// MarkTriangle()
//
// Marks the face with the current strip ID
//
inline void NvStripInfo::MarkTriangle(NvFaceInfo *faceInfo){
assert(!IsMarked(faceInfo));
if (IsExperiment()){
faceInfo->m_experimentId = m_experimentId;
faceInfo->m_testStripId = m_stripId;
}
else{
assert(faceInfo->m_stripId == -1);
faceInfo->m_experimentId = -1;
faceInfo->m_stripId = m_stripId;
}
}
bool NvStripInfo::Unique(const NvFaceInfoVec& faceVec, NvFaceInfo* face) const
{
bool bv0, bv1, bv2; //bools to indicate whether a vertex is in the faceVec or not
bv0 = bv1 = bv2 = false;
for(auto &f : faceVec)
{
if(!bv0)
{
if( (f->m_v0 == face->m_v0) ||
(f->m_v1 == face->m_v0) ||
(f->m_v2 == face->m_v0) )
bv0 = true;
}
if(!bv1)
{
if( (f->m_v0 == face->m_v1) ||
(f->m_v1 == face->m_v1) ||
(f->m_v2 == face->m_v1) )
bv1 = true;
}
if(!bv2)
{
if( (f->m_v0 == face->m_v2) ||
(f->m_v1 == face->m_v2) ||
(f->m_v2 == face->m_v2) )
bv2 = true;
}
//the face is not unique, all it's vertices exist in the face vector
if(bv0 && bv1 && bv2)
return false;
}
//if we get out here, it's unique
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Build()
//
// Builds a strip forward as far as we can go, then builds backwards, and joins the two lists
//
void NvStripInfo::Build(NvEdgeInfoVec &edgeInfos, NvFaceInfoVec &)
{
// used in building the strips forward and backward
UIntVec scratchIndices;
// build forward... start with the initial face
NvFaceInfoVec forwardFaces, backwardFaces;
forwardFaces.emplace_back(m_startInfo.m_startFace);
MarkTriangle(m_startInfo.m_startFace);
int v0 = (m_startInfo.m_toV1 ? m_startInfo.m_startEdge->m_v0 : m_startInfo.m_startEdge->m_v1);
int v1 = (m_startInfo.m_toV1 ? m_startInfo.m_startEdge->m_v1 : m_startInfo.m_startEdge->m_v0);
// easiest way to get v2 is to use this function which requires the
// other indices to already be in the list.
scratchIndices.emplace_back(v0);
scratchIndices.emplace_back(v1);
int v2 = NvStripifier::GetNextIndex(scratchIndices, m_startInfo.m_startFace);
scratchIndices.emplace_back(v2);
//
// build the forward list
//
int nv0 = v1;
int nv1 = v2;
NvFaceInfo *nextFace = NvStripifier::FindOtherFace(edgeInfos, nv0, nv1, m_startInfo.m_startFace);
while (nextFace != nullptr && !IsMarked(nextFace))
{
//check to see if this next face is going to cause us to die soon
int testnv0 = nv1;
int testnv1 = NvStripifier::GetNextIndex(scratchIndices, nextFace);
NvFaceInfo* nextNextFace = NvStripifier::FindOtherFace(edgeInfos, testnv0, testnv1, nextFace);
if( (nextNextFace == nullptr) || (IsMarked(nextNextFace)) )
{
//uh, oh, we're following a dead end, try swapping
NvFaceInfo* testNextFace = NvStripifier::FindOtherFace(edgeInfos, nv0, testnv1, nextFace);
if( ((testNextFace != nullptr) && !IsMarked(testNextFace)) )
{
//we only swap if it buys us something
//add a "fake" degenerate face
NvFaceInfo* tempFace = new NvFaceInfo(nv0, nv1, nv0);
// START EPIC MOD: Fix memory leak
// We need to store this pointer to avoid a memory leak from the experiments that didn't make it. Only the experiment
// that passes has its degenerates deleted. Add the pointer to the member vector for this StripInfo
m_Degenerates.emplace_back( tempFace );
// END EPIC MOD: Fix memory leak
forwardFaces.emplace_back(tempFace);
MarkTriangle(tempFace);
scratchIndices.emplace_back(nv0);
testnv0 = nv0;
++m_numDegenerates;
}
}
// add this to the strip
// EPIC NOTE: Is this correct? If we've just pushed back tempFace then surely this is wrong...
forwardFaces.emplace_back(nextFace);
MarkTriangle(nextFace);
// add the index
//nv0 = nv1;
//nv1 = NvStripifier::GetNextIndex(scratchIndices, nextFace);
// EPIC NOTE: This is OK in either case though...
scratchIndices.emplace_back(testnv1);
// and get the next face
// EPIC NOTE: As is this...
nv0 = testnv0;
nv1 = testnv1;
nextFace = NvStripifier::FindOtherFace(edgeInfos, nv0, nv1, nextFace);
}
// tempAllFaces is going to be forwardFaces + backwardFaces
// it's used for Unique()
NvFaceInfoVec tempAllFaces;
std::copy(std::begin(forwardFaces), std::end(forwardFaces), std::back_inserter(tempAllFaces));
//
// reset the indices for building the strip backwards and do so
//
scratchIndices.resize(0);
scratchIndices.emplace_back(v2);
scratchIndices.emplace_back(v1);
scratchIndices.emplace_back(v0);
nv0 = v1;
nv1 = v0;
nextFace = NvStripifier::FindOtherFace(edgeInfos, nv0, nv1, m_startInfo.m_startFace);
while (nextFace != nullptr && !IsMarked(nextFace))
{
//this tests to see if a face is "unique", meaning that its vertices aren't already in the list
// so, strips which "wrap-around" are not allowed
if(!Unique(tempAllFaces, nextFace))
break;
//check to see if this next face is going to cause us to die soon
int testnv0 = nv1;
int testnv1 = NvStripifier::GetNextIndex(scratchIndices, nextFace);
NvFaceInfo* nextNextFace = NvStripifier::FindOtherFace(edgeInfos, testnv0, testnv1, nextFace);
if( (nextNextFace == nullptr) || (IsMarked(nextNextFace)) )
{
//uh, oh, we're following a dead end, try swapping
NvFaceInfo* testNextFace = NvStripifier::FindOtherFace(edgeInfos, nv0, testnv1, nextFace);
if( ((testNextFace != nullptr) && !IsMarked(testNextFace)) )
{
//we only swap if it buys us something
//add a "fake" degenerate face
NvFaceInfo* tempFace = new NvFaceInfo(nv0, nv1, nv0);
// START EPIC MOD: Fix memory leak
// We need to store this pointer to avoid a memory leak from the experiments that didn't make it. Only the experiment
// that passes has its degenerates deleted. Add the pointer to the member vector for this StripInfo - Paul Fotheringham
m_Degenerates.emplace_back( tempFace );
// END EPIC MOD: Fix memory leak
backwardFaces.emplace_back(tempFace);
MarkTriangle(tempFace);
scratchIndices.emplace_back(nv0);
testnv0 = nv0;
++m_numDegenerates;
}
}
// add this to the strip
backwardFaces.emplace_back(nextFace);
//this is just so Unique() will work
tempAllFaces.emplace_back(nextFace);
MarkTriangle(nextFace);
// add the index
//nv0 = nv1;
//nv1 = NvStripifier::GetNextIndex(scratchIndices, nextFace);
scratchIndices.emplace_back(testnv1);
// and get the next face
nv0 = testnv0;
nv1 = testnv1;
nextFace = NvStripifier::FindOtherFace(edgeInfos, nv0, nv1, nextFace);
}
// Combine the forward and backwards stripification lists and put into our own face vector
Combine(forwardFaces, backwardFaces);
}
///////////////////////////////////////////////////////////////////////////////////////////
// Combine()
//
// Combines the two input face vectors and puts the result into m_faces
//
void NvStripInfo::Combine(const NvFaceInfoVec &forward, const NvFaceInfoVec &backward){
// add backward faces
ptrdiff_t numFaces = (ptrdiff_t)backward.size();
for (ptrdiff_t i = numFaces - 1; i >= 0; i--)
m_faces.emplace_back(backward[i]);
// add forward faces
for (auto &f : forward)
m_faces.emplace_back(f);
}
///////////////////////////////////////////////////////////////////////////////////////////
// SharesEdge()
//
// Returns true if the input face and the current strip share an edge
//
bool NvStripInfo::SharesEdge(const NvFaceInfo* faceInfo, NvEdgeInfoVec &edgeInfos) const
{
//check v0->v1 edge
NvEdgeInfo* currEdge = NvStripifier::FindEdgeInfo(edgeInfos, faceInfo->m_v0, faceInfo->m_v1);
if(IsInStrip(currEdge->m_face0) || IsInStrip(currEdge->m_face1))
return true;
//check v1->v2 edge
currEdge = NvStripifier::FindEdgeInfo(edgeInfos, faceInfo->m_v1, faceInfo->m_v2);
if(IsInStrip(currEdge->m_face0) || IsInStrip(currEdge->m_face1))
return true;
//check v2->v0 edge
currEdge = NvStripifier::FindEdgeInfo(edgeInfos, faceInfo->m_v2, faceInfo->m_v0);
if(IsInStrip(currEdge->m_face0) || IsInStrip(currEdge->m_face1))
return true;
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////
// CommitStrips()
//
// "Commits" the input strips by setting their m_experimentId to -1 and adding to the allStrips
// vector
//
void NvStripifier::CommitStrips(NvStripInfoVec &allStrips, const NvStripInfoVec &strips)
{
// Iterate through strips
for (auto *strip : strips){
// Tell the strip that it is now real
strip->m_experimentId = -1;
// add to the list of real strips
allStrips.emplace_back(strip);
// Iterate through the faces of the strip
// Tell the faces of the strip that they belong to a real strip now
for (auto &f : strip->m_faces)
{
strip->MarkTriangle(f);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// FindTraversal()
//
// Finds the next face to start the next strip on.
//
bool NvStripifier::FindTraversal(NvFaceInfoVec &,
NvEdgeInfoVec &edgeInfos,
NvStripInfo *strip,
NvStripStartInfo &startInfo){
// if the strip was v0->v1 on the edge, then v1 will be a vertex in the next edge.
int v = (strip->m_startInfo.m_toV1 ? strip->m_startInfo.m_startEdge->m_v1 : strip->m_startInfo.m_startEdge->m_v0);
NvFaceInfo *untouchedFace = nullptr;
NvEdgeInfo *edgeIter = edgeInfos[v];
while (edgeIter != nullptr){
NvFaceInfo *face0 = edgeIter->m_face0;
NvFaceInfo *face1 = edgeIter->m_face1;
if ((face0 != nullptr && !strip->IsInStrip(face0)) && face1 != nullptr && !strip->IsMarked(face1))
{
untouchedFace = face1;
break;
}
if ((face1 != nullptr && !strip->IsInStrip(face1)) && face0 != nullptr && !strip->IsMarked(face0)){
untouchedFace = face0;
break;
}
// find the next edgeIter
edgeIter = (edgeIter->m_v0 == v ? edgeIter->m_nextV0 : edgeIter->m_nextV1);
}
startInfo.m_startFace = untouchedFace;
startInfo.m_startEdge = edgeIter;
if (edgeIter != nullptr)
{
if(strip->SharesEdge(startInfo.m_startFace, edgeInfos))
startInfo.m_toV1 = (edgeIter->m_v0 == v); //note! used to be m_v1
else
startInfo.m_toV1 = (edgeIter->m_v1 == v);
}
return (startInfo.m_startFace != nullptr);
}
////////////////////////////////////////////////////////////////////////////////////////
// RemoveSmallStrips()
//
// allStrips is the whole strip vector...all small strips will be deleted from this list, to avoid leaking mem
// allBigStrips is an out parameter which will contain all strips above minStripLength
// faceList is an out parameter which will contain all faces which were removed from the striplist
//
void NvStripifier::RemoveSmallStrips(NvStripInfoVec& allStrips, NvStripInfoVec& allBigStrips, NvFaceInfoVec& faceList)
{
faceList.clear();
allBigStrips.clear(); //make sure these are empty
NvFaceInfoVec tempFaceList;
for(auto &as : allStrips)
{
if(as->m_faces.size() < minStripLength)
{
//strip is too small, add faces to faceList
for(auto &f : as->m_faces)
tempFaceList.emplace_back(f);
//and free memory
delete as;
}
else
{
allBigStrips.emplace_back(as);
}
}
if(tempFaceList.size())
{
std::unique_ptr<bool[]> bVisitedList = std::make_unique<bool[]>(tempFaceList.size());
memset(bVisitedList.get(), 0, tempFaceList.size()*sizeof(bool));
std::unique_ptr<VertexCache> vcache = std::make_unique<VertexCache>(cacheSize);
int numHits;
std::ptrdiff_t bestIndex = 0;
while(1)
{
int bestNumHits = -1;
//find best face to add next, given the current cache
for(std::ptrdiff_t i = 0; i < static_cast<ptrdiff_t>(tempFaceList.size()); i++)
{
if(bVisitedList[i])
continue;
numHits = CalcNumHitsFace(vcache.get(), tempFaceList[i]);
if(numHits > bestNumHits)
{
bestNumHits = numHits;
bestIndex = i;
}
}
if(bestNumHits == -1)
break;
bVisitedList[bestIndex] = true;
UpdateCacheFace(vcache.get(), tempFaceList[bestIndex]);
faceList.emplace_back(tempFaceList[bestIndex]);
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// NextIsCW()
//
// Returns true if the next face should be ordered in CW fashion
//
bool NvStripifier::NextIsCW(const int numIndices)
{
return ((numIndices % 2) == 0);
}
///////////////////////////////////////////////////////////////////////////////////////////
// IsCW()
//
// Returns true if the face is ordered in CW fashion
//
bool NvStripifier::IsCW(NvFaceInfo *faceInfo, int v0, int v1)
{
if (faceInfo->m_v0 == v0)
return (faceInfo->m_v1 == v1);
if (faceInfo->m_v1 == v0)
return (faceInfo->m_v2 == v1);
return (faceInfo->m_v0 == v1);
}
////////////////////////////////////////////////////////////////////////////////////////
// CreateStrips()
//
// Generates actual strips from the list-in-strip-order.
//
void NvStripifier::CreateStrips(const NvStripInfoVec& allStrips, IntVec& stripIndices,
const bool bStitchStrips, size_t& numSeparateStrips)
{
assert(numSeparateStrips == 0);
NvFaceInfo tLastFace(0, 0, 0);
NvFaceInfo tPrevStripLastFace(0, 0, 0);
size_t nStripCount = allStrips.size();
assert(nStripCount > 0);
//we infer the cw/ccw ordering depending on the number of indices
//this is screwed up by the fact that we insert -1s to denote changing strips
//this is to account for that
int accountForNegatives = 0;
for (size_t i = 0; i < nStripCount; i++)
{
NvStripInfo *strip = allStrips[i];
size_t nStripFaceCount = strip->m_faces.size();
assert(nStripFaceCount > 0);
// Handle the first face in the strip
{
NvFaceInfo tFirstFace(strip->m_faces[0]->m_v0, strip->m_faces[0]->m_v1, strip->m_faces[0]->m_v2);
// If there is a second face, reorder vertices such that the
// unique vertex is first
if (nStripFaceCount > 1)
{
int nUnique = NvStripifier::GetUniqueVertexInB(strip->m_faces[1], &tFirstFace);
if (nUnique == tFirstFace.m_v1)
{
std::swap(tFirstFace.m_v0, tFirstFace.m_v1);
}
else if (nUnique == tFirstFace.m_v2)
{
std::swap(tFirstFace.m_v0, tFirstFace.m_v2);
}
// If there is a third face, reorder vertices such that the
// shared vertex is last
if (nStripFaceCount > 2)
{
if(IsDegenerate(strip->m_faces[1]))
{
int pivot = strip->m_faces[1]->m_v1;
if(tFirstFace.m_v1 == pivot)
{
std::swap(tFirstFace.m_v1, tFirstFace.m_v2);
}
}