/* Copyright (c) 1997-2004
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.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.
*/

#ident "$Project: polymake $$Id: wedge-coord.cc,v 1.9 2004/06/22 16:23:08 gawrilow Exp $"

/* The only reason for extracting this portion code in a separate file is
   the exorbitant memory consumption by gcc. When it becomes more economical one nice day,
   this file should be merged back into wedge.cc */

#include <Rational.h>
#include <Matrix.h>
#include <Vector.h>
#include <IncidenceMatrix.h>
#include <linalg.h>

#if __GNUC__==3 && __GNUC_MINOR__<=2
template class pm::Vector<Rational>;
#endif

namespace polymake { namespace polytope {

namespace {

template <typename PointsSubset, typename Hyperplane> inline
void lift_to_hyperplane(PointsSubset P, const GenericVector<Hyperplane>& U) {
   typedef typename PointsSubset::value_type point;
   const int d=U.dim()-1;
   for (typename Entire<PointsSubset>::iterator p=entire(P); !p.at_end(); ++p) {
      typename point::iterator p_i=p->begin();
      typename Hyperplane::const_iterator u_i=U.top().begin();
      typename Hyperplane::element_type x(0);
      for (int i=d; i>0; --i, ++p_i, ++u_i)
	 x -= (*p_i) * (*u_i);
      (*p_i) = x / (*u_i);
   }
}

} // end unnamed namespace

Matrix<Rational>
wedge_coord(const Matrix<Rational>& V,		// input vertices
	    const Vector<Rational>& z0,		// interior point
	    const IncidenceMatrix<>& VIF,	// VERTICES_IN_FACETS
	    int edge_facet,
	    const Rational& z,
	    const Rational& z_prime) {
   const int d=V.cols(),
             n_vertices=VIF.cols(),
             n_vertices_edge=VIF[edge_facet].size(),
	     n_vertices_out= 2*n_vertices-n_vertices_edge;

   Matrix<Rational> V_out=(V / V.minor(~VIF[edge_facet],All)) | zero_vector<Rational>(n_vertices_out);

   // top (or bottom) facet goes thru the barycenter projection and the edge facet
   const Matrix<Rational> facet=
      null_space( ( z0                          | (z_prime ? z_prime : z)               ) /
		  (V.minor(VIF[edge_facet],All) | zero_vector<Rational>(n_vertices_edge)) );

   if (z_prime) {
      lift_to_hyperplane(select(rows(V_out), range(n_vertices,n_vertices_out-1)), facet[0]);

      if (z)	// bottom side facet has to be inclined too
	 V_out.col(d).slice(sequence(0,n_vertices)-VIF[edge_facet]) =
	    z/z_prime * V_out.col(d).slice(range(n_vertices,n_vertices_out-1));
   } else {
      lift_to_hyperplane(select(rows(V_out), sequence(0,n_vertices)-VIF[edge_facet]), facet[0]);
   }

   return V_out;
}

} }
