-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangulation.cpp
More file actions
1897 lines (1750 loc) · 55.3 KB
/
Triangulation.cpp
File metadata and controls
1897 lines (1750 loc) · 55.3 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 <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <clocale>
#include <cassert>
#include <set>
#include <deque>
#include "Triangulation.h"
#include "BinHeap.h"
using namespace R3Graph;
static const int TEST_MAX_TRIANGLES = 0;
static const int TEST_MAX_EDGES = 1000;
// Parsing of XML-file
static bool extractTag(FILE* f, int& tag, bool& closing);
static bool extractInt(FILE* f, int& value);
static bool extractDouble(FILE* f, double& value);
static int findTag(const char* tag);
static const char* const TAGS[] = {
"triangulation", // 0
"vertices", // 1
"vertex", // 2
"coord", // 3
"normal", // 4
"triangles", // 5
"triangle", // 6
0
};
static const int TAG_TRIANGULATION = 0;
static const int TAG_VERTICES = 1;
static const int TAG_VERTEX = 2;
static const int TAG_COORD = 3;
static const int TAG_NORMAL = 4;
static const int TAG_TRIANGLES = 5;
static const int TAG_TRIANGLE = 6;
static bool writeTag(
FILE* f, int tagID, int level, bool closing = false
);
static bool write3Int(
FILE* f, int x, int y, int z
);
static bool write3Double(
FILE* f, double x, double y, double z
);
static bool writeIndent(
FILE* f, int level
);
void Triangulation::computeFramingBox() {
if (vertices.size() == 0)
return;
R3Point minPoint = vertices[0].point;
R3Point maxPoint = vertices[0].point;
for (size_t i = 0; i < vertices.size(); ++i) {
if (vertices[i].point.x < minPoint.x)
minPoint.x = vertices[i].point.x;
if (vertices[i].point.x > maxPoint.x)
maxPoint.x = vertices[i].point.x;
if (vertices[i].point.y < minPoint.y)
minPoint.y = vertices[i].point.y;
if (vertices[i].point.y > maxPoint.y)
maxPoint.y = vertices[i].point.y;
if (vertices[i].point.z < minPoint.z)
minPoint.z = vertices[i].point.z;
if (vertices[i].point.z > maxPoint.z)
maxPoint.z = vertices[i].point.z;
}
box.origin = minPoint;
box.size = maxPoint - minPoint;
}
void Triangulation::clear() {
vertices.clear();
triangles.clear();
adjacentTrianglesCalculated = false;
delete adjacentTriangles; adjacentTriangles = 0;
trianglesOfVerticesCalculated = false;
trianglesOfVertices.clear();
linkedComponentsCalculated = false;
delete linkedComponents; linkedComponents = 0;
}
void Triangulation::orientate() {
for (size_t i = 0; i < triangles.size(); ++i) {
Triangle& t = triangles.at(i);
/*???
Vertex v0 = vertices.at(t.indices[0]);
Vertex v1 = vertices.at(t.indices[1]);
Vertex v2 = vertices.at(t.indices[2]);
R3Point p0 = v0.point;
R3Point p1 = v1.point;
R3Point p2 = v2.point;
R3Vector meanN = (v0.normal + v1.normal + v2.normal)*(1./3.);
R3Vector N = (p1 - p0).vectorProduct(p2 - p0);
if (N.scalarProduct(meanN) < 0.) {
t.invert();
}
???*/
t.invert(); //??? Nessary because of the error in Skala code:
//??? must be opposite orientation of triangles
}
}
static const char * const xmlFirstLine =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
bool Triangulation::save(const char* path) const {
setlocale(LC_ALL, "C");
FILE* f = fopen(path, "w");
if (f == NULL) {
perror("Cannot open a triangulationfile for writing");
return false;
}
if (fprintf(f, "%s\n", xmlFirstLine) <= 0) {
LError: ;
perror("Write error");
fclose(f);
return false;
}
int level = 0;
if (!writeTag(f, TAG_TRIANGULATION, level, false))
goto LError;
++level;
if (!writeTag(f, TAG_VERTICES, level, false))
goto LError;
++level;
for (size_t i = 0; i < vertices.size(); ++i) {
const Vertex& v = vertices[i];
if (!writeTag(f, TAG_VERTEX, level, false))
goto LError;
++level;
if (
!writeIndent(f, level) ||
!writeTag(f, TAG_COORD, (-1), false)
)
goto LError;
if (!write3Double(f, v.point.x, v.point.y, v.point.z))
goto LError;
if (!writeTag(f, TAG_COORD, (-1), true))
goto LError;
if (
!writeIndent(f, level) ||
!writeTag(f, TAG_NORMAL, (-1), false)
)
goto LError;
if (!write3Double(f, v.normal.x, v.normal.y, v.normal.z))
goto LError;
if (!writeTag(f, TAG_NORMAL, (-1), true))
goto LError;
--level;
if (!writeTag(f, TAG_VERTEX, level, true))
goto LError;
}
--level;
if (!writeTag(f, TAG_VERTICES, level, true))
goto LError;
if (!writeTag(f, TAG_TRIANGLES, level, false))
goto LError;
++level;
for (size_t i = 0; i < triangles.size(); ++i) {
const Triangle& t = triangles[i];
if (
!writeIndent(f, level) ||
!writeTag(f, TAG_TRIANGLE, (-1), false)
)
goto LError;
if (!write3Int(f, t.indices[0], t.indices[1], t.indices[2]))
goto LError;
if (!writeTag(f, TAG_TRIANGLE, (-1), true))
goto LError;
}
--level;
if (!writeTag(f, TAG_TRIANGLES, level, true))
goto LError;
--level;
if (!writeTag(f, TAG_TRIANGULATION, level, true))
goto LError;
assert(level == 0);
fclose(f);
return true;
}
// level defines identation
// if level < 0, then continue the current line and
// write '\n' for closing tag
static bool writeTag(
FILE* f, int tagID, int level, bool closing // = false
) {
if (level >= 0) {
if (!writeIndent(f, level))
return false;
}
if (fprintf(f, "<") <= 0)
return false;
if (closing) {
if (fprintf(f, "/") <= 0)
return false;
}
if (fprintf(f, "%s>", TAGS[tagID]) <= 0)
return false;
if (level >= 0 || closing) {
if (fprintf(f, "\n") <= 0)
return false;
}
return true;
}
static bool write3Int(
FILE* f, int x, int y, int z
) {
if (fprintf(f, "%d %d %d", x, y, z) <= 0)
return false;
return true;
}
static bool write3Double(
FILE* f, double x, double y, double z
) {
if (fprintf(f, "%f %f %f", x, y, z) <= 0)
return false;
return true;
}
static bool writeIndent(
FILE* f, int level
) {
// Indentation
for (int i = 0; i < level; ++i) {
if (fprintf(f, " ") <= 0)
return false;
}
return true;
}
static int findTag(const char* tag) {
const char* line = TAGS[0];
int id = 0;
while (line != 0) {
if (strcmp(line, tag) == 0)
return id;
++id;
line = TAGS[id];
}
return (-1);
}
static bool extractTag(FILE* f, int& tagID, bool& closing) {
// Using Finite State Machine
char tag[64];
int tagLen = 0;
int state = 0;
bool tagEndFound = false;
closing = false;
int c;
while (!tagEndFound) {
c = fgetc(f);
if (c == EOF)
return false;
if (state == 0) { // Looking for the tag beginning
if (c != '<') {
continue;
} else {
state = 1; // Tag beginning is found
closing = false;
tagLen = 0;
}
} else if (state == 1) { // Tag beginning "<" is found
if (c == '/') {
closing = true;
state = 2; // Name must follow
} else if (isalpha(c)) {
tag[tagLen] = (char) c;
++tagLen;
state = 2; // Name beginning is found
} else {
state = 0; // Go to initial state
closing = false;
tagLen = 0;
}
} else if (state == 2) { // Reading tag name
if (isalpha(c)) {
if (tagLen < 62) {
tag[tagLen] = (char) c;
++tagLen;
}
} else {
if (c == '>') {
if (tagLen > 0) {
tagEndFound = true;
break;
} else {
state = 0; // Go to initial state
closing = false;
tagLen = 0;
}
}
state = 3; // Name end is found
}
} else if (state == 3) { // Looking for the tag end ">"
if (c == '>') {
if (tagLen > 0) {
tagEndFound = true;
break;
} else {
state = 0; // Go to initial state
closing = false;
tagLen = 0;
}
}
}
}
if (!tagEndFound || tagLen == 0)
return false;
tag[tagLen] = 0;
int id = findTag(tag);
if (id < 0)
return false;
tagID = id;
return true;
}
static bool extractInt(FILE* f, int& value) {
char line[64];
int lineLen = 0;
int c;
while (true) {
c = fgetc(f);
if (c == EOF)
return false;
else if (isdigit(c) || c == '+' || c == '-') {
break;
}
}
// The beginning of integer constant is found
while (true) {
if (lineLen < 62) {
line[lineLen] = (char) c;
++lineLen;
}
c = fgetc(f);
if (c == EOF)
break;
if (!isdigit(c)) {
break;
}
}
if (c != EOF)
ungetc(c, f);
line[lineLen] = 0;
value = atoi(line);
return true;
}
static bool extractDouble(FILE* f, double& value) {
char line[64];
int lineLen = 0;
int c;
while (true) {
c = fgetc(f);
if (c == EOF)
return false;
else if (
isdigit(c) ||
c == '+' || c == '-' ||
c == '.'
) {
break;
}
}
// The beginning of double constant is found
while (true) {
if (lineLen < 62) {
line[lineLen] = (char) c;
++lineLen;
}
c = fgetc(f);
if (c == EOF)
break;
if (
!isdigit(c) &&
c != '+' && c != '-' &&
c != '.' && c != 'e' && c != 'E'
) {
break;
}
}
if (c != EOF)
ungetc(c, f);
line[lineLen] = 0;
value = atof(line);
return true;
}
bool Triangulation::load(const char *path) {
setlocale(LC_ALL, "C");
clear();
FILE* f = fopen(path, "r");
if (f == NULL)
return false;
int tagID = (-1);
bool closing = false;
while (
extractTag(f, tagID, closing) &&
tagID != TAG_TRIANGULATION
);
if (tagID != TAG_TRIANGULATION) {
fclose(f);
return false;
}
while (
extractTag(f, tagID, closing) &&
tagID != TAG_VERTICES
);
if (tagID != TAG_VERTICES || closing) {
fclose(f);
return false;
}
// Load vertices
vertices.clear();
if (
!extractTag(f, tagID, closing) ||
tagID != TAG_VERTEX || closing
) {
fclose(f);
return false;
}
while (true) {
if (
!extractTag(f, tagID, closing) ||
tagID != TAG_COORD || closing
) {
fclose(f);
return false;
}
double x, y, z;
if (!extractDouble(f, x)) {
fclose(f);
return false;
}
if (!extractDouble(f, y)) {
fclose(f);
return false;
}
if (!extractDouble(f, z)) {
fclose(f);
return false;
}
if (
!extractTag(f, tagID, closing) ||
tagID != TAG_COORD || !closing
) {
fclose(f);
return false;
}
if (
!extractTag(f, tagID, closing) ||
tagID != TAG_NORMAL || closing
) {
fclose(f);
return false;
}
double nx, ny, nz;
if (!extractDouble(f, nx)) {
fclose(f);
return false;
}
if (!extractDouble(f, ny)) {
fclose(f);
return false;
}
if (!extractDouble(f, nz)) {
fclose(f);
return false;
}
if (
!extractTag(f, tagID, closing) ||
tagID != TAG_NORMAL || !closing
) {
fclose(f);
return false;
}
if (
!extractTag(f, tagID, closing) ||
tagID != TAG_VERTEX || !closing
) {
fclose(f);
return false;
}
vertices.push_back(
Triangulation::Vertex(
R3Point(x, y, z),
//... R3Vector(nx, ny, nz).normalize()
R3Vector(nx, ny, nz)
)
);
if (!extractTag(f, tagID, closing)) {
fclose(f);
return false;
}
if (tagID == TAG_VERTEX && !closing)
continue;
break;
} // end while
// Load triangles
if (
!extractTag(f, tagID, closing) ||
tagID != TAG_TRIANGLES || closing
) {
fclose(f);
return false;
}
triangles.clear();
if (
!extractTag(f, tagID, closing) ||
tagID != TAG_TRIANGLE || closing
) {
fclose(f);
return false;
}
while (true) {
int v0, v1, v2;
if (!extractInt(f, v0)) {
fclose(f);
return false;
}
if (!extractInt(f, v1)) {
fclose(f);
return false;
}
if (!extractInt(f, v2)) {
fclose(f);
return false;
}
if (
!extractTag(f, tagID, closing) ||
tagID != TAG_TRIANGLE || !closing
) {
fclose(f);
return false;
}
triangles.push_back(
Triangulation::Triangle(v0, v1, v2)
);
if (!extractTag(f, tagID, closing)) {
fclose(f);
return false;
}
if (tagID == TAG_TRIANGLE && !closing)
continue;
break;
} // end while
fclose(f);
computeFramingBox();
return true;
}
//--------------------------------------------------------------------------------------------------------------
R3Point Triangulation::center() const {
return (box.origin + box.size * 0.5);
}
//
void Triangulation::computeNormals() {
if (!trianglesOfVerticesCalculated) {
defineTrianglesOfVertices();
}
for (size_t i = 0; i < vertices.size(); ++i) {
Vertex& v = vertices.at(i);
const TrianglesOfVertex& triangs = trianglesOfVertices.at(i);
R3Vector sum(0., 0., 0.);
for (size_t t = 0; t < triangs.size(); ++t) {
const Triangle& tr = triangles.at(triangs[t]);
R3Point p0 = vertices.at(tr[0]).point;
R3Point p1 = vertices.at(tr[1]).point;
R3Point p2 = vertices.at(tr[2]).point;
R3Vector v1 = p1 - p0; v1.normalize();
R3Vector v2 = p2 - p0; v2.normalize();
R3Vector n = v1.vectorProduct(v2);
n.normalize();
sum += n;
}
if (triangs.size() > 0) {
sum *= 1. / (double)triangs.size();
}
v.normal = sum;
}
}//
//void Triangulation::refine() { // Remove double vertices
// Triangulation t;
// std::map<R3Point, int> pointIdx;
// std::map<int, int> newVertexIdx;
// int numNewVertices = 0;
// for (int i = 0; i < (int)vertices.size(); ++i) {
// const Vertex& v = vertices[i];
// if (pointIdx.count(v.point) == 0) {
// t.vertices.push_back(v);
// pointIdx[v.point] = numNewVertices;
// newVertexIdx[i] = numNewVertices;
// ++numNewVertices;
// }
// else {
// // Double point found
// newVertexIdx[i] = pointIdx[v.point];
// }
// }
// //Q_ASSERT(numNewVertices == (int)t.vertices.size());
//
// //qDebug() << "refine(): vertices=" << vertices.size() <<
// // " different vertices=" << numNewVertices << "\n";
//
// //if (numNewVertices == (int)vertices.size()) {
// // qDebug() << "refine(): no double vertices!\n";
// // return;
// //}
//
// std::set<Triangle> triangleSet;
// for (int i = 0; i < (int)triangles.size(); ++i) {
// const Triangle& tr = triangles[i];
// Triangle newTriangle(
// newVertexIdx[tr[0]],
// newVertexIdx[tr[1]],
// newVertexIdx[tr[2]]
// );
// if (triangleSet.count(newTriangle) > 0)
// continue; // The triangle is already presented, skip it
// t.triangles.push_back(newTriangle);
// }
// /*qDebug() << "refine(): triangles=" << triangles.size() <<
// " different triangles=" << t.triangles.size() << "\n";*/
//
// t.computeFramingBox();
// clear(); //???
// *this = t;
//}
void Triangulation::defineAdjacentTriangles() const {
invalidateAdjacentTriangles();
if (adjacentTriangles == 0)
adjacentTriangles = new std::vector<AdjacentTriangles>;
adjacentTriangles->resize(triangles.size());
// Adjacent triangles for each triangle
std::map<Edge, int> triangleOfEdge;
for (size_t i = 0; i < triangles.size(); ++i) {
const Triangle& triangle = triangles[i];
for (int j = 0; j < 3; ++j) {
int k = j + 1;
if (k >= 3)
k = 0;
Edge e(triangle[j], triangle[k]);
if (triangleOfEdge.count(e) == 0) {
triangleOfEdge[e] = i;
}
else {
adjacentTriangles->at(triangleOfEdge[e]).add(i);
adjacentTriangles->at(i).add(triangleOfEdge[e]);
}
}
}
adjacentTrianglesCalculated = true;
}
void Triangulation::defineTrianglesOfVertices() const {
trianglesOfEdges.clear();
trianglesOfVertices.resize(vertices.size());
for (size_t i = 0; i < trianglesOfVertices.size(); ++i)
trianglesOfVertices.at(i).clear();
// Adjacent triangles for each vertex
for (size_t i = 0; i < triangles.size(); ++i) {
const Triangle& triangle = triangles.at(i);
for (int j = 0; j < 3; ++j) {
int v = triangle[j];
trianglesOfVertices.at(v).push_back(i);
int w = triangle[(j + 1) % 3];
Edge e(v, w);
trianglesOfEdges[e].push_back(i);
}
}
trianglesOfVerticesCalculated = true;
// starsOfVertices.clear();
starsOfVertices.resize(vertices.size());
for (int v = 0; v < int(vertices.size()); ++v) {
starsOfVertices.at(v).clear();
const TrianglesOfVertex& vertexTriangles = trianglesOfVertices.at(v);
for (int t = 0; t < int(vertexTriangles.size()); ++t) {
int triangleIdx = vertexTriangles[t];
const Triangle& triangle = triangles.at(triangleIdx);
for (int j = 0; j < 3; ++j) {
int w = triangle[j];
if (w != v) {
starsOfVertices.at(v).insert(w);
}
}
}
}
ringsOfVertices.resize(vertices.size());
for (size_t v = 0; v < vertices.size(); ++v) {
computeVertexRing(v, ringsOfVertices.at(v));
}
}
//int Triangulation::defineLinkedComponents() const {
// if (!adjacentTrianglesCalculated)
// defineAdjacentTriangles();
//
// clearLinkedComponents();
//
// if (linkedComponents == 0)
// linkedComponents = new std::vector<LinkedComponent>;
//
// int numTriangles = (int)triangles.size();
// if (numTriangles == 0)
// return 0;
//
// BinHeapMin<char> heap(numTriangles);
//
// int* triangleIndex = new int[numTriangles];
// int* heapIndex = new int[numTriangles];
// memset(heapIndex, (char)(-1), numTriangles * sizeof(int));
//
// int infinity = 4;
//
// int i = 0; // Index in heap
// for (int t = 0; t < numTriangles; ++t) {
// heap.elements[i] = (char)infinity; // Infinite distance
// triangleIndex[i] = t;
// heapIndex[t] = i;
// ++i;
// }
//
// heap.indexArray = triangleIndex;
// heap.heapIndex = heapIndex;
// heap.numElems = numTriangles;
//
// int initialTriangle = 0;
// int numComponents = 0;
// int totalVisited = 0;
//
// while (heap.numElems > 0) {
// // Add new empty component to the list
// linkedComponents->push_back(LinkedComponent());
//
// int initialTriangleIndexInHeap = heap.heapIndex[initialTriangle];
// heap.elements[initialTriangleIndexInHeap] = 0; // Zero distance
// heap.bubbleUp(initialTriangleIndexInHeap);
//
// int numVisited = 0;
//
// while (heap.numElems > 0 && heap.root() < infinity) {
// //... int currentDistance = heap.root();
// int current = heap.rootIndex();
// heapIndex[current] = (-1); // Remove triangle from heap
// heap.removeRoot();
// linkedComponents->back().push_back(current);
// ++numVisited;
//
// if (heap.numElems == 0)
// break;
//
// //... int newDistance = currentDistance + 1;
// for (int j = 0; j < 3; ++j) {
// int neighbor =
// adjacentTriangles->at(current).adjacentTriangles[j];
// if (neighbor < 0)
// break;
// int neighborIndexInHeap = heapIndex[neighbor];
// if (neighborIndexInHeap < 0) // Already visited
// continue;
//
// if (heap.elements[neighborIndexInHeap] != 0) {
// heap.elements[neighborIndexInHeap] = 0; // Mark as visited
// heap.bubbleUp(neighborIndexInHeap);
// }
// }
// } // end while (heap.root() < infinity)
//
// totalVisited += numVisited;
// ++numComponents;
//
// //qDebug() <<
// // "Linked component " << numComponents <<
// // " is extracted: numTriangles=" << numVisited <<
// // " totalVisited=" << totalVisited << "\n";
//
// // Define the first unvisited triangle
// if (heap.numElems > 0)
// initialTriangle = heap.rootIndex();
// else
// initialTriangle = (-1);
// } // end while
//
// //qDebug() <<
// // "Total linked components: " << numComponents <<
// // ", triangles: " << totalVisited << "\n";
//
// //Q_ASSERT(totalVisited == numTriangles);
// //Q_ASSERT(numComponents == (int)linkedComponents->size());
//
// delete[] heapIndex;
// delete[] triangleIndex;
//
// linkedComponentsCalculated = true;
// return numComponents;
//}
//void Triangulation::copyMaximalComponent(Triangulation& t) const {
// t.clear();
// //Q_ASSERT(linkedComponentsCalculated && linkedComponents != 0);
// if (!linkedComponentsCalculated || linkedComponents->size() == 0)
// return;
//
// // Find maximal component
// int indMax = 0;
// int numMax = (int)linkedComponents->at(0).size();
// for (int i = 1; i < (int)linkedComponents->size(); ++i) {
// if ((int)linkedComponents->at(i).size() > numMax) {
// indMax = i;
// numMax = (int)linkedComponents->at(i).size();
// }
// }
// //qDebug() << "Maximal linked component: " << numMax << " triangles.\n";
//
// t.triangles.resize(numMax);
//
// std::vector<int> vertexIndices(vertices.size());
// for (size_t i = 0; i < vertices.size(); ++i)
// vertexIndices[i] = (-1);
//
// const LinkedComponent& maxComp = linkedComponents->at(indMax);
// LinkedComponent::const_iterator i = maxComp.begin();
// int numTriangles = 0, numVert = 0;
// while (i != maxComp.end()) {
// int triangleIdx = *i;
// const Triangle& triangle = triangles.at(triangleIdx);
// Triangle newTriangle;
// for (int j = 0; j < 3; ++j) {
// int v = triangle[j];
// if (vertexIndices[v] < 0) {
// t.vertices.push_back(vertices[v]);
// vertexIndices[v] = numVert;
// ++numVert;
// }
// newTriangle[j] = vertexIndices[v];
// }
// t.triangles.at(numTriangles) = newTriangle;
// ++numTriangles;
// ++i;
// }
// //qDebug() << "Maximal linked component is copied.\n";
//}
//bool Triangulation::contains(const R3Point& p) const {
// double s = 0.;
// for (int i = 0; i < (int)triangles.size(); ++i) {
// const Triangle& tr = triangles[i];
// double a = (R3Vector ()).signedSolidAngle(
// vertices[tr[0]].point - p,
// vertices[tr[1]].point - p,
// vertices[tr[2]].point - p
// );
// s += a;
// }
// s = fabs(s); // Safety
// // s must be 4*pi for a point inside and 0 for a point outside.
// return (s > 3. * PI);
//}
void Triangulation::computeVertexRing(
int vertexIdx, VertexRing& vertexRing
) const {
assert(trianglesOfVerticesCalculated);
assert(trianglesOfVertices.size() == vertices.size());
std::multimap<int, int> ringEdges;
const TrianglesOfVertex& vertexTriangles =
trianglesOfVertices.at(vertexIdx);
int initialVertex = (-1);
for (size_t t = 0; t < vertexTriangles.size(); ++t) {
int triangleIdx = vertexTriangles[t];
const Triangle& triangle = triangles.at(triangleIdx);
int k = 0;
int triangleVertices[2];
for (int j = 0; j < 3; ++j) {
int w = triangle[j];
if (w != vertexIdx) {
assert(k < 2);
triangleVertices[k] = w;
++k;
if (initialVertex < 0)
initialVertex = w;
}
}
assert(k == 2);
ringEdges.insert(std::make_pair(
triangleVertices[0], triangleVertices[1]
));
ringEdges.insert(std::make_pair(
triangleVertices[1], triangleVertices[0]
));
}
assert(initialVertex >= 0);
std::deque<int> ring;
ring.push_back(initialVertex);
size_t ringSize = 1;
while (true) {
// Growing at the end of ring
auto range = ringEdges.equal_range(ring.back());
auto i = range.first;
while (i != range.second) {
int vNext = i->second;
if (
ring.size() <= 1 ||
(
vNext != ring[ring.size() - 2] &&
vNext != ring.front()
)
) {
ring.push_back(vNext);
break;
}
++i;
}
// Growing at the beginning of ring
range = ringEdges.equal_range(ring.front());
i = range.first;
while (i != range.second) {
int vPrev = i->second;
if (
ring.size() <= 1 ||
(
vPrev != ring[1] &&
vPrev != ring.back()
)
) {
ring.push_front(vPrev);
break;
}
++i;
}
if (ring.size() == ringSize) // No change =>
break; // end of ring construction
ringSize = ring.size(); // Continue the ring construstion
}
vertexRing.resize(ring.size());
for (size_t i = 0; i < ring.size(); ++i) {
vertexRing[i] = ring[i];
}
//assert(
// vertexRing.size() >= vertexTriangles.size()
//);