blib/lib/Math/Quaternion.pm

filter: Statementstmt 98.7% 234 / 237 Branchbran 93.4% 71 / 76 Conditioncond 48.4% 16 / 33 MC/DCmcdc 18.7% 3 / 16 Subroutinesub 100% 33 / 33 Podpod 100% 26 / 26 totaltotal 90.9% 383 / 421 CC 50 SCAR 22.7
CC
50
Cov
93%
rotation
25.9
new
11.0
power
5.0
CRAP 9.7
↑ Summary
1 package Math::Quaternion;
2  
3 2 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Math::Trig; # What?!? Where's acos()? You can't have cos and not acos!
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 require Exporter;
10 use overload
11 2 ▶ '+' => \&plus,
Subroutine BEGIN: called CC=1 SCAR=0.0
12 '-' => \&minus,
13 9 ▶ 'bool' => sub { 1; }, # So we can do if ($foo=Math::Quaternion->new) { .. }
Subroutine __ANON__: called CC=1 SCAR=0.0
14 2 '""' => \&stringify,
15 '*' => \&multiply,
16 '~' => \&conjugate,
17 'abs' => \&modulus,
18 'neg' => \&negate,
19 '**' => \&power,
20 'exp' => \&exp,
21 'log' => \&log,
22 2 ;
23  
24 2 our @ISA = qw(Exporter);
25  
26 # Items to export into callers namespace by default. Note: do not export
27 # names by default without a very good reason. Use EXPORT_OK instead.
28 # Do not simply export all your public functions/methods/constants.
29  
30 # This allows declaration use Math::Quaternion ':all';
31 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
32 # will save memory.
33  
34  
35 2 our %EXPORT_TAGS = ( 'all' => [ qw(
36 unit
37 conjugate
38 inverse
39 normalize
40 modulus
41 isreal
42 multiply
43 dot
44 plus
45 minus
46 power
47 negate
48 squarednorm
49 scale
50 rotation
51 rotation_angle
52 rotation_axis
53 rotate_vector
54 matrix4x4
55 matrix3x3
56 matrix4x4andinverse
57 stringify
58 slerp
59 exp
60 log
61 ) ],
62 );
63  
64 2 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
65  
66 2 our @EXPORT = qw(
67
68 );
69  
70 2 our $VERSION = '0.07';
71  
72  
73 # Preloaded methods go here.
74  
75  
76 # Below is stub documentation for your module. You'd better edit it!
77  
78 =head1 NAME
79  
80 Math::Quaternion - Perl class to represent quaternions
81  
82 =head1 SYNOPSIS
83  
84 use Math::Quaternion qw(slerp);
85 my $q = Math::Quaternion->new; # Make a new unit quaternion
86
87 # Make a rotation about the axis (0,1,0)
88 my $q2 = Math::Quaternion->new({axis=>[0,1,0],angle=>0.1});
89 my @v = (1,2,3); # A vector.
90 my @vrotated = $q2->rotate_vector(@v); # Rotate @v about (0,1,0).
91
92 my $q3 = Math::Quaternion::rotation(0.7,2,1,4); # A different rotation.
93 my $q4 = slerp($q2,$q3,0.5); # Interpolated rotation.
94 my @vinterp = $q4->rotate_vector(@v);
95  
96  
97 =head1 DESCRIPTION
98  
99 This package lets you create and manipulate quaternions. A
100 quaternion is a mathematical object developed as a kind of
101 generalization of complex numbers, usually represented by an array
102 of four real numbers, and is often used to represent rotations in
103 three-dimensional space.
104  
105 See, for example, L<http://mathworld.wolfram.com/Quaternion.html> for
106 more details on the mathematics of quaternions.
107  
108 Quaternions can be added, subtracted, and scaled just like complex
109 numbers or vectors -- they can also be multiplied, but quaternion
110 multiplication DOES NOT COMMUTE. That is to say, if you have
111 quaternions $q1 and $q2, then in general $q1*$q2 != $q2*$q1. This is
112 related to their use in representing rotations, which also do not
113 commute.
114  
115 If you just want to represent rotations and don't care about the
116 internal mathematical details, this should be all you need to know:
117  
118 All quaternions have a quantity called the "norm", similar to the
119 length of a vector. A quaternion with norm equal to 1 is called a
120 "unit quaternion". All quaternions which represent rotations are
121 unit quaternions.
122  
123 If you call new() without any arguments, it will give you a unit
124 quaternion which represents no rotation:
125
126 $q = Math::Quaternion->new;
127  
128 You can make a quaternion which represents a rotation of a given
129 angle (in radians) about a given axis:
130  
131 $qrot = Math::Quaternion->new({ angle => 0.1, axis => [ 2,3,4]});
132  
133 Say you have two rotations, $q1 and $q2, and you want to make a
134 quaternion representing a rotation of $q1 followed by $q2. Then, you
135 do:
136  
137 $q3 = $q2 * $q1; # Rotate by $q1, followed by $q2.
138  
139 Remember that this is NOT the same as $q1 * $q2, which will reverse
140 the order of the rotations.
141  
142 If you perform many iterated quaternion operations, the result may
143 not quite be a unit quaternion due to numerical inaccuracies. You
144 can make sure any quaternion has unit length, by doing:
145  
146 $unitquat = $anyquat->normalize;
147  
148 If you have a rotation quaternion, and you want to find the 3x3
149 matrix which represents the corresponding rotation, then:
150  
151 @matrix = $q->matrix3x3;
152  
153 Similarly, you can generate a 4x4 matrix of the sort you'd pass to
154 OpenGL:
155  
156 @glmatrix = $q->matrix4x4;
157  
158 If you have a vector representing a direction, and you want to
159 rotate the vector by a quaternion $q:
160  
161 my @vector = (0,0,1); # Vector pointing in the Z direction.
162
163 my @newvec = $q->rotate_vector(@vector); # New direction.
164  
165 Say you're using quaternions to represent the orientation of a
166 camera, and you have two quaternions: one to represent a
167 starting orientation, and another to represent a finishing
168 position. If you want to find all the quaternions representing
169 the orientations in between, allowing your camera to move
170 smoothly from start to finish, use the slerp() routine:
171  
172 use Math::Quaternion qw(slerp);
173
174 my ($qstart, $qend) = ... ;
175
176 # Set $tween to 9 points between start and end, exclusive.
177
178 for my $t (1..9) {
179 my $tween = slerp($qstart,$qend,0.1*$t);
180 ...
181 }
182  
183  
184 =head1 METHODS
185  
186 =over 1
187  
188 =item B<new>
189  
190 my $q = Math::Quaternion->new; # Make a new unit quaternion.
191 my $q2 = Math::Quaternion->new(1,2,3,4);# Make a specific quaternion.
192 my $q3 = Math::Quaternion->new($q2); # Copy an existing quaternion.
193 my $q4 = Math::Quaternion->new(5.6); # Make the quaternion (5.6,0,0,0)
194 my $q5 = Math::Quaternion->new(7,8,9); # Make the quaternion (0,7,8,9)
195
196 my $q6 = Math::Quaternion->new({ # Make a quaternion corresponding
197 axis => [ 1,2,3], # to a rotation of 0.2 radians
198 angle => 0.2, # about the vector (1,2,3).
199 });
200
201 my $q7 = Math::Quaternion->new({ # Make a quaternion which would
202 'v1' => [ 0,1,2], # rotate the vector (0,1,2) onto
203 'v2' => [ -1,2,0], # the vector (-1,2,0).
204 });
205  
206 If no parameters are given, a unit quaternion is returned. If one
207 non-reference parameter is given, a "scalar" quaternion is returned.
208 If one parameter is given and it is a reference to a quaternion or
209 an array of four numbers, the corresponding quaternion object is
210 returned. If three parameters are given, a "vector" quaternion is
211 returned. If four parameters are given, the corresponding
212 quaternion is returned.
213  
214 Rotation quaternions may also be created by passing a hashref with
215 the axis and angle of rotation, or by specifying two vectors
216 specifying start and finish directions. Bear in mind that the latter
217 method will take the shortest path between the two vectors, ignoring
218 the "roll" angle.
219  
220 =cut
221  
222 294 ▶sub new {
Subroutine new: called CC=11 SCAR=24.0
Pod documented
223 294 my $class = shift;
224  
225 294 my $arr=undef;
226  
227 294 ▶ if (0==@_) {
Branch: 4 branches
BranchTrueFalseTotal
if (0 == @_) { } 119 175 294
elsif (1 == @_) { } 32 143 175
elsif (3 == @_) { } 6 137 143
elsif (4 == @_) { } 136 1 137
228 # No arguments, default to unit quaternion.
229 119 $arr = [ 1,0,0,0];
230 } elsif (1==@_) {
231 # One argument: if it's not a reference, construct
232 # a "scalar quaternion" (x 0 0 0).
233 32 my $arg = $_[0];
234 32 my $reftype = ref($arg);
235  
236 32 ▶ if (!$reftype) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $reftype) { } 12 20 32
237 12 $arr = [ $arg,0,0,0];
238 } else {
239 # We've been passed a reference. If it's an array
240 # ref, then construct a quaternion out of the
241 # corresponding array.
242 20 ▶ if ("ARRAY" eq $reftype) {
Branch: 3 branches
BranchTrueFalseTotal
if ('ARRAY' eq $reftype) { } 2 18 20
elsif ('Math::Quaternion' eq $reftype) { } 11 7 18
elsif ('HASH' eq $reftype) { } 7 0 7
243 2 return Math::Quaternion->new(@$arg);
244 } elsif ("Math::Quaternion" eq $reftype) {
245 # If it's a reference to another quaternion,
246 # copy it.
247 11 return Math::Quaternion->new(@$arg);
248 } elsif ("HASH" eq $reftype) {
249 # Hashref.
250 7 my %hash = %$arg;
251 7 ▶ if (defined($hash{'axis'})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $hash{'axis'}) { } 3 4 7
elsif (defined $hash{'v2'}) { } 3 1 4
252 # Construct a rotation.
253 return rotation(
254 $hash{'angle'},
255 3 @{$hash{'axis'}}
256 );
257 } elsif (defined($hash{'v2'})) {
258 return rotation(
259 3 $hash{'v1'},$hash{'v2'}
260 );
261 }
262 }
263 1 croak("Don't understand arguments to new()");
264  
265 }
266 } elsif (3==@_) {
267 # Three arguments: construct a quaternion to represent
268 # the corresponding vector.
269 6 $arr = [ 0, @_[0,1,2] ];
270 } elsif (4==@_) {
271 # Four arguments: just slot the numbers right in.
272 136 $arr = [ @_[0,1,2,3] ];
273 } else {
274 1 croak("Don't understand arguments passed to new()");
275 }
276
277  
278 273 bless $arr, $class;
279  
280 }
281  
282 =item B<unit>
283  
284 Returns a unit quaternion.
285  
286 my $u = Math::Quaternion->unit; # Returns the quaternion (1,0,0,0).
287  
288 =cut
289  
290 1 ▶sub unit {
Subroutine unit: called CC=1 SCAR=0.0
Pod documented
291 1 my $class = shift;
292  
293 1 bless [ 1,0,0,0 ], $class;
294 }
295  
296 =item B<conjugate>
297  
298 Returns the conjugate of its argument.
299  
300 my $q = Math::Quaternion->new(1,2,3,4);
301 my $p = $q->conjugate; # (1,-2,-3,-4)
302  
303 =cut
304  
305 12 ▶sub conjugate {
Subroutine conjugate: called CC=1 SCAR=0.0
Pod documented
306 12 my $q=shift;
307  
308 12 return Math::Quaternion->new(
309 $q->[0],
310 - $q->[1],
311 - $q->[2],
312 - $q->[3],
313 );
314 }
315  
316 =item B<inverse>
317  
318 Returns the inverse of its argument.
319  
320 my $q = Math::Quaternion->new(1,2,3,4);
321 my $qi = $q->inverse;
322  
323 =cut
324  
325 6 ▶sub inverse {
Subroutine inverse: called CC=1 SCAR=0.0
Pod documented
326 6 my $q = shift;
327  
328 6 return scale(conjugate($q),1.0/squarednorm($q));
329  
330 }
331  
332  
333 =item B<normalize>
334  
335 Returns its argument, normalized to unit norm.
336  
337 my $q = Math::Quaternion->new(1,2,3,4);
338 my $qn = $q->normalize;
339  
340 =cut
341  
342 5 ▶sub normalize {
Subroutine normalize: called CC=1 SCAR=0.0
Pod documented
343 5 my $q = shift;
344 5 return scale($q,1.0/sqrt(squarednorm($q)));
345 }
346  
347 =item B<modulus>
348  
349 Returns the modulus of its argument, defined as the
350 square root of the scalar obtained by multiplying the quaternion
351 by its conjugate.
352  
353 my $q = Math::Quaternion->new(1,2,3,4);
354 print $q->modulus;
355  
356 =cut
357  
358 3 ▶sub modulus {
Subroutine modulus: called CC=1 SCAR=0.0
Pod documented
359 3 my $q = shift;
360 3 return sqrt(squarednorm($q));
361 }
362  
363 =item B<isreal>
364  
365 Returns 1 if the given quaternion is real ,ie has no quaternion
366 part, or else 0.
367  
368 my $q1 = Math::Quaternion->new(1,2,3,4);
369 my $q2 = Math::Quaternion->new(5,0,0,0);
370 print $q1->isreal; # 0;
371 print $q2->isreal; # 1;
372  
373 =cut
374  
375 43 ▶sub isreal {
Subroutine isreal: called CC=4 SCAR=14.0
Pod documented
376 43 my $q = shift;
377 43 my ($q0,$q1,$q2,$q3)=@$q;
378  
379 43 ▶ if ( (0.0==$q1) && (0.0==$q2) && (0.0==$q3) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $q1 and 0 == $q2 and 0 == $q3) { } 9 34 43
Condition66%4/6
0 == $q1 and 0 == $q2 and 0 == $q3
!ll&&!rl&&r
3409
0 == $q1 and 0 == $q2
!ll&&!rl&&r
3409
MC/DC33%1/3
0 == $q1 and 0 == $q2 and 0 == $q3
0 == $q1 0 == $q2 0 == $q3
Truth table50%2/4
A and B and C
A= 0 == $q1B= 0 == $q2C= 0 == $q3
ABCresult
0XX0
10X0
1100
1111
380 9 return 1;
381 } else {
382 34 return 0;
383 }
384 }
385  
386 =item B<multiply>
387  
388 Performs a quaternion multiplication of its two arguments.
389 If one of the arguments is a scalar, then performs a scalar
390 multiplication instead.
391  
392 my $q1 = Math::Quaternion->new(1,2,3,4);
393 my $q2 = Math::Quaternion->new(5,6,7,8);
394 my $q3 = Math::Quaternion::multiply($q1,$q2); # (-60 12 30 24)
395 my $q4 = Math::Quaternion::multiply($q1,$q1->inverse); # (1 0 0 0)
396  
397 =cut
398  
399 122 ▶sub multiply {
Subroutine multiply: called CC=4 SCAR=13.9
Pod documented
400 122 my ($a,$b,$reversed) = @_;
401 122 ▶ ($a,$b) = ($b,$a) if $reversed;
Branch: 1 branch
BranchTrueFalseTotal
if $reversed 1 121 122
402  
403 122 ▶ if (!ref $a) { return scale($b,$a); }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $a) 2 120 122
404 120 ▶ if (!ref $b) { return scale($a,$b); }
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $b) 2 118 120
405  
406 118 my $q = new Math::Quaternion;
407  
408 118 $q->[0] = $a->[0] * $b->[0]
409 - $a->[1]*$b->[1]
410 - $a->[2]*$b->[2]
411 - $a->[3]*$b->[3];
412
413 118 $q->[1] = $a->[0] * $b->[1]
414 + $b->[0] * $a->[1]
415 + $a->[2] * $b->[3] - $a->[3] * $b->[2];
416  
417 118 $q->[2] = $a->[0] * $b->[2]
418 + $b->[0] * $a->[2]
419 + $a->[3] * $b->[1] - $a->[1] * $b->[3];
420  
421 118 $q->[3] = $a->[0] * $b->[3]
422 + $b->[0] * $a->[3]
423 + $a->[1] * $b->[2] - $a->[2] * $b->[1];
424 118 return $q;
425 }
426  
427 =item B<dot>
428  
429 Returns the dot product of two quaternions.
430  
431 my $q1=Math::Quaternion->new(1,2,3,4);
432 my $q2=Math::Quaternion->new(2,4,5,6);
433 my $q3 = Math::Quaternion::dot($q1,$q2);
434  
435 =cut
436  
437 4 ▶sub dot {
Subroutine dot: called CC=1 SCAR=0.0
Pod documented
438 4 my ($q1,$q2) = @_;
439 4 my ($a0,$a1,$a2,$a3) = @$q1;
440 4 my ($b0,$b1,$b2,$b3) = @$q2;
441 4 return $a0*$b0 + $a1*$b1 + $a2*$b2 + $a3*$b3 ;
442 }
443  
444 =item B<plus>
445  
446 Performs a quaternion addition of its two arguments.
447  
448 my $q1 = Math::Quaternion->new(1,2,3,4);
449 my $q2 = Math::Quaternion->new(5,6,7,8);
450 my $q3 = Math::Quaternion::plus($q1,$q2); # (6 8 10 12)
451  
452 =cut
453  
454  
455 18 ▶sub plus {
Subroutine plus: called CC=1 SCAR=0.0
Pod documented
456 18 my ($a,$b,$reversed)=@_;
457 18 my $q = Math::Quaternion->new(
458 $a->[0] + $b->[0],
459 $a->[1] + $b->[1],
460 $a->[2] + $b->[2],
461 $a->[3] + $b->[3],
462 );
463  
464 18 return $q;
465  
466 }
467  
468 =item B<minus>
469  
470 Performs a quaternion subtraction of its two arguments.
471  
472 my $q1 = Math::Quaternion->new(1,2,3,4);
473 my $q2 = Math::Quaternion->new(5,6,7,8);
474 my $q3 = Math::Quaternion::minus($q1,$q2); # (-4 -4 -4 -4)
475  
476 =cut
477  
478 3 ▶sub minus {
Subroutine minus: called CC=2 SCAR=7.0
Pod documented
479 3 my ($a,$b,$reversed)=@_;
480 3 ▶ ($a,$b) = ($b,$a) if $reversed;
Branch: 1 branch
BranchTrueFalseTotal
if $reversed 0 3 3
481 3 my $q = Math::Quaternion->new(
482 $a->[0] - $b->[0],
483 $a->[1] - $b->[1],
484 $a->[2] - $b->[2],
485 $a->[3] - $b->[3],
486 );
487  
488 3 return $q;
489  
490 }
491  
492 =item B<power>
493  
494 Raise a quaternion to a scalar or quaternion power.
495  
496 my $q1 = Math::Quaternion->new(1,2,3,4);
497 my $q2 = Math::Quaternion::power($q1,4); # ( 668 -224 -336 -448 )
498 my $q3 = $q1->power(4); # ( 668 -224 -336 -448 )
499 my $q4 = $q1**(-1); # Same as $q1->inverse
500  
501 use Math::Trig;
502 my $q5 = exp(1)**( Math::Quaternion->new(pi,0,0) ); # approx (-1 0 0 0)
503  
504 =cut
505  
506 14 ▶sub power {
Subroutine power: called CC=5 SCAR=16.1
Pod documented
507 14 my ($a,$b,$reversed)=@_;
508 14 ▶ ($a,$b) = ($b,$a) if $reversed;
Branch: 1 branch
BranchTrueFalseTotal
if $reversed 1 13 14
509  
510 14 ▶ if (ref $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a) 10 4 14
511 10 $a = Math::Quaternion->new($a);
512 }
513  
514 14 ▶ if (ref $b) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $b) 7 7 14
515 # For quaternion^quaternion, use exp and log.
516 7 return Math::Quaternion::exp(Math::Quaternion::multiply($b,Math::Quaternion::log($a)));
517 }
518  
519 # For real_quaternion^real_number, use built-in power.
520 7 ▶ if ($a->isreal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->isreal) 1 6 7
521 1 return Math::Quaternion->new( $a->[0] ** $b, 0, 0, 0 ) ;
522 }
523  
524 # For quat raised to a scalar power, do it manually.
525  
526 6 my ($a0,$a1,$a2,$a3) = @$a;
527  
528 6 my $s = sqrt($a->squarednorm);
529 6 my $theta = Math::Trig::acos($a0/$s);
530 6 my $vecmod = sqrt($a1*$a1+$a2*$a2+$a3*$a3);
531 6 my $stob = ($s**$b);
532 6 my $coeff = $stob/$vecmod*sin($b*$theta);
533
534 6 my $u1 = $a1*$coeff;
535 6 my $u2 = $a2*$coeff;
536 6 my $u3 = $a3*$coeff;
537  
538  
539 6 return Math::Quaternion->new(
540 $stob * cos($b*$theta), $u1,$u2,$u3
541 );
542
543  
544 }
545  
546 =item B<negate>
547  
548 Negates the given quaternion.
549  
550 my $q = Math::Quaternion->new(1,2,3,4);
551 my $q1 = $q->negate; # (-1,-2,-3,-4)
552  
553 =cut
554  
555 3 ▶sub negate {
Subroutine negate: called CC=1 SCAR=0.0
Pod documented
556  
557 3 my $q = shift;
558 3 return Math::Quaternion->new(
559 -($q->[0]),
560 -($q->[1]),
561 -($q->[2]),
562 -($q->[3]),
563 );
564  
565 }
566  
567  
568 =item B<squarednorm>
569  
570 Returns the squared norm of its argument.
571  
572 my $q1 = Math::Quaternion->new(1,2,3,4);
573 my $sn = $q1->squarednorm; # 30
574  
575 =cut
576  
577 24 ▶sub squarednorm {
Subroutine squarednorm: called CC=1 SCAR=0.0
Pod documented
578 24 my $q = shift;
579 24 return $q->[0]*$q->[0]
580 + $q->[1]*$q->[1]
581 + $q->[2]*$q->[2]
582 + $q->[3]*$q->[3];
583  
584 }
585  
586 =item B<scale>
587  
588 Performs a scalar multiplication of its two arguments.
589  
590 my $q = Math::Quaternion->new(1,2,3,4);
591 my $qq = Math::Quaternion::scale($q,2); # ( 2 4 6 8)
592 my $qqq= $q->scale(3); # ( 3 6 9 12 )
593  
594 =cut
595  
596 24 ▶sub scale {
Subroutine scale: called CC=1 SCAR=0.0
Pod documented
597 24 my ($q,$s)=@_;
598 24 return Math::Quaternion->new(
599 $q->[0] * $s,
600 $q->[1] * $s,
601 $q->[2] * $s,
602 $q->[3] * $s
603 );
604 }
605  
606 =item B<rotation>
607  
608  
609 Generates a quaternion corresponding to a rotation.
610  
611 If given three arguments, interprets them as an angle and the
612 three components of an axis vector.
613  
614 use Math::Trig; # Define pi. my $theta = pi/2;
615 # Angle of rotation my $rotquat =
616 Math::Quaternion::rotation($theta,0,0,1);
617
618 # $rotquat now represents a rotation of 90 degrees about Z axis.
619
620 my ($x,$y,$z) = (1,0,0); # Unit vector in the X direction.
621 my ($xx,$yy,$zz) = $rotquat->rotate_vector($x,$y,$z);
622
623 # ($xx,$yy,$zz) is now ( 0, 1, 0), to within floating-point error.
624  
625  
626 rotation() can also be passed a scalar angle and a reference to
627 a vector (in either order), and will generate the corresponding
628 rotation quaternion.
629  
630 my @axis = (0,0,1); # Rotate about Z axis
631 $theta = pi/2;
632 $rotquat = Math::Quaternion::rotation($theta,\@axis);
633  
634  
635 If the arguments to rotation() are both references, they are
636 interpreted as two vectors, and a quaternion is returned which
637 rotates the first vector onto the second.
638  
639 my @startvec = (0,1,0); # Vector pointing north
640 my @endvec = (-1,0,0); # Vector pointing west
641 $rotquat = Math::Quaternion::rotation(\@startvec,\@endvec);
642
643 my @newvec = $rotquat->rotate_vector(@startvec); # Same as @endvec
644  
645 =cut
646  
647 15 ▶sub rotation {
Subroutine rotation: called CC=21 SCAR=32.5
Pod documented
648 15 my ($theta,$x,$y,$z);
649 15 ▶ if (2==@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (2 == @_) { } 10 5 15
elsif (4 == @_) { } 4 1 5
650 10 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 5 5 10
651 5 ▶ if (ref($_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1]) { } 4 1 5
652 # Both args references to vectors
653 4 my ($ax,$ay,$az)=@{$_[0]};
654 4 my ($bx,$by,$bz)=@{$_[1]};
655  
656 4 ▶ if ( (($ax == 0) and ($ay == 0) and ($az == 0)) or
Branch: 1 branch
BranchTrueFalseTotal
if ($ax == 0 and $ay == 0 and $az == 0 or $bx == 0 and $by == 0 and $bz == 0) 0 4 4
Condition33%5/15
$ax == 0 and $ay == 0 and $az == 0 or $bx == 0 and $by == 0 and $bz == 0
l!l&&r!l&&!r
004
$ax == 0 and $ay == 0 and $az == 0
!ll&&!rl&&r
400
$ax == 0 and $ay == 0
!ll&&!rl&&r
400
$bx == 0 and $by == 0 and $bz == 0
!ll&&!rl&&r
400
$bx == 0 and $by == 0
!ll&&!rl&&r
400
MC/DC0%0/6
$ax == 0 and $ay == 0 and $az == 0 or $bx == 0 and $by == 0 and $bz == 0
$ax == 0 $ay == 0 $az == 0 $bx == 0 $by == 0 $bz == 0
Truth table7%1/13
A and B and C or D and E and F
A= $ax == 0B= $ay == 0C= $az == 0D= $bx == 0E= $by == 0F= $bz == 0
ABCDEFresult
111XXX1
0XX1111
10X1111
1101111
0XX0XX0
0XX10X0
0XX1100
10X0XX0
10X10X0
10X1100
1100XX0
11010X0
1101100
657 (($bx == 0) and ($by == 0) and ($bz == 0)) ) {
658 0 croak("Math::Quaternion::rotation() passed zero-length vector");
659 }
660  
661 # Find cross product. This is a vector perpendicular to both
662 # argument vectors, and is therefore the axis of rotation.
663  
664 4 $x = $ay*$bz-$az*$by;
665 4 $y = $az*$bx-$ax*$bz;
666 4 $z = $ax*$by-$ay*$bx;
667  
668 # find the dot product.
669  
670 4 my $dotprod = $ax*$bx+$ay*$by+$az*$bz;
671 4 my $mod1 = sqrt($ax*$ax+$ay*$ay+$az*$az);
672 4 my $mod2 = sqrt($bx*$bx+$by*$by+$bz*$bz);
673  
674 # Find the angle of rotation.
675 4 $theta=Math::Trig::acos($dotprod/($mod1*$mod2));
676
677 # Check for parallel vectors (cross product is zero)
678  
679 4 ▶ if (($x == 0) and ($y == 0) and ($z == 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x == 0 and $y == 0 and $z == 0) 2 2 4
Condition66%4/6
$x == 0 and $y == 0 and $z == 0
!ll&&!rl&&r
202
$x == 0 and $y == 0
!ll&&!rl&&r
202
MC/DC33%1/3
$x == 0 and $y == 0 and $z == 0
$x == 0 $y == 0 $z == 0
Truth table50%2/4
A and B and C
A= $x == 0B= $y == 0C= $z == 0
ABCresult
0XX0
10X0
1100
1111
680  
681 # Vectors a and b are parallel, such that rotation
682 # vector is the zero-length vector (0,0,0), with
683 # theta either 0 or pi (if vectors are opposite).
684 # To remove round-off errors in theta, explicitly
685 # set it.
686  
687 2 ▶ $theta = $dotprod > 0 ? 0 : pi;
Branch: 1 branch
BranchTrueFalseTotal
$dotprod > 0 ? : 1 1 2
688  
689 # Such a zero-length rotation vector is annoying (e.g.
690 # division by 0 on normalization, and problems combining
691 # rotations). To solve this, select a random rotation
692 # vector that is also perpendicular to both parallel
693 # vectors a and b. This satisfies the rotation requirement,
694 # and helps programs relying on the logic that the rotation
695 # vector has to be perpendicular to both vectors given
696 # (even if there are an infinite amount of rotation vectors
697 # that would satisfy that condition). Algorithm: Find a
698 # random vector b at any non-zero angle to vector a. One of
699 # the main axis will do. To reduce round-off errors, make b
700 # as perpendicular as possible to a by selecting one of the
701 # smallest components of vector a as the main component of
702 # b. This also avoid accidentally selecting a vector
703 # parallel to a
704  
705 2 ▶ if ( (abs($ax) <= abs($ay)) and (abs($ax) <= abs($az)) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (abs $ax <= abs $ay and abs $ax <= abs $az) { } 1 1 2
elsif (abs $ay <= abs $ax and abs $ay <= abs $az) { } 1 0 1
Condition66%2/3
abs $ax <= abs $ay and abs $ax <= abs $az
!ll&&!rl&&r
101
MC/DC50%1/2
abs $ax <= abs $ay and abs $ax <= abs $az
abs $ax <= abs $ay abs $ax <= abs $az
Truth table66%2/3
A and B
A= abs $ax <= abs $ayB= abs $ax <= abs $az
ABresult
0X0
100
111
706 1 ($bx,$by,$bz)=(1,0,0);
707 ▶ } elsif ( (abs($ay) <= abs($ax)) and (abs($ay) <= abs($az)) ) {
Condition33%1/3
abs $ay <= abs $ax and abs $ay <= abs $az
!ll&&!rl&&r
001
MC/DC0%0/2
abs $ay <= abs $ax and abs $ay <= abs $az
abs $ay <= abs $ax abs $ay <= abs $az
Truth table33%1/3
A and B
A= abs $ay <= abs $axB= abs $ay <= abs $az
ABresult
0X0
100
111
708 1 ($bx,$by,$bz)=(0,1,0);
709 } else {
710 0 ($bx,$by,$bz)=(0,0,1);
711 }
712  
713 # Then, take the cross product between vector a and the new
714 # vector b, to generate some vector exactly perpendicular
715 # to vector a and hence also perpendicular to the original
716 # vector b (i.e. @{$_[1]})
717  
718 2 $x = $ay*$bz-$az*$by;
719 2 $y = $az*$bx-$ax*$bz;
720 2 $z = $ax*$by-$ay*$bx;
721  
722 # ($x,$y,$z) is now a random yet valid rotation vector
723 # perpendicular to the two original vectors.
724  
725 }
726 } else {
727 # 0 is a ref, 1 is not.
728 1 $theta = $_[1]; ($x,$y,$z)=@{$_[0]};
729 }
730 } else {
731 5 ▶ if (ref($_[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1]) { } 4 1 5
732 # 1 is a ref, 0 is not
733 4 $theta = $_[0]; ($x,$y,$z)=@{$_[1]};
734 } else {
735 1 croak("Math::Quaternion::rotation() passed 2 nonref args");
736 }
737 }
738 } elsif (4==@_) {
739 4 ($theta,$x,$y,$z) = @_;
740 } else {
741 1 croak("Math::Quaternion::rotation() passed wrong no of arguments");
742 }
743  
744 13 my $modulus = sqrt($x*$x+$y*$y+$z*$z); # Make it a unit vector
745 13 ▶ if ($modulus == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modulus == 0) 0 13 13
746 0 croak("Math::Quaternion::rotation() passed zero-length rotation vector");
747 }
748 13 $x /= $modulus;
749 13 $y /= $modulus;
750 13 $z /= $modulus;
751  
752 13 my $st = sin(0.5 * $theta);
753 13 my $ct = cos(0.5 * $theta);
754  
755 13 return Math::Quaternion->new(
756 $ct, $x * $st, $y * $st, $z * $st
757 );
758 }
759  
760 =item B<rotation_angle>
761  
762 Returns the angle of rotation represented by the quaternion
763 argument.
764  
765 my $q = Math::Quaternion::rotation(0.1,2,3,4);
766 my $theta = $q->rotation_angle; # Returns 0.1 .
767  
768 =cut
769  
770 4 ▶sub rotation_angle {
Subroutine rotation_angle: called CC=1 SCAR=0.0
Pod documented
771 4 my $q = shift;
772 4 return 2.0 * Math::Trig::acos($q->[0]);
773 }
774  
775 =item B<rotation_axis>
776  
777 Returns the unit vector representing the axis about which
778 rotations will be performed, for the rotation represented by the
779 quaternion argument.
780  
781 my $q = Math::Quaternion::rotation(0.1,1,1,0);
782 my @v = $q->rotation_axis; # Returns (0.5*sqrt(2),0.5*sqrt(2),0)
783  
784 =cut
785  
786 5 ▶sub rotation_axis {
Subroutine rotation_axis: called CC=2 SCAR=6.9
Pod documented
787 5 my $q = shift;
788 5 my $theta = Math::Trig::acos($q->[0]);
789 5 my $st = sin($theta);
790 5 ▶ if (0==$st) { return (0,0,1); } # Rotation of angle zero about Z axis
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $st) 1 4 5
791 4 my ($x,$y,$z) = @{$q}[1,2,3];
792  
793 4 return ( $x/$st, $y/$st, $z/$st );
794 }
795  
796  
797  
798  
799 =item B<rotate_vector>
800  
801 When called as a method on a rotation quaternion, uses this
802 quaternion to perform the corresponding rotation on the vector
803 argument.
804  
805 use Math::Trig; # Define pi.
806
807 my $theta = pi/2; # Rotate 90 degrees
808
809 my $rotquat = Math::Quaternion::rotation($theta,0,0,1); # about Z axis
810
811 my ($x,$y,$z) = (1,0,0); # Unit vector in the X direction.
812 my ($xx,$yy,$zz) = $rotquat->rotate_vector($x,$y,$z)
813
814 # ($xx,$yy,$zz) is now ( 0, 1, 0), to within floating-point error.
815  
816 =cut
817  
818  
819 3 ▶sub rotate_vector {
Subroutine rotate_vector: called CC=1 SCAR=0.0
Pod documented
820 3 my ($q,$x,$y,$z) = @_;
821  
822 3 my $p = Math::Quaternion->new($x,$y,$z);
823 3 my $qq = multiply($q,multiply($p,inverse($q)));
824 3 return @{$qq}[1,2,3];
825 }
826  
827  
828 =item B<matrix4x4>
829  
830 Takes one argument: a rotation quaternion.
831 Returns a 16-element array, equal to the OpenGL
832 matrix which represents the corresponding rotation.
833  
834 my $rotquat = Math::Quaternion::rotation($theta,@axis); # My rotation.
835 my @m = $rotquat->matrix4x4;
836  
837 =cut
838  
839 1 ▶sub matrix4x4 {
Subroutine matrix4x4: called CC=1 SCAR=0.0
Pod documented
840 1 my $q = shift;
841 1 my ($w,$x,$y,$z) = @{$q};
842  
843 return (
844 1 1 - 2*$y*$y - 2*$z*$z,
845 2*$x*$y + 2*$w*$z,
846 2*$x*$z - 2*$w*$y,
847 0,
848  
849 2*$x*$y - 2*$w*$z,
850 1 - 2*$x*$x - 2*$z*$z,
851 2*$y*$z + 2*$w*$x,
852 0,
853  
854 2*$x*$z + 2*$w*$y,
855 2*$y*$z - 2*$w*$x,
856 1 - 2*$x*$x - 2*$y*$y,
857 0,
858  
859 0,
860 0,
861 0,
862 1
863 );
864 }
865  
866 =item B<matrix3x3>
867  
868 Takes one argument: a rotation quaternion.
869 Returns a 9-element array, equal to the 3x3
870 matrix which represents the corresponding rotation.
871  
872 my $rotquat = Math::Quaternion::rotation($theta,@axis); # My rotation.
873 my @m = $rotquat->matrix3x3;
874  
875 =cut
876  
877 1 ▶sub matrix3x3 {
Subroutine matrix3x3: called CC=1 SCAR=0.0
Pod documented
878 1 my $q = shift;
879 1 my ($w,$x,$y,$z) = @{$q};
880  
881 return (
882 1 1 - 2*$y*$y - 2*$z*$z,
883 2*$x*$y + 2*$w*$z,
884 2*$x*$z - 2*$w*$y,
885  
886 2*$x*$y - 2*$w*$z,
887 1 - 2*$x*$x - 2*$z*$z,
888 2*$y*$z + 2*$w*$x,
889  
890 2*$x*$z + 2*$w*$y,
891 2*$y*$z - 2*$w*$x,
892 1 - 2*$x*$x - 2*$y*$y,
893 );
894 }
895  
896 =item B<matrix4x4andinverse>
897  
898 Similar to matrix4x4, but returnes a list of two array
899 references. The first is a reference to the rotation matrix;
900 the second is a reference to its inverse. This may be useful
901 when rendering sprites, since you can multiply by the rotation
902 matrix for the viewer position, perform some translations, and
903 then multiply by the inverse: any resulting rectangles drawn
904 will always face the viewer.
905  
906  
907 my $rotquat = Math::Quaternion::rotation($theta,@axis); # My rotation.
908 my ($matref,$invref) = $rotquat->matrix4x4andinverse;
909  
910 =cut
911  
912  
913 1 ▶sub matrix4x4andinverse {
Subroutine matrix4x4andinverse: called CC=1 SCAR=0.0
Pod documented
914 1 my $q = shift;
915 1 my ($w,$x,$y,$z) = @{$q};
916 1 my (@m,@mi);
917  
918 1 $mi[ 0] = $m[ 0] = 1 - 2*$y*$y - 2*$z*$z;
919 1 $mi[ 4] = $m[ 1] = 2*$x*$y + 2*$w*$z;
920 1 $mi[ 8] = $m[ 2] = 2*$x*$z - 2*$w*$y;
921 1 $mi[12] = $m[ 3] = 0;
922  
923 1 $mi[ 1] = $m[ 4] = 2*$x*$y - 2*$w*$z;
924 1 $mi[ 5] = $m[ 5] = 1 - 2*$x*$x - 2*$z*$z;
925 1 $mi[ 9] = $m[ 6] = 2*$y*$z + 2*$w*$x;
926 1 $mi[13] = $m[ 7] = 0;
927  
928 1 $mi[ 2] = $m[ 8] = 2*$x*$z + 2*$w*$y;
929 1 $mi[ 6] = $m[ 9] = 2*$y*$z - 2*$w*$x;
930 1 $mi[10] = $m[10] = 1 - 2*$x*$x - 2*$y*$y;
931 1 $mi[14] = $m[11] = 0;
932  
933 1 $mi[ 3] = $m[12] = 0;
934 1 $mi[ 7] = $m[13] = 0;
935 1 $mi[11] = $m[14] = 0;
936 1 $mi[15] = $m[15] = 1;
937  
938 1 return (\@m,\@mi);
939  
940 }
941  
942 =item B<stringify>
943  
944 Returns a string representation of the quaternion. This is used
945 to overload the '""' operator, so that quaternions may be
946 freely interpolated in strings.
947  
948 my $q = Math::Quaternion->new(1,2,3,4);
949 print $q->stringify; # "( 1 2 3 4 )"
950 print "$q"; # "( 1 2 3 4 )"
951  
952  
953 =cut
954  
955 2 ▶sub stringify {
Subroutine stringify: called CC=1 SCAR=0.0
Pod documented
956 2 my $self = shift;
957 2 return "( ".join(" ",@$self)." )";
958 }
959  
960 =item B<slerp>
961  
962 Takes two quaternion arguments and one scalar; performs
963 spherical linear interpolation between the two quaternions. The
964 quaternion arguments are assumed to be unit quaternions, and the
965 scalar is assumed to lie between 0 and 1: a scalar argument of
966 zero will return the first quaternion argument, and a scalar
967 argument of one will return the second.
968  
969 use Math::Trig;
970 my @axis = (0,0,1);
971 my $rq1 = Math::Quaternion::rotation(pi/2,\@axis); # 90 degs about Z
972 my $rq2 = Math::Quaternion::rotation(pi,\@axis); # 180 degs about Z
973
974 my $interp = Math::Quaternion::slerp($rq1,$rq2,0.5); # 135 degs about Z
975  
976 =cut
977  
978 3 ▶sub slerp {
Subroutine slerp: called CC=3 SCAR=11.0
Pod documented
979 3 my ($q0,$q1,$t) = @_;
980  
981 3 my $dotprod = dot($q0,$q1);
982 3 ▶ if ($dotprod<0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dotprod < 0) 1 2 3
983 # Reverse signs so we travel the short way round
984 1 $dotprod = -$dotprod;
985 1 $q1 = negate($q1);
986 }
987  
988 3 my $theta = Math::Trig::acos($dotprod);
989  
990 3 ▶ if (abs($theta) < 1e-5) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $theta < "1e-05") 1 2 3
991 # In the limit theta->0 , spherical interpolation is
992 # approximated by linear interpolation, which also
993 # avoids division-by-zero problems.
994  
995 1 return plus(scale($q0,(1-$t)) ,scale($q1,$t));
996  
997 }
998  
999 2 my $st = sin($theta);
1000 2 my $ist = 1.0/$st;
1001  
1002 2 my $q = plus(
1003 scale($q0,($ist * sin( (1-$t)*$theta ))),
1004 scale($q1,($ist*sin($t*$theta)))
1005 );
1006  
1007
1008 2 return normalize($q);
1009  
1010 }
1011  
1012  
1013 =item B<exp>
1014  
1015 Exponential operator e^q. Any quaternion q can be written as x+uy,
1016 where x is a real number, and u is a unit pure quaternion. Then,
1017 exp(q) == exp(x) * ( cos(y) + u sin(y) ).
1018  
1019 my $q = Math::Quaternion->new(1,2,3,4);
1020 print Math::Quaternion::exp($q);
1021  
1022 =cut
1023  
1024 18 ▶sub exp {
Subroutine exp: called CC=2 SCAR=6.9
Pod documented
1025 18 my $q = shift;
1026  
1027 18 ▶ if (isreal($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isreal($q)) 1 17 18
1028 1 return Math::Quaternion->new(CORE::exp($q->[0]),0,0,0);
1029 }
1030  
1031 17 my ($q0,$q1,$q2,$q3)=@$q;
1032  
1033 17 my $y = sqrt($q1*$q1+$q2*$q2+$q3*$q3); # Length of pure-quat part.
1034 17 my ($ux,$uy,$uz) = ($q1/$y,$q2/$y,$q3/$y); # Unit vector.
1035  
1036 17 my $ex = CORE::exp($q0);
1037 17 my $exs = $ex*sin($y);
1038  
1039 17 return Math::Quaternion->new($ex*cos($y),$exs*$ux,$exs*$uy,$exs*$uz);
1040 }
1041  
1042 =item B<log>
1043  
1044 Returns the logarithm of its argument. The logarithm of a negative
1045 real quaternion can take any value of them form (log(-q0),u*pi) for
1046 any unit vector u. In these cases, u is chosen to be (1,0,0).
1047  
1048 my $q = Math::Quaternion->new(1,2,3,4);
1049 print Math::Quaternion::log($q);
1050  
1051 =cut
1052  
1053 17 ▶sub log {
Subroutine log: called CC=5 SCAR=16.1
Pod documented
1054 17 my $q = shift;
1055  
1056 17 ▶ if (ref $q) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $q) { } 13 4 17
1057 13 ▶ if ("Math::Quaternion" ne ref $q) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Math::Quaternion" ne ref $q) 1 12 13
1058 1 $q = Math::Quaternion->new($q);
1059 }
1060 } else {
1061 4 $q = Math::Quaternion->new($q);
1062 }
1063  
1064 17 ▶ if (isreal($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (isreal($q)) 6 11 17
1065 6 ▶ if ($q->[0] > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q->[0] > 0) { } 5 1 6
1066 5 return Math::Quaternion->new(CORE::log($q->[0]));
1067 } else {
1068 1 return Math::Quaternion->new(CORE::log(-($q->[0])),pi,0,0);
1069 }
1070 }
1071  
1072 11 my ($q0,$q1,$q2,$q3)=@$q;
1073  
1074 11 my $modq = sqrt($q0*$q0 + $q1*$q1 + $q2*$q2 + $q3*$q3);
1075  
1076 11 my $x = CORE::log($modq);
1077 11 my $qquatmod = sqrt($q1*$q1+$q2*$q2+$q3*$q3); # mod of quat part
1078 11 my $y = atan2($qquatmod,$q0);
1079 11 my $c = $y/$qquatmod;
1080  
1081 11 return Math::Quaternion->new($x,$c*$q1,$c*$q2,$c*$q3);
1082
1083 }
1084  
1085  
1086  
1087 =back
1088  
1089 =head1 AUTHOR
1090  
1091 Jonathan Chin, E<lt>jon-quaternion.pm@earth.liE<gt>
1092  
1093 =head1 ACKNOWLEDGEMENTS
1094  
1095 Thanks to Rene Uittenbogaard and Daniel Connelly for useful suggestions, and
1096 Luc Vereecken and Bruce Gray for patches.
1097  
1098 =head1 SEE ALSO
1099  
1100 =over 4
1101  
1102 =item L<http://mathworld.wolfram.com/Quaternion.html>
1103  
1104 =item L<http://sjbaker.org/steve/omniv/eulers_are_evil.html>
1105  
1106 =item Acts 12:4
1107  
1108 =back
1109  
1110 =head1 COPYRIGHT AND LICENSE
1111  
1112 Copyright 2003 by Jonathan Chin
1113  
1114 This library is free software; you can redistribute it and/or modify
1115 it under the same terms as Perl itself.
1116  
1117 =cut
1118  
1119 2 1;
1120 __END__
↑ Summary