Qt 4.8
qquaternion.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qquaternion.h"
43 #include <QtCore/qmath.h>
44 #include <QtCore/qvariant.h>
45 #include <QtCore/qdebug.h>
46 
48 
49 #ifndef QT_NO_QUATERNION
50 
84 #ifndef QT_NO_VECTOR3D
85 
120 #endif
121 
133 #ifndef QT_NO_VECTOR4D
134 
153 #endif
154 
273 {
274  return qSqrt(xp * xp + yp * yp + zp * zp + wp * wp);
275 }
276 
283 {
284  return xp * xp + yp * yp + zp * zp + wp * wp;
285 }
286 
298 {
299  // Need some extra precision if the length is very small.
300  double len = double(xp) * double(xp) +
301  double(yp) * double(yp) +
302  double(zp) * double(zp) +
303  double(wp) * double(wp);
304  if (qFuzzyIsNull(len - 1.0f))
305  return *this;
306  else if (!qFuzzyIsNull(len))
307  return *this / qSqrt(len);
308  else
309  return QQuaternion(0.0f, 0.0f, 0.0f, 0.0f);
310 }
311 
319 {
320  // Need some extra precision if the length is very small.
321  double len = double(xp) * double(xp) +
322  double(yp) * double(yp) +
323  double(zp) * double(zp) +
324  double(wp) * double(wp);
325  if (qFuzzyIsNull(len - 1.0f) || qFuzzyIsNull(len))
326  return;
327 
328  len = qSqrt(len);
329 
330  xp /= len;
331  yp /= len;
332  zp /= len;
333  wp /= len;
334 }
335 
361 {
362  return (*this * QQuaternion(0, vector) * conjugate()).vector();
363 }
364 
414 #ifndef QT_NO_VECTOR3D
415 
421 {
422  // Algorithm from:
423  // http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56
424  // We normalize the result just in case the values are close
425  // to zero, as suggested in the above FAQ.
426  qreal a = (angle / 2.0f) * M_PI / 180.0f;
427  qreal s = qSin(a);
428  qreal c = qCos(a);
429  QVector3D ax = axis.normalized();
430  return QQuaternion(c, ax.x() * s, ax.y() * s, ax.z() * s).normalized();
431 }
432 
433 #endif
434 
441 {
442  qreal length = qSqrt(x * x + y * y + z * z);
443  if (!qFuzzyIsNull(length - 1.0f) && !qFuzzyIsNull(length)) {
444  x /= length;
445  y /= length;
446  z /= length;
447  }
448  qreal a = (angle / 2.0f) * M_PI / 180.0f;
449  qreal s = qSin(a);
450  qreal c = qCos(a);
451  return QQuaternion(c, x * s, y * s, z * s).normalized();
452 }
453 
583  (const QQuaternion& q1, const QQuaternion& q2, qreal t)
584 {
585  // Handle the easy cases first.
586  if (t <= 0.0f)
587  return q1;
588  else if (t >= 1.0f)
589  return q2;
590 
591  // Determine the angle between the two quaternions.
592  QQuaternion q2b;
593  qreal dot;
594  dot = q1.xp * q2.xp + q1.yp * q2.yp + q1.zp * q2.zp + q1.wp * q2.wp;
595  if (dot >= 0.0f) {
596  q2b = q2;
597  } else {
598  q2b = -q2;
599  dot = -dot;
600  }
601 
602  // Get the scale factors. If they are too small,
603  // then revert to simple linear interpolation.
604  qreal factor1 = 1.0f - t;
605  qreal factor2 = t;
606  if ((1.0f - dot) > 0.0000001) {
607  qreal angle = qreal(qAcos(dot));
608  qreal sinOfAngle = qreal(qSin(angle));
609  if (sinOfAngle > 0.0000001) {
610  factor1 = qreal(qSin((1.0f - t) * angle)) / sinOfAngle;
611  factor2 = qreal(qSin(t * angle)) / sinOfAngle;
612  }
613  }
614 
615  // Construct the result quaternion.
616  return q1 * factor1 + q2b * factor2;
617 }
618 
635  (const QQuaternion& q1, const QQuaternion& q2, qreal t)
636 {
637  // Handle the easy cases first.
638  if (t <= 0.0f)
639  return q1;
640  else if (t >= 1.0f)
641  return q2;
642 
643  // Determine the angle between the two quaternions.
644  QQuaternion q2b;
645  qreal dot;
646  dot = q1.xp * q2.xp + q1.yp * q2.yp + q1.zp * q2.zp + q1.wp * q2.wp;
647  if (dot >= 0.0f)
648  q2b = q2;
649  else
650  q2b = -q2;
651 
652  // Perform the linear interpolation.
653  return (q1 * (1.0f - t) + q2b * t).normalized();
654 }
655 
659 QQuaternion::operator QVariant() const
660 {
661  return QVariant(QVariant::Quaternion, this);
662 }
663 
664 #ifndef QT_NO_DEBUG_STREAM
665 
667 {
668  dbg.nospace() << "QQuaternion(scalar:" << q.scalar()
669  << ", vector:(" << q.x() << ", "
670  << q.y() << ", " << q.z() << "))";
671  return dbg.space();
672 }
673 
674 #endif
675 
676 #ifndef QT_NO_DATASTREAM
677 
692 {
693  stream << double(quaternion.scalar()) << double(quaternion.x())
694  << double(quaternion.y()) << double(quaternion.z());
695  return stream;
696 }
697 
712 {
713  double scalar, x, y, z;
714  stream >> scalar;
715  stream >> x;
716  stream >> y;
717  stream >> z;
718  quaternion.setScalar(qreal(scalar));
719  quaternion.setX(qreal(x));
720  quaternion.setY(qreal(y));
721  quaternion.setZ(qreal(z));
722  return stream;
723 }
724 
725 #endif // QT_NO_DATASTREAM
726 
727 #endif
728 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QDataStream & operator>>(QDataStream &stream, QEasingCurve &easing)
Reads an easing curve from the given stream into the given easing curve and returns a reference to th...
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
QQuaternion()
Constructs an identity quaternion, i.
Definition: qquaternion.h:141
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvector3d.h:60
double qreal
Definition: qglobal.h:1193
unsigned char c[8]
Definition: qnumeric_p.h:62
qreal x() const
Returns the x coordinate of this quaternion&#39;s vector.
Definition: qquaternion.h:156
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setZ(qreal z)
Sets the z coordinate of this quaternion&#39;s vector to the given z coordinate.
Definition: qquaternion.h:163
static QQuaternion nlerp(const QQuaternion &q1, const QQuaternion &q2, qreal t)
Interpolates along the shortest linear path between the rotational positions q1 and q2...
static QQuaternion slerp(const QQuaternion &q1, const QQuaternion &q2, qreal t)
Interpolates along the shortest spherical path between the rotational positions q1 and q2...
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
void setX(qreal x)
Sets the x coordinate of this quaternion&#39;s vector to the given x coordinate.
Definition: qquaternion.h:161
QVector3D rotatedVector(const QVector3D &vector) const
Rotates vector with this quaternion to produce a new vector in 3D space.
QQuaternion normalized() const
Returns the normalized unit form of this quaternion.
qreal lengthSquared() const
Returns the squared length of the quaternion.
long ASN1_INTEGER_get ASN1_INTEGER * a
qreal x() const
Returns the x coordinate of this point.
Definition: qvector3d.h:161
#define M_PI
Definition: qmath.h:261
QDataStream & operator<<(QDataStream &stream, const QQuaternion &quaternion)
Writes the given quaternion to the given stream and returns a reference to the stream.
static QQuaternion fromAxisAndAngle(const QVector3D &axis, qreal angle)
Creates a normalized quaternion that corresponds to rotating through angle degrees about the specifie...
QVector3D vector() const
Returns the vector component of this quaternion.
Definition: qquaternion.h:289
qreal scalar() const
Returns the scalar component of this quaternion.
Definition: qquaternion.h:159
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
qreal length() const
Returns the length of the quaternion.
static FILE * stream
void setScalar(qreal scalar)
Sets the scalar component of this quaternion to scalar.
Definition: qquaternion.h:164
qreal qSin(qreal v)
Definition: qmath.h:93
QVector3D normalized() const
Returns the normalized unit vector form of this vector.
Definition: qvector3d.cpp:247
qreal z() const
Returns the z coordinate of this point.
Definition: qvector3d.h:163
qreal y() const
Returns the y coordinate of this quaternion&#39;s vector.
Definition: qquaternion.h:157
qreal angle(const QPointF &p1, const QPointF &p2)
qreal qAcos(qreal v)
Definition: qmath.h:141
qreal z() const
Returns the z coordinate of this quaternion&#39;s vector.
Definition: qquaternion.h:158
qreal y() const
Returns the y coordinate of this point.
Definition: qvector3d.h:162
void normalize()
Normalizes the currect quaternion in place.
The QQuaternion class represents a quaternion consisting of a vector and scalar.
Definition: qquaternion.h:59
QQuaternion conjugate() const
Returns the conjugate of this quaternion, which is (-x, -y, -z, scalar).
Definition: qquaternion.h:166
static Q_DECL_CONSTEXPR bool qFuzzyIsNull(double d)
Definition: qglobal.h:2043
QVector3D axis() const
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
void setY(qreal y)
Sets the y coordinate of this quaternion&#39;s vector to the given y coordinate.
Definition: qquaternion.h:162
static qreal dot(const QPointF &a, const QPointF &b)
qreal qCos(qreal v)
Definition: qmath.h:109
qreal qSqrt(qreal v)
Definition: qmath.h:205