forked from MonoGame/MonoGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3.cs
1495 lines (1352 loc) · 67.2 KB
/
Vector3.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
// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Diagnostics;
using System.Text;
using System.Runtime.Serialization;
namespace Microsoft.Xna.Framework
{
/// <summary>
/// Describes a 3D-vector.
/// </summary>
#if XNADESIGNPROVIDED
[System.ComponentModel.TypeConverter(typeof(Microsoft.Xna.Framework.Design.Vector3TypeConverter))]
#endif
[DataContract]
[DebuggerDisplay("{DebugDisplayString,nq}")]
public struct Vector3 : IEquatable<Vector3>
{
#region Private Fields
private static readonly Vector3 zero = new Vector3(0f, 0f, 0f);
private static readonly Vector3 one = new Vector3(1f, 1f, 1f);
private static readonly Vector3 unitX = new Vector3(1f, 0f, 0f);
private static readonly Vector3 unitY = new Vector3(0f, 1f, 0f);
private static readonly Vector3 unitZ = new Vector3(0f, 0f, 1f);
private static readonly Vector3 up = new Vector3(0f, 1f, 0f);
private static readonly Vector3 down = new Vector3(0f, -1f, 0f);
private static readonly Vector3 right = new Vector3(1f, 0f, 0f);
private static readonly Vector3 left = new Vector3(-1f, 0f, 0f);
private static readonly Vector3 forward = new Vector3(0f, 0f, -1f);
private static readonly Vector3 backward = new Vector3(0f, 0f, 1f);
#endregion
#region Public Fields
/// <summary>
/// The x coordinate of this <see cref="Vector3"/>.
/// </summary>
[DataMember]
public float X;
/// <summary>
/// The y coordinate of this <see cref="Vector3"/>.
/// </summary>
[DataMember]
public float Y;
/// <summary>
/// The z coordinate of this <see cref="Vector3"/>.
/// </summary>
[DataMember]
public float Z;
#endregion
#region Public Properties
/// <summary>
/// Returns a <see cref="Vector3"/> with components 0, 0, 0.
/// </summary>
public static Vector3 Zero
{
get { return zero; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components 1, 1, 1.
/// </summary>
public static Vector3 One
{
get { return one; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components 1, 0, 0.
/// </summary>
public static Vector3 UnitX
{
get { return unitX; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components 0, 1, 0.
/// </summary>
public static Vector3 UnitY
{
get { return unitY; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components 0, 0, 1.
/// </summary>
public static Vector3 UnitZ
{
get { return unitZ; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components 0, 1, 0.
/// </summary>
public static Vector3 Up
{
get { return up; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components 0, -1, 0.
/// </summary>
public static Vector3 Down
{
get { return down; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components 1, 0, 0.
/// </summary>
public static Vector3 Right
{
get { return right; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components -1, 0, 0.
/// </summary>
public static Vector3 Left
{
get { return left; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components 0, 0, -1.
/// </summary>
public static Vector3 Forward
{
get { return forward; }
}
/// <summary>
/// Returns a <see cref="Vector3"/> with components 0, 0, 1.
/// </summary>
public static Vector3 Backward
{
get { return backward; }
}
#endregion
#region Internal Properties
internal string DebugDisplayString
{
get
{
return string.Concat(
this.X.ToString(), " ",
this.Y.ToString(), " ",
this.Z.ToString()
);
}
}
#endregion
#region Constructors
/// <summary>
/// Constructs a 3d vector with X, Y and Z from three values.
/// </summary>
/// <param name="x">The x coordinate in 3d-space.</param>
/// <param name="y">The y coordinate in 3d-space.</param>
/// <param name="z">The z coordinate in 3d-space.</param>
public Vector3(float x, float y, float z)
{
this.X = x;
this.Y = y;
this.Z = z;
}
/// <summary>
/// Constructs a 3d vector with X, Y and Z set to the same value.
/// </summary>
/// <param name="value">The x, y and z coordinates in 3d-space.</param>
public Vector3(float value)
{
this.X = value;
this.Y = value;
this.Z = value;
}
/// <summary>
/// Constructs a 3d vector with X, Y from <see cref="Vector2"/> and Z from a scalar.
/// </summary>
/// <param name="value">The x and y coordinates in 3d-space.</param>
/// <param name="z">The z coordinate in 3d-space.</param>
public Vector3(Vector2 value, float z)
{
this.X = value.X;
this.Y = value.Y;
this.Z = z;
}
#endregion
#region Public Methods
/// <summary>
/// Performs vector addition on <paramref name="value1"/> and <paramref name="value2"/>.
/// </summary>
/// <param name="value1">The first vector to add.</param>
/// <param name="value2">The second vector to add.</param>
/// <returns>The result of the vector addition.</returns>
public static Vector3 Add(Vector3 value1, Vector3 value2)
{
value1.X += value2.X;
value1.Y += value2.Y;
value1.Z += value2.Z;
return value1;
}
/// <summary>
/// Performs vector addition on <paramref name="value1"/> and
/// <paramref name="value2"/>, storing the result of the
/// addition in <paramref name="result"/>.
/// </summary>
/// <param name="value1">The first vector to add.</param>
/// <param name="value2">The second vector to add.</param>
/// <param name="result">The result of the vector addition.</param>
public static void Add(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
{
result.X = value1.X + value2.X;
result.Y = value1.Y + value2.Y;
result.Z = value1.Z + value2.Z;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 3d-triangle.
/// </summary>
/// <param name="value1">The first vector of 3d-triangle.</param>
/// <param name="value2">The second vector of 3d-triangle.</param>
/// <param name="value3">The third vector of 3d-triangle.</param>
/// <param name="amount1">Barycentric scalar <c>b2</c> which represents a weighting factor towards second vector of 3d-triangle.</param>
/// <param name="amount2">Barycentric scalar <c>b3</c> which represents a weighting factor towards third vector of 3d-triangle.</param>
/// <returns>The cartesian translation of barycentric coordinates.</returns>
public static Vector3 Barycentric(Vector3 value1, Vector3 value2, Vector3 value3, float amount1, float amount2)
{
return new Vector3(
MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2),
MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2),
MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, amount1, amount2));
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 3d-triangle.
/// </summary>
/// <param name="value1">The first vector of 3d-triangle.</param>
/// <param name="value2">The second vector of 3d-triangle.</param>
/// <param name="value3">The third vector of 3d-triangle.</param>
/// <param name="amount1">Barycentric scalar <c>b2</c> which represents a weighting factor towards second vector of 3d-triangle.</param>
/// <param name="amount2">Barycentric scalar <c>b3</c> which represents a weighting factor towards third vector of 3d-triangle.</param>
/// <param name="result">The cartesian translation of barycentric coordinates as an output parameter.</param>
public static void Barycentric(ref Vector3 value1, ref Vector3 value2, ref Vector3 value3, float amount1, float amount2, out Vector3 result)
{
result.X = MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2);
result.Y = MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2);
result.Z = MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, amount1, amount2);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains CatmullRom interpolation of the specified vectors.
/// </summary>
/// <param name="value1">The first vector in interpolation.</param>
/// <param name="value2">The second vector in interpolation.</param>
/// <param name="value3">The third vector in interpolation.</param>
/// <param name="value4">The fourth vector in interpolation.</param>
/// <param name="amount">Weighting factor.</param>
/// <returns>The result of CatmullRom interpolation.</returns>
public static Vector3 CatmullRom(Vector3 value1, Vector3 value2, Vector3 value3, Vector3 value4, float amount)
{
return new Vector3(
MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount),
MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount),
MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount));
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains CatmullRom interpolation of the specified vectors.
/// </summary>
/// <param name="value1">The first vector in interpolation.</param>
/// <param name="value2">The second vector in interpolation.</param>
/// <param name="value3">The third vector in interpolation.</param>
/// <param name="value4">The fourth vector in interpolation.</param>
/// <param name="amount">Weighting factor.</param>
/// <param name="result">The result of CatmullRom interpolation as an output parameter.</param>
public static void CatmullRom(ref Vector3 value1, ref Vector3 value2, ref Vector3 value3, ref Vector3 value4, float amount, out Vector3 result)
{
result.X = MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount);
result.Y = MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount);
result.Z = MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount);
}
/// <summary>
/// Round the members of this <see cref="Vector3"/> towards positive infinity.
/// </summary>
public void Ceiling()
{
X = MathF.Ceiling(X);
Y = MathF.Ceiling(Y);
Z = MathF.Ceiling(Z);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains members from another vector rounded towards positive infinity.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <returns>The rounded <see cref="Vector3"/>.</returns>
public static Vector3 Ceiling(Vector3 value)
{
value.X = MathF.Ceiling(value.X);
value.Y = MathF.Ceiling(value.Y);
value.Z = MathF.Ceiling(value.Z);
return value;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains members from another vector rounded towards positive infinity.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <param name="result">The rounded <see cref="Vector3"/>.</param>
public static void Ceiling(ref Vector3 value, out Vector3 result)
{
result.X = MathF.Ceiling(value.X);
result.Y = MathF.Ceiling(value.Y);
result.Z = MathF.Ceiling(value.Z);
}
/// <summary>
/// Clamps the specified value within a range.
/// </summary>
/// <param name="value1">The value to clamp.</param>
/// <param name="min">The min value.</param>
/// <param name="max">The max value.</param>
/// <returns>The clamped value.</returns>
public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max)
{
return new Vector3(
MathHelper.Clamp(value1.X, min.X, max.X),
MathHelper.Clamp(value1.Y, min.Y, max.Y),
MathHelper.Clamp(value1.Z, min.Z, max.Z));
}
/// <summary>
/// Clamps the specified value within a range.
/// </summary>
/// <param name="value1">The value to clamp.</param>
/// <param name="min">The min value.</param>
/// <param name="max">The max value.</param>
/// <param name="result">The clamped value as an output parameter.</param>
public static void Clamp(ref Vector3 value1, ref Vector3 min, ref Vector3 max, out Vector3 result)
{
result.X = MathHelper.Clamp(value1.X, min.X, max.X);
result.Y = MathHelper.Clamp(value1.Y, min.Y, max.Y);
result.Z = MathHelper.Clamp(value1.Z, min.Z, max.Z);
}
/// <summary>
/// Computes the cross product of two vectors.
/// </summary>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <returns>The cross product of two vectors.</returns>
public static Vector3 Cross(Vector3 vector1, Vector3 vector2)
{
Cross(ref vector1, ref vector2, out vector1);
return vector1;
}
/// <summary>
/// Computes the cross product of two vectors.
/// </summary>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <param name="result">The cross product of two vectors as an output parameter.</param>
public static void Cross(ref Vector3 vector1, ref Vector3 vector2, out Vector3 result)
{
var x = vector1.Y * vector2.Z - vector2.Y * vector1.Z;
var y = -(vector1.X * vector2.Z - vector2.X * vector1.Z);
var z = vector1.X * vector2.Y - vector2.X * vector1.Y;
result.X = x;
result.Y = y;
result.Z = z;
}
/// <summary>
/// Returns the distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The distance between two vectors.</returns>
public static float Distance(Vector3 value1, Vector3 value2)
{
float result;
DistanceSquared(ref value1, ref value2, out result);
return MathF.Sqrt(result);
}
/// <summary>
/// Returns the distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">The distance between two vectors as an output parameter.</param>
public static void Distance(ref Vector3 value1, ref Vector3 value2, out float result)
{
DistanceSquared(ref value1, ref value2, out result);
result = MathF.Sqrt(result);
}
/// <summary>
/// Returns 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 two vectors.</returns>
public static float DistanceSquared(Vector3 value1, Vector3 value2)
{
return (value1.X - value2.X) * (value1.X - value2.X) +
(value1.Y - value2.Y) * (value1.Y - value2.Y) +
(value1.Z - value2.Z) * (value1.Z - value2.Z);
}
/// <summary>
/// Returns the squared distance between two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">The squared distance between two vectors as an output parameter.</param>
public static void DistanceSquared(ref Vector3 value1, ref Vector3 value2, out float result)
{
result = (value1.X - value2.X) * (value1.X - value2.X) +
(value1.Y - value2.Y) * (value1.Y - value2.Y) +
(value1.Z - value2.Z) * (value1.Z - value2.Z);
}
/// <summary>
/// Divides the components of a <see cref="Vector3"/> by the components of another <see cref="Vector3"/>.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="value2">Divisor <see cref="Vector3"/>.</param>
/// <returns>The result of dividing the vectors.</returns>
public static Vector3 Divide(Vector3 value1, Vector3 value2)
{
value1.X /= value2.X;
value1.Y /= value2.Y;
value1.Z /= value2.Z;
return value1;
}
/// <summary>
/// Divides the components of a <see cref="Vector3"/> by a scalar.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="divider">Divisor scalar.</param>
/// <returns>The result of dividing a vector by a scalar.</returns>
public static Vector3 Divide(Vector3 value1, float divider)
{
float factor = 1 / divider;
value1.X *= factor;
value1.Y *= factor;
value1.Z *= factor;
return value1;
}
/// <summary>
/// Divides the components of a <see cref="Vector3"/> by a scalar.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="divider">Divisor scalar.</param>
/// <param name="result">The result of dividing a vector by a scalar as an output parameter.</param>
public static void Divide(ref Vector3 value1, float divider, out Vector3 result)
{
float factor = 1 / divider;
result.X = value1.X * factor;
result.Y = value1.Y * factor;
result.Z = value1.Z * factor;
}
/// <summary>
/// Divides the components of a <see cref="Vector3"/> by the components of another <see cref="Vector3"/>.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="value2">Divisor <see cref="Vector3"/>.</param>
/// <param name="result">The result of dividing the vectors as an output parameter.</param>
public static void Divide(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
{
result.X = value1.X / value2.X;
result.Y = value1.Y / value2.Y;
result.Z = value1.Z / value2.Z;
}
/// <summary>
/// Returns a dot product of two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The dot product of two vectors.</returns>
public static float Dot(Vector3 value1, Vector3 value2)
{
return value1.X * value2.X + value1.Y * value2.Y + value1.Z * value2.Z;
}
/// <summary>
/// Returns a dot product of two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">The dot product of two vectors as an output parameter.</param>
public static void Dot(ref Vector3 value1, ref Vector3 value2, out float result)
{
result = value1.X * value2.X + value1.Y * value2.Y + value1.Z * value2.Z;
}
/// <summary>
/// Compares whether current instance is equal to specified <see cref="Object"/>.
/// </summary>
/// <param name="obj">The <see cref="Object"/> to compare.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public override bool Equals(object obj)
{
if (!(obj is Vector3))
return false;
var other = (Vector3)obj;
return X == other.X &&
Y == other.Y &&
Z == other.Z;
}
/// <summary>
/// Compares whether current instance is equal to specified <see cref="Vector3"/>.
/// </summary>
/// <param name="other">The <see cref="Vector3"/> to compare.</param>
/// <returns><c>true</c> if the instances are equal; <c>false</c> otherwise.</returns>
public bool Equals(Vector3 other)
{
return X == other.X &&
Y == other.Y &&
Z == other.Z;
}
/// <summary>
/// Round the members of this <see cref="Vector3"/> towards negative infinity.
/// </summary>
public void Floor()
{
X = MathF.Floor(X);
Y = MathF.Floor(Y);
Z = MathF.Floor(Z);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains members from another vector rounded towards negative infinity.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <returns>The rounded <see cref="Vector3"/>.</returns>
public static Vector3 Floor(Vector3 value)
{
value.X = MathF.Floor(value.X);
value.Y = MathF.Floor(value.Y);
value.Z = MathF.Floor(value.Z);
return value;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains members from another vector rounded towards negative infinity.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <param name="result">The rounded <see cref="Vector3"/>.</param>
public static void Floor(ref Vector3 value, out Vector3 result)
{
result.X = MathF.Floor(value.X);
result.Y = MathF.Floor(value.Y);
result.Z = MathF.Floor(value.Z);
}
/// <summary>
/// Gets the hash code of this <see cref="Vector3"/>.
/// </summary>
/// <returns>Hash code of this <see cref="Vector3"/>.</returns>
public override int GetHashCode() {
unchecked
{
var hashCode = X.GetHashCode();
hashCode = (hashCode * 397) ^ Y.GetHashCode();
hashCode = (hashCode * 397) ^ Z.GetHashCode();
return hashCode;
}
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains hermite spline interpolation.
/// </summary>
/// <param name="value1">The first position vector.</param>
/// <param name="tangent1">The first tangent vector.</param>
/// <param name="value2">The second position vector.</param>
/// <param name="tangent2">The second tangent vector.</param>
/// <param name="amount">Weighting factor.</param>
/// <returns>The hermite spline interpolation vector.</returns>
public static Vector3 Hermite(Vector3 value1, Vector3 tangent1, Vector3 value2, Vector3 tangent2, float amount)
{
return new Vector3(MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount),
MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount),
MathHelper.Hermite(value1.Z, tangent1.Z, value2.Z, tangent2.Z, amount));
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains hermite spline interpolation.
/// </summary>
/// <param name="value1">The first position vector.</param>
/// <param name="tangent1">The first tangent vector.</param>
/// <param name="value2">The second position vector.</param>
/// <param name="tangent2">The second tangent vector.</param>
/// <param name="amount">Weighting factor.</param>
/// <param name="result">The hermite spline interpolation vector as an output parameter.</param>
public static void Hermite(ref Vector3 value1, ref Vector3 tangent1, ref Vector3 value2, ref Vector3 tangent2, float amount, out Vector3 result)
{
result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount);
result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount);
result.Z = MathHelper.Hermite(value1.Z, tangent1.Z, value2.Z, tangent2.Z, amount);
}
/// <summary>
/// Returns the length of this <see cref="Vector3"/>.
/// </summary>
/// <returns>The length of this <see cref="Vector3"/>.</returns>
public float Length()
{
return MathF.Sqrt((X * X) + (Y * Y) + (Z * Z));
}
/// <summary>
/// Returns the squared length of this <see cref="Vector3"/>.
/// </summary>
/// <returns>The squared length of this <see cref="Vector3"/>.</returns>
public float LengthSquared()
{
return (X * X) + (Y * Y) + (Z * Z);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains linear interpolation of the specified vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
/// <returns>The result of linear interpolation of the specified vectors.</returns>
public static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount)
{
return new Vector3(
MathHelper.Lerp(value1.X, value2.X, amount),
MathHelper.Lerp(value1.Y, value2.Y, amount),
MathHelper.Lerp(value1.Z, value2.Z, amount));
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains linear interpolation of the specified vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
/// <param name="result">The result of linear interpolation of the specified vectors as an output parameter.</param>
public static void Lerp(ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result)
{
result.X = MathHelper.Lerp(value1.X, value2.X, amount);
result.Y = MathHelper.Lerp(value1.Y, value2.Y, amount);
result.Z = MathHelper.Lerp(value1.Z, value2.Z, amount);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains linear interpolation of the specified vectors.
/// Uses <see cref="MathHelper.LerpPrecise"/> on MathHelper for the interpolation.
/// Less efficient but more precise compared to <see cref="Vector3.Lerp(Vector3, Vector3, float)"/>.
/// See remarks section of <see cref="MathHelper.LerpPrecise"/> on MathHelper for more info.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
/// <returns>The result of linear interpolation of the specified vectors.</returns>
public static Vector3 LerpPrecise(Vector3 value1, Vector3 value2, float amount)
{
return new Vector3(
MathHelper.LerpPrecise(value1.X, value2.X, amount),
MathHelper.LerpPrecise(value1.Y, value2.Y, amount),
MathHelper.LerpPrecise(value1.Z, value2.Z, amount));
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains linear interpolation of the specified vectors.
/// Uses <see cref="MathHelper.LerpPrecise"/> on MathHelper for the interpolation.
/// Less efficient but more precise compared to <see cref="Vector3.Lerp(ref Vector3, ref Vector3, float, out Vector3)"/>.
/// See remarks section of <see cref="MathHelper.LerpPrecise"/> on MathHelper for more info.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="amount">Weighting value(between 0.0 and 1.0).</param>
/// <param name="result">The result of linear interpolation of the specified vectors as an output parameter.</param>
public static void LerpPrecise(ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result)
{
result.X = MathHelper.LerpPrecise(value1.X, value2.X, amount);
result.Y = MathHelper.LerpPrecise(value1.Y, value2.Y, amount);
result.Z = MathHelper.LerpPrecise(value1.Z, value2.Z, amount);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a maximal values from the two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The <see cref="Vector3"/> with maximal values from the two vectors.</returns>
public static Vector3 Max(Vector3 value1, Vector3 value2)
{
return new Vector3(
MathHelper.Max(value1.X, value2.X),
MathHelper.Max(value1.Y, value2.Y),
MathHelper.Max(value1.Z, value2.Z));
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a maximal values from the two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">The <see cref="Vector3"/> with maximal values from the two vectors as an output parameter.</param>
public static void Max(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
{
result.X = MathHelper.Max(value1.X, value2.X);
result.Y = MathHelper.Max(value1.Y, value2.Y);
result.Z = MathHelper.Max(value1.Z, value2.Z);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a minimal values from the two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <returns>The <see cref="Vector3"/> with minimal values from the two vectors.</returns>
public static Vector3 Min(Vector3 value1, Vector3 value2)
{
return new Vector3(
MathHelper.Min(value1.X, value2.X),
MathHelper.Min(value1.Y, value2.Y),
MathHelper.Min(value1.Z, value2.Z));
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a minimal values from the two vectors.
/// </summary>
/// <param name="value1">The first vector.</param>
/// <param name="value2">The second vector.</param>
/// <param name="result">The <see cref="Vector3"/> with minimal values from the two vectors as an output parameter.</param>
public static void Min(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
{
result.X = MathHelper.Min(value1.X, value2.X);
result.Y = MathHelper.Min(value1.Y, value2.Y);
result.Z = MathHelper.Min(value1.Z, value2.Z);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a multiplication of two vectors.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="value2">Source <see cref="Vector3"/>.</param>
/// <returns>The result of the vector multiplication.</returns>
public static Vector3 Multiply(Vector3 value1, Vector3 value2)
{
value1.X *= value2.X;
value1.Y *= value2.Y;
value1.Z *= value2.Z;
return value1;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a multiplication of <see cref="Vector3"/> and a scalar.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="scaleFactor">Scalar value.</param>
/// <returns>The result of the vector multiplication with a scalar.</returns>
public static Vector3 Multiply(Vector3 value1, float scaleFactor)
{
value1.X *= scaleFactor;
value1.Y *= scaleFactor;
value1.Z *= scaleFactor;
return value1;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a multiplication of <see cref="Vector3"/> and a scalar.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="scaleFactor">Scalar value.</param>
/// <param name="result">The result of the multiplication with a scalar as an output parameter.</param>
public static void Multiply(ref Vector3 value1, float scaleFactor, out Vector3 result)
{
result.X = value1.X * scaleFactor;
result.Y = value1.Y * scaleFactor;
result.Z = value1.Z * scaleFactor;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a multiplication of two vectors.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="value2">Source <see cref="Vector3"/>.</param>
/// <param name="result">The result of the vector multiplication as an output parameter.</param>
public static void Multiply(ref Vector3 value1, ref Vector3 value2, out Vector3 result)
{
result.X = value1.X * value2.X;
result.Y = value1.Y * value2.Y;
result.Z = value1.Z * value2.Z;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains the specified vector inversion.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <returns>The result of the vector inversion.</returns>
public static Vector3 Negate(Vector3 value)
{
value = new Vector3(-value.X, -value.Y, -value.Z);
return value;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains the specified vector inversion.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <param name="result">The result of the vector inversion as an output parameter.</param>
public static void Negate(ref Vector3 value, out Vector3 result)
{
result.X = -value.X;
result.Y = -value.Y;
result.Z = -value.Z;
}
/// <summary>
/// Turns this <see cref="Vector3"/> to a unit vector with the same direction.
/// </summary>
public void Normalize()
{
float factor = MathF.Sqrt((X * X) + (Y * Y) + (Z * Z));
factor = 1f / factor;
X *= factor;
Y *= factor;
Z *= factor;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a normalized values from another vector.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <returns>Unit vector.</returns>
public static Vector3 Normalize(Vector3 value)
{
float factor = MathF.Sqrt((value.X * value.X) + (value.Y * value.Y) + (value.Z * value.Z));
factor = 1f / factor;
return new Vector3(value.X * factor, value.Y * factor, value.Z * factor);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains a normalized values from another vector.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <param name="result">Unit vector as an output parameter.</param>
public static void Normalize(ref Vector3 value, out Vector3 result)
{
float factor = MathF.Sqrt((value.X * value.X) + (value.Y * value.Y) + (value.Z * value.Z));
factor = 1f / factor;
result.X = value.X * factor;
result.Y = value.Y * factor;
result.Z = value.Z * factor;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains reflect vector of the given vector and normal.
/// </summary>
/// <param name="vector">Source <see cref="Vector3"/>.</param>
/// <param name="normal">Reflection normal.</param>
/// <returns>Reflected vector.</returns>
public static Vector3 Reflect(Vector3 vector, Vector3 normal)
{
// I is the original array
// N is the normal of the incident plane
// R = I - (2 * N * ( DotProduct[ I,N] ))
Vector3 reflectedVector;
// inline the dotProduct here instead of calling method
float dotProduct = ((vector.X * normal.X) + (vector.Y * normal.Y)) + (vector.Z * normal.Z);
reflectedVector.X = vector.X - (2.0f * normal.X) * dotProduct;
reflectedVector.Y = vector.Y - (2.0f * normal.Y) * dotProduct;
reflectedVector.Z = vector.Z - (2.0f * normal.Z) * dotProduct;
return reflectedVector;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains reflect vector of the given vector and normal.
/// </summary>
/// <param name="vector">Source <see cref="Vector3"/>.</param>
/// <param name="normal">Reflection normal.</param>
/// <param name="result">Reflected vector as an output parameter.</param>
public static void Reflect(ref Vector3 vector, ref Vector3 normal, out Vector3 result)
{
// I is the original array
// N is the normal of the incident plane
// R = I - (2 * N * ( DotProduct[ I,N] ))
// inline the dotProduct here instead of calling method
float dotProduct = ((vector.X * normal.X) + (vector.Y * normal.Y)) + (vector.Z * normal.Z);
result.X = vector.X - (2.0f * normal.X) * dotProduct;
result.Y = vector.Y - (2.0f * normal.Y) * dotProduct;
result.Z = vector.Z - (2.0f * normal.Z) * dotProduct;
}
/// <summary>
/// Round the members of this <see cref="Vector3"/> towards the nearest integer value.
/// </summary>
public void Round()
{
X = MathF.Round(X);
Y = MathF.Round(Y);
Z = MathF.Round(Z);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains members from another vector rounded to the nearest integer value.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <returns>The rounded <see cref="Vector3"/>.</returns>
public static Vector3 Round(Vector3 value)
{
value.X = MathF.Round(value.X);
value.Y = MathF.Round(value.Y);
value.Z = MathF.Round(value.Z);
return value;
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains members from another vector rounded to the nearest integer value.
/// </summary>
/// <param name="value">Source <see cref="Vector3"/>.</param>
/// <param name="result">The rounded <see cref="Vector3"/>.</param>
public static void Round(ref Vector3 value, out Vector3 result)
{
result.X = MathF.Round(value.X);
result.Y = MathF.Round(value.Y);
result.Z = MathF.Round(value.Z);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains cubic interpolation of the specified vectors.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="value2">Source <see cref="Vector3"/>.</param>
/// <param name="amount">Weighting value.</param>
/// <returns>Cubic interpolation of the specified vectors.</returns>
public static Vector3 SmoothStep(Vector3 value1, Vector3 value2, float amount)
{
return new Vector3(
MathHelper.SmoothStep(value1.X, value2.X, amount),
MathHelper.SmoothStep(value1.Y, value2.Y, amount),
MathHelper.SmoothStep(value1.Z, value2.Z, amount));
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains cubic interpolation of the specified vectors.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="value2">Source <see cref="Vector3"/>.</param>
/// <param name="amount">Weighting value.</param>
/// <param name="result">Cubic interpolation of the specified vectors as an output parameter.</param>
public static void SmoothStep(ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result)
{
result.X = MathHelper.SmoothStep(value1.X, value2.X, amount);
result.Y = MathHelper.SmoothStep(value1.Y, value2.Y, amount);
result.Z = MathHelper.SmoothStep(value1.Z, value2.Z, amount);
}
/// <summary>
/// Creates a new <see cref="Vector3"/> that contains subtraction of on <see cref="Vector3"/> from a another.
/// </summary>
/// <param name="value1">Source <see cref="Vector3"/>.</param>
/// <param name="value2">Source <see cref="Vector3"/>.</param>
/// <returns>The result of the vector subtraction.</returns>
public static Vector3 Subtract(Vector3 value1, Vector3 value2)
{
value1.X -= value2.X;
value1.Y -= value2.Y;
value1.Z -= value2.Z;