tcElevationOptimization: fix typo s/write/read/
[tecorrec.git] / geo / tcAffineTransform.h
blob7fc4842404276bc20450198c39a478c02da01e5b
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 #ifndef _tcAffineTransform_h_
21 #define _tcAffineTransform_h_
23 /**
24 * @file tcAffineTransform.h
25 * @brief Affine transformation between 2d linear coordinate spaces.
28 #include "tcGeo.h"
29 #include <Vector.h>
31 #include <cmath>
33 /// Geographical coordinates.
34 template <typename T>
35 class tcAffineTransform2
37 public:
40 * Constructors + destructor
43 /// Default constructor.
44 tcAffineTransform2()
45 : m_transform()
47 m_transform[0][1] = m_transform[1][2] = 1.0f;
48 m_transform[0][2] = m_transform[1][1]
49 = m_transform[0][0] = m_transform[0][0] = 0.0f;
52 /// Primary constructor.
53 tcAffineTransform2(const maths::Vector<2,T>& min, const maths::Vector<2,T>& diagonal)
54 : m_transform()
56 for (int i = 0; i < 2; ++i)
58 m_transform[i][0] = min[i];
59 m_transform[i][1+i] = diagonal[i];
60 m_transform[i][2-i] = 0.0f;
64 /// Primary constructor.
65 tcAffineTransform2(const maths::Vector<2,T>& min, const maths::Vector<2,T>& x, const maths::Vector<2,T>& y)
66 : m_transform()
68 for (int i = 0; i < 2; ++i)
70 m_transform[i][0] = min[i];
71 m_transform[i][1] = x[i];
72 m_transform[i][2] = y[i];
76 /// Primary constructor.
77 tcAffineTransform2(const T* m)
78 : m_transform()
80 for (int i = 0; i < 2*3; ++i)
82 ((T*)m_transform)[i] = m[i];
86 /// Primary constructor.
87 tcAffineTransform2(T m11, T m12, T m13, T m21, T m22, T m23)
88 : m_transform()
90 m_transform[0][0] = m11;
91 m_transform[0][1] = m12;
92 m_transform[0][2] = m13;
93 m_transform[1][0] = m21;
94 m_transform[1][1] = m22;
95 m_transform[1][2] = m23;
99 * Accessors + mutators
102 /// Origin of coordinate system.
103 maths::Vector<2,T> origin() const
105 return maths::Vector<2,T>(m_transform[0][0], m_transform[1][0]);
107 maths::Vector<2,T> axis(int index) const
109 Q_ASSERT(index >= 0 && index < 2);
110 return maths::Vector<2,T>(m_transform[0][1+index], m_transform[1][1+index]);
113 /// Find the inverse.
114 tcAffineTransform2 inverse() const;
117 * Operators
120 /// Transform a coordinate.
121 maths::Vector<2,T> operator * (const maths::Vector<2,T>& rhs) const
123 return maths::Vector<2,T>(
124 m_transform[0][0] + m_transform[0][1]*rhs[0] + m_transform[0][2]*rhs[1],
125 m_transform[1][0] + m_transform[1][1]*rhs[0] + m_transform[1][2]*rhs[1]
129 /// Transform a coordinate by inverse of this matrix.
130 maths::Vector<2,T> operator / (const maths::Vector<2,T>& rhs) const
132 maths::Vector<2,T> rebased = rhs - origin();
133 return maths::Vector<2,T>( rebased / axis(0), rebased / axis(1) );
136 /// Transform by another such matrix to get another which performs rhs then lhs on the result.
137 tcAffineTransform2 operator * (const tcAffineTransform2& rhs) const
139 return tcAffineTransform2(*this * rhs.origin(), *this * rhs.axis(0), *this * rhs.axis(1));
142 /// Convert to a pointer to elements.
143 operator T* ()
145 return &m_transform[0][0];
148 /// Convert to a pointer to elements.
149 operator const T* () const
151 return &m_transform[0][0];
154 private:
157 * Variables
160 /// Matrix, first column is offset, second is x vector, third is y vector.
161 T m_transform[2][3];
165 #endif // _tcAffineTransform_h_