forked from ZelimDamian/geometry3Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNTMesh3.cs
1648 lines (1303 loc) · 53.3 KB
/
NTMesh3.cs
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
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace g3
{
//
// NTMesh3 is a variant of DMesh3 that supports non-manifold mesh topology.
// See DMesh3 comments for most details.
// Main change is that edges buffer only stores 2-tuple vertex pairs.
// Each edge can be connected to arbitrary number of triangle, which are
// stored in edge_triangles
//
// per-vertex UVs have been removed (perhaps temporarily)
//
// Currently poke-face and split-edge are supported, but not collapse or flip.
//
public partial class NTMesh3 : IDeformableMesh
{
public const int InvalidID = -1;
public const int NonManifoldID = -2;
public static readonly Vector3d InvalidVertex = new Vector3d(Double.MaxValue, 0, 0);
public static readonly Index3i InvalidTriangle = new Index3i(InvalidID, InvalidID, InvalidID);
public static readonly Index2i InvalidEdge = new Index2i(InvalidID, InvalidID);
RefCountVector vertices_refcount;
DVector<double> vertices;
DVector<float> normals;
DVector<float> colors;
SmallListSet vertex_edges;
RefCountVector triangles_refcount;
DVector<int> triangles;
DVector<int> triangle_edges;
DVector<int> triangle_groups;
RefCountVector edges_refcount;
DVector<int> edges;
SmallListSet edge_triangles;
int timestamp = 0;
int shape_timestamp = 0;
int max_group_id = 0;
public NTMesh3(bool bWantNormals = true, bool bWantColors = false, bool bWantTriGroups = false)
{
allocate(bWantNormals, bWantColors, bWantTriGroups);
}
public NTMesh3(MeshComponents flags) :
this( (flags & MeshComponents.VertexNormals) != 0, (flags & MeshComponents.VertexColors) != 0,
(flags & MeshComponents.FaceGroups) != 0 )
{
}
private void allocate(bool bWantNormals, bool bWantColors, bool bWantTriGroups)
{
vertices = new DVector<double>();
if (bWantNormals)
normals = new DVector<float>();
if (bWantColors)
colors = new DVector<float>();
vertex_edges = new SmallListSet();
vertices_refcount = new RefCountVector();
triangles = new DVector<int>();
triangle_edges = new DVector<int>();
triangles_refcount = new RefCountVector();
if (bWantTriGroups)
triangle_groups = new DVector<int>();
max_group_id = 0;
edges = new DVector<int>();
edges_refcount = new RefCountVector();
edge_triangles = new SmallListSet();
}
public NTMesh3(NTMesh3 copy) {
Copy(copy, true, true);
}
public void Copy(NTMesh3 copy, bool bNormals = true, bool bColors = true)
{
vertices = new DVector<double>(copy.vertices);
normals = (bNormals && copy.normals != null) ? new DVector<float>(copy.normals) : null;
colors = (bColors && copy.colors != null) ? new DVector<float>(copy.colors) : null;
vertices_refcount = new RefCountVector(copy.vertices_refcount);
vertex_edges = new SmallListSet(copy.vertex_edges);
triangles = new DVector<int>(copy.triangles);
triangle_edges = new DVector<int>(copy.triangle_edges);
triangles_refcount = new RefCountVector(copy.triangles_refcount);
if (copy.triangle_groups != null)
triangle_groups = new DVector<int>(copy.triangle_groups);
max_group_id = copy.max_group_id;
edges = new DVector<int>(copy.edges);
edges_refcount = new RefCountVector(copy.edges_refcount);
edge_triangles = new SmallListSet(copy.edge_triangles);
}
public NTMesh3(DMesh3 copy)
{
allocate(copy.HasVertexNormals, copy.HasVertexColors, copy.HasTriangleGroups);
int[] mapV = new int[copy.MaxVertexID];
foreach (int vid in copy.VertexIndices())
mapV[vid] = AppendVertex(copy.GetVertex(vid));
foreach ( Index3i tri in copy.Triangles()) {
AppendTriangle(mapV[tri.a], mapV[tri.b], mapV[tri.c]);
}
}
void updateTimeStamp(bool bShapeChange) {
timestamp++;
if (bShapeChange)
shape_timestamp++;
}
public int Timestamp {
get { return timestamp; }
}
public int ShapeTimestamp {
get { return shape_timestamp; }
}
// IMesh impl
public int VertexCount {
get { return vertices_refcount.count; }
}
public int TriangleCount {
get { return triangles_refcount.count; }
}
public int EdgeCount {
get { return edges_refcount.count; }
}
// these values are (max_used+1), ie so an iteration should be < MaxTriangleID, not <=
public int MaxVertexID {
get { return vertices_refcount.max_index; }
}
public int MaxTriangleID {
get { return triangles_refcount.max_index; }
}
public int MaxEdgeID {
get { return edges_refcount.max_index; }
}
public int MaxGroupID {
get { return max_group_id; }
}
public bool HasVertexColors { get { return colors != null; } }
public bool HasVertexNormals { get { return normals != null; } }
public bool HasVertexUVs { get { return false; } }
public bool HasTriangleGroups { get { return triangle_groups != null; } }
public MeshComponents Components {
get {
MeshComponents c = 0;
if (normals != null) c |= MeshComponents.VertexNormals;
if (colors != null) c |= MeshComponents.VertexColors;
if (triangle_groups != null) c |= MeshComponents.FaceGroups;
return c;
}
}
// info
public bool IsVertex(int vID) {
return vertices_refcount.isValid(vID);
}
public bool IsTriangle(int tID) {
return triangles_refcount.isValid(tID);
}
public bool IsEdge(int eID) {
return edges_refcount.isValid(eID);
}
// getters
public Vector3d GetVertex(int vID) {
debug_check_is_vertex(vID);
int i = 3 * vID;
return new Vector3d(vertices[i], vertices[i + 1], vertices[i + 2]);
}
public Vector3f GetVertexf(int vID) {
debug_check_is_vertex(vID);
int i = 3 * vID;
return new Vector3f((float)vertices[i], (float)vertices[i + 1], (float)vertices[i + 2]);
}
public void SetVertex(int vID, Vector3d vNewPos) {
Debug.Assert(vNewPos.IsFinite); // this will really catch a lot of bugs...
debug_check_is_vertex(vID);
int i = 3*vID;
vertices[i] = vNewPos.x; vertices[i+1] = vNewPos.y; vertices[i+2] = vNewPos.z;
updateTimeStamp(true);
}
public Vector3f GetVertexNormal(int vID) {
if (normals == null) {
return Vector3f.AxisY;
} else {
debug_check_is_vertex(vID);
int i = 3 * vID;
return new Vector3f(normals[i], normals[i + 1], normals[i + 2]);
}
}
public Vector2f GetVertexUV(int i) {
return Vector2f.Zero;
}
public void SetVertexNormal(int vID, Vector3f vNewNormal) {
if ( HasVertexNormals ) {
debug_check_is_vertex(vID);
int i = 3*vID;
normals[i] = vNewNormal.x; normals[i+1] = vNewNormal.y; normals[i+2] = vNewNormal.z;
updateTimeStamp(false);
}
}
public Vector3f GetVertexColor(int vID) {
if (colors == null) {
return Vector3f.One;
} else {
debug_check_is_vertex(vID);
int i = 3 * vID;
return new Vector3f(colors[i], colors[i + 1], colors[i + 2]);
}
}
public void SetVertexColor(int vID, Vector3f vNewColor) {
if ( HasVertexColors ) {
debug_check_is_vertex(vID);
int i = 3*vID;
colors[i] = vNewColor.x; colors[i+1] = vNewColor.y; colors[i+2] = vNewColor.z;
updateTimeStamp(false);
}
}
public bool GetVertex(int vID, ref NewVertexInfo vinfo, bool bWantNormals, bool bWantColors, bool bWantUVs)
{
if (vertices_refcount.isValid(vID) == false)
return false;
vinfo.v.Set(vertices[3 * vID], vertices[3 * vID + 1], vertices[3 * vID + 2]);
vinfo.bHaveN = vinfo.bHaveUV = vinfo.bHaveC = false;
if (HasVertexColors && bWantNormals) {
vinfo.bHaveN = true;
vinfo.n.Set(normals[3 * vID], normals[3 * vID + 1], normals[3 * vID + 2]);
}
if (HasVertexColors && bWantColors) {
vinfo.bHaveC = true;
vinfo.c.Set(colors[3 * vID], colors[3 * vID + 1], colors[3 * vID + 2]);
}
return true;
}
public int GetVtxEdgeCount(int vID) {
return vertices_refcount.isValid(vID) ? vertex_edges.Count(vID) : -1;
}
public int GetMaxVtxEdgeCount() {
int max = 0;
foreach (int vid in vertices_refcount)
max = Math.Max(max, vertex_edges.Count(vid));
return max;
}
public NewVertexInfo GetVertexAll(int i) {
NewVertexInfo vi = new NewVertexInfo();
vi.v = GetVertex(i);
if ( HasVertexNormals ) {
vi.bHaveN = true;
vi.n = GetVertexNormal(i);
} else
vi.bHaveN = false;
if ( HasVertexColors ) {
vi.bHaveC = true;
vi.c = GetVertexColor(i);
} else
vi.bHaveC = false;
vi.bHaveUV = false;
return vi;
}
public Index3i GetTriangle(int tID) {
debug_check_is_triangle(tID);
int i = 3 * tID;
return new Index3i(triangles[i], triangles[i + 1], triangles[i + 2]);
}
public Index3i GetTriEdges(int tID) {
debug_check_is_triangle(tID);
int i = 3 * tID;
return new Index3i(triangle_edges[i], triangle_edges[i + 1], triangle_edges[i + 2]);
}
public int GetTriEdge(int tid, int j) {
debug_check_is_triangle(tid);
return triangle_edges[3*tid+j];
}
public IEnumerable<int> TriTrianglesItr(int tID) {
if (triangles_refcount.isValid(tID)) {
int tei = 3 * tID;
for (int j = 0; j < 3; ++j) {
int eid = triangle_edges[tei + j];
foreach ( int nbr_t in edge_triangles.ValueItr(eid) ) {
if (nbr_t != tID)
yield return nbr_t;
}
}
}
}
public int GetTriangleGroup(int tID) {
return (triangle_groups == null) ? -1
: ( triangles_refcount.isValid(tID) ? triangle_groups[tID] : 0 );
}
public void SetTriangleGroup(int tid, int group_id) {
if ( triangle_groups != null ) {
debug_check_is_triangle(tid);
triangle_groups[tid] = group_id;
max_group_id = Math.Max(max_group_id, group_id+1);
updateTimeStamp(false);
}
}
public int AllocateTriangleGroup() {
return max_group_id++;
}
public void GetTriVertices(int tID, ref Vector3d v0, ref Vector3d v1, ref Vector3d v2) {
int ai = 3 * triangles[3 * tID];
v0.x = vertices[ai]; v0.y = vertices[ai + 1]; v0.z = vertices[ai + 2];
int bi = 3 * triangles[3 * tID + 1];
v1.x = vertices[bi]; v1.y = vertices[bi + 1]; v1.z = vertices[bi + 2];
int ci = 3 * triangles[3 * tID + 2];
v2.x = vertices[ci]; v2.y = vertices[ci + 1]; v2.z = vertices[ci + 2];
}
public Vector3d GetTriVertex(int tid, int j) {
int a = triangles[3 * tid + j];
return new Vector3d(vertices[3 * a], vertices[3 * a + 1], vertices[3 * a + 2]);
}
public Vector3d GetTriNormal(int tID)
{
Vector3d v0 = Vector3d.Zero, v1 = Vector3d.Zero, v2 = Vector3d.Zero;
GetTriVertices(tID, ref v0, ref v1, ref v2);
return MathUtil.Normal(ref v0, ref v1, ref v2);
}
public double GetTriArea(int tID)
{
Vector3d v0 = Vector3d.Zero, v1 = Vector3d.Zero, v2 = Vector3d.Zero;
GetTriVertices(tID, ref v0, ref v1, ref v2);
return MathUtil.Area(ref v0, ref v1, ref v2);
}
/// <summary>
/// Compute triangle normal, area, and centroid all at once. Re-uses vertex
/// lookups and computes normal & area simultaneously. *However* does not produce
/// the same normal/area as separate calls, because of this.
/// </summary>
public void GetTriInfo(int tID, out Vector3d normal, out double fArea, out Vector3d vCentroid)
{
Vector3d v0 = Vector3d.Zero, v1 = Vector3d.Zero, v2 = Vector3d.Zero;
GetTriVertices(tID, ref v0, ref v1, ref v2);
vCentroid = (1.0 / 3.0) * (v0 + v1 + v2);
normal = MathUtil.FastNormalArea(ref v0, ref v1, ref v2, out fArea);
}
public AxisAlignedBox3d GetTriBounds(int tID)
{
int vi = 3 * triangles[3 * tID];
double x = vertices[vi], y = vertices[vi + 1], z = vertices[vi + 2];
double minx = x, maxx = x, miny = y, maxy = y, minz = z, maxz = z;
for (int i = 1; i < 3; ++i) {
vi = 3 * triangles[3 * tID + i];
x = vertices[vi]; y = vertices[vi + 1]; z = vertices[vi + 2];
if (x < minx) minx = x; else if (x > maxx) maxx = x;
if (y < miny) miny = y; else if (y > maxy) maxy = y;
if (z < minz) minz = z; else if (z > maxz) maxz = z;
}
return new AxisAlignedBox3d(minx, miny, minz, maxx, maxy, maxz);
}
public Frame3f GetTriFrame(int tID, int nEdge = 0)
{
int ti = 3 * tID;
int a = triangles[ti + (nEdge % 3)];
int b = triangles[ti + ((nEdge+1) % 3)];
int c = triangles[ti + ((nEdge+2) % 3)];
Vector3d v0 = new Vector3d(vertices[3 * a], vertices[3 * a + 1], vertices[3 * a + 2]);
Vector3d v1 = new Vector3d(vertices[3 * b], vertices[3 * b + 1], vertices[3 * b + 2]);
Vector3d v2 = new Vector3d(vertices[3 * c], vertices[3 * c + 1], vertices[3 * c + 2]);
Vector3f edge = (Vector3f)(v1 - v0).Normalized;
Vector3f normal = (Vector3f)MathUtil.Normal(ref v0, ref v1, ref v2);
Vector3f other = edge.Cross(normal);
Vector3f center = (Vector3f)(v0 + v1 + v2) / 3;
return new Frame3f(center, edge, other, normal);
}
public Index2i GetEdgeV(int eID) {
debug_check_is_edge(eID);
int i = 2 * eID;
return new Index2i(edges[i], edges[i + 1]);
}
public bool GetEdgeV(int eID, ref Vector3d a, ref Vector3d b) {
debug_check_is_edge(eID);
int iv0 = 3 * edges[2 * eID];
a.x = vertices[iv0]; a.y = vertices[iv0 + 1]; a.z = vertices[iv0 + 2];
int iv1 = 3 * edges[2 * eID + 1];
b.x = vertices[iv1]; b.y = vertices[iv1 + 1]; b.z = vertices[iv1 + 2];
return true;
}
public IEnumerable<int> EdgeTrianglesItr(int eID)
{
return edge_triangles.ValueItr(eID);
}
public int EdgeTrianglesCount(int eID)
{
return edge_triangles.Count(eID);
}
// return same indices as GetEdgeV, but oriented based on attached triangle
public Index2i GetOrientedBoundaryEdgeV(int eID)
{
if ( edges_refcount.isValid(eID) && edge_is_boundary(eID) ) {
int ei = 2 * eID;
int a = edges[ei], b = edges[ei + 1];
int ti = edge_triangles.First(eID);
Index3i tri = new Index3i(triangles[ti], triangles[ti + 1], triangles[ti + 2]);
int ai = IndexUtil.find_edge_index_in_tri(a, b, ref tri);
return new Index2i(tri[ai], tri[(ai + 1) % 3]);
}
Util.gDevAssert(false);
return InvalidEdge;
}
// mesh-building
public int AppendVertex(Vector3d v) {
return AppendVertex(new NewVertexInfo() {
v = v, bHaveC = false, bHaveUV = false, bHaveN = false
});
}
public int AppendVertex(NewVertexInfo info)
{
int vid = vertices_refcount.allocate();
int i = 3*vid;
vertices.insert(info.v[2], i + 2);
vertices.insert(info.v[1], i + 1);
vertices.insert(info.v[0], i);
if ( normals != null ) {
Vector3f n = (info.bHaveN) ? info.n : Vector3f.AxisY;
normals.insert(n[2], i + 2);
normals.insert(n[1], i + 1);
normals.insert(n[0], i);
}
if ( colors != null ) {
Vector3f c = (info.bHaveC) ? info.c : Vector3f.One;
colors.insert(c[2], i + 2);
colors.insert(c[1], i + 1);
colors.insert(c[0], i);
}
allocate_vertex_edges_list(vid);
updateTimeStamp(true);
return vid;
}
public int AppendTriangle(int v0, int v1, int v2, int gid = -1) {
return AppendTriangle(new Index3i(v0, v1, v2), gid);
}
public int AppendTriangle(Index3i tv, int gid = -1) {
if (IsVertex(tv[0]) == false || IsVertex(tv[1]) == false || IsVertex(tv[2]) == false) {
Util.gDevAssert(false);
return InvalidID;
}
if (tv[0] == tv[1] || tv[0] == tv[2] || tv[1] == tv[2]) {
Util.gDevAssert(false);
return InvalidID;
}
// look up edges.
int e0 = find_edge(tv[0], tv[1]);
int e1 = find_edge(tv[1], tv[2]);
int e2 = find_edge(tv[2], tv[0]);
// now safe to insert triangle
int tid = triangles_refcount.allocate();
int i = 3*tid;
triangles.insert(tv[2], i + 2);
triangles.insert(tv[1], i + 1);
triangles.insert(tv[0], i);
if (triangle_groups != null) {
triangle_groups.insert(gid, tid);
max_group_id = Math.Max(max_group_id, gid+1);
}
// increment ref counts and update/create edges
vertices_refcount.increment(tv[0]);
vertices_refcount.increment(tv[1]);
vertices_refcount.increment(tv[2]);
add_tri_edge(tid, tv[0], tv[1], 0, e0);
add_tri_edge(tid, tv[1], tv[2], 1, e1);
add_tri_edge(tid, tv[2], tv[0], 2, e2);
updateTimeStamp(true);
return tid;
}
// helper fn for above, just makes code cleaner
void add_tri_edge(int tid, int v0, int v1, int j, int eid)
{
if (eid != InvalidID) {
edge_triangles.Insert(eid, tid);
triangle_edges.insert(eid, 3 * tid + j);
} else {
eid = add_edge(v0, v1, tid);
triangle_edges.insert(eid, 3 * tid + j);
}
}
public void EnableVertexNormals(Vector3f initial_normal)
{
if (HasVertexNormals)
return;
normals = new DVector<float>();
int NV = MaxVertexID;
normals.resize(3*NV);
for (int i = 0; i < NV; ++i) {
int vi = 3 * i;
normals[vi] = initial_normal.x;
normals[vi + 1] = initial_normal.y;
normals[vi + 2] = initial_normal.z;
}
}
public void DiscardVertexNormals() {
normals = null;
}
public void EnableVertexColors(Vector3f initial_color)
{
if (HasVertexColors)
return;
colors = new DVector<float>();
int NV = MaxVertexID;
colors.resize(3*NV);
for (int i = 0; i < NV; ++i) {
int vi = 3 * i;
colors[vi] = initial_color.x;
colors[vi + 1] = initial_color.y;
colors[vi + 2] = initial_color.z;
}
}
public void DiscardVertexColors() {
colors= null;
}
public void EnableTriangleGroups(int initial_group = 0)
{
if (HasTriangleGroups)
return;
triangle_groups = new DVector<int>();
int NT = MaxTriangleID;
triangle_groups.resize(NT);
for (int i = 0; i < NT; ++i)
triangle_groups[i] = initial_group;
max_group_id = 0;
}
public void DiscardTriangleGroups() {
triangle_groups = null;
max_group_id = 0;
}
// iterators
public IEnumerable<int> VertexIndices() {
foreach (int vid in vertices_refcount)
yield return vid;
}
public IEnumerable<int> TriangleIndices() {
foreach (int tid in triangles_refcount)
yield return tid;
}
public IEnumerable<int> EdgeIndices() {
foreach (int eid in edges_refcount)
yield return eid;
}
public IEnumerable<int> BoundaryEdgeIndices() {
foreach ( int eid in edges_refcount ) {
if ( edge_triangles.Count(eid) == 1 )
yield return eid;
}
}
public IEnumerable<Vector3d> Vertices() {
foreach (int vid in vertices_refcount) {
int i = 3 * vid;
yield return new Vector3d(vertices[i], vertices[i + 1], vertices[i + 2]);
}
}
public IEnumerable<Index3i> Triangles() {
foreach (int tid in triangles_refcount) {
int i = 3 * tid;
yield return new Index3i(triangles[i], triangles[i + 1], triangles[i + 2]);
}
}
// queries
// linear search through edges of vA
public int FindEdge(int vA, int vB) {
debug_check_is_vertex(vA);
debug_check_is_vertex(vB);
return find_edge(vA, vB);
}
// faster than FindEdge
public int FindEdgeFromTri(int vA, int vB, int t) {
return find_edge_from_tri(vA, vB, t);
}
// [RMS] not just 2 in NTMesh...
// public Index2i GetEdgeOpposingV(int eID)
// {
//// ** it is important that verts returned maintain [c,d] order!!
//int i = 4*eID;
// int a = edges[i], b = edges[i + 1];
// int t0 = edges[i + 2], t1 = edges[i + 3];
//int c = IndexUtil.find_tri_other_vtx(a, b, triangles, t0);
// if (t1 != InvalidID) {
// int d = IndexUtil.find_tri_other_vtx(a, b, triangles, t1);
// return new Index2i(c, d);
// } else
// return new Index2i(c, InvalidID);
// }
public IEnumerable<int> VtxVerticesItr(int vID) {
if ( vertices_refcount.isValid(vID) ) {
foreach ( int eid in vertex_edges.ValueItr(vID) )
yield return edge_other_v(eid, vID);
}
}
public IEnumerable<int> VtxEdgesItr(int vID) {
if ( vertices_refcount.isValid(vID) ) {
return vertex_edges.ValueItr(vID);
}
return Enumerable.Empty<int>();
}
/// <summary>
/// Returns count of boundary edges at vertex
/// </summary>
public int VtxBoundaryEdges(int vID)
{
if ( vertices_refcount.isValid(vID) ) {
int count = 0;
foreach (int eid in vertex_edges.ValueItr(vID)) {
if (edge_triangles.Count(eid) == 1)
count++;
}
return count;
}
Debug.Assert(false);
return -1;
}
/// <summary>
/// e needs to be large enough (ie call VtxBoundaryEdges, or as large as max one-ring)
/// returns count, ie number of elements of e that were filled
/// </summary>
public int VtxAllBoundaryEdges(int vID, int[] e)
{
if (vertices_refcount.isValid(vID)) {
int count = 0;
foreach (int eid in vertex_edges.ValueItr(vID)) {
if (edge_triangles.Count(eid) == 1)
e[count++] = eid;
}
return count;
}
Debug.Assert(false);
return -1;
}
public MeshResult GetVtxTriangles(int vID, List<int> vTriangles)
{
if (!IsVertex(vID))
return MeshResult.Failed_NotAVertex;
vTriangles.Clear();
foreach (int eid in vertex_edges.ValueItr(vID)) {
foreach (int tid in edge_triangles.ValueItr(eid)) {
if (vTriangles.Contains(tid) == false)
vTriangles.Add(tid);
}
}
return MeshResult.Ok;
}
/// <summary>
/// return # of triangles attached to vID, or -1 if invalid vertex
/// </summary>
public int GetVtxTriangleCount(int vID, bool bBruteForce = false)
{
List<int> vTriangles = new List<int>();
if (GetVtxTriangles(vID, vTriangles) != MeshResult.Ok)
return -1;
return vTriangles.Count;
}
public IEnumerable<int> VtxTrianglesItr(int vID) {
if ( IsVertex(vID) ) {
List<int> tris = new List<int>();
GetVtxTriangles(vID, tris);
foreach (int tid in tris)
yield return tid;
}
}
protected bool tri_has_v(int tID, int vID) {
int i = 3*tID;
return triangles[i] == vID
|| triangles[i + 1] == vID
|| triangles[i + 2] == vID;
}
protected bool tri_is_boundary(int tID) {
int i = 3*tID;
return edge_is_boundary(triangle_edges[i])
|| edge_is_boundary(triangle_edges[i + 1])
|| edge_is_boundary(triangle_edges[i + 2]);
}
protected bool tri_has_neighbour_t(int tCheck, int tNbr) {
int i = 3*tCheck;
return edge_has_t(triangle_edges[i], tNbr)
|| edge_has_t(triangle_edges[i + 1], tNbr)
|| edge_has_t(triangle_edges[i + 2], tNbr);
}
protected bool tri_has_sequential_v(int tID, int vA, int vB)
{
int i = 3*tID;
int v0 = triangles[i], v1 = triangles[i + 1], v2 = triangles[i + 2];
if (v0 == vA && v1 == vB) return true;
if (v1 == vA && v2 == vB) return true;
if (v2 == vA && v0 == vB) return true;
return false;
}
//! returns edge ID
protected int find_tri_neighbour_edge(int tID, int vA, int vB)
{
int i = 3*tID;
int tv0 = triangles[i], tv1 = triangles[i+1];
if ( IndexUtil.same_pair_unordered(tv0, tv1, vA, vB) ) return triangle_edges[3*tID];
int tv2 = triangles[i+2];
if ( IndexUtil.same_pair_unordered(tv1, tv2, vA, vB) ) return triangle_edges[3*tID+1];
if ( IndexUtil.same_pair_unordered(tv2, tv0, vA, vB) ) return triangle_edges[3*tID+2];
return InvalidID;
}
// returns 0/1/2
protected int find_tri_neighbour_index(int tID, int vA, int vB)
{
int i = 3*tID;
int tv0 = triangles[i], tv1 = triangles[i+1];
if ( IndexUtil.same_pair_unordered(tv0, tv1, vA, vB) ) return 0;
int tv2 = triangles[i+2];
if ( IndexUtil.same_pair_unordered(tv1, tv2, vA, vB) ) return 1;
if ( IndexUtil.same_pair_unordered(tv2, tv0, vA, vB) ) return 2;
return InvalidID;
}
public bool IsNonManifoldEdge(int eid)
{
return edge_triangles.Count(eid) > 2;
}
public bool IsBoundaryEdge(int eid) {
return edge_triangles.Count(eid) == 1;
}
protected bool edge_is_boundary(int eid) {
return edge_triangles.Count(eid) == 1;
}
protected bool edge_has_v(int eid, int vid) {
int i = 2*eid;
return (edges[i] == vid) || (edges[i + 1] == vid);
}
protected bool edge_has_t(int eid, int tid) {
return edge_triangles.Contains(eid, tid);
}
protected int edge_other_v(int eID, int vID)
{
int i = 2*eID;
int ev0 = edges[i], ev1 = edges[i + 1];
return (ev0 == vID) ? ev1 : ((ev1 == vID) ? ev0 : InvalidID);
}
//protected int edge_other_t(int eID, int tid) {
// int i = 4 * eID;
// int et0 = edges[i + 2], et1 = edges[i + 3];
// return (et0 == tid) ? et1 : ((et1 == tid) ? et0 : InvalidID);
//}
// ugh need to deprecate this...weird API!
public bool vertex_is_boundary(int vID) {
return IsBoundaryVertex(vID);
}
public bool IsBoundaryVertex(int vID) {
foreach (int eid in vertex_edges.ValueItr(vID)) {
if (edge_triangles.Count(eid) == 1)
return true;
}
return false;
}
public bool IsBoundaryTriangle(int tID)
{
debug_check_is_triangle(tID);
int i = 3 * tID;
return IsBoundaryEdge(triangle_edges[i]) || IsBoundaryEdge(triangle_edges[i + 1]) || IsBoundaryEdge(triangle_edges[i + 2]);
}
int find_edge(int vA, int vB)
{
// [RMS] edge vertices must be sorted (min,max),
// that means we only need one index-check in inner loop.
// commented out code is robust to incorrect ordering, but slower.
int vO = Math.Max(vA, vB);
int vI = Math.Min(vA, vB);
foreach (int eid in vertex_edges.ValueItr(vI)) {
if (edges[2 * eid + 1] == vO)
//if (edge_has_v(eid, vO))
return eid;
}
return InvalidID;
// this is slower, likely because it creates new func<> every time. can we do w/o that?
//return vertex_edges.Find(vI, (eid) => { return edges[4 * eid + 1] == vO; }, InvalidID);
}
int find_edge_from_tri(int vA, int vB, int tID)
{
int i = 3 * tID;
int t0 = triangles[i], t1 = triangles[i + 1];
if (IndexUtil.same_pair_unordered(vA, vB, t0, t1))
return triangle_edges[i];
int t2 = triangles[i + 2];
if (IndexUtil.same_pair_unordered(vA, vB, t1, t2))
return triangle_edges[i+1];
if (IndexUtil.same_pair_unordered(vA, vB, t2, t0))
return triangle_edges[i+2];
return InvalidID;
}
/// <summary>
/// returns true if vID is a "bowtie" vertex, ie multiple disjoint triangle sets in one-ring
/// </summary>
public bool IsBowtieVertex(int vID)
{
if (vertices_refcount.isValid(vID)) {
int nTris = GetVtxTriangleCount(vID);
int vtx_edge_count = GetVtxEdgeCount(vID);
if (!(nTris == vtx_edge_count || nTris == vtx_edge_count - 1))
return true;
return false;
} else
throw new Exception("NTMesh3.IsBowtieVertex: " + vID + " is not a valid vertex");
}
// compute vertex bounding box
public AxisAlignedBox3d GetBounds()
{
double x = 0, y = 0, z = 0;
foreach ( int vi in vertices_refcount ) {
x = vertices[3*vi]; y = vertices[3*vi + 1]; z = vertices[3*vi + 2];
break;
}
double minx = x, maxx = x, miny = y, maxy = y, minz = z, maxz = z;
foreach ( int vi in vertices_refcount ) {
x = vertices[3*vi]; y = vertices[3*vi + 1]; z = vertices[3*vi + 2];
if (x < minx) minx = x; else if (x > maxx) maxx = x;
if (y < miny) miny = y; else if (y > maxy) maxy = y;
if (z < minz) minz = z; else if (z > maxz) maxz = z;
}
return new AxisAlignedBox3d(minx, miny, minz, maxx, maxy, maxz);
}
AxisAlignedBox3d cached_bounds;
int cached_bounds_timestamp = -1;
//! cached bounding box, lazily re-computed on access if mesh has changed
public AxisAlignedBox3d CachedBounds
{
get {
if (cached_bounds_timestamp != Timestamp) {
cached_bounds = GetBounds();
cached_bounds_timestamp = Timestamp;
}
return cached_bounds;
}
}