QCAD
Open Source 2D CAD
Loading...
Searching...
No Matches
opennurbs_quaternion.h
Go to the documentation of this file.
1/* $NoKeywords: $ */
2/*
3//
4// Copyright (c) 1993-2009 Robert McNeel & Associates. All rights reserved.
5// Rhinoceros is a registered trademark of Robert McNeel & Assoicates.
6//
7// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
8// ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
9// MERCHANTABILITY ARE HEREBY DISCLAIMED.
10//
11// For complete openNURBS copyright information see <http://www.opennurbs.org>.
12//
14*/
15
16#if !defined(ON_QUATERNION_INC_)
17#define ON_QUATERNION_INC_
18
20{
21public:
22 // quaternion = a + bi + cj + dk
23 double a,b,c,d;
24
25 static const ON_Quaternion Zero; // 0 = (0,0,0,0
26 static const ON_Quaternion Identity; // 1 = (1,0,0,0)
27 static const ON_Quaternion I; // "i" = (0,1,0,0)
28 static const ON_Quaternion J; // "j" = (0,0,1,0)
29 static const ON_Quaternion K; // "k" = (0,0,0,1)
30
32
33 ON_Quaternion(double qa, double qb, double qc, double qd);
34
35 // (a,b,c,d) = (0,v.x,v.y,v.z)
36 ON_Quaternion(const ON_3dVector& v);
37
38 // (a,b,c,d) = (0,v.x,v.y,v.z)
39 ON_Quaternion& operator=(const ON_3dVector& v);
40
41 void Set(double qa, double qb, double qc, double qd);
42
43 // arithmetic operators
44 ON_Quaternion operator*(int) const;
45 ON_Quaternion operator/(int) const;
46 ON_Quaternion operator*(float) const;
47 ON_Quaternion operator/(float) const;
48 ON_Quaternion operator*(double) const;
49 ON_Quaternion operator/(double) const;
50
51 ON_Quaternion operator+(const ON_Quaternion&) const;
52 ON_Quaternion operator-(const ON_Quaternion&) const;
53
54 // quaternion multiplication is not commutative
56
57 /*
58 Returns:
59 True if a, b, c, and d are valid finite IEEE doubles.
60 */
61 bool IsValid() const;
62
63 /*
64 Description:
65 Returns the conjugate of the quaternion = (a,-b,-c,-d).
66 */
67 ON_Quaternion Conjugate() const;
68
69 /*
70 Description:
71 Sets the quaternion to a/L2, -b/L2, -c/L2, -d/L2,
72 where L2 = length squared = (a*a + b*b + c*c + d*d).
73 This is the multiplicative inverse, i.e.,
74 (a,b,c,d)*(a/L2, -b/L2, -c/L2, -d/L2) = (1,0,0,0).
75 Returns:
76 True if successful. False if the quaternion is zero
77 and cannot be inverted.
78 */
79 bool Invert();
80
81 /*
82 Returns:
83 Sets the quaternion to a/L2, -b/L2, -c/L2, -d/L2,
84 where L2 = length squared = (a*a + b*b + c*c + d*d).
85 This is the multiplicative inverse, i.e.,
86 (a,b,c,d)*(a/L2, -b/L2, -c/L2, -d/L2) = (1,0,0,0).
87 If "this" is the zero quaternion, then the zero quaternion
88 is returned.
89 */
90 ON_Quaternion Inverse() const;
91
92 /*
93 Returns:
94 Returns the length or norm of the quaternion
95 sqrt(a*a + b*b + c*c + d*d).
96 */
97 double Length() const;
98
99 /*
100 Returns:
101 Returns a*a + b*b + c*c + d*d.
102 */
103 double LengthSquared() const;
104
105 /*
106 Returns:
107 The distance or norm of the difference between the two quaternions.
108 = ("this" - q).Length().
109 */
110 double DistanceTo(const ON_Quaternion& q) const;
111
112 /*
113 Returns:
114 The distance or norm of the difference between the two quaternions.
115 = (p - q).Length().
116 */
117 static double Distance(const ON_Quaternion& p, const ON_Quaternion& q);
118
119 /*
120 Returns:
121 4x4 real valued matrix form of the quaternion
122
123 a b c d
124 -b a -d c
125 -c d a -b
126 -d -c b a
127
128 which has the same arithmetic properties in as the
129 quaternion.
130 Remarks:
131 Do not confuse this with the rotation defined
132 by the quaternion. This function will only be interesting
133 to math nerds and is not useful in rendering or animation
134 applications.
135 */
136 ON_Xform MatrixForm() const;
137
138 /*
139 Description:
140 Scales the quaternion's coordinates so that
141 a*a + b*b + c*c + d*d = 1.
142 Returns:
143 True if successful. False if the quaternion is zero
144 and cannot be unitized.
145 */
146 bool Unitize();
147
148 /*
149 Description:
150 Sets the quaternion to
151
152 cos(angle/2), sin(angle/2)*x, sin(angle/2)*y, sin(angle/2)*z
153
154 where (x,y,z) is the unit vector parallel to axis. This is
155 the unit quaternion that represents the rotation of angle
156 about axis.
157 Parameters:
158 angle - [in] in radians
159 axis - [in] axis of rotation
160 Returns:
161 */
162 void SetRotation(double angle, const ON_3dVector& axis);
163
164 /*
165 Parameters:
166 angle - [in] in radians
167 axis - [in] axis of rotation
168 Returns:
169 The unit quaternion
170
171 cos(angle/2), sin(angle/2)*x, sin(angle/2)*y, sin(angle/2)*z
172
173 where (x,y,z) is the unit vector parallel to axis. This is the
174 unit quaternion that represents the rotation of angle about axis.
175 */
176 static ON_Quaternion Rotation(double angle, const ON_3dVector& axis);
177
178 /*
179 Descriptin:
180 Sets the quaternion to the unit quaternion which rotates
181 plane0.xaxis to plane1.xaxis,
182 plane0.yaxis to plane1.yaxis, and
183 plane0.zaxis to plane1.zaxis.
184 Parameters:
185 plane0 - [in]
186 plane1 - [in]
187 Remarks:
188 The plane origins are ignored.
189 */
190 void SetRotation(const ON_Plane& plane0, const ON_Plane& plane1);
191
192 /*
193 Parameters:
194 plane0 - [in]
195 plane1 - [in]
196 Returns:
197 The unit quaternion that represents the the rotation that maps
198 plane0.xaxis to plane1.xaxis,
199 plane0.yaxis to plane1.yaxis, and
200 plane0.zaxis to plane1.zaxis.
201 Remarks:
202 The plane origins are ignored.
203 */
204 static ON_Quaternion Rotation(const ON_Plane& plane0, const ON_Plane& plane1);
205
206 /*
207 Parameters:
208 angle - [out]
209 in radians
210 axis - [out]
211 unit axis of rotation of 0 if (b,c,d) is the zero vector.
212 Returns:
213 The rotation defined by the quaternion.
214 Remarks:
215 If the quaternion is not unitized, the rotation of its
216 unitized form is returned.
217 */
218 bool GetRotation(double& angle, ON_3dVector& axis) const;
219
220 /*
221 Description:
222 The transformation returned by this function has the property
223 that xform*V = q.Rotate(V).
224 Parameters:
225 xform - [out]
226 Returns:
227 A transformation matrix that performs the rotation defined
228 by the quaternion.
229 Remarks:
230 If the quaternion is not unitized, the rotation of its
231 unitized form is returned. Do not confuse the result of this
232 function the matrix returned by ON_Quaternion::MatrixForm().
233 The transformation returned by this function has the property
234 that xform*V = q.Rotate(V).
235 */
236 bool GetRotation(ON_Xform& xform) const;
237
238 /*
239 Parameters:
240 plane - [out]
241 Returns:
242 The frame created by applying the quaternion's rotation
243 to the canonical world frame (1,0,0),(0,1,0),(0,0,1).
244 */
245 bool GetRotation(ON_Plane& plane) const;
246
247 /*
248 Description
249 Rotate a 3d vector. This operation is also called
250 conjugation, because the result is the same as
251
252 (q.Conjugate()*(0,x,y,x)*q/q.LengthSquared()).Vector()
253
254 Parameters:
255 v - [in]
256 Returns:
257 R*v, where R is the rotation defined by the unit quaternion.
258 This is mathematically the same as the values
259 (Inverse(q)*(0,x,y,z)*q).Vector()
260 and
261 (q.Conjugate()*(0,x,y,x)*q/q.LengthSquared()).Vector()
262 Remarks:
263 If you need to rotate more than a dozen or so vectors, it will
264 be more efficient to call GetRotation(ON_Xform& xform)
265 and multiply the vectors by xform.
266 */
268
269 /*
270 Returns:
271 The "vector" or "imaginary" part of the quaternion = (b,c,d)
272 */
273 ON_3dVector Vector() const;
274
275 /*
276 Returns:
277 The "real" or "scalar" part of the quaternion = a.
278 */
279 double Scalar() const;
280
281 /*
282 Returns:
283 True if a, b, c, and d are all zero.
284 */
285 bool IsZero() const;
286
287 /*
288 Returns:
289 True if b, c, and d are all zero.
290 */
291 bool IsScalar() const;
292
293 /*
294 Returns:
295 True if a = 0 and at least one of b, c, or d is not zero.
296 */
297 bool IsVector() const;
298
299
300 /*
301 Returns:
302 exp(q) = e^a*( cos(|V|) + V/|V|*sin(|V|) ), where V = b*i + c*j + d*k.
303 */
304 static ON_Quaternion Exp(ON_Quaternion q);
305
306 /*
307 Returns:
308 log(q) = log(|q|) + V/|V|*acos(a/|q|), where V = b*i + c*j + d*k.
309 */
310 static ON_Quaternion Log(ON_Quaternion q);
311
312 /*
313 Returns:
314 q^t = Exp(t*Log(q))
315 */
316 static ON_Quaternion Pow(ON_Quaternion q, double t);
317
318
319 static ON_Quaternion Slerp(ON_Quaternion q0, ON_Quaternion q1, double t);
320
321};
322
323/*
324Returns:
325 The quaternion product of p and q. This is the same value as p*q.
326*/
329
330/*
331Returns:
332 The vector cross product of p and q = (0,x,y,z) where
333 (x,y,z) = ON_CrossProduct(p.Vector(),q.Vector())
334
335 This is NOT the same as the quaternion product p*q.
336*/
339
342
345
348
349#endif
Definition opennurbs_point.h:931
Definition opennurbs_plane.h:20
Definition opennurbs_quaternion.h:20
static const ON_Quaternion I
Definition opennurbs_quaternion.h:27
ON_Quaternion()
Definition opennurbs_quaternion.h:31
static const ON_Quaternion J
Definition opennurbs_quaternion.h:28
static const ON_Quaternion Identity
Definition opennurbs_quaternion.h:26
static const ON_Quaternion K
Definition opennurbs_quaternion.h:29
static const ON_Quaternion Zero
Definition opennurbs_quaternion.h:25
double a
Definition opennurbs_quaternion.h:23
Definition opennurbs_xform.h:28
Rotates selected entities.
Definition Rotate.js:11
#define ON_DECL
Definition opennurbs_defines.h:92
#define ON_CLASS
Definition opennurbs_defines.h:91
ON_DECL ON_2fPoint operator*(int, const ON_2fPoint &)
Definition opennurbs_point.cpp:1718
ON_DECL ON_Quaternion ON_CrossProduct(const ON_Quaternion &p, const ON_Quaternion &q)
Definition opennurbs_quaternion.cpp:18
ON_DECL ON_Quaternion operator*(int, const ON_Quaternion &)
Definition opennurbs_quaternion.cpp:514
ON_DECL ON_Quaternion ON_QuaternionProduct(const ON_Quaternion &p, const ON_Quaternion &q)
Definition opennurbs_quaternion.cpp:23