#  Copyright (c) 1997-2006
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.math.tu-berlin.de/polymake,  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: http://www.gnu.org/licenses/gpl.txt.
#
#  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.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: VisualGale.pm 7533 2006-12-20 23:55:58Z gawrilow $

no integer;

package Visual::Gale;
use Math::Trig;

use Struct (
   [ '@ISA' => 'Visual::Object' ],
   '$Polytope',
   [ '$VertexLabels' => 'unify_labels(#%)', default => 'undef' ],
   '@points',
   '@colors',
   '$dim',
   '%different_x_y',
   '%different_angles',
   '%blacks',
   '%whites',
   '@loops',
   '@gale_lines',
);

declare %decorations=( Name => undef, Title => undef, VertexLabels => enum("hidden") );

sub new {
   my $self=&_new;

   my ($gt, $gv, $VIF)=($self->Polytope->GALE_TRANSFORM, $self->Polytope->GALE_VERTICES, $self->Polytope->VERTICES_IN_FACETS);
   my @Transform=map { [ split ] } convert_to_float($gt);

   $self->dim = $#{$Transform[0]};
   croak( "can't visualize ", $self->dim, "-dimensional Gale diagrams" )
      if ($self->dim<1 || $self->dim>2);

   # get affine coordinates
   my $v=0;
   foreach (convert_to_float($gv)) {
      my @point=split;
      my $color=shift @point;
      push @{$self->colors}, $color;
      push @{$self->points}, \@point;
      if ($color) {
	 if ($self->dim==1) {
	    my $angle=atan2($Transform[$v]->[1], $Transform[$v]->[0]);
	    push @{$self->different_angles->{$angle}}, $v;
	 }
	 my $x_y="@point";
	 push @{$self->different_x_y->{$x_y}}, $v;
	 if ($color>0) {
	    $self->whites->{$x_y}=1;
	    delete $self->blacks->{$x_y};
	 } elsif (!exists $self->whites->{$x_y}) {
	    $self->blacks->{$x_y}=1;
	 }
      } else {
         push @{$self->loops}, $v;
      }
      ++$v;
   }

   # for each facet find a triple of b/w points not belonging to it
   my $n_vertices=@{$self->points};
   foreach (@$VIF) {
      my @vertex_list=/\d+/g;
      next if @vertex_list != $n_vertices-3;
      my @gale_line;
      # find the 3 vertices that are NOT on a facet
      my $candidate=0;
      foreach $v (@vertex_list, $n_vertices) {
	 if ($candidate < $v) {
	    push @gale_line, $candidate..$v-1;
	    last if @gale_line==3;
	 }
	 $candidate=$v+1;
      }
      push @{$self->gale_lines}, \@gale_line;
   }

   $self;
}

1

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