#  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: qhull.rules 9502 2010-02-24 00:26:29Z gawrilow $


# path to the qhull executable
custom $qhull;

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

require Polymake::ProgramPipe;

object Polytope<Float> {

# @category Convex hull computation
# Use the [[wiki:external_software#qhull]] program for convex hull computation.
label qhull

# @topic credits/qhull
# The Quickhull algorithm for convex hulls.
# Copyright by The Geometry Center, University of Minnesota
# http://www.qhull.org/

sub run_qhull {
   my ($points)=@_;
   my $Q=new ProgramPipe($qhull, qw{Fd n i});
   print $Q "begin\n", $points->rows, " ", $points->cols, " real\n", $points, "end\n";

   local $_=<$Q>;	# dimension
   my ($d)= /(\d+)/;
   $_=<$Q>;		# number of facets
   my ($n)= /(\d+)/;
   my $FACETS=new Matrix<Float>($n,$d);  --$d;

   # toggle the signs and move the facet offset in the first position
   for (my $i=0; $i<$n; ++$i) {
      $_=<$Q> or die "premature end of input\n";
      my $f=new Vector<Float>($_);
      @{$FACETS->[$i]}=(-$f->[$d], @{ -($f->slice(0,$d)) });
   }

   $_=<$Q>;
   /(\d+)/ && $1==$n or die "inconsistency in qhull output\n";
   my @VIF; $#VIF=$n-1;
   for (my $i=0; $i<$n; ++$i) {
      $_=<$Q> or die "premature end of input\n";
      chomp;
      $VIF[$i]="{$_}";
   }
   close $Q;
   ($n, $FACETS, \@VIF);
}

rule qhull.convex_hull.primal: FACETS, N_FACETS, VERTICES_IN_FACETS : VERTICES {
   my ($n, $FACETS, $VIF)=run_qhull($this->VERTICES);
   $this->FACETS=$FACETS;
   $this->N_FACETS=$n;
   $this->VERTICES_IN_FACETS=$VIF;
}
precondition : BOUNDED;
precondition : DIM, AMBIENT_DIM {
   $this->DIM==$this->AMBIENT_DIM;
}
weight 4.10;
permutation : FacetPerm;

# @notest
rule qhull.convex_hull.primal: FACETS, N_FACETS, POINTS_IN_FACETS : POINTS {
   my ($n, $FACETS, $VIF)=run_qhull($this->POINTS);
   $this->FACETS=$FACETS;
   $this->N_FACETS=$n;
   $this->POINTS_IN_FACETS=$VIF;
}
precondition : BOUNDED;
precondition : DIM, AMBIENT_DIM {
   $this->DIM==$this->AMBIENT_DIM;
}
weight 4.10;
permutation : FacetPerm;

}


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