/* 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_params.cc,v 1.8 2006/01/19 14:21:19 gawrilow Exp $"

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

namespace polymake { namespace polytope {

void schlegel_params(Poly& S, Poly& p)
{
   static const char* const req[]={ "VERTICES", "VERTEX_BARYCENTER", "VERTICES_IN_FACETS" };
   p.provide(req);
   const Matrix<Rational> V=p.give("VERTICES");
   const Vector<Rational> VB=p.give("VERTEX_BARYCENTER");
   const IncidenceMatrix<> VIF=p.give("VERTICES_IN_FACETS");

   const int d=V.cols();
   if (d<=3)
      throw std::runtime_error("dimension of polytope must be >= 3");

   const int proj_facet=S.give("FACET");
   if (proj_facet<0 || proj_facet>=VIF.rows())
      throw std::runtime_error("projection facet index out of range");

   const Vector<Rational> FB=average(select(rows(V), VIF[proj_facet]));
   S.take("FACET_POINT") << FB;
   S.take("INNER_POINT") << VB;
}

} }

using namespace polymake;

/** @file schlegel_params
 *
 *  Store the parameters of the Schlegel diagram in the file.
 *
 *  $facet$ is the index of the facet the polytope is projected on.
 *
 *  This client puts the viewpoint always on the ray joining the vertex barycenter
 *  of the polytope with the one of the chosen projection facet. Obviously, it should lie outside the polytope.
 *  On the other side, the ray crosses the hyperplanes of the neighbor facets.
 *  The viewpoint should lie closer to the projection facet than any of these crossing points;
 *  otherwise the diagram will not be a valid polytopal complex any more.
 *  However, in some rare cases all crossing points lie on the positive side of the projection facet;
 *  then the range of the valid viewpoint positions is open.
 *  
 *  $zoom$ controls the exact position of the viewpoint within the valid range.
 *  If specified, it should be a rational number between 0 and 1. The greater the zoom factor,
 *  the larger appear the rear facets in the diagram.
 *
 *  Defaults are the first facet (index 0) and zoom factor 9/10.
 *
 *  @synopsis schlegel_params <file>
 *
 *  @reading  VERTICES, VERTEX_BARYCENTER, VERTICES_IN_FACETS
 *  @writing  SCHLEGEL_PARAMS
 *
 *  @index clients Visualization
 */

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

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