Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 13, 2008, 11:45:51 PM (16 years ago)
Author:
rgrieder
Message:

Updated to Bullet 2.73 (first part).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/physics/src/bullet/LinearMath/btQuaternion.h

    r2192 r2430  
    1818#define SIMD__QUATERNION_H_
    1919
     20
    2021#include "btVector3.h"
    2122
    22 ///The btQuaternion implements quaternion to perform linear algebra rotations in combination with btMatrix3x3, btVector3 and btTransform.
     23/**@brief The btQuaternion implements quaternion to perform linear algebra rotations in combination with btMatrix3x3, btVector3 and btTransform. */
    2324class btQuaternion : public btQuadWord {
    2425public:
     26  /**@brief No initialization constructor */
    2527        btQuaternion() {}
    2628
    2729        //              template <typename btScalar>
    2830        //              explicit Quaternion(const btScalar *v) : Tuple4<btScalar>(v) {}
    29 
     31  /**@brief Constructor from scalars */
    3032        btQuaternion(const btScalar& x, const btScalar& y, const btScalar& z, const btScalar& w)
    3133                : btQuadWord(x, y, z, w)
    3234        {}
    33 
     35  /**@brief Axis angle Constructor
     36   * @param axis The axis which the rotation is around
     37   * @param angle The magnitude of the rotation around the angle (Radians) */
    3438        btQuaternion(const btVector3& axis, const btScalar& angle)
    3539        {
    3640                setRotation(axis, angle);
    3741        }
    38 
     42  /**@brief Constructor from Euler angles
     43   * @param yaw Angle around Y unless BT_EULER_DEFAULT_ZYX defined then Z
     44   * @param pitch Angle around X unless BT_EULER_DEFAULT_ZYX defined then Y
     45   * @param roll Angle around Z unless BT_EULER_DEFAULT_ZYX defined then X */
    3946        btQuaternion(const btScalar& yaw, const btScalar& pitch, const btScalar& roll)
    4047        {
     48#ifndef BT_EULER_DEFAULT_ZYX
    4149                setEuler(yaw, pitch, roll);
    42         }
    43 
     50#else
     51                setEulerZYX(yaw, pitch, roll);
     52#endif
     53        }
     54  /**@brief Set the rotation using axis angle notation
     55   * @param axis The axis around which to rotate
     56   * @param angle The magnitude of the rotation in Radians */
    4457        void setRotation(const btVector3& axis, const btScalar& angle)
    4558        {
     
    5063                        btCos(angle * btScalar(0.5)));
    5164        }
    52 
     65  /**@brief Set the quaternion using Euler angles
     66   * @param yaw Angle around Y
     67   * @param pitch Angle around X
     68   * @param roll Angle around Z */
    5369        void setEuler(const btScalar& yaw, const btScalar& pitch, const btScalar& roll)
    5470        {
     
    6783                        cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw);
    6884        }
    69 
    70         btQuaternion& operator+=(const btQuaternion& q)
    71         {
    72                 m_x += q.x(); m_y += q.y(); m_z += q.z(); m_unusedW += q.m_unusedW;
     85  /**@brief Set the quaternion using euler angles
     86   * @param yaw Angle around Z
     87   * @param pitch Angle around Y
     88   * @param roll Angle around X */
     89        void setEulerZYX(const btScalar& yaw, const btScalar& pitch, const btScalar& roll)
     90        {
     91                btScalar halfYaw = btScalar(yaw) * btScalar(0.5); 
     92                btScalar halfPitch = btScalar(pitch) * btScalar(0.5); 
     93                btScalar halfRoll = btScalar(roll) * btScalar(0.5); 
     94                btScalar cosYaw = btCos(halfYaw);
     95                btScalar sinYaw = btSin(halfYaw);
     96                btScalar cosPitch = btCos(halfPitch);
     97                btScalar sinPitch = btSin(halfPitch);
     98                btScalar cosRoll = btCos(halfRoll);
     99                btScalar sinRoll = btSin(halfRoll);
     100                setValue(sinRoll * cosPitch * cosYaw - cosRoll * sinPitch * sinYaw, //x
     101                         cosRoll * sinPitch * cosYaw + sinRoll * cosPitch * sinYaw, //y
     102                         cosRoll * cosPitch * sinYaw - sinRoll * sinPitch * cosYaw, //z
     103                         cosRoll * cosPitch * cosYaw + sinRoll * sinPitch * sinYaw); //formerly yzx
     104        }
     105  /**@brief Add two quaternions
     106   * @param q The quaternion to add to this one */
     107        SIMD_FORCE_INLINE       btQuaternion& operator+=(const btQuaternion& q)
     108        {
     109                m_floats[0] += q.x(); m_floats[1] += q.y(); m_floats[2] += q.z(); m_floats[3] += q.m_floats[3];
    73110                return *this;
    74111        }
    75112
     113  /**@brief Subtract out a quaternion
     114   * @param q The quaternion to subtract from this one */
    76115        btQuaternion& operator-=(const btQuaternion& q)
    77116        {
    78                 m_x -= q.x(); m_y -= q.y(); m_z -= q.z(); m_unusedW -= q.m_unusedW;
     117                m_floats[0] -= q.x(); m_floats[1] -= q.y(); m_floats[2] -= q.z(); m_floats[3] -= q.m_floats[3];
    79118                return *this;
    80119        }
    81120
     121  /**@brief Scale this quaternion
     122   * @param s The scalar to scale by */
    82123        btQuaternion& operator*=(const btScalar& s)
    83124        {
    84                 m_x *= s; m_y *= s; m_z *= s; m_unusedW *= s;
     125                m_floats[0] *= s; m_floats[1] *= s; m_floats[2] *= s; m_floats[3] *= s;
    85126                return *this;
    86127        }
    87128
    88 
     129  /**@brief Multiply this quaternion by q on the right
     130   * @param q The other quaternion
     131   * Equivilant to this = this * q */
    89132        btQuaternion& operator*=(const btQuaternion& q)
    90133        {
    91                 setValue(m_unusedW * q.x() + m_x * q.m_unusedW + m_y * q.z() - m_z * q.y(),
    92                         m_unusedW * q.y() + m_y * q.m_unusedW + m_z * q.x() - m_x * q.z(),
    93                         m_unusedW * q.z() + m_z * q.m_unusedW + m_x * q.y() - m_y * q.x(),
    94                         m_unusedW * q.m_unusedW - m_x * q.x() - m_y * q.y() - m_z * q.z());
     134                setValue(m_floats[3] * q.x() + m_floats[0] * q.m_floats[3] + m_floats[1] * q.z() - m_floats[2] * q.y(),
     135                        m_floats[3] * q.y() + m_floats[1] * q.m_floats[3] + m_floats[2] * q.x() - m_floats[0] * q.z(),
     136                        m_floats[3] * q.z() + m_floats[2] * q.m_floats[3] + m_floats[0] * q.y() - m_floats[1] * q.x(),
     137                        m_floats[3] * q.m_floats[3] - m_floats[0] * q.x() - m_floats[1] * q.y() - m_floats[2] * q.z());
    95138                return *this;
    96139        }
    97 
     140  /**@brief Return the dot product between this quaternion and another
     141   * @param q The other quaternion */
    98142        btScalar dot(const btQuaternion& q) const
    99143        {
    100                 return m_x * q.x() + m_y * q.y() + m_z * q.z() + m_unusedW * q.m_unusedW;
    101         }
    102 
     144                return m_floats[0] * q.x() + m_floats[1] * q.y() + m_floats[2] * q.z() + m_floats[3] * q.m_floats[3];
     145        }
     146
     147  /**@brief Return the length squared of the quaternion */
    103148        btScalar length2() const
    104149        {
     
    106151        }
    107152
     153  /**@brief Return the length of the quaternion */
    108154        btScalar length() const
    109155        {
     
    111157        }
    112158
     159  /**@brief Normalize the quaternion
     160   * Such that x^2 + y^2 + z^2 +w^2 = 1 */
    113161        btQuaternion& normalize()
    114162        {
     
    116164        }
    117165
     166  /**@brief Return a scaled version of this quaternion
     167   * @param s The scale factor */
    118168        SIMD_FORCE_INLINE btQuaternion
    119169        operator*(const btScalar& s) const
    120170        {
    121                 return btQuaternion(x() * s, y() * s, z() * s, m_unusedW * s);
    122         }
    123 
    124 
    125 
     171                return btQuaternion(x() * s, y() * s, z() * s, m_floats[3] * s);
     172        }
     173
     174
     175  /**@brief Return an inversely scaled versionof this quaternion
     176   * @param s The inverse scale factor */
    126177        btQuaternion operator/(const btScalar& s) const
    127178        {
     
    130181        }
    131182
    132 
     183  /**@brief Inversely scale this quaternion
     184   * @param s The scale factor */
    133185        btQuaternion& operator/=(const btScalar& s)
    134186        {
     
    137189        }
    138190
    139 
     191  /**@brief Return a normalized version of this quaternion */
    140192        btQuaternion normalized() const
    141193        {
    142194                return *this / length();
    143195        }
    144 
     196  /**@brief Return the angle between this quaternion and the other
     197   * @param q The other quaternion */
    145198        btScalar angle(const btQuaternion& q) const
    146199        {
     
    149202                return btAcos(dot(q) / s);
    150203        }
    151 
     204  /**@brief Return the angle of rotation represented by this quaternion */
    152205        btScalar getAngle() const
    153206        {
    154                 btScalar s = btScalar(2.) * btAcos(m_unusedW);
     207                btScalar s = btScalar(2.) * btAcos(m_floats[3]);
    155208                return s;
    156209        }
    157210
    158211
    159 
     212  /**@brief Return the inverse of this quaternion */
    160213        btQuaternion inverse() const
    161214        {
    162                 return btQuaternion(-m_x, -m_y, -m_z, m_unusedW);
    163         }
    164 
     215                return btQuaternion(-m_floats[0], -m_floats[1], -m_floats[2], m_floats[3]);
     216        }
     217
     218  /**@brief Return the sum of this quaternion and the other
     219   * @param q2 The other quaternion */
    165220        SIMD_FORCE_INLINE btQuaternion
    166221        operator+(const btQuaternion& q2) const
    167222        {
    168223                const btQuaternion& q1 = *this;
    169                 return btQuaternion(q1.x() + q2.x(), q1.y() + q2.y(), q1.z() + q2.z(), q1.m_unusedW + q2.m_unusedW);
    170         }
    171 
     224                return btQuaternion(q1.x() + q2.x(), q1.y() + q2.y(), q1.z() + q2.z(), q1.m_floats[3] + q2.m_floats[3]);
     225        }
     226
     227  /**@brief Return the difference between this quaternion and the other
     228   * @param q2 The other quaternion */
    172229        SIMD_FORCE_INLINE btQuaternion
    173230        operator-(const btQuaternion& q2) const
    174231        {
    175232                const btQuaternion& q1 = *this;
    176                 return btQuaternion(q1.x() - q2.x(), q1.y() - q2.y(), q1.z() - q2.z(), q1.m_unusedW - q2.m_unusedW);
    177         }
    178 
     233                return btQuaternion(q1.x() - q2.x(), q1.y() - q2.y(), q1.z() - q2.z(), q1.m_floats[3] - q2.m_floats[3]);
     234        }
     235
     236  /**@brief Return the negative of this quaternion
     237   * This simply negates each element */
    179238        SIMD_FORCE_INLINE btQuaternion operator-() const
    180239        {
    181240                const btQuaternion& q2 = *this;
    182                 return btQuaternion( - q2.x(), - q2.y(),  - q2.z(),  - q2.m_unusedW);
    183         }
    184 
     241                return btQuaternion( - q2.x(), - q2.y(),  - q2.z(),  - q2.m_floats[3]);
     242        }
     243  /**@todo document this and it's use */
    185244        SIMD_FORCE_INLINE btQuaternion farthest( const btQuaternion& qd) const
    186245        {
     
    193252        }
    194253
     254  /**@brief Return the quaternion which is the result of Spherical Linear Interpolation between this and the other quaternion
     255   * @param q The other quaternion to interpolate with
     256   * @param t The ratio between this and q to interpolate.  If t = 0 the result is this, if t=1 the result is q.
     257   * Slerp interpolates assuming constant velocity.  */
    195258        btQuaternion slerp(const btQuaternion& q, const btScalar& t) const
    196259        {
     
    201264                        btScalar s0 = btSin((btScalar(1.0) - t) * theta);
    202265                        btScalar s1 = btSin(t * theta);   
    203                         return btQuaternion((m_x * s0 + q.x() * s1) * d,
    204                                 (m_y * s0 + q.y() * s1) * d,
    205                                 (m_z * s0 + q.z() * s1) * d,
    206                                 (m_unusedW * s0 + q.m_unusedW * s1) * d);
     266                        return btQuaternion((m_floats[0] * s0 + q.x() * s1) * d,
     267                                (m_floats[1] * s0 + q.y() * s1) * d,
     268                                (m_floats[2] * s0 + q.z() * s1) * d,
     269                                (m_floats[3] * s0 + q.m_floats[3] * s1) * d);
    207270                }
    208271                else
     
    212275        }
    213276
    214         SIMD_FORCE_INLINE const btScalar& getW() const { return m_unusedW; }
     277        SIMD_FORCE_INLINE const btScalar& getW() const { return m_floats[3]; }
    215278
    216279       
     
    218281
    219282
    220 
     283/**@brief Return the negative of a quaternion */
    221284SIMD_FORCE_INLINE btQuaternion
    222285operator-(const btQuaternion& q)
     
    227290
    228291
    229 
     292/**@brief Return the product of two quaternions */
    230293SIMD_FORCE_INLINE btQuaternion
    231294operator*(const btQuaternion& q1, const btQuaternion& q2) {
     
    254317}
    255318
     319/**@brief Calculate the dot product between two quaternions */
    256320SIMD_FORCE_INLINE btScalar
    257321dot(const btQuaternion& q1, const btQuaternion& q2)
     
    261325
    262326
     327/**@brief Return the length of a quaternion */
    263328SIMD_FORCE_INLINE btScalar
    264329length(const btQuaternion& q)
     
    267332}
    268333
     334/**@brief Return the angle between two quaternions*/
    269335SIMD_FORCE_INLINE btScalar
    270336angle(const btQuaternion& q1, const btQuaternion& q2)
     
    273339}
    274340
    275 
     341/**@brief Return the inverse of a quaternion*/
    276342SIMD_FORCE_INLINE btQuaternion
    277343inverse(const btQuaternion& q)
     
    280346}
    281347
     348/**@brief Return the result of spherical linear interpolation betwen two quaternions
     349 * @param q1 The first quaternion
     350 * @param q2 The second quaternion
     351 * @param t The ration between q1 and q2.  t = 0 return q1, t=1 returns q2
     352 * Slerp assumes constant velocity between positions. */
    282353SIMD_FORCE_INLINE btQuaternion
    283354slerp(const btQuaternion& q1, const btQuaternion& q2, const btScalar& t)
Note: See TracChangeset for help on using the changeset viewer.