/* 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: splits_in_subdivision.cc 9991 2010-12-12 23:36:28Z gawrilow $
*/

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

namespace polymake { namespace polytope {

Set<int> splits_in_subdivision(const Matrix<Rational>& vert, const Array< Set<int> >& subdivision, const Matrix<Rational>& splits)
{
   const int n=vert.rows();  

   int n_splits=splits.rows();
   Set<int> split_set;

   for (int j=0; j<n_splits; ++j) {
      const Vector<Rational> a=splits.row(j);
      Set<int> left;
      Set<int> right;
      for (int k=0; k<n; ++k) {
         const Vector<Rational> v(vert.row(k));
         Rational val=a*v;
         if (val>0) {
            left.insert(k);
         }
         if (val< 0) {
            right.insert(k);
         }
      }
      bool cut=false;
      for (Entire< Array <Set<int> > >::const_iterator k=entire(subdivision); !cut && !k.at_end();  ++k)
         if (!(*k*left).empty() && !(*k*right).empty()) cut=true;
      if (!cut) split_set.push_back(j);
   }
   return split_set;
}

UserFunction4perl("# @category Subdivisions"
                  "# Tests which of the //splits// of a polyhedron are coarsenings of the given //subdivision//."
                  "# @param Matrix vertices the vertices of the polyhedron"
                  "# @param Array<Set<Int>> subdivision a subdivision of the polyhedron"
                  "# @param Matrix splits the splits of the polyhedron"
                  "# @return Set<Int>"
                  "# @author Sven Herrmann",
                  &splits_in_subdivision,"splits_in_subdivision");
} }

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