/* 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: split_polyhedron.cc 10057 2011-03-11 17:29:30Z herr $
*/

#include "polymake/client.h"
#include "polymake/Matrix.h"
#include "polymake/SparseMatrix.h"
#include "polymake/Rational.h"
#include "polymake/Set.h"

namespace polymake { namespace polytope {

perl::Object split_polyhedron(perl::Object p_in)
{
   const Matrix<Rational> vert=p_in.give("VERTICES");
   const int n=vert.rows();  
   const int d=vert.cols();
  
   const Matrix<Rational> splits=p_in.give("SPLITS");
   int n_splits=splits.rows();

   SparseMatrix<Rational> facets(n_splits,n+1);
   for (int j=0; j<n_splits; ++j) {
      const Vector<Rational> a=splits.row(j);
      Set<int> left; //vertices of the left (>=) polytope
      for (int k=0; k<n; ++k) {
         const Rational val=a*vert.row(k);
         if (val>=0) {
            left.insert(k);
            if (val>0) facets(j,k+1)=val;
         }
      }
      perl::Object p_left("Polytope<Rational>");
      p_left.take("VERTICES")<<vert.minor(left,All);
      const Vector<Rational> left_centroid=p_left.give("CENTROID");
      const Rational left_volume=p_left.give("VOLUME");
      facets(j,0)=-d*left_volume*(a*left_centroid);
   }
   perl::Object p_out("Polytope<Rational>");
   p_out.take("FACETS")<<facets;

   const Vector<Rational> centroid=p_in.give("CENTROID");
   const Rational volume=p_in.give("VOLUME");
   const Vector<Rational> c=-d*volume*centroid;
   p_out.take("AFFINE_HULL")<<(c|T(vert));
   p_out.take("CONE_DIM")<<(n-d)+1;

   return p_out;
}

UserFunction4perl("# @category Subdivisions"
                  "# Computes the split polyhedron of a full-dimensional"
                  "# polyhdron //P//."
                  "# @param Polytope P"
                  "# @return Polytope",
                  &split_polyhedron,"split_polyhedron(Polytope)");
} }

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
