#  Copyright (c) 1997-2019
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://polymake.org
#
#  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.
#-------------------------------------------------------------------------------

use application 'polytope';

die "usage: polymake --script visual_normal_fan object|DATA-FILE\n" unless @ARGV;

my $poly=ref($ARGV[0]) ? $ARGV[0] : load($ARGV[0]);

die "defined for objects of type Polytope<Scalar> only" unless ref($poly) =~ m/^Polymake::polytope::Polytope__/;
die "defined for polytopes of dimension <= 3 only" unless $poly->DIM <= 3;

my $nf = fan::normal_fan($poly);


my $d=$nf->RAYS->cols;
my $rays= normalized(convert_to<Float>($nf->RAYS));
my $zero=new Vector<Float>($d);
my @visuals = ();
my $i = 0;
my $vertices = $poly->VERTICES;
foreach (@{$nf->MAXIMAL_CONES}) {
  my $cone=$_;
  my $all_ones=ones_vector<Float>($cone->size()+1);
  my $v=$all_ones | -($rays->minor($cone,All) / $zero);
  my $T1 = new Matrix<Float>([[0,1,0,0],[0,0,1,0],[0,0,0,1]]);
  my $V = 1 | (convert_to<Float>(dehomogenize($vertices->[$i])));
  my $T = $V / $T1;
  my @labels = @{$cone};
  push @labels, " ";
  my $p=new polytope::Polytope<Float>(VERTICES => $v*$T, VERTEX_LABELS => \@labels);
  my $pv=$p->VISUAL(FacetColor => new RGB(.7,.7,0) );
  my $dim=$p->DIM;
  push @visuals, $pv;
  ++$i;
}
compose($poly->VISUAL(VertexLabels => "hidden"), @visuals);

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