From a1fcd1cb733ef3d2b180a863540ffeb7100d364f Mon Sep 17 00:00:00 2001 From: +merlan #flirora Date: Sat, 3 Jun 2023 03:55:36 -0400 Subject: [PATCH 1/3] Add OPoint::lerp --- src/base/interpolation.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/base/interpolation.rs b/src/base/interpolation.rs index 81b1a374..056bea1c 100644 --- a/src/base/interpolation.rs +++ b/src/base/interpolation.rs @@ -1,6 +1,7 @@ use crate::storage::Storage; use crate::{ - Allocator, DefaultAllocator, Dim, OVector, One, RealField, Scalar, Unit, Vector, Zero, + Allocator, DefaultAllocator, Dim, DimName, OPoint, OVector, One, RealField, Scalar, Unit, + Vector, Zero, }; use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub}; @@ -58,6 +59,30 @@ impl OPoint +where + DefaultAllocator: Allocator, +{ + /// Returns `self * (1.0 - t) + rhs.coords * t`, i.e., the linear blend of the points x and y using the scalar value a. + /// + /// The value for a is not restricted to the range `[0, 1]`. + /// + /// # Examples: + /// + /// ``` + /// # use nalgebra::Point3; + /// let x = Point3::new(1.0, 2.0, 3.0); + /// let y = Point3::new(10.0, 20.0, 30.0); + /// assert_eq!(x.lerp(&y, 0.1), Point3::new(1.9, 3.8, 5.7)); + /// ``` + #[must_use] + pub fn lerp(&self, rhs: &OPoint, t: T) -> OPoint { + OPoint { + coords: self.coords.lerp(&rhs.coords, t), + } + } +} + /// # Interpolation between two unit vectors impl> Unit> { /// Computes the spherical linear interpolation between two unit vectors. From f7cd897fd651fd85e080c767c4193796c2fd3b00 Mon Sep 17 00:00:00 2001 From: +merlan #flirora Date: Sat, 3 Jun 2023 04:02:05 -0400 Subject: [PATCH 2/3] Add doc comment for new impl block --- src/base/interpolation.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/base/interpolation.rs b/src/base/interpolation.rs index 056bea1c..e0f855e1 100644 --- a/src/base/interpolation.rs +++ b/src/base/interpolation.rs @@ -59,6 +59,7 @@ impl OPoint where DefaultAllocator: Allocator, From 1e38e6f59534ce77201515c07960a314d08341a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Sat, 8 Jul 2023 14:55:16 +0200 Subject: [PATCH 3/3] Mote Point::lerp to the point.rs file. --- src/base/interpolation.rs | 28 +--------------------------- src/geometry/point.rs | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/base/interpolation.rs b/src/base/interpolation.rs index e0f855e1..81b1a374 100644 --- a/src/base/interpolation.rs +++ b/src/base/interpolation.rs @@ -1,7 +1,6 @@ use crate::storage::Storage; use crate::{ - Allocator, DefaultAllocator, Dim, DimName, OPoint, OVector, One, RealField, Scalar, Unit, - Vector, Zero, + Allocator, DefaultAllocator, Dim, OVector, One, RealField, Scalar, Unit, Vector, Zero, }; use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub}; @@ -59,31 +58,6 @@ impl OPoint -where - DefaultAllocator: Allocator, -{ - /// Returns `self * (1.0 - t) + rhs.coords * t`, i.e., the linear blend of the points x and y using the scalar value a. - /// - /// The value for a is not restricted to the range `[0, 1]`. - /// - /// # Examples: - /// - /// ``` - /// # use nalgebra::Point3; - /// let x = Point3::new(1.0, 2.0, 3.0); - /// let y = Point3::new(10.0, 20.0, 30.0); - /// assert_eq!(x.lerp(&y, 0.1), Point3::new(1.9, 3.8, 5.7)); - /// ``` - #[must_use] - pub fn lerp(&self, rhs: &OPoint, t: T) -> OPoint { - OPoint { - coords: self.coords.lerp(&rhs.coords, t), - } - } -} - /// # Interpolation between two unit vectors impl> Unit> { /// Computes the spherical linear interpolation between two unit vectors. diff --git a/src/geometry/point.rs b/src/geometry/point.rs index 71ace3a9..8e950816 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -1,5 +1,5 @@ use approx::{AbsDiffEq, RelativeEq, UlpsEq}; -use num::One; +use num::{One, Zero}; use std::cmp::Ordering; use std::fmt; use std::hash; @@ -13,6 +13,7 @@ use crate::base::allocator::Allocator; use crate::base::dimension::{DimName, DimNameAdd, DimNameSum, U1}; use crate::base::iter::{MatrixIter, MatrixIterMut}; use crate::base::{Const, DefaultAllocator, OVector, Scalar}; +use simba::scalar::{ClosedAdd, ClosedMul, ClosedSub}; use std::mem::MaybeUninit; /// A point in an euclidean space. @@ -221,6 +222,31 @@ where unsafe { res.assume_init() } } + /// Linear interpolation between two points. + /// + /// Returns `self * (1.0 - t) + rhs.coords * t`, i.e., the linear blend of the points + /// `self` and `rhs` using the scalar value `t`. + /// + /// The value for a is not restricted to the range `[0, 1]`. + /// + /// # Examples: + /// + /// ``` + /// # use nalgebra::Point3; + /// let a = Point3::new(1.0, 2.0, 3.0); + /// let b = Point3::new(10.0, 20.0, 30.0); + /// assert_eq!(a.lerp(&b, 0.1), Point3::new(1.9, 3.8, 5.7)); + /// ``` + #[must_use] + pub fn lerp(&self, rhs: &OPoint, t: T) -> OPoint + where + T: Scalar + Zero + One + ClosedAdd + ClosedSub + ClosedMul, + { + OPoint { + coords: self.coords.lerp(&rhs.coords, t), + } + } + /// Creates a new point with the given coordinates. #[deprecated(note = "Use Point::from(vector) instead.")] #[inline]