]> Dogcows Code - chaz/yoink/blob - src/moof/cml/mathlib/matrix_translation.h
664c5778da27d18b6a2d6419c17b964ba61902f3
[chaz/yoink] / src / moof / cml / mathlib / matrix_translation.h
1 /* -*- C++ -*- ------------------------------------------------------------
2
3 Copyright (c) 2007 Jesse Anders and Demian Nave http://cmldev.net/
4
5 The Configurable Math Library (CML) is distributed under the terms of the
6 Boost Software License, v1.0 (see cml/LICENSE for details).
7
8 *-----------------------------------------------------------------------*/
9 /** @file
10 * @brief
11 */
12
13 #ifndef matrix_translation_h
14 #define matrix_translation_h
15
16 #include <cml/mathlib/checking.h>
17
18 /* Functions for getting and setting the translation of a 3D or 2D affine
19 * transform.
20 */
21
22 namespace cml {
23
24 //////////////////////////////////////////////////////////////////////////////
25 // Functions for setting the translation of a 3D or 2D affine transform matrix
26 //////////////////////////////////////////////////////////////////////////////
27
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)
31 {
32 /* Checking */
33 detail::CheckMatAffine3D(m);
34
35 m.set_basis_element(3,0,x);
36 m.set_basis_element(3,1,y);
37 m.set_basis_element(3,2,z);
38 }
39
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)
43 {
44 typedef matrix<E,A,B,L> matrix_type;
45 typedef typename matrix_type::value_type value_type;
46
47 matrix_set_translation(m, x, y, value_type(0));
48 }
49
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)
53 {
54 /* Checking */
55 detail::CheckVec2Or3(translation);
56
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]);
62 }
63 }
64
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)
68 {
69 /* Checking */
70 detail::CheckMatAffine2D(m);
71
72 m.set_basis_element(2,0,x);
73 m.set_basis_element(2,1,y);
74 }
75
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)
79 {
80 /* Checking */
81 detail::CheckVec2(translation);
82
83 matrix_set_translation_2D(m, translation[0], translation[1]);
84 }
85
86 //////////////////////////////////////////////////////////////////////////////
87 // Functions for getting the translation of a 3D or 2D affine transform matrix
88 //////////////////////////////////////////////////////////////////////////////
89
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)
93 {
94 typedef typename MatT::value_type value_type;
95 typedef vector< value_type, fixed<3> > vector_type;
96
97 /* Checking */
98 detail::CheckMatAffine3D(m);
99
100 return vector_type(
101 m.basis_element(3,0),
102 m.basis_element(3,1),
103 m.basis_element(3,2)
104 );
105 }
106
107 /** Get the translation of a 3D affine transform */
108 template < class MatT > void
109 matrix_get_translation(
110 const MatT& m,
111 typename MatT::value_type& t1,
112 typename MatT::value_type& t2,
113 typename MatT::value_type& t3
114 )
115 {
116 typedef typename MatT::value_type value_type;
117 typedef vector< value_type, fixed<3> > vector_type;
118
119 /* Checking */
120 detail::CheckMatAffine3D(m);
121
122 t1 = m.basis_element(3,0);
123 t2 = m.basis_element(3,1);
124 t3 = m.basis_element(3,2);
125 }
126
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)
130 {
131 typedef typename MatT::value_type value_type;
132 typedef vector< value_type, fixed<2> > vector_type;
133
134 /* Checking */
135 detail::CheckMatAffine2D(m);
136
137 return vector_type(m.basis_element(2,0), m.basis_element(2,1));
138 }
139
140 /** Get the translation of a 2D affine transform */
141 template < class MatT > void
142 matrix_get_translation_2D(
143 const MatT& m,
144 typename MatT::value_type& t1,
145 typename MatT::value_type& t2
146 )
147 {
148 typedef typename MatT::value_type value_type;
149 typedef vector< value_type, fixed<2> > vector_type;
150
151 /* Checking */
152 detail::CheckMatAffine2D(m);
153
154 t1 = m.basis_element(2,0);
155 t2 = m.basis_element(2,1);
156 }
157
158 //////////////////////////////////////////////////////////////////////////////
159 // Function for getting the translation of a 3D view matrix
160 //////////////////////////////////////////////////////////////////////////////
161
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)
165 {
166 typedef typename MatT::value_type value_type;
167 typedef vector< value_type, fixed<3> > vector_type;
168
169 vector_type x, y, z;
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));
173 }
174
175 } // namespace cml
176
177 #endif
This page took 0.039999 seconds and 3 git commands to generate.