Project3D.pm

filter: Statementstmt 94.4% 170 / 180 Branchbran 63.1% 48 / 76 Conditioncond 61.9% 13 / 21 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 95.6% 22 / 23 Podpod 100% 10 / 10 totaltotal 82.9% 268 / 323 CC 56 SCAR 20.0
CC
56
Cov
83%
_new_from_rows
20.0
new
13.2
project_range_callback
12.1
CRAP 7.4
↑ Summary Project3D/Function.pm »
1
2 # See the POD documentation at the end of this
3 # document for detailed copyright information.
4 # (c) 2002-2006 Steffen Mueller, all rights reserved.
5
6 package Math::Project3D;
7
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 2 ▶use vars qw/$VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 2 $VERSION = 1.02;
16
17 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18
19 2 ▶use Math::MatrixReal;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Math::Project3D::Function;
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22 199 ▶ sub _max {
Subroutine _max: called CC=3 SCAR=11.0
23 199 my $max = 0;
24 199 for (@_) {
25 241 ▶ $max = $_ if $_ > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $max 199 42 241
26 }
27 $max
28 199 }
29 0 ▶ sub _new_from_rows {
Subroutine _new_from_rows: not called CC=4 SCAR=30.0
30 0 my $class = 'Math::MatrixReal';
31 0 my $rows = shift;
32 0 my @rows;
33 0 foreach (@$rows) {
34 push @rows,
35 0 ▶ (ref($_) eq 'Math::MatrixReal' ? [@{$_->[0][0]}] : [@{$_}]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'Math::MatrixReal' ? : 0 0 0
36 }
37 return bless [
38 \@rows,
39 scalar(@rows),
40 _max(
41 map {
42 0 ▶ ref($_) eq 'Math::MatrixReal' ? $_->[2] : scalar(@$_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'Math::MatrixReal' ? : 0 0 0
43 }
44 @rows
45 ),
46 undef,
47 undef,
48 undef
49 ] => $class;
50 }
51
52 199 ▶ sub _new_from_cols {
Subroutine _new_from_cols: called CC=6 SCAR=17.9
53 199 my $class = 'Math::MatrixReal';
54 199 my $cols = shift;
55 my $num_rows = _max(
56 map {
57 199 ▶ ref($_) eq 'Math::MatrixReal' ? $_->[1] : scalar(@$_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'Math::MatrixReal' ? : 88 153 241
58 }
59 @$cols
60 );
61 199 my $num_cols = @$cols;
62
63 199 my @rows;
64 199 my $cn = 0;
65 199 foreach (@$cols) {
66 241 my $col = $_;
67 241 ▶ if (ref($col) eq 'Math::MatrixReal') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $col eq 'Math::MatrixReal') { } 88 153 241
68 88 $col = $col->[0];
69 88 $rows[$_][$cn] = $col->[$_][0] for 0..$num_rows-1;
70 }
71 else {
72 153 $rows[$_][$cn] = $col->[$_] for 0..$num_rows-1;
73 }
74 241 $cn++;
75 }
76 199 return bless [
77 \@rows,
78 $num_rows,
79 $num_cols,
80 undef,
81 undef,
82 undef
83 ] => $class;
84 }
85
86
87 # class and object method new_function
88 #
89 # Wrapper around Math::Project3D->new()
90 # Returns compiled function (anon sub)
91 # As a class method, it does not have side effects.
92 # As an object method, it assigns the returned function to
93 # the object's function attribute.
94
95 6 ▶sub new_function {
Subroutine new_function: called CC=3 SCAR=11.0
Pod documented
96 6 ▶ @_ > 1 or croak "No arguments supplied to 'new_function' method.";
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 or croak("No arguments supplied to 'new_function' method.") 6 0 6
97
98 6 my $self = shift;
99
100 6 my @components = @_;
101
102 6 my $function = Math::Project3D::Function->new(@components);
103
104 6 ▶ if (ref $self eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq __PACKAGE__) 3 3 6
105 3 $self->{function} = $function;
106 }
107
108 6 return $function;
109 }
110
111
112 # Class and object method/constructor new
113 #
114 # Arguments are used in the object's anon hash.
115 # Creates new object.
116 # Returns MP3D instance.
117
118 3 ▶sub new {
Subroutine new: called CC=11 SCAR=25.8
Pod documented
119 3 my $proto = shift;
120 3 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
121
122 # !!!FIXME!!! Insert argument checking here
123 3 my $self = {
124 function => undef,
125 @_
126 };
127
128 3 bless $self => $class;
129
130 # Some attributes are required.
131 3 my $missing_attribute = $self->_require_attributes(qw(
132 plane_basis_vector
133 plane_direction1
134 plane_direction2
135 ));
136 3 ▶ croak "Required attribute '$missing_attribute' missing."
Branch: 1 branch
BranchTrueFalseTotal
if defined $missing_attribute 0 3 3
137 if defined $missing_attribute;
138
139 # Transform the vector/matrix specifications ((nested) array refs)
140 # into Math::MatrixReal's.
141 3 foreach ( qw(
142 plane_basis_vector
143 plane_direction1
144 plane_direction2
145 ) ) {
146
147 # Argument checking, we either want an array ref or a MMR object.
148 croak "Invalid argument '$_' passed to new. Should be an array reference."
149 9 ▶ if not exists $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$_} 0 9 9
150
151 9 ▶ next if ref $self->{$_} eq 'Math::MatrixReal';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{$_} eq "Math::MatrixReal" 0 9 9
152 croak "Invalid argument '$_' passed to new. Should be an array reference."
153 9 ▶ if ref $self->{$_} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{$_} ne "ARRAY" 0 9 9
154
155 # Transform into an MMR object.
156 9 $self->{$_} = $self->_make_matrix($self->{$_});
157 }
158
159 # Projection vector defaults to the normal vector of the plane,
160 # but may also be specified explicitly as array ref or MMR object.
161 3 ▶ if ( defined $self->{projection_vector} and
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'projection_vector'} and ref $self->{'projection_vector'} eq 'ARRAY') { } 1 2 3
elsif (not defined $self->{'projection_vector'} or ref $self->{'projection_vector'} ne 'Math::MatrixReal') { } 2 0 2
Condition66%2/3
defined $self->{"projection_vector"} and ref $self->{"projection_vector"} eq "ARRAY"
!ll&&!rl&&r
201
MC/DC50%1/2
defined $self->{"projection_vector"} and ref $self->{"projection_vector"} eq "ARRAY"
defined $self->{"projection_vector"} ref $self->{"projection_vector"} eq "ARRAY"
Truth table66%2/3
A and B
A= defined $self->{"projection_vector"}B= ref $self->{"projection_vector"} eq "ARRAY"
ABresult
0X0
100
111
162 ref $self->{projection_vector} eq 'ARRAY' ) {
163
164 1 $self->{projection_vector} = $self->_make_matrix( $self->{projection_vector} );
165
166 ▶ } elsif ( not defined $self->{projection_vector} or
Condition33%1/3
not defined $self->{"projection_vector"} or ref $self->{"projection_vector"} ne "Math::MatrixReal"
l!l&&r!l&&!r
200
MC/DC0%0/2
not defined $self->{"projection_vector"} or ref $self->{"projection_vector"} ne "Math::MatrixReal"
not defined $self->{"projection_vector"} ref $self->{"projection_vector"} ne "Math::MatrixReal"
Truth table33%1/3
not(A) or B
A= not defined $self->{"projection_vector"}B= ref $self->{"projection_vector"} ne "Math::MatrixReal"
ABresult
1X1
011
000
167 ref $self->{projection_vector} ne 'Math::MatrixReal' ) {
168
169 # Defaults to the normal of the plane.
170 $self->{projection_vector} = $self->{plane_direction1}->vector_product(
171 $self->{plane_direction2}
172 2 );
173 }
174
175 # Now generate the linear equation system that has to be solved by
176 # MMR in order to do project a point onto the plane.
177 #
178 # MMR does all the dirty work. (Thanks!) So we just create a matrix.
179 # (d, e be the directional vectors, n the vector we project along,
180 # p the basis point. Let i be the solution)
181 #
182 # x(t) + n1*i1 = d1*i2 + e1*i3 + p1
183 # y(t) + n2*i1 = d2*i2 + e2*i3 + p2
184 # z(t) + n3*i1 = d3*i2 + e3*i3 + p3
185 #
186 # This is the intersection of the plane and the corresponding orthogonal
187 # line through s(t). Another form:
188 #
189 # n1*i1 + d1*i2 + e1*i3 = p1 + x(t)
190 # n2*i1 + d2*i2 + e2*i3 = p2 + y(t)
191 # n3*i1 + d3*i2 + e3*i3 = p3 + z(t)
192 #
193 # linear_eq_system will be the matrix of n,d,e.
194 # result_vector will be p+s(t) (later)
195
196 my $linear_eq_system = _new_from_cols(
197 [
198 $self->{projection_vector},
199 $self->{plane_direction1},
200 $self->{plane_direction2},
201 3 ]
202 );
203
204 # Now we do the first step towards solving the equation system.
205 # This does not have to be repeated for every projection.
206 3 $self->{lr_matrix} = $linear_eq_system->decompose_LR();
207
208 3 return $self;
209 }
210
211
212 # Method project
213 #
214 # Does the projection calculations for a single set of function
215 # parameters.
216 # Takes the function parameters as argument.
217 # Returns undef on failure (plane and P + lambda*projection_vector do
218 # not intersect!). On success:
219 # Returns the coefficients of the point on the plane,
220 # the distance of the source point from the plane in lengths
221 # of the projection vector.
222
223 34 ▶sub project {
Subroutine project: called CC=3 SCAR=11.2
Pod documented
224 34 my $self = shift;
225
226 34 ▶ croak "You need to assign a function before projecting it."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_function 0 34 34
227 if not defined $self->get_function();
228
229 # Apply function
230 my $point = _new_from_cols(
231 [
232 34 [ $self->{function}->(@_) ],
233 ]
234 );
235
236 # Generate result_vector
237 my $result_vector = _new_from_cols(
238 [
239 34 $self->{plane_basis_vector} + $point
240 ]
241 );
242
243 # Solve the l_e_s.
244 34 my ($dimension, $p_vector, undef) = $self->{lr_matrix}->solve_LR($result_vector);
245
246 # Did we find a solution?
247 34 ▶ return undef if not defined $dimension;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimension 0 34 34
248
249 # $dimension == 0 => one solution
250 # $dimension == 1 => straight line
251 # $dimension == 2 => plane (impossible :) )
252 # ...
253 # $dimension == 1 is possible (point and projection_vector part
254 # of the plane). Hence: !!!FIXME!!!
255
256 34 return $p_vector->element(2,1), # coefficient 1
257 $p_vector->element(3,1), # coefficient 2
258 $p_vector->element(1,1); # distance in lengths of the projection vector
259 }
260
261
262 # Method project_range_callback
263 #
264 # Does the projection calculations for ranges of function parameters.
265 # Takes an code reference and a number array refs of ranges as argument.
266 # Ranges are specified using three values:
267 # [lower_bound, upper_bound, increment].
268 # Alternatively, one may specify only one element in any of the array
269 # references. This will then be a static parameter.
270 # The number of ranges (array references) corresponds to the number of
271 # parameters to the vectorial function.
272 # The callback is called for every set of result values with an array
273 # reference of parameters and the list of the three result values.
274
275 1 ▶sub project_range_callback {
Subroutine project_range_callback: called CC=12 SCAR=24.9
Pod documented
276 1 my $self = shift;
277
278 1 ▶ croak "You need to assign a function before projecting it."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_function 0 1 1
279 if not defined $self->get_function();
280
281 # Argument checking
282 1 my $callback = shift;
283 1 ▶ ref $callback eq 'CODE' or
Branch: 1 branch
BranchTrueFalseTotal
ref $callback eq "CODE" or croak("Invalid code reference passed to project_range_callback.") 1 0 1
284 croak "Invalid code reference passed to project_range_callback.";
285
286 1 my @ranges = @_;
287
288 croak "Invalid range parameters passed to project_range_callback"
289 1 ▶ if grep { ref $_ ne 'ARRAY' } @ranges;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "ARRAY";} @ranges 0 1 1
290
291
292 1 my $function = $self->get_function();
293
294 # Replace all ranges that consist of a single number with ranges
295 # that iterate from that number to itself.
296 1 foreach (@ranges) {
297 7 ▶ $_ = [$_->[0], $_->[0], 1] if @$_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$_ == 1 2 5 7
298 }
299
300 # Calculate every range's length and store it in @lengths.
301 my @lengths = map {
302 # upper - lower / increment
303 1 int( ($_->[1] - $_->[0]) / $_->[2] ),
304 } @ranges;
305
306 # Prepare counters for every range.
307 1 my @counters = (0) x scalar(@ranges);
308
309 # Calculate the number if iterations needed.
310 # It is $_+1 and not $_ because the lengths are the lengths we
311 # need for the comparisons inside the long for loop. We save one
312 # op in there that way.
313 1 my $iterations = 1;
314 1 $iterations *= ( $_ + 1 ) for @lengths;
315
316 # For all possible combinations of parameters...
317 1 ▶ for (my $i = 1; $i <= $iterations; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $iterations) 45 1 46
318
319 # Get current function parameters
320 45 my @params;
321
322 # Get one parameter for every range
323 45 ▶ for (my $range_no = 0; $range_no < @ranges; $range_no++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range_no < @ranges) 315 45 360
324 # lower + increment * current_count
325 315 push @params, $ranges[$range_no][0] +
326 $ranges[$range_no][2] * $counters[$range_no];
327 }
328
329 # Increment outermost range by one. If it got out of bounds,
330 # make it 0 and increment the next range, etc.
331 45 my $j = 0;
332 45 ▶ while (defined $ranges[$j] and ++$counters[$j] > $lengths[$j]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ranges[$j] and ++$counters[$j] > $lengths[$j]) 33 45 78
Condition100%3/3
defined $ranges[$j] and ++$counters[$j] > $lengths[$j]
!ll&&!rl&&r
14433
MC/DC100%2/2
defined $ranges[$j] and ++$counters[$j] > $lengths[$j]
defined $ranges[$j] ++$counters[$j] > $lengths[$j]
Truth table100%3/3
A and B
A= defined $ranges[$j]B= ++$counters[$j] > $lengths[$j]
ABresult
0X0
100
111
333 33 $counters[$j] = 0;
334 33 $j++;
335 }
336
337 # Apply function
338 45 my $point = _new_from_cols(
339 [
340 [ $function->(@params) ],
341 ]
342 );
343
344 # Generate result_vector
345 my $result_vector = _new_from_cols(
346 [
347 45 $self->{plane_basis_vector} + $point
348 ]
349 );
350
351 # Solve the l_e_s.
352 45 my ($dimension, $p_vector, undef) = $self->{lr_matrix}->solve_LR($result_vector);
353
354 # Did we find a solution?
355 45 ▶ croak "Could not project $result_vector."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dimension 0 45 45
356 if not defined $dimension;
357
358 # $dimension == 0 => one solution
359 # $dimension == 1 => straight line
360 # $dimension == 2 => plane (impossible :) )
361 # ...
362 # $dimension == 1 is possible (point and projection_vector part
363 # of the plane). Hence: !!!FIXME!!!
364
365 45 $callback->(
366 $p_vector->element(2,1), # coefficient 1
367 $p_vector->element(3,1), # coefficient 2
368 $p_vector->element(1,1), # distance in lengths of the projection vector
369 $j, # how many ranges did we increment?
370 );
371 }
372
373 1 return();
374 }
375
376
377 # Method project_list
378 #
379 # Wrapper around project(), therefore slow.
380 # Takes a list of array refs as argument. The array refs
381 # are to contain sets of function parameters.
382 # Calculates every set of parameters' projection and stores the
383 # three associated values (coefficients and distance coefficient)
384 # in an n*3 matrix (MMR obj) wheren is the number of points
385 # projected.
386 # Currently croaks if any of the points cannot be projected onto
387 # the plane using the given projection vector. (In R3 -> R2, try
388 # using the plane's normal vector which is guaranteed not to be
389 # parallel to the plane.)
390 # Returns that MMR object.
391
392 1 ▶sub project_list {
Subroutine project_list: called CC=5 SCAR=16.3
Pod documented
393 1 my $self = shift;
394 1 ▶ croak "No arguments passed to project_list()."
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 0 0 1 1
395 if @_ == 0;
396
397 1 ▶ croak "You need to assign a function before projecting it."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_function 0 1 1
398 if not defined $self->get_function();
399
400 # Create result matrix to hold individual results.
401 1 my $result_matrix = Math::MatrixReal->new(scalar(@_), 3);
402
403 # Keep track of the matrix row
404 1 my $result_no = 1;
405
406 1 foreach my $array_ref (@_) {
407 12 my ($coeff1, $coeff2, $dist) = $self->project(@$array_ref);
408
409 12 ▶ croak "Vector $result_no cannot be projected."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $coeff1 0 12 12
410 if not defined $coeff1;
411
412 # Assign results
413 12 $result_matrix->assign($result_no, 1, $coeff1);
414 12 $result_matrix->assign($result_no, 2, $coeff2);
415 12 $result_matrix->assign($result_no, 3, $dist);
416 12 $result_no++;
417 }
418
419 1 return $result_matrix;
420 }
421
422
423 # Accessor get_function
424 #
425 # No parameters.
426 # Returns the current function code ref.
427
428 59 ▶sub get_function {
Subroutine get_function: called CC=1 SCAR=0.0
Pod documented
429 59 my $self = shift;
430 59 return $self->{function};
431 }
432
433
434 # Accessor set_function
435 #
436 # Takes a code ref as argument.
437 # Sets the object's function to that code ref.
438 # Returns the code ref.
439
440 10 ▶sub set_function {
Subroutine set_function: called CC=2 SCAR=7.0
Pod documented
441 10 my $self = shift;
442 10 my $function = shift;
443
444 10 ▶ ref $function eq 'CODE' or croak "Argument to set_function must be code reference.";
Branch: 1 branch
BranchTrueFalseTotal
ref $function eq "CODE" or croak("Argument to set_function must be code reference.") 10 0 10
445
446 10 $self->{function} = $function;
447
448 10 return $self->{function};
449 }
450
451
452 # Private method _require_attributes
453 #
454 # Arguments must be a list of attribute names (strings).
455 # Tests for the existance of those attributes.
456 # Returns the missing attribute on failure, undef on success.
457
458 3 ▶sub _require_attributes {
Subroutine _require_attributes: called CC=3 SCAR=11.2
459 3 my $self = shift;
460 3 ▶ exists $self->{$_} or return $_ foreach @_;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{$_} or return $_ 9 0 9
461 3 return undef;
462 }
463
464
465 # Private method _make_matrix
466 #
467 # Takes a list of array refs as arguments.
468 # Creates a Math::MatrixReal object from the arrays.
469 # Returns the Math::MatrixReal object.
470
471 10 ▶sub _make_matrix {
Subroutine _make_matrix: called CC=2 SCAR=7.0
472 10 my $self = shift;
473 10 ▶ @_ or croak "No arguments passed to _make_matrix.";
Branch: 1 branch
BranchTrueFalseTotal
@_ or croak("No arguments passed to _make_matrix.") 10 0 10
474
475 10 my $matrix = Math::MatrixReal->new_from_cols( [ @_ ] );
476
477 10 return $matrix;
478 }
479
480
481 # Evil method rotate
482 #
483 # Takes a vector as argument. (Either an MMR object or an array ref
484 # of an array containing three components.)
485 # The method will replace the function with a wrapper around the original
486 # function that rotates the result of the original function by the same
487 # angles that the z-axis (e3) needs to be turned to become the passed vector.
488 # Example: Passed [1,0,0] means that e3 will be rotated to become e1.
489 # Hence all points will be rotated by 90 degrees and orthogonally to e2.
490 # Returns the wrapper function and the old function as code references.
491 #
492 # Important note: You can apply this function multiple times. That means
493 # if you rotated the function once, you may do so again and the original
494 # rotated function will be wrapped again so that you effectively rotate
495 # it twice. Note, however, that you will sacrifice performance on the
496 # altar of recursion that way.
497 #
498 # !!!FIXME!!! This is slow. Sloooooow. Conceptually slow, but the
499 # implementation sucks badly, too. It is especially unnerving to me that
500 # the whole lexical scope of a myriad of intermediate result matrices and
501 # vectors is kept because we use closures. Closures are great, but not
502 # in bloated lexical scopes. How fix that without introducing either
503 # - an ugly additional method
504 # - ugly additional blocks to keep the scope clean. (Yuck!)
505
506 9 ▶sub rotate {
Subroutine rotate: called CC=4 SCAR=13.9
Pod documented
507 9 my $self = shift;
508
509 9 ▶ croak "You need to assign a function before rotating it."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_function 0 9 9
510 if not defined $self->get_function();
511
512 # We want to rotate everything the same way we would
513 # have to rotate e3 (z unit vector) to become the unit
514 # vector parallel to $e3_.
515 9 my $e3_ = shift;
516
517 # Make sure we work with a MMR vector
518 9 ▶ if (ref $e3_ ne 'Math::MatrixReal') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e3_ ne "Math::MatrixReal") 9 0 9
519 9 ▶ croak "Invalid vector passed to rotate()."
Branch: 1 branch
BranchTrueFalseTotal
if ref $e3_ ne "ARRAY" 0 9 9
520 if ref $e3_ ne 'ARRAY';
521
522 9 $e3_ = _new_from_cols([$e3_]);
523 }
524
525 9 $e3_ *= 1 / $e3_->length();
526
527 9 my $e3 = _new_from_cols([[0,0,1]]);
528
529 # The axis we want to rotate around
530 9 my $axis = $e3->vector_product($e3_);
531
532 # The angle we want to rotate by.
533 9 my $angle = acos($e3->scalar_product($e3_));
534
535 # Rotationsmatrix um Achse (Vektor) a = (a,b,c):
536 # [a*a a*b a*c]
537 # M(rot) = (1-cos(alpha)) * [a*b b*b b*c] +
538 # [a*c b*c c*c]
539 # [1 0 0 ]
540 # cos(alpha) * [0 1 0 ] +
541 # [0 0 1 ]
542 # [0 -c b ]
543 # sin(alpha) * [c 0 -a ]
544 # [-b a 0 ]
545 # (from: Merziger, Wirth: "Repetitorium der Hoeheren Mathematik"
546 # Binomi Verlag. ISBN 3-923923-33-3)
547
548 9 my ($a, $b, $c) = ($axis->element(1,1), $axis->element(2,1), $axis->element(3,1));
549
550 9 my $matrix1 = _new_from_cols(
551 [
552 [$a*$a, $a*$b, $a*$c],
553 [$b*$a, $b*$b, $b*$c],
554 [$c*$a, $c*$b, $c*$c],
555 ],
556 );
557
558 9 my $matrix2 = Math::MatrixReal->new_diag([1,1,1]);
559
560 9 my $matrix3 = _new_from_cols(
561 [
562 [0, -$c, $b ],
563 [$c, 0, -$a],
564 [-$b, $a, 0 ],
565 ],
566 );
567
568 # What a painful birth, but here we are:
569 9 my $rot_matrix = (1-cos($angle)) * $matrix1 + cos($angle) * $matrix2 +
570 sin($angle) * $matrix3;
571
572 # For (Rotated vector r=(r1,r2,r3), source vector x=(x1,x2,x3)):
573 # (eq1) r = $rot_matrix * x
574 # Hence:
575 # (eq2) x = transposed($rot_matrix) * r
576 #
577 # x and $rot_matrix (and therefore also transposed($rot_matrix)
578 # are know. Hance, (eq2) can be solved as a linear equation system
579 # of the form: ($rot_matrix be all elements a(ij))
580 #
581 # a11*r1 + a12*r2 + a13*r3 = x1
582 # a21*r1 + a22*r2 + a23*r3 = x2
583 # a31*r1 + a32*r2 + a33*r3 = x3
584
585 # Transpose the rotation matrix and then decompose_LR
586 9 $rot_matrix->transpose($rot_matrix);
587 9 $rot_matrix = $rot_matrix->decompose_LR();
588
589 # Save old function
590 9 my $old_function = $self->get_function;
591
592 11 ▶ my $rotator = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
593
594 # If the first argument is 'restore', we restore the old
595 # function from the lexical $old_function and return.
596 # This is needed for the unrotate() method.
597 11 ▶ $self->{function} = $old_function, return if $_[0] eq 'restore';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "restore" 9 2 11
598
599 # We apply the old function as usual.
600 2 my $source_vec = _new_from_cols(
601 [ [ $old_function->(@_) ] ],
602 );
603
604 # But then we rotate the results.
605 2 my ($dimension, $result_vector, undef) = $rot_matrix->solve_LR($source_vec);
606
607 # There should be a solution to the equation system.
608 2 ▶ croak "Dimension of rotation result is '$dimension' but should be 0."
Branch: 1 branch
BranchTrueFalseTotal
if $dimension 0 2 2
609 if $dimension;
610
611 # return the rotated vector
612 return (
613 2 $result_vector->element(1,1),
614 $result_vector->element(2,1),
615 $result_vector->element(3,1),
616 );
617 9 };
618
619 # Set the new function wrapper to become the actual function
620 # that will be used for projection calculations.
621 9 $self->set_function($rotator);
622
623 # The level of nested rotation has been increased.
624 9 $self->{rotated}++;
625
626 9 return ($rotator, $old_function);
627 }
628
629
630 # method unrotate
631 #
632 # Removes the evil hack of rotation using an evil hack.
633 # Takes an optional integer as argument. Removes
634 # [integer] levels of rotation. 'Level of rotation'
635 # means: one rotation wrapper of the original function
636 # as wrapped using rotate().
637 # If no integer was passed, defaults to the total number
638 # of rotations that were made, effectively removing
639 # any rotation.
640 # Returns the number of wrappers (rotations) removed.
641
642 2 ▶sub unrotate {
Subroutine unrotate: called CC=8 SCAR=21.3
Pod documented
643 2 my $self = shift;
644
645 2 ▶ croak "You need to assign a function before rotating it."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->get_function 0 2 2
646 if not defined $self->get_function();
647
648 # How many levels of rotation to we want to remove?
649 2 my $level = shift;
650
651 # Boundary checking for the level. Default to the number
652 # of rotation wrappers.
653 2 ▶ if (not defined $level or $level <= 0 or $level > $self->{rotated}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $level or $level <= 0 or $level > $self->{"rotated"}) 1 1 2
Condition66%4/6
not defined $level or $level <= 0 or $level > $self->{"rotated"}
l!l&&r!l&&!r
101
not defined $level or $level <= 0
l!l&&r!l&&!r
101
MC/DC33%1/3
not defined $level or $level <= 0 or $level > $self->{"rotated"}
not defined $level $level <= 0 $level > $self->{"rotated"}
Truth table50%2/4
not(A) or B or C
A= not defined $levelB= $level <= 0C= $level > $self->{"rotated"}
ABCresult
1XX1
01X1
0011
0000
654 1 $level = $self->{rotated};
655 }
656
657 2 $level = int $level; # I don't trust users.
658
659 # If we're rotated at all.
660 2 ▶ if ($self->{rotated}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'rotated'}) { } 2 0 2
661
662 # Count the number of wrappers removed.
663 2 my $no_unrotated = 0;
664
665 # While we still want to unrotate and while we still can
666 2 ▶ while ($level != 0 and $self->{rotated} != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level != 0 and $self->{"rotated"} != 0) 9 2 11
Condition66%2/3
$level != 0 and $self->{"rotated"} != 0
!ll&&!rl&&r
209
MC/DC50%1/2
$level != 0 and $self->{"rotated"} != 0
$level != 0 $self->{"rotated"} != 0
Truth table66%2/3
A and B
A= $level != 0B= $self->{"rotated"} != 0
ABresult
0X0
100
111
667
668 # Let the function restore its parent.
669 9 $self->{function}->('restore');
670
671 9 $self->{rotated}--;
672 9 $level--;
673 9 $no_unrotated++;
674 }
675
676 2 return $no_unrotated;
677
678 } else {
679 # We aren't even rotated.
680 0 return 0;
681
682 }
683 }
684
685
686 # Function acos (arc cosine)
687 # Not a method.
688 9 ▶sub acos { atan2( sqrt( 1 - $_[0] * $_[0] ), $_[0] ) }
Subroutine acos: called CC=1 SCAR=0.0
Pod documented
689
690 2 1;
691
692 __END__
↑ Summary Project3D/Function.pm »