@@ -18,51 +18,51 @@ use trig::Trig;
1818use std:: fmt;
1919
2020define_matrix ! {
21- /// A 2d transform stored as a 2 by 3 matrix in row-major order in memory,
21+ /// A 2d transform stored as a 2 by 3 transform in row-major order in memory,
2222 /// useful to represent 2d transformations.
2323 ///
2424 /// Matrices can be parametrized over the source and destination units, to describe a
2525 /// transformation from a space to another.
26- /// For example, `TypedMatrix2D <f32, WordSpace, ScreenSpace>::transform_point4d`
26+ /// For example, `TypedTransform2D <f32, WordSpace, ScreenSpace>::transform_point4d`
2727 /// takes a `TypedPoint2D<f32, WordSpace>` and returns a `TypedPoint2D<f32, ScreenSpace>`.
2828 ///
2929 /// Matrices expose a set of convenience methods for pre- and post-transformations.
3030 /// A pre-transformation corresponds to adding an operation that is applied before
3131 /// the rest of the transformation, while a post-transformation adds an operation
3232 /// that is applied after.
33- pub struct TypedMatrix2D <T , Src , Dst > {
33+ pub struct TypedTransform2D <T , Src , Dst > {
3434 pub m11: T , pub m12: T ,
3535 pub m21: T , pub m22: T ,
3636 pub m31: T , pub m32: T ,
3737 }
3838}
3939
40- /// The default 2d matrix type with no units.
41- pub type Matrix2D < T > = TypedMatrix2D < T , UnknownUnit , UnknownUnit > ;
40+ /// The default 2d transform type with no units.
41+ pub type Transform2D < T > = TypedTransform2D < T , UnknownUnit , UnknownUnit > ;
4242
43- impl < T : Copy , Src , Dst > TypedMatrix2D < T , Src , Dst > {
44- /// Create a matrix specifying its components in row-major order.
45- pub fn row_major ( m11 : T , m12 : T , m21 : T , m22 : T , m31 : T , m32 : T ) -> TypedMatrix2D < T , Src , Dst > {
46- TypedMatrix2D {
43+ impl < T : Copy , Src , Dst > TypedTransform2D < T , Src , Dst > {
44+ /// Create a transform specifying its components in row-major order.
45+ pub fn row_major ( m11 : T , m12 : T , m21 : T , m22 : T , m31 : T , m32 : T ) -> TypedTransform2D < T , Src , Dst > {
46+ TypedTransform2D {
4747 m11 : m11, m12 : m12,
4848 m21 : m21, m22 : m22,
4949 m31 : m31, m32 : m32,
5050 _unit : PhantomData ,
5151 }
5252 }
5353
54- /// Create a matrix specifying its components in column-major order.
55- pub fn column_major ( m11 : T , m21 : T , m31 : T , m12 : T , m22 : T , m32 : T ) -> TypedMatrix2D < T , Src , Dst > {
56- TypedMatrix2D {
54+ /// Create a transform specifying its components in column-major order.
55+ pub fn column_major ( m11 : T , m21 : T , m31 : T , m12 : T , m22 : T , m32 : T ) -> TypedTransform2D < T , Src , Dst > {
56+ TypedTransform2D {
5757 m11 : m11, m12 : m12,
5858 m21 : m21, m22 : m22,
5959 m31 : m31, m32 : m32,
6060 _unit : PhantomData ,
6161 }
6262 }
6363
64- /// Returns an array containing this matrix 's terms in row-major order (the order
65- /// in which the matrix is actually laid out in memory).
64+ /// Returns an array containing this transform 's terms in row-major order (the order
65+ /// in which the transform is actually laid out in memory).
6666 pub fn to_row_major_array ( & self ) -> [ T ; 6 ] {
6767 [
6868 self . m11 , self . m12 ,
@@ -71,7 +71,7 @@ impl<T: Copy, Src, Dst> TypedMatrix2D<T, Src, Dst> {
7171 ]
7272 }
7373
74- /// Returns an array containing this matrix 's terms in column-major order.
74+ /// Returns an array containing this transform 's terms in column-major order.
7575 pub fn to_column_major_array ( & self ) -> [ T ; 6 ] {
7676 [
7777 self . m11 , self . m21 , self . m31 ,
@@ -80,31 +80,31 @@ impl<T: Copy, Src, Dst> TypedMatrix2D<T, Src, Dst> {
8080 }
8181
8282 /// Drop the units, preserving only the numeric value.
83- pub fn to_untyped ( & self ) -> Matrix2D < T > {
84- Matrix2D :: row_major (
83+ pub fn to_untyped ( & self ) -> Transform2D < T > {
84+ Transform2D :: row_major (
8585 self . m11 , self . m12 ,
8686 self . m21 , self . m22 ,
8787 self . m31 , self . m32
8888 )
8989 }
9090
9191 /// Tag a unitless value with units.
92- pub fn from_untyped ( p : & Matrix2D < T > ) -> TypedMatrix2D < T , Src , Dst > {
93- TypedMatrix2D :: row_major (
92+ pub fn from_untyped ( p : & Transform2D < T > ) -> TypedTransform2D < T , Src , Dst > {
93+ TypedTransform2D :: row_major (
9494 p. m11 , p. m12 ,
9595 p. m21 , p. m22 ,
9696 p. m31 , p. m32
9797 )
9898 }
9999}
100100
101- impl < T , Src , Dst > TypedMatrix2D < T , Src , Dst >
101+ impl < T , Src , Dst > TypedTransform2D < T , Src , Dst >
102102where T : Copy +
103103 PartialEq +
104104 One + Zero {
105- pub fn identity ( ) -> TypedMatrix2D < T , Src , Dst > {
105+ pub fn identity ( ) -> TypedTransform2D < T , Src , Dst > {
106106 let ( _0, _1) = ( Zero :: zero ( ) , One :: one ( ) ) ;
107- TypedMatrix2D :: row_major (
107+ TypedTransform2D :: row_major (
108108 _1, _0,
109109 _0, _1,
110110 _0, _0
@@ -115,11 +115,11 @@ where T: Copy +
115115 // while most consumers will probably want some sort of approximate
116116 // equivalence to deal with floating-point errors.
117117 fn is_identity ( & self ) -> bool {
118- * self == TypedMatrix2D :: identity ( )
118+ * self == TypedTransform2D :: identity ( )
119119 }
120120}
121121
122- impl < T , Src , Dst > TypedMatrix2D < T , Src , Dst >
122+ impl < T , Src , Dst > TypedTransform2D < T , Src , Dst >
123123where T : Copy + Clone +
124124 Add < T , Output =T > +
125125 Mul < T , Output =T > +
@@ -131,8 +131,8 @@ where T: Copy + Clone +
131131
132132 /// Returns the multiplication of the two matrices such that mat's transformation
133133 /// applies after self's transformation.
134- pub fn post_mul < NewDst > ( & self , mat : & TypedMatrix2D < T , Dst , NewDst > ) -> TypedMatrix2D < T , Src , NewDst > {
135- TypedMatrix2D :: row_major (
134+ pub fn post_mul < NewDst > ( & self , mat : & TypedTransform2D < T , Dst , NewDst > ) -> TypedTransform2D < T , Src , NewDst > {
135+ TypedTransform2D :: row_major (
136136 self . m11 * mat. m11 + self . m12 * mat. m21 ,
137137 self . m11 * mat. m12 + self . m12 * mat. m22 ,
138138 self . m21 * mat. m11 + self . m22 * mat. m21 ,
@@ -144,85 +144,85 @@ where T: Copy + Clone +
144144
145145 /// Returns the multiplication of the two matrices such that mat's transformation
146146 /// applies before self's transformation.
147- pub fn pre_mul < NewSrc > ( & self , mat : & TypedMatrix2D < T , NewSrc , Src > ) -> TypedMatrix2D < T , NewSrc , Dst > {
147+ pub fn pre_mul < NewSrc > ( & self , mat : & TypedTransform2D < T , NewSrc , Src > ) -> TypedTransform2D < T , NewSrc , Dst > {
148148 mat. post_mul ( self )
149149 }
150150
151- /// Returns a translation matrix .
152- pub fn create_translation ( x : T , y : T ) -> TypedMatrix2D < T , Src , Dst > {
151+ /// Returns a translation transform .
152+ pub fn create_translation ( x : T , y : T ) -> TypedTransform2D < T , Src , Dst > {
153153 let ( _0, _1) : ( T , T ) = ( Zero :: zero ( ) , One :: one ( ) ) ;
154- TypedMatrix2D :: row_major (
154+ TypedTransform2D :: row_major (
155155 _1, _0,
156156 _0, _1,
157157 x, y
158158 )
159159 }
160160
161- /// Applies a translation after self's transformation and returns the resulting matrix .
162- pub fn post_translated ( & self , x : T , y : T ) -> TypedMatrix2D < T , Src , Dst > {
163- self . post_mul ( & TypedMatrix2D :: create_translation ( x, y) )
161+ /// Applies a translation after self's transformation and returns the resulting transform .
162+ pub fn post_translated ( & self , x : T , y : T ) -> TypedTransform2D < T , Src , Dst > {
163+ self . post_mul ( & TypedTransform2D :: create_translation ( x, y) )
164164 }
165165
166- /// Applies a translation before self's transformation and returns the resulting matrix .
167- pub fn pre_translated ( & self , x : T , y : T ) -> TypedMatrix2D < T , Src , Dst > {
168- self . pre_mul ( & TypedMatrix2D :: create_translation ( x, y) )
166+ /// Applies a translation before self's transformation and returns the resulting transform .
167+ pub fn pre_translated ( & self , x : T , y : T ) -> TypedTransform2D < T , Src , Dst > {
168+ self . pre_mul ( & TypedTransform2D :: create_translation ( x, y) )
169169 }
170170
171- /// Returns a scale matrix .
172- pub fn create_scale ( x : T , y : T ) -> TypedMatrix2D < T , Src , Dst > {
171+ /// Returns a scale transform .
172+ pub fn create_scale ( x : T , y : T ) -> TypedTransform2D < T , Src , Dst > {
173173 let _0 = Zero :: zero ( ) ;
174- TypedMatrix2D :: row_major (
174+ TypedTransform2D :: row_major (
175175 x, _0,
176176 _0, y,
177177 _0, _0
178178 )
179179 }
180180
181- /// Applies a scale after self's transformation and returns the resulting matrix .
182- pub fn post_scaled ( & self , x : T , y : T ) -> TypedMatrix2D < T , Src , Dst > {
183- self . post_mul ( & TypedMatrix2D :: create_scale ( x, y) )
181+ /// Applies a scale after self's transformation and returns the resulting transform .
182+ pub fn post_scaled ( & self , x : T , y : T ) -> TypedTransform2D < T , Src , Dst > {
183+ self . post_mul ( & TypedTransform2D :: create_scale ( x, y) )
184184 }
185185
186- /// Applies a scale before self's transformation and returns the resulting matrix .
187- pub fn pre_scaled ( & self , x : T , y : T ) -> TypedMatrix2D < T , Src , Dst > {
188- TypedMatrix2D :: row_major (
186+ /// Applies a scale before self's transformation and returns the resulting transform .
187+ pub fn pre_scaled ( & self , x : T , y : T ) -> TypedTransform2D < T , Src , Dst > {
188+ TypedTransform2D :: row_major (
189189 self . m11 * x, self . m12 ,
190190 self . m21 , self . m22 * y,
191191 self . m31 , self . m32
192192 )
193193 }
194194
195- /// Returns a rotation matrix .
196- pub fn create_rotation ( theta : Radians < T > ) -> TypedMatrix2D < T , Src , Dst > {
195+ /// Returns a rotation transform .
196+ pub fn create_rotation ( theta : Radians < T > ) -> TypedTransform2D < T , Src , Dst > {
197197 let _0 = Zero :: zero ( ) ;
198198 let cos = theta. get ( ) . cos ( ) ;
199199 let sin = theta. get ( ) . sin ( ) ;
200- TypedMatrix2D :: row_major (
200+ TypedTransform2D :: row_major (
201201 cos, _0 - sin,
202202 sin, cos,
203203 _0, _0
204204 )
205205 }
206206
207- /// Applies a rotation after self's transformation and returns the resulting matrix .
208- pub fn post_rotated ( & self , theta : Radians < T > ) -> TypedMatrix2D < T , Src , Dst > {
209- self . post_mul ( & TypedMatrix2D :: create_rotation ( theta) )
207+ /// Applies a rotation after self's transformation and returns the resulting transform .
208+ pub fn post_rotated ( & self , theta : Radians < T > ) -> TypedTransform2D < T , Src , Dst > {
209+ self . post_mul ( & TypedTransform2D :: create_rotation ( theta) )
210210 }
211211
212- /// Applies a rotation after self's transformation and returns the resulting matrix .
213- pub fn pre_rotated ( & self , theta : Radians < T > ) -> TypedMatrix2D < T , Src , Dst > {
214- self . pre_mul ( & TypedMatrix2D :: create_rotation ( theta) )
212+ /// Applies a rotation after self's transformation and returns the resulting transform .
213+ pub fn pre_rotated ( & self , theta : Radians < T > ) -> TypedTransform2D < T , Src , Dst > {
214+ self . pre_mul ( & TypedTransform2D :: create_rotation ( theta) )
215215 }
216216
217- /// Returns the given point transformed by this matrix .
217+ /// Returns the given point transformed by this transform .
218218 #[ inline]
219219 pub fn transform_point ( & self , point : & TypedPoint2D < T , Src > ) -> TypedPoint2D < T , Dst > {
220220 TypedPoint2D :: new ( point. x * self . m11 + point. y * self . m21 + self . m31 ,
221221 point. x * self . m12 + point. y * self . m22 + self . m32 )
222222 }
223223
224224 /// Returns a rectangle that encompasses the result of transforming the given rectangle by this
225- /// matrix .
225+ /// transform .
226226 #[ inline]
227227 pub fn transform_rect ( & self , rect : & TypedRect < T , Src > ) -> TypedRect < T , Dst > {
228228 TypedRect :: from_points ( & [
@@ -233,13 +233,13 @@ where T: Copy + Clone +
233233 ] )
234234 }
235235
236- /// Computes and returns the determinant of this matrix .
236+ /// Computes and returns the determinant of this transform .
237237 pub fn determinant ( & self ) -> T {
238238 self . m11 * self . m22 - self . m12 * self . m21
239239 }
240240
241- /// Returns the inverse matrix if possible.
242- pub fn inverse ( & self ) -> Option < TypedMatrix2D < T , Dst , Src > > {
241+ /// Returns the inverse transform if possible.
242+ pub fn inverse ( & self ) -> Option < TypedTransform2D < T , Dst , Src > > {
243243 let det = self . determinant ( ) ;
244244
245245 let _0: T = Zero :: zero ( ) ;
@@ -250,7 +250,7 @@ where T: Copy + Clone +
250250 }
251251
252252 let inv_det = _1 / det;
253- Some ( TypedMatrix2D :: row_major (
253+ Some ( TypedTransform2D :: row_major (
254254 inv_det * self . m22 ,
255255 inv_det * ( _0 - self . m12 ) ,
256256 inv_det * ( _0 - self . m21 ) ,
@@ -260,36 +260,36 @@ where T: Copy + Clone +
260260 ) )
261261 }
262262
263- /// Returns the same matrix with a different destination unit.
263+ /// Returns the same transform with a different destination unit.
264264 #[ inline]
265- pub fn with_destination < NewDst > ( & self ) -> TypedMatrix2D < T , Src , NewDst > {
266- TypedMatrix2D :: row_major (
265+ pub fn with_destination < NewDst > ( & self ) -> TypedTransform2D < T , Src , NewDst > {
266+ TypedTransform2D :: row_major (
267267 self . m11 , self . m12 ,
268268 self . m21 , self . m22 ,
269269 self . m31 , self . m32 ,
270270 )
271271 }
272272
273- /// Returns the same matrix with a different source unit.
273+ /// Returns the same transform with a different source unit.
274274 #[ inline]
275- pub fn with_source < NewSrc > ( & self ) -> TypedMatrix2D < T , NewSrc , Dst > {
276- TypedMatrix2D :: row_major (
275+ pub fn with_source < NewSrc > ( & self ) -> TypedTransform2D < T , NewSrc , Dst > {
276+ TypedTransform2D :: row_major (
277277 self . m11 , self . m12 ,
278278 self . m21 , self . m22 ,
279279 self . m31 , self . m32 ,
280280 )
281281 }
282282}
283283
284- impl < T : ApproxEq < T > , Src , Dst > TypedMatrix2D < T , Src , Dst > {
284+ impl < T : ApproxEq < T > , Src , Dst > TypedTransform2D < T , Src , Dst > {
285285 pub fn approx_eq ( & self , other : & Self ) -> bool {
286286 self . m11 . approx_eq ( & other. m11 ) && self . m12 . approx_eq ( & other. m12 ) &&
287287 self . m21 . approx_eq ( & other. m21 ) && self . m22 . approx_eq ( & other. m22 ) &&
288288 self . m31 . approx_eq ( & other. m31 ) && self . m32 . approx_eq ( & other. m32 )
289289 }
290290}
291291
292- impl < T : Copy + fmt:: Debug , Src , Dst > fmt:: Debug for TypedMatrix2D < T , Src , Dst >
292+ impl < T : Copy + fmt:: Debug , Src , Dst > fmt:: Debug for TypedTransform2D < T , Src , Dst >
293293where T : Copy + fmt:: Debug +
294294 PartialEq +
295295 One + Zero {
@@ -311,7 +311,7 @@ mod test {
311311
312312 use std:: f32:: consts:: FRAC_PI_2 ;
313313
314- type Mat = Matrix2D < f32 > ;
314+ type Mat = Transform2D < f32 > ;
315315
316316 fn rad ( v : f32 ) -> Radians < f32 > { Radians :: new ( v) }
317317
@@ -396,8 +396,8 @@ mod test {
396396
397397 #[ test]
398398 pub fn test_pre_post ( ) {
399- let m1 = Matrix2D :: identity ( ) . post_scaled ( 1.0 , 2.0 ) . post_translated ( 1.0 , 2.0 ) ;
400- let m2 = Matrix2D :: identity ( ) . pre_translated ( 1.0 , 2.0 ) . pre_scaled ( 1.0 , 2.0 ) ;
399+ let m1 = Transform2D :: identity ( ) . post_scaled ( 1.0 , 2.0 ) . post_translated ( 1.0 , 2.0 ) ;
400+ let m2 = Transform2D :: identity ( ) . pre_translated ( 1.0 , 2.0 ) . pre_scaled ( 1.0 , 2.0 ) ;
401401 assert ! ( m1. approx_eq( & m2) ) ;
402402
403403 let r = Mat :: create_rotation ( rad ( FRAC_PI_2 ) ) ;
@@ -417,13 +417,13 @@ mod test {
417417 #[ test]
418418 fn test_size_of ( ) {
419419 use std:: mem:: size_of;
420- assert_eq ! ( size_of:: <Matrix2D <f32 >>( ) , 6 * size_of:: <f32 >( ) ) ;
421- assert_eq ! ( size_of:: <Matrix2D <f64 >>( ) , 6 * size_of:: <f64 >( ) ) ;
420+ assert_eq ! ( size_of:: <Transform2D <f32 >>( ) , 6 * size_of:: <f32 >( ) ) ;
421+ assert_eq ! ( size_of:: <Transform2D <f64 >>( ) , 6 * size_of:: <f64 >( ) ) ;
422422 }
423423
424424 #[ test]
425425 pub fn test_is_identity ( ) {
426- let m1 = Matrix2D :: identity ( ) ;
426+ let m1 = Transform2D :: identity ( ) ;
427427 assert ! ( m1. is_identity( ) ) ;
428428 let m2 = m1. post_translated ( 0.1 , 0.0 ) ;
429429 assert ! ( !m2. is_identity( ) ) ;
0 commit comments