forked from BAndysc/WoWDatabaseEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOldVector4.cs
1544 lines (1405 loc) · 72.2 KB
/
OldVector4.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
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// -----------------------------------------------------------------------------
// Original code from SlimMath project. http://code.google.com/p/slimmath/
// Greetings to SlimDX Group. Original code published with the following license:
// -----------------------------------------------------------------------------
/*
* Copyright (c) 2007-2011 SlimDX Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace TheMaths
{
[StructLayout(LayoutKind.Sequential)]
public struct VectorInt4
{
public int r;
public int g;
public int b;
public int a;
public VectorInt4(int r, int g, int b, int a)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct VectorByte4
{
public byte r;
public byte g;
public byte b;
public byte a;
public VectorByte4(byte r, byte g, byte b, byte a)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
}
/// <summary>
/// Represents a four dimensional mathematical vector.
/// </summary>
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct OldVector4 : IEquatable<OldVector4>, IFormattable
{
/// <summary>
/// The size of the <see cref="OldVector4"/> type, in bytes.
/// </summary>
public static readonly int SizeInBytes = Utilities.SizeOf<OldVector4>();
/// <summary>
/// A <see cref="OldVector4"/> with all of its components set to zero.
/// </summary>
public static readonly OldVector4 Zero = new OldVector4();
/// <summary>
/// The X unit <see cref="OldVector4"/> (1, 0, 0, 0).
/// </summary>
public static readonly OldVector4 UnitX = new OldVector4(1.0f, 0.0f, 0.0f, 0.0f);
/// <summary>
/// The Y unit <see cref="OldVector4"/> (0, 1, 0, 0).
/// </summary>
public static readonly OldVector4 UnitY = new OldVector4(0.0f, 1.0f, 0.0f, 0.0f);
/// <summary>
/// The Z unit <see cref="OldVector4"/> (0, 0, 1, 0).
/// </summary>
public static readonly OldVector4 UnitZ = new OldVector4(0.0f, 0.0f, 1.0f, 0.0f);
/// <summary>
/// The W unit <see cref="OldVector4"/> (0, 0, 0, 1).
/// </summary>
public static readonly OldVector4 UnitW = new OldVector4(0.0f, 0.0f, 0.0f, 1.0f);
/// <summary>
/// A <see cref="OldVector4"/> with all of its components set to one.
/// </summary>
public static readonly OldVector4 One = new OldVector4(1.0f, 1.0f, 1.0f, 1.0f);
/// <summary>
/// The X component of the vector.
/// </summary>
public float X;
/// <summary>
/// The Y component of the vector.
/// </summary>
public float Y;
/// <summary>
/// The Z component of the vector.
/// </summary>
public float Z;
/// <summary>
/// The W component of the vector.
/// </summary>
public float W;
/// <summary>
/// Initializes a new instance of the <see cref="OldVector4"/> struct.
/// </summary>
/// <param name="value">The value that will be assigned to all components.</param>
public OldVector4(float value)
{
X = value;
Y = value;
Z = value;
W = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="OldVector4"/> struct.
/// </summary>
/// <param name="x">Initial value for the X component of the vector.</param>
/// <param name="y">Initial value for the Y component of the vector.</param>
/// <param name="z">Initial value for the Z component of the vector.</param>
/// <param name="w">Initial value for the W component of the vector.</param>
public OldVector4(float x, float y, float z, float w)
{
X = x;
Y = y;
Z = z;
W = w;
}
/// <summary>
/// Initializes a new instance of the <see cref="OldVector4"/> struct.
/// </summary>
/// <param name="value">A vector containing the values with which to initialize the X, Y, and Z components.</param>
/// <param name="w">Initial value for the W component of the vector.</param>
public OldVector4(OldVector3 value, float w)
{
X = value.X;
Y = value.Y;
Z = value.Z;
W = w;
}
/// <summary>
/// Initializes a new instance of the <see cref="OldVector4"/> struct.
/// </summary>
/// <param name="value">A vector containing the values with which to initialize the X and Y components.</param>
/// <param name="z">Initial value for the Z component of the vector.</param>
/// <param name="w">Initial value for the W component of the vector.</param>
public OldVector4(OldVector2 value, float z, float w)
{
X = value.X;
Y = value.Y;
Z = z;
W = w;
}
/// <summary>
/// Initializes a new instance of the <see cref="OldVector4"/> struct.
/// </summary>
/// <param name="values">The values to assign to the X, Y, Z, and W components of the vector. This must be an array with four elements.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
public OldVector4(float[] values)
{
if (values == null)
throw new ArgumentNullException("values");
if (values.Length != 4)
throw new ArgumentOutOfRangeException("values", "There must be four and only four input values for OldVector4.");
X = values[0];
Y = values[1];
Z = values[2];
W = values[3];
}
/// <summary>
/// Gets a value indicting whether this instance is normalized.
/// </summary>
public bool IsNormalized
{
get { return MathUtil.IsOne((X * X) + (Y * Y) + (Z * Z) + (W * W)); }
}
/// <summary>
/// Gets a value indicting whether this vector is zero
/// </summary>
public bool IsZero
{
get { return X == 0 && Y == 0 && Z == 0 && W == 0; }
}
public OldVector3 XYZ => new (X, Y, Z);
/// <summary>
/// Gets or sets the component at the specified index.
/// </summary>
/// <value>The value of the X, Y, Z, or W component, depending on the index.</value>
/// <param name="index">The index of the component to access. Use 0 for the X component, 1 for the Y component, 2 for the Z component, and 3 for the W component.</param>
/// <returns>The value of the component at the specified index.</returns>
/// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 3].</exception>
public float this[int index]
{
get
{
switch (index)
{
case 0: return X;
case 1: return Y;
case 2: return Z;
case 3: return W;
}
throw new ArgumentOutOfRangeException("index", "Indices for OldVector4 run from 0 to 3, inclusive.");
}
set
{
switch (index)
{
case 0: X = value; break;
case 1: Y = value; break;
case 2: Z = value; break;
case 3: W = value; break;
default: throw new ArgumentOutOfRangeException("index", "Indices for OldVector4 run from 0 to 3, inclusive.");
}
}
}
/// <summary>
/// Calculates the length of the vector.
/// </summary>
/// <returns>The length of the vector.</returns>
/// <remarks>
/// <see cref="OldVector4.LengthSquared"/> may be preferred when only the relative length is needed
/// and speed is of the essence.
/// </remarks>
public float Length()
{
return (float)Math.Sqrt((X * X) + (Y * Y) + (Z * Z) + (W * W));
}
/// <summary>
/// Calculates the squared length of the vector.
/// </summary>
/// <returns>The squared length of the vector.</returns>
/// <remarks>
/// This method may be preferred to <see cref="OldVector4.Length"/> when only a relative length is needed
/// and speed is of the essence.
/// </remarks>
public float LengthSquared()
{
return (X * X) + (Y * Y) + (Z * Z) + (W * W);
}
/// <summary>
/// Converts the vector into a unit vector.
/// </summary>
public void Normalize()
{
float length = Length();
if (!MathUtil.IsZero(length))
{
float inverse = 1.0f / length;
X *= inverse;
Y *= inverse;
Z *= inverse;
W *= inverse;
}
}
/// <summary>
/// Creates an array containing the elements of the vector.
/// </summary>
/// <returns>A four-element array containing the components of the vector.</returns>
public float[] ToArray()
{
return new float[] { X, Y, Z, W };
}
/// <summary>
/// Adds two vectors.
/// </summary>
/// <param name="left">The first vector to add.</param>
/// <param name="right">The second vector to add.</param>
/// <param name="result">When the method completes, contains the sum of the two vectors.</param>
public static void Add(ref OldVector4 left, ref OldVector4 right, out OldVector4 result)
{
result = new OldVector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}
/// <summary>
/// Adds two vectors.
/// </summary>
/// <param name="left">The first vector to add.</param>
/// <param name="right">The second vector to add.</param>
/// <returns>The sum of the two vectors.</returns>
public static OldVector4 Add(OldVector4 left, OldVector4 right)
{
return new OldVector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}
/// <summary>
/// Perform a component-wise addition
/// </summary>
/// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be added to elements</param>
/// <param name="result">The vector with added scalar for each element.</param>
public static void Add(ref OldVector4 left, ref float right, out OldVector4 result)
{
result = new OldVector4(left.X + right, left.Y + right, left.Z + right, left.W + right);
}
/// <summary>
/// Perform a component-wise addition
/// </summary>
/// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be added to elements</param>
/// <returns>The vector with added scalar for each element.</returns>
public static OldVector4 Add(OldVector4 left, float right)
{
return new OldVector4(left.X + right, left.Y + right, left.Z + right, left.W + right);
}
/// <summary>
/// Subtracts two vectors.
/// </summary>
/// <param name="left">The first vector to subtract.</param>
/// <param name="right">The second vector to subtract.</param>
/// <param name="result">When the method completes, contains the difference of the two vectors.</param>
public static void Subtract(ref OldVector4 left, ref OldVector4 right, out OldVector4 result)
{
result = new OldVector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
}
/// <summary>
/// Subtracts two vectors.
/// </summary>
/// <param name="left">The first vector to subtract.</param>
/// <param name="right">The second vector to subtract.</param>
/// <returns>The difference of the two vectors.</returns>
public static OldVector4 Subtract(OldVector4 left, OldVector4 right)
{
return new OldVector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
}
/// <summary>
/// Perform a component-wise subtraction
/// </summary>
/// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be subtraced from elements</param>
/// <param name="result">The vector with subtracted scalar for each element.</param>
public static void Subtract(ref OldVector4 left, ref float right, out OldVector4 result)
{
result = new OldVector4(left.X - right, left.Y - right, left.Z - right, left.W - right);
}
/// <summary>
/// Perform a component-wise subtraction
/// </summary>
/// <param name="left">The input vector</param>
/// <param name="right">The scalar value to be subtraced from elements</param>
/// <returns>The vector with subtracted scalar for each element.</returns>
public static OldVector4 Subtract(OldVector4 left, float right)
{
return new OldVector4(left.X - right, left.Y - right, left.Z - right, left.W - right);
}
/// <summary>
/// Perform a component-wise subtraction
/// </summary>
/// <param name="left">The scalar value to be subtraced from elements</param>
/// <param name="right">The input vector.</param>
/// <param name="result">The vector with subtracted scalar for each element.</param>
public static void Subtract(ref float left, ref OldVector4 right, out OldVector4 result)
{
result = new OldVector4(left - right.X, left - right.Y, left - right.Z, left - right.W);
}
/// <summary>
/// Perform a component-wise subtraction
/// </summary>
/// <param name="left">The scalar value to be subtraced from elements</param>
/// <param name="right">The input vector.</param>
/// <returns>The vector with subtracted scalar for each element.</returns>
public static OldVector4 Subtract(float left, OldVector4 right)
{
return new OldVector4(left - right.X, left - right.Y, left - right.Z, left - right.W);
}
/// <summary>
/// Scales a vector by the given value.
/// </summary>
/// <param name="value">The vector to scale.</param>
/// <param name="scale">The amount by which to scale the vector.</param>
/// <param name="result">When the method completes, contains the scaled vector.</param>
public static void Multiply(ref OldVector4 value, float scale, out OldVector4 result)
{
result = new OldVector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
/// <summary>
/// Scales a vector by the given value.
/// </summary>
/// <param name="value">The vector to scale.</param>
/// <param name="scale">The amount by which to scale the vector.</param>
/// <returns>The scaled vector.</returns>
public static OldVector4 Multiply(OldVector4 value, float scale)
{
return new OldVector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
/// <summary>
/// Multiplies a vector with another by performing component-wise multiplication.
/// </summary>
/// <param name="left">The first vector to multiply.</param>
/// <param name="right">The second vector to multiply.</param>
/// <param name="result">When the method completes, contains the multiplied vector.</param>
public static void Multiply(ref OldVector4 left, ref OldVector4 right, out OldVector4 result)
{
result = new OldVector4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
}
/// <summary>
/// Multiplies a vector with another by performing component-wise multiplication.
/// </summary>
/// <param name="left">The first vector to multiply.</param>
/// <param name="right">The second vector to multiply.</param>
/// <returns>The multiplied vector.</returns>
public static OldVector4 Multiply(OldVector4 left, OldVector4 right)
{
return new OldVector4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
}
/// <summary>
/// Scales a vector by the given value.
/// </summary>
/// <param name="value">The vector to scale.</param>
/// <param name="scale">The amount by which to scale the vector.</param>
/// <param name="result">When the method completes, contains the scaled vector.</param>
public static void Divide(ref OldVector4 value, float scale, out OldVector4 result)
{
result = new OldVector4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
/// <summary>
/// Scales a vector by the given value.
/// </summary>
/// <param name="value">The vector to scale.</param>
/// <param name="scale">The amount by which to scale the vector.</param>
/// <returns>The scaled vector.</returns>
public static OldVector4 Divide(OldVector4 value, float scale)
{
return new OldVector4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
/// <summary>
/// Scales a vector by the given value.
/// </summary>
/// <param name="scale">The amount by which to scale the vector.</param>
/// <param name="value">The vector to scale.</param>
/// <param name="result">When the method completes, contains the scaled vector.</param>
public static void Divide(float scale, ref OldVector4 value, out OldVector4 result)
{
result = new OldVector4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W);
}
/// <summary>
/// Scales a vector by the given value.
/// </summary>
/// <param name="value">The vector to scale.</param>
/// <param name="scale">The amount by which to scale the vector.</param>
/// <returns>The scaled vector.</returns>
public static OldVector4 Divide(float scale, OldVector4 value)
{
return new OldVector4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W);
}
/// <summary>
/// Reverses the direction of a given vector.
/// </summary>
/// <param name="value">The vector to negate.</param>
/// <param name="result">When the method completes, contains a vector facing in the opposite direction.</param>
public static void Negate(ref OldVector4 value, out OldVector4 result)
{
result = new OldVector4(-value.X, -value.Y, -value.Z, -value.W);
}
/// <summary>
/// Reverses the direction of a given vector.
/// </summary>
/// <param name="value">The vector to negate.</param>
/// <returns>A vector facing in the opposite direction.</returns>
public static OldVector4 Negate(OldVector4 value)
{
return new OldVector4(-value.X, -value.Y, -value.Z, -value.W);
}
/// <summary>
/// Returns a <see cref="OldVector4"/> containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 4D triangle.
/// </summary>
/// <param name="value1">A <see cref="OldVector4"/> containing the 4D Cartesian coordinates of vertex 1 of the triangle.</param>
/// <param name="value2">A <see cref="OldVector4"/> containing the 4D Cartesian coordinates of vertex 2 of the triangle.</param>
/// <param name="value3">A <see cref="OldVector4"/> containing the 4D Cartesian coordinates of vertex 3 of the triangle.</param>
/// <param name="amount1">Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in <paramref name="value2"/>).</param>
/// <param name="amount2">Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in <paramref name="value3"/>).</param>
/// <param name="result">When the method completes, contains the 4D Cartesian coordinates of the specified point.</param>
public static void Barycentric(ref OldVector4 value1, ref OldVector4 value2, ref OldVector4 value3, float amount1, float amount2, out OldVector4 result)
{
result = new OldVector4((value1.X + (amount1 * (value2.X - value1.X))) + (amount2 * (value3.X - value1.X)),
(value1.Y + (amount1 * (value2.Y - value1.Y))) + (amount2 * (value3.Y - value1.Y)),
(value1.Z + (amount1 * (value2.Z - value1.Z))) + (amount2 * (value3.Z - value1.Z)),
(value1.W + (amount1 * (value2.W - value1.W))) + (amount2 * (value3.W - value1.W)));
}
/// <summary>
/// Returns a <see cref="OldVector4"/> containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 4D triangle.
/// </summary>
/// <param name="value1">A <see cref="OldVector4"/> containing the 4D Cartesian coordinates of vertex 1 of the triangle.</param>
/// <param name="value2">A <see cref="OldVector4"/> containing the 4D Cartesian coordinates of vertex 2 of the triangle.</param>
/// <param name="value3">A <see cref="OldVector4"/> containing the 4D Cartesian coordinates of vertex 3 of the triangle.</param>
/// <param name="amount1">Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in <paramref name="value2"/>).</param>
/// <param name="amount2">Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in <paramref name="value3"/>).</param>
/// <returns>A new <see cref="OldVector4"/> containing the 4D Cartesian coordinates of the specified point.</returns>
public static OldVector4 Barycentric(OldVector4 value1, OldVector4 value2, OldVector4 value3, float amount1, float amount2)
{
OldVector4 result;
Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out result);
return result;
}
/// <summary>
/// Restricts a value to be within a specified range.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <param name="result">When the method completes, contains the clamped value.</param>
public static void Clamp(ref OldVector4 value, ref OldVector4 min, ref OldVector4 max, out OldVector4 result)
{
float x = value.X;
x = (x > max.X) ? max.X : x;
x = (x < min.X) ? min.X : x;
float y = value.Y;
y = (y > max.Y) ? max.Y : y;
y = (y < min.Y) ? min.Y : y;
float z = value.Z;
z = (z > max.Z) ? max.Z : z;
z = (z < min.Z) ? min.Z : z;
float w = value.W;
w = (w > max.W) ? max.W : w;
w = (w < min.W) ? min.W : w;
result = new OldVector4(x, y, z, w);
}
/// <summary>
/// Restricts a value to be within a specified range.
/// </summary>
/// <param name="value">The value to clamp.</param>
/// <param name="min">The minimum value.</param>
/// <param name="max">The maximum value.</param>
/// <returns>The clamped value.</returns>
public static OldVector4 Clamp(OldVector4 value, OldVector4 min, OldVector4 max)
{
OldVector4 result;
Clamp(ref value, ref min, ref max, out result);
return result;
}
/// <summary>
/// Calculates the distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">When the method completes, contains the distance between the two vectors.</param>
/// <remarks>
/// <see cref="OldVector4.DistanceSquared(ref OldVector4, ref OldVector4, out float)"/> may be preferred when only the relative distance is needed
/// and speed is of the essence.
/// </remarks>
public static void Distance(ref OldVector4 value1, ref OldVector4 value2, out float result)
{
float x = value1.X - value2.X;
float y = value1.Y - value2.Y;
float z = value1.Z - value2.Z;
float w = value1.W - value2.W;
result = (float)Math.Sqrt((x * x) + (y * y) + (z * z) + (w * w));
}
/// <summary>
/// Calculates the distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The distance between the two vectors.</returns>
/// <remarks>
/// <see cref="OldVector4.DistanceSquared(OldVector4, OldVector4)"/> may be preferred when only the relative distance is needed
/// and speed is of the essence.
/// </remarks>
public static float Distance(OldVector4 value1, OldVector4 value2)
{
float x = value1.X - value2.X;
float y = value1.Y - value2.Y;
float z = value1.Z - value2.Z;
float w = value1.W - value2.W;
return (float)Math.Sqrt((x * x) + (y * y) + (z * z) + (w * w));
}
/// <summary>
/// Calculates the squared distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">When the method completes, contains the squared distance between the two vectors.</param>
/// <remarks>Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static void DistanceSquared(ref OldVector4 value1, ref OldVector4 value2, out float result)
{
float x = value1.X - value2.X;
float y = value1.Y - value2.Y;
float z = value1.Z - value2.Z;
float w = value1.W - value2.W;
result = (x * x) + (y * y) + (z * z) + (w * w);
}
/// <summary>
/// Calculates the squared distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The squared distance between the two vectors.</returns>
/// <remarks>Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
/// </remarks>
public static float DistanceSquared(OldVector4 value1, OldVector4 value2)
{
float x = value1.X - value2.X;
float y = value1.Y - value2.Y;
float z = value1.Z - value2.Z;
float w = value1.W - value2.W;
return (x * x) + (y * y) + (z * z) + (w * w);
}
/// <summary>
/// Calculates the dot product of two vectors.
/// </summary>
/// <param name="left">First source vector</param>
/// <param name="right">Second source vector.</param>
/// <param name="result">When the method completes, contains the dot product of the two vectors.</param>
public static void Dot(ref OldVector4 left, ref OldVector4 right, out float result)
{
result = (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z) + (left.W * right.W);
}
/// <summary>
/// Calculates the dot product of two vectors.
/// </summary>
/// <param name="left">First source vector.</param>
/// <param name="right">Second source vector.</param>
/// <returns>The dot product of the two vectors.</returns>
public static float Dot(OldVector4 left, OldVector4 right)
{
return (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z) + (left.W * right.W);
}
/// <summary>
/// Converts the vector into a unit vector.
/// </summary>
/// <param name="value">The vector to normalize.</param>
/// <param name="result">When the method completes, contains the normalized vector.</param>
public static void Normalize(ref OldVector4 value, out OldVector4 result)
{
OldVector4 temp = value;
result = temp;
result.Normalize();
}
/// <summary>
/// Converts the vector into a unit vector.
/// </summary>
/// <param name="value">The vector to normalize.</param>
/// <returns>The normalized vector.</returns>
public static OldVector4 Normalize(OldVector4 value)
{
value.Normalize();
return value;
}
/// <summary>
/// Performs a linear interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <param name="result">When the method completes, contains the linear interpolation of the two vectors.</param>
/// <remarks>
/// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
/// </remarks>
public static void Lerp(ref OldVector4 start, ref OldVector4 end, float amount, out OldVector4 result)
{
result.X = MathUtil.Lerp(start.X, end.X, amount);
result.Y = MathUtil.Lerp(start.Y, end.Y, amount);
result.Z = MathUtil.Lerp(start.Z, end.Z, amount);
result.W = MathUtil.Lerp(start.W, end.W, amount);
}
/// <summary>
/// Performs a linear interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <returns>The linear interpolation of the two vectors.</returns>
/// <remarks>
/// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
/// </remarks>
public static OldVector4 Lerp(OldVector4 start, OldVector4 end, float amount)
{
OldVector4 result;
Lerp(ref start, ref end, amount, out result);
return result;
}
/// <summary>
/// Performs a cubic interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <param name="result">When the method completes, contains the cubic interpolation of the two vectors.</param>
public static void SmoothStep(ref OldVector4 start, ref OldVector4 end, float amount, out OldVector4 result)
{
amount = MathUtil.SmoothStep(amount);
Lerp(ref start, ref end, amount, out result);
}
/// <summary>
/// Performs a cubic interpolation between two vectors.
/// </summary>
/// <param name="start">Start vector.</param>
/// <param name="end">End vector.</param>
/// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
/// <returns>The cubic interpolation of the two vectors.</returns>
public static OldVector4 SmoothStep(OldVector4 start, OldVector4 end, float amount)
{
OldVector4 result;
SmoothStep(ref start, ref end, amount, out result);
return result;
}
/// <summary>
/// Performs a Hermite spline interpolation.
/// </summary>
/// <param name="value1">First source position vector.</param>
/// <param name="tangent1">First source tangent vector.</param>
/// <param name="value2">Second source position vector.</param>
/// <param name="tangent2">Second source tangent vector.</param>
/// <param name="amount">Weighting factor.</param>
/// <param name="result">When the method completes, contains the result of the Hermite spline interpolation.</param>
public static void Hermite(ref OldVector4 value1, ref OldVector4 tangent1, ref OldVector4 value2, ref OldVector4 tangent2, float amount, out OldVector4 result)
{
float squared = amount * amount;
float cubed = amount * squared;
float part1 = ((2.0f * cubed) - (3.0f * squared)) + 1.0f;
float part2 = (-2.0f * cubed) + (3.0f * squared);
float part3 = (cubed - (2.0f * squared)) + amount;
float part4 = cubed - squared;
result = new OldVector4((((value1.X * part1) + (value2.X * part2)) + (tangent1.X * part3)) + (tangent2.X * part4),
(((value1.Y * part1) + (value2.Y * part2)) + (tangent1.Y * part3)) + (tangent2.Y * part4),
(((value1.Z * part1) + (value2.Z * part2)) + (tangent1.Z * part3)) + (tangent2.Z * part4),
(((value1.W * part1) + (value2.W * part2)) + (tangent1.W * part3)) + (tangent2.W * part4));
}
/// <summary>
/// Performs a Hermite spline interpolation.
/// </summary>
/// <param name="value1">First source position vector.</param>
/// <param name="tangent1">First source tangent vector.</param>
/// <param name="value2">Second source position vector.</param>
/// <param name="tangent2">Second source tangent vector.</param>
/// <param name="amount">Weighting factor.</param>
/// <returns>The result of the Hermite spline interpolation.</returns>
public static OldVector4 Hermite(OldVector4 value1, OldVector4 tangent1, OldVector4 value2, OldVector4 tangent2, float amount)
{
OldVector4 result;
Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
return result;
}
/// <summary>
/// Performs a Catmull-Rom interpolation using the specified positions.
/// </summary>
/// <param name="value1">The first position in the interpolation.</param>
/// <param name="value2">The second position in the interpolation.</param>
/// <param name="value3">The third position in the interpolation.</param>
/// <param name="value4">The fourth position in the interpolation.</param>
/// <param name="amount">Weighting factor.</param>
/// <param name="result">When the method completes, contains the result of the Catmull-Rom interpolation.</param>
public static void CatmullRom(ref OldVector4 value1, ref OldVector4 value2, ref OldVector4 value3, ref OldVector4 value4, float amount, out OldVector4 result)
{
float squared = amount * amount;
float cubed = amount * squared;
result.X = 0.5f * ((((2.0f * value2.X) + ((-value1.X + value3.X) * amount)) + (((((2.0f * value1.X) - (5.0f * value2.X)) + (4.0f * value3.X)) - value4.X) * squared)) + ((((-value1.X + (3.0f * value2.X)) - (3.0f * value3.X)) + value4.X) * cubed));
result.Y = 0.5f * ((((2.0f * value2.Y) + ((-value1.Y + value3.Y) * amount)) + (((((2.0f * value1.Y) - (5.0f * value2.Y)) + (4.0f * value3.Y)) - value4.Y) * squared)) + ((((-value1.Y + (3.0f * value2.Y)) - (3.0f * value3.Y)) + value4.Y) * cubed));
result.Z = 0.5f * ((((2.0f * value2.Z) + ((-value1.Z + value3.Z) * amount)) + (((((2.0f * value1.Z) - (5.0f * value2.Z)) + (4.0f * value3.Z)) - value4.Z) * squared)) + ((((-value1.Z + (3.0f * value2.Z)) - (3.0f * value3.Z)) + value4.Z) * cubed));
result.W = 0.5f * ((((2.0f * value2.W) + ((-value1.W + value3.W) * amount)) + (((((2.0f * value1.W) - (5.0f * value2.W)) + (4.0f * value3.W)) - value4.W) * squared)) + ((((-value1.W + (3.0f * value2.W)) - (3.0f * value3.W)) + value4.W) * cubed));
}
/// <summary>
/// Performs a Catmull-Rom interpolation using the specified positions.
/// </summary>
/// <param name="value1">The first position in the interpolation.</param>
/// <param name="value2">The second position in the interpolation.</param>
/// <param name="value3">The third position in the interpolation.</param>
/// <param name="value4">The fourth position in the interpolation.</param>
/// <param name="amount">Weighting factor.</param>
/// <returns>A vector that is the result of the Catmull-Rom interpolation.</returns>
public static OldVector4 CatmullRom(OldVector4 value1, OldVector4 value2, OldVector4 value3, OldVector4 value4, float amount)
{
OldVector4 result;
CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out result);
return result;
}
/// <summary>
/// Returns a vector containing the largest components of the specified vectors.
/// </summary>
/// <param name="left">The first source vector.</param>
/// <param name="right">The second source vector.</param>
/// <param name="result">When the method completes, contains an new vector composed of the largest components of the source vectors.</param>
public static void Max(ref OldVector4 left, ref OldVector4 right, out OldVector4 result)
{
result.X = (left.X > right.X) ? left.X : right.X;
result.Y = (left.Y > right.Y) ? left.Y : right.Y;
result.Z = (left.Z > right.Z) ? left.Z : right.Z;
result.W = (left.W > right.W) ? left.W : right.W;
}
/// <summary>
/// Returns a vector containing the largest components of the specified vectors.
/// </summary>
/// <param name="left">The first source vector.</param>
/// <param name="right">The second source vector.</param>
/// <returns>A vector containing the largest components of the source vectors.</returns>
public static OldVector4 Max(OldVector4 left, OldVector4 right)
{
OldVector4 result;
Max(ref left, ref right, out result);
return result;
}
/// <summary>
/// Returns a vector containing the smallest components of the specified vectors.
/// </summary>
/// <param name="left">The first source vector.</param>
/// <param name="right">The second source vector.</param>
/// <param name="result">When the method completes, contains an new vector composed of the smallest components of the source vectors.</param>
public static void Min(ref OldVector4 left, ref OldVector4 right, out OldVector4 result)
{
result.X = (left.X < right.X) ? left.X : right.X;
result.Y = (left.Y < right.Y) ? left.Y : right.Y;
result.Z = (left.Z < right.Z) ? left.Z : right.Z;
result.W = (left.W < right.W) ? left.W : right.W;
}
/// <summary>
/// Returns a vector containing the smallest components of the specified vectors.
/// </summary>
/// <param name="left">The first source vector.</param>
/// <param name="right">The second source vector.</param>
/// <returns>A vector containing the smallest components of the source vectors.</returns>
public static OldVector4 Min(OldVector4 left, OldVector4 right)
{
OldVector4 result;
Min(ref left, ref right, out result);
return result;
}
/// <summary>
/// Orthogonalizes a list of vectors.
/// </summary>
/// <param name="destination">The list of orthogonalized vectors.</param>
/// <param name="source">The list of vectors to orthogonalize.</param>
/// <remarks>
/// <para>Orthogonalization is the process of making all vectors orthogonal to each other. This
/// means that any given vector in the list will be orthogonal to any other given vector in the
/// list.</para>
/// <para>Because this method uses the modified Gram-Schmidt process, the resulting vectors
/// tend to be numerically unstable. The numeric stability decreases according to the vectors
/// position in the list so that the first vector is the most stable and the last vector is the
/// least stable.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="source"/> or <paramref name="destination"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="destination"/> is shorter in length than <paramref name="source"/>.</exception>
public static void Orthogonalize(OldVector4[] destination, params OldVector4[] source)
{
//Uses the modified Gram-Schmidt process.
//q1 = m1
//q2 = m2 - ((q1 ⋅ m2) / (q1 ⋅ q1)) * q1
//q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2
//q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3
//q5 = ...
if (source == null)
throw new ArgumentNullException("source");
if (destination == null)
throw new ArgumentNullException("destination");
if (destination.Length < source.Length)
throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
for (int i = 0; i < source.Length; ++i)
{
OldVector4 newvector = source[i];
for (int r = 0; r < i; ++r)
{
newvector -= (OldVector4.Dot(destination[r], newvector) / OldVector4.Dot(destination[r], destination[r])) * destination[r];
}
destination[i] = newvector;
}
}
/// <summary>
/// Orthonormalizes a list of vectors.
/// </summary>
/// <param name="destination">The list of orthonormalized vectors.</param>
/// <param name="source">The list of vectors to orthonormalize.</param>
/// <remarks>
/// <para>Orthonormalization is the process of making all vectors orthogonal to each
/// other and making all vectors of unit length. This means that any given vector will
/// be orthogonal to any other given vector in the list.</para>
/// <para>Because this method uses the modified Gram-Schmidt process, the resulting vectors
/// tend to be numerically unstable. The numeric stability decreases according to the vectors
/// position in the list so that the first vector is the most stable and the last vector is the
/// least stable.</para>
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="source"/> or <paramref name="destination"/> is <c>null</c>.</exception>