CFx SDK Documentation  2023 SP0
GeQuaternion.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2017, Open Design Alliance (the "Alliance").
3 // All rights reserved.
4 //
5 // This software and its documentation and related materials are owned by
6 // the Alliance. The software may only be incorporated into application
7 // programs owned by members of the Alliance, subject to a signed
8 // Membership Agreement and Supplemental Software License Agreement with the
9 // Alliance. The structure and organization of this software are the valuable
10 // trade secrets of the Alliance and its suppliers. The software is also
11 // protected by copyright law and international treaty provisions. Application
12 // programs incorporating this software must include the following statement
13 // with their copyright notices:
14 //
15 // This application incorporates Teigha(R) software pursuant to a license
16 // agreement with Open Design Alliance.
17 // Teigha(R) Copyright (C) 2002-2017 by Open Design Alliance.
18 // All rights reserved.
19 //
20 // By use of this software, its documentation or related materials, you
21 // acknowledge and accept the above terms.
23 
24 
25 
26 #ifndef OD_GEQUATERNION_H
27 #define OD_GEQUATERNION_H
29 #include "Ge/GeExport.h"
30 #include "Ge/GeMatrix3d.h"
31 
32 
33 #include "TD_PackPush.h"
34 
46 {
47 public:
48 
58  : w( 1. ), x( 0. ), y( 0. ), z( 0. )
59  {
60  }
61  OdGeQuaternion (double ww, double xx, double yy, double zz)
62  : w(ww), x(xx), y(yy), z(zz)
63  {
64  }
65 
66  OdGeQuaternion& set (double ww, double xx, double yy, double zz)
67  {
68  w = ww;
69  x = xx;
70  y = yy;
71  z = zz;
72  return *this;
73  }
74 
75  OdGeQuaternion& set( const OdGeMatrix3d& matrix )
76  {
77  double trace = matrix[0][0] + matrix[1][1] + matrix[2][2];
78  if ( trace > 0.0 )
79  {
80  w = sqrt(1.0 + trace) / 2.0;
81 
82  double w4 = (4.0 * w);
83  x = (matrix[1][2] - matrix[2][1]) / w4;
84  y = (matrix[2][0] - matrix[0][2]) / w4;
85  z = (matrix[0][1] - matrix[1][0]) / w4;
86  }
87  else
88  {
89  OdUInt32 i = 0;
90  if (matrix[1][1] > matrix[0][0]) i = 1;
91  if (matrix[2][2] > matrix[i][i]) i = 2;
92  OdUInt32 j = (2 == i ? 0 : i + 1);
93  OdUInt32 k = (2 == j ? 0 : j + 1);
94 
95  double s = sqrt((matrix[i][i] - (matrix[j][j] + matrix[k][k])) + 1.0);
96  double q[ 4 ];
97 
98  q[i] = s * 0.5;
99  if (s != 0.0) s = 0.5/s;
100 
101  q[3] = (matrix[j][k] - matrix[k][j]) * s;
102  q[j] = (matrix[i][j] + matrix[j][i]) * s;
103  q[k] = (matrix[i][k] + matrix[k][i]) * s;
104 
105  x = q[0];
106  y = q[1];
107  z = q[2];
108  w = q[3];
109  }
110  return( *this );
111  }
112 
114  {
115  OdGeMatrix3d matrix;
116 
117  matrix( 0, 0 ) = w * w + x * x - y * y - z * z;
118  matrix( 1, 0 ) = 2. * ( x * y - w * z );
119  matrix( 2, 0 ) = 2. * ( w * y + x * z );
120 
121  matrix( 0, 1 ) = 2. * ( w * z + x * y );
122  matrix( 1, 1 ) = w * w - x * x + y * y - z * z;
123  matrix( 2, 1 ) = 2. * ( y * z - w * x );
124 
125  matrix( 0, 2 ) = 2. * ( x * z - w * y );
126  matrix( 1, 2 ) = 2. * ( w * x + y * z );
127  matrix( 2, 2 ) = w * w - x * x - y * y + z * z;
128 
129  return matrix;
130  }
131 
132  OdGePoint3d rotate( OdGePoint3d& sourcePoint ) const
133  {
134  OdGeMatrix3d matrix = getMatrix();
135 
136  return sourcePoint.transformBy( matrix );
137  }
138 
140  {
141  OdGeMatrix3d matrix = getMatrix();
142 
143  return vector.transformBy( matrix );
144  }
145 
153  bool isEqualTo(
154  const OdGeQuaternion& quat,
155  const OdGeTol& tol = OdGeContext::gTol) const
156  {
157  return fabs(x - quat.x) <= tol.equalVector() &&
158  fabs(y - quat.y) <= tol.equalVector() &&
159  fabs(z - quat.z) <= tol.equalVector() &&
160  fabs(w - quat.w) <= tol.equalVector();
161  }
162 
163  bool operator == (const OdGeQuaternion& quat) const
164  {
165  return isEqualTo(quat);
166  }
167  bool operator != (const OdGeQuaternion& quat) const
168  {
169  return !isEqualTo(quat);
170  }
171 
172  double w;
173  double x;
174  double y;
175  double z;
176 
178 
179 };
180 
181 #include "TD_PackPop.h"
182 
183 
184 #endif // OD_GEQUATERNION_H
tol
Definition: DimVarDefs.h:2287
#define GE_TOOLKIT_EXPORT
Definition: GeExport.h:49
#define GE_STATIC_EXPORT
Definition: GeExport.h:53
unsigned int OdUInt32
OdGePoint3d & transformBy(const OdGeMatrix3d &xfm)
static GE_STATIC_EXPORT const OdGeQuaternion kOrigin
Definition: GeQuaternion.h:177
OdGeQuaternion & set(const OdGeMatrix3d &matrix)
Definition: GeQuaternion.h:75
OdGeMatrix3d getMatrix() const
Definition: GeQuaternion.h:113
OdGeQuaternion(double ww, double xx, double yy, double zz)
Definition: GeQuaternion.h:61
OdGeQuaternion & set(double ww, double xx, double yy, double zz)
Definition: GeQuaternion.h:66
OdGePoint3d rotate(OdGePoint3d &sourcePoint) const
Definition: GeQuaternion.h:132
bool isEqualTo(const OdGeQuaternion &quat, const OdGeTol &tol=OdGeContext::gTol) const
Definition: GeQuaternion.h:153
OdGeVector3d rotate(OdGeVector3d &vector) const
Definition: GeQuaternion.h:139
Definition: GeTol.h:49
OdGeVector3d & transformBy(const OdGeMatrix3d &xfm)
GLfloat GLfloat GLfloat z
Definition: gles2_ext.h:318
GLfloat GLfloat GLfloat GLfloat w
Definition: gles2_ext.h:320
GLfloat x
Definition: gles2_ext.h:314
GLfloat GLfloat y
Definition: gles2_ext.h:316
bool operator==(const BlockRefPath &rA, const BlockRefPath &rB)
DOM.
bool operator!=(const BlockRefPath &rA, const BlockRefPath &rB)
DOM.
static GE_STATIC_EXPORT OdGeTol gTol
Definition: GeGbl.h:60