/* Copyright (c) 1997-2006
   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: schlegel_transform.cc,v 1.55 2006/01/19 14:21:19 gawrilow Exp $"

#include <Rational.h>
#include <Poly.h>
#include <linalg.h>
#include <Vector.h>
#include <Matrix.h>
#include <Graph.h>
#include <schlegel_common.h>

namespace polymake { namespace polytope {

void schlegel_transform(Poly& S, Poly& p)
{
   static const char* const req[]={ "BOUNDED", "VERTICES", "FACETS", "DUAL_GRAPH" };
   p.provide(req);
   const bool bounded=p.give("BOUNDED");
   if (!bounded)
      throw std::runtime_error("polytope must be bounded");

   const Matrix<Rational> V=p.give("VERTICES");
   const int d=V.cols();
   const Matrix<Rational> F=p.give("FACETS");
   const Graph<> DG=p.give("DUAL_GRAPH");

   const int proj_facet=S.give("FACET");
   const Rational zoom=S.give("ZOOM");
   const Vector<Rational> FacetPoint=S.give("FACET_POINT"),
                          InnerPoint=S.give("INNER_POINT");

   const Vector<Rational> ViewRay=FacetPoint-InnerPoint;
   const Rational alpha=schlegel_nearest_neighbor_crossing(F.minor(DG.neighbors(proj_facet),All), FacetPoint, ViewRay),
	          FxVR=F[proj_facet] * ViewRay;
   
   Vector<Rational> ViewPoint = FacetPoint;

   // First transformation step: move FacetPoint to the origin
   Matrix<Rational> tau = unit_matrix<Rational>(d);
   tau.row(0).slice(1) = -FacetPoint.slice(1);

   // Second step: move the view point to a far point if it is not already.
   // The projection facet is still fixed
   if (alpha>=0) {
      tau.col(0) -= F[proj_facet] / (zoom*alpha*FxVR);
      ViewPoint += zoom*alpha*ViewRay;
   } else if (zoom!=1) {
      // mapping zoom from (0,1) to (0,+inf)
      ViewPoint += (zoom/(1-zoom))*ViewRay;
      tau.col(0) -= F[proj_facet] / (zoom/(1-zoom)*FxVR);
   }
      
   // Third step: project the points onto the facet parallel to the view ray (shear)
   tau = tau * ( unit_matrix<Rational>(d) -
		 (vector2col(F[proj_facet]) * vector2row(ViewRay)) / FxVR );

   S.take("TRANSFORM") << tau;
   S.take("VIEWPOINT") << ViewPoint;
}

} }

using namespace polymake;

/** @file schlegel_transform
 *
 *  Calculate the transformation matrix for Schlegel diagram.
 *  Polytope must be bounded and full dimensional.
 *
 *  The transformation matrix, when applied to the points of the polytope, will put them
 *  on the projection facet, the latter will stay fixed. All calculations are made with
 *  rational coordinates. The last step towards a ready-to-display
 *  Schlegel diagram - an orthogonal transformation to $R^d-1$ - requires floating-point coordinates,
 *  and is `outsourced' into a separate client, @see schlegel_vertices.
 *
 *  @synopsis schlegel_transform <Schlegel> <Polytope>
 *
 *  @reading  SCHLEGEL_PARAMS, BOUNDED, VERTICES, FACETS, DUAL_GRAPH
 *  @writing  TRANSFORM, VIEWPOINT
 */

int main(int argc, const char *argv[])
{
   if (argc!=3) {
      cerr << "usage: " << argv[0] << " <Schlegel> <Polytope>" << endl;
      return 1;
   }
   try {
      Poly p(argv[2], ios::in);
      Poly S(argv[1], ios::in | ios::out);
      polytope::schlegel_transform(S,p);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}

// Local Variables:
// c-basic-offset:3
// End:
