#  Copyright (c) 1997-2004
#  Technische Universitaet Berlin, Germany
#  Department of Mathematics,
#  Research Group Algorithmic and Discrete Mathematics
#  mailto:polymake@math.tu-berlin.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, 59 Temple Place - Suite 330 Boston, MA 02111-1307, USA.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: transformation,v 1.6 2006/02/21 20:33:24 gawrilow Exp $

application 'polytope';

my $default_iterations=50;


# Polytope, Transformation Matrix, #iterations =>
sub visual_transformation {
   my ($Poly, $Transform, $n_iter)=@_;
   if ($Poly->AMBIENT_DIM>3) {
      die "only 2-d and 3-d polytopes are allowed\n";
   }
   $n_iter ||= $default_iterations;

   my @vis=($Poly->VISUAL(VertexStyle => "thickness 1"));
   my $vertices=$Poly->VERTICES;

   for (my $i=1; $i<=$n_iter; ++$i) {
      my $new_vertices=[ ];
      Modules::client("projective_transformation", $Poly, $new_vertices, $vertices, $Transform);
      push @vis, $vis[0]->clone(Name => $Poly->name."_$i", Vertices => [ $Poly->dehomogenize($new_vertices) ]);
      $vertices=$new_vertices;
   }
   compose(@vis);
}


# Polytope, Array<Transformation Matrix>, #iterations =>
sub visual_random_transformation {
   my ($Poly, $TransformationArray, $n_iter)=@_;
   if ($Poly->AMBIENT_DIM>3) {
      die "only 2-d and 3-d polytopes are allowed\n";
   }
   $n_iter ||= $default_iterations;

   my $n_lines=@$TransformationArray;
   if ($n_lines%5!=0) {
      die "TransformationArray has $n_lines lines, not divisible by 5.\n";
   }
   my $m=$n_lines/5;
   my @Transformation;
   for (my $k=0; $k<$m; ++$k) {
      push @Transformation, [ @$TransformationArray[5*$k..5*$k+3] ];
      $Transformation[-1]->[0] =~ s/^\s*<//;
   }

   my @vis=($Poly->VISUAL(VertexStyle => "thickness 1"));
   $n_iter*=$m;
   my @transformed=($Poly->VERTICES, map { [ ] } 2..$n_iter);	# scratch sections
   for (my $i=1; $i<$n_iter; ++$i) {
      my $rnd_tr=$Transformation[int(rand($m))];
      my $rnd_poly=$transformed[int(rand($i))];
      my $target=$transformed[$i];
      Modules::client("projective_transformation", $Poly, $target, $rnd_poly, $rnd_tr);
      push @vis, $vis[0]->clone(Name => $Poly->name."_$i", Vertices => [ $Poly->dehomogenize($target) ]);
   }
   compose(@vis);
}


# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
