Skip to content

Commit

Permalink
Implementing short constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
samiBendou committed Jan 22, 2020
1 parent c8972fc commit 22365f4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 68 deletions.
11 changes: 8 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! assert_near {

#[macro_export]
macro_rules! impl_vector {
($VectorN:ident { $($field:ident),+ }, $n: expr) => {
($VectorN:ident { $($field:ident),+ }, $n: expr, $label: ident) => {
impl From<f64> for $VectorN {
fn from(s: f64) -> Self {
$VectorN { $($field: s),+ }
Expand All @@ -20,7 +20,7 @@ macro_rules! impl_vector {

impl $VectorN {
#[inline]
pub fn new($($field: f64),+) -> Self {
pub const fn new($($field: f64),+) -> Self {
$VectorN { $($field: $field),+ }
}
}
Expand Down Expand Up @@ -247,6 +247,11 @@ macro_rules! impl_vector {
self
}
}

#[inline]
pub const fn $label($($field: f64),+) -> $VectorN {
$VectorN::new($($field),+)
}
}
}

Expand Down Expand Up @@ -282,4 +287,4 @@ macro_rules! impl_debug_matrix {
}
}
}
}
}
52 changes: 22 additions & 30 deletions src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ pub struct Matrix4 {
pub ww: f64,
}

impl_vector!(Matrix2 {xx, xy, yx, yy}, 4);
impl_vector!(Matrix3 {xx, xy, xz, yx, yy, yz, zx, zy, zz}, 9);
impl_vector!(Matrix4 {xx, xy, xz, xw, yx, yy, yz, yw, zx, zy, zz, zw, wx, wy, wz, ww}, 16);
impl_vector!(Matrix2 {xx, xy, yx, yy}, 4, mat2);
impl_vector!(Matrix3 {xx, xy, xz, yx, yy, yz, zx, zy, zz}, 9, mat3);
impl_vector!(Matrix4 {xx, xy, xz, xw, yx, yy, yz, yw, zx, zy, zz, zw, wx, wy, wz, ww}, 16, mat4);

impl_debug_matrix!(Matrix2);
impl_debug_matrix!(Matrix3);
Expand All @@ -118,8 +118,8 @@ impl_debug_matrix!(Matrix4);
impl Rows<[Vector2; 2]> for Matrix2 {
fn rows(&self) -> [Vector2; 2] {
[
Vector2::new(self.xx, self.xy),
Vector2::new(self.yx, self.yy)
vec2(self.xx, self.xy),
vec2(self.yx, self.yy)
]
}

Expand All @@ -136,9 +136,9 @@ impl Rows<[Vector2; 2]> for Matrix2 {
impl Rows<[Vector3; 3]> for Matrix3 {
fn rows(&self) -> [Vector3; 3] {
[
Vector3::new(self.xx, self.xy, self.xz),
Vector3::new(self.yx, self.yy, self.yz),
Vector3::new(self.zx, self.zy, self.zz),
vec3(self.xx, self.xy, self.xz),
vec3(self.yx, self.yy, self.yz),
vec3(self.zx, self.zy, self.zz),
]
}

Expand All @@ -161,10 +161,10 @@ impl Rows<[Vector3; 3]> for Matrix3 {
impl Rows<[Vector4; 4]> for Matrix4 {
fn rows(&self) -> [Vector4; 4] {
[
Vector4::new(self.xx, self.xy, self.xz, self.xw),
Vector4::new(self.yx, self.yy, self.yz, self.yw),
Vector4::new(self.zx, self.zy, self.zz, self.zw),
Vector4::new(self.wx, self.wy, self.wz, self.ww),
vec4(self.xx, self.xy, self.xz, self.xw),
vec4(self.yx, self.yy, self.yz, self.yw),
vec4(self.zx, self.zy, self.zz, self.zw),
vec4(self.wx, self.wy, self.wz, self.ww),
]
}

Expand Down Expand Up @@ -870,11 +870,8 @@ impl transforms::Rotation3 for Matrix3 {
mod tests {
mod matrix3 {
use crate::common::transforms::Rotation3;
use crate::vector;
use crate::matrix;

use super::super::Algebra;
use super::super::Matrix3;
use crate::vector::{self};
use crate::matrix::{self, *};

#[test]
fn arithmetic() {
Expand All @@ -892,8 +889,8 @@ mod tests {

#[test]
fn transposed() {
let a = Matrix3::new(1., 2., 3., 4., 5., 6., 7., 8., 9.);
assert_eq!(a.transposed(), Matrix3::new(1., 4., 7., 2., 5., 8., 3., 6., 9.));
let a = mat3(1., 2., 3., 4., 5., 6., 7., 8., 9.);
assert_eq!(a.transposed(), mat3(1., 4., 7., 2., 5., 8., 3., 6., 9.));
}

#[test]
Expand Down Expand Up @@ -934,13 +931,8 @@ mod tests {
use crate::common::*;
use crate::common::coordinates::Homogeneous;
use crate::common::transforms::{Rigid, Rotation3, Similarity, Translation};
use crate::matrix::Matrix3;
use crate::vector::Vector3;
use crate::vector;
use crate::matrix;

use super::super::Algebra;
use super::super::Matrix4;
use crate::matrix::{self, *};
use crate::vector::{self, *};

#[test]
fn arithmetic() {
Expand All @@ -958,8 +950,8 @@ mod tests {

#[test]
fn transposed() {
let a = Matrix4::new(1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16.);
assert_eq!(a.transposed(), Matrix4::new(1., 5., 9., 13., 2., 6., 10., 14., 3., 7., 11., 15., 4., 8., 12., 16.));
let a = mat4(1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12., 13., 14., 15., 16.);
assert_eq!(a.transposed(), mat4(1., 5., 9., 13., 2., 6., 10., 14., 3., 7., 11., 15., 4., 8., 12., 16.));
}

#[test]
Expand Down Expand Up @@ -991,7 +983,7 @@ mod tests {
let a = Matrix4::from_rigid(&rotation_z, &unit_x);
let u = unit_x.to_homogeneous();
let moved = a * u;
assert_eq!(Vector3::from_homogeneous(&moved), Vector3::new(1., 1., 0.));
assert_eq!(Vector3::from_homogeneous(&moved), vec3(1., 1., 0.));
}

#[test]
Expand All @@ -1003,7 +995,7 @@ mod tests {
let a = Matrix4::from_similarity(scale, &rotation_z, &unit_x);
let u = unit_x.to_homogeneous();
let moved = a * u;
assert_near!(Vector3::from_homogeneous(&moved).distance2(&Vector3::new(1., 2., 0.)), 0., std::f64::EPSILON);
assert_near!(Vector3::from_homogeneous(&moved).distance2(&vec3(1., 2., 0.)), 0., std::f64::EPSILON);
}
}
}
70 changes: 35 additions & 35 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,38 +79,38 @@ pub struct Vector6 {

impl From<[f64; 2]> for Vector2 {
fn from(array: [f64; 2]) -> Self {
Vector2::new(array[0], array[1])
vec2(array[0], array[1])
}
}

impl From<[f64; 3]> for Vector3 {
fn from(array: [f64; 3]) -> Self {
Vector3::new(array[0], array[1], array[2])
vec3(array[0], array[1], array[2])
}
}

impl From<[f64; 4]> for Vector4 {
fn from(array: [f64; 4]) -> Self {
Vector4::new(array[0], array[1], array[2], array[3])
vec4(array[0], array[1], array[2], array[3])
}
}

impl From<[f64; 6]> for Vector6 {
fn from(array: [f64; 6]) -> Self {
Vector6::new(array[0], array[1], array[2], array[3], array[4], array[5])
vec6(array[0], array[1], array[2], array[3], array[4], array[5])
}
}

impl From<Point2> for Vector4 {
fn from(point: Point2) -> Self {
Vector4::new(point.position.x, point.position.y, point.speed.x, point.speed.y)
vec4(point.position.x, point.position.y, point.speed.x, point.speed.y)
}
}

impl_vector!(Vector2 {x, y}, 2);
impl_vector!(Vector3 {x, y, z}, 3);
impl_vector!(Vector4 {x, y, z, w}, 4);
impl_vector!(Vector6 {x, y, z, u, v, w}, 6);
impl_vector!(Vector2 {x, y}, 2, vec2);
impl_vector!(Vector3 {x, y, z}, 3, vec3);
impl_vector!(Vector4 {x, y, z, w}, 4, vec4);
impl_vector!(Vector6 {x, y, z, u, v, w}, 6, vec6);

impl_debug_vector!(Vector2 {x, y});
impl_debug_vector!(Vector3 {x, y, z});
Expand Down Expand Up @@ -350,21 +350,21 @@ impl coordinates::Spherical for Vector3 {

impl coordinates::Homogeneous<Vector3> for Vector2 {
fn from_homogeneous(vector: &Vector3) -> Self {
Vector2::new(vector.x / vector.z, vector.y / vector.z)
vec2(vector.x / vector.z, vector.y / vector.z)
}

fn to_homogeneous(&self) -> Vector3 {
Vector3::new(self.x, self.y, 1.)
vec3(self.x, self.y, 1.)
}
}

impl coordinates::Homogeneous<Vector4> for Vector3 {
fn from_homogeneous(vector: &Vector4) -> Self {
Vector3::new(vector.x / vector.w, vector.y / vector.w, vector.z / vector.w)
vec3(vector.x / vector.w, vector.y / vector.w, vector.z / vector.w)
}

fn to_homogeneous(&self) -> Vector4 {
Vector4::new(self.x, self.y, self.z, 1.)
vec4(self.x, self.y, self.z, 1.)
}
}

Expand Down Expand Up @@ -493,17 +493,17 @@ impl Split<Vector2> for Vector4 {
}

fn concat(lhs: &Vector2, rhs: &Vector2) -> Self {
Vector4::new(lhs.x, lhs.y, rhs.x, rhs.y)
vec4(lhs.x, lhs.y, rhs.x, rhs.y)
}

#[inline]
fn upper(&self) -> Vector2 {
Vector2::new(self.x, self.y)
vec2(self.x, self.y)
}

#[inline]
fn lower(&self) -> Vector2 {
Vector2::new(self.z, self.w)
vec2(self.z, self.w)
}

#[inline]
Expand All @@ -527,17 +527,17 @@ impl Split<Vector3> for Vector6 {
}

fn concat(lhs: &Vector3, rhs: &Vector3) -> Self {
Vector6::new(lhs.x, lhs.y, lhs.z, rhs.x, rhs.y, rhs.z)
vec6(lhs.x, lhs.y, lhs.z, rhs.x, rhs.y, rhs.z)
}

#[inline]
fn upper(&self) -> Vector3 {
Vector3::new(self.x, self.y, self.z)
vec3(self.x, self.y, self.z)
}

#[inline]
fn lower(&self) -> Vector3 {
Vector3::new(self.u, self.v, self.w)
vec3(self.u, self.v, self.w)
}

#[inline]
Expand Down Expand Up @@ -565,11 +565,11 @@ mod tests {
use crate::common::transforms::Rotation3;
use crate::vector;
use crate::common::coordinates::*;
use crate::vector::Vector3;
use crate::vector::vec3;

#[test]
fn new() {
let u = Vector3::new(1., 2., 3.);
let u = vec3(1., 2., 3.);
assert_eq!(u.x, 1.);
assert_eq!(u.y, 2.);
assert_eq!(u.z, 3.);
Expand All @@ -579,7 +579,7 @@ mod tests {
fn magnitude() {
let sqrt_2 = std::f64::consts::SQRT_2;
let zero = vector::consts::ZEROS_3;
let u = Vector3::new(1., 1., 0.);
let u = vec3(1., 1., 0.);
assert_eq!(u.magnitude2(), 2.);
assert_eq!(u.magnitude(), sqrt_2);
assert_eq!(u.distance2(&zero), 2.);
Expand All @@ -588,22 +588,22 @@ mod tests {

#[test]
fn partial_eq() {
let u = Vector3::new(-4., 0., 1.);
let v = Vector3::new(-2., 0., 1.);
let u = vec3(-4., 0., 1.);
let v = vec3(-2., 0., 1.);
assert_eq!(u, u);
assert_ne!(u, v);
}

#[test]
fn polar_coordinates() {
let u = Vector3::ones();
let u = vector::consts::ONES_3;
assert_eq!(u.rho(), std::f64::consts::SQRT_2);
assert_eq!(u.phi(), std::f64::consts::FRAC_PI_4);
}

#[test]
fn normalized() {
let mut u = Vector3::new(1., 1., 0.);
let mut u = vec3(1., 1., 0.);
let tol = 10. * std::f64::EPSILON;
let inv_sqrt2 = std::f64::consts::FRAC_1_SQRT_2;
u.set_normalized();
Expand All @@ -615,30 +615,30 @@ mod tests {

#[test]
fn fmt() {
let u = Vector3::new(1., 2., 3.);
let u = vec3(1., 2., 3.);
let formatted = format!("{:?}", u);
assert_eq!(formatted.as_str(), "( 1.000 2.000 3.000 )");
}

#[test]
fn distance() {
let u = Vector3::new(1., 1., 0.);
let u = vec3(1., 1., 0.);
let v = vector::consts::ZEROS_3;
assert_eq!(u.distance2(&v), 2f64);
}

#[test]
fn arithmetic() {
let mut u = Vector3::new(-4., 1., 1.);
let v = Vector3::new(3., 2., -1.);
let mut u = vec3(-4., 1., 1.);
let v = vec3(3., 2., -1.);

assert_eq!(u + v, Vector3::new(-1., 3., 0.));
assert_eq!(u - v, Vector3::new(-7., -1., 2.));
assert_eq!(u * 2., Vector3::new(-8., 2., 2.));
assert_eq!(u / 4., Vector3::new(-1., 0.25, 0.25));
assert_eq!(u + v, vec3(-1., 3., 0.));
assert_eq!(u - v, vec3(-7., -1., 2.));
assert_eq!(u * 2., vec3(-8., 2., 2.));
assert_eq!(u / 4., vec3(-1., 0.25, 0.25));

u += v;
assert_eq!(u, Vector3::new(-1., 3., 0.));
assert_eq!(u, vec3(-1., 3., 0.));
}

#[test]
Expand Down

0 comments on commit 22365f4

Please sign in to comment.