]>
Dogcows Code - chaz/yoink/blob - src/Moof/cml/mathlib/matrix_translation.h
1 /* -*- C++ -*- ------------------------------------------------------------
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
8 *-----------------------------------------------------------------------*/
13 #ifndef matrix_translation_h
14 #define matrix_translation_h
16 #include <cml/mathlib/checking.h>
18 /* Functions for getting and setting the translation of a 3D or 2D affine
24 //////////////////////////////////////////////////////////////////////////////
25 // Functions for setting the translation of a 3D or 2D affine transform matrix
26 //////////////////////////////////////////////////////////////////////////////
28 /** Set the translation of a 3D affine transform */
29 template < typename E
, class A
, class B
, class L
> void
30 matrix_set_translation(matrix
<E
,A
,B
,L
>& m
, E x
, E y
, E z
)
33 detail::CheckMatAffine3D(m
);
35 m
.set_basis_element(3,0,x
);
36 m
.set_basis_element(3,1,y
);
37 m
.set_basis_element(3,2,z
);
40 /** Set the translation of a 3D affine transform with z set to 0 */
41 template < typename E
, class A
, class B
, class L
> void
42 matrix_set_translation(matrix
<E
,A
,B
,L
>& m
, E x
, E y
)
44 typedef matrix
<E
,A
,B
,L
> matrix_type
;
45 typedef typename
matrix_type::value_type value_type
;
47 matrix_set_translation(m
, x
, y
, value_type(0));
50 /** Set the translation of a 3D affine transform from a 3D or 2D vector */
51 template < typename E
, class A
, class B
, class L
, class VecT
> void
52 matrix_set_translation(matrix
<E
,A
,B
,L
>& m
, const VecT
& translation
)
55 detail::CheckVec2Or3(translation
);
57 if (translation
.size() == 3) {
58 matrix_set_translation(
59 m
,translation
[0], translation
[1], translation
[2]);
60 } else { // translation.size() == 2
61 matrix_set_translation(m
, translation
[0], translation
[1]);
65 /** Set the translation of a 2D affine transform */
66 template < typename E
, class A
, class B
, class L
> void
67 matrix_set_translation_2D(matrix
<E
,A
,B
,L
>& m
, E x
, E y
)
70 detail::CheckMatAffine2D(m
);
72 m
.set_basis_element(2,0,x
);
73 m
.set_basis_element(2,1,y
);
76 /** Set the translation of a 2D affine transform from a 2D vector */
77 template < typename E
, class A
, class B
, class L
, class VecT
> void
78 matrix_set_translation_2D(matrix
<E
,A
,B
,L
>& m
, const VecT
& translation
)
81 detail::CheckVec2(translation
);
83 matrix_set_translation_2D(m
, translation
[0], translation
[1]);
86 //////////////////////////////////////////////////////////////////////////////
87 // Functions for getting the translation of a 3D or 2D affine transform matrix
88 //////////////////////////////////////////////////////////////////////////////
90 /** Get the translation of a 3D affine transform */
91 template < class MatT
> vector
< typename
MatT::value_type
, fixed
<3> >
92 matrix_get_translation(const MatT
& m
)
94 typedef typename
MatT::value_type value_type
;
95 typedef vector
< value_type
, fixed
<3> > vector_type
;
98 detail::CheckMatAffine3D(m
);
101 m
.basis_element(3,0),
102 m
.basis_element(3,1),
107 /** Get the translation of a 3D affine transform */
108 template < class MatT
> void
109 matrix_get_translation(
111 typename
MatT::value_type
& t1
,
112 typename
MatT::value_type
& t2
,
113 typename
MatT::value_type
& t3
116 typedef typename
MatT::value_type value_type
;
117 typedef vector
< value_type
, fixed
<3> > vector_type
;
120 detail::CheckMatAffine3D(m
);
122 t1
= m
.basis_element(3,0);
123 t2
= m
.basis_element(3,1);
124 t3
= m
.basis_element(3,2);
127 /** Get the translation of a 2D affine transform */
128 template < class MatT
> vector
< typename
MatT::value_type
, fixed
<2> >
129 matrix_get_translation_2D(const MatT
& m
)
131 typedef typename
MatT::value_type value_type
;
132 typedef vector
< value_type
, fixed
<2> > vector_type
;
135 detail::CheckMatAffine2D(m
);
137 return vector_type(m
.basis_element(2,0), m
.basis_element(2,1));
140 /** Get the translation of a 2D affine transform */
141 template < class MatT
> void
142 matrix_get_translation_2D(
144 typename
MatT::value_type
& t1
,
145 typename
MatT::value_type
& t2
148 typedef typename
MatT::value_type value_type
;
149 typedef vector
< value_type
, fixed
<2> > vector_type
;
152 detail::CheckMatAffine2D(m
);
154 t1
= m
.basis_element(2,0);
155 t2
= m
.basis_element(2,1);
158 //////////////////////////////////////////////////////////////////////////////
159 // Function for getting the translation of a 3D view matrix
160 //////////////////////////////////////////////////////////////////////////////
162 /** Get the translation of a 3D affine transform */
163 template < class MatT
> vector
< typename
MatT::value_type
, fixed
<3> >
164 matrix_get_view_translation(const MatT
& m
)
166 typedef typename
MatT::value_type value_type
;
167 typedef vector
< value_type
, fixed
<3> > vector_type
;
170 matrix_get_basis_vectors(m
,x
,y
,z
);
171 vector_type p
= matrix_get_translation(m
);
172 return vector_type(-dot(p
,x
),-dot(p
,y
),-dot(p
,z
));
This page took 0.042378 seconds and 4 git commands to generate.