1 | /************************************************************** ggt-head beg |
---|
2 | * |
---|
3 | * GGT: Generic Graphics Toolkit |
---|
4 | * |
---|
5 | * Original Authors: |
---|
6 | * Allen Bierbaum |
---|
7 | * |
---|
8 | * ----------------------------------------------------------------- |
---|
9 | * File: Quat.h,v |
---|
10 | * Date modified: 2005/06/06 15:36:03 |
---|
11 | * Version: 1.25 |
---|
12 | * ----------------------------------------------------------------- |
---|
13 | * |
---|
14 | *********************************************************** ggt-head end */ |
---|
15 | /*************************************************************** ggt-cpr beg |
---|
16 | * |
---|
17 | * GGT: The Generic Graphics Toolkit |
---|
18 | * Copyright (C) 2001,2002 Allen Bierbaum |
---|
19 | * |
---|
20 | * This library is free software; you can redistribute it and/or |
---|
21 | * modify it under the terms of the GNU Lesser General Public |
---|
22 | * License as published by the Free Software Foundation; either |
---|
23 | * version 2.1 of the License, or (at your option) any later version. |
---|
24 | * |
---|
25 | * This library is distributed in the hope that it will be useful, |
---|
26 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
27 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
28 | * Lesser General Public License for more details. |
---|
29 | * |
---|
30 | * You should have received a copy of the GNU Lesser General Public |
---|
31 | * License along with this library; if not, write to the Free Software |
---|
32 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
33 | * |
---|
34 | ************************************************************ ggt-cpr end */ |
---|
35 | #ifndef _GMTL_QUAT_H_ |
---|
36 | #define _GMTL_QUAT_H_ |
---|
37 | |
---|
38 | #include <gmtl/Defines.h> |
---|
39 | #include <gmtl/Vec.h> |
---|
40 | |
---|
41 | namespace gmtl |
---|
42 | { |
---|
43 | |
---|
44 | /** Quat: Class to encapsulate quaternion behaviors. |
---|
45 | * |
---|
46 | * this Quaternion is ordered in memory: x,y,z,w. |
---|
47 | * @see Quatf |
---|
48 | * @see Quatd |
---|
49 | * |
---|
50 | * Note: The code for most of these routines was built using the following |
---|
51 | * references |
---|
52 | * |
---|
53 | * References: |
---|
54 | * <ul> |
---|
55 | * <li> Advanced Animation and Rendering Techniques: pp363-365 |
---|
56 | * <li> Animating Rotation with Quaternion Curves, Ken Shoemake, |
---|
57 | * SIGGRAPH Proceedings Vol 19, Number 3, 1985 |
---|
58 | * <li> Quaternion Calculus for Animation, Ken Shoemake SIGGRAPH course |
---|
59 | * notes 1989 |
---|
60 | * <li> Game Developer Magazine: Feb 98, pg.34-42 |
---|
61 | * <li> Motivation for the use of Quaternions to perform transformations |
---|
62 | * http://www.rust.net/~kgeoinfo/quat1.htm |
---|
63 | * <li> On quaternions; or on a new system of imaginaries in algebra, |
---|
64 | * Sir William Rowan Hamilton, Philosophical Magazine, xxv, |
---|
65 | * pp. 10-13 (July 1844) |
---|
66 | * <li> You also can find more on quaternions at |
---|
67 | * <ul> |
---|
68 | * <li> http://www.gamasutra.com/features/19980703/quaternions_01.htm and at |
---|
69 | * <li> http://archive.ncsa.uiuc.edu/VEG/VPS/emtc/quaternions/index.html |
---|
70 | * </ul> |
---|
71 | * <li> Or search on google.... |
---|
72 | * </ul> |
---|
73 | * @ingroup Types |
---|
74 | */ |
---|
75 | template <typename DATA_TYPE> |
---|
76 | class Quat |
---|
77 | { |
---|
78 | public: |
---|
79 | /** use this to declare single value types of the same type as this matrix. |
---|
80 | */ |
---|
81 | typedef DATA_TYPE DataType; |
---|
82 | |
---|
83 | enum Params { Size = 4 }; |
---|
84 | |
---|
85 | /** default constructor, initializes to quaternion multiplication identity |
---|
86 | * [x,y,z,w] == [0,0,0,1]. |
---|
87 | * NOTE: the addition identity is [0,0,0,0] |
---|
88 | */ |
---|
89 | Quat() |
---|
90 | : mData( (DATA_TYPE)0.0, (DATA_TYPE)0.0, (DATA_TYPE)0.0, (DATA_TYPE)1.0 ) |
---|
91 | { |
---|
92 | } |
---|
93 | |
---|
94 | /** data constructor, initializes to quaternion multiplication identity |
---|
95 | * [x,y,z,w] == [0,0,0,1]. |
---|
96 | * NOTE: the addition identity is [0,0,0,0] |
---|
97 | */ |
---|
98 | Quat( const DATA_TYPE& x, const DATA_TYPE& y, const DATA_TYPE& z, |
---|
99 | const DATA_TYPE& w ) |
---|
100 | : mData( x, y, z, w ) |
---|
101 | { |
---|
102 | } |
---|
103 | |
---|
104 | /** copy constructor |
---|
105 | */ |
---|
106 | Quat( const Quat<DATA_TYPE>& q ) : mData( q.mData ) |
---|
107 | { |
---|
108 | } |
---|
109 | |
---|
110 | /** directly set the quaternion's values |
---|
111 | * @pre x,y,z,w should be normalized |
---|
112 | * @post the quaternion is set with the given values |
---|
113 | */ |
---|
114 | void set( const DATA_TYPE x, const DATA_TYPE y, const DATA_TYPE z, const DATA_TYPE w ) |
---|
115 | { |
---|
116 | mData.set( x, y, z, w ); |
---|
117 | } |
---|
118 | |
---|
119 | /** get the raw data elements of the quaternion. |
---|
120 | * @post sets the given variables to the quaternion's x, y, z, and w values |
---|
121 | */ |
---|
122 | void get( DATA_TYPE& x, DATA_TYPE& y, DATA_TYPE& z, DATA_TYPE& w ) const |
---|
123 | { |
---|
124 | x = mData[Xelt]; |
---|
125 | y = mData[Yelt]; |
---|
126 | z = mData[Zelt]; |
---|
127 | w = mData[Welt]; |
---|
128 | } |
---|
129 | |
---|
130 | /** bracket operator. |
---|
131 | * raw data accessor. |
---|
132 | * |
---|
133 | * <h3> "Example (access raw data element in a Quat):" </h3> |
---|
134 | * \code |
---|
135 | * Quatf q; |
---|
136 | * q[Xelt] = 0.001231176f; |
---|
137 | * q[Yelt] = 0.1222f; |
---|
138 | * q[Zelt] = 0.721f; |
---|
139 | * q[Welt] = 0.982323f; |
---|
140 | * \endcode |
---|
141 | * |
---|
142 | * @see VectorIndex |
---|
143 | */ |
---|
144 | DATA_TYPE& operator[]( const int x ) |
---|
145 | { |
---|
146 | gmtlASSERT( x >= 0 && x < 4 && "out of bounds error" ); |
---|
147 | return mData[x]; |
---|
148 | } |
---|
149 | |
---|
150 | /** bracket operator (const version). |
---|
151 | * raw data accessor. |
---|
152 | * |
---|
153 | * <h3> "Example (access raw data element in a Quat):" </h3> |
---|
154 | * \code |
---|
155 | * Quatf q; |
---|
156 | * float rads = acos( q[Welt] ) / 2.0f; |
---|
157 | * \endcode |
---|
158 | * |
---|
159 | * @see VectorIndex |
---|
160 | */ |
---|
161 | const DATA_TYPE& operator[]( const int x ) const |
---|
162 | { |
---|
163 | gmtlASSERT( x >= 0 && x < 4 && "out of bounds error" ); |
---|
164 | return mData[x]; |
---|
165 | } |
---|
166 | |
---|
167 | /** Get a DATA_TYPE pointer to the quat internal data. |
---|
168 | * @post Returns a ptr to the head of the quat data |
---|
169 | */ |
---|
170 | const DATA_TYPE* getData() const { return (DATA_TYPE*)mData.getData();} |
---|
171 | |
---|
172 | public: |
---|
173 | // Order x, y, z, w |
---|
174 | Vec<DATA_TYPE, 4> mData; |
---|
175 | }; |
---|
176 | |
---|
177 | const Quat<float> QUAT_MULT_IDENTITYF( 0.0f, 0.0f, 0.0f, 1.0f ); |
---|
178 | const Quat<float> QUAT_ADD_IDENTITYF( 0.0f, 0.0f, 0.0f, 0.0f ); |
---|
179 | const Quat<float> QUAT_IDENTITYF( QUAT_MULT_IDENTITYF ); |
---|
180 | const Quat<double> QUAT_MULT_IDENTITYD( 0.0, 0.0, 0.0, 1.0 ); |
---|
181 | const Quat<double> QUAT_ADD_IDENTITYD( 0.0, 0.0, 0.0, 0.0 ); |
---|
182 | const Quat<double> QUAT_IDENTITYD( QUAT_MULT_IDENTITYD ); |
---|
183 | |
---|
184 | typedef Quat<float> Quatf; |
---|
185 | typedef Quat<double> Quatd; |
---|
186 | |
---|
187 | } // end of namespace gmtl |
---|
188 | |
---|
189 | #endif |
---|