From bfab204a2412c2d03065fc53cec7ca783eb3ffd1 Mon Sep 17 00:00:00 2001 From: sebcrozet Date: Sat, 27 Oct 2018 15:16:03 +0200 Subject: [PATCH] Add lerp for vectors. Fix #453 --- src/base/matrix.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/base/matrix.rs b/src/base/matrix.rs index a52b18e4..91e47624 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -1,4 +1,4 @@ -use num::Zero; +use num::{One, Zero}; use num_complex::Complex; #[cfg(feature = "abomonation-serialize")] use std::io::{Result as IOResult, Write}; @@ -16,7 +16,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(feature = "abomonation-serialize")] use abomonation::Abomonation; -use alga::general::{Real, Ring}; +use alga::general::{ClosedAdd, ClosedMul, ClosedSub, Real, Ring}; use base::allocator::{Allocator, SameShapeAllocator, SameShapeC, SameShapeR}; use base::constraint::{DimEq, SameNumberOfColumns, SameNumberOfRows, ShapeConstraint}; @@ -1307,6 +1307,29 @@ impl> Matrix { } } +impl> + Vector +{ + /// Returns `self * (1.0 - t) + rhs * t`, i.e., the linear blend of the vectors x and y using the scalar value a. + /// + /// The value for a is not restricted to the range `[0, 1]`. + /// + /// # Examples: + /// + /// ``` + /// # use nalgebra::Vector3; + /// let x = Vector3::new(1.0, 2.0, 3.0); + /// let y = Vector3::new(10.0, 20.0, 30.0); + /// assert_eq!(x.lerp(&y, 0.1), Vector3::new(1.9, 3.8, 5.7)); + /// ``` + pub fn lerp>(&self, rhs: &Vector, t: N) -> VectorN + where DefaultAllocator: Allocator { + let mut res = self.clone_owned(); + res.axpy(t, rhs, N::one() - t); + res + } +} + impl> Unit> { /// Computes the spherical linear interpolation between two unit vectors. pub fn slerp>(