forked from technificentConsulting/TS4-SimRipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometry.cs
1823 lines (1607 loc) · 64.3 KB
/
Geometry.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
/* Xmods Data Library, a library to support tools for The Sims 4,
Copyright (C) 2014 C. Marinetti
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
The author may be contacted at modthesims.info, username cmarNYC. */
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
// Parts adapted from article "A Vector Type for C#" by R. Potter on codeproject.com
namespace TS4SimRipper
{
// VECTOR3
public struct Vector3 : IEquatable<Vector3>
{
private float x, y, z;
public float X
{
get { return x; }
set { x = value; }
}
public float Y
{
get { return y; }
set { y = value; }
}
public float Z
{
get { return z; }
set { z = value; }
}
public float[] Coordinates
{
get { return new float[] { x, y, z }; }
set
{
x = value[0];
y = value[1];
z = value[2];
}
}
public float Magnitude
{
get
{
double tmp = (x * x) + (y * y) + (z * z);
return (float)Math.Sqrt(tmp);
}
}
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Vector3(float[] coordinates)
{
this.x = coordinates[0];
this.y = coordinates[1];
this.z = coordinates[2];
}
public Vector3(Vector3 vector)
{
this.x = vector.X;
this.y = vector.Y;
this.z = vector.Z;
}
public static Vector3 operator +(Vector3 v1, Vector3 v2)
{
return
(
new Vector3
(
v1.X + v2.X,
v1.Y + v2.Y,
v1.Z + v2.Z
)
);
}
public static Vector3 operator -(Vector3 v1, Vector3 v2)
{
return
(
new Vector3
(
v1.X - v2.X,
v1.Y - v2.Y,
v1.Z - v2.Z
)
);
}
public static bool operator ==(Vector3 v1, Vector3 v2)
{
return
(
(AlmostEquals(v1.X, v2.X)) &&
(AlmostEquals(v1.Y, v2.Y)) &&
(AlmostEquals(v1.Z, v2.Z))
);
}
public override bool Equals(object obj)
{
// Check object other is a Vector3 object
if (obj is Vector3)
{
// Convert object to Vector3
Vector3 otherVector = (Vector3)obj;
// Check for equality
return otherVector == this;
}
else
{
return false;
}
}
public bool Equals(Vector3 obj)
{
return obj == this;
}
public static bool operator !=(Vector3 v1, Vector3 v2)
{
return !(v1 == v2);
}
public static Vector3 operator *(Vector3 v1, float s2)
{
return
(
new Vector3
(
v1.X * s2,
v1.Y * s2,
v1.Z * s2
)
);
}
public static Vector3 operator *(float s1, Vector3 v2)
{
return v2 * s1;
}
public static Vector3 operator /(Vector3 v1, float s2)
{
return
(
new Vector3
(
v1.X / s2,
v1.Y / s2,
v1.Z / s2
)
);
}
public static Vector3 Scale(Vector3 v1, Vector3 v2)
{
return
(
new Vector3
(
v1.X * v2.X,
v1.Y * v2.Y,
v1.Z * v2.Z
)
);
}
public static Vector3 AbsoluteValue(Vector3 v)
{
return new Vector3(Math.Abs(v.x), Math.Abs(v.y), Math.Abs(v.z));
}
public Vector3 Scale(Vector3 scalingVector)
{
return Scale(this, scalingVector);
}
public static Vector3 Cross(Vector3 v1, Vector3 v2)
{
return
(
new Vector3
(
v1.Y * v2.Z - v1.Z * v2.Y,
v1.Z * v2.X - v1.X * v2.Z,
v1.X * v2.Y - v1.Y * v2.X
)
);
}
public Vector3 Cross(Vector3 other)
{
return Cross(this, other);
}
public static float Dot(Vector3 v1, Vector3 v2)
{
return
(
v1.X * v2.X +
v1.Y * v2.Y +
v1.Z * v2.Z
);
}
public float Dot(Vector3 other)
{
return Dot(this, other);
}
public static Vector3 Normalize(Vector3 v1)
{
// Check for divide by zero errors
if (v1.Magnitude == 0)
{
return v1;
}
else
{
// find the inverse of the vector's magnitude
float inverse = 1 / v1.Magnitude;
return
(
new Vector3
(
// multiply each component by the inverse of the magnitude
v1.X * inverse,
v1.Y * inverse,
v1.Z * inverse
)
);
}
}
public void Normalize()
{
Vector3 n = Normalize(this);
this.x = n.x;
this.y = n.y;
this.z = n.z;
}
public static float Distance(Vector3 v1, Vector3 v2)
{
return
(
(float)Math.Sqrt
(
(v1.X - v2.X) * (v1.X - v2.X) +
(v1.Y - v2.Y) * (v1.Y - v2.Y) +
(v1.Z - v2.Z) * (v1.Z - v2.Z)
)
);
}
public float Distance(Vector3 other)
{
return Distance(this, other);
}
public static float Angle(Vector3 v1, Vector3 v2)
{
return
(
(float)Math.Acos
(
Normalize(v1).Dot(Normalize(v2))
)
);
}
public float Angle(Vector3 other)
{
return Angle(this, other);
}
public static Vector3 Centroid(Vector3 P1, Vector3 P2, Vector3 P3)
{
return new Vector3((P1.x + P2.x + P3.x) / 3f, (P1.y + P2.y + P3.y) / 3f, (P1.z + P2.z + P3.z) / 3f);
}
public Vector3 ProjectToLine(Vector3 Point1, Vector3 Point2)
{
Vector3 tmp = Point2 - Point1;
tmp.Normalize();
Vector3 tmp2 = this - Point1;
Vector3 tmp3 = Vector3.Dot(tmp, tmp2) * tmp;
return new Vector3(Point1 + tmp3);
}
public bool Between(Vector3 Point1, Vector3 Point2)
{
float min = Math.Min(Point1.X, Point2.X);
float max = Math.Max(Point1.X, Point2.X);
if (min > this.X | this.X > max)
{
return false;
}
min = Math.Min(Point1.Y, Point2.Y);
max = Math.Max(Point1.Y, Point2.Y);
if (min > this.Y | this.Y > max)
{
return false;
}
min = Math.Min(Point1.Z, Point2.Z);
max = Math.Max(Point1.Z, Point2.Z);
if (min > this.Z | this.Z > max)
{
return false;
}
return true;
}
public float[] GetInterpolationWeights(Vector3[] points, float weightingFactor)
{
float[] weights = new float[points.Length];
if (points.Length == 1)
{
weights[0] = 1f;
return weights;
}
for (int i = 0; i < points.Length; i++)
{
if (Vector3.Distance(points[i], this) == 0f)
{
weights[i] = 1f;
return weights;
}
}
float[] d = new float[points.Length];
float dt = 0;
for (int i = 0; i < points.Length; i++)
{
d[i] = 1f / (float)Math.Pow(Vector3.Distance(points[i], this), weightingFactor);
dt += d[i];
}
for (int i = 0; i < points.Length; i++)
{
weights[i] = d[i] / dt;
}
//string a = "Distance: ";
//string b = "Weights: ";
//string x = "Positions: ";
//for (int i = 0; i < weights.Length; i++)
//{
// a += d[i].ToString() + ", ";
// b += weights[i].ToString() + ", ";
// x += points[i].x.ToString() + "," + points[i].y.ToString() + "," + points[i].z.ToString() + ", ";
//}
//MessageBox.Show(a + System.Environment.NewLine + b + System.Environment.NewLine + x);
return weights;
}
public int NearestPointIndexSimple(Vector3[] RefPointsArray)
{
float minDistance = float.MaxValue;
int ind = 0;
for (int i = 0; i < RefPointsArray.Length; i++)
{
if (this.Distance(RefPointsArray[i]) < minDistance)
{
minDistance = this.Distance(RefPointsArray[i]);
ind = i;
}
}
return ind;
}
public Vector3 NearestPointSimple(Vector3[] RefPointsArray)
{
int ind = this.NearestPointIndexSimple(RefPointsArray);
return RefPointsArray[ind];
}
public int NearestPointIndex(Vector3 thisFacesCentroid, Vector3[] RefPointsArray, Vector3[] refPointsFacesCentroids)
{
float minDistance = float.MaxValue;
List<int> workingIndexes = new List<int>();
for (int i = 0; i < RefPointsArray.Length; i++)
{
if (this.Distance(RefPointsArray[i]) < minDistance)
{
workingIndexes.Clear();
workingIndexes.Add(i);
minDistance = this.Distance(RefPointsArray[i]);
}
else if (this.Distance(RefPointsArray[i]) == minDistance)
{
workingIndexes.Add(i);
}
}
float minFaceDistance = float.MaxValue;
int ind = 0;
for (int i = 0; i < workingIndexes.Count; i++)
{
if (thisFacesCentroid.Distance(refPointsFacesCentroids[workingIndexes[i]]) < minFaceDistance)
{
ind = workingIndexes[i];
minFaceDistance = thisFacesCentroid.Distance(refPointsFacesCentroids[workingIndexes[i]]);
}
}
return ind;
}
public Vector3 NearestPoint(Vector3 thisFacesCentroid, Vector3[] RefPointsArray, Vector3[] refPointsFacesCentroids)
{
int ind = this.NearestPointIndex(thisFacesCentroid, RefPointsArray, refPointsFacesCentroids);
return RefPointsArray[ind];
}
internal int ArrayMinimumIndex(float[] array)
{
int tmp = -1;
float tmpVal = float.MaxValue;
for (int i = 0; i < array.Length; i++)
{
if (array[i] < tmpVal) tmp = i;
}
return tmp;
}
public override int GetHashCode()
{
return
(
(int)((X + Y + Z) % Int32.MaxValue)
);
}
public override string ToString()
{
return this.X.ToString() + ", " + this.Y.ToString() + ", " + this.Z.ToString();
}
public static Vector3 Parse(string coordinateString)
{
string[] coordsStr = coordinateString.Split(new char[] { ',' });
if (coordsStr.Length != 3) throw new FormatException("Input not in correct format");
float[] coords = new float[3];
for (int i = 0; i < 3; i++)
{
if (!float.TryParse(coordsStr[i], NumberStyles.Float, CultureInfo.InvariantCulture, out coords[i])) throw new FormatException("Input not in correct format");
}
return new Vector3(coords);
}
internal static bool AlmostEquals(float f1, float f2)
{
const float EPSILON = 0.00005f;
return (Math.Abs(f1 - f2) < EPSILON);
}
internal bool positionMatches(float[] other)
{
return this.positionMatches(new Vector3(other));
}
internal bool positionMatches(float x, float y, float z)
{
return this.positionMatches(new Vector3(x, y, z));
}
internal bool positionMatches(Vector3 other)
{
const float EPSILON = 0.0005f;
if (Math.Abs(this.x - other.x) < EPSILON && Math.Abs(this.y - other.y) < EPSILON && Math.Abs(this.z - other.z) < EPSILON) return true;
return false;
}
internal bool positionClose(float[] other)
{
return this.positionClose(new Vector3(other));
}
internal bool positionClose(Vector3 other)
{
const float EPSILON = 0.005f;
if (Math.Abs(this.x - other.x) < EPSILON && Math.Abs(this.y - other.y) < EPSILON && Math.Abs(this.z - other.z) < EPSILON) return true;
return false;
}
}
// VECTOR2
public struct Vector2
{
private float x, y;
public float X
{
get { return x; }
set { x = value; }
}
public float Y
{
get { return y; }
set { y = value; }
}
public float[] Coordinates
{
get { return new float[] { x, y }; }
set
{
x = value[0];
y = value[1];
}
}
public Vector2(float x, float y)
{
this.x = x;
this.y = y;
}
public Vector2(float[] coordinates)
{
this.x = coordinates[0];
this.y = coordinates[1];
}
public Vector2(Vector2 vector)
{
this.x = vector.X;
this.y = vector.Y;
}
public double Magnitude
{
get { return Math.Sqrt((this.x * this.x) + (this.y * this.y)); }
}
public float Distance(Vector2 other)
{
double deltaX = this.x - other.x;
double deltaY = this.y - other.y;
return (float)Math.Sqrt(Math.Pow(deltaX, 2d) + Math.Pow(deltaY, 2d));
}
public bool CloseTo(Vector2 v2, float epsilon)
{
return
(
(Math.Abs(this.X - v2.X) < epsilon) &&
(Math.Abs(this.Y - v2.Y) < epsilon)
);
}
public static bool operator ==(Vector2 v1, Vector2 v2)
{
const float EPSILON = 0.00005f;
return
(
(Math.Abs(v1.X - v2.X) < EPSILON) &&
(Math.Abs(v1.Y - v2.Y) < EPSILON)
);
}
public override bool Equals(object obj)
{
if (obj is Vector2)
{
Vector2 other = (Vector2)obj;
return (other == this);
}
else
{
return false;
}
}
public bool Equals(Vector2 obj)
{
return obj == this;
}
public static bool operator !=(Vector2 v1, Vector2 v2)
{
return !(v1 == v2);
}
public static Vector2 operator +(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x + v2.x, v1.y + v2.y);
}
public static Vector2 operator -(Vector2 v1, Vector2 v2)
{
return new Vector2(v1.x - v2.x, v1.y - v2.y);
}
public static Vector2 operator *(Vector2 v1, float m2)
{
return new Vector2(v1.x * m2, v1.y * m2);
}
public static Vector2 operator *(float m1, Vector2 v2)
{
return v2 * m1;
}
public static Vector2 Normalize(Vector2 v1)
{
// Check for divide by zero errors
if (v1.Magnitude == 0)
{
throw new DivideByZeroException("Cannot normalize a vector with magnitude of zero!");
}
else
{
// find the inverse of the vector's magnitude
float inverse = (float)(1f / v1.Magnitude);
return new Vector2(v1.X * inverse, v1.Y * inverse);
}
}
public override int GetHashCode()
{
return
(
(int)((X + Y) % Int32.MaxValue)
);
}
public override string ToString()
{
return this.X.ToString() + ", " + this.Y.ToString();
}
public static float Dot(Vector2 v1, Vector2 v2)
{
return (v1.x * v2.x) + (v1.y * v2.y);
}
public float Dot(Vector2 other)
{
return Dot(this, other);
}
public bool DistanceFromLineRestricted(Vector2 P1, Vector2 P2, out float distance, out int endpointIndex)
//returns whether point project to line segment falls within endpoints,
//distance = distance point to projected point or to nearest endpoint,
//endPontIndex = 0 if nearest endpoint is P1 or 1 if P2
{
Vector2 tmp = this - P1;
Vector2 line = P2 - P1;
float lineLenSq = (line.x * line.x) + (line.y * line.y);
float distanceOnSegment = Vector2.Dot(tmp, line) / lineLenSq;
endpointIndex = 0;
if (distanceOnSegment >= 0f && distanceOnSegment <= 1f) //within segment
{
Vector2 projectedPoint = P1 + (line * distanceOnSegment);
distance = this.Distance(projectedPoint);
return true;
}
else
{
if (distanceOnSegment > 1f) endpointIndex = 1;
distance = this.Distance(endpointIndex == 0 ? P1 : P2);
return false;
}
}
public float DistanceFromLine(Vector2 P1, Vector2 P2)
{
return Math.Abs(((P2.y - P1.y) * this.x) - ((P2.x - P1.x) * this.y) + (P2.x * P1.y) - (P2.y * P1.x)) / P1.Distance(P2);
}
public Vector2 ProjectToLine(Vector2 A, Vector2 B, out bool withinLine)
{
Vector2 AP = this - A; //Vector from A to P
Vector2 AB = B - A; //Vector from A to B
float magnitudeAB = (AB.x * AB.x) + (AB.y * AB.y); //Magnitude of AB vector (its length squared)
float ABAPproduct = Vector2.Dot(AP, AB); //The DOT product of a_to_p and a_to_b
float distance = ABAPproduct / magnitudeAB; //The normalized "distance" from a to your closest point
withinLine = (distance >= 0f && distance <= 1f);
return A + AB * distance;
}
public bool ProjectsWithinLine(Vector2 v1, Vector2 v2)
{
bool tmp;
this.ProjectToLine(v1, v2, out tmp);
return tmp;
}
}
// Triangles
public struct Triangle2D
{
private Vector2 p1;
private Vector2 p2;
private Vector2 p3;
public Vector2 Point1
{
get { return p1; }
set { p1 = value; }
}
public Vector2 Point2
{
get { return p2; }
set { p2 = value; }
}
public Vector2 Point3
{
get { return p3; }
set { p3 = value; }
}
public Vector2[] TrianglePoints
{
get { return new Vector2[] { this.p1, this.p2, this.p3 }; }
}
public Triangle2D(Vector2 Pnt1, Vector2 Pnt2, Vector2 Pnt3)
{
this.p1 = new Vector2(Pnt1);
this.p2 = new Vector2(Pnt2);
this.p3 = new Vector2(Pnt3);
}
public Triangle2D(Vector2[] Points)
{
this.p1 = new Vector2(Points[0]);
this.p2 = new Vector2(Points[1]);
this.p3 = new Vector2(Points[2]);
}
public Triangle2D(float[] Point1, float[] Point2, float[] Point3)
{
this.p1 = new Vector2(Point1);
this.p2 = new Vector2(Point2);
this.p3 = new Vector2(Point3);
}
public Triangle2D(Triangle2D other)
{
this.p1 = new Vector2(other.p1);
this.p2 = new Vector2(other.p2);
this.p3 = new Vector2(other.p3);
}
public bool PointInUV1Triangle(Vector2 p, out float[] weights) //return whether point p is inside UV1 face, weights = barycentric weights
{
float padding = 0.0001f; //compensate for rounding etc. errors
float lowval = 0f - padding;
float highval = 1f + padding;
float denominator = ((p2.Y - p3.Y) * (p1.X - p3.X) + (p3.X - p2.X) * (p1.Y - p3.Y));
float w1 = ((p2.Y - p3.Y) * (p.X - p3.X) + (p3.X - p2.X) * (p.Y - p3.Y)) / denominator;
float w2 = ((p3.Y - p1.Y) * (p.X - p3.X) + (p1.X - p3.X) * (p.Y - p3.Y)) / denominator;
float w3 = 1f - w1 - w2;
weights = new float[] { w1, w2, w3 };
return lowval <= w1 && w1 <= highval && lowval <= w2 && w2 <= highval && lowval <= w3 && w3 <= highval;
}
public Vector2 Centroid
{
get { return new Vector2((p1.X + p2.X + p3.X) / 3f, (p1.Y + p2.Y + p3.Y) / 3f); }
}
public override string ToString()
{
return "(" + this.Point1.ToString() + "), (" + this.Point2.ToString() + "), (" + this.Point3.ToString() + ")";
}
}
public struct Triangle3D
{
private Vector3 p1;
private Vector3 p2;
private Vector3 p3;
public Vector3 Point1
{
get { return p1; }
set { p1 = value; }
}
public Vector3 Point2
{
get { return p2; }
set { p2 = value; }
}
public Vector3 Point3
{
get { return p3; }
set { p3 = value; }
}
public Vector3[] TrianglePoints
{
get { return new Vector3[] { this.p1, this.p2, this.p3 }; }
}
public Triangle3D(Vector3 Pnt1, Vector3 Pnt2, Vector3 Pnt3)
{
this.p1 = new Vector3(Pnt1);
this.p2 = new Vector3(Pnt2);
this.p3 = new Vector3(Pnt3);
}
public Triangle3D(Vector3[] Points)
{
this.p1 = new Vector3(Points[0]);
this.p2 = new Vector3(Points[1]);
this.p3 = new Vector3(Points[2]);
}
public Triangle3D(float[] Point1, float[] Point2, float[] Point3)
{
this.p1 = new Vector3(Point1);
this.p2 = new Vector3(Point2);
this.p3 = new Vector3(Point3);
}
public Triangle3D(Triangle3D other)
{
this.p1 = new Vector3(other.p1);
this.p2 = new Vector3(other.p2);
this.p3 = new Vector3(other.p3);
}
public Vector3 Centroid
{
get { return new Vector3((p1.X + p2.X + p3.X) / 3f, (p1.Y + p2.Y + p3.Y) / 3f, (p1.Z + p2.Z + p3.Z) / 3f); }
}
public Vector3 BarycentricCoordinates(Vector3 p) //return point barycentric coordinates
{
float denominatorInverse = 1f / ((p2.Y - p3.Y) * (p1.X - p3.X) + (p3.X - p2.X) * (p1.Y - p3.Y));
float w1 = ((p2.Y - p3.Y) * (p.X - p3.X) + (p3.X - p2.X) * (p.Y - p3.Y)) * denominatorInverse;
float w2 = ((p3.Y - p1.Y) * (p.X - p3.X) + (p1.X - p3.X) * (p.Y - p3.Y)) * denominatorInverse;
float w3 = 1f - w1 - w2;
return new Vector3(new float[] { w1, w2, w3 });
}
public Vector3 WorldCoordinates(Vector3 barycentricCoordinates) //return point Cartesian coordinates
{
return barycentricCoordinates.X * this.p1 + barycentricCoordinates.Y * this.p2 + barycentricCoordinates.Z * this.p3;
}
public override string ToString()
{
return "(" + this.Point1.ToString() + "), (" + this.Point2.ToString() + "), (" + this.Point3.ToString() + ")";
}
}
// Rotation quaternions and matrices
public class Quaternion
{
double x, y, z, w;
public float X { get { return (float)this.x; } }
public float Y { get { return (float)this.y; } }
public float Z { get { return (float)this.z; } }
public float W { get { return (float)this.w; } }
public float[] Coordinates { get { return new float[] { (float)this.x, (float)this.y, (float)this.z, (float)this.w }; } }
public Quaternion(float x, float y, float z, float w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public Quaternion(double x, double y, double z, double w)
{
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
public Quaternion(float[] quat)
{
this.x = quat[0];
this.y = quat[1];
this.z = quat[2];
this.w = quat[3];
}
public static Quaternion Identity
{
get { return new Quaternion(0, 0, 0, 1); }
}
public static Quaternion RotateYUpToZUp
{
get { return new Quaternion(0.7071068f, 0, 0, 0.7071068f); }
}
public bool isEmpty
{
get { return this.x == 0d && this.y == 0d && this.z == 0d && (this.w == 1d || this.w == 0d); }
}
public bool isIdentity
{
get { return this.x == 0d && this.y == 0d && this.z == 0d && this.w == 1d; }
}
public bool isNormalized
{
get
{
double magnitude = this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
return (Math.Abs(magnitude - 1d) <= .000001d);
}
}
public void Normalize()
{
double magnitude = Math.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
this.x = (float)(this.x / magnitude);
this.y = (float)(this.y / magnitude);
this.z = (float)(this.z / magnitude);
this.w = (float)(this.w / magnitude);
}
public void Balance()
{
double m = this.x * this.x - this.y * this.y - this.z * this.z;
if (m <= 1d)
{
this.w = (float)Math.Sqrt(1d - m);
}
else
{
this.Normalize();
}
}
public static Quaternion operator *(Quaternion q, Quaternion r)
{
return new Quaternion(r.w * q.x + r.x * q.w - r.y * q.z + r.z * q.y,
r.w * q.y + r.x * q.z + r.y * q.w - r.z * q.x,
r.w * q.z - r.x * q.y + r.y * q.x + r.z * q.w,
r.w * q.w - r.x * q.x - r.y * q.y - r.z * q.z);
}
public static Quaternion operator *(Quaternion q, float f)
{
Quaternion tmp = new Quaternion(q.x * f, q.y * f, q.z * f, q.w);
tmp.Normalize();
return tmp;
}
public static Quaternion operator *(Quaternion q, Vector3 v)
{
Quaternion tmp = new Quaternion(v.X, v.Y, v.Z, 0);
return q * tmp;
}
public Quaternion Conjugate()
{
return new Quaternion(-this.x, -this.y, -this.z, this.w);
}
public Quaternion Inverse()