Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
transformutils.h
Go to the documentation of this file.
1 
18 #ifndef ION_MATH_TRANSFORMUTILS_H_
19 #define ION_MATH_TRANSFORMUTILS_H_
20 
40 
41 #include "ion/math/angle.h"
42 #include "ion/math/matrix.h"
43 #include "ion/math/rotation.h"
44 #include "ion/math/vector.h"
45 
46 namespace ion {
47 namespace math {
48 
50 
53 
54 
59 template <int Dimension, typename T>
61  const Vector<Dimension, T>& v) {
63  for (int row = 0; row < Dimension; ++row) {
64  for (int col = 0; col < Dimension; ++col)
65  result[row] += m(row, col) * v[col];
66  }
67  return result;
68 }
69 
75 template <int Dimension, typename T>
77  const Point<Dimension, T>& p) {
79  for (int row = 0; row < Dimension; ++row) {
80  for (int col = 0; col < Dimension; ++col)
81  result[row] += m(row, col) * p[col];
82  result[row] += m(row, Dimension); // Homogeneous coordinate.
83  }
84  return result;
85 }
86 
91 template <int Dimension, typename T>
93  const Point<Dimension, T>& p) {
95  for (int row = 0; row < Dimension; ++row) {
96  for (int col = 0; col < Dimension; ++col)
97  result[row] += m(row, col) * p[col];
98  result[row] += m(row, Dimension);
99  }
100 
101  T homogeneous_coordinate = m(Dimension, Dimension);
102  for (int col = 0; col < Dimension; ++col)
103  homogeneous_coordinate += m(Dimension, col) * p[col];
104 
106  if (homogeneous_coordinate != T(0))
107  result /= homogeneous_coordinate;
108  return result;
109 }
110 
112 
115 
117 template <typename T>
119  return Matrix<3, T>(m(0, 0), m(0, 1), m(0, 2),
120  m(1, 0), m(1, 1), m(1, 2),
121  m(2, 0), m(2, 1), m(2, 2));
122 }
123 
127 template <typename T>
128 const Matrix<4, T> OrthoInverseH(const Matrix<4, T>& m);
129 
131 
134 
135 
140 template <int Dimension, typename T>
142  const VectorBase<Dimension, T>& t) {
143  typedef Matrix<Dimension + 1, T> MatrixType;
144  MatrixType result = MatrixType::Identity();
145  for (int row = 0; row < Dimension; ++row)
146  result[row][Dimension] = t[row];
147  return result;
148 }
149 
154 template <int Dimension, typename T>
157  for (int row = 0; row < Dimension; ++row) {
158  for (int col = 0; col < Dimension + 1; ++col)
159  result[row][col] = row == col ? s[row] : static_cast<T>(0);
160  }
162  for (int col = 0; col < Dimension; ++col)
163  result[Dimension][col] = static_cast<T>(0);
164  result[Dimension][Dimension] = static_cast<T>(1);
165  return result;
166 }
167 
171 template <int Dimension, typename T>
173  Matrix<Dimension, T> result;
174  for (int row = 0; row < Dimension; ++row) {
175  for (int col = 0; col < Dimension; ++col)
176  result[row][col] = row == col ? s[row] : static_cast<T>(0);
177  }
178  return result;
179 }
180 
183 template <typename T> ION_API
184 const Matrix<4, T> RotationMatrixH(const Rotation<T>& r);
185 
189 template <typename T> ION_API
190 const Matrix<3, T> RotationMatrixNH(const Rotation<T>& r);
191 
195 template <typename T>
197  const Angle<T>& angle) {
198  return RotationMatrixH(Rotation<T>::FromAxisAndAngle(axis, angle));
199 }
200 
204 template <typename T>
206  const Angle<T>& angle) {
207  return RotationMatrixNH(Rotation<T>::FromAxisAndAngle(axis, angle));
208 }
209 
211 
214 
215 
220 template <typename T> ION_API
221 const Matrix<4, T> LookAtMatrixFromCenter(
222  const Point<3, T>& eye, const Point<3, T>& center, const Vector<3, T>& up);
223 
227 template <typename T> ION_API
228 const Matrix<4, T> LookAtMatrixFromDir(
229  const Point<3, T>& eye, const Vector<3, T>& dir, const Vector<3, T>& up);
230 
232 
235 
236 
241 template <typename T> ION_API
242 const Matrix<4, T> OrthographicMatrixFromFrustum(
243  T x_left, T x_right, T y_bottom, T y_top, T z_near, T z_far);
244 
249 template <typename T> ION_API
250 const Matrix<4, T> PerspectiveMatrixFromFrustum(
251  T x_left, T x_right, T y_bottom, T y_top, T z_near, T z_far);
252 
257 template <typename T> ION_API
258 const Matrix<4, T> PerspectiveMatrixFromView(const Angle<T>& fovy, T aspect,
259  T z_near, T z_far);
260 
272 template <typename T>
273 const Matrix<4, T> PerspectiveMatrixInverse(const Matrix<4, T>& m);
274 
275 } // namespace math
276 } // namespace ion
277 
278 #endif // ION_MATH_TRANSFORMUTILS_H_
VectorBase.
Definition: vector.h:43
static const Vector Zero()
Returns a Vector containing all zeroes.
Definition: vector.h:201
const Matrix< 4, T > RotationMatrixAxisAngleH(const Vector< 3, T > &axis, const Angle< T > &angle)
Returns a 4x4 Matrix representing a 3D rotation specified as axis and angle.
ION_API const Matrix< 4, T > RotationMatrixH(const Rotation< T > &r)
Returns a 4x4 Matrix representing a 3D rotation.
A simple class to represent angles.
Definition: angle.h:33
ION_API const Matrix< 4, T > LookAtMatrixFromDir(const Point< 3, T > &eye, const Vector< 3, T > &dir, const Vector< 3, T > &up)
Returns a 4x4 viewing matrix based on the given camera parameters, which use a view direction rather ...
ION_API const Matrix< 4, T > LookAtMatrixFromCenter(const Point< 3, T > &eye, const Point< 3, T > &center, const Vector< 3, T > &up)
View matrices.
The Matrix class defines a square N-dimensional matrix.
Definition: matrix.h:35
ION_API const Matrix< 4, T > PerspectiveMatrixFromFrustum(T x_left, T x_right, T y_bottom, T y_top, T z_near, T z_far)
Returns a 4x4 perspective projection matrix based on the given parameters, which follow the conventio...
const Matrix< Dimension, T > ScaleMatrixNH(const Vector< Dimension, T > &s)
Returns a Matrix representing a scale by the factors in a Vector, which is the same Dimension as the ...
const Point< Dimension, T > ProjectPoint(const Matrix< Dimension+1, T > &m, const Point< Dimension, T > &p)
Multiplies a Matrix and a Point of one smaller Dimension (the template parameter) to produce another ...
const Matrix< Dimension+1, T > TranslationMatrix(const VectorBase< Dimension, T > &t)
Affine transformation matrices.
ION_API const Matrix< 3, T > RotationMatrixNH(const Rotation< T > &r)
Returns a 3x3 Matrix representing a 3D rotation.
ION_API const Matrix< 4, T > PerspectiveMatrixFromView(const Angle< T > &fovy, T aspect, T z_near, T z_far)
Returns a 4x4 perspective projection matrix based on the given parameters, which follow the conventio...
Vector< Dimension, T > operator*(const Matrix< Dimension, T > &m, const Vector< Dimension, T > &v)
Multiplies a Matrix and a column Vector of the same Dimension to produce another column Vector...
Definition: matrixutils.h:77
const Matrix< 4, T > OrthoInverseH(const Matrix< 4, T > &m)
Public functions.
Vector.
Definition: vector.h:180
The Rotation class represents a rotation around a 3-dimensional axis.
Definition: rotation.h:37
const Matrix< Dimension+1, T > ScaleMatrixH(const Vector< Dimension, T > &s)
Returns a Matrix representing a scale by the factors in a Vector whose Dimension is one less than the...
ION_API const Matrix< 4, T > OrthographicMatrixFromFrustum(T x_left, T x_right, T y_bottom, T y_top, T z_near, T z_far)
Projection matrices.
ION_API const Matrix< 4, T > PerspectiveMatrixInverse(const Matrix< 4, T > &m)
Returns the inverse of m iff m is a perspective projection matrix, i.e., iff it has the following for...
static const Point Zero()
Returns a Point containing all zeroes.
Definition: vector.h:320
Point.
Definition: vector.h:296
const Matrix< 3, T > RotationMatrixAxisAngleNH(const Vector< 3, T > &axis, const Angle< T > &angle)
Returns a 3x3 Matrix representing a 3D rotation specified as axis and angle.
Matrix< 3, T > NonhomogeneousSubmatrixH(const Matrix< 4, T > &m)
Homogeneous matrices.