Remove operator methods from Vector trait
This commit is contained in:
parent
d468d76e0a
commit
25f5dfd5fc
5 changed files with 60 additions and 145 deletions
|
@ -467,14 +467,14 @@ impl<S: BaseFloat> Matrix for Matrix2<S> {
|
|||
|
||||
#[inline]
|
||||
fn mul_self_s(&mut self, s: S) {
|
||||
self[0].mul_self_s(s);
|
||||
self[1].mul_self_s(s);
|
||||
self[0] = self[0] * s;
|
||||
self[1] = self[1] * s;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn div_self_s(&mut self, s: S) {
|
||||
self[0].div_self_s(s);
|
||||
self[1].div_self_s(s);
|
||||
self[0] = self[0] / s;
|
||||
self[1] = self[1] / s;
|
||||
}
|
||||
|
||||
fn transpose(&self) -> Matrix2<S> {
|
||||
|
@ -511,14 +511,14 @@ impl<S: BaseFloat> SquareMatrix for Matrix2<S> {
|
|||
|
||||
#[inline]
|
||||
fn add_self_m(&mut self, m: &Matrix2<S>) {
|
||||
self[0].add_self_v(m[0]);
|
||||
self[1].add_self_v(m[1]);
|
||||
self[0] = self[0] + m[0];
|
||||
self[1] = self[1] + m[1];
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sub_self_m(&mut self, m: &Matrix2<S>) {
|
||||
self[0].sub_self_v(m[0]);
|
||||
self[1].sub_self_v(m[1]);
|
||||
self[0] = self[0] - m[0];
|
||||
self[1] = self[1] - m[1];
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -615,16 +615,16 @@ impl<S: BaseFloat> Matrix for Matrix3<S> {
|
|||
|
||||
#[inline]
|
||||
fn mul_self_s(&mut self, s: S) {
|
||||
self[0].mul_self_s(s);
|
||||
self[1].mul_self_s(s);
|
||||
self[2].mul_self_s(s);
|
||||
self[0] = self[0] * s;
|
||||
self[1] = self[1] * s;
|
||||
self[2] = self[2] * s;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn div_self_s(&mut self, s: S) {
|
||||
self[0].div_self_s(s);
|
||||
self[1].div_self_s(s);
|
||||
self[2].div_self_s(s);
|
||||
self[0] = self[0] / s;
|
||||
self[1] = self[1] / s;
|
||||
self[2] = self[2] / s;
|
||||
}
|
||||
|
||||
fn transpose(&self) -> Matrix3<S> {
|
||||
|
@ -664,16 +664,16 @@ impl<S: BaseFloat> SquareMatrix for Matrix3<S> {
|
|||
|
||||
#[inline]
|
||||
fn add_self_m(&mut self, m: &Matrix3<S>) {
|
||||
self[0].add_self_v(m[0]);
|
||||
self[1].add_self_v(m[1]);
|
||||
self[2].add_self_v(m[2]);
|
||||
self[0] = self[0] + m[0];
|
||||
self[1] = self[1] + m[1];
|
||||
self[2] = self[2] + m[2];
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sub_self_m(&mut self, m: &Matrix3<S>) {
|
||||
self[0].sub_self_v(m[0]);
|
||||
self[1].sub_self_v(m[1]);
|
||||
self[2].sub_self_v(m[2]);
|
||||
self[0] = self[0] - m[0];
|
||||
self[1] = self[1] - m[1];
|
||||
self[2] = self[2] - m[2];
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -784,18 +784,18 @@ impl<S: BaseFloat> Matrix for Matrix4<S> {
|
|||
|
||||
#[inline]
|
||||
fn mul_self_s(&mut self, s: S) {
|
||||
self[0].mul_self_s(s);
|
||||
self[1].mul_self_s(s);
|
||||
self[2].mul_self_s(s);
|
||||
self[3].mul_self_s(s);
|
||||
self[0] = self[0] * s;
|
||||
self[1] = self[1] * s;
|
||||
self[2] = self[2] * s;
|
||||
self[3] = self[3] * s;
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn div_self_s(&mut self, s: S) {
|
||||
self[0].div_self_s(s);
|
||||
self[1].div_self_s(s);
|
||||
self[2].div_self_s(s);
|
||||
self[3].div_self_s(s);
|
||||
self[0] = self[0] / s;
|
||||
self[1] = self[1] / s;
|
||||
self[2] = self[2] / s;
|
||||
self[3] = self[3] / s;
|
||||
}
|
||||
|
||||
fn transpose(&self) -> Matrix4<S> {
|
||||
|
@ -838,18 +838,18 @@ impl<S: BaseFloat> SquareMatrix for Matrix4<S> {
|
|||
|
||||
#[inline]
|
||||
fn add_self_m(&mut self, m: &Matrix4<S>) {
|
||||
self[0].add_self_v(m[0]);
|
||||
self[1].add_self_v(m[1]);
|
||||
self[2].add_self_v(m[2]);
|
||||
self[3].add_self_v(m[3]);
|
||||
self[0] = self[0] + m[0];
|
||||
self[1] = self[1] + m[1];
|
||||
self[2] = self[2] + m[2];
|
||||
self[3] = self[3] + m[3];
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn sub_self_m(&mut self, m: &Matrix4<S>) {
|
||||
self[0].sub_self_v(m[0]);
|
||||
self[1].sub_self_v(m[1]);
|
||||
self[2].sub_self_v(m[2]);
|
||||
self[3].sub_self_v(m[3]);
|
||||
self[0] = self[0] - m[0];
|
||||
self[1] = self[1] - m[1];
|
||||
self[2] = self[2] - m[2];
|
||||
self[3] = self[3] - m[3];
|
||||
}
|
||||
|
||||
fn transpose_self(&mut self) {
|
||||
|
|
|
@ -363,7 +363,7 @@ impl<S: BaseFloat> Rotation3<S> for Quaternion<S> where S: 'static {
|
|||
#[inline]
|
||||
fn from_axis_angle(axis: Vector3<S>, angle: Rad<S>) -> Quaternion<S> {
|
||||
let (s, c) = sin_cos(angle.mul_s(cast(0.5f64).unwrap()));
|
||||
Quaternion::from_sv(c, axis.mul_s(s))
|
||||
Quaternion::from_sv(c, axis * s)
|
||||
}
|
||||
|
||||
/// - [Maths - Conversion Euler to Quaternion]
|
||||
|
|
|
@ -81,6 +81,8 @@ pub struct Decomposed<V: Vector, R> {
|
|||
impl<P: Point, R: Rotation<P>> Transform<P> for Decomposed<P::Vector, R> where
|
||||
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
|
||||
<P as Point>::Scalar: BaseFloat,
|
||||
// FIXME: Investigate why this is needed!
|
||||
<P as Point>::Vector: Vector,
|
||||
{
|
||||
#[inline]
|
||||
fn one() -> Decomposed<P::Vector, R> {
|
||||
|
@ -104,7 +106,7 @@ impl<P: Point, R: Rotation<P>> Transform<P> for Decomposed<P::Vector, R> where
|
|||
|
||||
#[inline]
|
||||
fn transform_vector(&self, vec: P::Vector) -> P::Vector {
|
||||
self.rot.rotate_vector(vec.mul_s(self.scale))
|
||||
self.rot.rotate_vector(vec * self.scale)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -126,7 +128,7 @@ impl<P: Point, R: Rotation<P>> Transform<P> for Decomposed<P::Vector, R> where
|
|||
} else {
|
||||
let s = <P as Point>::Scalar::one() / self.scale;
|
||||
let r = self.rot.invert();
|
||||
let d = r.rotate_vector(self.disp.clone()).mul_s(-s);
|
||||
let d = r.rotate_vector(self.disp.clone()) * -s;
|
||||
Some(Decomposed {
|
||||
scale: s,
|
||||
rot: r,
|
||||
|
|
123
src/vector.rs
123
src/vector.rs
|
@ -73,13 +73,6 @@
|
|||
//! // But there is also a top-level function:
|
||||
//! assert_eq!(a.dot(b), dot(a, b));
|
||||
//!
|
||||
//! // Scalar multiplication can return a new object, or be done in place
|
||||
//! // to avoid an allocation:
|
||||
//! let mut c = Vector4::from_value(3f64);
|
||||
//! let d: Vector4<f64> = c.mul_s(2.0);
|
||||
//! c.mul_self_s(2.0);
|
||||
//! assert_eq!(c, d);
|
||||
//!
|
||||
//! // Cross products are defined for 3-dimensional vectors:
|
||||
//! let e: Vector3<f64> = Vector3::unit_x();
|
||||
//! let f: Vector3<f64> = Vector3::unit_y();
|
||||
|
@ -114,20 +107,18 @@ use num::{BaseNum, BaseFloat, PartialOrd};
|
|||
pub trait Vector: Copy + Clone where
|
||||
// FIXME: Ugly type signatures - blocked by rust-lang/rust#24092
|
||||
Self: Array<Element = <Self as Vector>::Scalar>,
|
||||
// FIXME: blocked by rust-lang/rust#20671
|
||||
//
|
||||
// for<'a, 'b> &'a Self: Add<&'b Self, Output = Self>,
|
||||
// for<'a, 'b> &'a Self: Sub<&'b Self, Output = Self>,
|
||||
// for<'a, 'b> &'a Self: Mul<&'b Self, Output = Self>,
|
||||
// for<'a, 'b> &'a Self: Div<&'b Self, Output = Self>,
|
||||
// for<'a, 'b> &'a Self: Rem<&'b Self, Output = Self>,
|
||||
// for<'a, 'b> &'a Self: Sub<&'b Self, Output = Self>,
|
||||
//
|
||||
// for<'a> &'a Self: Add<S, Output = Self>,
|
||||
// for<'a> &'a Self: Sub<S, Output = Self>,
|
||||
// for<'a> &'a Self: Mul<S, Output = Self>,
|
||||
// for<'a> &'a Self: Div<S, Output = Self>,
|
||||
// for<'a> &'a Self: Rem<S, Output = Self>,
|
||||
|
||||
Self: Add<Self, Output = Self>,
|
||||
Self: Sub<Self, Output = Self>,
|
||||
Self: Mul<Self, Output = Self>,
|
||||
Self: Div<Self, Output = Self>,
|
||||
Self: Rem<Self, Output = Self>,
|
||||
|
||||
Self: Add<<Self as Vector>::Scalar, Output = Self>,
|
||||
Self: Sub<<Self as Vector>::Scalar, Output = Self>,
|
||||
Self: Mul<<Self as Vector>::Scalar, Output = Self>,
|
||||
Self: Div<<Self as Vector>::Scalar, Output = Self>,
|
||||
Self: Rem<<Self as Vector>::Scalar, Output = Self>,
|
||||
{
|
||||
/// The associated scalar.
|
||||
type Scalar: BaseNum;
|
||||
|
@ -142,60 +133,6 @@ pub trait Vector: Copy + Clone where
|
|||
#[inline]
|
||||
fn one() -> Self { Self::from_value(Self::Scalar::one()) }
|
||||
|
||||
/// Add a scalar to this vector, returning a new vector.
|
||||
#[must_use]
|
||||
fn add_s(self, scalar: Self::Scalar) -> Self;
|
||||
/// Subtract a scalar from this vector, returning a new vector.
|
||||
#[must_use]
|
||||
fn sub_s(self, scalar: Self::Scalar) -> Self;
|
||||
/// Multiply this vector by a scalar, returning a new vector.
|
||||
#[must_use]
|
||||
fn mul_s(self, scalar: Self::Scalar) -> Self;
|
||||
/// Divide this vector by a scalar, returning a new vector.
|
||||
#[must_use]
|
||||
fn div_s(self, scalar: Self::Scalar) -> Self;
|
||||
/// Take the remainder of this vector by a scalar, returning a new vector.
|
||||
#[must_use]
|
||||
fn rem_s(self, scalar: Self::Scalar) -> Self;
|
||||
|
||||
/// Add this vector to another, returning a new vector.
|
||||
#[must_use]
|
||||
fn add_v(self, v: Self) -> Self;
|
||||
/// Subtract another vector from this one, returning a new vector.
|
||||
#[must_use]
|
||||
fn sub_v(self, v: Self) -> Self;
|
||||
/// Multiply this vector by another, returning a new vector.
|
||||
#[must_use]
|
||||
fn mul_v(self, v: Self) -> Self;
|
||||
/// Divide this vector by another, returning a new vector.
|
||||
#[must_use]
|
||||
fn div_v(self, v: Self) -> Self;
|
||||
/// Take the remainder of this vector by another, returning a new scalar.
|
||||
#[must_use]
|
||||
fn rem_v(self, v: Self) -> Self;
|
||||
|
||||
/// Add a scalar to this vector in-place.
|
||||
fn add_self_s(&mut self, scalar: Self::Scalar);
|
||||
/// Subtract a scalar from this vector, in-place.
|
||||
fn sub_self_s(&mut self, scalar: Self::Scalar);
|
||||
/// Multiply this vector by a scalar, in-place.
|
||||
fn mul_self_s(&mut self, scalar: Self::Scalar);
|
||||
/// Divide this vector by a scalar, in-place.
|
||||
fn div_self_s(&mut self, scalar: Self::Scalar);
|
||||
/// Take the remainder of this vector by a scalar, in-place.
|
||||
fn rem_self_s(&mut self, scalar: Self::Scalar);
|
||||
|
||||
/// Add another vector to this one, in-place.
|
||||
fn add_self_v(&mut self, v: Self);
|
||||
/// Subtract another vector from this one, in-place.
|
||||
fn sub_self_v(&mut self, v: Self);
|
||||
/// Multiply this matrix by another, in-place.
|
||||
fn mul_self_v(&mut self, v: Self);
|
||||
/// Divide this matrix by anothor, in-place.
|
||||
fn div_self_v(&mut self, v: Self);
|
||||
/// Take the remainder of this vector by another, in-place.
|
||||
fn rem_self_v(&mut self, v: Self);
|
||||
|
||||
/// Vector dot product
|
||||
fn dot(self, other: Self) -> Self::Scalar;
|
||||
}
|
||||
|
@ -253,30 +190,6 @@ macro_rules! impl_vector {
|
|||
|
||||
#[inline] fn from_value(scalar: S) -> $VectorN<S> { $VectorN { $($field: scalar),+ } }
|
||||
|
||||
#[inline] fn add_s(self, scalar: S) -> $VectorN<S> { self + scalar }
|
||||
#[inline] fn sub_s(self, scalar: S) -> $VectorN<S> { self - scalar }
|
||||
#[inline] fn mul_s(self, scalar: S) -> $VectorN<S> { self * scalar }
|
||||
#[inline] fn div_s(self, scalar: S) -> $VectorN<S> { self / scalar }
|
||||
#[inline] fn rem_s(self, scalar: S) -> $VectorN<S> { self % scalar }
|
||||
|
||||
#[inline] fn add_v(self, v: $VectorN<S>) -> $VectorN<S> { self + v }
|
||||
#[inline] fn sub_v(self, v: $VectorN<S>) -> $VectorN<S> { self - v }
|
||||
#[inline] fn mul_v(self, v: $VectorN<S>) -> $VectorN<S> { self * v }
|
||||
#[inline] fn div_v(self, v: $VectorN<S>) -> $VectorN<S> { self / v }
|
||||
#[inline] fn rem_v(self, v: $VectorN<S>) -> $VectorN<S> { self % v }
|
||||
|
||||
#[inline] fn add_self_s(&mut self, scalar: S) { *self = &*self + scalar; }
|
||||
#[inline] fn sub_self_s(&mut self, scalar: S) { *self = &*self - scalar; }
|
||||
#[inline] fn mul_self_s(&mut self, scalar: S) { *self = &*self * scalar; }
|
||||
#[inline] fn div_self_s(&mut self, scalar: S) { *self = &*self / scalar; }
|
||||
#[inline] fn rem_self_s(&mut self, scalar: S) { *self = &*self % scalar; }
|
||||
|
||||
#[inline] fn add_self_v(&mut self, v: $VectorN<S>) { *self = &*self + v; }
|
||||
#[inline] fn sub_self_v(&mut self, v: $VectorN<S>) { *self = &*self - v; }
|
||||
#[inline] fn mul_self_v(&mut self, v: $VectorN<S>) { *self = &*self * v; }
|
||||
#[inline] fn div_self_v(&mut self, v: $VectorN<S>) { *self = &*self / v; }
|
||||
#[inline] fn rem_self_v(&mut self, v: $VectorN<S>) { *self = &*self % v; }
|
||||
|
||||
#[inline] fn dot(self, other: $VectorN<S>) -> S { (self * other).sum() }
|
||||
}
|
||||
|
||||
|
@ -524,7 +437,7 @@ pub trait EuclideanVector: Vector + Sized where
|
|||
#[inline]
|
||||
#[must_use]
|
||||
fn normalize_to(self, length: Self::Scalar) -> Self {
|
||||
self.mul_s(length / self.length())
|
||||
self * (length / self.length())
|
||||
}
|
||||
|
||||
/// Returns the result of linarly interpolating the length of the vector
|
||||
|
@ -532,7 +445,7 @@ pub trait EuclideanVector: Vector + Sized where
|
|||
#[inline]
|
||||
#[must_use]
|
||||
fn lerp(self, other: Self, amount: Self::Scalar) -> Self {
|
||||
self.add_v(other.sub_v(self).mul_s(amount))
|
||||
self + ((other - self) * amount)
|
||||
}
|
||||
|
||||
/// Normalises the vector to a length of `1`.
|
||||
|
@ -540,21 +453,21 @@ pub trait EuclideanVector: Vector + Sized where
|
|||
fn normalize_self(&mut self) {
|
||||
// Not sure why these annotations are needed
|
||||
let rlen = <<Self as Vector>::Scalar as ::rust_num::Float>::recip(self.length());
|
||||
self.mul_self_s(rlen);
|
||||
*self = *self * rlen;
|
||||
}
|
||||
|
||||
/// Normalizes the vector to `length`.
|
||||
#[inline]
|
||||
fn normalize_self_to(&mut self, length: Self::Scalar) {
|
||||
let n = length / self.length();
|
||||
self.mul_self_s(n);
|
||||
*self = *self * n;
|
||||
}
|
||||
|
||||
/// Linearly interpolates the length of the vector towards the length of
|
||||
/// `other` by the specified amount.
|
||||
fn lerp_self(&mut self, other: Self, amount: Self::Scalar) {
|
||||
let v = other.sub_v(*self).mul_s(amount);
|
||||
self.add_self_v(v);
|
||||
let v = (other - *self) * amount;
|
||||
*self = *self * v;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
extern crate cgmath;
|
||||
|
||||
use cgmath::{Vector4, ortho, Matrix, Matrix4, Vector};
|
||||
use cgmath::{Vector4, ortho, Matrix, Matrix4};
|
||||
|
||||
#[test]
|
||||
fn test_ortho_scale() {
|
||||
|
|
Loading…
Reference in a new issue