Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/box2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,15 @@ where
}
}

impl<T, U> From<Rect<T, U>> for Box2D<T, U>
where
T: Copy + Add<T, Output = T>,
{
fn from(r: Rect<T, U>) -> Self {
r.to_box2d()
}
}

impl<T: Default, U> Default for Box2D<T, U> {
fn default() -> Self {
Box2D {
Expand Down
9 changes: 9 additions & 0 deletions src/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,15 @@ where
}
}

impl<T, U> From<Box2D<T, U>> for Rect<T, U>
where
T: Copy + Sub<T, Output = T>,
{
fn from(b: Box2D<T, U>) -> Self {
b.to_rect()
}
}

/// Shorthand for `Rect::new(Point2D::new(x, y), Size2D::new(w, h))`.
pub const fn rect<T, U>(x: T, y: T, w: T, h: T) -> Rect<T, U> {
Rect::new(Point2D::new(x, y), Size2D::new(w, h))
Expand Down
12 changes: 11 additions & 1 deletion src/rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// except according to those terms.

use crate::approxeq::ApproxEq;
use crate::num::{One, Zero};
use crate::trig::Trig;
use crate::{point2, point3, vec3, Angle, Point2D, Point3D, Vector2D, Vector3D};
use crate::{Transform2D, Transform3D, UnknownUnit};
Expand All @@ -23,7 +24,7 @@ use bytemuck::{Pod, Zeroable};
#[cfg(feature = "malloc_size_of")]
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::real::Real;
use num_traits::{NumCast, One, Zero};
use num_traits::NumCast;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -807,6 +808,15 @@ where
}
}

impl<T, Src, Dst> From<Rotation3D<T, Src, Dst>> for Transform3D<T, Src, Dst>
where
T: Real + ApproxEq<T>,
{
fn from(r: Rotation3D<T, Src, Dst>) -> Self {
r.to_transform()
}
}

#[test]
fn simple_rotation_2d() {
use crate::default::Rotation2D;
Expand Down
36 changes: 36 additions & 0 deletions src/transform2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ use crate::box2d::Box2D;
use crate::num::{One, Zero};
use crate::point::{point2, Point2D};
use crate::rect::Rect;
use crate::scale::Scale;
use crate::transform3d::Transform3D;
use crate::translation::Translation2D;
use crate::trig::Trig;
use crate::vector::{vec2, Vector2D};
use crate::Rotation2D;
use core::cmp::{Eq, PartialEq};
use core::fmt;
use core::hash::Hash;
Expand Down Expand Up @@ -742,6 +745,27 @@ impl<T, Src, Dst> From<Transform2D<T, Src, Dst>> for mint::RowMatrix3x2<T> {
}
}

impl<T, Src, Dst> From<Rotation2D<T, Src, Dst>> for Transform2D<T, Src, Dst>
where
T: Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Zero + Trig,
{
fn from(r: Rotation2D<T, Src, Dst>) -> Self {
r.to_transform()
}
}

impl<T: Copy + Zero, Src, Dst> From<ScaleOffset2D<T, Src, Dst>> for Transform2D<T, Src, Dst> {
fn from(t: ScaleOffset2D<T, Src, Dst>) -> Self {
t.to_transform2d()
}
}

impl<T: Copy + Zero, Src, Dst> From<Scale<T, Src, Dst>> for Transform2D<T, Src, Dst> {
fn from(s: Scale<T, Src, Dst>) -> Self {
Transform2D::scale(s.0, s.0)
}
}

#[repr(C)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
Expand Down Expand Up @@ -1134,6 +1158,18 @@ where
}
}

impl<T: Copy + Zero, Src, Dst> From<Scale<T, Src, Dst>> for ScaleOffset2D<T, Src, Dst> {
fn from(s: Scale<T, Src, Dst>) -> Self {
ScaleOffset2D::scale(s.0, s.0)
}
}

impl<T: Copy + One, Src, Dst> From<Translation2D<T, Src, Dst>> for ScaleOffset2D<T, Src, Dst> {
fn from(t: Translation2D<T, Src, Dst>) -> Self {
ScaleOffset2D::offset(t.x, t.y)
}
}

#[cfg(feature = "arbitrary")]
impl<'a, T, Src, Dst> arbitrary::Arbitrary<'a> for ScaleOffset2D<T, Src, Dst>
where
Expand Down
12 changes: 12 additions & 0 deletions src/transform3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1269,6 +1269,18 @@ impl<T, Src, Dst> From<Transform3D<T, Src, Dst>> for mint::RowMatrix4<T> {
}
}

impl<T: Copy + Zero + One, Src, Dst> From<Transform2D<T, Src, Dst>> for Transform3D<T, Src, Dst> {
fn from(t: Transform2D<T, Src, Dst>) -> Self {
t.to_3d()
}
}

impl<T: Copy + Zero + One, Src, Dst> From<Scale<T, Src, Dst>> for Transform3D<T, Src, Dst> {
fn from(s: Scale<T, Src, Dst>) -> Self {
Transform3D::scale(s.get(), s.get(), s.get())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down