1 #ifndef MSP_GEOMETRY_AFFINETRANSFORMATION_H_
2 #define MSP_GEOMETRY_AFFINETRANSFORMATION_H_
4 #include <msp/linal/squarematrix.h>
10 template<typename T, unsigned D>
11 class AffineTransformation;
15 Helper class to provide specialized operations for AffineTransformation.
17 template<typename T, unsigned D>
18 class AffineTransformationOps
21 AffineTransformationOps() { }
25 class AffineTransformationOps<T, 2>
28 AffineTransformationOps() { }
31 static AffineTransformation<T, 2> rotation(const Angle<T> &);
35 class AffineTransformationOps<T, 3>
38 AffineTransformationOps() { }
41 static AffineTransformation<T, 3> rotation(const Angle<T> &, const LinAl::Vector<T, 3> &);
46 An affine transformation in D dimensions. Affine transformations preserve
47 straightness of lines and ratios of distances. Angles and distances themselves
48 may change. Internally this is represented by a square matrix of size D+1.
50 template<typename T, unsigned D>
51 class AffineTransformation: public AffineTransformationOps<T, D>
53 friend class AffineTransformationOps<T, D>;
56 LinAl::SquareMatrix<T, D+1> matrix;
59 AffineTransformation();
61 static AffineTransformation<T, D> translation(const LinAl::Vector<T, D> &);
62 static AffineTransformation<T, D> scaling(const LinAl::Vector<T, D> &);
63 static AffineTransformation<T, D> shear(const LinAl::Vector<T, D> &, const LinAl::Vector<T, D> &);
65 const LinAl::SquareMatrix<T, D+1> &get_matrix() const { return matrix; }
66 operator const LinAl::SquareMatrix<T, D+1> &() const { return matrix; }
68 LinAl::Vector<T, D> transform(const LinAl::Vector<T, D> &) const;
69 LinAl::Vector<T, D> transform_linear(const LinAl::Vector<T, D> &) const;
72 template<typename T, unsigned D>
73 inline AffineTransformation<T, D>::AffineTransformation()
75 this->matrix = LinAl::SquareMatrix<T, D+1>::identity();
79 template<typename T, unsigned D>
80 AffineTransformation<T, D> AffineTransformation<T, D>::translation(const LinAl::Vector<T, D> &v)
82 AffineTransformation<T, D> r;
83 for(unsigned i=0; i<D; ++i)
84 r.matrix(i, D) = v[i];
88 template<typename T, unsigned D>
89 AffineTransformation<T, D> AffineTransformation<T, D>::scaling(const LinAl::Vector<T, D> &factors)
91 AffineTransformation<T, D> r;
92 for(unsigned i=0; i<D; ++i)
93 r.matrix(i, i) = factors[i];
97 template<typename T, unsigned D>
98 AffineTransformation<T, D> AffineTransformation<T, D>::shear(const LinAl::Vector<T, D> &normal, const LinAl::Vector<T, D> &shift)
100 AffineTransformation<T, D> r;
101 for(unsigned i=0; i<D; ++i)
102 for(unsigned j=0; j<D; ++j)
103 r.matrix(i, j) += normal[j]*shift[i];
108 AffineTransformation<T, 2> AffineTransformationOps<T, 2>::rotation(const Angle<T> &angle)
110 AffineTransformation<T, 2> r;
121 AffineTransformation<T, 3> AffineTransformationOps<T, 3>::rotation(const Angle<T> &angle, const LinAl::Vector<T, 3> &axis)
123 AffineTransformation<T, 3> r;
124 LinAl::Vector<T, 3> axn = normalize(axis);
127 // http://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_and_angle
128 r.matrix(0, 0) = c+axn.x*axn.x*(1-c);
129 r.matrix(0, 1) = axn.x*axn.y*(1-c)-axn.z*s;
130 r.matrix(0, 2) = axn.x*axn.z*(1-c)+axn.y*s;
131 r.matrix(1, 0) = axn.y*axn.x*(1-c)+axn.z*s;
132 r.matrix(1, 1) = c+axn.y*axn.y*(1-c);
133 r.matrix(1, 2) = axn.y*axn.z*(1-c)-axn.x*s;
134 r.matrix(2, 0) = axn.z*axn.x*(1-c)-axn.y*s;
135 r.matrix(2, 1) = axn.z*axn.y*(1-c)+axn.x*s;
136 r.matrix(2, 2) = c+axn.z*axn.z*(1-c);
141 template<typename T, unsigned N>
142 inline LinAl::Vector<T, N+1> augment_vector(const LinAl::Vector<T, N> &v, T s)
144 LinAl::Vector<T, N+1> r;
145 for(unsigned i=0; i<N; ++i)
151 template<typename T, unsigned N>
152 inline LinAl::Vector<T, N-1> reduce_vector(const LinAl::Vector<T, N> &v)
154 LinAl::Vector<T, N-1> r;
155 for(unsigned i=0; i<N-1; ++i)
160 template<typename T, unsigned N>
161 inline LinAl::Vector<T, N-1> divide_vector(const LinAl::Vector<T, N> &v)
163 LinAl::Vector<T, N-1> r;
164 for(unsigned i=0; i<N-1; ++i)
170 template<typename T, unsigned D>
171 inline LinAl::Vector<T, D> AffineTransformation<T, D>::transform(const LinAl::Vector<T, D> &v) const
173 return reduce_vector(matrix*augment_vector(v, T(1)));
176 template<typename T, unsigned D>
177 inline LinAl::Vector<T, D> AffineTransformation<T, D>::transform_linear(const LinAl::Vector<T, D> &v) const
179 return reduce_vector(matrix*augment_vector(v, T(0)));
182 } // namespace Geometry