diff --git a/src/angle.rs b/src/angle.rs index e3a84b8..dbb0a5e 100644 --- a/src/angle.rs +++ b/src/angle.rs @@ -40,39 +40,19 @@ pub struct Deg { pub s: S } /// Create a new angle, in degrees #[inline] pub fn deg(s: S) -> Deg { Deg { s: s } } -/// Represents types that can be converted to radians. -pub trait ToRad { - /// Convert this value to radians. - fn to_rad(&self) -> Rad; -} - -/// Represents types that can be converted to degrees. -pub trait ToDeg { - /// Convert this value to degrees. - fn to_deg(&self) -> Deg; -} - -impl ToRad for Rad { +impl From> for Deg where S: BaseFloat { #[inline] - fn to_rad(&self) -> Rad { self.clone() } -} -impl ToRad for Deg { - #[inline] - fn to_rad(&self) -> Rad { - rad(self.s * cast(f64::consts::PI / 180.0).unwrap()) + fn from(r: Rad) -> Deg { + deg(r.s * cast(180.0 / f64::consts::PI).unwrap()) } } -impl ToDeg for Rad { +impl From> for Rad where S: BaseFloat { #[inline] - fn to_deg(&self) -> Deg { - deg(self.s * cast(180.0 / f64::consts::PI).unwrap()) + fn from(d: Deg) -> Rad { + rad(d.s * cast(f64::consts::PI / 180.0).unwrap()) } } -impl ToDeg for Deg { - #[inline] - fn to_deg(&self) -> Deg { self.clone() } -} /// Private utility functions for converting to/from scalars trait ScalarConv { @@ -102,8 +82,8 @@ pub trait Angle + PartialEq + PartialOrd + ApproxEq + Neg -+ ToRad -+ ToDeg ++ Into> ++ Into> + ScalarConv + fmt::Debug { @@ -279,13 +259,13 @@ impl One for Deg { const PI_2: f64 = f64::consts::PI * 2f64; impl Angle for Rad { - #[inline] fn from>(theta: A) -> Rad { theta.to_rad() } + #[inline] fn from>(theta: A) -> Rad { theta.into() } #[inline] fn full_turn() -> Rad { rad(cast(PI_2).unwrap()) } } impl Angle for Deg { - #[inline] fn from>(theta: A) -> Deg { theta.to_deg() } + #[inline] fn from>(theta: A) -> Deg { theta.into() } #[inline] fn full_turn() -> Deg { deg(cast(360i32).unwrap()) } } diff --git a/src/projection.rs b/src/projection.rs index 6835ddf..90a9f31 100644 --- a/src/projection.rs +++ b/src/projection.rs @@ -16,7 +16,7 @@ use rust_num::{zero, one}; use rust_num::traits::cast; -use angle::{Angle, tan, cot}; +use angle::{Angle, Rad, tan, cot}; use frustum::Frustum; use matrix::{Matrix4, ToMatrix4}; use num::BaseFloat; @@ -81,7 +81,8 @@ pub struct PerspectiveFov { impl> PerspectiveFov { pub fn to_perspective(&self) -> Perspective { let angle = self.fovy.div_s(cast(2i8).unwrap()); - let ymax = self.near * tan(angle.to_rad()); + let angle: Rad<_> = angle.into(); + let ymax = self.near * tan(angle); let xmax = ymax * self.aspect; Perspective { @@ -113,7 +114,8 @@ impl> ToMatrix4 for PerspectiveFov { assert!(self.far > zero(), "The far plane distance cannot be below zero, found: {:?}", self.far); assert!(self.far > self.near, "The far plane cannot be closer than the near plane, found: far: {:?}, near: {:?}", self.far, self.near); - let f = cot(self.fovy.div_s(cast(2i8).unwrap()).to_rad()); + let f: Rad<_> = self.fovy.div_s(cast(2i8).unwrap()).into(); + let f = cot(f); let two: S = cast(2i8).unwrap(); let c0r0 = f / self.aspect; diff --git a/tests/angle.rs b/tests/angle.rs index e8a1224..02bf6f0 100644 --- a/tests/angle.rs +++ b/tests/angle.rs @@ -16,16 +16,25 @@ extern crate cgmath; use cgmath::{Angle, Rad, Deg, rad, deg}; -use cgmath::{ToRad, ToDeg}; use cgmath::ApproxEq; #[test] fn conv() { - assert!(deg(-5.0f64).to_rad().to_deg().approx_eq(°(-5.0f64))); - assert!(deg(30.0f64).to_rad().to_deg().approx_eq(°(30.0f64))); + let angle: Rad<_> = deg(-5.0f64).into(); + let angle: Deg<_> = angle.into(); + assert!(angle.approx_eq(°(-5.0f64))); - assert!(rad(-5.0f64).to_deg().to_rad().approx_eq(&rad(-5.0f64))); - assert!(rad(30.0f64).to_deg().to_rad().approx_eq(&rad(30.0f64))); + let angle: Rad<_> = deg(30.0f64).into(); + let angle: Deg<_> = angle.into(); + assert!(angle.approx_eq(°(30.0f64))); + + let angle: Deg<_> = rad(-5.0f64).into(); + let angle: Rad<_> = angle.into(); + assert!(angle.approx_eq(&rad(-5.0f64))); + + let angle: Deg<_> = rad(30.0f64).into(); + let angle: Rad<_> = angle.into(); + assert!(angle.approx_eq(&rad(30.0f64))); } #[test] diff --git a/tests/rotation.rs b/tests/rotation.rs index c44df49..5acc26e 100644 --- a/tests/rotation.rs +++ b/tests/rotation.rs @@ -21,12 +21,12 @@ mod rotation { use super::cgmath::*; pub fn a2>() -> R { - Rotation2::from_angle(deg(30.0).to_rad()) + Rotation2::from_angle(deg(30.0).into()) } pub fn a3>() -> R { let axis = Vector3::new(1.0, 1.0, 0.0).normalize(); - Rotation3::from_axis_angle(&axis, deg(30.0).to_rad()) + Rotation3::from_axis_angle(&axis, deg(30.0).into()) } }