#  Copyright (c) 1997-2015
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.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.
#-------------------------------------------------------------------------------

CREDIT vinci
  Vinci - Computing Volumes of Convex Polytopes.
  Copyright by Benno Büeler, Andreas Enge, and Komei Fukuda
  http://www.lix.polytechnique.fr/Labo/Andreas.Enge/Vinci.html

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

CONFIGURE {
   find_program($vinci, "vinci");
}

object Polytope<Float> {

# @category Triangulation and volume
# Use the [[wiki:external_software#vinci]] program for volume computation.
label vinci

rule vinci.volume: VOLUME : CONE_AMBIENT_DIM, VERTICES, N_VERTICES, FACETS, N_FACETS {
   my $dim = $this->CONE_AMBIENT_DIM;

   # write the ext-file
   my $tempname=new Tempfile;
   open my $P, ">$tempname.ext"
      or die "can't create temporary file $tempname.ext: $!";
   print $P "Automatically generated by polymake.\n",
            "begin\n", $this->N_VERTICES, " $dim real\n",
	    dense($this->VERTICES),
	    "end\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",
            "begin\n", $this->N_FACETS, " $dim real\n",
	    dense($this->FACETS),
	    "end\n";
   close $P;

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

}


# Local Variables:
# mode: perl
# cperl-indent-level:3
# End:
