-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mesh2.cs
2404 lines (2122 loc) · 79.2 KB
/
Mesh2.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.Generic;
using System.Text;
/// <summary>
/// Organizes data needed to draw a two dimensional shape with vertices and
/// faces.
/// </summary>
public class Mesh2
{
/// <summary>
/// An array of coordinates.
/// </summary>
protected Vec2[] coords;
/// <summary>
/// Loops that describe the indices which reference the coordinates and
/// texture coordinates to compose a face.
/// </summary>
protected Loop2[] loops;
/// <summary>
/// The texture (UV) coordinates that describe how an image is mapped onto
/// the mesh. Typically in the range [0.0, 1.0] .
/// </summary>
protected Vec2[] texCoords;
/// <summary>
/// An array of coordinates.
/// </summary>
/// <value>coordinates</value>
public Vec2[] Coords
{
get
{
return this.coords;
}
set
{
this.coords = value;
}
}
/// <summary>
/// Loops that describe the indices which reference the coordinates and
/// texture coordinates to compose a face.
/// </summary>
/// <value>loops</value>
public Loop2[] Loops
{
get
{
return this.loops;
}
set
{
this.loops = value;
}
}
/// <summary>
/// The texture (UV) coordinates that describe how an image is mapped onto
/// the mesh. Typically in the range [0.0, 1.0] .
/// </summary>
/// <value>texture coordinates</value>
public Vec2[] TexCoords
{
get
{
return this.texCoords;
}
set
{
this.texCoords = value;
}
}
/// <summary>
/// Gets a face from the mesh.
/// </summary>
/// <value>face</value>
public Face2 this[int i]
{
get { return this.GetFace(i); }
}
/// <summary>
/// The default constructor.
/// </summary>
public Mesh2()
{
// TODO: All loops are classes and so need to be resized,
// then reset as references, rather than making new allocations.
}
/// <summary>
/// Constructs a mesh from data.
/// </summary>
/// <param name="loops">loops</param>
/// <param name="coords">coordinates</param>
/// <param name="texCoords">texture coordinates</param>
public Mesh2(
in Loop2[] loops,
in Vec2[] coords,
in Vec2[] texCoords)
{
this.loops = loops;
this.coords = coords;
this.texCoords = texCoords;
}
/// <summary>
/// Copies a source mesh to a new mesh.
/// </summary>
/// <param name="source">source mesh</param>
public Mesh2(in Mesh2 source)
{
this.Set(source);
}
/// <summary>
/// Returns a string representation of this mesh.
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return Mesh2.ToString(this);
}
/// <summary>
/// Calculates texture coordinates (UVs) for this mesh. Finds the
/// object-space dimensions of each coordinate, then uses the frame as a
/// reference for new UVs, such that the shape acts as a mask for the
/// texture (or, the texture fills the shape without repeating).
/// </summary>
/// <returns>this mesh/returns>
public Mesh2 CalcUvs()
{
// TODO: Test.
Bounds2 aabb = Mesh2.CalcBounds(this);
Vec2 dim = Bounds2.ExtentSigned(aabb);
Vec2 lb = aabb.Min;
float lbx = lb.X;
float lby = lb.Y;
float xInv = 1.0f / dim.X;
float yInv = 1.0f / dim.Y;
float sAspect = 1.0f;
float tAspect = 1.0f;
if (dim.X < dim.Y) { sAspect = dim.X / dim.Y; }
else if (dim.X > dim.Y) { tAspect = dim.Y / dim.X; }
int vsLen = this.coords.Length;
this.texCoords = Vec2.Resize(this.texCoords, vsLen);
for (int i = 0; i < vsLen; ++i)
{
Vec2 v = this.coords[i];
float sStretch = (v.X - lbx) * xInv;
float tStretch = (v.Y - lby) * yInv;
float s = (sStretch - 0.5f) * sAspect + 0.5f;
float t = (tStretch - 0.5f) * tAspect + 0.5f;
this.texCoords[i] = new Vec2(s, 1.0f - t);
}
int facesLen = this.loops.Length;
for (int i = 0; i < facesLen; ++i)
{
Loop2 loop = this.loops[i];
Index2[] srcIndices = loop.Indices;
int idcsLen = srcIndices.Length;
for (int j = 0; j < idcsLen; ++j)
{
Index2 src = srcIndices[j];
loop.Indices[j] = new Index2(src.V, src.V);
}
}
return this;
}
/// <summary>
/// Removes elements from the coordinate, texture coordinate and normal
/// arrays of the mesh which are not visited by the face indices.
/// </summary>
/// <returns>this mesh</returns>
public Mesh2 Clean()
{
// TODO: Test.
// Transfer arrays to dictionaries where the face index is the key.
Dictionary<int, Vec2> usedCoords = new();
Dictionary<int, Vec2> usedTexCoords = new();
// Visit all data arrays with the faces array. Any data not used by any
// face will be left out.
int facesLen = this.loops.Length;
for (int i = 0; i < facesLen; ++i)
{
Loop2 loop = this.loops[i];
Index2[] verts = loop.Indices;
int vertsLen = verts.Length;
for (int j = 0; j < vertsLen; ++j)
{
// The dictionary should ignore repeated visitations.
Index2 vert = verts[j];
usedCoords[vert.V] = this.coords[vert.V];
usedTexCoords[vert.VT] = this.texCoords[vert.VT];
}
}
// Use a sorted set to filter out similar vectors.
SortQuantized2 v2Cmp = new();
SortedSet<Vec2> coordsSet = new(v2Cmp);
SortedSet<Vec2> texCoordsSet = new(v2Cmp);
coordsSet.UnionWith(usedCoords.Values);
texCoordsSet.UnionWith(usedTexCoords.Values);
// Dictionary's keys are no longer needed; just values.
Vec2[] newCoords = new Vec2[coordsSet.Count];
Vec2[] newTexCoords = new Vec2[texCoordsSet.Count];
// Convert from sorted set to arrays.
coordsSet.CopyTo(newCoords);
texCoordsSet.CopyTo(newTexCoords);
for (int i = 0; i < facesLen; ++i)
{
Loop2 loop = this.loops[i];
Index2[] verts = loop.Indices;
int vertsLen = verts.Length;
for (int j = 0; j < vertsLen; ++j)
{
// Find index of vector in new array by using indexed value from old
// array as a reference.
Index2 oldVert = verts[j];
verts[j] = new Index2(
Array.BinarySearch<Vec2>(newCoords, this.coords[oldVert.V], v2Cmp),
Array.BinarySearch<Vec2>(newTexCoords, this.texCoords[oldVert.VT], v2Cmp));
}
}
// Replace old arrays with the new.
this.coords = newCoords;
this.texCoords = newTexCoords;
// Sort faces by center.
Array.Sort(this.loops, new SortLoops2(this.coords));
return this;
}
/// <summary>
/// Removes a given number of face indices from this mesh beginning at an
/// index. Does not remove any data associated with the indices.
/// </summary>
/// <param name="faceIndex">index</param>
/// <param name="deletions">removal count</param>
/// <returns>this mesh</returns>
public Mesh2 DeleteFaces(in int faceIndex, in int deletions = 1)
{
int aLen = this.loops.Length;
int valIdx = Utils.RemFloor(faceIndex, aLen);
int valDel = Utils.Clamp(deletions, 0, aLen - valIdx);
int bLen = aLen - valDel;
Loop2[] result = new Loop2[bLen];
System.Array.Copy(this.loops, 0, result, 0, valIdx);
System.Array.Copy(this.loops, valIdx + valDel, result, valIdx, bLen - valIdx);
this.loops = result;
return this;
}
/// <summary>
/// Negates the x component of all texture coordinates (u) in the mesh.
/// Does so by subtracting the value from 1.0.
/// </summary>
/// <returns>this mesh</returns>
public Mesh2 FlipU()
{
int vtsLen = this.texCoords.Length;
for (int i = 0; i < vtsLen; ++i)
{
Vec2 vt = this.texCoords[i];
this.texCoords[i] = new Vec2(1.0f - vt.X, vt.Y);
}
return this;
}
/// <summary>
/// Negates the y component of all texture coordinates (v) in the mesh.
/// Does so by subtracting the value from 1.0.
/// </summary>
/// <returns>this mesh</returns>
public Mesh2 FlipV()
{
int vtsLen = this.texCoords.Length;
for (int i = 0; i < vtsLen; ++i)
{
Vec2 vt = this.texCoords[i];
this.texCoords[i] = new Vec2(vt.X, 1.0f - vt.Y);
}
return this;
}
/// <summary>
/// Negates the x component of all coordinates in the mesh,
/// then reverses the mesh's faces.
/// Use this instead of scaling the mesh by (-1.0, 1.0).
/// </summary>
/// <returns>this mesh</returns>
public Mesh2 FlipX()
{
int vsLen = this.coords.Length;
for (int i = 0; i < vsLen; ++i)
{
this.coords[i] = Vec2.FlipX(this.coords[i]);
}
this.ReverseFaces();
return this;
}
/// <summary>
/// Negates the y component of all coordinates in the mesh,
/// then reverses the mesh's faces.
/// Use this instead of scaling the mesh by (1.0, -1.0).
/// </summary>
/// <returns>this mesh</returns>
public Mesh2 FlipY()
{
int vsLen = this.coords.Length;
for (int i = 0; i < vsLen; ++i)
{
this.coords[i] = Vec2.FlipY(this.coords[i]);
}
this.ReverseFaces();
return this;
}
/// <summary>
/// Gets an edge from the mesh.
/// </summary>
/// <param name="faceIndex">face index</param>
/// <param name="edgeIndex">edge index</param>
/// <returns>edge</returns>
public Edge2 GetEdge(in int faceIndex = -1, in int edgeIndex = -1)
{
Loop2 loop = this.loops[Utils.RemFloor(faceIndex, this.loops.Length)];
Index2 idxOrigin = loop[edgeIndex];
Index2 idxDest = loop[edgeIndex + 1];
return new Edge2(
new Vert2(
this.coords[idxOrigin.V],
this.texCoords[idxOrigin.VT]),
new Vert2(
this.coords[idxDest.V],
this.texCoords[idxDest.VT]));
}
///<summary>
///Gets an array of edges from the mesh.
///</summary>
///<returns>edges array</returns>
public Edge2[] GetEdges()
{
return this.GetEdgesUndirected();
}
///<summary>
///Gets an array of edges from the mesh. Edges are treated as directed, so
///(origin, destination) and (destination, edge) are considered to be
///different.
///</summary>
///<returns>edges array</returns>
public Edge2[] GetEdgesDirected()
{
int loopsLen = this.loops.Length;
SortedSet<Edge2> result = new();
for (int i = 0; i < loopsLen; ++i)
{
Loop2 loop = this.loops[i];
Index2[] indices = loop.Indices;
int indicesLen = indices.Length;
for (int j = 0; j < indicesLen; ++j)
{
Index2 idxOrigin = indices[j];
Index2 idxDest = indices[(j + 1) % indicesLen];
Edge2 trial = new(
new Vert2(
this.coords[idxOrigin.V],
this.texCoords[idxOrigin.VT]),
new Vert2(
this.coords[idxDest.V],
this.texCoords[idxDest.VT]));
result.Add(trial);
}
}
Edge2[] arr = new Edge2[result.Count];
result.CopyTo(arr);
return arr;
}
///<summary>
///Gets an array of edges from the mesh. Edges are treated as undirected,
///so (origin, destination) and (destination, edge) are considered to be
///equal
///</summary>
///<returns>edges array</returns>
public Edge2[] GetEdgesUndirected()
{
int loopsLen = this.loops.Length;
Dictionary<int, Edge2> result = new();
for (int i = 0; i < loopsLen; ++i)
{
Loop2 loop = this.loops[i];
Index2[] indices = loop.Indices;
int indicesLen = indices.Length;
for (int j = 0; j < indicesLen; ++j)
{
Index2 idxOrigin = indices[j];
Index2 idxDest = indices[(j + 1) % indicesLen];
int vIdxOrigin = idxOrigin.V;
int vIdxDest = idxDest.V;
int aHsh = (Utils.MulBase ^ vIdxOrigin) *
Utils.HashMul ^ vIdxDest;
int bHsh = (Utils.MulBase ^ vIdxDest) *
Utils.HashMul ^ vIdxOrigin;
if (!result.ContainsKey(aHsh) && !result.ContainsKey(bHsh))
{
result[vIdxOrigin < vIdxDest ? aHsh : bHsh] = new Edge2(
new Vert2(
this.coords[idxOrigin.V],
this.texCoords[idxOrigin.VT]),
new Vert2(
this.coords[idxDest.V],
this.texCoords[idxDest.VT]));
}
}
}
Edge2[] arr = new Edge2[result.Count];
result.Values.CopyTo(arr, 0);
return arr;
}
/// <summary>
/// Gets a face from a mesh.
/// </summary>
/// <param name="faceIndex">face index</param>
/// <returns>face</returns>
public Face2 GetFace(in int faceIndex = -1)
{
Loop2 loop = this.loops[Utils.RemFloor(faceIndex, this.loops.Length)];
Index2[] indices = loop.Indices;
int indicesLen = indices.Length;
Edge2[] edges = new Edge2[indicesLen];
for (int i = 0; i < indicesLen; ++i)
{
Index2 idxOrigin = indices[i];
Index2 idxDest = indices[(i + 1) % indicesLen];
edges[i] = new Edge2(
new Vert2(
this.coords[idxOrigin.V],
this.texCoords[idxOrigin.VT]),
new Vert2(
this.coords[idxDest.V],
this.texCoords[idxDest.VT]));
}
return new Face2(edges);
}
/// <summary>
/// Gets an array of faces from the mesh.
/// </summary>
/// <returns>faces array</returns>
public Face2[] GetFaces()
{
int loopsLen = this.loops.Length;
Face2[] faces = new Face2[loopsLen];
for (int i = 0; i < loopsLen; ++i)
{
Loop2 loop = this.loops[i];
Index2[] indices = loop.Indices;
int indicesLen = indices.Length;
Edge2[] edges = new Edge2[indicesLen];
for (int j = 0; j < indicesLen; ++j)
{
Index2 idxOrigin = indices[j];
Index2 idxDest = indices[(j + 1) % indicesLen];
edges[j] = new Edge2(
new Vert2(
this.coords[idxOrigin.V],
this.texCoords[idxOrigin.VT]),
new Vert2(
this.coords[idxDest.V],
this.texCoords[idxDest.VT]));
}
faces[i] = new Face2(edges);
}
return faces;
}
/// <summary>
/// Gets a vertex from the mesh.
/// </summary>
/// <param name="faceIndex">face index</param>
/// <param name="vertIndex">vertex index</param>
/// <returns>vertex</returns>
public Vert2 GetVertex(in int faceIndex = -1, in int vertIndex = -1)
{
Index2 index = this.loops[Utils.RemFloor(faceIndex, this.loops.Length)][vertIndex];
return new Vert2(
this.coords[index.V],
this.texCoords[index.VT]);
}
/// <summary>
/// Gets an array of vertices from the mesh.
/// </summary>
/// <returns>vertices</returns>
public Vert2[] GetVertices()
{
int len0 = this.loops.Length;
SortedSet<Vert2> result = new();
for (int i = 0; i < len0; ++i)
{
Loop2 loop = this.loops[i];
Index2[] indices = loop.Indices;
int len1 = indices.Length;
for (int j = 0; j < len1; ++j)
{
Index2 vert = indices[j];
result.Add(new Vert2(
this.coords[vert.V],
this.texCoords[vert.VT]));
}
}
Vert2[] arr = new Vert2[result.Count];
result.CopyTo(arr);
return arr;
}
/// <summary>
/// Insets a face by calculating its center then easing from the face's
/// vertices toward the center by the factor. The factor is expected to be
/// in the range [0.0, 1.0] . When it is less than 0.0, the face remains
/// unchanged. When it is greater than 1.0, the face is subdivided by
/// center. Returns a tuple containing the new data created.
/// </summary>
/// <param name="faceIdx">face index</param>
/// <param name="fac">factor</param>
/// <returns>tuple</returns>
public (Loop2[] loopsNew, Vec2[] vsNew, Vec2[] vtsNew) InsetFace(in int faceIdx = 0, in float fac = 0.5f)
{
if (fac <= 0.0f)
{
return (loopsNew: new Loop2[0],
vsNew: new Vec2[0],
vtsNew: new Vec2[0]);
}
if (fac >= 1.0f)
{
return this.SubdivFaceFan(faceIdx);
}
int loopsLen = this.loops.Length;
int i = Utils.RemFloor(faceIdx, loopsLen);
Loop2 loop = this.loops[i];
Index2[] indices = loop.Indices;
int loopLen = indices.Length;
int vsOldLen = this.coords.Length;
int vtsOldLen = this.texCoords.Length;
Loop2[] loopsNew = new Loop2[loopLen + 1];
Loop2 centerLoop = loopsNew[loopLen] = new Loop2(loopLen);
// Sum centers.
Vec2 vCenter = Vec2.Zero;
Vec2 vtCenter = Vec2.Zero;
for (int j = 0; j < loopLen; ++j)
{
Index2 curr = indices[j];
vCenter += this.coords[curr.V];
vtCenter += this.texCoords[curr.VT];
}
// Find average.
if (loopLen > 0)
{
float flInv = 1.0f / loopLen;
vCenter *= flInv;
vtCenter *= flInv;
}
Vec2[] vsNew = new Vec2[loopLen];
Vec2[] vtsNew = new Vec2[loopLen];
// Find new corners.
for (int j = 0; j < loopLen; ++j)
{
int k = (j + 1) % loopLen;
Index2 vertCurr = indices[j];
Index2 vertNext = indices[k];
int vCornerIdx = vertCurr.V;
int vtCornerIdx = vertCurr.VT;
vsNew[j] = Vec2.Mix(this.coords[vCornerIdx], vCenter, fac);
vtsNew[j] = Vec2.Mix(this.texCoords[vtCornerIdx], vtCenter, fac);
int vSubdivIdx = vsOldLen + j;
int vtSubdivIdx = vtsOldLen + j;
loopsNew[j] = new Loop2(
new Index2(vCornerIdx, vtCornerIdx),
new Index2(vertNext.V, vertNext.VT),
new Index2(vsOldLen + k, vtsOldLen + k),
new Index2(vSubdivIdx, vtSubdivIdx));
centerLoop[j] = new Index2(vSubdivIdx, vtSubdivIdx);
}
this.coords = Vec2.Concat(this.coords, vsNew);
this.texCoords = Vec2.Concat(this.texCoords, vtsNew);
this.loops = Loop2.Splice(this.loops, i, 1, loopsNew);
return (loopsNew, vsNew, vtsNew);
}
/// <summary>
/// Reverses all the face loops in this mesh.
/// </summary>
/// <returns>this mesh</returns>
public Mesh2 ReverseFaces()
{
int fsLen = this.loops.Length;
for (int i = 0; i < fsLen; ++i)
{
this.loops[i].Reverse();
}
return this;
}
/// <summary>
/// Scales this mesh by a uniform scalar.
/// Scalar must not be zero.
/// </summary>
/// <param name="s">scalar</param>
/// <returns>this mesh</returns>
public Mesh2 Scale(in float s)
{
if (s != 0.0f)
{
int vsLen = this.coords.Length;
for (int i = 0; i < vsLen; ++i) { this.coords[i] *= s; }
}
return this;
}
/// <summary>
/// Scales this mesh by a nonuniform scalar.
/// All components of the scalar must be nonzero.
/// </summary>
/// <param name="s">scalar</param>
/// <returns>this mesh</returns>
public Mesh2 Scale(in Vec2 s)
{
if (Vec2.All(s))
{
int vsLen = this.coords.Length;
for (int i = 0; i < vsLen; ++i) { this.coords[i] *= s; }
}
return this;
}
/// <summary>
/// Copies a source mesh by value.
/// </summary>
/// <param name="source">source mesh</param>
/// <returns>this mesh</returns>
public Mesh2 Set(in Mesh2 source)
{
// TODO: Test
int vsLen = source.coords.Length;
this.coords = new Vec2[vsLen];
System.Array.Copy(source.coords, this.coords, vsLen);
int vtsLen = source.texCoords.Length;
this.texCoords = new Vec2[vtsLen];
System.Array.Copy(source.texCoords, this.texCoords, vtsLen);
int loopsLen = source.loops.Length;
this.loops = new Loop2[loopsLen];
for (int i = 0; i < loopsLen; ++i)
{
Index2[] sourceIndices = source.loops[i].Indices;
int loopLen = sourceIndices.Length;
Index2[] targetIndices = new Index2[loopLen];
System.Array.Copy(sourceIndices, targetIndices, loopLen);
this.loops[i] = new Loop2(targetIndices);
}
return this;
}
/// <summary>
/// Subdivides a convex face by calculating its center, subdividing each of
/// its edges with one cut to create a midpoint, then connecting the
/// midpoints to the center. This generates a quadrilateral for the number
/// of edges in the face. Returns a tuple containing the new data created.
/// </summary>
/// <param name="faceIdx">face index</param>
/// <returns>tuple</returns>
public (Loop2[] loopsNew,
Vec2[] vsNew,
Vec2[] vtsNew) SubdivFaceCenter(in int faceIdx = 0)
{
int facesLen = this.loops.Length;
int i = Utils.RemFloor(faceIdx, facesLen);
Index2[] face = this.loops[i].Indices;
int faceLen = face.Length;
int vsOldLen = this.coords.Length;
int vtsOldLen = this.texCoords.Length;
Vec2[] vsNew = new Vec2[faceLen + 1];
Vec2[] vtsNew = new Vec2[vsNew.Length];
Loop2[] loopsNew = new Loop2[faceLen];
int vCenterIdx = vsOldLen + faceLen;
int vtCenterIdx = vtsOldLen + faceLen;
Vec2 vCenter = Vec2.Zero;
Vec2 vtCenter = Vec2.Zero;
for (int j = 0; j < faceLen; ++j)
{
int k = (j + 1) % faceLen;
Index2 vertCurr = face[j];
Index2 vertNext = face[k];
Vec2 vCurr = this.coords[vertCurr.V];
Vec2 vtCurr = this.texCoords[vertCurr.VT];
vCenter += vCurr;
vtCenter += vtCurr;
int vNextIdx = vertNext.V;
int vtNextIdx = vertNext.VT;
vsNew[j] = Vec2.Mix(vCurr, this.coords[vNextIdx]);
vtsNew[j] = Vec2.Mix(vtCurr, this.texCoords[vtNextIdx]);
loopsNew[j] = new Loop2(
new Index2(vCenterIdx, vtCenterIdx),
new Index2(vsOldLen + j, vtsOldLen + j),
new Index2(vNextIdx, vtNextIdx),
new Index2(vsOldLen + k, vtsOldLen + k));
}
if (faceLen > 0)
{
float flInv = 1.0f / faceLen;
vCenter *= flInv;
vtCenter *= flInv;
}
vsNew[faceLen] = vCenter;
vtsNew[faceLen] = vtCenter;
this.coords = Vec2.Concat(this.coords, vsNew);
this.texCoords = Vec2.Concat(this.texCoords, vtsNew);
this.loops = Loop2.Splice(this.loops, i, 1, loopsNew);
return (loopsNew, vsNew, vtsNew);
}
/// <summary>
/// Subdivides a convex face by calculating its center, then connecting its
/// vertices to the center. This generates a triangle for the number of
/// edges in the face.
/// </summary>
/// <param name="faceIdx">the face index</param>
/// <returns>tuple</returns>
public (Loop2[] loopsNew,
Vec2[] vsNew,
Vec2[] vtsNew) SubdivFaceFan(in int faceIdx = 0)
{
int facesLen = this.loops.Length;
int i = Utils.RemFloor(faceIdx, facesLen);
Index2[] face = this.loops[i].Indices;
int faceLen = face.Length;
Loop2[] loopsNew = new Loop2[faceLen];
Vec2 vCenter = Vec2.Zero;
Vec2 vtCenter = Vec2.Zero;
int vCenterIdx = this.coords.Length;
int vtCenterIdx = this.texCoords.Length;
for (int j = 0; j < faceLen; ++j)
{
int k = (j + 1) % faceLen;
Index2 vertCurr = face[j];
Index2 vertNext = face[k];
int vCurrIdx = vertCurr.V;
int vtCurrIdx = vertCurr.VT;
Vec2 vCurr = this.coords[vCurrIdx];
Vec2 vtCurr = this.texCoords[vtCurrIdx];
vCenter += vCurr;
vtCenter += vtCurr;
loopsNew[j] = new Loop2(
new Index2(vCenterIdx, vtCenterIdx),
new Index2(vCurrIdx, vtCurrIdx),
new Index2(vertNext.V, vertNext.VT));
}
if (faceLen > 0)
{
float flInv = 1.0f / faceLen;
vCenter *= flInv;
vtCenter *= flInv;
}
this.coords = Vec2.Append(this.coords, vCenter);
this.texCoords = Vec2.Append(this.texCoords, vtCenter);
this.loops = Loop2.Splice(this.loops, i, 1, loopsNew);
return (loopsNew,
vsNew: new Vec2[] { vCenter },
vtsNew: new Vec2[] { vtCenter });
}
/// <summary>
/// Subdivides a convex face by cutting each of its edges once to create a
/// midpoint, then connecting each midpoint. This generates peripheral
/// triangles and a new central face with the same number of edges as
/// original. This is best suited to meshes made of triangles. Returns a
/// tuple containing the new data created.
/// </summary>
/// <param name="faceIdx">face index</param>
/// <returns>tuple</returns>
public (Loop2[] loopsNew,
Vec2[] vsNew,
Vec2[] vtsNew) SubdivFaceInscribe(in int faceIdx = 0)
{
int facesLen = this.loops.Length;
int i = Utils.RemFloor(faceIdx, facesLen);
Index2[] face = this.loops[i].Indices;
int faceLen = face.Length;
int vsOldLen = this.coords.Length;
int vtsOldLen = this.texCoords.Length;
Vec2[] vsNew = new Vec2[faceLen];
Vec2[] vtsNew = new Vec2[faceLen];
Loop2[] loopsNew = new Loop2[faceLen + 1];
Index2[] cfIdcs = new Index2[3];
for (int j = 0; j < faceLen; ++j)
{
int k = (j + 1) % faceLen;
Index2 vertCurr = face[j];
Index2 vertNext = face[k];
int vNextIdx = vertNext.V;
int vtNextIdx = vertNext.VT;
vsNew[j] = Vec2.Mix(
this.coords[vertCurr.V],
this.coords[vNextIdx]);
vtsNew[j] = Vec2.Mix(
this.texCoords[vertCurr.VT],
this.texCoords[vtNextIdx]);
int vSubdivIdx = vsOldLen + j;
int vtSubdivIdx = vtsOldLen + j;
loopsNew[j] = new Loop2(
new Index2(vSubdivIdx, vtSubdivIdx),
new Index2(vNextIdx, vtNextIdx),
new Index2(vsOldLen + k, vtsOldLen + k));
cfIdcs[j] = new Index2(vSubdivIdx, vtSubdivIdx);
}
// Center face.
loopsNew[faceLen] = new Loop2(cfIdcs);
this.coords = Vec2.Concat(this.coords, vsNew);
this.texCoords = Vec2.Concat(this.texCoords, vtsNew);
this.loops = Loop2.Splice(this.loops, i, 1, loopsNew);
return (loopsNew, vsNew, vtsNew);
}
/// <summary>
/// Subdivides all faces in the mesh by a number of iterations. Uses the
/// center method.
/// </summary>
/// <param name="itr">iterations</param>
/// <returns>this mesh</returns>
public (Loop2[] loopsNew,
Vec2[] vsNew,
Vec2[] vtsNew) SubdivFacesCenter(in int itr = 1)
{
List<Loop2> loopsNew = new();
List<Vec2> vsNew = new();
List<Vec2> vtsNew = new();
for (int i = 0; i < itr; ++i)
{
int k = 0;
int len = this.loops.Length;
for (int j = 0; j < len; ++j)
{
int vertLen = this.loops[k].Length;
var result = this.SubdivFaceCenter(k);
loopsNew.AddRange(result.loopsNew);
vsNew.AddRange(result.vsNew);
vtsNew.AddRange(result.vtsNew);
k += vertLen;
}
}
return (loopsNew: loopsNew.ToArray(),
vsNew: vsNew.ToArray(),
vtsNew: vtsNew.ToArray());
}
/// <summary>
/// Subdivides all faces in the mesh by a number of iterations. Uses the
/// triangle fan method.
/// </summary>
/// <param name="itr">iterations</param>
/// <returns>this mesh</returns>
public (Loop2[] loopsNew,
Vec2[] vsNew,
Vec2[] vtsNew) SubdivFacesFan(in int itr = 1)
{
List<Loop2> loopsNew = new();
List<Vec2> vsNew = new();
List<Vec2> vtsNew = new();
for (int i = 0; i < itr; ++i)
{
int k = 0;
int len = this.loops.Length;
for (int j = 0; j < len; ++j)
{
int vertLen = this.loops[k].Length;
var result = this.SubdivFaceFan(k);
loopsNew.AddRange(result.loopsNew);
vsNew.AddRange(result.vsNew);
vtsNew.AddRange(result.vtsNew);
k += vertLen;
}
}
return (loopsNew: loopsNew.ToArray(),
vsNew: vsNew.ToArray(),
vtsNew: vtsNew.ToArray());
}
/// <summary>
/// Subdivides all faces in the mesh by a number of iterations. Uses the
/// inscription method.
/// </summary>