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

#include "polymake/client.h"
#include "polymake/Rational.h"
#include "polymake/Vector.h"
#include "polymake/Matrix.h"
#include "polymake/ListMatrix.h"

namespace polymake { namespace polytope {

Matrix<Rational> voronoi(perl::Object p)
{
   const Matrix<Rational> sites=p.give("SITES");
   const int n=sites.rows(), d=sites.cols();
   Vector<Rational> min_val=sites[0];
   Vector<Rational> max_val=min_val;
   Rational maxx(0);


   // if not all SITES have first coordinate 1, we consider
   // the sites as a vector configuration
   int vector_configuration = 0;
   for (int i=0; !vector_configuration&&i<n; ++i) {
     if (sites(i,0)!=1) vector_configuration=1;
   }

   ListMatrix< Vector<Rational> > voronoi_ineq(0,d+1+vector_configuration);


   for (int i=0; i<n; ++i) {
      Vector<Rational> next_voronoi_ineq=unit_vector<Rational>(d+1+vector_configuration,d+vector_configuration);
      next_voronoi_ineq[0]=sqr(sites[i])-1+vector_configuration;
      if (next_voronoi_ineq[0]>maxx) maxx=next_voronoi_ineq[0];
      for (int k=1-vector_configuration; k<d; ++k) {
         const Rational& x(sites(i,k));
         if (x>max_val[k])
            max_val[k]=x;
         else if (x<min_val[k])
            min_val[k]=x;
         next_voronoi_ineq[k]=(-2)*x;
      }
      voronoi_ineq /= next_voronoi_ineq;
   }

   // since all SITES are required to be distinct, the defining inequalities are, in fact, facets;
   // e.g., for the computation of DELAUNAY_GRAPH it is essential that the first facets correspond
   // to the sites
   voronoi_ineq /= unit_vector<Rational>(d+1,0); // facet at infinity

   return voronoi_ineq;
}

UserFunctionTemplate4perl("# @category Delaunay subdivisions and Voronoi diagrams"
                          "# Compute the inequalities of the Voronoi polyhedron of a given VoronoiDiagram //V//."
                          "# The polyhedron is always unbounded. Introduce artificial cut facets later"
                          "# (e.g., for visualization); this must be done after the vertices have been computed."
                          "# @param VoronoiDiagram V"
                          "# @return Matrix",
                          "voronoi(VoronoiDiagram)");
} }

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