Math-VectorReal-1.02/0040755000024000000120000000000007530117233013722 5ustar anthonystaffMath-VectorReal-1.02/VectorReal.pm0100644000024000000120000005176607530117063016343 0ustar anthonystaff# # Math::VectorReal Vector Mathematics # # # Copyright (c) 2001 Anthony Thyssen. All rights reserved. This program # is free software; you can redistribute it and/or modify it under the # same terms as Perl itself. # package Math::VectorReal; =head1 NAME Math::VectorReal - Module to handle 3D Vector Mathematics =head1 SYNOPSIS #!/usr/bin/perl use Math::VectorReal; $a = vector( 1, 2, .5 ); print "Vector as string (MatrixReal default format)\n\$a => ", $a; print $a->stringify("Formated Output \$a => { %g, %g, %g }\n"); # I hate newline in the default output format (defined as MatrixReal) $Math::VectorReal::FORMAT = "[ %.5f %.5f %.5f ]"; print "Modified default output format \$a => $a\n"; print 'length => ', $a->length, "\n"; print 'normalised => ', $a->norm, "\n"; use Math::VectorReal qw(:all); # Include O X Y Z axis constant vectors print 'string concat $a."**" = ', $a."**", "\n"; print 'vector constant X = ', X, "\n"; print 'subtraction $a - Z = ', $a - Z, "\n"; print 'scalar divide $a / 3 = ', $a / 3, "\n"; print 'dot product $a . Y = ', $a . Y, "\n"; print 'cross product $a x Y = ', $a x Y, "\n"; print "Plane containing points X, \$a, Z (in anti-clockwise order)\n"; ($n,$d) = plane( X, $a, Z ); # return normal and disance from O print ' normal = $n = ', $n, "\n"; print ' disance from O = $d = ', $d, "\n"; print ' Y axis intersect = $d/($n.Y) = ', $d/($n.Y), "\n"; print "VectorReal and MatrixReal interaction\n\n"; use Math::MatrixReal; # Not required for pure vector math as above $r = $a->vector2matrix_row; # convert to MatrixReal Row Vector $c = $a->vector2matrix_col; # convert to MatrixReal Column Vector print 'Vector as a MatrixReal Row $r (vector -> matrix) => ', "\n", $r; print 'Vector as a MatrixReal Col $c (vector -> matrix) => ', "\n", $c; $nx = $a->norm; $ny = $nx x Z; $nz = $nx x $ny; # orthogonal vectors $R = vector_matrix( $nx, $ny, $nz ); # make the rotation matrix print 'Rotation Matrix from 3 Vectors $R => ',"\n", $R, "\n"; print "Extract the Y row from the matrix as a VectorReal..\n"; print '$R->matrix_row2vector(1) => ', $R->matrix_row2vector(1), "\n"; print "Rotate a vector with above rotation matrix\n"; print '$a * $R (vector -> vector)',"\n", $a * $R, "\n"; print "Rotate a MatrixReal column (post multiply)...\n"; print "(NB: matrix must be transposed (~) to match column format)\n"; print '~$R * $c (col_matrix -> col_matrix) =>',"\n", ~$R * $c, "\n"; =head1 DESCRIPTION The C package defines a 3D mathematical "vector", in a way that is compatible with the previous CPAN module C. However it provides a more vector oriented set of mathematical functions and overload operators, to the C package. For example the normal perl string functions "x" and "." have been overloaded to allow vector cross and dot product operations. Vector math formula thus looks like vector math formula in perl programs using this package. While this package is compatible with Math::MatrixReal, you DO NOT need to have that package to perform purely vector orientated calculations. You will need it however if you wish to do matrix operations with these vectors. The interface has been designed with this package flexibility in mind. The vectors are defined in the same way as a "row" C matrix, instead of that packages choice of "column" definition for vector operations. Such vectors are multiplied to matices with the vector on the left and the matrix on the right. EG: v * M -> 'v Not only is this the way I prefer to handle vectors, but it is the way most graphics books use vectors. As a bonus it results in no overload conflicts between this package and that of Math::MatrixReal, (the left objects overload operator is called to do the mathematics). It also is a lot simpler than C column vector methods, which were designed for equation solving rather than 3D geometry operations. The vector_matrix() function provided, simplifies the creation a C object from 3 (usually orthogonal) vectors. This with its vector orientated math operators makes it very easy to define orthogonal rotation matrices from C objects. See a rough example in the synopsis above, or in the file "matrix_test" in the packages source. NOTE: the 6th element the C array object is used to hold the length of the vector so that it can be re-used without needing to be re-calculated all the time. This means the expensive sqrt() function, need not be called unless nessary. This usage should not effect the direct use of these objects in the C functions. =cut use strict; #require Math::MatrixReal; # not required! use strict; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); require Exporter; @ISA = qw(Exporter); @EXPORT = qw( vector plane vector_matrix ); @EXPORT_OK = qw( O X Y Z ); %EXPORT_TAGS = ( axis => [ qw( O X Y Z ) ], # Unix Axis Vector Constants all => [@EXPORT, @EXPORT_OK] ); $VERSION = '1.0'; use Carp; use vars qw( $FORMAT $TRACE ); $TRACE = 0; $FORMAT = "[ %#19.12E %#19.12E %#19.12E ]\n"; # output format (as MatrixReal) =head1 CONSTANTS Four constant vectors are available for export (using an ":all" tag). these are 0 = [ 0 0 0 ] the zero vector or origin X = [ 1 0 0 ] | Y = [ 0 1 0 ] > Unit axis vectors Z = [ 0 0 1 ] | =cut # Constant Vector Functions # The format is as per a Math::MatrixReal object, with extra length item sub O() { bless [ [[0,0,0]], 1,3, undef,undef,undef, 0 ], __PACKAGE__; } sub X() { bless [ [[1,0,0]], 1,3, undef,undef,undef, 1 ], __PACKAGE__; } sub Y() { bless [ [[0,1,0]], 1,3, undef,undef,undef, 1 ], __PACKAGE__; } sub Z() { bless [ [[0,0,1]], 1,3, undef,undef,undef, 1 ], __PACKAGE__; } =head1 CONSTRUCTORS =over 4 =item new(x,y,z) Create a new vector with the values of C, C, C returning the appropriate object. =item vector(x,y,z) As C but is a exported function which does not require a package reference to create a C object. =item clone() Return a completely new copy of the referring C object. =cut sub new { # typical object creation (not many checks) croak "Usage: \$vector = ".__PACKAGE__."->new(x,y,z);\n" unless @_; my $ref = shift; return bless [ [[ @_ ]], 1,3 ], ref $ref || $ref; } sub vector { # normal way to create a vector - Exported function # This works as both a Object Method or Exported Function croak "Usage: \$vector = ".__PACKAGE__."->vector(x,y,z);\n". " or \$vector = vector(x,y,z);\n" unless @_ == 3 || @_ == 4 && ref $_[0]; my $class = __PACKAGE__; $class = ref shift if @_ == 4; return $class->new(@_); } sub clone { croak "Usage: \$vector_copy = \$vector->clone;\n" unless @_ == 1; my $v = shift; my $c = $v->new( $v->array ); # create a new vector using values $c->[6] = $v->[6] if defined $v->[6]; # also note its length (if known) return $c; } =head1 METHODS =item array() Return the x,y,z elements of the referring vector are an array of values. =item x() Return the x element of the referring vector. =item y() Return the y element of the referring vector. =item z() Return the z element of the referring vector. =item stringify( [ FORMAT ] ) Return the referring verctor as a string. The C if given is used to sprintf format the vector. This is used for all VectorReal to String conversions. By default this format is the same as it would be for a C object, "[ %#19.12E %#19.12E %#19.12E ]\n". Note that this includes a newline character!. However unlike C you can assign a new default sprintf format by assigning it to the packages C<$FORMAT> variable. For Example $Math::VectorReal::FORMAT = "{ %g, %g, %g }" Which is a good format to output vectors for use by the POVray (Persistance of Vision Raytracer) program. =item length() Return the length of the given vector. As a side effect the length is saved into that vectors object to avoid the use of the expensive sqrt() function. =item norm() Normalise the Vector. That is scalar divide the vector by its length, so that it becomes of length one. Normal vectors are commonly use to define directions, without scale, or orientation of a 3 dimensional plane. =cut sub array { # return vector as an array of values my $v = shift; return @{$v->[0][0]}; } sub x { my $v = shift; return ($v->array)[0]; } sub y { my $v = shift; return ($v->array)[1]; } sub z { my $v = shift; return ($v->array)[2]; } sub stringify { # convert a vector to a string (with optional format) my( $v, $fmt ) = @_; $fmt = $FORMAT unless defined $fmt; # if not given use current default return sprintf $fmt, $v->array; } sub length { # convert a vector to a string my $v = shift; return $v->[6] if defined $v->[6]; return $v->[6] = sqrt( $v.$v ); } sub norm { # scale vector to a length of one my $v = shift; return $v / $v->length; } =item plane( v1, v2, v3 ) Given three points defined counter clockwise on a plane, return an array in which the first element is the planes normal unit vector, and the second its distance from the origin, along that vector. NOTE: the distance may be negitive, in which case the origon is above the defined plane in 3d space. =cut sub plane { # Given three points on the plane (right-hand rule) # return a normal vector and distance from origin for a plane croak "Usage: (\$normal, \$distance) = plane(\$p1,\$p2,\$p3);\n" unless @_ == 3; my ($a, $b, $c) = @_; my $normal = (($b - $a) x ($c - $b))->norm; return ( $normal, $a . $normal ); } =item vector_matrix( nx, ny, nz ) Given the new location for the X, Y and Z vectors, concatanate them together (row wise) to create a C translation matrix. For example if the 3 vectors are othogonal to each other, the matrix created will be a rotation matrix to rotate the X, Y and Z axis to the given vectors. See above for an example. =cut sub vector_matrix { my( $nx, $ny, $nz ) = @_; bless [ [[$nx->array], [$ny->array], [$nz->array]], 3, 3 ], "Math::MatrixReal"; } # ------------------------------------------------------------------ # Convertsions between Math::MatrixReal and Math::VectorReal packages =back =head1 VECTOR/MATRIX CONVERSION The following functions provide links between the C and C packages. NOTE: While this package is closely related to C, it does NOT require that that package to be installed unless you actually want to perform matrix operations. Also the overload operations will automatically handle vector/matrix mathematics (See below). =head2 Vector to Matrix Conversion =over 4 =item vector2matrix_row( [CLASS] ) =item vector2matrix_col( [CLASS] ) Convert C objects to a C objects. Optional argument defines the object class to be returned (defaults to C). Note that as a C is internally equivelent to a C row matrix, C is essentually just a bless operation, which is NOT required to use with C functions. The C performs the required transpose to convert the C object into a C version of a vector (a column matrix). =cut sub vector2matrix_row { my( $v, $ref ) = @_; $ref ||= "Math::MatrixReal"; bless $v->clone, ref $ref || $ref; # clone and bless (object unchanged) } sub vector2matrix_col { my( $v, $ref ) = @_; $ref ||= "Math::MatrixReal"; my @v = $v->array; bless [ [[$v[0]],[$v[1]],[$v[2]]], 3, 1 ], ref $ref || $ref; } =head2 Matrix to Vector Conversion =item matrix_row2vector( [ROW] ) =item matrix_col2vector( [COLUMN] ) When referred to by a C object, extracts the vector from the matrix. the optional argument defines which row or column of the matrix is to be extracted as a C vector. =cut { # Enclose MartixReal package in a block package Math::MatrixReal; # Fake a change into the Math::MatrixReal package use Carp; # import carp into this package sub matrix_row2vector { my $m = shift; my($rows,$cols) = ($m->[1],$m->[2]); my $r = shift; # optional, which column from matrix croak "Error: matrix does not have 3D rows" unless ($cols == 3); if ( defined $r ) { croak "Error: matrix does not have that row" unless ( $r < $rows); } else { # if no option, it must be a Math::MatrixReal Row Vector croak "Error: matrix given to matrix_row2vector is not a 3D row matrix" unless ($rows == 1); $r = 0; } return Math::VectorReal->new(@{$m->[0][$r]}); # same result, only cleaned up } sub matrix_col2vector { my $m = shift; my($rows,$cols) = ($m->[1],$m->[2]); my $c = shift; # optional, which column from matrix croak "Error: matrix does not have 3D rows" unless ($rows == 3); if ( defined $c ) { croak "Error: matrix does not have that column" unless ( $c < $cols); } else { # if no option, it must be a Math::MatrixReal Column Vector croak "Error: matrix given to matrix_col2vector is not a 3D column matrix" unless ($cols == 1); $c = 0; } return Math::VectorReal->new($m->[0][0][$c], $m->[0][1][$c], $m->[0][2][$c]); } } # Return to the Math::VectorReal package we are really defining # ------------------------------------------------------------------ # Overloaded Math functions =back =head1 OPERATOR OVERLOADING Overload operations are provided to perform the usual string conversion, addition, subtraction, unary minus, scalar multiplation & division. On top of this however the multiply have been expanded to look for and execute C multiplation. The Main purpose of this package however was to provide the special vector product operations: dot product "." and cross product "x". In perl these operations are normally used for string operations, but if either argument is a C object, the operation will attempt the approprate vector math operation instead. Note however that if one side of the dot "." operator is already a string, then the vector will be converted to a sting and a string concatantion will be performed. The cross operator "x" will just croak() as it is non-sensical to either repeat the string conversion of a vector, OR to repeat a string, vector, times! Overloaded operator summery... neg unary minus - multiply vector by -1 "" automatic string conversion using stringify() function + vector addition - vector subtraction / scalar division (left argument must be the vector) * scalar multiplication OR MatrixReal multiplication x vector/cross product of two vectors . dot product of two vectors OR vector/string concatanation Posible future addition '~' to transpose a C into a C column vector (as per that operator on C objects). It was not added as it just did not seem to be needed. =cut use overload 'neg' => \&_negate, '""' => \&_stringify, '+' => \&_addition, '-' => \&_subtract, '*' => \&_multiply, '/' => \&_scalar_divide, 'x' => \&_cross_product, # Redefination of the string function '.' => \&_dot_product, # These includes stingify/concatanation 'fallback' => undef; sub _trace { return unless $TRACE; my($text,$object,$argument,$flip) = @_; unless (defined $object) { $object = 'undef'; }; unless (defined $argument) { $argument = 'undef'; }; unless (defined $flip) { $flip = 'undef'; }; if (ref($object)) { $object = ref($object); } if (ref($argument)) { $argument = ref($argument); } $argument =~ s/\n/\\n/g; print "$text: \$obj='$object' \$arg='$argument' \$flip='$flip'\n"; } sub _negate { my($object,$argument,$flip) = @_; _trace("'neg'",$object,$argument,$flip); my $v = $object->clone; for ( 0 .. 2 ) { $v->[0][0][$_] = -$v->[0][0][$_]; } # $v->[6]; does not change. return $v } sub _stringify { my($object,$argument,$flip) = @_; _trace("'\"\"'",$object,$argument,$flip); return $object->stringify; } sub _addition { # Operation on two vectors, as such $flip will be undefined or false # The operation is also communitive - order does not matter. my($object,$argument,$flip) = @_; _trace("'+'",$object,$argument,$flip); if ( (defined $argument) && ref($argument) && (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) ) { my $v = $object->clone; for ( 0 .. 2 ) { $v->[0][0][$_] += $argument->[0][0][$_]; } $#{$v} = 2; # any cached vector length is now invalid return $v; } croak("non-vector argument for '+'"); } sub _subtract { my($object,$argument,$flip) = @_; _trace("'-'",$object,$argument,$flip); # Operation on two vectors, as such $flip will be undefined or false # Note; however this is not communitive - order matters if ( (defined $argument) && ref($argument) && (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) ) { my $v = $object->clone; for ( 0 .. 2 ) { $v->[0][0][$_] -= $argument->[0][0][$_]; } $#{$v} = 2; # any cached vector length is now invalid return $v; } croak("non-vector argument for '-'"); } sub _multiply { my($object,$argument,$flip) = @_; _trace("'*'",$object,$argument,$flip); if ( ref($argument) ) { # Assume multiply by Math::MatrixReal object EG: $v * $M --> $new_v # Order is communicative, but $flip should NOT be true if ( ! $flip ) { return ( $object->vector2matrix_row($argument) * $argument )->matrix_row2vector; } else { # just in case flip is true.. return ( $argument * $object->vector2matrix_row($argument) )->matrix_row2vector; } } elsif ( defined $argument ) { # defined $argument must be a scalar, so Scalar Multiply # Communitive - order does not matter, $flip can be ignored my $v = $object->clone; for ( 0 .. 2 ) { $v->[0][0][$_] *= $argument; } $v->[6] *= abs($argument) if defined $v->[6]; # multiply vector length return $v; } croak("undefined argument given for vector multiply"); } sub _scalar_divide { my($object,$argument,$flip) = @_; _trace("'/'",$object,$argument,$flip); # The order is very important, you can NOT divide a scalar by a vector croak("You can not divide a scalar by a vector") if $flip; # The provided $argument must be a defined scalar if ( (defined $argument) && ! ref($argument) ) { my $v = $object->clone; for ( 0 .. 2 ) { $v->[0][0][$_] /= $argument; } $v->[6] /= abs($argument) if defined $v->[6]; # do vector length return $v; } croak("non-scalar given for vector scalar divide"); } sub _cross_product { my($object,$argument,$flip) = @_; # Operation on two vectors, as such $flip will be undefined or false # Note: however this is not communitive - order does matters _trace("'x'",$object,$argument,$flip); if ( (defined $argument) && ref($argument) && (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) ) { my $v = $object->new; my @o = $object->array; my @a = $argument->array; @{$v->[0][0]} = ( $o[1]*$a[2] - $o[2]*$a[1], $o[2]*$a[0] - $o[0]*$a[2], $o[0]*$a[1] - $o[1]*$a[0] ); $#{$v} = 2; # any cached vector length is now invalid return $v; } croak("string 'x' with a vector does not make sense!"); } sub _dot_product { my($object,$argument,$flip) = @_; if ( (defined $argument) && ref($argument) && (ref($argument) !~ /^SCALAR$|^ARRAY$|^HASH$|^CODE$|^REF$/) ) { # Operation on two vectors, and communitive - order does not matter _trace("'.'",$object,$argument,$flip); my $v = 0; # result is NOT an object, but a scalar for ( 0 .. 2 ) { $v += $object->[0][0][$_] * $argument->[0][0][$_]; } return $v; } # Argument is NOT a vector! Assume String concatenation wanted elsif ( defined $flip ) { if ( $flip ) { _trace("'.\"\"'",$object,$argument,$flip); return $argument . $object->stringify; } else { _trace("'\"\".'",$object,$argument,$flip); return $object->stringify . $argument; } } # concatenate a string to a vector _trace("'.='",$object,$argument,$flip); return $object->stringify . $argument; # Concatenate a vector to string is handled automatically with '""' operator } 1; # ------------------------------------------------------------------ =head1 SEE ALSO The C CPAN Module by Steffen Beyer and the C CPAN extension by Mike South =head1 AUTHOR Anthony Thyssen EFE =head1 COPYRIGHT Copyright (c) 2001 Anthony Thyssen. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. I would appreciate any suggestions however. =cut Math-VectorReal-1.02/Makefile.PL0100644000024000000120000000051007314055002015660 0ustar anthonystaffuse ExtUtils::MakeMaker; # See lib/ExtUtils/MakeMaker.pm for details of how to influence # the contents of the Makefile that is written. WriteMakefile( 'AUTHOR' => 'Anthony Thyssen 'Math::VectorReal', 'ABSTRACT_FROM' => 'VectorReal.pm', 'VERSION_FROM' => 'VectorReal.pm', ); Math-VectorReal-1.02/MANIFEST0100644000024000000120000000022107426652016015052 0ustar anthonystaffChanges MANIFEST Makefile.PL README VectorReal.pm matrix_test.pl matrix_test.out synopsis.pl synopsis.out test.pl vector_test.pl vector_test.out Math-VectorReal-1.02/Changes0100644000024000000120000000053507530117233015215 0ustar anthonystaffRevision history for Perl extension Math::VectorReal. 1.02 Mon Aug 20 17:28:30 2002 - Addition of x,y,z function patch by Gene Boggs 1.01 Tue Jul 10 17:23:58 2001 - Bug fix for test environment (pass -Iblib/lib to sub test scripts) 1.00 Tue Jun 19 16:17:16 2001 - original version; first CPAN upload Math-VectorReal-1.02/README0100644000024000000120000000566507426652065014626 0ustar anthonystaff Math::VectorReal - Module to handle 3D Vector Mathematics The `Math::VectorReal' package defines a 3D mathematical "vector", in a way that is compatible with the previous CPAN module `Math::MatrixReal'. However it provides a more vector oriented set of mathematical functions and overload operators, to the `MatrixReal' package. For example the normal perl string functions "x" and "." have been overloaded to allow vector cross and dot product operations. Vector math formula thus looks like vector math formula in perl programs using this package. While this package is compatible with Math::MatrixReal, you DO NOT need to have that package to perform purely vector orientated calculations. You will need it however if you wish to do matrix operations with these vectors. The interface has been designed with this package flexibility in mind. The vectors are defined in the same way as a "row" `Math::MatrixReal' matrix, instead of that packages choice of "column" definition for vector operations. Such vectors are multiplied to matrices with the vector on the left and the matrix on the right. EG: v * M -> 'v Not only is this the way I prefer to handle vectors, but it is the way most graphics books use vectors. As a bonus it results in no overload conflicts between this package and that of Math::MatrixReal, (the left objects overload operator is called to do the mathematics). It also is a lot simpler than `MatrixReal' column vector methods, which were designed for equation solving rather than 3D geometry operations. The vector_matrix() function provided, simplifies the creation a `MatrixReal' object from 3 (usually orthogonal) vectors. This with its vector orientated math operators makes it very easy to define orthogonal rotation matrices from C http://www.sct.gu.edu.au/~anthony/ ----------------------------------------------------------------------------- We will encourage you to develop the three great virtues of a programmer : laziness, impatience and hubris. --- Larry Wall - "Programming Perl" ----------------------------------------------------------------------------- Copyright (c) 2001 Anthony Thyssen. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. I would appreciate any suggestions however. Math-VectorReal-1.02/matrix_test.out0100644000024000000120000000233507314055370017021 0ustar anthonystaffGenerate a rotation matix to rotate [ 1 1 1 ] to [ 0 1 0 ] Vectors are normalised and rotation is by the minimal amount. Axis of Rotation (n) = [ -0.707107 0.000000 0.707107 ] Matrix to rotate Z axis to n => Q [ 5.773502691896E-01 5.773502691896E-01 5.773502691896E-01 ] [ -4.082482904639E-01 8.164965809277E-01 -4.082482904639E-01 ] [ -7.071067811865E-01 0.000000000000E+00 7.071067811865E-01 ] Rotate around Z axis => T [ 5.773502691896E-01 8.164965809277E-01 0.000000000000E+00 ] [ -8.164965809277E-01 5.773502691896E-01 0.000000000000E+00 ] [ 0.000000000000E+00 0.000000000000E+00 1.000000000000E+00 ] Resultant Rotation Matrix => R = transpose(Q) * T * Q [ 7.886751345948E-01 5.773502691896E-01 -2.113248654052E-01 ] [ -5.773502691896E-01 5.773502691896E-01 -5.773502691896E-01 ] [ -2.113248654052E-01 5.773502691896E-01 7.886751345948E-01 ] Rotate some vectors with matrix => v' = v * R v -> [ 0.000000 1.732051 0.000000 ] x -> [ 0.788675 0.577350 -0.211325 ] y -> [ -0.577350 0.577350 -0.577350 ] z -> [ -0.211325 0.577350 0.788675 ] Check orthogonaly is preserved 'x x 'y -> [ -0.211325 0.577350 0.788675 ] = 'z 'y x 'z -> [ 0.788675 0.577350 -0.211325 ] = 'x 'z x 'x -> [ -0.577350 0.577350 -0.577350 ] = 'y Math-VectorReal-1.02/test.pl0100644000024000000120000000470407426652460015252 0ustar anthonystaff# Before `make install' is performed this script should be runnable with # `make test'. After `make install' it should work as `perl test.pl' # vim: set filetype=perl : ######################### We start with some black magic to print on failure. BEGIN { $| = 1; } END {print "1..1\nnot ok 1 Premature end of Script\n" unless $loaded;} ######################### End of black magic. $testnum = 1; eval { require Math::VectorReal; }; if ( $@ ) { # The module is NOT available so fake a successfull test print STDERR " Failed to locate module Math::VectorReal under test.\n"; print STDERR " Aborting testing process...\n"; exit 0 } # Matrix tests are useless if Math::MatrixReal has not been installed # However it is NOT an error if that module is not installed so abort # if that is the case. eval { require Math::MatrixReal; }; if ( $@ ) { # The module is NOT available so fake a successfull test print STDERR " WARNING: Module Math::MatrixReal is not available.\n"; print STDERR " As this module can use it but does not require it, I will\n"; print STDERR " skip the testing of the Math::MatrixReal interface.....\n"; print "1..16\n"; $skip_matrix_tests = 1; } else { print "1..26\n"; } $loaded = 1; print "ok ", $testnum++, "\tModule Math::VectorReal Located\n"; # --------------------------------------- # Compare a script and its previous output sub check_script_output { my $script = shift; print "not " unless -f $script.'.pl'; # script found? print "ok ", $testnum++, "\t--- $script script ----\n"; open(OUT, "$script.out") || die; open(TEST, "- |") or exec('perl', $script.'.pl') or exit 0; $/=''; # read sections by paragraph while( $t = ) { $o = ; ($testname) = split(/\n/,$t); # ignore any white space funny busness $t =~ s/\s+/ /; $t =~ s/^\s//; $t =~ s/\s$//; $o =~ s/\s+/ /; $o =~ s/^\s//; $o =~ s/\s$//; print "not " if $t ne $o; print "ok ", $testnum++, "\t$testname\n" } close(OUT); close(TEST); print "not " if $?; print "ok ", $testnum++, "\t--- exit of $script script ----\n"; } # ====================================== &check_script_output( "vector_test" ); # tests 2..16 exit 0 if $skip_matrix_tests; # proceed with matrix interface tests? print "ok ", $testnum++, "\tModule Math::MatrixReal Located\n"; &check_script_output( "matrix_test" ); # tests 18..26 &check_script_output( "synopsis" ); # tests 27..39 # ====================================== Math-VectorReal-1.02/synopsis.out0100644000024000000120000000333107314061073016337 0ustar anthonystaffVector as string (default ouput format) $a => [ 1.000000000000E+00 2.000000000000E+00 5.000000000000E-01 ] Specified output format $a => { 1, 2, 0.5 } Modified default output format $a => [ 1.00000 2.00000 0.50000 ] General Vector Mathematics length 2.29128784747792 normalised [ 0.43644 0.87287 0.21822 ] string concat $a."**" = [ 1.00000 2.00000 0.50000 ]** vector constant X = [ 1.00000 0.00000 0.00000 ] subtraction $a - Z = [ 1.00000 2.00000 -0.50000 ] scalar divide $a / 3 = [ 0.33333 0.66667 0.16667 ] dot product $a . Y = 2 cross product $a x Y = [ -0.50000 0.00000 1.00000 ] Plane containing points X, $a, Z (in anti-clockwise order) normal = $n = [ 0.69631 -0.17408 0.69631 ] disance from O = $d = 0.696310623822791 Y axis intersect = $d/($n.Y) = -4 Conversions to MatrixReal objects Vector as a MatrixReal Row $r (vector -> matrix) => [ 1.000000000000E+00 2.000000000000E+00 5.000000000000E-01 ] Vector as a MatrixReal Col $c (vector -> matrix) => [ 1.000000000000E+00 ] [ 2.000000000000E+00 ] [ 5.000000000000E-01 ] Rotation Matrix from 3 Vectors $R => [ 4.364357804720E-01 8.728715609440E-01 2.182178902360E-01 ] [ 8.728715609440E-01 -4.364357804720E-01 0.000000000000E+00 ] [ 9.523809523810E-02 1.904761904762E-01 -9.523809523810E-01 ] Extract the Y row from the matrix as a VectorReal $R->matrix_row2vector(1) => [ 0.87287 -0.43644 0.00000 ] Rotate a vector with above rotation matrix $a * $R (vector -> vector) [ 2.22980 0.09524 -0.25797 ] Rotate a MatrixReal column (post multiply) (NB: matrix must be transposed (~) to match column format) ~$R * $c (col_matrix -> col_matrix) => [ 2.229797949979E+00 ] [ 9.523809523810E-02 ] [ -2.579725859545E-01 ] Math-VectorReal-1.02/vector_test.out0100644000024000000120000000331307314057721017016 0ustar anthonystaffStringify output testing (MatrixReal default) O->stringify => [ 0.000000000000E+00 0.000000000000E+00 0.000000000000E+00 ] Changing default vector to string format $Math::VectorReal::FORMAT = "[ %g %g %g ]"; Axis functions, assign to constants $o = O => [ 0 0 0 ] $x = X => [ 1 0 0 ] $y = Y => [ 0 1 0 ] $z = Z => [ 0 0 1 ] String conversion operation testing Note: this include some automatic stringify concat ('.') operations "$o" => [ 0 0 0 ] ""$x => [ 1 0 0 ] $y"" => [ 0 1 0 ] $z => [ 0 0 1 ] vector(1,2,3) => [ 1 2 3 ] Addition $a = $x + Y => [ 1 1 0 ] $a += $y => [ 1 2 0 ] Clone and Addition Tests $b = $y => [ 0 1 0 ] $b += Z => [ 0 1 1 ] $y => [ 0 1 0 ] Subtraction $b -= $z => [ 0 1 0 ] $b = $b - Z => [ 0 1 -1 ] Scalar Multiply $a = $z * 2 => [ 0 0 2 ] $a = 2 * Z => [ 0 0 2 ] $a *= 2.5 => [ 0 0 5 ] Scalar Divide $a = $b / 2 => [ 0 0.5 -0.5 ] $a /= 3e14 => [ 0 1.66667e-15 -1.66667e-15 ] Unary - and more subtraction $b = -$b => [ -0 -1 1 ] $b -= Z => [ -0 -1 0 ] $b -= $z - -$y => [ -0 -2 -1 ] $b = $o - $b => [ 0 2 1 ] Cross Product $a = $b x X => [ 0 1 -2 ] $a = $b x $y => [ -1 0 0 ] $a = $b x $z => [ 2 0 0 ] Dot Product / String Concatenation $a = Z . $b => 1 $a = $b . -$y => -2 $s = $b . "!" => [ 0 2 1 ]! $s = "!" . $b => ![ 0 2 1 ] $a .= $b => -2[ 0 2 1 ] Special Functions (length, norm, plane) $b->length => 2.23606797749979 $b->norm => [ 0 0.894427 0.447214 ] @a = plane(X,Y,Z) => [ 0.57735 0.57735 0.57735 ] 0.577350269189626 check output from plane() function normal => [ 0.57735 0.57735 0.57735 ] distance => 0.577350269189626 Are defined constants still OK $o => [ 0 0 0 ] $x => [ 1 0 0 ] $y => [ 0 1 0 ] $z => [ 0 0 1 ] Math-VectorReal-1.02/synopsis.pl0100644000024000000120000000471407323236767016167 0ustar anthonystaff#!/usr/bin/perl # # Direct extraction of the VectorReal POD synopsis # # location of my libraries (I normally set this via PERL5LIB) # # #use lib '/home/anthony/lib/perl5'; # location of my libraries use FindBin; use lib "$FindBin::Bin/blib/lib"; use Math::VectorReal qw(:all); # Include O X Y Z axis constant vectors $a = vector( 1, 2, .5 ); print "Vector as string (default ouput format)\n\$a => ", $a; print "\n"; print "Specified output format\n"; print $a->stringify("\$a => { %g, %g, %g }\n"); print "\n"; # I hate newline in the default output format (defined as MatrixReal) $Math::VectorReal::FORMAT = "[ %.5f %.5f %.5f ]"; print "Modified default output format\n\$a => $a\n"; print "\n"; print "General Vector Mathematics\n"; print "length\n", $a->length, "\n"; print "normalised\n", $a->norm, "\n"; print 'string concat $a."**" = ', $a."**", "\n"; print 'vector constant X = ', X, "\n"; print 'subtraction $a - Z = ', $a - Z, "\n"; print 'scalar divide $a / 3 = ', $a / 3, "\n"; print 'dot product $a . Y = ', $a . Y, "\n"; print 'cross product $a x Y = ', $a x Y, "\n"; print "\n"; print "Plane containing points X, \$a, Z (in anti-clockwise order)\n"; ($n,$d) = plane( X, $a, Z ); # return normal and disance from O print ' normal = $n = ', $n, "\n"; print ' disance from O = $d = ', $d, "\n"; print ' Y axis intersect = $d/($n.Y) = ', $d/($n.Y), "\n"; print "\n"; use Math::MatrixReal; # Not required for pure vector math as above print "Conversions to MatrixReal objects\n"; $r = $a->vector2matrix_row; # convert to MatrixReal Row Vector $c = $a->vector2matrix_col; # convert to MatrixReal Column Vector print 'Vector as a MatrixReal Row $r (vector -> matrix) => ', "\n", $r; print 'Vector as a MatrixReal Col $c (vector -> matrix) => ', "\n", $c; print "\n"; print "Rotation Matrix from 3 Vectors\n"; $nx = $a->norm; $ny = $nx x Z; $nz = $nx x $ny; # orthogonal vectors $R = vector_matrix( $nx, $ny, $nz ); # make the rotation matrix print '$R => ',"\n", $R, "\n"; print "Extract the Y row from the matrix as a VectorReal\n"; print '$R->matrix_row2vector(1) => ', $R->matrix_row2vector(1), "\n\n"; print "Rotate a vector with above rotation matrix\n"; print '$a * $R (vector -> vector)',"\n", $a * $R, "\n\n"; print "Rotate a MatrixReal column (post multiply)\n"; print "(NB: matrix must be transposed (~) to match column format)\n"; print '~$R * $c (col_matrix -> col_matrix) =>',"\n", ~$R * $c, "\n"; Math-VectorReal-1.02/vector_test.pl0100644000024000000120000000606407323236750016631 0ustar anthonystaff#!/usr/bin/perl # # Testing the Math::VectorReal module # # location of my libraries (I normally set this via PERL5LIB) #use lib '/home/anthony/lib/perl5'; # location of my libraries use FindBin; use lib "$FindBin::Bin/blib/lib"; use Math::VectorReal qw( :all ); #$Math::VectorReal::TRACE = 1; # Tracing of overload operators on/off print "Stringify output testing (MatrixReal default)\n"; print 'O->stringify => ', O->stringify; print "\n"; print "Changing default vector to string format\n"; print '$Math::VectorReal::FORMAT = "[ %g %g %g ]";', "\n"; $Math::VectorReal::FORMAT = "[ %g %g %g ]"; print "\n"; print "Axis functions, assign to constants\n"; print ' $o = O => ', $o=O, "\n"; print ' $x = X => ', $x=X, "\n"; print ' $y = Y => ', $y=Y, "\n"; print ' $z = Z => ', $z=Z, "\n"; print "\n"; print "String conversion operation testing\n"; print "Note: this include some automatic stringify concat ('.') operations\n"; print ' "$o" => ', "$o", "\n"; print '""$x =>', " $x", "\n"; print ' $y"" => ', "$y\n"; print ' $z => ', $z, "\n"; print 'vector(1,2,3) => ', vector(1,2,3), "\n"; print "\n"; print "Addition\n"; $a = $x + Y; print '$a = $x + Y => ', $a, "\n"; $a += $y ; print '$a += $y => ', $a, "\n"; print "\n"; print "Clone and Addition Tests\n"; $b = $y; print '$b = $y => ', $b, "\n"; $b += Z; print '$b += Z => ', $b, "\n"; print ' $y => ', $y, "\n"; print "\n"; print "Subtraction\n"; $b -= $z ; print '$b -= $z => ', $b, "\n"; $b = $b - Z ; print '$b = $b - Z => ', $b, "\n"; print "\n"; print "Scalar Multiply\n"; $a = $z * 2; print '$a = $z * 2 => ', $a, "\n"; $a = 2 * Z; print '$a = 2 * Z => ', $a, "\n"; $a *= 2.5; print '$a *= 2.5 => ', $a, "\n"; print "\n"; print "Scalar Divide\n"; $a = $b / 2; print '$a = $b / 2 => ', $a, "\n"; $a /= 3e14; print '$a /= 3e14 => ', $a, "\n"; print "\n"; print "Unary - and more subtraction\n"; $b = -$b; print '$b = -$b => ', $b, "\n"; $b -= Z; print '$b -= Z => ', $b, "\n"; $b -= $z - -$y; print '$b -= $z - -$y => ', $b, "\n"; $b = $o - $b; print '$b = $o - $b => ', $b, "\n"; print "\n"; print "Cross Product\n"; $a = $b x X; print '$a = $b x X => ', $a, "\n"; $a = $b x $y; print '$a = $b x $y => ', $a, "\n"; $a = $b x $z; print '$a = $b x $z => ', $a, "\n"; print "\n"; print "Dot Product / String Concatenation\n"; $a = Z . $b; print '$a = Z . $b => ', $a, "\n"; $a = $b . -$y; print '$a = $b . -$y => ', $a, "\n"; $s = $b . "!"; print '$s = $b . "!" => ', $s, "\n"; $s = "!" . $b; print '$s = "!" . $b => ', $s, "\n"; $a .= $b; print '$a .= $b => ', $a, "\n"; print "\n"; print "Special Functions (length, norm, plane)\n"; print '$b->length => ', $b->length, "\n"; print '$b->norm => ', $b->norm, "\n"; @a = plane(X,Y,Z); print '@a = plane(X,Y,Z) => ', "\n @a\n"; print "check output from plane() function\n"; $a = X+Y+Z; print 'normal => ', $a->norm, "\n"; print 'distance => ', ($a/3)->length, "\n"; print "\n"; print "Are defined constants still OK\n"; print '$o => ', $o, "\n"; print '$x => ', $x, "\n"; print '$y => ', $y, "\n"; print '$z => ', $z, "\n"; print "\n"; Math-VectorReal-1.02/matrix_test.pl0100644000024000000120000000546007323236724016633 0ustar anthonystaff#!/usr/bin/perl # # matrix_test # # To a test of the conversion functions between Math::VectorReal # and Math::MatrixReal functions by defining a generalise rotation # matrix generator function. # # For details see # http://www.sct.gu.edu.au/~anthony/info/graphics/matrix.hints # # location of my libraries (I normally set this via PERL5LIB) # #use lib '/home/anthony/lib/perl5'; # location of my libraries use FindBin; use lib "$FindBin::Bin/blib/lib"; use strict; use Math::MatrixReal; use Math::VectorReal qw( :all ); sub rotation_matrix { # create rotation matrix to rotate first vector to second # second vector, through plane common to both. # Input vectors (normalise the input) my $v = shift->norm; my $w = shift->norm; # workout the axis framework around $v (to become X) my $n = ( $v x $w )->norm; # axis of rotation (to become Z) my $m = $n x $v; # othogonal to $v and $n (to become Y) print "Axis of Rotation (n) = $n\n\n"; # Rotation matrix from this framework to the XYZ framework (inverted) # $v mapping to Z axis # # Creating a Matrix using strings is a crazy way to to do things! # $Q = Math::MatrixReal->new_from_string("[ $v ]\n[ $m ]\n[ $n ]\n"); # # Math::VectorReal has a vector_matrix() is MUCH easier and more exact. my $Q = vector_matrix( $v, $m, $n ); print "Matrix to rotate Z axis to n => Q\n$Q\n"; # Transfrom $w to the new coordinate system # Note ~$Q is the transpose of $Q which is also its inverse my $wx = $w * ~$Q; # produce w' my $wy = Z x $wx; # and the third orthogonal vector. # Rotate around Z axis so v' ($x) --> w' ($wx) my $T = vector_matrix( $wx, $wy, Z ); print "Rotate around Z axis => T\n$T\n"; # multiply together the three resulting rotation matrix # to produce the general rotation matrix to map $v to $w return (~$Q) * $T * $Q; } my $v = vector( 1, 1, 1 ); # rotate this vectior to Y #my $v = vector( shift, shift, shift ); # rotate this vectior to Y $Math::VectorReal::FORMAT = "[ %g %g %g ]"; print "Generate a rotation matix to rotate $v to ", Y, "\n", "Vectors are normalised and rotation is by the minimal amount.\n\n"; $Math::VectorReal::FORMAT = "[ %5f %5f %5f ]"; my $R = rotation_matrix( $v, Y ); # generate the general rotation matrix print "Resultant Rotation Matrix => R = transpose(Q) * T * Q\n$R\n"; my( $nx, $ny, $nz ); print "Rotate some vectors with matrix => v' = v * R\n"; print "v -> ", $v * $R, "\n"; # test out the matrix generated print "x -> ", $nx = X * $R, "\n"; print "y -> ", $ny = Y * $R, "\n"; print "z -> ", $nz = Z * $R, "\n"; print "\n"; print "Check orthogonaly is preserved\n"; print "'x x 'y -> ", $nx x $ny, " = 'z\n"; print "'y x 'z -> ", $ny x $nz, " = 'x\n"; print "'z x 'x -> ", $nz x $nx, " = 'y\n"; print "\n"; exit(0);