Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/forks/sandbox_light/src/external/ogremath/OgreQuaternion.h @ 12302

Last change on this file since 12302 was 7908, checked in by rgrieder, 14 years ago

Stripped down trunk to form a new light sandbox.

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2006 Torus Knot Software Ltd
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23
24You may alternatively use this source under the terms of a specific version of
25the OGRE Unrestricted License provided you have obtained such a license from
26Torus Knot Software Ltd.
27-----------------------------------------------------------------------------
28*/
29// NOTE THAT THIS FILE IS BASED ON MATERIAL FROM:
30
31// Magic Software, Inc.
32// http://www.geometrictools.com/
33// Copyright (c) 2000, All Rights Reserved
34//
35// Source code from Magic Software is supplied under the terms of a license
36// agreement and may not be copied or disclosed except in accordance with the
37// terms of that agreement.  The various license agreements may be found at
38// the Magic Software web site.  This file is subject to the license
39//
40// FREE SOURCE CODE
41// http://www.geometrictools.com/License/WildMagic3License.pdf
42
43#ifndef __Quaternion_H__
44#define __Quaternion_H__
45
46#include "OgrePrerequisites.h"
47
48#include <cassert>
49#include "OgreMath.h"
50#include <ostream>
51
52namespace Ogre {
53
54    /** Implementation of a Quaternion, i.e. a rotation around an axis.
55    */
56    class _OgreExport Quaternion
57    {
58    public:
59        inline Quaternion (
60            Real fW = 1.0,
61            Real fX = 0.0, Real fY = 0.0, Real fZ = 0.0)
62                {
63                        w = fW;
64                        x = fX;
65                        y = fY;
66                        z = fZ;
67                }
68        /// Construct a quaternion from a rotation matrix
69        inline Quaternion(const Matrix3& rot)
70        {
71            this->FromRotationMatrix(rot);
72        }
73        /// Construct a quaternion from an angle/axis
74        inline Quaternion(const Radian& rfAngle, const Vector3& rkAxis)
75        {
76            this->FromAngleAxis(rfAngle, rkAxis);
77        }
78        /// Construct a quaternion from 3 orthonormal local axes
79        inline Quaternion(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
80        {
81            this->FromAxes(xaxis, yaxis, zaxis);
82        }
83        /// Construct a quaternion from 3 orthonormal local axes
84        inline Quaternion(const Vector3* akAxis)
85        {
86            this->FromAxes(akAxis);
87        }
88                /// Construct a quaternion from 4 manual w/x/y/z values
89                inline Quaternion(Real* valptr)
90                {
91                        memcpy(&w, valptr, sizeof(Real)*4);
92                }
93
94                /// Array accessor operator
95                inline Real operator [] ( const size_t i ) const
96                {
97                        assert( i < 4 );
98
99                        return *(&w+i);
100                }
101
102                /// Array accessor operator
103                inline Real& operator [] ( const size_t i )
104                {
105                        assert( i < 4 );
106
107                        return *(&w+i);
108                }
109
110                /// Pointer accessor for direct copying
111                inline Real* ptr()
112                {
113                        return &w;
114                }
115
116                /// Pointer accessor for direct copying
117                inline const Real* ptr() const
118                {
119                        return &w;
120                }
121
122                void FromRotationMatrix (const Matrix3& kRot);
123        void ToRotationMatrix (Matrix3& kRot) const;
124        void FromAngleAxis (const Radian& rfAngle, const Vector3& rkAxis);
125        void ToAngleAxis (Radian& rfAngle, Vector3& rkAxis) const;
126        inline void ToAngleAxis (Degree& dAngle, Vector3& rkAxis) const {
127            Radian rAngle;
128            ToAngleAxis ( rAngle, rkAxis );
129            dAngle = rAngle;
130        }
131        void FromAxes (const Vector3* akAxis);
132        void FromAxes (const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
133        void ToAxes (Vector3* akAxis) const;
134        void ToAxes (Vector3& xAxis, Vector3& yAxis, Vector3& zAxis) const;
135        /// Get the local x-axis
136        Vector3 xAxis(void) const;
137        /// Get the local y-axis
138        Vector3 yAxis(void) const;
139        /// Get the local z-axis
140        Vector3 zAxis(void) const;
141
142        inline Quaternion& operator= (const Quaternion& rkQ)
143                {
144                        w = rkQ.w;
145                        x = rkQ.x;
146                        y = rkQ.y;
147                        z = rkQ.z;
148                        return *this;
149                }
150        Quaternion operator+ (const Quaternion& rkQ) const;
151        Quaternion operator- (const Quaternion& rkQ) const;
152        Quaternion operator* (const Quaternion& rkQ) const;
153        Quaternion operator* (Real fScalar) const;
154        _OgreExport friend Quaternion operator* (Real fScalar,
155            const Quaternion& rkQ);
156        Quaternion operator- () const;
157        inline bool operator== (const Quaternion& rhs) const
158                {
159                        return (rhs.x == x) && (rhs.y == y) &&
160                                (rhs.z == z) && (rhs.w == w);
161                }
162        inline bool operator!= (const Quaternion& rhs) const
163                {
164                        return !operator==(rhs);
165                }
166        // functions of a quaternion
167        Real Dot (const Quaternion& rkQ) const;  // dot product
168        Real Norm () const;  // squared-length
169        /// Normalises this quaternion, and returns the previous length
170        Real normalise(void); 
171        Quaternion Inverse () const;  // apply to non-zero quaternion
172        Quaternion UnitInverse () const;  // apply to unit-length quaternion
173        Quaternion Exp () const;
174        Quaternion Log () const;
175
176        // rotation of a vector by a quaternion
177        Vector3 operator* (const Vector3& rkVector) const;
178
179                /** Calculate the local roll element of this quaternion.
180                @param reprojectAxis By default the method returns the 'intuitive' result
181                        that is, if you projected the local Y of the quaternion onto the X and
182                        Y axes, the angle between them is returned. If set to false though, the
183                        result is the actual yaw that will be used to implement the quaternion,
184                        which is the shortest possible path to get to the same orientation and
185                        may involve less axial rotation.
186                */
187                Radian getRoll(bool reprojectAxis = true) const;
188                /** Calculate the local pitch element of this quaternion
189                @param reprojectAxis By default the method returns the 'intuitive' result
190                        that is, if you projected the local Z of the quaternion onto the X and
191                        Y axes, the angle between them is returned. If set to true though, the
192                        result is the actual yaw that will be used to implement the quaternion,
193                        which is the shortest possible path to get to the same orientation and
194                        may involve less axial rotation.
195                */
196                Radian getPitch(bool reprojectAxis = true) const;
197                /** Calculate the local yaw element of this quaternion
198                @param reprojectAxis By default the method returns the 'intuitive' result
199                        that is, if you projected the local Z of the quaternion onto the X and
200                        Z axes, the angle between them is returned. If set to true though, the
201                        result is the actual yaw that will be used to implement the quaternion,
202                        which is the shortest possible path to get to the same orientation and
203                        may involve less axial rotation.
204                */
205                Radian getYaw(bool reprojectAxis = true) const;         
206                /// Equality with tolerance (tolerance is max angle difference)
207                bool equals(const Quaternion& rhs, const Radian& tolerance) const;
208               
209            // spherical linear interpolation
210        static Quaternion Slerp (Real fT, const Quaternion& rkP,
211            const Quaternion& rkQ, bool shortestPath = false);
212
213        static Quaternion SlerpExtraSpins (Real fT,
214            const Quaternion& rkP, const Quaternion& rkQ,
215            int iExtraSpins);
216
217        // setup for spherical quadratic interpolation
218        static void Intermediate (const Quaternion& rkQ0,
219            const Quaternion& rkQ1, const Quaternion& rkQ2,
220            Quaternion& rka, Quaternion& rkB);
221
222        // spherical quadratic interpolation
223        static Quaternion Squad (Real fT, const Quaternion& rkP,
224            const Quaternion& rkA, const Quaternion& rkB,
225            const Quaternion& rkQ, bool shortestPath = false);
226
227        // normalised linear interpolation - faster but less accurate (non-constant rotation velocity)
228        static Quaternion nlerp(Real fT, const Quaternion& rkP, 
229            const Quaternion& rkQ, bool shortestPath = false);
230
231        // cutoff for sine near zero
232        static const Real ms_fEpsilon;
233
234        // special values
235        static const Quaternion ZERO;
236        static const Quaternion IDENTITY;
237
238                Real w, x, y, z;
239
240        /** Function for writing to a stream. Outputs "Quaternion(w, x, y, z)" with w,x,y,z
241            being the member values of the quaternion.
242        */
243        inline _OgreExport friend std::ostream& operator <<
244            ( std::ostream& o, const Quaternion& q )
245        {
246            o << "Quaternion(" << q.w << ", " << q.x << ", " << q.y << ", " << q.z << ")";
247            return o;
248        }
249
250    };
251
252}
253
254
255
256
257#endif
Note: See TracBrowser for help on using the repository browser.