Replace ToDeg/ToRad by From/Into

This commit is contained in:
Pierre Krieger 2015-05-06 10:10:26 +02:00
parent 84fa175901
commit e5822e3864
4 changed files with 31 additions and 40 deletions

View file

@ -40,39 +40,19 @@ pub struct Deg<S> { pub s: S }
/// Create a new angle, in degrees /// Create a new angle, in degrees
#[inline] pub fn deg<S: BaseFloat>(s: S) -> Deg<S> { Deg { s: s } } #[inline] pub fn deg<S: BaseFloat>(s: S) -> Deg<S> { Deg { s: s } }
/// Represents types that can be converted to radians. impl<S> From<Rad<S>> for Deg<S> where S: BaseFloat {
pub trait ToRad<S: BaseFloat> {
/// Convert this value to radians.
fn to_rad(&self) -> Rad<S>;
}
/// Represents types that can be converted to degrees.
pub trait ToDeg<S: BaseFloat> {
/// Convert this value to degrees.
fn to_deg(&self) -> Deg<S>;
}
impl<S: BaseFloat> ToRad<S> for Rad<S> {
#[inline] #[inline]
fn to_rad(&self) -> Rad<S> { self.clone() } fn from(r: Rad<S>) -> Deg<S> {
} deg(r.s * cast(180.0 / f64::consts::PI).unwrap())
impl<S: BaseFloat> ToRad<S> for Deg<S> {
#[inline]
fn to_rad(&self) -> Rad<S> {
rad(self.s * cast(f64::consts::PI / 180.0).unwrap())
} }
} }
impl<S: BaseFloat> ToDeg<S> for Rad<S> { impl<S> From<Deg<S>> for Rad<S> where S: BaseFloat {
#[inline] #[inline]
fn to_deg(&self) -> Deg<S> { fn from(d: Deg<S>) -> Rad<S> {
deg(self.s * cast(180.0 / f64::consts::PI).unwrap()) rad(d.s * cast(f64::consts::PI / 180.0).unwrap())
} }
} }
impl<S: BaseFloat> ToDeg<S> for Deg<S> {
#[inline]
fn to_deg(&self) -> Deg<S> { self.clone() }
}
/// Private utility functions for converting to/from scalars /// Private utility functions for converting to/from scalars
trait ScalarConv<S> { trait ScalarConv<S> {
@ -102,8 +82,8 @@ pub trait Angle
+ PartialEq + PartialOrd + PartialEq + PartialOrd
+ ApproxEq<S> + ApproxEq<S>
+ Neg<Output=Self> + Neg<Output=Self>
+ ToRad<S> + Into<Rad<S>>
+ ToDeg<S> + Into<Deg<S>>
+ ScalarConv<S> + ScalarConv<S>
+ fmt::Debug + fmt::Debug
{ {
@ -279,13 +259,13 @@ impl<S: BaseFloat> One for Deg<S> {
const PI_2: f64 = f64::consts::PI * 2f64; const PI_2: f64 = f64::consts::PI * 2f64;
impl<S: BaseFloat> impl<S: BaseFloat>
Angle<S> for Rad<S> { Angle<S> for Rad<S> {
#[inline] fn from<A: Angle<S>>(theta: A) -> Rad<S> { theta.to_rad() } #[inline] fn from<A: Angle<S>>(theta: A) -> Rad<S> { theta.into() }
#[inline] fn full_turn() -> Rad<S> { rad(cast(PI_2).unwrap()) } #[inline] fn full_turn() -> Rad<S> { rad(cast(PI_2).unwrap()) }
} }
impl<S: BaseFloat> impl<S: BaseFloat>
Angle<S> for Deg<S> { Angle<S> for Deg<S> {
#[inline] fn from<A: Angle<S>>(theta: A) -> Deg<S> { theta.to_deg() } #[inline] fn from<A: Angle<S>>(theta: A) -> Deg<S> { theta.into() }
#[inline] fn full_turn() -> Deg<S> { deg(cast(360i32).unwrap()) } #[inline] fn full_turn() -> Deg<S> { deg(cast(360i32).unwrap()) }
} }

View file

@ -16,7 +16,7 @@
use rust_num::{zero, one}; use rust_num::{zero, one};
use rust_num::traits::cast; use rust_num::traits::cast;
use angle::{Angle, tan, cot}; use angle::{Angle, Rad, tan, cot};
use frustum::Frustum; use frustum::Frustum;
use matrix::{Matrix4, ToMatrix4}; use matrix::{Matrix4, ToMatrix4};
use num::BaseFloat; use num::BaseFloat;
@ -81,7 +81,8 @@ pub struct PerspectiveFov<S, A> {
impl<S: BaseFloat, A: Angle<S>> PerspectiveFov<S, A> { impl<S: BaseFloat, A: Angle<S>> PerspectiveFov<S, A> {
pub fn to_perspective(&self) -> Perspective<S> { pub fn to_perspective(&self) -> Perspective<S> {
let angle = self.fovy.div_s(cast(2i8).unwrap()); 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; let xmax = ymax * self.aspect;
Perspective { Perspective {
@ -113,7 +114,8 @@ impl<S: BaseFloat, A: Angle<S>> ToMatrix4<S> for PerspectiveFov<S, A> {
assert!(self.far > zero(), "The far plane distance cannot be below zero, found: {:?}", self.far); 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); 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 two: S = cast(2i8).unwrap();
let c0r0 = f / self.aspect; let c0r0 = f / self.aspect;

View file

@ -16,16 +16,25 @@
extern crate cgmath; extern crate cgmath;
use cgmath::{Angle, Rad, Deg, rad, deg}; use cgmath::{Angle, Rad, Deg, rad, deg};
use cgmath::{ToRad, ToDeg};
use cgmath::ApproxEq; use cgmath::ApproxEq;
#[test] #[test]
fn conv() { fn conv() {
assert!(deg(-5.0f64).to_rad().to_deg().approx_eq(&deg(-5.0f64))); let angle: Rad<_> = deg(-5.0f64).into();
assert!(deg(30.0f64).to_rad().to_deg().approx_eq(&deg(30.0f64))); let angle: Deg<_> = angle.into();
assert!(angle.approx_eq(&deg(-5.0f64)));
assert!(rad(-5.0f64).to_deg().to_rad().approx_eq(&rad(-5.0f64))); let angle: Rad<_> = deg(30.0f64).into();
assert!(rad(30.0f64).to_deg().to_rad().approx_eq(&rad(30.0f64))); let angle: Deg<_> = angle.into();
assert!(angle.approx_eq(&deg(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] #[test]

View file

@ -21,12 +21,12 @@ mod rotation {
use super::cgmath::*; use super::cgmath::*;
pub fn a2<R: Rotation2<f64>>() -> R { pub fn a2<R: Rotation2<f64>>() -> R {
Rotation2::from_angle(deg(30.0).to_rad()) Rotation2::from_angle(deg(30.0).into())
} }
pub fn a3<R: Rotation3<f64>>() -> R { pub fn a3<R: Rotation3<f64>>() -> R {
let axis = Vector3::new(1.0, 1.0, 0.0).normalize(); 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())
} }
} }