39#ifndef GLS_QUATERNION_H
40#define GLS_QUATERNION_H
111 Type angle2,
const Vector& axis2,
112 Type angle3,
const Vector& axis3 )
184 return _v[ 0 ] == q.
_v[ 0 ] &&
_v[ 1 ] == q.
_v[ 1 ] &&
_v[ 2 ] == q.
_v[ 2 ] &&
_v[ 3 ] == q.
_v[ 3 ];
196 return _v[ 0 ] != q.
_v[ 0 ] ||
_v[ 1 ] != q.
_v[ 1 ] ||
_v[ 2 ] != q.
_v[ 2 ] ||
_v[ 3 ] != q.
_v[ 3 ];
208 if(
_v[ 0 ] < q.
_v[ 0 ] )
210 else if(
_v[ 0 ] > q.
_v[ 0 ] )
212 else if(
_v[ 1 ] < q.
_v[ 1 ] )
214 else if(
_v[ 1 ] > q.
_v[ 1 ] )
216 else if(
_v[ 2 ] < q.
_v[ 2 ] )
218 else if(
_v[ 2 ] > q.
_v[ 2 ] )
221 return (
_v[ 3 ] < q.
_v[ 3 ] );
258 q.
_v[ 3 ] *
_v[ 1 ] + q.
_v[ 0 ] *
_v[ 2 ] + q.
_v[ 1 ] *
_v[ 3 ] - q.
_v[ 2 ] *
_v[ 0 ],
259 q.
_v[ 3 ] *
_v[ 2 ] - q.
_v[ 0 ] *
_v[ 1 ] + q.
_v[ 1 ] *
_v[ 0 ] + q.
_v[ 2 ] *
_v[ 3 ],
260 q.
_v[ 3 ] *
_v[ 3 ] - q.
_v[ 0 ] *
_v[ 0 ] - q.
_v[ 1 ] *
_v[ 1 ] - q.
_v[ 2 ] *
_v[ 2 ] );
268 Type x = q.
_v[ 3 ] *
_v[ 0 ] + q.
_v[ 0 ] *
_v[ 3 ] - q.
_v[ 1 ] *
_v[ 2 ] + q.
_v[ 2 ] *
_v[ 1 ];
269 Type y = q.
_v[ 3 ] *
_v[ 1 ] + q.
_v[ 0 ] *
_v[ 2 ] + q.
_v[ 1 ] *
_v[ 3 ] - q.
_v[ 2 ] *
_v[ 0 ];
270 Type z = q.
_v[ 3 ] *
_v[ 2 ] - q.
_v[ 0 ] *
_v[ 1 ] + q.
_v[ 1 ] *
_v[ 0 ] + q.
_v[ 2 ] *
_v[ 3 ];
271 _v[ 3 ] = q.
_v[ 3 ] *
_v[ 3 ] - q.
_v[ 0 ] *
_v[ 0 ] - q.
_v[ 1 ] *
_v[ 1 ] - q.
_v[ 2 ] *
_v[ 2 ];
289 Type factor = 1.0 / s;
319 return ( ( *
this ) * q.
Inverse() );
331 ( *this ) = ( *this ) * q.
Inverse();
345 _v[ 2 ] + q.
_v[ 2 ],
_v[ 3 ] + q.
_v[ 3 ] );
357 _v[ 0 ] += q.
_v[ 0 ];
358 _v[ 1 ] += q.
_v[ 1 ];
359 _v[ 2 ] += q.
_v[ 2 ];
360 _v[ 3 ] += q.
_v[ 3 ];
374 _v[ 2 ] - q.
_v[ 2 ],
_v[ 3 ] - q.
_v[ 3 ] );
386 _v[ 0 ] -= q.
_v[ 0 ];
387 _v[ 1 ] -= q.
_v[ 1 ];
388 _v[ 2 ] -= q.
_v[ 2 ];
389 _v[ 3 ] -= q.
_v[ 3 ];
409 return sqrt(
_v[ 0 ] *
_v[ 0 ] +
_v[ 1 ] *
_v[ 1 ] +
_v[ 2 ] *
_v[ 2 ] +
_v[ 3 ] *
_v[ 3 ] );
420 return _v[ 0 ] *
_v[ 0 ] +
_v[ 1 ] *
_v[ 1 ] +
_v[ 2 ] *
_v[ 2 ] +
_v[ 3 ] *
_v[ 3 ];
454 return ( (
_v[ 0 ] * q.
_v[ 0 ] ) + (
_v[ 1 ] * q.
_v[ 1 ] ) + (
_v[ 2 ] * q.
_v[ 2 ] ) + (
_v[ 3 ] * q.
_v[ 3 ] ) );
476 void Set( Type x, Type y, Type z, Type w )
493 Type half_angle = angle * (Type)0.5;
494 Type sin_half_angle = sin( half_angle );
496 _v[ 0 ] = sin_half_angle * axis.
x;
497 _v[ 1 ] = sin_half_angle * axis.
y;
498 _v[ 2 ] = sin_half_angle * axis.
z;
499 _v[ 3 ] = cos( half_angle );
514 Type angle2,
const Vector& axis2,
515 Type angle3,
const Vector& axis3 )
521 *
this = q1 * q2 * *
this;
537 Type xOver2 = angleX * (Type)0.5;
538 Type yOver2 = angleY * (Type)0.5;
539 Type zOver2 = angleZ * (Type)0.5;
542 qx.
_v[ 0 ] = sin( xOver2 );
543 qx.
_v[ 1 ] = (Type)0.0;
544 qx.
_v[ 2 ] = (Type)0.0;
545 qx.
_v[ 3 ] = cos( xOver2 );
548 qy.
_v[ 0 ] = (Type)0.0;
549 qy.
_v[ 1 ] = sin( yOver2 );
550 qy.
_v[ 2 ] = (Type)0.0;
551 qy.
_v[ 3 ] = cos( yOver2 );
554 qz.
_v[ 0 ] = (Type)0.0;
555 qz.
_v[ 1 ] = (Type)0.0;
556 qz.
_v[ 2 ] = sin( zOver2 );
557 qz.
_v[ 3 ] = cos( zOver2 );
560 *
this = qx * qy * qz;
589 if( fabs( from.
x ) < fabs( from.
y ) )
590 if( fabs( from.
x ) < fabs( from.
z ) )
591 tmp =
Vector( 1.0, 0.0, 0.0 );
593 tmp =
Vector( 0.0, 0.0, 1.0 );
594 else if( fabs( from.
y ) < fabs( from.
z ) )
595 tmp =
Vector( 0.0, 1.0, 0.0 );
597 tmp =
Vector( 0.0, 0.0, 1.0 );
614 Type angle = acos( cosangle );
632 int nxt[ 3 ] = { 1, 2, 0 };
634 tr = mat[ 0 * 4 + 0 ] + mat[ 1 * 4 + 1 ] + mat[ 2 * 4 + 2 ];
639 s = (Type)sqrt( tr + 1.0 );
642 _v[ 0 ] = ( mat[ 1 * 4 + 2 ] - mat[ 2 * 4 + 1 ] ) * s;
643 _v[ 1 ] = ( mat[ 2 * 4 + 0 ] - mat[ 0 * 4 + 2 ] ) * s;
644 _v[ 2 ] = ( mat[ 0 * 4 + 1 ] - mat[ 1 * 4 + 0 ] ) * s;
650 if( mat[ 1 * 4 + 1 ] > mat[ 0 * 4 + 0 ] )
652 if( mat[ 2 * 4 + 2 ] > mat[ i * 4 + i ] )
657 s = (Type)sqrt( ( mat[ i * 4 + i ] - ( mat[ j * 4 + j ] + mat[ k * 4 + k ] ) ) + 1.0 );
664 tq[ 3 ] = ( mat[ j * 4 + k ] - mat[ k * 4 + j ] ) * s;
665 tq[ j ] = ( mat[ i * 4 + j ] + mat[ j * 4 + i ] ) * s;
666 tq[ k ] = ( mat[ i * 4 + k ] + mat[ k * 4 + i ] ) * s;
680 return atan2( 2 * (
_v[ 1 ] *
_v[ 2 ] +
_v[ 3 ] *
_v[ 0 ] ),
_v[ 3 ] *
_v[ 3 ] -
_v[ 0 ] *
_v[ 0 ] -
_v[ 1 ] *
_v[ 1 ] +
_v[ 2 ] *
_v[ 2 ] );
688 Type tmp = -2 * (
_v[ 0 ] *
_v[ 2 ] -
_v[ 3 ] *
_v[ 1 ] );
694 else if( tmp < -1.0 )
706 return atan2( 2 * (
_v[ 0 ] *
_v[ 1 ] +
_v[ 3 ] *
_v[ 2 ] ),
_v[ 3 ] *
_v[ 3 ] +
_v[ 0 ] *
_v[ 0 ] -
_v[ 1 ] *
_v[ 1 ] -
_v[ 2 ] *
_v[ 2 ] );
726 if( abs(
_v[ 3 ] ) > (Type)1.0 )
728 if( fabs( (
double)
_v[ 3 ] ) > 1.0 )
735 Type rad = acos(
_v[ 3 ] ) * (Type)2.0;
745 Type sin_half_angle = sin( rad * (Type)0.5 );
746 if( sin_half_angle >= (Type)0.0001 )
748 Type sin_half_angle_inv = Type( 1.0 ) / sin_half_angle;
749 axis->
x = float(
_v[ 0 ] * sin_half_angle_inv );
750 axis->
y = float(
_v[ 1 ] * sin_half_angle_inv );
751 axis->
z = float(
_v[ 2 ] * sin_half_angle_inv );
761 axis->
x = (float)1.0;
762 axis->
y = (float)0.0;
763 axis->
z = (float)0.0;
773 Type wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
776 x2 =
_v[ 0 ] +
_v[ 0 ];
777 y2 =
_v[ 1 ] +
_v[ 1 ];
778 z2 =
_v[ 2 ] +
_v[ 2 ];
792 mat[ 0 ][ 0 ] = 1.0f - ( yy + zz );
793 mat[ 0 ][ 1 ] = xy - wz;
794 mat[ 0 ][ 2 ] = xz + wy;
795 mat[ 0 ][ 3 ] = 0.0f;
797 mat[ 1 ][ 0 ] = xy + wz;
798 mat[ 1 ][ 1 ] = 1.0f - ( xx + zz );
799 mat[ 1 ][ 2 ] = yz - wx;
800 mat[ 1 ][ 3 ] = 0.0f;
802 mat[ 2 ][ 0 ] = xz - wy;
803 mat[ 2 ][ 1 ] = yz + wx;
804 mat[ 2 ][ 2 ] = 1.0f - ( xx + yy );
805 mat[ 2 ][ 3 ] = 0.0f;
831 if( adjustSign && ( cosom < (Type)0.0 ) )
834 q.
_v[ 0 ] = -to.
_v[ 0 ];
835 q.
_v[ 1 ] = -to.
_v[ 1 ];
836 q.
_v[ 2 ] = -to.
_v[ 2 ];
837 q.
_v[ 3 ] = -to.
_v[ 3 ];
846 if( ( (Type)1.0 - cosom ) > (Type)0.0001 )
850 omega = acos( cosom );
851 sinom = sin( omega );
852 sclp = sin( ( (Type)1.0 - t ) * omega ) / sinom;
853 sclq = sin( t * omega ) / sinom;
858 sclp = (Type)1.0 - t;
862 _v[ 0 ] = sclp * p.
_v[ 0 ] + sclq * q.
_v[ 0 ];
863 _v[ 1 ] = sclp * p.
_v[ 1 ] + sclq * q.
_v[ 1 ];
864 _v[ 2 ] = sclp * p.
_v[ 2 ] + sclq * q.
_v[ 2 ];
865 _v[ 3 ] = sclp * p.
_v[ 3 ] + sclq * q.
_v[ 3 ];
890 outstr << quat.
Data()[ 0 ] <<
" ";
892 outstr << quat.
Data()[ 1 ] <<
" ";
894 outstr << quat.
Data()[ 2 ] <<
" ";
896 outstr << quat.
Data()[ 3 ];
908 instr >> quat.
Data()[ 0 ] >> quat.
Data()[ 1 ] >> quat.
Data()[ 2 ] >> quat.
Data()[ 3 ];
Definition: gls_matrix_affine.h:60
Definition: gls_quaternion.h:55
void Set(Type x, Type y, Type z, Type w)
Definition: gls_quaternion.h:476
Type GetRoll() const
Definition: gls_quaternion.h:678
bool operator==(const GlsQuaternion &q) const
Definition: gls_quaternion.h:182
void SetFromSlerp(float t, const GlsQuaternion &from, const GlsQuaternion &to, bool adjustSign=true)
Definition: gls_quaternion.h:822
void SetFromVectors(const Vector &to, const Vector &from)
Definition: gls_quaternion.h:572
GlsQuaternion & operator+=(const GlsQuaternion &q)
Definition: gls_quaternion.h:355
GlsQuaternion(Type angle, const Vector &axis)
Definition: gls_quaternion.h:94
GlsQuaternion & operator/=(const GlsQuaternion &q)
Definition: gls_quaternion.h:329
static bool IsVeryCloseToZero(double value)
Definition: gls_quaternion.h:874
const GlsQuaternion operator/(const GlsQuaternion &q) const
Definition: gls_quaternion.h:317
GlsQuaternion & operator-=(const GlsQuaternion &q)
Definition: gls_quaternion.h:384
GlsQuaternion & operator/=(Type s)
Definition: gls_quaternion.h:300
Type * Data()
Definition: gls_quaternion.h:159
GlsQuaternion operator/(Type s) const
Definition: gls_quaternion.h:287
void SetFromAngleAxis(Type angle1, const Vector &axis1, Type angle2, const Vector &axis2, Type angle3, const Vector &axis3)
Definition: gls_quaternion.h:513
Type _v[4]
The four components of the quaternion.
Definition: gls_quaternion.h:869
const GlsQuaternion operator+(const GlsQuaternion &q) const
Definition: gls_quaternion.h:342
const Type * Data() const
Definition: gls_quaternion.h:170
void GetAngleAxis(Type *angle, Vector *axis)
Definition: gls_quaternion.h:722
const GlsQuaternion Inverse() const
Definition: gls_quaternion.h:440
Type GetYaw() const
Definition: gls_quaternion.h:704
Type MagnitudeSquared() const
Definition: gls_quaternion.h:418
GlsQuaternion(Type x, Type y, Type z, Type w)
Definition: gls_quaternion.h:79
void SetFromEulerAngles(Type angleX, Type angleY, Type angleZ)
Definition: gls_quaternion.h:532
GlsQuaternion(const GlsMatrixAffine< Type > &matrix)
Definition: gls_quaternion.h:148
GlsQuaternion & operator*=(const GlsQuaternion &q)
Definition: gls_quaternion.h:266
const GlsQuaternion operator*(const GlsQuaternion &q) const
Definition: gls_quaternion.h:255
const GlsQuaternion operator*(Type s) const
Definition: gls_quaternion.h:231
GlsQuaternion(const Vector &anglesVec)
Definition: gls_quaternion.h:137
Vector GetEulerAngles()
Definition: gls_quaternion.h:712
void SetFromRotationMatrix(const GlsMatrixAffine< Type > &matrix)
Definition: gls_quaternion.h:625
GlsQuaternion(Type angleX, Type angleY, Type angleZ)
Definition: gls_quaternion.h:125
GlsQuaternion(Type angle1, const Vector &axis1, Type angle2, const Vector &axis2, Type angle3, const Vector &axis3)
Definition: gls_quaternion.h:110
bool operator!=(const GlsQuaternion &q) const
Definition: gls_quaternion.h:194
const GlsQuaternion operator-() const
Definition: gls_quaternion.h:396
void SetFromAngleAxis(Type angle, const Vector &axis)
Definition: gls_quaternion.h:491
GlsQuaternion()
Definition: gls_quaternion.h:62
Type GetPitch() const
Definition: gls_quaternion.h:686
GlsQuaternion Conjugate() const
Definition: gls_quaternion.h:429
GlsQuaternion & operator*=(Type s)
Definition: gls_quaternion.h:243
Type DotProduct(const GlsQuaternion &q) const
Definition: gls_quaternion.h:452
Type Magnitude() const
Definition: gls_quaternion.h:407
GlsMatrixAffine< Type > GetRotationMatrix()
Definition: gls_quaternion.h:771
void Normalize()
Definition: gls_quaternion.h:462
bool operator<(const GlsQuaternion &q) const
Definition: gls_quaternion.h:206
const GlsQuaternion operator-(const GlsQuaternion &q) const
Definition: gls_quaternion.h:371
VertexNoColor CrossProduct(const VertexNoColor &w) const
Definition: vertex.h:348
float DotProduct(const VertexNoColor &w) const
Definition: vertex.h:359
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
Macros and helper code to determine what subset of C++11/14/17 is available.
Definition: bmpimage.h:46
GlsQuaternion< float > GlsQuaternionF
Typedef for a quaternion of floats.
Definition: gls_quaternion.h:877
std::ostream & operator<<(std::ostream &outstr, const AttributeName &name)
GlsQuaternion< double > GlsQuaternionD
Typedef for a quaternion of doubles.
Definition: gls_quaternion.h:878
VertexNoColor Vector
Definition: gls_font_base.h:68
std::istream & operator>>(std::istream &instr, GlsColor &color)
Definition: gls_cpp_lang_support.h:498
The disti::Vertex class. A class for manipulating 3D vertices.