Debian Codegraph @

README

raw
1 NAME
2 Math::Quaternion - Perl class to represent quaternions
3
4 SYNOPSIS
5 use Math::Quaternion qw(slerp);
6 my $q = Math::Quaternion->new; # Make a new unit quaternion
7
8 # Make a rotation about the axis (0,1,0)
9 my $q2 = Math::Quaternion->new({axis=>[0,1,0],angle=>0.1});
10 my @v = (1,2,3); # A vector.
11 my @vrotated = $q2->rotate_vector(@v); # Rotate @v about (0,1,0).
12
13 my $q3 = Math::Quaternion::rotation(0.7,2,1,4); # A different rotation.
14 my $q4 = slerp($q2,$q3,0.5); # Interpolated rotation.
15 my @vinterp = $q4->rotate_vector(@v);
16
17 DESCRIPTION
18 This package lets you create and manipulate quaternions. A quaternion is
19 a mathematical object developed as a kind of generalization of complex
20 numbers, usually represented by an array of four real numbers, and is
21 often used to represent rotations in three-dimensional space.
22
23 See, for example, <http://mathworld.wolfram.com/Quaternion.html> for
24 more details on the mathematics of quaternions.
25
26 Quaternions can be added, subtracted, and scaled just like complex
27 numbers or vectors -- they can also be multiplied, but quaternion
28 multiplication DOES NOT COMMUTE. That is to say, if you have quaternions
29 $q1 and $q2, then in general $q1*$q2 != $q2*$q1. This is related to
30 their use in representing rotations, which also do not commute.
31
32 If you just want to represent rotations and don't care about the
33 internal mathematical details, this should be all you need to know:
34
35 All quaternions have a quantity called the "norm", similar to the length
36 of a vector. A quaternion with norm equal to 1 is called a "unit
37 quaternion". All quaternions which represent rotations are unit
38 quaternions.
39
40 If you call new() without any arguments, it will give you a unit
41 quaternion which represents no rotation:
42
43 $q = Math::Quaternion->new;
44
45 You can make a quaternion which represents a rotation of a given angle
46 (in radians) about a given axis:
47
48 $qrot = Math::Quaternion->new({ axis => 0.1, angle => [ 2,3,4]});
49
50 Say you have two rotations, $q1 and $q2, and you want to make a
51 quaternion representing a rotation of $q1 followed by $q2. Then, you do:
52
53 $q3 = $q2 * $q1; # Rotate by $q1, followed by $q2.
54
55 Remember that this is NOT the same as $q1 * $q2, which will reverse the
56 order of the rotations.
57
58 If you perform many iterated quaternion operations, the result may not
59 quite be a unit quaternion due to numerical inaccuracies. You can make
60 sure any quaternion has unit length, by doing:
61
62 $unitquat = $anyquat->normalize;
63
64 If you have a rotation quaternion, and you want to find the 3x3 matrix
65 which represents the corresponding rotation, then:
66
67 @matrix = $q->matrix3x3;
68
69 Similarly, you can generate a 4x4 matrix of the sort you'd pass to
70 OpenGL:
71
72 @glmatrix = $q->matrix4x4;
73
74 If you have a vector representing a direction, and you want to rotate
75 the vector by a quaternion $q:
76
77 my @vector = (0,0,1); # Vector pointing in the Z direction.
78
79 my @newvec = $q->rotate_vector(@vector); # New direction.
80
81 Say you're using quaternions to represent the orientation of a camera,
82 and you have two quaternions: one to represent a starting orientation,
83 and another to represent a finishing position. If you want to find all
84 the quaternions representing the orientations in between, allowing your
85 camera to move smoothly from start to finish, use the slerp() routine:
86
87 use Math::Quaternion qw(slerp);
88
89 my ($qstart, $qend) = ... ;
90
91 # Set $tween to 9 points between start and end, exclusive.
92
93 for my $t (1..9) {
94 my $tween = slerp($qstart,$qend,0.1*$t);
95 ...
96 }
97
98 METHODS
99 new
100 my $q = Math::Quaternion->new; # Make a new unit quaternion.
101 my $q2 = Math::Quaternion->new(1,2,3,4);# Make a specific quaternion.
102 my $q3 = Math::Quaternion->new($q2); # Copy an existing quaternion.
103 my $q4 = Math::Quaternion->new(5.6); # Make the quaternion (5.6,0,0,0)
104 my $q5 = Math::Quaternion->new(7,8,9); # Make the quaternion (0,7,8,9)
105
106 my $q6 = Math::Quaternion->new({ # Make a quaternion corresponding
107 axis => [ 1,2,3], # to a rotation of 0.2 radians
108 angle => 0.2, # about the vector (1,2,3).
109 });
110
111 my $q7 = Math::Quaternion->new({ # Make a quaternion which would
112 'v1' => [ 0,1,2], # rotate the vector (0,1,2) onto
113 'v2' => [ -1,2,0], # the vector (-1,2,0).
114 });
115
116 If no parameters are given, a unit quaternion is returned. If one
117 non-reference parameter is given, a "scalar" quaternion is returned. If
118 one parameter is given and it is a reference to a quaternion or an
119 array of four numbers, the corresponding quaternion object is returned.
120 If three parameters are given, a "vector" quaternion is returned. If
121 four parameters are given, the corresponding quaternion is returned.
122
123 Rotation quaternions may also be created by passing a hashref with the
124 axis and angle of rotation, or by specifying two vectors specifying
125 start and finish directions. Bear in mind that the latter method will
126 take the shortest path between the two vectors, ignoring the "roll"
127 angle.
128
129 unit
130 Returns a unit quaternion.
131
132 my $u = Math::Quaternion->unit; # Returns the quaternion (1,0,0,0).
133
134 conjugate
135 Returns the conjugate of its argument.
136
137 my $q = Math::Quaternion->new(1,2,3,4);
138 my $p = $q->conjugate; # (1,-2,-3,-4)
139
140 inverse
141 Returns the inverse of its argument.
142
143 my $q = Math::Quaternion->new(1,2,3,4);
144 my $qi = $q->inverse;
145
146 normalize
147 Returns its argument, normalized to unit norm.
148
149 my $q = Math::Quaternion->new(1,2,3,4);
150 my $qn = $q->normalize;
151
152 modulus
153 Returns the modulus of its argument, defined as the square root of the
154 scalar obtained by multiplying the quaternion by its conjugate.
155
156 my $q = Math::Quaternion->new(1,2,3,4);
157 print $q->modulus;
158
159 isreal
160 Returns 1 if the given quaternion is real ,ie has no quaternion part,
161 or else 0.
162
163 my $q1 = Math::Quaternion->new(1,2,3,4);
164 my $q2 = Math::Quaternion->new(5,0,0,0);
165 print $q1->isreal; # 1;
166 print $q2->isreal; # 0;
167
168 multiply
169 Performs a quaternion multiplication of its two arguments. If one of
170 the arguments is a scalar, then performs a scalar multiplication
171 instead.
172
173 my $q1 = Math::Quaternion->new(1,2,3,4);
174 my $q2 = Math::Quaternion->new(5,6,7,8);
175 my $q3 = Math::Quaternion::multiply($q1,$q2); # (-60 12 30 24)
176 my $q4 = Math::Quaternion::multiply($q1,$q1->inverse); # (1 0 0 0)
177
178 dot
179 Returns the dot product of two quaternions.
180
181 my $q1=Math::Quaternion->new(1,2,3,4);
182 my $q2=Math::Quaternion->new(2,4,5,6);
183 my $q3 = Math::Quaternion::dot($q1,$q2);
184
185 plus
186 Performs a quaternion addition of its two arguments.
187
188 my $q1 = Math::Quaternion->new(1,2,3,4);
189 my $q2 = Math::Quaternion->new(5,6,7,8);
190 my $q3 = Math::Quaternion::plus($q1,$q2); # (6 8 10 12)
191
192 minus
193 Performs a quaternion subtraction of its two arguments.
194
195 my $q1 = Math::Quaternion->new(1,2,3,4);
196 my $q2 = Math::Quaternion->new(5,6,7,8);
197 my $q3 = Math::Quaternion::minus($q1,$q2); # (-4 -4 -4 -4)
198
199 power
200 Raise a quaternion to a scalar or quaternion power.
201
202 my $q1 = Math::Quaternion->new(1,2,3,4);
203 my $q2 = Math::Quaternion::power($q1,4); # ( 668 -224 -336 -448 )
204 my $q3 = $q1->power(4); # ( 668 -224 -336 -448 )
205 my $q4 = $q1**(-1); # Same as $q1->inverse
206
207 use Math::Trig;
208 my $q5 = exp(1)**( Math::Quaternion->new(pi,0,0) ); # approx (-1 0 0 0)
209
210 negate
211 Negates the given quaternion.
212
213 my $q = Math::Quaternion->new(1,2,3,4);
214 my $q1 = $q->negate; # (-1,-2,-3,-4)
215
216 squarednorm
217 Returns the squared norm of its argument.
218
219 my $q1 = Math::Quaternion->new(1,2,3,4);
220 my $sn = $q1->squarednorm; # 30
221
222 scale
223 Performs a scalar multiplication of its two arguments.
224
225 my $q = Math::Quaternion->new(1,2,3,4);
226 my $qq = Math::Quaternion::scale($q,2); # ( 2 4 6 8)
227 my $qqq= $q->scale(3); # ( 3 6 9 12 )
228
229 rotation
230 Generates a quaternion corresponding to a rotation.
231
232 If given three arguments, interprets them as an angle and the three
233 components of an axis vector.
234
235 use Math::Trig; # Define pi. my $theta = pi/2;
236 # Angle of rotation my $rotquat =
237 Math::Quaternion::rotation($theta,0,0,1);
238
239 # $rotquat now represents a rotation of 90 degrees about Z axis.
240
241 my ($x,$y,$z) = (1,0,0); # Unit vector in the X direction.
242 my ($xx,$yy,$zz) = $rotquat->rotate_vector($x,$y,$z);
243
244 # ($xx,$yy,$zz) is now ( 0, 1, 0), to within floating-point error.
245
246 rotation() can also be passed a scalar angle and a reference to a
247 vector (in either order), and will generate the corresponding rotation
248 quaternion.
249
250 my @axis = (0,0,1); # Rotate about Z axis
251 $theta = pi/2;
252 $rotquat = Math::Quaternion::rotation($theta,\@axis);
253
254 If the arguments to rotation() are both references, they are
255 interpreted as two vectors, and a quaternion is returned which rotates
256 the first vector onto the second.
257
258 my @startvec = (0,1,0); # Vector pointing north
259 my @endvec = (-1,0,0); # Vector pointing west
260 $rotquat = Math::Quaternion::rotation(\@startvec,\@endvec);
261
262 my @newvec = $rotquat->rotate_vector(@startvec); # Same as @endvec
263
264 rotation_angle
265 Returns the angle of rotation represented by the quaternion argument.
266
267 my $q = Math::Quaternion::rotation(0.1,2,3,4);
268 my $theta = $q->rotation_angle; # Returns 0.1 .
269
270 rotation_axis
271 Returns the unit vector representing the axis about which rotations
272 will be performed, for the rotation represented by the quaternion
273 argument.
274
275 my $q = Math::Quaternion::rotation(0.1,1,1,0);
276 my @v = $q->rotation_axis; # Returns (0.5*sqrt(2),0.5*sqrt(2),0)
277
278 rotate_vector
279 When called as a method on a rotation quaternion, uses this quaternion
280 to perform the corresponding rotation on the vector argument.
281
282 use Math::Trig; # Define pi.
283
284 my $theta = pi/2; # Rotate 90 degrees
285
286 my $rotquat = Math::Quaternion::rotation($theta,0,0,1); # about Z axis
287
288 my ($x,$y,$z) = (1,0,0); # Unit vector in the X direction.
289 my ($xx,$yy,$zz) = $rotquat->rotate_vector($x,$y,$z)
290
291 # ($xx,$yy,$zz) is now ( 0, 1, 0), to within floating-point error.
292
293 matrix4x4
294 Takes one argument: a rotation quaternion. Returns a 16-element array,
295 equal to the OpenGL matrix which represents the corresponding rotation.
296
297 my $rotquat = Math::Quaternion::rotation($theta,@axis); # My rotation.
298 my @m = $rotquat->matrix4x4;
299
300 matrix3x3
301 Takes one argument: a rotation quaternion. Returns a 9-element array,
302 equal to the 3x3 matrix which represents the corresponding rotation.
303
304 my $rotquat = Math::Quaternion::rotation($theta,@axis); # My rotation.
305 my @m = $rotquat->matrix3x3;
306
307 matrix4x4andinverse
308 Similar to matrix4x4, but returnes a list of two array references. The
309 first is a reference to the rotation matrix; the second is a reference
310 to its inverse. This may be useful when rendering sprites, since you
311 can multiply by the rotation matrix for the viewer position, perform
312 some translations, and then multiply by the inverse: any resulting
313 rectangles drawn will always face the viewer.
314
315 my $rotquat = Math::Quaternion::rotation($theta,@axis); # My rotation.
316 my ($matref,$invref) = $rotquat->matrix4x4andinverse;
317
318 stringify
319 Returns a string representation of the quaternion. This is used to
320 overload the '""' operator, so that quaternions may be freely
321 interpolated in strings.
322
323 my $q = Math::Quaternion->new(1,2,3,4);
324 print $q->stringify; # "( 1 2 3 4 )"
325 print "$q"; # "( 1 2 3 4 )"
326
327 slerp
328 Takes two quaternion arguments and one scalar; performs spherical
329 linear interpolation between the two quaternions. The quaternion
330 arguments are assumed to be unit quaternions, and the scalar is assumed
331 to lie between 0 and 1: a scalar argument of zero will return the first
332 quaternion argument, and a scalar argument of one will return the
333 second.
334
335 use Math::Trig;
336 my @axis = (0,0,1);
337 my $rq1 = Math::Quaternion::rotation(pi/2,\@axis); # 90 degs about Z
338 my $rq2 = Math::Quaternion::rotation(pi,\@axis); # 180 degs about Z
339
340 my $interp = Math::Quaternion::slerp($rq1,$rq2,0.5); # 135 degs about Z
341
342 exp
343 Exponential operator e^q. Any quaternion q can be written as x+uy,
344 where x is a real number, and u is a unit pure quaternion. Then, exp(q)
345 == exp(x) * ( cos(y) + u sin(y) ).
346
347 my $q = Math::Quaternion->new(1,2,3,4);
348 print Math::Quaternion::exp($q);
349
350 log
351 Returns the logarithm of its argument. The logarithm of a negative real
352 quaternion can take any value of them form (log(-q0),u*pi) for any unit
353 vector u. In these cases, u is chosen to be (1,0,0).
354
355 my $q = Math::Quaternion->new(1,2,3,4);
356 print Math::Quaternion::log($q);
357
358 AUTHOR
359 Jonathan Chin, <[email protected]>
360
361 ACKNOWLEDGEMENTS
362 Thanks to Rene Uittenbogaard for useful suggestions.
363
364 SEE ALSO
365 <http://mathworld.wolfram.com/Quaternion.html>
366 <http://sjbaker.org/steve/omniv/eulers_are_evil.html>
367 Acts 12:4
368
369 COPYRIGHT AND LICENSE
370 Copyright 2003 by Jonathan Chin
371
372 This library is free software; you can redistribute it and/or modify it
373 under the same terms as Perl itself.
374