Skip to content

Commit 30948c4

Browse files
authored
Add move_towards for vectors (#492)
1 parent 231a867 commit 30948c4

File tree

17 files changed

+234
-0
lines changed

17 files changed

+234
-0
lines changed

codegen/templates/vec.rs.tera

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,21 @@ impl {{ self_t }} {
16091609
self + ((rhs - self) * s)
16101610
}
16111611

1612+
/// Moves towards `rhs` based on the value `d`.
1613+
///
1614+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
1615+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
1616+
#[inline]
1617+
#[must_use]
1618+
pub fn move_towards(&self, rhs: Self, d: {{ scalar_t }}) -> Self {
1619+
let a = rhs - *self;
1620+
let len = a.length();
1621+
if len <= d || len <= 1e-4 {
1622+
return rhs;
1623+
}
1624+
*self + a / len * d
1625+
}
1626+
16121627
/// Calculates the midpoint between `self` and `rhs`.
16131628
///
16141629
/// The midpoint is the average of, or halfway point between, two vectors.

src/f32/coresimd/vec3a.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,21 @@ impl Vec3A {
673673
self + ((rhs - self) * s)
674674
}
675675

676+
/// Moves towards `rhs` based on the value `d`.
677+
///
678+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
679+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
680+
#[inline]
681+
#[must_use]
682+
pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
683+
let a = rhs - *self;
684+
let len = a.length();
685+
if len <= d || len <= 1e-4 {
686+
return rhs;
687+
}
688+
*self + a / len * d
689+
}
690+
676691
/// Calculates the midpoint between `self` and `rhs`.
677692
///
678693
/// The midpoint is the average of, or halfway point between, two vectors.

src/f32/coresimd/vec4.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,21 @@ impl Vec4 {
651651
self + ((rhs - self) * s)
652652
}
653653

654+
/// Moves towards `rhs` based on the value `d`.
655+
///
656+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
657+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
658+
#[inline]
659+
#[must_use]
660+
pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
661+
let a = rhs - *self;
662+
let len = a.length();
663+
if len <= d || len <= 1e-4 {
664+
return rhs;
665+
}
666+
*self + a / len * d
667+
}
668+
654669
/// Calculates the midpoint between `self` and `rhs`.
655670
///
656671
/// The midpoint is the average of, or halfway point between, two vectors.

src/f32/scalar/vec3a.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,21 @@ impl Vec3A {
714714
self + ((rhs - self) * s)
715715
}
716716

717+
/// Moves towards `rhs` based on the value `d`.
718+
///
719+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
720+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
721+
#[inline]
722+
#[must_use]
723+
pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
724+
let a = rhs - *self;
725+
let len = a.length();
726+
if len <= d || len <= 1e-4 {
727+
return rhs;
728+
}
729+
*self + a / len * d
730+
}
731+
717732
/// Calculates the midpoint between `self` and `rhs`.
718733
///
719734
/// The midpoint is the average of, or halfway point between, two vectors.

src/f32/scalar/vec4.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,21 @@ impl Vec4 {
760760
self + ((rhs - self) * s)
761761
}
762762

763+
/// Moves towards `rhs` based on the value `d`.
764+
///
765+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
766+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
767+
#[inline]
768+
#[must_use]
769+
pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
770+
let a = rhs - *self;
771+
let len = a.length();
772+
if len <= d || len <= 1e-4 {
773+
return rhs;
774+
}
775+
*self + a / len * d
776+
}
777+
763778
/// Calculates the midpoint between `self` and `rhs`.
764779
///
765780
/// The midpoint is the average of, or halfway point between, two vectors.

src/f32/sse2/vec3a.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,21 @@ impl Vec3A {
721721
self + ((rhs - self) * s)
722722
}
723723

724+
/// Moves towards `rhs` based on the value `d`.
725+
///
726+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
727+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
728+
#[inline]
729+
#[must_use]
730+
pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
731+
let a = rhs - *self;
732+
let len = a.length();
733+
if len <= d || len <= 1e-4 {
734+
return rhs;
735+
}
736+
*self + a / len * d
737+
}
738+
724739
/// Calculates the midpoint between `self` and `rhs`.
725740
///
726741
/// The midpoint is the average of, or halfway point between, two vectors.

src/f32/sse2/vec4.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,21 @@ impl Vec4 {
700700
self + ((rhs - self) * s)
701701
}
702702

703+
/// Moves towards `rhs` based on the value `d`.
704+
///
705+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
706+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
707+
#[inline]
708+
#[must_use]
709+
pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
710+
let a = rhs - *self;
711+
let len = a.length();
712+
if len <= d || len <= 1e-4 {
713+
return rhs;
714+
}
715+
*self + a / len * d
716+
}
717+
703718
/// Calculates the midpoint between `self` and `rhs`.
704719
///
705720
/// The midpoint is the average of, or halfway point between, two vectors.

src/f32/vec2.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,21 @@ impl Vec2 {
646646
self + ((rhs - self) * s)
647647
}
648648

649+
/// Moves towards `rhs` based on the value `d`.
650+
///
651+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
652+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
653+
#[inline]
654+
#[must_use]
655+
pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
656+
let a = rhs - *self;
657+
let len = a.length();
658+
if len <= d || len <= 1e-4 {
659+
return rhs;
660+
}
661+
*self + a / len * d
662+
}
663+
649664
/// Calculates the midpoint between `self` and `rhs`.
650665
///
651666
/// The midpoint is the average of, or halfway point between, two vectors.

src/f32/vec3.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,21 @@ impl Vec3 {
705705
self + ((rhs - self) * s)
706706
}
707707

708+
/// Moves towards `rhs` based on the value `d`.
709+
///
710+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
711+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
712+
#[inline]
713+
#[must_use]
714+
pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
715+
let a = rhs - *self;
716+
let len = a.length();
717+
if len <= d || len <= 1e-4 {
718+
return rhs;
719+
}
720+
*self + a / len * d
721+
}
722+
708723
/// Calculates the midpoint between `self` and `rhs`.
709724
///
710725
/// The midpoint is the average of, or halfway point between, two vectors.

src/f32/wasm32/vec3a.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,21 @@ impl Vec3A {
692692
self + ((rhs - self) * s)
693693
}
694694

695+
/// Moves towards `rhs` based on the value `d`.
696+
///
697+
/// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
698+
/// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
699+
#[inline]
700+
#[must_use]
701+
pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
702+
let a = rhs - *self;
703+
let len = a.length();
704+
if len <= d || len <= 1e-4 {
705+
return rhs;
706+
}
707+
*self + a / len * d
708+
}
709+
695710
/// Calculates the midpoint between `self` and `rhs`.
696711
///
697712
/// The midpoint is the average of, or halfway point between, two vectors.

0 commit comments

Comments
 (0)