blib/lib/Panotools/Script.pm untested

filter: Statementstmt 0.0% 0 / 583 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 106 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 58 Podpod 0.0% 0 / 39 totaltotal 0.0% 0 / 936 SCAR 86.5
CC
75
Cov
0%
Read
156.0
Subset
156.0
Merge
72.0
CRAP 5700.0
1 package Panotools::Script;
2  
3 =head1 NAME
4  
5 Panotools::Script - Panorama Tools scripting
6  
7 =head1 SYNOPSIS
8  
9 Read, write and manipulate hugin script files.
10  
11 =head1 DESCRIPTION
12  
13 Library and utilities for manipulating project files created by the hugin photo
14 stitching software.
15  
16 This file format is shared with various other tools, in particular this module
17 is also capable of working with Panorama Tools script files.
18  
19 =cut
20  
21 use strict;
22 use warnings;
23  
24 use Panotools::Script::Line::Mode;
25 use Panotools::Script::Line::Panorama;
26 use Panotools::Script::Line::Option;
27 use Panotools::Script::Line::Image;
28 use Panotools::Script::Line::ImageMetadata;
29 use Panotools::Script::Line::Output;
30 use Panotools::Script::Line::Control;
31 use Panotools::Script::Line::ControlMorph;
32 use Panotools::Script::Line::Variable;
33 use Panotools::Script::Line::Mask;
34 use Digest::MD5 'md5_hex';
35  
36 use File::Temp qw/ tempdir /;
37 use File::Spec;
38 use Math::Trig qw/:radial pi great_circle_distance/;
39  
40 use Storable qw/ dclone /;
41  
42 our $VERSION = '0.28';
43  
44 our $CLEANUP = 1;
45 $CLEANUP = 0 if defined $ENV{DEBUG};
46  
47 =head1 USAGE
48  
49 my $p = new Panotools::Script;
50  
51 =cut
52  
53 sub new
54 {
55 my $class = shift;
56 $class = ref $class || $class;
57 my $self = bless {}, $class;
58 $self->_defaults;
59 return $self;
60 }
61  
62 sub _defaults
63 {
64 my $self = shift;
65 $self->{mode} = new Panotools::Script::Line::Mode;
66 $self->{panorama} = new Panotools::Script::Line::Panorama;
67 $self->{option} = new Panotools::Script::Line::Option;
68 $self->{variable} = new Panotools::Script::Line::Variable;
69 $self->{image} = [];
70 $self->{imagemetadata} = [];
71 $self->{output} = [];
72 $self->{control} = [];
73 $self->{controlmorph} = [];
74 $self->{mask} = [];
75 }
76  
77 =pod
78  
79 $p->Read ('/path/to/script.txt');
80  
81 =cut
82  
83 sub Read
84 {
85 my $self = shift;
86 $self->_defaults;
87 my $path = shift || return 0;
88 if ($path eq '-')
89 {
90 open FILE, '<-';
91 }
92 else
93 {
94 open FILE, "<", $path or die "cannot read-open $path";
95 }
96 my @raw = <FILE>;
97 close FILE;
98  
99 $self->{md5} = md5_hex (join '', @raw);
100  
101 my ($volume, $directories, $file) = File::Spec->splitpath ($path);
102  
103 for my $line (@raw)
104 {
105 $line =~ s/(\r|\n)//g;
106 $self->Mode->Parse ($line) if ($line =~ /^m /);
107 $self->Panorama->Parse ($line) if ($line =~ /^p /);
108 $self->Option->Parse ($line) if ($line =~ /^#hugin_/);
109 $self->Variable->Parse ($line) if ($line =~ /^v /);
110 if ($line =~ /^i /)
111 {
112 my $image = new Panotools::Script::Line::Image;
113 $image->Parse ($line);
114 push @{$self->Image}, $image;
115 }
116 if ($line =~ /^o /)
117 {
118 my $output = new Panotools::Script::Line::Output;
119 $output->Parse ($line);
120 push @{$self->Output}, $output;
121 }
122 if ($line =~ /^c /)
123 {
124 my $control = new Panotools::Script::Line::Control;
125 $control->Parse ($line);
126 push @{$self->Control}, $control;
127 }
128 if ($line =~ /^C /)
129 {
130 my $controlmorph = new Panotools::Script::Line::ControlMorph;
131 $controlmorph->Parse ($line);
132 push @{$self->ControlMorph}, $controlmorph;
133 }
134 if ($line =~ /^#-hugin /)
135 {
136 # per-image metadata
137 my $imagemeta = new Panotools::Script::Line::ImageMetadata;
138 $imagemeta->Parse ($line);
139 push @{$self->ImageMetadata}, $imagemeta;
140 }
141 if ($line =~ /^k /)
142 {
143 my $mask = new Panotools::Script::Line::Mask;
144 $mask->Parse ($line);
145 push @{$self->Mask}, $mask;
146 }
147 }
148 $self->Output2Image;
149 return 1;
150 }
151  
152 =pod
153  
154 $p->Write ('/path/to/script.txt');
155  
156 File paths in a Panorama Tools script file are generally relative to the
157 directory containing the script. Modify this or otherwise prefix the filenames
158 by supplying an optional second argument:
159  
160 $p->Write ('/path/to/script.txt', '../path/to/prefix/tofiles');
161  
162 =cut
163  
164 sub Write
165 {
166 my $self = shift;
167 $self->Image2Output if scalar @{$self->Output};
168 my $path = shift || return 0;
169 my $vector = shift || '';
170 if ($path eq '-')
171 {
172 open FILE, '>-';
173 }
174 else
175 {
176 open FILE, ">", $path or die "cannot write-open $path";
177 }
178 print FILE "# Created by ". (ref $self) ." $VERSION\n\n";
179 print FILE $self->Panorama->Assemble;
180 print FILE $self->Mode->Assemble;
181 print FILE "\n# Image lines\n";
182 for my $index (0 .. (scalar (@{$self->Image}) - 1))
183 {
184 print FILE $self->ImageMetadata->[$index]->Assemble if defined ($self->ImageMetadata->[$index]);
185 print FILE $self->Image->[$index]->Assemble if defined ($self->Image->[$index]);
186 }
187 print FILE "\n# Variable lines\n";
188 print FILE $self->Variable->Assemble;
189 print FILE "\n# Control point lines\n";
190 for my $control (@{$self->Control})
191 {
192 print FILE $control->Assemble;
193 }
194 for my $controlmorph (@{$self->ControlMorph})
195 {
196 print FILE $controlmorph->Assemble;
197 }
198 print FILE "\n# Mask lines\n";
199 for my $mask (@{$self->Mask})
200 {
201 print FILE $mask->Assemble;
202 }
203 print FILE "\n# option lines\n";
204 print FILE $self->Option->Assemble;
205 print FILE "\n*\n";
206 print FILE "\n# Output image lines\n";
207 for my $output (@{$self->Output})
208 {
209 print FILE $output->Assemble ($vector);
210 }
211 close FILE;
212 }
213  
214 =pod
215  
216 Clone a script object
217  
218 $clone = $p->Clone;
219  
220 =cut
221  
222 sub Clone
223 {
224 my $self = shift;
225 dclone ($self);
226 }
227  
228 =pod
229  
230 Access various sections of the scriptfile:
231  
232 $p->Mode; # a L<Panotools::Script::Line::Mode> object
233 $p->Panorama; # a L<Panotools::Script::Line::Panorama> object
234 $p->Variable; # a L<Panotools::Script::Line::Variable> object
235  
236 =cut
237  
238 sub Mode
239 {
240 my $self = shift;
241 $self->{mode};
242 }
243  
244 sub Panorama
245 {
246 my $self = shift;
247 $self->{panorama};
248 }
249  
250 sub Option
251 {
252 my $self = shift;
253 $self->{option};
254 }
255  
256 sub Variable
257 {
258 my $self = shift;
259 $self->{variable};
260 }
261  
262 =pod
263  
264 $p->Image; # an array of L<Panotools::Script::Line::Image> objects
265 $p->Output; # an array of L<Panotools::Script::Line::Output> objects
266 $p->Control; # an array of L<Panotools::Script::Line::Control> objects
267 $p->ControlMorph; # an array of L<Panotools::Script::Line::ControlMorph> objects
268  
269 =cut
270  
271 sub Image
272 {
273 my $self = shift;
274 $self->{image};
275 }
276  
277 sub ImageMetadata
278 {
279 my $self = shift;
280 $self->{imagemetadata};
281 }
282  
283 sub Output
284 {
285 my $self = shift;
286 $self->{output};
287 }
288  
289 sub Control
290 {
291 my $self = shift;
292 $self->{control} = shift if scalar @_;
293 $self->{control};
294 }
295  
296 sub ControlMorph
297 {
298 my $self = shift;
299 $self->{controlmorph};
300 }
301  
302 sub Mask
303 {
304 my $self = shift;
305 $self->{mask};
306 }
307  
308 =pod
309  
310 Rotate transform all the images in a project, angles in degrees:
311  
312 $p->Transform ($roll, $pitch, $yaw);
313  
314 =cut
315  
316 sub Transform
317 {
318 my $self = shift;
319 my ($roll, $pitch, $yaw) = @_;
320 for my $image (@{$self->Image})
321 {
322 $image->Transform ($roll, $pitch, $yaw);
323 }
324 $self->Image2Output;
325 }
326  
327 =pod
328  
329 'o' output lines are generated by PTOptimizer and contain stitching parameters
330 for each input image.
331 'i' image lines provide parameters for optimisation as well as stitching.
332  
333 Update the 'image' lines based on 'output' lines and vice-versa like so:
334  
335 $p->Output2Image;
336 $p->Image2Output;
337  
338 =cut
339  
340 sub Output2Image
341 {
342 my $self = shift;
343 for my $index (0 .. (@{$self->Output} - 1))
344 {
345 for my $entry (keys %{$self->Output->[$index]})
346 {
347 $self->Image->[$index] = new Panotools::Script::Line::Image unless (defined $self->Image->[$index]);
348 $self->Image->[$index]->{$entry} = $self->Output->[$index]->{$entry}
349 unless (defined $self->Image->[$index]->{$entry} and $self->Image->[$index]->{$entry} =~ /=/);
350 }
351 }
352 }
353  
354 sub Image2Output
355 {
356 my $self = shift;
357 for my $index (0 .. (@{$self->Image} - 1))
358 {
359 for my $entry (keys %{$self->Image->[$index]})
360 {
361 $self->Output->[$index] = new Panotools::Script::Line::Output unless (defined $self->Output->[$index]);
362 unless ($self->Image->[$index]->{$entry} =~ /=/)
363 {
364 $self->Output->[$index]->{$entry} = $self->Image->[$index]->{$entry};
365 }
366 else
367 {
368 my $base = $self->Image->[$index]->{$entry};
369 $base =~ s/=//;
370 $self->Output->[$index]->{$entry} = $self->Image->[$base]->{$entry};
371 }
372 }
373 }
374 }
375  
376 =pod
377  
378 Remove duplicate control points from the project, returns a list of deleted
379 points:
380  
381 my $deleted = $p->Duplicates;
382  
383 =cut
384  
385 sub Duplicates
386 {
387 my $self = shift;
388 my $packed_seen = {};
389 my $points_uniq = [];
390 my $points_deleted = [];
391 for my $point (@{$self->Control})
392 {
393 my $packed = $point->Packed;
394 if (defined $packed_seen->{$packed})
395 {
396 push @{$points_deleted}, $point;
397 }
398 else
399 {
400 push @{$points_uniq}, $point;
401 }
402 $packed_seen->{$packed} = 'TRUE';
403 }
404  
405 $self->Control ($points_uniq);
406 return $points_deleted;
407 }
408  
409 =pod
410  
411 Remove all points with an error distance greater than a threshold measured in
412 pixels, returns a list of deleted points:
413  
414 my $pruned = $p->Prune (12.345);
415  
416 =cut
417  
418 sub Prune
419 {
420 my $self = shift;
421 my $threshold = shift;
422 return [] unless $threshold > 0;
423 my $points_new = [];
424 my $points_pruned = [];
425  
426 for my $point (@{$self->Control})
427 {
428 if ($point->{t} > 0 or $point->Distance ($self) < $threshold)
429 {
430 push @{$points_new}, $point;
431 }
432 else
433 {
434 push @{$points_pruned}, $point;
435 }
436 }
437  
438 $self->{control} = $points_new;
439 return $points_pruned;
440 }
441  
442 =pod
443  
444 Extract a new object consisting of just the requested images, related
445 control points and optimisation settings:
446  
447 my $subset = $p->Subset (1, 2, 34, 56);
448  
449 Images can be requested in any order, but they will be returned in the same
450 order as the 'parent' project.
451  
452 =cut
453  
454 sub Subset
455 {
456 my $self = shift;
457 my @selection = sort {$a <=> $b} @_;
458  
459 my $mapping;
460 for my $index (0 .. scalar @selection -1)
461 {
462 return 0 unless $selection[$index] =~ /^[0-9]+$/;
463 return 0 if $selection[$index] >= scalar @{$self->{image}};
464 $mapping->{$selection[$index]} = $index;
465 }
466 return 0 unless scalar keys %{$mapping} == scalar @selection;
467  
468 my $pto_out = $self->Clone;
469  
470 # only use selected images
471 $pto_out->{image} = [];
472 $pto_out->{imagemetadata} = [];
473 $pto_out->{variable} = new Panotools::Script::Line::Variable;
474  
475 for my $index (0 .. scalar @{$self->{image}} -1)
476 {
477 next unless defined $mapping->{$index};
478  
479 # copy metadata for selected image
480 $pto_out->{imagemetadata}->[$mapping->{$index}]
481 = $self->{imagemetadata}->[$index]->Clone
482 if defined $self->{imagemetadata}->[$index];
483  
484 # copy selected image but resolve '=0' style references
485 my $image = $self->{image}->[$index]->Clone;
486 for my $key (keys %{$image})
487 {
488 # resolve references as anchor image may be gone
489 if ($image->{$key} =~ /^=([0-9]+)$/)
490 {
491 $image->{$key} = $self->{image}->[$1]->{$key};
492 }
493 # rereference to image 0 if possible
494 if (scalar @{$pto_out->{image}} > 0
495 and $image->{$key} eq $pto_out->{image}->[0]->{$key}
496 and $key =~ /^([abcdev]|R[abcde]|V[abcdxy])$/)
497 {
498 $image->{$key} = '=0';
499 }
500 }
501 $pto_out->{image}->[$mapping->{$index}] = $image;
502  
503 # copy only optimisation parameters for selected image
504 $pto_out->{variable}->{$mapping->{$index}}
505 = {%{$self->{variable}->{$index}}}
506 if defined $self->{variable}->{$index};
507 }
508  
509 # copy only control points related to selected images
510 $pto_out->{control} = [];
511 for my $control (@{$self->{control}})
512 {
513 next unless defined $mapping->{$control->{n}};
514 next unless defined $mapping->{$control->{N}};
515 my $clone = $control->Clone;
516 $clone->{n} = $mapping->{$control->{n}};
517 $clone->{N} = $mapping->{$control->{N}};
518 push @{$pto_out->{control}}, $clone;
519 }
520  
521 # copy masks for selected images
522 $pto_out->{mask} = [];
523 for my $mask (@{$self->{mask}})
524 {
525 next unless defined $mapping->{$mask->{i}};
526 my $clone = $mask->Clone;
527 $clone->{i} = $mapping->{$mask->{i}};
528 push @{$pto_out->{mask}}, $clone;
529 }
530  
531 return $pto_out;
532 }
533  
534 =pod
535  
536 Merge a project with another:
537  
538 $p->Merge ($newstuff);
539  
540 This adds extra images from $newstuff, skipping duplicates. All control points
541 except exact duplicates are imported regardless.
542  
543 =cut
544  
545 sub Merge
546 {
547 my $self = shift;
548 my $b = shift || return 0;
549  
550 # create lookup table relating filenames to index in final project
551 my $mapping = {};
552  
553 for my $index (0 .. scalar @{$self->Image} -1)
554 {
555 $mapping->{$self->Image->[$index]->{n}} = $index;
556 }
557  
558 my $index = scalar keys %{$mapping};
559 for my $image (@{$b->Image})
560 {
561 unless (defined $mapping->{$image->{n}})
562 {
563 $mapping->{$image->{n}} = $index;
564 $index++;
565 }
566 }
567  
568 # insert metadata, image and variable info if a new filename
569 for my $index (0 .. scalar @{$b->Image} -1)
570 {
571 my $filename = $b->Image->[$index]->{n};
572 next if defined $self->Image->[$mapping->{$filename}];
573  
574 $self->ImageMetadata->[$mapping->{$filename}] = $b->ImageMetadata->[$index]->Clone
575 if defined $b->ImageMetadata->[$index];
576  
577 my $image = $b->Image->[$index]->Clone;
578 for my $key (keys %{$image})
579 {
580 # update references
581 if ($image->{$key} =~ /^=([0-9]+)$/)
582 {
583 my $index_new = $mapping->{$b->Image->[$1]->{n}};
584 $image->{$key} = "=$index_new";
585 }
586 }
587 $self->Image->[$mapping->{$filename}] = $image;
588  
589 my $variable = $b->Variable->Clone;
590 $self->Variable->{$mapping->{$filename}} = $variable->{$index};
591 }
592  
593 # append control points
594 for my $control (@{$b->Control})
595 {
596 my $clone = $control->Clone;
597 $clone->{n} = $mapping->{$b->Image->[$clone->{n}]->{n}};
598 $clone->{N} = $mapping->{$b->Image->[$clone->{N}]->{n}};
599 push @{$self->Control}, $clone;
600 }
601  
602 # add masks
603 for my $mask (@{$b->Mask})
604 {
605 my $jump;
606 for my $self_mask (@{$self->Mask})
607 {
608 $jump = 1 if ($self_mask->{i} eq $mask->{i} and $self_mask->{p} eq $mask->{p});
609 }
610 next if $jump;
611 my $clone = $mask->Clone;
612 $clone->{i} = $mapping->{$b->Image->[$clone->{i}]->{n}};
613 push @{$self->Mask}, $clone;
614 }
615  
616 $self->Duplicates;
617  
618 for my $option (keys %{$b->Option})
619 {
620 $self->Option->{$option} = $b->Option->{$option} unless defined $self->Option->{$option};
621 }
622  
623 return 1;
624 }
625  
626 =pod
627  
628 Get a summary of control point error distances in pixel units scaled to the
629 output panorama:
630  
631 my ($total, $min, $max, $average, $sigma) = $p->Stats;
632  
633 =cut
634  
635 sub Stats
636 {
637 my $self = shift;
638  
639 # get a list of all the distances
640 my @distances;
641 for my $point (@{$self->Control})
642 {
643 next unless $point->{t} == 0;
644 push @distances, $point->Distance ($self);
645 }
646  
647 my $total = scalar (@distances);
648  
649 return (0,0,0,0,0) unless $total;
650  
651 # calculate maximum and average distance
652 my $max = undef;
653 my $min = undef;
654 my $sum = 0;
655 for my $distance (@distances)
656 {
657 $min = $distance unless defined $min;
658 $min = $distance if ($min > $distance);
659 $max = $distance unless defined $max;
660 $max = $distance if ($max < $distance);
661 $sum += $distance;
662 }
663 my $average = $sum / $total;
664  
665 # calculate variation and standard deviation (sigma)
666 $sum = 0;
667 for my $distance (@distances)
668 {
669 my $variation = $distance - $average;
670 $sum += $variation * $variation;
671 }
672 my $variance = $sum / $total;
673 my $sigma = sqrt ($variance);
674  
675 return ($total, $min, $max, $average, $sigma);
676 }
677  
678 =pod
679  
680 Centre input images into the final panorama:
681  
682 $p->Centre ('y');
683 $p->Centre ('p');
684 $p->Centre ('r');
685  
686 =cut
687  
688 sub Centre
689 {
690 my $self = shift;
691 my $param = shift;
692  
693 for my $image (@{$self->Image})
694 {
695 my $sigma_old = $self->Sigma ($param);
696 $image->{$param} += 360;
697 next if $self->Sigma ($param) < $sigma_old;
698 $image->{$param} -= 720;
699 next if $self->Sigma ($param) < $sigma_old;
700 $image->{$param} += 360;
701 }
702  
703 my $average_r = $self->_average ('r');
704 my $average_p = $self->_average ('p');
705 my $average_y = $self->_average ('y');
706  
707 if ($param eq 'r')
708 {
709 $self->Transform (0, 0, 0 - $average_y);
710 $self->Transform (0, 0 - $average_p, 0);
711 $self->Transform (0 - $average_r, 0, 0);
712 $self->Transform (0, $average_p, 0);
713 $self->Transform (0, 0, $average_y);
714 }
715 if ($param eq 'p')
716 {
717 $self->Transform (0, 0, 0 - $average_y);
718 $self->Transform (0, 0 - $average_p, 0);
719 $self->Transform (0, 0, $average_y);
720 }
721 if ($param eq 'y')
722 {
723 $self->Transform (0, 0, 0 - $average_y);
724 }
725 }
726  
727 sub _average
728 {
729 my $self = shift;
730 my $param = shift;
731 my $sum = 0;
732 for my $image (@{$self->Image})
733 {
734 $sum += $image->{$param}
735 }
736 return $sum / scalar @{$self->Image};
737 }
738  
739 sub Sigma
740 {
741 my $self = shift;
742 my $param = shift;
743 my $sum = 0;
744 my $average = $self->_average ($param);
745 for my $image (@{$self->Image})
746 {
747 my $variation = $image->{$param} - $average;
748 $sum += $variation * $variation;
749 }
750 my $variance = $sum / scalar @{$self->Image};
751 return sqrt ($variance);
752 }
753  
754 =pod
755  
756 Split the project into exposure stacks based in roll, pitch & yaw, or into
757 exposure layers based on EV values:
758  
759 $stacks = $pto->Stacks;
760 $layers = $pto->ExposureLayers;
761  
762 Returns a list of image number lists.
763  
764 e.g. extract the first stack as a new project:
765  
766 $pto_stack = $pto->Subset (@{$pto->Stacks->[0]});
767  
768 =cut
769  
770 sub Stacks
771 {
772 my $self = shift->Clone;
773 my $stacks = [];
774 my $maxShift = $self->Image->[0]->{v} / 10.0;
775 my @images = (0 .. scalar @{$self->Image} -1);
776 while (@images)
777 {
778 my $base_image = shift @images;
779 my $stack = [$base_image];
780 my @images_remaining = @images;
781 for my $image (@images)
782 {
783 if (_samestack ($self->{image}->[$base_image], $self->{image}->[$image], $maxShift))
784 {
785 push @{$stack}, $image;
786 @images_remaining = grep !/^$image$/, @images_remaining;
787 }
788 }
789 @images = @images_remaining;
790 push @{$stacks}, $stack;
791 }
792 return $stacks;
793 }
794  
795 sub _samestack
796 {
797 my ($image0, $image1, $maxShift) = @_;
798 my $minShift = 360.0 - $maxShift;
799 return 1
800 if ( (abs ($image0->y - $image1->y) < $maxShift || abs ($image0->y - $image1->y) > $minShift)
801 && abs ($image0->p - $image1->p) < $maxShift );
802 return 0;
803 }
804  
805 =pod
806  
807 Split a project into exposure layers, returns a list of lists of image ids:
808  
809 my $layers = $pto->ExposureLayers (1.0);
810  
811 Deafults to 0.5EV difference threshold.
812  
813 =cut
814  
815 sub ExposureLayers
816 {
817 my $self = shift->Clone;
818 my $layers = [];
819 my $maxEVDiff = shift || 0.5;
820 my @images = (0 .. scalar @{$self->Image} -1);
821 while (@images)
822 {
823 my $base_image = shift @images;
824 my $layer = [$base_image];
825 my @images_remaining = @images;
826 for my $image (@images)
827 {
828 if (_samelayer ($self->{image}->[$base_image], $self->{image}->[$image], $maxEVDiff))
829 {
830 push @{$layer}, $image;
831 @images_remaining = grep !/^$image$/, @images_remaining;
832 }
833 }
834 @images = @images_remaining;
835 push @{$layers}, $layer;
836 }
837 return $layers;
838 }
839  
840 sub _samelayer
841 {
842 my ($image0, $image1, $maxEVDiff) = @_;
843 return 1 if (abs ($image0->{Eev} - $image1->{Eev}) < $maxEVDiff );
844 return 0;
845 }
846  
847 =pod
848  
849 Get a list of unconnected groups, i.e. a list of image id lists:
850  
851 $groups = $pto->ConnectedGroups;
852  
853 warn 'just one group' if scalar @{$groups} == 1;
854  
855 =cut
856  
857 sub ConnectedGroups
858 {
859 my $self = shift;
860 return [[]] unless scalar @{$self->Image};
861 my $groups = [[0]];
862 my $group_id = 0;
863  
864 my @images = (1 .. scalar @{$self->Image} -1);
865 while (@images)
866 {
867 my $match = 0;
868 for my $image (@images)
869 {
870 next if $match;
871 next if grep /^$image$/, @{$groups->[$group_id]};
872 for my $base_image (@{$groups->[$group_id]})
873 {
874 next if $match;
875 if (scalar $self->Connections ($base_image, $image))
876 {
877 push @{$groups->[$group_id]}, $image;
878 $match = 1;
879 @images = grep !/^$image$/, @images;
880 }
881 }
882 }
883 unless ($match)
884 {
885 $group_id++;
886 $groups->[$group_id]->[0] = shift @images;
887 }
888 }
889 return $groups;
890 }
891  
892 =pod
893  
894 Count the connections between any two images:
895  
896 $points = $pto->Connections (3, 5);
897  
898 =cut
899  
900 sub Connections
901 {
902 my $self = shift;
903 my ($a, $b) = @_;
904  
905 my $results = 0;
906 for my $control (@{$self->Control})
907 {
908 my $N = $control->{N};
909 my $n = $control->{n};
910 $results++ if (($n == $a and $N == $b) or ($n == $b and $N == $a));
911 }
912 return $results;
913 }
914  
915 =pod
916  
917 Given a project with unlinked lens parameters, link them together with the same
918 lens number if all distortion, and photometric parameters match:
919  
920 $pto->UnifyLenses;
921  
922 =cut
923  
924 sub UnifyLenses
925 {
926 my $self = shift;
927 for my $id (1 .. scalar @{$self->Image} -1)
928 {
929 my $img = $self->Image->[$id];
930 for my $base_id (0 .. $id -1)
931 {
932 my $base_img = $self->Image->[$base_id];
933 if ($img->v ($self) eq $base_img->{v}
934 and $img->a ($self) eq $base_img->{a}
935 and $img->b ($self) eq $base_img->{b}
936 and $img->c ($self) eq $base_img->{c}
937 and $img->d ($self) eq $base_img->{d}
938 and $img->e ($self) eq $base_img->{e}
939 and $img->Ra ($self) eq $base_img->{Ra}
940 and $img->Rb ($self) eq $base_img->{Rb}
941 and $img->Rc ($self) eq $base_img->{Rc}
942 and $img->Rd ($self) eq $base_img->{Rd}
943 and $img->Re ($self) eq $base_img->{Re}
944 and $img->Va ($self) eq $base_img->{Va}
945 and $img->Vb ($self) eq $base_img->{Vb}
946 and $img->Vc ($self) eq $base_img->{Vc}
947 and $img->Vd ($self) eq $base_img->{Vd}
948 and $img->Vx ($self) eq $base_img->{Vx}
949 and $img->Vy ($self) eq $base_img->{Vy}
950 )
951 {
952 $img->{v} = "=$base_id";
953 $img->{a} = "=$base_id";
954 $img->{b} = "=$base_id";
955 $img->{c} = "=$base_id";
956 $img->{d} = "=$base_id";
957 $img->{e} = "=$base_id";
958 $img->{Ra} = "=$base_id";
959 $img->{Rb} = "=$base_id";
960 $img->{Rc} = "=$base_id";
961 $img->{Rd} = "=$base_id";
962 $img->{Re} = "=$base_id";
963 $img->{Va} = "=$base_id";
964 $img->{Vb} = "=$base_id";
965 $img->{Vc} = "=$base_id";
966 $img->{Vd} = "=$base_id";
967 $img->{Vx} = "=$base_id";
968 $img->{Vy} = "=$base_id";
969 next;
970 }
971 }
972 }
973 }
974  
975 =pod
976  
977 Given a project with stacks indicated by 'j' parameters, hard-link the
978 positions (only recognised by Hugin with layout mode code).
979  
980 $pto->LinkStacks;
981  
982 =cut
983  
984 sub LinkStacks
985 {
986 my $self = shift;
987 for my $id (1 .. scalar @{$self->Image} -1)
988 {
989 my $img = $self->Image->[$id];
990 my $found;
991 for my $base_id (0 .. $id -1)
992 {
993 next if $found;
994 my $base_img = $self->Image->[$base_id];
995 next unless defined $img->{j};
996 if ($img->{j} eq $base_img->{j})
997 {
998 $img->{r} = "=$base_id";
999 $img->{p} = "=$base_id";
1000 $img->{y} = "=$base_id";
1001 $found = 1;
1002 next;
1003 }
1004 }
1005 }
1006 }
1007  
1008 =pod
1009  
1010 Return the angular distance in degrees between two images:
1011  
1012 $deg = $pto->AngularDistance (3, 5);
1013  
1014 =cut
1015  
1016 sub AngularDistance
1017 {
1018 my $self = shift;
1019 return undef unless ($_[0] =~ /^[0-9]+$/ and $_[1] =~ /^[0-9]+$/);
1020 my $yaw_a = $self->Image->[$_[0]]->y ($self);
1021 my $pitch_a = $self->Image->[$_[0]]->p ($self);
1022 my $yaw_b = $self->Image->[$_[1]]->y ($self);
1023 my $pitch_b = $self->Image->[$_[1]]->p ($self);
1024 my $distance = great_circle_distance ($yaw_a * pi/180, pi/2 - ($pitch_a * pi/180),
1025 $yaw_b * pi/180, pi/2 - ($pitch_b * pi/180));
1026 return $distance * 180/pi;
1027 }
1028  
1029 =pod
1030  
1031 Look at all photos and calculate an optimal pixel width for this panorama,
1032 optionally supply a scaling factor:
1033  
1034 $width = $pto->OptimalWidth (0.7);
1035  
1036 This number is rounded up to the nearest multiple of 16 pixels.
1037  
1038 =cut
1039  
1040 sub OptimalWidth
1041 {
1042 my $self = shift;
1043 my $factor = shift || 1;
1044 my $pix_radius_max = 1;
1045 for (@{$self->Image})
1046 {
1047 my $pix_radius = $_->Radius ($self);
1048 $pix_radius_max = $pix_radius if $pix_radius > $pix_radius_max;
1049 }
1050 my $pix_width;
1051 my $rad_fov = Math::Trig::deg2rad ($self->Panorama->{v});
1052 $rad_fov = 2 * Math::Trig::pi() if $rad_fov == 0;
1053 if ($self->Panorama->{f} == 0)
1054 {
1055 return $self->Panorama->{w} if $self->Panorama->{v} >= 180;
1056 $pix_width = 2 * $pix_radius_max * Math::Trig::tan ($rad_fov/2);
1057 }
1058 else
1059 {
1060 $pix_width = $pix_radius_max * $rad_fov;
1061 }
1062 return int (($pix_width * $factor / 16) +1) * 16;
1063 }
1064  
1065 =pod
1066  
1067 Hugin ships with a tool called pano_trafo for querying the forward and reverse
1068 transform for pixel coordinates in a PTO project.
1069  
1070 Initialise this as a service, spawns two pano_trafo processes which are only
1071 killed when the perl process finishes:
1072  
1073 $pto->InitTrafo ('/path/to/project.pto');
1074  
1075 This is very unlikely to work on non-unixy systems.
1076  
1077 =cut
1078  
1079 sub InitTrafo
1080 {
1081 my $self = shift;
1082 my $path_pto = shift || return 0;
1083 use IPC::Open2;
1084 use Symbol;
1085  
1086 my $pid_forward_old = $self->{trafo_forward}->{pid} || undef;
1087 my $WTR_forward = gensym;
1088 my $RDR_forward = gensym;
1089 my $pid_forward = open2 ($RDR_forward, $WTR_forward, 'pano_trafo', $path_pto);
1090 $self->{trafo_forward} = {pid => $pid_forward, WTR => $WTR_forward, RDR => $RDR_forward};
1091  
1092 my $pid_reverse_old = $self->{trafo_reverse}->{pid} || undef;
1093 my $WTR_reverse = gensym;
1094 my $RDR_reverse = gensym;
1095 my $pid_reverse = open2 ($RDR_reverse, $WTR_reverse, 'pano_trafo', '-r', $path_pto);
1096 $self->{trafo_reverse} = {pid => $pid_reverse, WTR => $WTR_reverse, RDR => $RDR_reverse};
1097  
1098 waitpid ($pid_forward_old, 0) if defined $pid_forward_old;
1099 waitpid ($pid_reverse_old, 0) if defined $pid_reverse_old;
1100 }
1101  
1102 =pod
1103  
1104 Query the forward transform like so:
1105  
1106 ($X, $Y) = $pto->Trafo ($image_no, $x, $y);
1107  
1108 =cut
1109  
1110 sub Trafo
1111 {
1112 my $self = shift;
1113 my ($image, $x, $y) = @_;
1114 my $WTX = $self->{trafo_forward}->{WTR};
1115 my $RDX = $self->{trafo_forward}->{RDR};
1116 print $WTX join (' ', $image, $x, $y) . "\n";
1117 my $result = <$RDX>;
1118 chomp $result;
1119 return split ' ', $result;
1120 }
1121  
1122 =pod
1123  
1124 Query the reverse transform like so:
1125  
1126 ($x, $y) = $pto->TrafoReverse ($image_no, $X, $Y);
1127  
1128 =cut
1129  
1130 sub TrafoReverse
1131 {
1132 my $self = shift;
1133 my ($image, $x, $y) = @_;
1134 my $WTX = $self->{trafo_reverse}->{WTR};
1135 my $RDX = $self->{trafo_reverse}->{RDR};
1136 print $WTX join (' ', $image, $x, $y) . "\n";
1137 my $result = <$RDX>;
1138 chomp $result;
1139 return split ' ', $result;
1140 }
1141  
1142 =head1 COPYRIGHT
1143  
1144 Copyright (c) 2001 Bruno Postle <bruno@postle.net>. All Rights Reserved.
1145  
1146 This program is free software; you can redistribute it and/or
1147 modify it under the terms of the GNU General Public
1148 License as published by the Free Software Foundation; either
1149 version 2 of the License, or (at your option) any later version.
1150  
1151 This software is distributed in the hope that it will be useful,
1152 but WITHOUT ANY WARRANTY; without even the implied warranty of
1153 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1154 General Public License for more details.
1155  
1156 You should have received a copy of the GNU General Public
1157 License along with this software; if not, write to the Free Software
1158 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1159  
1160 =cut
1161  
1162 1;
1163