GteTransform.h
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #pragma once
9 
10 #include <LowLevel/GteLogger.h>
14 
15 // Transforms when using the convention GTE_USE_MAT_VEC.
16 //
17 // The transform is Y = M*X+T, where M is a 3-by-3 matrix and T is a 3x1
18 // translation. In most cases, M = R, a rotation matrix, or M = R*S,
19 // where R is a rotation matrix and S is a diagonal matrix whose diagonal
20 // entries are positive scales. To support modeling packages that allow
21 // general affine transforms, M can be any invertible 3x3 matrix. The vector
22 // X is transformed in the "forward" direction to Y. The "inverse" direction
23 // transforms Y to X, namely X = M^{-1}*(Y-T) in the general case. In the
24 // special case of M = R*S, the inverse direction is X = S^{-1}*R^t*(Y-T),
25 // where S^{-1} is the diagonal matrix whose diagonal entries are the
26 // reciprocoals of those of S and where R^t is the transpose of R. For SIMD
27 // support of matrix-vector and matrix-matrix multiplications, a homogeneous
28 // matrix H = {{M,T},{0,1}} is stored by this class. The forward transform is
29 // {Y,1} = H*{X,1} and the inverse transform is {X,1} = H^{-1}*{Y,1}.
30 
31 // Transforms when using the convention GTE_USE_VEC_MAT.
32 //
33 // The transform is Y = T + X*M, where M is a 3-by-3 matrix and T is a 1x3
34 // translation. In most cases, M = R, a rotation matrix, or M = S*R,
35 // where R is a rotation matrix and S is a diagonal matrix whose diagonal
36 // entries are positive scales. To support modeling packages that allow
37 // general affine transforms, M can be any invertible 3x3 matrix. The vector
38 // X is transformed in the "forward" direction to Y. The "inverse" direction
39 // transforms Y to X, namely X = (Y-T)*M^{-1} in the general case. In the
40 // special case of M = S*R, the inverse direction is X = (Y-T)*R^t*S^{-1},
41 // where S^{-1} is the diagonal matrix whose diagonal entries are the
42 // reciprocoals of those of S and where R^t is the transpose of R. For SIMD
43 // support of matrix-vector and matrix-matrix multiplications, a homogeneous
44 // matrix H = {{M,0},{T,1}} is stored by this class. The forward transform is
45 // {Y,1} = {X,1}*H and the inverse transform is {X,1} = {Y,1}*H^{-1}.
46 
47 // With either multiplication convention, a matrix M = R*S (GTE_USE_MAT_VEC)
48 // or a matrix M = S*R (GTE_USE_VEC_MAT) is referred to as an "RS-matrix".
49 // The class does not provide a member function to compute the inverse of a
50 // transform: 'Transform GetInverse() const'. If one were to add this,
51 // be aware that the inverse of an RS-matrix is not generally an RS-matrix;
52 // that is, the inverse of R*S is S^{-1}*R^t which cannot always be factored
53 // as S^{-1} * R^t = R' * S'. You would need to SetMatrix using S^{-1}*R^t
54 // as the input.
55 
56 namespace gte
57 {
58 
59 class Transform
60 {
61 public:
62  // The default constructor produces the identity transformation. The
63  // default copy constructor and assignment operator are generated by the
64  // compiler.
65  Transform();
66 
67  // Implicit conversion.
68  inline operator Matrix4x4<float> const&() const;
69 
70  // Set the transformation to the identity matrix.
71  void MakeIdentity();
72 
73  // Set the transformation to have scales of 1.
74  void MakeUnitScale();
75 
76  // Hints about the structure of the transformation.
77  inline bool IsIdentity() const; // I
78  inline bool IsRSMatrix() const; // R*S or S*R (depends on convention)
79  inline bool IsUniformScale() const; // RS-matrix with S = c*I
80 
81  // Member access.
82  // (1) The Set* functions set the is-identity hint to false.
83  // (2) The SetRotate function sets the is-rsmatrix hint to true. If this
84  // hint is false, GetRotate triggers an assertion in debug mode.
85  // (3) The SetMatrix function sets the is-rsmatrix and is-uniform-scale
86  // hints to false.
87  // (4) The SetScale function sets the is-uniform-scale hint to false.
88  // The SetUniformScale function sets the is-uniform-scale hint to
89  // true. If this hint is false, GetUniformScale triggers an assertion
90  // in debug mode.
91  // (5) All Set* functions set the inverse-needs-update to true. When
92  // GetHInverse is called, the inverse must be computed in this case
93  // and the inverse-needs-update is reset to false.
94  void SetRotation(Matrix4x4<float> const& rotate); // {{R,0},{0,1}}
95  void SetMatrix(Matrix4x4<float> const& matrix); // {{M,0},{0,1}}
96  void SetTranslation(float x0, float x1, float x2);
97  void SetTranslation(Vector3<float> const& translate);
98  void SetTranslation(Vector4<float> const& translate);
99  void SetScale(float s0, float s1, float s2);
100  void SetScale(Vector3<float> const& scale);
101  void SetScale(Vector4<float> const& scale);
102  void SetUniformScale(float scale);
103  inline Matrix4x4<float> const& GetRotation() const; // {{R,0},{0,1}}
104  inline Matrix4x4<float> const& GetMatrix() const; // {{M,0},{0,1}}
105  inline Vector3<float> GetTranslation() const; // (x,y,z)
106  inline Vector4<float> GetTranslationW0() const; // (x,y,z,0)
107  inline Vector4<float> GetTranslationW1() const; // (x,y,z,1)
108  inline Vector3<float> GetScale() const; // (s0,s1,s2)
109  inline Vector4<float> GetScaleW1() const; // (s0,s1,s2,1)
110  inline float GetUniformScale() const;
111 
112  // Alternate representations to set/get the rotation.
113 
114  // Set/get from 3x3 matrices.
115  void SetRotation(Matrix3x3<float> const& rotate);
116  void GetRotation(Matrix3x3<float>& rotate) const;
117 
118  // The quaternion is unit length.
119  void SetRotation(Quaternion<float> const& q);
120  void GetRotation(Quaternion<float>& q) const;
121 
122  // The axis is unit length and the angle is in radians.
123  void SetRotation(AxisAngle<4, float> const& axisAngle);
124  void GetRotation(AxisAngle<4, float>& axisAngle) const;
125 
126  // The Euler angles are in radians. The GetEulerAngles function
127  // expects the eulerAngles.axis[] values to be set to the axis order
128  // you want.
129  void SetRotation(EulerAngles<float> const& eulerAngles);
130  void GetRotation(EulerAngles<float>& eulerAngles) const;
131 
132  // For M = R*S or M = S*R, the largest value of S in absolute value is
133  // returned. For general M, the max-row-sum norm is returned for the
134  // GTE_USE_MAT_VEC convention and the max-col-sum norm is returned for
135  // the GTE_USE_VEC_MAT convetion, which is a reasonable measure of maximum
136  // scale of the transformation.
137  float GetNorm() const;
138 
139  // Get the homogeneous matrix (composite of all channels).
140  inline Matrix4x4<float> const& GetHMatrix() const;
141 
142  // Get the inverse homogeneous matrix, recomputing it when necessary.
143  // GTE_USE_MAT_VEC
144  // H = {{M,T},{0,1}}, then H^{-1} = {{M^{-1},-M^{-1}*T},{0,1}}
145  // GTE_USE_VEC_MAT
146  // H = {{M,0},{T,1}}, then H^{-1} = {{M^{-1},0},{-M^{-1}*T,1}}
147  Matrix4x4<float> const& GetHInverse() const;
148 
149  // Invert the transform. If possible, the channels are properly
150  // assigned. For example, if the input has mIsRSMatrix equal to
151  // 'true', then the inverse also has mIsRSMatrix equal to 'true'
152  // and the inverse's mMatrix is a rotation matrix and mScale is
153  // set accordingly.
154  Transform Inverse() const;
155 
156  // The identity transformation.
157  static Transform const IDENTITY;
158 
159 private:
160  // Fill in the entries of mHMatrix whenever one of the components
161  // mMatrix, mTranslate, or mScale changes.
162  void UpdateHMatrix();
163 
164  // Invert the 3x3 upper-left block of the input matrix.
165  static void Invert3x3(Matrix4x4<float> const& mat,
166  Matrix4x4<float>& invMat);
167 
168  // The full 4x4 homogeneous matrix H and its inverse H^{-1}, stored
169  // according to the conventions (see GetHInverse description). The
170  // inverse is computed only on demand.
173 
174  Matrix4x4<float> mMatrix; // M (general) or R (rotation)
178  mutable bool mInverseNeedsUpdate;
179 };
180 
181 // Compute M*V.
183 
184 // Compute V^T*M.
186 
187 // Compute A*B.
188 Transform operator*(Transform const& A, Transform const& B);
189 
191  Transform const& B);
192 
193 inline Matrix4x4<float> operator*(Transform const& A,
194  Matrix4x4<float> const& B);
195 
196 
197 inline Transform::operator Matrix4x4<float> const&() const
198 {
199  return GetHMatrix();
200 }
201 
202 inline bool Transform::IsIdentity() const
203 {
204  return mIsIdentity;
205 }
206 
207 inline bool Transform::IsRSMatrix() const
208 {
209  return mIsRSMatrix;
210 }
211 
212 inline bool Transform::IsUniformScale() const
213 {
214  return mIsRSMatrix && mIsUniformScale;
215 }
216 
218 {
219  LogAssert(mIsRSMatrix, "Transform is not rotation-scale.");
220  return mMatrix;
221 }
222 
224 {
225  return mMatrix;
226 }
227 
229 {
230  return Vector3<float>{mTranslate[0], mTranslate[1], mTranslate[2]};
231 }
232 
234 {
235  return Vector4<float>{mTranslate[0], mTranslate[1], mTranslate[2], 0.0f};
236 }
237 
239 {
240  return mTranslate;
241 }
242 
244 {
245  LogAssert(mIsRSMatrix, "Transform is not rotation-scale.");
246  return Vector3<float>{ mScale[0], mScale[1], mScale[2] };
247 }
248 
250 {
251  LogAssert(mIsRSMatrix, "Transform is not rotation-scale.");
252  return mScale;
253 }
254 
255 inline float Transform::GetUniformScale() const
256 {
257  LogAssert(mIsRSMatrix, "Transform is not rotation-scale.");
258  LogAssert(mIsUniformScale, "Transform is not uniform scale.");
259  return mScale[0];
260 }
261 
263 {
264  return mHMatrix;
265 }
266 
268  Transform const& B)
269 {
270  return A * B.GetHMatrix();
271 }
272 
274  Matrix4x4<float> const& B)
275 {
276  return A.GetHMatrix() * B;
277 }
278 
279 
280 }
bool IsIdentity() const
Definition: GteTransform.h:202
bool mInverseNeedsUpdate
Definition: GteTransform.h:178
float GetUniformScale() const
Definition: GteTransform.h:255
float GetNorm() const
void SetUniformScale(float scale)
Matrix4x4< float > mHMatrix
Definition: GteTransform.h:171
static void Invert3x3(Matrix4x4< float > const &mat, Matrix4x4< float > &invMat)
GLuint GLenum matrix
Definition: glext.h:10574
Matrix4x4< float > mInvHMatrix
Definition: GteTransform.h:172
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:9914
#define LogAssert(condition, message)
Definition: GteLogger.h:86
Matrix4x4< float > const & GetHMatrix() const
Definition: GteTransform.h:262
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s0
Definition: glext.h:9013
Matrix4x4< float > mMatrix
Definition: GteTransform.h:174
bool IsRSMatrix() const
Definition: GteTransform.h:207
Transform Inverse() const
void SetScale(float s0, float s1, float s2)
Vector4< float > GetScaleW1() const
Definition: GteTransform.h:249
bool IsUniformScale() const
Definition: GteTransform.h:212
GLuint GLfloat x0
Definition: glext.h:9013
Matrix4x4< float > const & GetRotation() const
Definition: GteTransform.h:217
Vector3< float > GetScale() const
Definition: GteTransform.h:243
Matrix4x4< float > const & GetMatrix() const
Definition: GteTransform.h:223
void MakeUnitScale()
GLuint GLfloat GLfloat GLfloat x1
Definition: glext.h:9013
void SetMatrix(Matrix4x4< float > const &matrix)
GLfixed GLfixed x2
Definition: glext.h:4952
GLdouble GLdouble GLdouble GLdouble q
Definition: glext.h:255
Vector4< float > mTranslate
Definition: GteTransform.h:175
void SetRotation(Matrix4x4< float > const &rotate)
static Transform const IDENTITY
Definition: GteTransform.h:157
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat s1
Definition: glext.h:9013
Vector4< float > GetTranslationW0() const
Definition: GteTransform.h:233
Vector3< float > GetTranslation() const
Definition: GteTransform.h:228
Vector4< float > GetTranslationW1() const
Definition: GteTransform.h:238
void SetTranslation(float x0, float x1, float x2)
Vector4< float > operator*(Transform const &M, Vector4< float > const &V)
Matrix4x4< float > const & GetHInverse() const
Vector4< float > mScale
Definition: GteTransform.h:176


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:01