GL Studio C++ Runtime API
gls_matrix_affine.h
Go to the documentation of this file.
1/// \file gls_matrix_affine.h
2/// \brief The GlsMatrixAffine class.
3///
4/// \par Copyright Information
5///
6/// Copyright (c) 2017 by The DiSTI Corporation.<br>
7/// 12249 Science Drive; Suite 300<br>
8/// Orlando, Florida 32826<br>
9/// USA<br>
10/// <br>
11/// All rights reserved.<br>
12///
13/// This Software contains proprietary trade secrets of DiSTI and may not be
14/// reproduced, in whole or part, in any form, or by any means of electronic,
15/// mechanical, or otherwise, without the written permission of DiSTI. Said
16/// permission may be derived through the purchase of applicable DiSTI product
17/// licenses which detail the distribution rights of this content and any
18/// Derivative Works based on this or other copyrighted DiSTI Software.
19///
20/// NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21/// AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22/// PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23/// AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24/// IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25/// PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26///
27/// LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28/// IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29/// INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30/// DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31/// INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32/// INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33/// OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34/// EXCEED FIVE DOLLARS (US$5.00).
35///
36/// The aforementioned terms and restrictions are governed by the laws of the
37/// State of Florida and the United States of America.
38
39#ifndef GLS_MATRIXAFFINE_H
40#define GLS_MATRIXAFFINE_H
41
42#include "gls_matrix.h"
43#include "gls_quaternion.h"
44#include "vertex.h"
45
46namespace disti
47{
48//----------------------------------------------------------------------------
49/**
50* The GlsMatrixAffine class provides support for a 4x4 affine transformation
51* matrix of any numeric type. It provides ability to perform rotations,
52* scaling, and translations. Results are cummulative, i.e. every
53* rotate, scale, and translate call transforms the terms that already exist
54* in the matrix. Call MakeIdentity() to start with an identity matrix, or
55* Clear() to clear the matrix with all zeros.
56*/
57//----------------------------------------------------------------------------
58template<class Type>
59class GlsMatrixAffine : public GlsMatrix<Type, 4>
60{
61public:
62 using GlsMatrix<Type, 4>::_matrix;
63 DISTI_DEPRECATED( "This identifier is forbidden by the C++ standard. Use BaseClass instead." )
64 typedef GlsMatrix<Type, 4> _BaseClass; ///< Deprecated typedef for the base class.
65 typedef GlsMatrix<Type, 4> BaseClass; ///< Typedef for the base class.
66
67 //------------------------------------------------------------------------
68 /**
69 * Default constructor. The new matrix will be the identity matrix.
70 */
71 //------------------------------------------------------------------------
73
74 //------------------------------------------------------------------------
75 /**
76 * Constructor.
77 *
78 * \param m a row-major, two-dimensional array used to initialize the
79 * matrix.
80 */
81 //------------------------------------------------------------------------
82 GlsMatrixAffine( const Type m[ 4 ][ 4 ] );
83
84 //------------------------------------------------------------------------
85 /**
86 * Constructor.
87 *
88 * \param m a column-major, one-dimensional array containing all values
89 * to initialize the matrix with.
90 */
91 //------------------------------------------------------------------------
92 GlsMatrixAffine( const Type* m );
93
94 //------------------------------------------------------------------------
95 /**
96 * Copy Constructor.
97 *
98 * \param m base class matrix to copy.
99 */
100 //------------------------------------------------------------------------
101 GlsMatrixAffine( const GlsMatrix<Type, 4>& m );
102
103 //------------------------------------------------------------------------
104 /**
105 * Destructor.
106 */
107 //------------------------------------------------------------------------
109
110#ifdef _MSC_VER
111# pragma warning( push )
112# pragma warning( disable : 4458 ) // PRECISION is technically shadowed in the function below, but most compilers need it defined below anyway to compile...
113#endif
114 /// \return True if the supplied matrix is "very close" to this one.
115 /// \param b The matrix to compare.
116 bool IsVeryClose( const GlsMatrix<Type, 4>& b ) const
117 {
118 static const unsigned int SIZE = 4 * 4;
119 static const double PRECISION = 0.00001;
120
121 for( unsigned int i = 0; i < SIZE; ++i )
122 {
123 if( fabs( this->_data[ i ] - b.Data()[ i ] ) > PRECISION )
124 return false;
125 }
126 return true;
127 }
128#ifdef _MSC_VER
129# pragma warning( pop )
130#endif
131
132 //------------------------------------------------------------------------
133 /**
134 * Perform a rotation transformation around a major axis around the origin,
135 * and combine this transformation with the existing matrix data.
136 *
137 * \param angle rotation angle in degrees
138 * \param axis major axis of rotation: X_AXIS, Y_AXIS, Z_AXIS
139 */
140 //------------------------------------------------------------------------
141 void Rotate( Type angle, RotationAxis axis );
142
143 //------------------------------------------------------------------------
144 /**
145 * Perform a rotation transformation around a major axis using a specified
146 * center of rotation, and combine this transformation with the existing
147 * matrix data.
148 *
149 * \param angle rotation angle in degrees
150 * \param centerOfRotation point to rotate around
151 * \param axis major axis of rotation: X_AXIS, Y_AXIS, Z_AXIS
152 */
153 //------------------------------------------------------------------------
154 void Rotate( Type angle, const Vector& centerOfRotation, RotationAxis axis );
155
156 //------------------------------------------------------------------------
157 /**
158 * Perform a rotation transformation around an arbitrary axis using a
159 * specified center of rotation, and combine this transformation with the
160 * existing matrix data.
161 *
162 * \param angle rotation angle in degrees
163 * \param centerOfRotation point to rotate around
164 * \param axis arbitray axis of rotation
165 */
166 //------------------------------------------------------------------------
167 void Rotate( Type angle, const Vector& centerOfRotation, const Vector& axis );
168
169 //------------------------------------------------------------------------
170 /**
171 * Perform a scaling transformation using the origin as the anchor point,
172 * and combine this transformation with the existing matrix data.
173 *
174 * \param x scaling factor in the x direction. 1 = 100% (no scaling change)
175 * \param y scaling factor in the y direction. 1 = 100% (no scaling change)
176 * \param z scaling factor in the z direction. 1 = 100% (no scaling change)
177 */
178 //------------------------------------------------------------------------
179 void Scale( Type x, Type y, Type z );
180
181 //------------------------------------------------------------------------
182 /**
183 * Perform a scaling transformation using an arbitrary anchor point,
184 * and combine this transformation with the existing matrix data.
185 *
186 * \param x scaling factor in the x direction. 1 = 100% (no scaling change)
187 * \param y scaling factor in the y direction. 1 = 100% (no scaling change)
188 * \param z scaling factor in the z direction. 1 = 100% (no scaling change)
189 * \param anchor The anchor point for the scaling
190 */
191 //------------------------------------------------------------------------
192 void Scale( Type x, Type y, Type z, const Vector& anchor );
193
194 //------------------------------------------------------------------------
195 /**
196 * Perform a translation, and combine this transformation with the
197 * existing matrix data.
198 *
199 * \param x translation in the x direction
200 * \param y translation in the y direction
201 * \param z translation in the z direction
202 */
203 //------------------------------------------------------------------------
204 void Translate( Type x, Type y, Type z )
205 {
206 // The translation matrix is:
207 // |1 0 0 x|
208 // T = |0 1 0 y|
209 // |0 0 1 z|
210 // |0 0 0 1|
211 // We multiply it on the left: *this = T * *this;
212
213 _matrix[ 0 ][ 0 ] += x * _matrix[ 0 ][ 3 ];
214 _matrix[ 0 ][ 1 ] += y * _matrix[ 0 ][ 3 ];
215 _matrix[ 0 ][ 2 ] += z * _matrix[ 0 ][ 3 ];
216 //_matrix[0][3] = _matrix[0][3];
217 _matrix[ 1 ][ 0 ] += x * _matrix[ 1 ][ 3 ];
218 _matrix[ 1 ][ 1 ] += y * _matrix[ 1 ][ 3 ];
219 _matrix[ 1 ][ 2 ] += z * _matrix[ 1 ][ 3 ];
220 //_matrix[1][3] = _matrix[1][3];
221 _matrix[ 2 ][ 0 ] += x * _matrix[ 2 ][ 3 ];
222 _matrix[ 2 ][ 1 ] += y * _matrix[ 2 ][ 3 ];
223 _matrix[ 2 ][ 2 ] += z * _matrix[ 2 ][ 3 ];
224 //_matrix[2][3] = _matrix[2][3];
225 _matrix[ 3 ][ 0 ] += x * _matrix[ 3 ][ 3 ];
226 _matrix[ 3 ][ 1 ] += y * _matrix[ 3 ][ 3 ];
227 _matrix[ 3 ][ 2 ] += z * _matrix[ 3 ][ 3 ];
228 //_matrix[3][3] = _matrix[3][3];
229 }
230
231 //------------------------------------------------------------------------
232 /**
233 * Perform a translation, and combine this transformation with the
234 * existing matrix data.
235 *
236 * \param v containing the translate values
237 */
238 //------------------------------------------------------------------------
239 void Translate( const Vector& v ) { Translate( (Type)v.x, (Type)v.y, (Type)v.z ); }
240
241 // LynxOS does not support 'using'
242 using BaseClass::operator*;
243
244 /// \return A transformed vector using the specified vertex and the data in the matrix, e.g. w = M * v
245 /// \param v The vector to transform.
246 Vector operator*( const Vector& v ) const
247 {
248 const GlsMatrix<Type, 4>& M = *this;
249 return Vector(
250 float( ( M( 0, 0 ) * v.x ) + ( M( 0, 1 ) * v.y ) + ( M( 0, 2 ) * v.z ) + M( 0, 3 ) ),
251 float( ( M( 1, 0 ) * v.x ) + ( M( 1, 1 ) * v.y ) + ( M( 1, 2 ) * v.z ) + M( 1, 3 ) ),
252 float( ( M( 2, 0 ) * v.x ) + ( M( 2, 1 ) * v.y ) + ( M( 2, 2 ) * v.z ) + M( 2, 3 ) ) );
253 }
254
255 /// \return A transformed vertex using the specified vertex and the data in the matrix, e.g. w = M * v
256 /// \param v The vertex to transform.
257 Vertex operator*( const Vertex& v ) const
258 {
259 const GlsMatrix<Type, 4>& M = *this;
260 return Vertex(
261 float( ( M( 0, 0 ) * v.x ) + ( M( 0, 1 ) * v.y ) + ( M( 0, 2 ) * v.z ) + M( 0, 3 ) ),
262 float( ( M( 1, 0 ) * v.x ) + ( M( 1, 1 ) * v.y ) + ( M( 1, 2 ) * v.z ) + M( 1, 3 ) ),
263 float( ( M( 2, 0 ) * v.x ) + ( M( 2, 1 ) * v.y ) + ( M( 2, 2 ) * v.z ) + M( 2, 3 ) ),
264 v.color.R(),
265 v.color.G(),
266 v.color.B(),
267 v.color.A() );
268 }
269
270 /// Transform the specified vertex using the data in the matrix, where the 4th vector component is assumed to be a 1 (position) i.e. v = M * v
271 /// \param v A modifiable reference to the vector to transform.
272 void Transform( Vector& v ) const
273 {
274 // Simple version is:
275 // v = *this * v;
276
277 // Faster version is:
278 float x = float( ( _matrix[ 0 ][ 0 ] * v.x ) + ( _matrix[ 1 ][ 0 ] * v.y ) + ( _matrix[ 2 ][ 0 ] * v.z ) + _matrix[ 3 ][ 0 ] );
279 float y = float( ( _matrix[ 0 ][ 1 ] * v.x ) + ( _matrix[ 1 ][ 1 ] * v.y ) + ( _matrix[ 2 ][ 1 ] * v.z ) + _matrix[ 3 ][ 1 ] );
280 float z = float( ( _matrix[ 0 ][ 2 ] * v.x ) + ( _matrix[ 1 ][ 2 ] * v.y ) + ( _matrix[ 2 ][ 2 ] * v.z ) + _matrix[ 3 ][ 2 ] );
281 v.x = x;
282 v.y = y;
283 v.z = z;
284 }
285};
286
287/** decompose the scale factors from a GlsMatrix
288 * \param[in] m the matrix to decompose
289 * \return scale a vector that contains the x, y, and z scale factors
290 */
291template<class Type>
293{
294 const Vector xAxis = Vector( float( m( 0, 0 ) ), float( m( 1, 0 ) ), float( m( 2, 0 ) ) );
295 const Vector yAxis = Vector( float( m( 0, 1 ) ), float( m( 1, 1 ) ), float( m( 2, 1 ) ) );
296 const Vector zAxis = Vector( float( m( 0, 2 ) ), float( m( 1, 2 ) ), float( m( 2, 2 ) ) );
297 return Vector( xAxis.Magnitude(), yAxis.Magnitude(), zAxis.Magnitude() );
298}
299
300/** decompose the rotation matrix from a GlsMatrix by dividing out the scale factor. If the x,y, or z scale is determined
301 * to be zero, the scale factors not removed from the matrix.
302 * \param[in] matrix the matrix to decompose
303 * \param[in] scale a vector that contains the x, y, and z scale factors
304 * \return the rotation matrix with the scale factors removed
305 */
306template<class Type>
308{
309 GlsMatrix<Type, 4> m( matrix );
310
311 // set the translation to 0
312 m( 0, 3 ) = Type();
313 m( 1, 3 ) = Type();
314 m( 2, 3 ) = Type();
315
316 // only decompose rotation if all scales are non-zero
317 if( 0 != scale.x && 0 != scale.y && 0 != scale.z )
318 {
319 m( 0, 0 ) /= scale.x;
320 m( 1, 0 ) /= scale.x;
321 m( 2, 0 ) /= scale.x;
322 m( 0, 1 ) /= scale.y;
323 m( 1, 1 ) /= scale.y;
324 m( 2, 1 ) /= scale.y;
325 m( 0, 2 ) /= scale.z;
326 m( 1, 2 ) /= scale.z;
327 m( 2, 2 ) /= scale.z;
328 }
329
330 return m;
331}
332
333/** decompose a GlsMatrix into its scaling, translation, and rotation counterparts. If the x,y, or z scale is determined
334 * to be zero, the rotation may not be accurate
335 * \param[in] matrix the matrix to decompose
336 * \param[out] scale a vector that contains the x, y, and z scale factors
337 * \param[out] location a vector that contains the x, y, and z translation
338 * \param[out] rotation a quaternion that contains the rotation
339 */
340template<class Type>
341void DecomposeMatrix( const GlsMatrix<Type, 4>& matrix, Vector& scale, Vector& location, GlsQuaternion<Type>& rotation )
342{
343 scale = DecomposeScale( matrix );
344
345 // decompose location
346 location = Vector( float( matrix( 0, 3 ) ), float( matrix( 1, 3 ) ), float( matrix( 2, 3 ) ) );
347
348 rotation = GlsQuaternion<Type>( DecomposeRotationMatrix( matrix, scale ) );
349}
350
351typedef GlsMatrixAffine<float> GlsMatrixAffineF; ///< Typedef for a matrix of floats.
352typedef GlsMatrixAffine<double> GlsMatrixAffineD; ///< Typedef for a matrix of doubles.
353
354//============================================================================
355//
356// CLASS IMPLEMENTATION
357//
358//============================================================================
359
360//----------------------------------------------------------------------------
361template<class Type>
363{
364}
365//----------------------------------------------------------------------------
366template<class Type>
368 : GlsMatrix<Type, 4>( m )
369{
370}
371//----------------------------------------------------------------------------
372template<class Type>
374 : GlsMatrix<Type, 4>( m )
375{
376}
377//----------------------------------------------------------------------------
378template<class Type>
380 : GlsMatrix<Type, 4>( m )
381{
382}
383//----------------------------------------------------------------------------
384template<class Type>
386{
387}
388
389//----------------------------------------------------------------------------
390template<class Type>
392{
393 // Find the sine and cosine of the angle of rotation to be used
394 // in the rotation transform matrix.
395 const double a( double( angle ) * DEG_TO_RAD );
396 const Type sina( (Type)sin( a ) );
397 const Type cosa( (Type)cos( a ) );
398
399 Type temp;
400
401 switch( axis )
402 {
403 case X_AXIS: {
404 // The rotation matrix for rotation about the x axis is
405 // |1 0 0 0|
406 // R = |0 cos(a) -sin(a) 0|
407 // |0 sin(a) cos(a) 0|
408 // |0 0 0 1|
409
410 // Multiply the current matrix by the rotation transformation
411 // *this = R * *this;
412
413 temp = _matrix[ 0 ][ 1 ];
414 _matrix[ 0 ][ 1 ] = temp * cosa - _matrix[ 0 ][ 2 ] * sina;
415 _matrix[ 0 ][ 2 ] = temp * sina + _matrix[ 0 ][ 2 ] * cosa;
416 temp = _matrix[ 1 ][ 1 ];
417 _matrix[ 1 ][ 1 ] = temp * cosa - _matrix[ 1 ][ 2 ] * sina;
418 _matrix[ 1 ][ 2 ] = temp * sina + _matrix[ 1 ][ 2 ] * cosa;
419 temp = _matrix[ 2 ][ 1 ];
420 _matrix[ 2 ][ 1 ] = temp * cosa - _matrix[ 2 ][ 2 ] * sina;
421 _matrix[ 2 ][ 2 ] = temp * sina + _matrix[ 2 ][ 2 ] * cosa;
422 temp = _matrix[ 3 ][ 1 ];
423 _matrix[ 3 ][ 1 ] = temp * cosa - _matrix[ 3 ][ 2 ] * sina;
424 _matrix[ 3 ][ 2 ] = temp * sina + _matrix[ 3 ][ 2 ] * cosa;
425
426 break;
427 }
428 case Y_AXIS: {
429 // The rotation matrix for rotation about the y axis is
430 // | cos(a) 0 sin(a) 0|
431 // R = | 0 1 0 0|
432 // |-sin(a) 0 cos(a) 0|
433 // | 0 0 0 1|
434
435 // Multiply the current matrix by the rotation transformation
436 // *this = R * *this;
437
438 temp = _matrix[ 0 ][ 0 ];
439 _matrix[ 0 ][ 0 ] = temp * cosa + _matrix[ 0 ][ 2 ] * sina;
440 _matrix[ 0 ][ 2 ] = _matrix[ 0 ][ 2 ] * cosa - temp * sina;
441 temp = _matrix[ 1 ][ 0 ];
442 _matrix[ 1 ][ 0 ] = temp * cosa + _matrix[ 1 ][ 2 ] * sina;
443 _matrix[ 1 ][ 2 ] = _matrix[ 1 ][ 2 ] * cosa - temp * sina;
444 temp = _matrix[ 2 ][ 0 ];
445 _matrix[ 2 ][ 0 ] = temp * cosa + _matrix[ 2 ][ 2 ] * sina;
446 _matrix[ 2 ][ 2 ] = _matrix[ 2 ][ 2 ] * cosa - temp * sina;
447 temp = _matrix[ 3 ][ 0 ];
448 _matrix[ 3 ][ 0 ] = temp * cosa + _matrix[ 3 ][ 2 ] * sina;
449 _matrix[ 3 ][ 2 ] = _matrix[ 3 ][ 2 ] * cosa - temp * sina;
450
451 break;
452 }
453 case Z_AXIS: {
454 // The rotation matrix for rotation about the z axis is
455 // |cos(a) sin(a) 0 0|
456 // R = |-sin(a) cos(a) 0 0|
457 // | 0 0 1 0|
458 // | 0 0 0 1|
459
460 // Multiply the current matrix by the rotation transformation
461 // *this = R * *this;
462
463 temp = _matrix[ 0 ][ 0 ];
464 _matrix[ 0 ][ 0 ] = temp * cosa - _matrix[ 0 ][ 1 ] * sina;
465 _matrix[ 0 ][ 1 ] = temp * sina + _matrix[ 0 ][ 1 ] * cosa;
466 temp = _matrix[ 1 ][ 0 ];
467 _matrix[ 1 ][ 0 ] = temp * cosa - _matrix[ 1 ][ 1 ] * sina;
468 _matrix[ 1 ][ 1 ] = temp * sina + _matrix[ 1 ][ 1 ] * cosa;
469 temp = _matrix[ 2 ][ 0 ];
470 _matrix[ 2 ][ 0 ] = temp * cosa - _matrix[ 2 ][ 1 ] * sina;
471 _matrix[ 2 ][ 1 ] = temp * sina + _matrix[ 2 ][ 1 ] * cosa;
472 temp = _matrix[ 3 ][ 0 ];
473 _matrix[ 3 ][ 0 ] = temp * cosa - _matrix[ 3 ][ 1 ] * sina;
474 _matrix[ 3 ][ 1 ] = temp * sina + _matrix[ 3 ][ 1 ] * cosa;
475
476 break;
477 }
478 }
479}
480//----------------------------------------------------------------------------
481template<class Type>
483 const Vector& centerOfRotation,
484 RotationAxis axis )
485{
486 // First, we must translate the center of rotation to the origin,
487 // perform the rotation and then translate back to the center of
488 // rotation.
489
490 // Find the sine and cosine of the angle of rotation to be used
491 // in the rotation transform matrix.
492 const double a( double( angle ) * DEG_TO_RAD );
493 const Type sina( (Type)sin( a ) );
494 const Type cosa( (Type)cos( a ) );
495
496 Type temp;
497
498 switch( axis )
499 {
500 case X_AXIS: {
501 // The rotation matrix for rotation about the x axis is
502 // |1 0 0 0|
503 // R = |0 cos(a) -sin(a) 0|
504 // |0 sin(a) cos(a) 0|
505 // |0 0 0 1|
506 //
507 // The total transformation T is:
508 // |1 0 0 x-x |
509 // T = |0 R11 R12 (-R11*y-R12*z)+y |
510 // |0 R21 R22 (-R21*y-R22*z)+z |
511 // |0 0 0 1 |
512 const Type T11( cosa ), T12( -sina ), T21( sina ), T22( cosa ),
513 // T03( centerOfRotation.x-centerOfRotation.x ),
514 T13( centerOfRotation.y - ( cosa * centerOfRotation.y - sina * centerOfRotation.z ) ),
515 T23( centerOfRotation.z - ( sina * centerOfRotation.y + cosa * centerOfRotation.z ) );
516
517 // Multiply by the transformation matrix
518 // *this = T * *this;
519 temp = _matrix[ 0 ][ 1 ];
520 _matrix[ 0 ][ 1 ] = temp * T11 + _matrix[ 0 ][ 2 ] * T12 + _matrix[ 0 ][ 3 ] * T13;
521 _matrix[ 0 ][ 2 ] = temp * T21 + _matrix[ 0 ][ 2 ] * T22 + _matrix[ 0 ][ 3 ] * T23;
522 temp = _matrix[ 1 ][ 1 ];
523 _matrix[ 1 ][ 1 ] = temp * T11 + _matrix[ 1 ][ 2 ] * T12 + _matrix[ 1 ][ 3 ] * T13;
524 _matrix[ 1 ][ 2 ] = temp * T21 + _matrix[ 1 ][ 2 ] * T22 + _matrix[ 1 ][ 3 ] * T23;
525 temp = _matrix[ 2 ][ 1 ];
526 _matrix[ 2 ][ 1 ] = temp * T11 + _matrix[ 2 ][ 2 ] * T12 + _matrix[ 2 ][ 3 ] * T13;
527 _matrix[ 2 ][ 2 ] = temp * T21 + _matrix[ 2 ][ 2 ] * T22 + _matrix[ 2 ][ 3 ] * T23;
528 temp = _matrix[ 3 ][ 1 ];
529 _matrix[ 3 ][ 1 ] = temp * T11 + _matrix[ 3 ][ 2 ] * T12 + _matrix[ 3 ][ 3 ] * T13;
530 _matrix[ 3 ][ 2 ] = temp * T21 + _matrix[ 3 ][ 2 ] * T22 + _matrix[ 3 ][ 3 ] * T23;
531
532 break;
533 }
534 case Y_AXIS: {
535 // The rotation matrix for rotation about the y axis is
536 // | cos(a) 0 sin(a) 0|
537 // R = | 0 1 0 0|
538 // |-sin(a) 0 cos(a) 0|
539 // | 0 0 0 1|
540 //
541 // The total transformation T is:
542 // |R00 0 R02 (-R00*x-R02*z)+x |
543 // T = | 0 1 0 y-y |
544 // |R20 0 R22 (-R20*x-R22*z)+z |
545 // | 0 0 0 1 |
546 const Type T00( cosa ), T02( sina ), T20( -sina ), T22( cosa ),
547 T03( centerOfRotation.x - ( sina * centerOfRotation.z + cosa * centerOfRotation.x ) ),
548 // T13( centerOfRotation.y-centerOfRotation.y ),
549 T23( centerOfRotation.z - ( cosa * centerOfRotation.z - sina * centerOfRotation.x ) );
550
551 // Multiply by the transformation matrix
552 // *this = T * *this;
553 temp = _matrix[ 0 ][ 0 ];
554 _matrix[ 0 ][ 0 ] = temp * T00 + _matrix[ 0 ][ 2 ] * T02 + _matrix[ 0 ][ 3 ] * T03;
555 _matrix[ 0 ][ 2 ] = temp * T20 + _matrix[ 0 ][ 2 ] * T22 + _matrix[ 0 ][ 3 ] * T23;
556 temp = _matrix[ 1 ][ 0 ];
557 _matrix[ 1 ][ 0 ] = temp * T00 + _matrix[ 1 ][ 2 ] * T02 + _matrix[ 1 ][ 3 ] * T03;
558 _matrix[ 1 ][ 2 ] = temp * T20 + _matrix[ 1 ][ 2 ] * T22 + _matrix[ 1 ][ 3 ] * T23;
559 temp = _matrix[ 2 ][ 0 ];
560 _matrix[ 2 ][ 0 ] = temp * T00 + _matrix[ 2 ][ 2 ] * T02 + _matrix[ 2 ][ 3 ] * T03;
561 _matrix[ 2 ][ 2 ] = temp * T20 + _matrix[ 2 ][ 2 ] * T22 + _matrix[ 2 ][ 3 ] * T23;
562 temp = _matrix[ 3 ][ 0 ];
563 _matrix[ 3 ][ 0 ] = temp * T00 + _matrix[ 3 ][ 2 ] * T02 + _matrix[ 3 ][ 3 ] * T03;
564 _matrix[ 3 ][ 2 ] = temp * T20 + _matrix[ 3 ][ 2 ] * T22 + _matrix[ 3 ][ 3 ] * T23;
565
566 break;
567 }
568 case Z_AXIS: {
569 // The rotation matrix for rotation about the z axis is
570 // |cos(a) -sin(a) 0 0|
571 // R = |sin(a) cos(a) 0 0|
572 // | 0 0 1 0|
573 // | 0 0 0 1|
574 //
575 // The total transformation T is:
576 // |R00 R01 0 (-R00*x-R01*y)+x |
577 // T = |R10 R11 0 (-R10*x-R11*y)+y |
578 // | 0 0 1 z-z |
579 // | 0 0 0 1 |
580 const Type T00( cosa ), T01( -sina ), T10( sina ), T11( cosa ),
581 T03( centerOfRotation.x - ( cosa * centerOfRotation.x - sina * centerOfRotation.y ) ),
582 T13( centerOfRotation.y - ( sina * centerOfRotation.x + cosa * centerOfRotation.y ) );
583 // T23( centerOfRotation.z-centerOfRotation.z );
584
585 // Multiply by the transformation matrix
586 // *this = T * *this;
587 temp = _matrix[ 0 ][ 0 ];
588 _matrix[ 0 ][ 0 ] = temp * T00 + _matrix[ 0 ][ 1 ] * T01 + _matrix[ 0 ][ 3 ] * T03;
589 _matrix[ 0 ][ 1 ] = temp * T10 + _matrix[ 0 ][ 1 ] * T11 + _matrix[ 0 ][ 3 ] * T13;
590 temp = _matrix[ 1 ][ 0 ];
591 _matrix[ 1 ][ 0 ] = temp * T00 + _matrix[ 1 ][ 1 ] * T01 + _matrix[ 1 ][ 3 ] * T03;
592 _matrix[ 1 ][ 1 ] = temp * T10 + _matrix[ 1 ][ 1 ] * T11 + _matrix[ 1 ][ 3 ] * T13;
593 temp = _matrix[ 2 ][ 0 ];
594 _matrix[ 2 ][ 0 ] = temp * T00 + _matrix[ 2 ][ 1 ] * T01 + _matrix[ 2 ][ 3 ] * T03;
595 _matrix[ 2 ][ 1 ] = temp * T10 + _matrix[ 2 ][ 1 ] * T11 + _matrix[ 2 ][ 3 ] * T13;
596 temp = _matrix[ 3 ][ 0 ];
597 _matrix[ 3 ][ 0 ] = temp * T00 + _matrix[ 3 ][ 1 ] * T01 + _matrix[ 3 ][ 3 ] * T03;
598 _matrix[ 3 ][ 1 ] = temp * T10 + _matrix[ 3 ][ 1 ] * T11 + _matrix[ 3 ][ 3 ] * T13;
599
600 break;
601 }
602 }
603}
604//----------------------------------------------------------------------------
605template<class Type>
607 const Vector& centerOfRotation,
608 const Vector& axis )
609{
610 // Normalize the arbitrary axis into a unit vector to be used in the
611 // calculations for the terms of the rotation matrix. Only perform
612 // the rotation if the length of the axis vector is not zero.
613 Vector u( axis );
614 u.Normalize();
615
616 if( u.MagnitudeSquared() != 0 )
617 {
618 // The following describes the theory used to determine the rotation
619 // matrix for a rotation around an arbitrary axis:
620 //
621 // Definitions:
622 //
623 // The "dual matrix" of a vector u(x,y,z) is S, where
624 // | 0 -z y |
625 // S = | z 0 -x |
626 // |-y x 0 |
627 //
628 // The "outer product" of a vector u with itself (where uT is
629 // the transpose of u) is:
630 // |x| |xx xy xz|
631 // uuT = |y||x y z| = |xy yy yz|
632 // |z| |xz yz zz|
633 //
634 // Suppose we have a unit direction vector, u, then we can derive
635 // a rotation by a given angle about this axis:
636 //
637 // R(a,u) = uuT + (I - uuT) * cos(a) + S * sin(a)
638 //
639 // where I is the identity matrix. This actually produces a 3x3
640 // matrix which we just need to add the 4th column and row to so
641 // that we can use it for affine transformations.
642
643 // Calculate some constants from uuT
644 const Type xx( Type( u.x * u.x ) );
645 const Type yy( Type( u.y * u.y ) );
646 const Type zz( Type( u.z * u.z ) );
647 const Type xy( Type( u.x * u.y ) );
648 const Type xz( Type( u.x * u.z ) );
649 const Type yz( Type( u.y * u.z ) );
650
651 // Calculate the sine and cosine of the rotation angle for use
652 // in the calculation of the rotation matrix.
653 const double a( double( angle ) * DEG_TO_RAD );
654 const Type sina( (Type)sin( a ) );
655 const Type cosa( (Type)cos( a ) );
656
657 // Now calculate M, which is the 3x3 version of the rotation
658 // matrix using the formula for rotation about an arbitrary axis.
659 // GlsMatrix<Type,3> M = UUT + ((I - UUT) * cosa) + (S * sina);
660
661 // Assign M to the first 3 columns and rows of the final rotation
662 // matrix R.
664
665 R( 0, 0 ) = xx + ( 1 - xx ) * cosa;
666 R( 1, 0 ) = xy - xy * cosa + u.z * sina;
667 R( 2, 0 ) = xz - xz * cosa - u.y * sina;
668
669 R( 0, 1 ) = xy - xy * cosa - u.z * sina;
670 R( 1, 1 ) = yy + ( 1 - yy ) * cosa;
671 R( 2, 1 ) = yz - yz * cosa + u.x * sina;
672
673 R( 0, 2 ) = xz - xz * cosa + u.y * sina;
674 R( 1, 2 ) = yz - yz * cosa - u.x * sina;
675 R( 2, 2 ) = zz + ( 1 - zz ) * cosa;
676
677 // Multiply the current matrix by the translation, then the rotation,
678 // and then translate back using the inverse.
679 // *this = TInv * R * T * *this;
680 this->Translate( -centerOfRotation.x, -centerOfRotation.y, -centerOfRotation.z );
681 *this = R * *this;
682 this->Translate( centerOfRotation.x, centerOfRotation.y, centerOfRotation.z );
683 }
684}
685//----------------------------------------------------------------------------
686template<class Type>
687void GlsMatrixAffine<Type>::Scale( Type x, Type y, Type z )
688{
689 // The scaling matrix, where x, y, and z are on the diagonal.
690 // x 0 0 0
691 // 0 y 0 0
692 // 0 0 z 0
693 // 0 0 0 1
694 // As can be seen most of the matrix is zero
695 // So optimized
696
697 _matrix[ 0 ][ 0 ] = _matrix[ 0 ][ 0 ] * x;
698 _matrix[ 0 ][ 1 ] = _matrix[ 0 ][ 1 ] * y;
699 _matrix[ 0 ][ 2 ] = _matrix[ 0 ][ 2 ] * z;
700 //_matrix[0][3] = _matrix[0][3];
701
702 _matrix[ 1 ][ 0 ] = _matrix[ 1 ][ 0 ] * x;
703 _matrix[ 1 ][ 1 ] = _matrix[ 1 ][ 1 ] * y;
704 _matrix[ 1 ][ 2 ] = _matrix[ 1 ][ 2 ] * z;
705 //_matrix[1][3] = _matrix[1][3];
706
707 _matrix[ 2 ][ 0 ] = _matrix[ 2 ][ 0 ] * x;
708 _matrix[ 2 ][ 1 ] = _matrix[ 2 ][ 1 ] * y;
709 _matrix[ 2 ][ 2 ] = _matrix[ 2 ][ 2 ] * z;
710 //_matrix[2][3] = _matrix[2][3];
711
712 _matrix[ 3 ][ 0 ] = _matrix[ 3 ][ 0 ] * x;
713 _matrix[ 3 ][ 1 ] = _matrix[ 3 ][ 1 ] * y;
714 _matrix[ 3 ][ 2 ] = _matrix[ 3 ][ 2 ] * z;
715 //_matrix[3][3] = _matrix[3][3];
716
717 // Row 3 is multiplied by 1, so no need...
718}
719//----------------------------------------------------------------------------
720template<class Type>
721void GlsMatrixAffine<Type>::Scale( Type x, Type y, Type z, const Vector& anchor )
722{
723 // First, we must translate the anchor point to the origin,
724 // perform the scaling and then translate back to the anchor
725 // point. This is the transformation matrix we are multiplying by:
726 // (Ax, Ay, Az are the components of anchor)
727 // | x 0 0 Ax-x*Ax |
728 // T = | 0 y 0 Ay-y*Ay |
729 // | 0 0 z Az-z*Az |
730 // | 0 0 0 1 |
731 const Type T00 = x;
732 const Type T11 = y;
733 const Type T22 = z;
734 const Type T03 = anchor.x * ( 1 - x );
735 const Type T13 = anchor.y * ( 1 - y );
736 const Type T23 = anchor.z * ( 1 - z );
737
738 // Multiply the current matrix by the transformation
739 // *this = T * *this;
740
741 _matrix[ 0 ][ 0 ] = _matrix[ 0 ][ 0 ] * T00 + _matrix[ 0 ][ 3 ] * T03;
742 _matrix[ 0 ][ 1 ] = _matrix[ 0 ][ 1 ] * T11 + _matrix[ 0 ][ 3 ] * T13;
743 _matrix[ 0 ][ 2 ] = _matrix[ 0 ][ 2 ] * T22 + _matrix[ 0 ][ 3 ] * T23;
744 //_matrix[0][3] = _matrix[0][3];
745
746 _matrix[ 1 ][ 0 ] = _matrix[ 1 ][ 0 ] * T00 + _matrix[ 1 ][ 3 ] * T03;
747 _matrix[ 1 ][ 1 ] = _matrix[ 1 ][ 1 ] * T11 + _matrix[ 1 ][ 3 ] * T13;
748 _matrix[ 1 ][ 2 ] = _matrix[ 1 ][ 2 ] * T22 + _matrix[ 1 ][ 3 ] * T23;
749 //_matrix[1][3] = _matrix[1][3];
750
751 _matrix[ 2 ][ 0 ] = _matrix[ 2 ][ 0 ] * T00 + _matrix[ 2 ][ 3 ] * T03;
752 _matrix[ 2 ][ 1 ] = _matrix[ 2 ][ 1 ] * T11 + _matrix[ 2 ][ 3 ] * T13;
753 _matrix[ 2 ][ 2 ] = _matrix[ 2 ][ 2 ] * T22 + _matrix[ 2 ][ 3 ] * T23;
754 //_matrix[2][3] = _matrix[2][3];
755
756 _matrix[ 3 ][ 0 ] = _matrix[ 3 ][ 0 ] * T00 + _matrix[ 3 ][ 3 ] * T03;
757 _matrix[ 3 ][ 1 ] = _matrix[ 3 ][ 1 ] * T11 + _matrix[ 3 ][ 3 ] * T13;
758 _matrix[ 3 ][ 2 ] = _matrix[ 3 ][ 2 ] * T22 + _matrix[ 3 ][ 3 ] * T23;
759 //_matrix[3][3] = _matrix[3][3];
760}
761
762/// Typedef for a generic (float or double) matrix element.
763#ifdef MATRIX_TYPE_FLOAT
764typedef float GlsMatrixElementType;
765#else
766typedef double GlsMatrixElementType;
767#endif
768
769/// Typedef for a generic (float or double elements) matrix.
770#ifdef MATRIX_TYPE_FLOAT
772#else
774#endif
775
776/// Macro to call the appropriate glLoadMatrix call whether we are using floats or doubles.
777#ifdef MATRIX_TYPE_FLOAT
778# define GLS_glLoadMatrix_TYPE glLoadMatrixf
779#else
780# define GLS_glLoadMatrix_TYPE glLoadMatrixd
781#endif
782
783/// Macro to call the appropriate glMultMatrix call whether we are using floats or doubles.
784#ifdef MATRIX_TYPE_FLOAT
785# define GLS_glMultMatrix_TYPE glMultMatrixf
786#else
787# define GLS_glMultMatrix_TYPE glMultMatrixd
788#endif
789
790} // namespace disti
791
792#endif
unsigned char G() const
Definition: gls_color.h:217
unsigned char R() const
Definition: gls_color.h:214
unsigned char B() const
Definition: gls_color.h:220
unsigned char A() const
Definition: gls_color.h:223
Definition: gls_matrix_affine.h:60
void Rotate(Type angle, RotationAxis axis)
Definition: gls_matrix_affine.h:391
void Scale(Type x, Type y, Type z)
Definition: gls_matrix_affine.h:687
void Rotate(Type angle, const Vector &centerOfRotation, RotationAxis axis)
Definition: gls_matrix_affine.h:482
void Scale(Type x, Type y, Type z, const Vector &anchor)
Definition: gls_matrix_affine.h:721
GlsMatrixAffine()
Definition: gls_matrix_affine.h:362
void Rotate(Type angle, const Vector &centerOfRotation, const Vector &axis)
Definition: gls_matrix_affine.h:606
void Translate(const Vector &v)
Definition: gls_matrix_affine.h:239
Vertex operator*(const Vertex &v) const
Definition: gls_matrix_affine.h:257
Vector operator*(const Vector &v) const
Definition: gls_matrix_affine.h:246
void Transform(Vector &v) const
Definition: gls_matrix_affine.h:272
~GlsMatrixAffine()
Definition: gls_matrix_affine.h:385
void Translate(Type x, Type y, Type z)
Definition: gls_matrix_affine.h:204
bool IsVeryClose(const GlsMatrix< Type, 4 > &b) const
Definition: gls_matrix_affine.h:116
Definition: gls_matrix.h:61
Type * Data()
Definition: gls_matrix.h:127
static const double PRECISION
Definition: gls_matrix.h:367
Type * _matrix[DIM]
Definition: gls_matrix.h:388
Type _data[DIM *DIM]
Definition: gls_matrix.h:376
Definition: gls_quaternion.h:55
Definition: vertex.h:84
float MagnitudeSquared() const
Definition: vertex.h:337
float y
The Y component.
Definition: vertex.h:87
float x
The X component.
Definition: vertex.h:86
float Magnitude() const
Definition: vertex.h:329
void Normalize()
Definition: vertex.h:310
float z
The Z component.
Definition: vertex.h:88
Definition: vertex.h:419
GlsColor color
The RGBA color component.
Definition: vertex.h:421
#define DISTI_DEPRECATED(msg)
Defines whether this compiler supports the C++14 deprecated attribute.
Definition: gls_cpp_lang_support.h:487
The GlsMatrix class.
The disti::GlsQuaternion class.
Definition: bmpimage.h:46
GlsMatrix< Type, 4 > DecomposeRotationMatrix(const GlsMatrix< Type, 4 > &matrix, const Vector &scale)
Definition: gls_matrix_affine.h:307
GlsMatrixAffine< double > GlsMatrixAffineD
Typedef for a matrix of doubles.
Definition: gls_matrix_affine.h:352
Vector DecomposeScale(const GlsMatrix< Type, 4 > &m)
Definition: gls_matrix_affine.h:292
RotationAxis
Definition: vertex.h:73
@ Z_AXIS
Definition: vertex.h:76
@ X_AXIS
Definition: vertex.h:74
@ Y_AXIS
Definition: vertex.h:75
const double DEG_TO_RAD
Definition: vertex.h:60
VertexNoColor Vector
Definition: gls_font_base.h:68
void DecomposeMatrix(const GlsMatrix< Type, 4 > &matrix, Vector &scale, Vector &location, GlsQuaternion< Type > &rotation)
Definition: gls_matrix_affine.h:341
GlsMatrixAffine< float > GlsMatrixAffineF
Typedef for a matrix of floats.
Definition: gls_matrix_affine.h:351
GlsMatrixAffineD GlsMatrixType
Typedef for a generic (float or double elements) matrix.
Definition: gls_matrix_affine.h:773
double GlsMatrixElementType
Typedef for a generic (float or double) matrix element.
Definition: gls_matrix_affine.h:766
The disti::Vertex class. A class for manipulating 3D vertices.