diff --git a/src/point.rs b/src/point.rs index 28f47f74..b93d3305 100644 --- a/src/point.rs +++ b/src/point.rs @@ -327,6 +327,19 @@ impl, U> ApproxEq for TypedPoint2D { } } +impl Into<[T; 2]> for TypedPoint2D { + fn into(self) -> [T; 2] { + self.to_array() + } +} + +impl From<[T; 2]> for TypedPoint2D { + fn from(array: [T; 2]) -> Self { + point2(array[0], array[1]) + } +} + + define_matrix! { /// A 3d Point tagged with a unit. pub struct TypedPoint3D { @@ -598,6 +611,18 @@ impl, U> ApproxEq for TypedPoint3D { } } +impl Into<[T; 3]> for TypedPoint3D { + fn into(self) -> [T; 3] { + self.to_array() + } +} + +impl From<[T; 3]> for TypedPoint3D { + fn from(array: [T; 3]) -> Self { + point3(array[0], array[1], array[2]) + } +} + pub fn point2(x: T, y: T) -> TypedPoint2D { TypedPoint2D::new(x, y) diff --git a/src/rect.rs b/src/rect.rs index defd4e79..22dd23bb 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -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. @@ -54,6 +55,14 @@ impl Serialize for TypedRect { } } +impl Hash for TypedRect +{ + fn hash(&self, h: &mut H) { + self.origin.hash(h); + self.size.hash(h); + } +} + impl Copy for TypedRect {} impl Clone for TypedRect { diff --git a/src/transform2d.rs b/src/transform2d.rs index 9852fa82..253be30f 100644 --- a/src/transform2d.rs +++ b/src/transform2d.rs @@ -79,6 +79,36 @@ impl TypedTransform2D { ] } + /// 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 { Transform2D::row_major( diff --git a/src/transform3d.rs b/src/transform3d.rs index d4613b97..e846f22f 100644 --- a/src/transform3d.rs +++ b/src/transform3d.rs @@ -597,7 +597,7 @@ impl TypedTransform3D { /// 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], @@ -618,6 +618,26 @@ impl TypedTransform3D { [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 fmt::Debug for TypedTransform3D diff --git a/src/vector.rs b/src/vector.rs index fa7ef7bc..a1c9076d 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -348,6 +348,18 @@ impl, U> ApproxEq> for TypedVector2D Into<[T; 2]> for TypedVector2D { + fn into(self) -> [T; 2] { + self.to_array() + } +} + +impl From<[T; 2]> for TypedVector2D { + fn from(array: [T; 2]) -> Self { + vec2(array[0], array[1]) + } +} + define_matrix! { /// A 3d Vector tagged with a unit. pub struct TypedVector3D { @@ -675,6 +687,19 @@ impl, U> ApproxEq> for TypedVector3D Into<[T; 3]> for TypedVector3D { + fn into(self) -> [T; 3] { + self.to_array() + } +} + +impl From<[T; 3]> for TypedVector3D { + fn from(array: [T; 3]) -> Self { + vec3(array[0], array[1], array[2]) + } +} + + /// Convenience constructor. #[inline] pub fn vec2(x: T, y: T) -> TypedVector2D {