Skip to content

Commit 6e560fb

Browse files
committed
Merge pull request #216 from tomaka/into
Replace most To* traits by Into
2 parents 84fa175 + 1a6dc52 commit 6e560fb

File tree

9 files changed

+191
-238
lines changed

9 files changed

+191
-238
lines changed

src/angle.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -40,39 +40,19 @@ pub struct Deg<S> { pub s: S }
4040
/// Create a new angle, in degrees
4141
#[inline] pub fn deg<S: BaseFloat>(s: S) -> Deg<S> { Deg { s: s } }
4242

43-
/// Represents types that can be converted to radians.
44-
pub trait ToRad<S: BaseFloat> {
45-
/// Convert this value to radians.
46-
fn to_rad(&self) -> Rad<S>;
47-
}
48-
49-
/// Represents types that can be converted to degrees.
50-
pub trait ToDeg<S: BaseFloat> {
51-
/// Convert this value to degrees.
52-
fn to_deg(&self) -> Deg<S>;
53-
}
54-
55-
impl<S: BaseFloat> ToRad<S> for Rad<S> {
43+
impl<S> From<Rad<S>> for Deg<S> where S: BaseFloat {
5644
#[inline]
57-
fn to_rad(&self) -> Rad<S> { self.clone() }
58-
}
59-
impl<S: BaseFloat> ToRad<S> for Deg<S> {
60-
#[inline]
61-
fn to_rad(&self) -> Rad<S> {
62-
rad(self.s * cast(f64::consts::PI / 180.0).unwrap())
45+
fn from(r: Rad<S>) -> Deg<S> {
46+
deg(r.s * cast(180.0 / f64::consts::PI).unwrap())
6347
}
6448
}
6549

66-
impl<S: BaseFloat> ToDeg<S> for Rad<S> {
50+
impl<S> From<Deg<S>> for Rad<S> where S: BaseFloat {
6751
#[inline]
68-
fn to_deg(&self) -> Deg<S> {
69-
deg(self.s * cast(180.0 / f64::consts::PI).unwrap())
52+
fn from(d: Deg<S>) -> Rad<S> {
53+
rad(d.s * cast(f64::consts::PI / 180.0).unwrap())
7054
}
7155
}
72-
impl<S: BaseFloat> ToDeg<S> for Deg<S> {
73-
#[inline]
74-
fn to_deg(&self) -> Deg<S> { self.clone() }
75-
}
7656

7757
/// Private utility functions for converting to/from scalars
7858
trait ScalarConv<S> {
@@ -102,8 +82,8 @@ pub trait Angle
10282
+ PartialEq + PartialOrd
10383
+ ApproxEq<S>
10484
+ Neg<Output=Self>
105-
+ ToRad<S>
106-
+ ToDeg<S>
85+
+ Into<Rad<S>>
86+
+ Into<Deg<S>>
10787
+ ScalarConv<S>
10888
+ fmt::Debug
10989
{
@@ -279,13 +259,13 @@ impl<S: BaseFloat> One for Deg<S> {
279259
const PI_2: f64 = f64::consts::PI * 2f64;
280260
impl<S: BaseFloat>
281261
Angle<S> for Rad<S> {
282-
#[inline] fn from<A: Angle<S>>(theta: A) -> Rad<S> { theta.to_rad() }
262+
#[inline] fn from<A: Angle<S>>(theta: A) -> Rad<S> { theta.into() }
283263
#[inline] fn full_turn() -> Rad<S> { rad(cast(PI_2).unwrap()) }
284264
}
285265

286266
impl<S: BaseFloat>
287267
Angle<S> for Deg<S> {
288-
#[inline] fn from<A: Angle<S>>(theta: A) -> Deg<S> { theta.to_deg() }
268+
#[inline] fn from<A: Angle<S>>(theta: A) -> Deg<S> { theta.into() }
289269
#[inline] fn full_turn() -> Deg<S> { deg(cast(360i32).unwrap()) }
290270
}
291271

src/matrix.rs

Lines changed: 38 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use approx::ApproxEq;
2929
use array::{Array1, Array2, FixedArray};
3030
use num::{BaseFloat, BaseNum};
3131
use point::{Point, Point3};
32-
use quaternion::{Quaternion, ToQuaternion};
32+
use quaternion::Quaternion;
3333
use vector::{Vector, EuclideanVector};
3434
use vector::{Vector2, Vector3, Vector4};
3535

@@ -1326,94 +1326,76 @@ impl<S: BaseFloat> ApproxEq<S> for Matrix4<S> {
13261326

13271327
// Conversion traits
13281328

1329-
/// Represents types which can be converted to a Matrix2
1330-
pub trait ToMatrix2<S: BaseNum> {
1331-
/// Convert this value to a Matrix2
1332-
fn to_matrix2(&self) -> Matrix2<S>;
1333-
}
1334-
1335-
/// Represents types which can be converted to a Matrix3
1336-
pub trait ToMatrix3<S: BaseNum> {
1337-
/// Convert this value to a Matrix3
1338-
fn to_matrix3(&self) -> Matrix3<S>;
1339-
}
1340-
1341-
/// Represents types which can be converted to a Matrix4
1342-
pub trait ToMatrix4<S: BaseNum> {
1343-
/// Convert this value to a Matrix4
1344-
fn to_matrix4(&self) -> Matrix4<S>;
1345-
}
1346-
1347-
impl<S: BaseFloat> ToMatrix3<S> for Matrix2<S> {
1329+
impl<S: BaseFloat> From<Matrix2<S>> for Matrix3<S> {
13481330
/// Clone the elements of a 2-dimensional matrix into the top-left corner
13491331
/// of a 3-dimensional identity matrix.
1350-
fn to_matrix3(&self) -> Matrix3<S> {
1351-
Matrix3::new(self[0][0], self[0][1], zero(),
1352-
self[1][0], self[1][1], zero(),
1353-
zero(), zero(), one())
1332+
fn from(m: Matrix2<S>) -> Matrix3<S> {
1333+
Matrix3::new(m[0][0], m[0][1], zero(),
1334+
m[1][0], m[1][1], zero(),
1335+
zero(), zero(), one())
13541336
}
13551337
}
13561338

1357-
impl<S: BaseFloat> ToMatrix4<S> for Matrix2<S> {
1339+
impl<S: BaseFloat> From<Matrix2<S>> for Matrix4<S> {
13581340
/// Clone the elements of a 2-dimensional matrix into the top-left corner
13591341
/// of a 4-dimensional identity matrix.
1360-
fn to_matrix4(&self) -> Matrix4<S> {
1361-
Matrix4::new(self[0][0], self[0][1], zero(), zero(),
1362-
self[1][0], self[1][1], zero(), zero(),
1363-
zero(), zero(), one(), zero(),
1364-
zero(), zero(), zero(), one())
1342+
fn from(m: Matrix2<S>) -> Matrix4<S> {
1343+
Matrix4::new(m[0][0], m[0][1], zero(), zero(),
1344+
m[1][0], m[1][1], zero(), zero(),
1345+
zero(), zero(), one(), zero(),
1346+
zero(), zero(), zero(), one())
13651347
}
13661348
}
13671349

1368-
impl<S: BaseFloat> ToMatrix4<S> for Matrix3<S> {
1350+
impl<S: BaseFloat> From<Matrix3<S>> for Matrix4<S> {
13691351
/// Clone the elements of a 3-dimensional matrix into the top-left corner
13701352
/// of a 4-dimensional identity matrix.
1371-
fn to_matrix4(&self) -> Matrix4<S> {
1372-
Matrix4::new(self[0][0], self[0][1], self[0][2], zero(),
1373-
self[1][0], self[1][1], self[1][2], zero(),
1374-
self[2][0], self[2][1], self[2][2], zero(),
1375-
zero(), zero(), zero(), one())
1353+
fn from(m: Matrix3<S>) -> Matrix4<S> {
1354+
Matrix4::new(m[0][0], m[0][1], m[0][2], zero(),
1355+
m[1][0], m[1][1], m[1][2], zero(),
1356+
m[2][0], m[2][1], m[2][2], zero(),
1357+
zero(), zero(), zero(), one())
13761358
}
13771359
}
13781360

1379-
impl<S: BaseFloat> ToQuaternion<S> for Matrix3<S> {
1361+
impl<S: BaseFloat> From<Matrix3<S>> for Quaternion<S> {
13801362
/// Convert the matrix to a quaternion
1381-
fn to_quaternion(&self) -> Quaternion<S> {
1363+
fn from(mat: Matrix3<S>) -> Quaternion<S> {
13821364
// http://www.cs.ucr.edu/~vbz/resources/quatut.pdf
1383-
let trace = self.trace();
1365+
let trace = mat.trace();
13841366
let half: S = cast(0.5f64).unwrap();
13851367

13861368
if trace >= zero::<S>() {
13871369
let s = (one::<S>() + trace).sqrt();
13881370
let w = half * s;
13891371
let s = half / s;
1390-
let x = (self[1][2] - self[2][1]) * s;
1391-
let y = (self[2][0] - self[0][2]) * s;
1392-
let z = (self[0][1] - self[1][0]) * s;
1372+
let x = (mat[1][2] - mat[2][1]) * s;
1373+
let y = (mat[2][0] - mat[0][2]) * s;
1374+
let z = (mat[0][1] - mat[1][0]) * s;
13931375
Quaternion::new(w, x, y, z)
1394-
} else if (self[0][0] > self[1][1]) && (self[0][0] > self[2][2]) {
1395-
let s = (half + (self[0][0] - self[1][1] - self[2][2])).sqrt();
1376+
} else if (mat[0][0] > mat[1][1]) && (mat[0][0] > mat[2][2]) {
1377+
let s = (half + (mat[0][0] - mat[1][1] - mat[2][2])).sqrt();
13961378
let w = half * s;
13971379
let s = half / s;
1398-
let x = (self[0][1] - self[1][0]) * s;
1399-
let y = (self[2][0] - self[0][2]) * s;
1400-
let z = (self[1][2] - self[2][1]) * s;
1380+
let x = (mat[0][1] - mat[1][0]) * s;
1381+
let y = (mat[2][0] - mat[0][2]) * s;
1382+
let z = (mat[1][2] - mat[2][1]) * s;
14011383
Quaternion::new(w, x, y, z)
1402-
} else if self[1][1] > self[2][2] {
1403-
let s = (half + (self[1][1] - self[0][0] - self[2][2])).sqrt();
1384+
} else if mat[1][1] > mat[2][2] {
1385+
let s = (half + (mat[1][1] - mat[0][0] - mat[2][2])).sqrt();
14041386
let w = half * s;
14051387
let s = half / s;
1406-
let x = (self[0][1] - self[1][0]) * s;
1407-
let y = (self[1][2] - self[2][1]) * s;
1408-
let z = (self[2][0] - self[0][2]) * s;
1388+
let x = (mat[0][1] - mat[1][0]) * s;
1389+
let y = (mat[1][2] - mat[2][1]) * s;
1390+
let z = (mat[2][0] - mat[0][2]) * s;
14091391
Quaternion::new(w, x, y, z)
14101392
} else {
1411-
let s = (half + (self[2][2] - self[0][0] - self[1][1])).sqrt();
1393+
let s = (half + (mat[2][2] - mat[0][0] - mat[1][1])).sqrt();
14121394
let w = half * s;
14131395
let s = half / s;
1414-
let x = (self[2][0] - self[0][2]) * s;
1415-
let y = (self[1][2] - self[2][1]) * s;
1416-
let z = (self[0][1] - self[1][0]) * s;
1396+
let x = (mat[2][0] - mat[0][2]) * s;
1397+
let y = (mat[1][2] - mat[2][1]) * s;
1398+
let z = (mat[0][1] - mat[1][0]) * s;
14171399
Quaternion::new(w, x, y, z)
14181400
}
14191401
}

0 commit comments

Comments
 (0)