39 |
|
|
40 |
@implementation Quaternion |
@implementation Quaternion |
41 |
|
|
42 |
|
+ (id) quaternionWithValuesX: (float)x |
43 |
|
Y: (float)y |
44 |
|
Z: (float)z |
45 |
|
W: (float)w |
46 |
|
{ |
47 |
|
return AUTORELEASE([[self alloc] initWithValuesX: x Y: y Z: z W: w]); |
48 |
|
} |
49 |
|
|
50 |
|
+ (id) quaternionWithRotation: (Matrix)rotation |
51 |
|
{ |
52 |
|
return AUTORELEASE([[self alloc] initWithRotation: rotation]); |
53 |
|
} |
54 |
|
|
55 |
/* |
/** |
56 |
* This method builds an instance of Quaternion, |
* Initializes the quaternion |
57 |
* with all the attributes initialised with the parameters |
* with all the attributes initialised with the parameters. |
58 |
*/ |
*/ |
59 |
- (id) initWithValuesX: (float)x |
- (id) initWithValuesX: (float)x |
60 |
Y: (float)y |
Y: (float)y |
73 |
return self; |
return self; |
74 |
} |
} |
75 |
|
|
76 |
/* This method builds an instance of Quaternion, |
/** |
77 |
* by converting the matrix in parameter |
* Initializes the quaternion using the rotation matrix. |
78 |
* This matrix must be a 4x4 matrix, otherwise the function will crash. |
* This matrix must be a 4x4 matrix. |
79 |
*/ |
*/ |
80 |
- (id) initWithRotation: (Matrix)rotation |
- (id) initWithRotation: (Matrix)rotation |
81 |
{ |
{ |
137 |
return self; |
return self; |
138 |
} |
} |
139 |
|
|
140 |
/* |
/** |
141 |
* This method returns the rotation matrix 4x4 corresponding to the quaternion. |
* Converts the quaternion to a 4x4 rotation matrix. |
|
* So it converts a quaternion to a matrix 4x4. |
|
142 |
*/ |
*/ |
143 |
- (void) convertToRotation: (Matrix)rotation |
- (void) convertToRotation: (Matrix)rotation |
144 |
{ |
{ |
178 |
} |
} |
179 |
|
|
180 |
|
|
181 |
/* |
- (float) x |
182 |
* Says if the quaternion in parameter is egal to the current quaternionn, i.e. |
{ |
183 |
* the values x, y, z and z are egal. |
return _x; |
184 |
|
} |
185 |
|
|
186 |
|
- (float) y |
187 |
|
{ |
188 |
|
return _y; |
189 |
|
} |
190 |
|
|
191 |
|
- (float) z |
192 |
|
{ |
193 |
|
return _z; |
194 |
|
} |
195 |
|
|
196 |
|
- (float) w |
197 |
|
{ |
198 |
|
return _w; |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
|
* Compares the quaternions. |
203 |
|
* Quaternions are equals, if their x y z w parameters are equal. |
204 |
*/ |
*/ |
205 |
- (BOOL) isEqual: (Quaternion *)quat |
- (BOOL) isEqual: (Quaternion *)quat |
206 |
{ |
{ |
209 |
return _x == quat->_x && _y == quat->_y && _z == quat->_z && _w == quat->_w; |
return _x == quat->_x && _y == quat->_y && _z == quat->_z && _w == quat->_w; |
210 |
} |
} |
211 |
|
|
212 |
|
/** |
213 |
|
* Negates the quaternion. |
214 |
|
*/ |
215 |
- (void) negate |
- (void) negate |
216 |
{ |
{ |
217 |
_x = -_x; |
_x = -_x; |
220 |
_w = -_w; |
_w = -_w; |
221 |
} |
} |
222 |
|
|
223 |
/* Performs a spherical linear interpolation of the two quaternions in |
/** |
224 |
|
* Performs a spherical linear interpolation of the two quaternions in |
225 |
* parameter, and the current quaternion becomes the quaternion interpolated. |
* parameter, and the current quaternion becomes the quaternion interpolated. |
226 |
* q1 is the starting quaternion, q2 the final quaternion. |
* q1 is the starting quaternion, q2 the final quaternion. |
227 |
* t represents the time for the interpolation; it must be valued |
* t represents the time for the interpolation; it must be valued |
233 |
{ |
{ |
234 |
NSParameterAssert (q1); |
NSParameterAssert (q1); |
235 |
NSParameterAssert (q2); |
NSParameterAssert (q2); |
236 |
NSParameterAssert (t >= 0); |
NSParameterAssert (t >= 0.0f); |
237 |
NSParameterAssert (t <= 1); |
NSParameterAssert (t <= 1.0f); |
|
|
|
238 |
|
|
239 |
if ([q1 isEqual: q2]) |
if ([q1 isEqual: q2]) |
240 |
{ |
{ |