#  Copyright (c) 1997-2010
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.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: porta.rules 9502 2010-02-24 00:26:29Z gawrilow $


require PortaParser;

# path to the xporta executable
custom $xporta;

CONFIGURE {
   find_via_path($xporta,"xporta");
}

# @topic credits/porta
# PORTA is a collection of routines for analyzing polytopes and polyhedra.
# Copyright by Thomas Christof and Andreas Loebel.
# http://www.iwr.uni-heidelberg.de/groups/comopt/software/PORTA/

object Polytope<Rational> {

# @category Convex hull computation
# Run the [[wiki:external_software#porta]] program, implementing the Fourier-Motzkin elimination method.
# The essential drawback of this tool is that it employs a limited-precision arithmetic,
# and therefore can fail on numerically difficult problems.
label porta


rule porta.convex_hull.primal: FACETS, AFFINE_HULL : AMBIENT_DIM, DIM, VERTICES | POINTS {
   my $tempname=new Tempfile;
   my $converter=new PortaConverter("$tempname.poi");
   $converter->print_dim($this->AMBIENT_DIM);
   $converter->print_points($this->VERTICES | POINTS);
   undef $converter;

   my $command="$xporta -l -T $tempname.poi";
   $command.=" >/dev/null 2>&1" unless $DebugLevel;
   system($command) and die "porta exited with error code\n";

   my $parser=new PortaParser("$tempname.poi.ieq");
   if ($this->has_far_facet) {
      push @{$parser->Ineq}, [ 1, (0) x $this->AMBIENT_DIM ];
   }
   $this->FACETS=$parser->Ineq;
   $this->AFFINE_HULL=$parser->Eq;
   unlink "porta.log";
}
weight 4.20;
precondition : DIM { $this->DIM > 0 }
permutation : FacetPerm;


rule porta.convex_hull.dual: VERTICES : AMBIENT_DIM, FACETS | INEQUALITIES, VALID_POINT {
   my $tempname=new Tempfile;
   my $converter=new PortaConverter("$tempname.ieq");
   $converter->print_dim($this->AMBIENT_DIM);
   $converter->print_valid_point($this->VALID_POINT);
   $converter->print_inequalities($this->FACETS | INEQUALITIES, $this->lookup("AFFINE_HULL | EQUATIONS"));
   undef $converter;

   my $command="$xporta -l -T $tempname.ieq";
   $command.=" >/dev/null 2>&1" unless $DebugLevel;
   system($command) and die "xporta exited with error code\n";

   my $parser=new PortaParser("$tempname.ieq.poi");
   $this->VERTICES=$parser->Points;
   unlink "porta.log";
}
weight 4.10;
permutation : VertexPerm;

}


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