#  Copyright (c) 1997-2004
#  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: vinci.rules,v 1.20 2004/11/05 21:50:51 gawrilow Exp $

# path to the "vinci" executable
custom $vinci;

CONFIGURE {
   $vinci=find_via_path("vinci")
   or die "'vinci' program not found\n";
}

object FloatPolytope;

# Use the @see external.vinci program for volume computation.
label vinci

vinci.volume: VOLUME : AMBIENT_DIM, VERTICES, FACETS, VERTICES_IN_FACETS
PRECONDITION: N_FACETS, AMBIENT_DIM
   $this->AMBIENT_DIM <= 10  &&  $this->N_FACETS <= 200
PRECONDITION: DIM, AMBIENT_DIM
   $this->DIM==$this->AMBIENT_DIM;
BODY:
   my $dim = $this->AMBIENT_DIM;
   my @vertices = convert_to_float($this->VERTICES);
   my $v = @vertices;
   my @facets = convert_to_float($this->FACETS);
   my $f = @facets;

# write the ext-file
   my $tempname=new Poly::Tempfile;
   open P, ">$tempname.ext"
   or die "can't create temporary file $tempname.ext: $!";
   print P "Automatically generated by polymake.\n";
   print P "begin\n", $v, " ", $dim+1, " real\n";		# v+1 ???
   print P @vertices; # polymake and cdd have the same format
   print P "end\n", "hull\n", "incidences\n";
   close P;

# write the ine-file
   open(P, ">$tempname.ine")
   or die "can't create temporary file $tempname.ine: $!";
   print P "Automatically generated by polymake.\n";
   print P "begin\n", $f, " ", $dim+1, " real\n";		# f+1 ???
   print P @facets; # polymake and cdd have the same format
   print P "end\n";
   close P;

# write the icd-file
   open P, ">$tempname.inc"
   or die "can't create temporary file $tempname.inc: $!";
   print P "Automatically generated by polymake.\n";
   print P "begin\n";
   print P $f, " ", $v, " ", $v, "\n";
   foreach (@{$this->VERTICES_IN_FACETS}) {
      my @vi=/\d+/g;
      print P $#vi+1, ": @vi";
   }
   print P "end\n";
   close P;

# call vinci
   open P, "vinci $tempname |"
   or die "couldn't run vinci: $!";
   while (<P>) {
      if (/^Volume:\s+/) {
	 $this->VOLUME=0+$';
	 close P;
	 return;
      }
   }
   die "can't parse output from vinci";


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