blib/lib/Math/matrix_test.pl untested

filter: Statementstmt 0.0% 0 / 46 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 53 CC 1 SCAR 6.9
CC
1
Cov
0%
rotation_matrix
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/perl
2 #
3 # matrix_test
4 #
5 # To a test of the conversion functions between Math::VectorReal
6 # and Math::MatrixReal functions by defining a generalise rotation
7 # matrix generator function.
8 #
9 # For details see
10 # http://www.sct.gu.edu.au/~anthony/info/graphics/matrix.hints
11 #
12 # location of my libraries (I normally set this via PERL5LIB)
13 # #use lib '/home/anthony/lib/perl5'; # location of my libraries
14 use FindBin;
15 use lib "$FindBin::Bin/blib/lib";
16  
17 use strict;
18 use Math::MatrixReal;
19 use Math::VectorReal qw( :all );
20  
21 sub rotation_matrix { # create rotation matrix to rotate first vector to second
22 # second vector, through plane common to both.
23 # Input vectors (normalise the input)
24 my $v = shift->norm;
25 my $w = shift->norm;
26  
27 # workout the axis framework around $v (to become X)
28 my $n = ( $v x $w )->norm; # axis of rotation (to become Z)
29 my $m = $n x $v; # othogonal to $v and $n (to become Y)
30  
31 print "Axis of Rotation (n) = $n\n\n";
32  
33 # Rotation matrix from this framework to the XYZ framework (inverted)
34 # $v mapping to Z axis
35 #
36 # Creating a Matrix using strings is a crazy way to to do things!
37 # $Q = Math::MatrixReal->new_from_string("[ $v ]\n[ $m ]\n[ $n ]\n");
38 #
39 # Math::VectorReal has a vector_matrix() is MUCH easier and more exact.
40 my $Q = vector_matrix( $v, $m, $n );
41  
42 print "Matrix to rotate Z axis to n => Q\n$Q\n";
43  
44 # Transfrom $w to the new coordinate system
45 # Note ~$Q is the transpose of $Q which is also its inverse
46 my $wx = $w * ~$Q; # produce w'
47 my $wy = Z x $wx; # and the third orthogonal vector.
48  
49 # Rotate around Z axis so v' ($x) --> w' ($wx)
50 my $T = vector_matrix( $wx, $wy, Z );
51 print "Rotate around Z axis => T\n$T\n";
52  
53 # multiply together the three resulting rotation matrix
54 # to produce the general rotation matrix to map $v to $w
55 return (~$Q) * $T * $Q;
56 }
57  
58  
59 my $v = vector( 1, 1, 1 ); # rotate this vectior to Y
60 #my $v = vector( shift, shift, shift ); # rotate this vectior to Y
61  
62 $Math::VectorReal::FORMAT = "[ %g %g %g ]";
63 print "Generate a rotation matix to rotate $v to ", Y, "\n",
64 "Vectors are normalised and rotation is by the minimal amount.\n\n";
65  
66 $Math::VectorReal::FORMAT = "[ %5f %5f %5f ]";
67 my $R = rotation_matrix( $v, Y ); # generate the general rotation matrix
68 print "Resultant Rotation Matrix => R = transpose(Q) * T * Q\n$R\n";
69  
70 my( $nx, $ny, $nz );
71  
72 print "Rotate some vectors with matrix => v' = v * R\n";
73 print "v -> ", $v * $R, "\n"; # test out the matrix generated
74 print "x -> ", $nx = X * $R, "\n";
75 print "y -> ", $ny = Y * $R, "\n";
76 print "z -> ", $nz = Z * $R, "\n";
77 print "\n";
78  
79 print "Check orthogonaly is preserved\n";
80 print "'x x 'y -> ", $nx x $ny, " = 'z\n";
81 print "'y x 'z -> ", $ny x $nz, " = 'x\n";
82 print "'z x 'x -> ", $nz x $nx, " = 'y\n";
83 print "\n";
84  
85 exit(0);
86