]> Dogcows Code - chaz/yoink/blob - src/cml/mathlib/matrix_translation.h
now using cml for vectors and math stuff
[chaz/yoink] / src / 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 2D affine transform */
108 template < class MatT > vector< typename MatT::value_type, fixed<2> >
109 matrix_get_translation_2D(const MatT& m)
110 {
111 typedef typename MatT::value_type value_type;
112 typedef vector< value_type, fixed<2> > vector_type;
113
114 /* Checking */
115 detail::CheckMatAffine2D(m);
116
117 return vector_type(m.basis_element(2,0), m.basis_element(2,1));
118 }
119
120 //////////////////////////////////////////////////////////////////////////////
121 // Function for getting the translation of a 3D view matrix
122 //////////////////////////////////////////////////////////////////////////////
123
124 /** Get the translation of a 3D affine transform */
125 template < class MatT > vector< typename MatT::value_type, fixed<3> >
126 matrix_get_view_translation(const MatT& m)
127 {
128 typedef typename MatT::value_type value_type;
129 typedef vector< value_type, fixed<3> > vector_type;
130
131 vector_type x, y, z;
132 matrix_get_basis_vectors(m,x,y,z);
133 vector_type p = matrix_get_translation(m);
134 return vector_type(-dot(p,x),-dot(p,y),-dot(p,z));
135 }
136
137 } // namespace cml
138
139 #endif
This page took 0.036702 seconds and 4 git commands to generate.