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


# path to the programs from the TOPCOM package
custom $topcom;

CONFIGURE {
   $topcom =~ s{(?<=\S)$}{/points2chiro};
   find_via_path($topcom, "Please enter a full path to the `points2chiro' program from the TOPCOM package", "points2chiro", "A")
      and
   $topcom =~ s{/points2chiro$}{};
}

# @topic credits/topcom
# TOPCOM is a package for computing Triangulations Of Point Configurations and Oriented Matroids.
# Copyright by Jörg Rambau.
# http://www.rambau.wm.uni-bayreuth.de/TOPCOM/

require Polymake::ProgramPipe;

# @category Triangulation and volume
# Use the [[wiki:external_software#TOPCOM]] package for computing polytope triangulation.

label topcom

sub call_topcom_chiro($) {
   my $P=new ProgramPipe($DebugLevel ? () : ("2>/dev/null"), "$topcom/points2chiro", "--nosymmetries");
   # TOPCOM expects the homogeneous coordinate at the last position in the vector
   print $P "[", join(",", map { "[".join(",", @{$_->slice(1)}, $_->[0])."]" } @{$_[0]}), "][]\n";
   local $/;
   local $_=<$P>;
   s/\[\]\n$//s;
   $_
}


sub call_topcom_triang($) {
   my $P=new ProgramPipe($DebugLevel ? () : ("2>/dev/null"), "$topcom/chiro2placingtriang");
   print $P $_[0], "\n";
   local $/;
   local $_=<$P>;
   chomp;
   s/^\{//; s/\}$//; s/\},\{/}\n{/g; tr/,/ /;
   [ split /\n/, $_ ];
}

object Polytope {

rule CHIROTOPE : VERTICES {
   $this->CHIROTOPE=call_topcom_chiro($this->VERTICES);
}
weight 6.10;


rule topcom.triangulation: TRIANGULATION.FACETS : CHIROTOPE {
   $this->TRIANGULATION->FACETS=call_topcom_triang($this->CHIROTOPE);
}

}

object PointConfiguration {


rule CHIROTOPE : POINTS {
   $this->CHIROTOPE=call_topcom_chiro($this->POINTS);
}
weight 6.10;

rule topcom.triangulation: TRIANGULATION.FACETS : CHIROTOPE {
   $this->TRIANGULATION->FACETS=call_topcom_triang($this->CHIROTOPE);
}

}


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