Skip to content

Commit

Permalink
Implement some missing convenience traits and methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed May 30, 2017
1 parent 6610d5f commit 0e9bf89
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,19 @@ impl<T: Copy+ApproxEq<T>, U> ApproxEq<Self> for TypedPoint2D<T, U> {
}
}

impl<T: Copy, U> Into<[T; 2]> for TypedPoint2D<T, U> {
fn into(self) -> [T; 2] {
self.to_array()
}
}

impl<T: Copy, U> From<[T; 2]> for TypedPoint2D<T, U> {
fn from(array: [T; 2]) -> Self {
point2(array[0], array[1])
}
}


define_matrix! {
/// A 3d Point tagged with a unit.
pub struct TypedPoint3D<T, U> {
Expand Down Expand Up @@ -598,6 +611,18 @@ impl<T: Copy+ApproxEq<T>, U> ApproxEq<Self> for TypedPoint3D<T, U> {
}
}

impl<T: Copy, U> Into<[T; 3]> for TypedPoint3D<T, U> {
fn into(self) -> [T; 3] {
self.to_array()
}
}

impl<T: Copy, U> From<[T; 3]> for TypedPoint3D<T, U> {
fn from(array: [T; 3]) -> Self {
point3(array[0], array[1], array[2])
}
}


pub fn point2<T: Copy, U>(x: T, y: T) -> TypedPoint2D<T, U> {
TypedPoint2D::new(x, y)
Expand Down
9 changes: 9 additions & 0 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use num_traits::NumCast;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cmp::PartialOrd;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::ops::{Add, Sub, Mul, Div};

/// A 2d Rectangle optionally tagged with a unit.
Expand Down Expand Up @@ -54,6 +55,14 @@ impl<T: Serialize, U> Serialize for TypedRect<T, U> {
}
}

impl<T: Hash, U> Hash for TypedRect<T, U>
{
fn hash<H: Hasher>(&self, h: &mut H) {
self.origin.hash(h);
self.size.hash(h);
}
}

impl<T: Copy, U> Copy for TypedRect<T, U> {}

impl<T: Copy, U> Clone for TypedRect<T, U> {
Expand Down
30 changes: 30 additions & 0 deletions src/transform2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,36 @@ impl<T: Copy, Src, Dst> TypedTransform2D<T, Src, Dst> {
]
}

/// Returns an array containing this transform's 3 rows in (in row-major order)
/// as arrays.
///
/// This is a convenience method to interface with other libraries like glium.
pub fn to_row_arrays(&self) -> [[T; 2]; 3] {
[
[self.m11, self.m12],
[self.m21, self.m22],
[self.m31, self.m32],
]
}

/// Creates a transform from an array of 6 elements in row-major order.
pub fn from_row_major_array(array: [T; 6]) -> Self {
Self::row_major(
array[0], array[1],
array[2], array[3],
array[4], array[5],
)
}

/// Creates a transform from 3 rows of 2 elements (row-major order).
pub fn from_row_arrays(array: [[T; 2]; 3]) -> Self {
Self::row_major(
array[0][0], array[0][1],
array[1][0], array[1][1],
array[2][0], array[2][1],
)
}

/// Drop the units, preserving only the numeric value.
pub fn to_untyped(&self) -> Transform2D<T> {
Transform2D::row_major(
Expand Down
22 changes: 21 additions & 1 deletion src/transform3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ impl<T: Copy, Src, Dst> TypedTransform3D<T, Src, Dst> {
/// as arrays.
///
/// This is a convenience method to interface with other libraries like glium.
pub fn to_row_arrays(&self) -> [[T; 4];4] {
pub fn to_row_arrays(&self) -> [[T; 4]; 4] {
[
[self.m11, self.m12, self.m13, self.m14],
[self.m21, self.m22, self.m23, self.m24],
Expand All @@ -618,6 +618,26 @@ impl<T: Copy, Src, Dst> TypedTransform3D<T, Src, Dst> {
[self.m14, self.m24, self.m34, self.m44]
]
}

/// Creates a transform from an array of 16 elements in row-major order.
pub fn from_array(array: [T; 16]) -> Self {
Self::row_major(
array[0], array[1], array[2], array[3],
array[4], array[5], array[6], array[7],
array[8], array[9], array[10], array[11],
array[12], array[13], array[14], array[15],
)
}

/// Creates a transform from 4 rows of 4 elements (row-major order).
pub fn from_row_arrays(array: [[T; 4]; 4]) -> Self {
Self::row_major(
array[0][0], array[0][1], array[0][2], array[0][3],
array[1][0], array[1][1], array[1][2], array[1][3],
array[2][0], array[2][1], array[2][2], array[2][3],
array[3][0], array[3][1], array[3][2], array[3][3],
)
}
}

impl<T, Src, Dst> fmt::Debug for TypedTransform3D<T, Src, Dst>
Expand Down
25 changes: 25 additions & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,18 @@ impl<T: Copy+ApproxEq<T>, U> ApproxEq<TypedVector2D<T, U>> for TypedVector2D<T,
}
}

impl<T: Copy, U> Into<[T; 2]> for TypedVector2D<T, U> {
fn into(self) -> [T; 2] {
self.to_array()
}
}

impl<T: Copy, U> From<[T; 2]> for TypedVector2D<T, U> {
fn from(array: [T; 2]) -> Self {
vec2(array[0], array[1])
}
}

define_matrix! {
/// A 3d Vector tagged with a unit.
pub struct TypedVector3D<T, U> {
Expand Down Expand Up @@ -675,6 +687,19 @@ impl<T: Copy+ApproxEq<T>, U> ApproxEq<TypedVector3D<T, U>> for TypedVector3D<T,
}
}

impl<T: Copy, U> Into<[T; 3]> for TypedVector3D<T, U> {
fn into(self) -> [T; 3] {
self.to_array()
}
}

impl<T: Copy, U> From<[T; 3]> for TypedVector3D<T, U> {
fn from(array: [T; 3]) -> Self {
vec3(array[0], array[1], array[2])
}
}


/// Convenience constructor.
#[inline]
pub fn vec2<T: Copy, U>(x: T, y: T) -> TypedVector2D<T, U> {
Expand Down

0 comments on commit 0e9bf89

Please sign in to comment.