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
use core::ops::Neg;
use crate::Math;
use crate::Vector2;
use crate::{AddSubArithmetic, MulDivScalar, use_impl_ops, impl_add, impl_sub, impl_mul, impl_div};
/// A 3D vector that holds an x-coordinate, y-coordinate, and z-coordinate
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy)]
pub struct Vector3 {
/// The x coordinate of the vector
x: f32,
/// The y coordinate of the vector
y: f32,
/// The z coordinate of the vector
z: f32,
}
/// Constructors
impl Vector3 {
/// Creates a new 3D vector
/// - **x**: The x coordinate of the vector
/// - **y**: The y coordinate of the vector
/// - **z**: The z coordinate of the vector
///
/// **Returns**: Returns a new 3D vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let vector = Vector3::new(1.2, 3.45, 6.789);
/// assert_eq!(1.2, vector.x());
/// assert_eq!(3.45, vector.y());
/// assert_eq!(6.789, vector.z());
/// ```
pub fn new(x: f32, y: f32, z: f32) -> Self { Vector3 { x, y, z } }
/// Creates a new 3D vector from a 2D vector
/// - **vector**: The 2D vector to convert from
///
/// **Returns**: Returns a converted 3D vector
/// #### Examples
/// ```
/// # use mathx::{Vector2,Vector3};
/// let vector2 = Vector2::new(1.2, 3.45);
/// let vector3 = Vector3::from_vector2(vector2);
/// assert_eq!(1.2, vector3.x());
/// assert_eq!(3.45, vector3.y());
/// assert_eq!(0.0, vector3.z());
/// ```
pub fn from_vector2(vector: Vector2) -> Self { Vector3::new(vector.x(), vector.y(), 0.0) }
/// Creates an empty 3D vector
///
/// **Returns**: Returns an empty 3D vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let vector = Vector3::zero();
/// assert_eq!(0.0, vector.x());
/// assert_eq!(0.0, vector.y());
/// assert_eq!(0.0, vector.z());
/// ```
pub fn zero() -> Self { Vector3 { x: 0.0, y: 0.0, z: 0.0 } }
/// Creates a 3D unit vector that's pointing to the lefT: (-1, 0, 0)
///
/// **Returns**: Returns a 3D unit vector that's pointing to the left
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let vector = Vector3::left();
/// assert_eq!(-1.0, vector.x());
/// assert_eq!(0.0, vector.y());
/// assert_eq!(0.0, vector.z());
/// ```
pub fn left() -> Self { Vector3 { x: -1.0, y: 0.0, z: 0.0 } }
/// Creates a 3D unit vector that's pointing to the right: (1, 0, 0)
///
/// **Returns**: Returns a 3D unit vector that's pointing to the left
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let vector = Vector3::right();
/// assert_eq!(1.0, vector.x());
/// assert_eq!(0.0, vector.y());
/// assert_eq!(0.0, vector.z());
/// ```
pub fn right() -> Self { Vector3 { x: 1.0, y: 0.0, z: 0.0 } }
/// Creates a 3D unit vector that's pointing up: (0, 1, 0)
///
/// **Returns**: Returns a 3D unit vector that's pointing up
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let vector = Vector3::up();
/// assert_eq!(0.0, vector.x());
/// assert_eq!(1.0, vector.y());
/// assert_eq!(0.0, vector.z());
/// ```
pub fn up() -> Self { Vector3 { x: 0.0, y: 1.0, z: 0.0 } }
/// Creates a 3D unit vector that's pointing down: (0, -1, 0)
///
/// **Returns**: Returns a 3D unit vector that's pointing down
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let vector = Vector3::down();
/// assert_eq!(0.0, vector.x());
/// assert_eq!(-1.0, vector.y());
/// assert_eq!(0.0, vector.z());
/// ```
pub fn down() -> Self { Vector3 { x: 0.0, y: -1.0, z: 0.0 } }
/// Creates a 3D unit vector that's pointing forward: (0, 0, 1)
///
/// **Returns**: Returns a 3D unit vector that's pointing forward
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let vector = Vector3::forward();
/// assert_eq!(0.0, vector.x());
/// assert_eq!(0.0, vector.y());
/// assert_eq!(1.0, vector.z());
/// ```
pub fn forward() -> Self { Vector3 { x: 0.0, y: 0.0, z: 1.0 } }
/// Creates a 3D unit vector that's pointing backwards: (0, 0, -1)
///
/// **Returns**: Returns a 3D unit vector that's pointing backwards
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let vector = Vector3::back();
/// assert_eq!(0.0, vector.x());
/// assert_eq!(0.0, vector.y());
/// assert_eq!(-1.0, vector.z());
/// ```
pub fn back() -> Self { Vector3 { x: 0.0, y: 0.0, z: -1.0 } }
/// Creates a 3D vector that contains 1 in all it's components: (1, 1, 1)
///
/// **Returns**: Returns a 3D vector that contains 1 in all it's components
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let vector = Vector3::one();
/// assert_eq!(1.0, vector.x());
/// assert_eq!(1.0, vector.y());
/// assert_eq!(1.0, vector.z());
/// ```
pub fn one() -> Self { Vector3 { x: 1.0, y: 1.0, z: 1.0 } }
/// Creates a 3D vector from two given angles
/// - **theta**: The first angle to create the vector from
/// - **phi**: The second angle to create the vector from
///
/// **Returns**: Returns a 3D vector from the two angles
/// #### Examples
/// ```
/// # use mathx::{Math,Vector3,assert_range};
/// let vector = Vector3::from_angles(Math::PI_OVER_4, Math::PI_OVER_4);
/// let expected = Vector3::new(0.5, 0.5, 0.707106781187);
/// assert_range!(expected.x(), vector.x());
/// assert_range!(expected.y(), vector.y());
/// assert_range!(expected.z(), vector.z());
/// let vector = Vector3::from_angles(-2.21656815003, 2.21656815003);
/// let expected = Vector3::new(0.3621814, 0.4806309, 0.7986355);
/// assert_range!(expected.x(), vector.x());
/// assert_range!(expected.y(), vector.y());
/// assert_range!(expected.z(), vector.z());
/// ```
pub fn from_angles(theta: f32, phi: f32) -> Self {
let (sin_theta, cos_theta) = Math::sin_cos(theta);
let (sin_phi, cos_phi) = Math::sin_cos(phi);
Vector3::new(
cos_phi * cos_theta,
cos_phi * sin_theta,
sin_phi
)
}
/// Creates a 3D vector from two given angles
/// - **theta**: The first angle to create the vector from
/// - **phi**: The second angle to create the vector from
///
/// **Returns**: Returns a 3D vector from the two angles
/// #### Examples
/// ```
/// # use mathx::{Math,Vector3,assert_range};
/// let vector = Vector3::from_angles_deg(45.0, 45.0);
/// let expected = Vector3::new(0.5, 0.5, 0.707106781187);
/// assert_range!(expected.x(), vector.x());
/// assert_range!(expected.y(), vector.y());
/// assert_range!(expected.z(), vector.z());
/// let vector = Vector3::from_angles_deg(-127.0, 127.0);
/// let expected = Vector3::new(0.3621814, 0.4806309, 0.7986355);
/// assert_range!(expected.x(), vector.x());
/// assert_range!(expected.y(), vector.y());
/// assert_range!(expected.z(), vector.z());
/// ```
pub fn from_angles_deg(theta: f32, phi: f32) -> Self {
Vector3::from_angles(Math::deg2rad(theta), Math::deg2rad(phi))
}
}
/// Properties
impl Vector3 {
/// Gets the x coordinate of the vector
///
/// **Returns**: Returns the x coordinate of the vector
pub fn x(&self) -> f32 { self.x }
/// Sets the x coordinate of the vector
/// - **value**: The value to set the x coordinate of the vector
pub fn set_x(&mut self, value: f32) { self.x = value; }
/// Gets the y coordinate of the vector
///
/// **Returns**: Returns the y coordinate of the vector
pub fn y(&self) -> f32 { self.y }
/// Sets the y coordinate of the vector
/// - **value**: The value to set the y coordinate of the vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let mut a = Vector3::up();
/// a.set_y(6.0);
/// assert_eq!(6.0, a.y());
/// ```
pub fn set_y(&mut self, value: f32) { self.y = value; }
/// Gets the z coordinate of the vector
///
/// **Returns**: Returns the z coordinate of the vector
pub fn z(&self) -> f32 { self.z }
/// Sets the z coordinate of the vector
/// - **value**: The value to set the z coordinate of the vector
pub fn set_z(&mut self, value: f32) { self.z = value; }
/// Gets the magnitude of the vector. This returns the length of the vector
///
/// **Returns**: Returns the magnitude of the vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(-1.0, 2.0, -2.0);
/// assert_eq!(3.0, a.magnitude());
/// ```
pub fn magnitude(&self) -> f32 {
let magnitude = self.square_magnitude();
if magnitude == 0.0 || magnitude == 1.0 {
return magnitude;
}
return Math::sqrt(magnitude);
}
/// Gets the magnitude squared, avoiding the use of a square root
///
/// **Returns**: Returns the magnitude of the vector squared
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(-1.0, 2.0, 2.0);
/// assert_eq!(9.0, a.square_magnitude());
/// ```
pub fn square_magnitude(&self) -> f32 { self.x * self.x + self.y * self.y + self.z * self.z }
}
/// Public Methods
impl Vector3 {
/// Gets the angle between the two vectors in radians
/// - **rhs**: The other vector to get the angle from
///
/// **Returns**: Returns the angle between the two vectors in radians
/// #### Examples
/// ```
/// # use mathx::{Vector3,Math,assert_range};
/// let a = Vector3::new(0.25, -0.5, 1.25);
/// let b = Vector3::new(2.0, 0.5, -1.0);
/// assert_range!(1.89518322157, a.angle_between(b));
/// ```
pub fn angle_between(self, rhs: Vector3) -> f32 {
let value = Math::sqrt(self.square_magnitude() * rhs.square_magnitude());
if value < 0.0000000001 { return 0.0; }
else { return Math::acos(Math::clamp((self * rhs) / value, -1.0, 1.0)); }
}
/// Gets the angle between the two vectors in degrees
/// - **rhs**: The other vector to get the angle from
///
/// **Returns**: Returns the angle between the two vectors in degrees
/// #### Examples
/// ```
/// # use mathx::{Vector3,Math,assert_range};
/// let a = Vector3::new(0.25, -0.5, 1.25);
/// let b = Vector3::new(2.0, 0.5, -1.0);
/// assert_range!(108.586, a.angle_between_deg(b), 0.01);
/// ```
pub fn angle_between_deg(self, rhs: Vector3) -> f32 { return Math::rad2deg(self.angle_between(rhs)); }
/// Performs a cross product and creates a 3D vector that is orthogonal to both vectors provided
/// - **rhs**: The other vector to cross product
///
///
/// **Returns**: Returns the vector that is orthogonal to both vectors
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(1.0, 2.0, 3.0);
/// let b = Vector3::new(4.0, 5.0, 6.0);
/// let expected = Vector3::new(-3.0, 6.0, -3.0);
/// assert_eq!(expected, a.cross(b));
/// assert_eq!(Vector3::zero(), a.cross(a));
/// ```
pub fn cross(self, rhs: Vector3) -> Self {
Vector3::new(
self.y * rhs.z - self.z * rhs.y,
self.z * rhs.x - self.x * rhs.z,
self.x * rhs.y - self.y * rhs.x
)
}
/// Gets the distance between the two vectors
/// - **rhs**: The other vector to get the distance between
///
/// **Returns**: Returns the distance between the two vectors
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(0.25, -0.5, 1.25);
/// let b = Vector3::new(2.0, 0.5, -1.0);
/// assert_eq!(3.0207615, a.distance(b));
/// ```
pub fn distance(self, rhs: Vector3) -> f32 { (rhs - self).magnitude() }
/// Gets the dot product of between the two vectors.
/// It can be used to determine the angle between two vectors.
/// - **rhs**: The other vector to dot product with
///
/// **Returns**: Returns the dot product
/// #### Remarks
/// Using two unit vectors, the maximum range of numbers go from -1 to 1. It scales with
/// the magnitude of both vectors (multiplying them together `a.magnitude() * b.magnitude()`)
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::one();
/// let b = Vector3::new(0.25, 1.1, -4.1);
/// let dot = a.dot(b);
/// assert_eq!(-2.75, dot);
/// ```
/// Note that if the angle is 90 degrees (PI / 2) then it's going to return 0
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::right();
/// let b = 2.0 * Vector3::up();
/// let dot = a.dot(b);
/// assert_eq!(0.0, dot);
/// ```
/// Where as, if the angle is 0 degrees or 180 degrees (PI) then it's going to return 1 and -1 respectively;
/// given that the two vectors are unit vectors
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::right();
/// let b = Vector3::left();
/// let dot_one = a.dot(a);
/// let dot_negative_one = a.dot(b);
/// assert_eq!(1.0, dot_one);
/// assert_eq!(-1.0, dot_negative_one);
/// ```
pub fn dot(self, rhs: Vector3) -> f32 {
self.x * rhs.x + self.y * rhs.y + self.z * rhs.z
}
/// Linearly interpolates between the this and the other vector
/// - **rhs**: The other vector to end from
/// - **t**: The ratio value to interpolate between both vectors. Clamped between 0.0 and 1.0
///
/// **Returns**: Returns the interpolated vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(0.0, 4.0, -10.0);
/// let b = Vector3::new(1.0, 10.0, -4.0);
/// let expected = Vector3::new(0.7, 8.2, -5.8);
/// assert_eq!(expected, a.lerp(b, 0.7));
/// ```
pub fn lerp(self, rhs: Vector3, t: f32) -> Self { self.lerp_unclamped(rhs, t.clamp(0.0, 1.0)) }
/// Linearly interpolates between the this and the other vector (not clamped)
/// - **rhs**: The other vector to end from
/// - **t**: The ratio value to interpolate between both vectors
///
/// **Returns**: Returns the interpolated vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(0.0, 4.0, -10.0);
/// let b = Vector3::new(1.0, 10.0, -4.0);
/// let expected = Vector3::new(0.7, 8.2, -5.8);
/// assert_eq!(expected, a.lerp_unclamped(b, 0.7));
/// ```
pub fn lerp_unclamped(self, rhs: Vector3, t: f32) -> Self {
Vector3::new(
Math::lerp_unclamped(self.x, rhs.x, t),
Math::lerp_unclamped(self.y, rhs.y, t),
Math::lerp_unclamped(self.z, rhs.z, t)
)
}
/// Moves this vector towards the target vector, it will never move past the target
/// - **target**: The target vector to move towards
/// - **delta**: The delta distance to try and move with, defines the maximum distance moved
///
/// **Returns**: Returns the vector that is closer towards the target
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(0.25, -0.5, 1.25);
/// let b = Vector3::new(2.0, 0.5, -1.0);
/// let expected = Vector3::new(0.3658648, -0.4337915, 1.101031);
/// assert_eq!(expected, a.move_towards(b, 0.2));
/// assert_eq!(b, a.move_towards(b, 20.0));
/// ```
pub fn move_towards(self, target: Vector3, delta: f32) -> Self {
let dir = target - self;
let sq_magnitude = dir.square_magnitude();
if sq_magnitude == 0.0 || (delta >= 0.0 && sq_magnitude <= delta * delta) {
return target;
}
let diff = delta / Math::sqrt(sq_magnitude);
return diff * dir + self;
}
/// Normalizes the vector
///
/// **Returns**: Returns the unit vector version of this vector
/// #### Examples
/// ```
/// # use mathx::{Vector3,Math,assert_range};
/// let vector = Vector3::one().normalize();
/// assert_range!(0.5773503, vector.x());
/// assert_range!(0.5773503, vector.y());
/// assert_range!(0.5773503, vector.z());
/// let vector = Vector3::new(-0.1, 1.0, -2.4).normalize();
/// assert_range!(-0.03843312, vector.x());
/// assert_range!(0.3843312, vector.y());
/// assert_range!(-0.9223949, vector.z());
/// ```
pub fn normalize(self) -> Self { self / self.magnitude() }
/// Projects this vector onto the given vector
/// - **rhs**: The vector to project onto
///
/// **Returns**: Returns the projected vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(1.0, 2.0, 3.0);
/// let b = Vector3::new(4.0, 5.0, 6.0);
/// let expected = Vector3::new(1.662337662337662, 2.077922077922078, 2.493506493506494);
/// assert_eq!(expected, a.project(b));
/// ```
pub fn project(self, rhs: Vector3) -> Self {
let top = self * rhs;
let bottom = rhs.square_magnitude();
return (top / bottom) * rhs;
}
/// Rejects this vector from the given vector
/// - **rhs**: The vector to reject from
///
/// **Returns**: Returns the rejected vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(1.0, 2.0, 3.0);
/// let b = Vector3::new(4.0, 5.0, 6.0);
/// let expected = Vector3::new(-0.66233766, -0.077922106, 0.50649357);
/// assert_eq!(expected, a.reject(b));
/// ```
pub fn reject(self, rhs: Vector3) -> Self {
self - self.project(rhs)
}
/// Reflects this vector using a normal vector
/// - **normal**: The normal vector to reflect off of
///
/// **Returns**: Returns the reflected vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let direction = Vector3::new(1.0, 0.0, 1.0);
/// let normal = Vector3::new(0.0, 0.0, -1.0);
/// let expected = Vector3::new(1.0, 0.0, -1.0);
/// assert_eq!(expected, direction.reflect(normal));
/// let direction = Vector3::new(0.25, -0.5, 1.25);
/// let normal = Vector3::new(1.0, 0.5, -1.0);
/// let expected = Vector3::new(2.75, 0.75, -1.25);
/// assert_eq!(expected, direction.reflect(normal));
/// ```
pub fn reflect(self, normal: Vector3) -> Self {
let dot = -2.0 * (self * normal);
return dot * normal + self;
}
/// Rotates the vector around towards the target vector
/// - **target**: The target vector to rotate towards
/// - **radians_delta**: The maximum angle delta the vector will rotate in radians
/// - **magnitude_delta**: The maximum magnitude the vector will rotate with
///
/// **Returns**: Returns the rotated vector
/// #### Remarks
/// This method uses quaternions to rotate the vector, and does not appear if using the `no_quaternions` feature
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(1.0, 3.0, 4.0);
/// let b = Vector3::new(4.0, 6.0, 7.0);
/// let expected = Vector3::new(1.504205, 3.097963, 3.894842);
/// let actual = Vector3::rotate_towards(a, b, 0.1, 0.1);
/// assert_eq!(expected, actual);
/// ```
#[cfg(not(feature = "no_quaternions"))]
pub fn rotate_towards(self, target: Vector3, radians_delta: f32, magnitude_delta: f32) -> Self {
use crate::Quaternion;
let axis = self.cross(target);
let abs_radians = Math::abs(radians_delta);
let angle = Math::clamp(self.signed_angle_between(target, axis), -abs_radians, abs_radians);
if angle == 0.0 { return target; }
let rotation = Quaternion::from_axis_angle(axis, angle);
let rotated = rotation * self;
let magnitude = self.magnitude();
let target_magnitude = target.magnitude();
let towards_magnitude = if magnitude < target_magnitude {
Math::min(self.magnitude() + magnitude_delta, target_magnitude)
}
else if magnitude > target_magnitude {
Math::max(self.magnitude() - magnitude_delta, target_magnitude)
}
else {
return rotated;
};
return rotated.normalize() * towards_magnitude;
}
/// Scales the vector using another vector, multiplying everything component-wise
/// - **rhs**: The other vector to scale with
///
/// **Returns**: Returns the scaled vector
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let a = Vector3::new(0.25, -0.5, 1.25);
/// let b = Vector3::new(2.0, 0.5, -1.0);
/// let expected = Vector3::new(0.5, -0.25, -1.25);
/// assert_eq!(expected, a.scale(b));
/// ```
pub fn scale(self, rhs: Vector3) -> Self {
Vector3::new(
self.x * rhs.x,
self.y * rhs.y,
self.z * rhs.z
)
}
/// Gets the signed angle between the two vectors using an axis in radians
/// - **rhs**: The other vector to get the angle from
/// - **axis**: The axis vector to determine what direction the angle is going
///
/// **Returns**: Returns the signed angle between the two vectors using an axis in radians
/// #### Examples
/// ```
/// # use mathx::{Vector3,Math,assert_range};
/// let a = Vector3::new(0.25, -0.5, 1.25);
/// let b = Vector3::new(2.0, 0.5, -1.0);
/// let axis = Vector3::new(1.0, -1.0, 0.0);
/// assert_range!(-1.89518322157, a.signed_angle_between(b, axis));
/// ```
pub fn signed_angle_between(self, rhs: Vector3, axis: Vector3) -> f32 {
let angle = self.angle_between(rhs);
let cross = self.cross(rhs);
let sign = Math::sign(axis * cross);
return sign * angle;
}
/// Gets the signed angle between the two vectors using an axis in degrees
/// - **rhs**: The other vector to get the angle from
/// - **axis**: The axis vector to determine what direction the angle is going
///
/// **Returns**: Returns the signed angle between the two vectors using an axis in degrees
/// #### Examples
/// ```
/// # use mathx::{Vector3,Math,assert_range};
/// let a = Vector3::new(0.25, -0.5, 1.25);
/// let b = Vector3::new(2.0, 0.5, -1.0);
/// let axis = Vector3::new(1.0, -1.0, 0.0);
/// assert_range!(-108.586, a.signed_angle_between_deg(b, axis), 0.01);
/// ```
pub fn signed_angle_between_deg(self, rhs: Vector3, axis: Vector3) -> f32 { Math::rad2deg(self.signed_angle_between(rhs, axis)) }
/// Spherically interpolates between two vectors
/// - **rhs**: The target vector to interpolate towards
/// - **t**: The ratio (t) to interpolate with
///
/// **Returns**: Returns the spherically interpolated vector
/// #### Examples
/// ```
/// # use mathx::{Vector3,Math,assert_range};
/// let a = Vector3::new(1.0, 3.0, 4.0);
/// let b = Vector3::new(4.0, 6.0, 7.0);
/// let actual = Vector3::slerp_unclamped(a, b, 0.7);
/// let expected = Vector3::new(2.903773, 5.117129, 6.223807);
/// assert_range!(expected.x(), actual.x(), 0.0001);
/// assert_range!(expected.y(), actual.y(), 0.0001);
/// assert_range!(expected.z(), actual.z(), 0.0001);
/// ```
pub fn slerp(self, rhs: Vector3, t: f32) -> Self { self.slerp_unclamped(rhs, Math::clamp(t, 0.0, 1.0)) }
/// Spherically interpolates between two vectors (not clamped)
/// - **rhs**: The target vector to interpolate towards
/// - **t**: The ratio (t) to interpolate with (not clamped)
///
/// **Returns**: Returns the spherically interpolated vector
/// #### Examples
/// ```
/// # use mathx::{Vector3,Math,assert_range};
/// let a = Vector3::new(1.0, 3.0, 4.0);
/// let b = Vector3::new(4.0, 6.0, 7.0);
/// let actual = Vector3::slerp_unclamped(a, b, 0.7);
/// let expected = Vector3::new(2.903773, 5.117129, 6.223807);
/// assert_range!(expected.x(), actual.x(), 0.0001);
/// assert_range!(expected.y(), actual.y(), 0.0001);
/// assert_range!(expected.z(), actual.z(), 0.0001);
/// ```
pub fn slerp_unclamped(self, rhs: Vector3, t: f32) -> Self {
let size = Math::lerp_unclamped(self.magnitude(), rhs.magnitude(), t);
let unit_self = self.normalize();
let mut unit_rhs = rhs.normalize();
let mut dot = unit_self.dot(unit_rhs);
if dot < 0.0 {
unit_rhs = -unit_rhs;
dot = -dot;
}
if dot > 0.9995 {
return size * (unit_self + t * (unit_rhs - unit_self)).normalize();
}
let angle = t * Math::acos(dot);
let unit = dot * unit_self;
let unit_rhs = (unit_rhs - unit).normalize();
let (sin, cos) = Math::sin_cos(angle);
return size * cos * unit_self + size * sin * unit_rhs;
}
/// Smooths a vector towards a desired goal over time
/// - **target**: The position to try to reach
/// - **velocity**: The current velocity
/// - **smooth_time**: The time (in seconds) it will take to reach the target
/// - **max_speed**: The maximum speed of the vector
/// - **delta**: The time between frames
///
/// **Returns**: Returns a tuple of a vector that is closer towards the target and the new velocity
/// #### Examples
/// ```
/// # use mathx::Vector3;
/// let current = Vector3::new(1.0, 2.0, 3.0);
/// let target = Vector3::new(14.0, 15.0, 16.0);
/// let velocity = Vector3::new(4.0, 5.0, 6.0);
/// let time = 8.0;
/// let max_speed = 2.3;
/// let delta = 0.2;
/// let (position, velocity) = Vector3::smooth_damp(
/// current,
/// target,
/// velocity,
/// time,
/// max_speed,
/// delta
/// );
/// let expected_position = Vector3::new(1.7734365, 2.9636898, 4.156722);
/// let expected_velocity = Vector3::new(3.7411351, 4.644839, 5.5768046);
/// assert_eq!(expected_position, position);
/// assert_eq!(expected_velocity, velocity);
/// ```
pub fn smooth_damp(self, target: Vector3, velocity: Vector3, smooth_time: f32, max_speed: f32, delta: f32) -> (Self, Self) {
let smooth_time = Math::max(0.0001, smooth_time);
let inv_smooth_time = 2.0 / smooth_time;
let inv_smooth_delta = inv_smooth_time * delta;
let cubic = 1.0 / (
1.0
+ inv_smooth_delta
+ 0.47999998927116394 * inv_smooth_delta * inv_smooth_delta
+ 0.23499999940395355 * inv_smooth_delta * inv_smooth_delta * inv_smooth_delta
);
let mut dir = self - target;
let smooth_speed = max_speed * smooth_time;
let sq_speed = smooth_speed * smooth_speed;
let sq_magnitude = dir.square_magnitude();
if sq_magnitude > sq_speed {
dir *= smooth_speed / Math::sqrt(sq_magnitude);
}
let temp_target = target;
let target = self - dir;
let smooth_velocity = (velocity + inv_smooth_time * dir) * delta;
let mut velocity = (velocity - inv_smooth_time * smooth_velocity) * cubic;
let a = temp_target - self;
let result = target + (dir + smooth_velocity) * cubic;
let b = result - temp_target;
if a * b > 0.0 {
velocity = (result - temp_target) / delta;
}
return (result, velocity);
}
}
/// Conversions
impl Vector3 {
pub fn to_vector2(self) -> Vector2 { Vector2::new(self.x, self.y) }
}
impl From<Vector2> for Vector3 {
fn from(value: Vector2) -> Self { Vector3::from_vector2(value) }
}
unsafe impl Send for Vector3 {}
unsafe impl Sync for Vector3 {}
// Equates
impl Eq for Vector3 {}
impl PartialEq for Vector3 {
fn eq(&self, other: &Self) -> bool {
Math::approx(self.x, other.x)
&& Math::approx(self.y, other.y)
&& Math::approx(self.z, other.z)
}
}
// Display
#[cfg(not(feature = "no_std"))]
impl std::fmt::Display for Vector3 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("({}, {}, {})", self.x, self.y, self.z))
}
}
// Arithmetic
impl AddSubArithmetic<Vector3> for Vector3 {
type Output = Vector3;
fn add_other(self, rhs: Vector3) -> Self::Output {
Vector3 { x: self.x + rhs.x, y: self.y + rhs.y, z: self.z + rhs.z }
}
fn add_assign_other(&mut self, rhs: Vector3) {
self.x += rhs.x;
self.y += rhs.y;
self.z += rhs.z;
}
fn subtract_other(self, rhs: Vector3) -> Self::Output {
Vector3 { x: self.x - rhs.x, y: self.y - rhs.y, z: self.z - rhs.z }
}
fn subtract_assign_other(&mut self, rhs: Vector3) {
self.x -= rhs.x;
self.y -= rhs.y;
self.z -= rhs.z;
}
}
impl AddSubArithmetic<Vector2> for Vector3 {
type Output = Vector3;
fn add_other(self, rhs: Vector2) -> Self::Output {
Vector3 { x: self.x + rhs.x(), y: self.y + rhs.y(), z: self.z }
}
fn add_assign_other(&mut self, rhs: Vector2) {
self.x += rhs.x();
self.y += rhs.y();
}
fn subtract_other(self, rhs: Vector2) -> Self::Output {
Vector3 { x: self.x - rhs.x(), y: self.y - rhs.y(), z: self.z }
}
fn subtract_assign_other(&mut self, rhs: Vector2) {
self.x -= rhs.x();
self.y -= rhs.y();
}
}
impl MulDivScalar for Vector3 {
type Output = Vector3;
fn multiply_scalar(self, rhs: f32) -> Self::Output {
Vector3 { x: rhs * self.x, y: rhs * self.y, z: rhs * self.z }
}
fn multiply_assign_scalar(&mut self, rhs: f32) {
self.x *= rhs;
self.y *= rhs;
}
fn divide_scalar(self, rhs: f32) -> Self::Output {
if rhs == 0.0 { return Vector3::zero(); }
Vector3 { x: self.x / rhs, y: self.y / rhs, z: self.z / rhs }
}
fn divide_assign_scalar(&mut self, rhs: f32) {
if rhs == 0.0 {
self.x = 0.0;
self.y = 0.0;
self.z = 0.0;
}
else {
self.x /= rhs;
self.y /= rhs;
self.z /= rhs;
}
}
fn reciprocal_scalar(self, rhs: f32) -> Self::Output {
Vector3 {
x: if self.x != 0.0 { rhs / self.x } else { 0.0 },
y: if self.y != 0.0 { rhs / self.y } else { 0.0 },
z: if self.z != 0.0 { rhs / self.z } else { 0.0 },
}
}
}
impl Neg for Vector3 {
type Output = Vector3;
fn neg(self) -> Self::Output { Vector3::new(-self.x, -self.y, -self.z) }
}
use_impl_ops!();
impl_add!(Vector3);
impl_add!(Vector3 => Vector2: Vector3);
impl_sub!(Vector3);
impl_sub!(Vector3 => Vector2: Vector3);
impl_mul!(Vector3, Vector3 => f32: dot);
impl_mul!(Vector3);
impl_div!(Vector3);