/* 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: expansive_motions.cc,v 1.1 2005/11/10 10:29:11 thilosch Exp $"

#include <Poly.h>
#include <Vector.h>
#include <Matrix.h>
#include <SparseMatrix.h>
#include <SparseVector.h>
#include <Rational.h>
#include <Graph.h>
#include <linalg.h>
#include <PowerSet.h>

#include <ch_client.h>
#define GMPRATIONAL
#include <cdd_interface.h>

namespace polymake { namespace polytope {
   namespace {
      typedef Graph< Vector< Rational > > Framework;
   }

   void expansive_motions(Poly& p) {

     const Framework F = p.give("FRAMEWORK");
     const int n_points = F.nodes();
     const int dim = F.node(0).size();
     const SparseMatrix< Rational > M = p.give("INFINITESIMAL_MOTIONS");

     //generate rigidity matrix of complete graph
     SparseMatrix< Rational > K(((n_points-1)*n_points)/2,dim*n_points);
     Rows< SparseMatrix< Rational >  >::iterator ri = rows(K).begin();
     const sequence points = sequence(0,n_points);
     const Vector<Rational> zero_vector_dim(dim,Rational(0));
     for(Entire< Subsets_of_k< sequence > >::const_iterator ei = entire(all_subsets_of_k(points,2)); 
	 !ei.at_end(); ++ei, ++ri) {
	int v = (*ei).front();
	int w = (*ei).back();
	const Vector<Rational> diff = F.node(v)-F.node(w);
	if(diff != zero_vector_dim) {
	   (*ri).slice(v*dim,dim) = diff;
	   (*ri).slice(w*dim,dim) = -diff;
	}
     }

     const Vector<Rational> zero_vector_npointschoose2((n_points*(n_points-1))/2,Rational(0));     
     const SparseMatrix< Rational > KMt(K*T(M));
     p.take("EXPANSIVE_MOTION_CONE") << Matrix<Rational>(zero_vector_npointschoose2 | KMt);
     
     //calculate extreme rays of cone of expansive motions.
     polytope::cdd_interface::solver<Rational> solver;
     try {
	const Matrix<Rational> ineq(zero_vector_npointschoose2 | KMt);
 	const Matrix<Rational> V = solver.enumerate_vertices(ineq,Matrix<Rational>(0,M.rows()),true);
	ListMatrix< Vector<Rational> > rays(0,V.cols()-1);
	for(Entire< Rows< Matrix<Rational> > >::const_iterator ri = entire(rows(V)); !ri.at_end(); ++ri) {
	   if((*ri)[0] == 0) {
	      if( ineq * (*ri) != zero_vector_npointschoose2) {
		 rays /= (*ri).slice(range(1,V.cols() -1));
	      }
	   }
	}
	const Matrix<Rational> EM(rays*M);
	p.take("EXPANSIVE_MOTIONS") << EM;
	p.take("EXPANSIVE_PATTERNS") << EM*T(K);
	//calculate coordinates of translated points EXPANSIVE_MOTION_COORDINATES
	{
	   int i = 0;
	   Matrix<Rational> MplusP(EM);     
	   for(Entire< Nodes< Framework > >::const_iterator ni = entire(nodes(F)); !ni.at_end(); ++ni, ++i) {
	      for(Entire< Rows< Matrix<Rational> > >::iterator mi = entire(rows(MplusP)); !mi.at_end(); ++mi) {
		 (*mi).slice(i*dim,dim) += *ni;
	      }
	   }
	   p.take("EXPANSIVE_MOTION_COORDINATES") << MplusP;
	}
     }
     catch (const linalg_error& error) {
	cerr << "error computing a V-representation of the cone of expansive motions" << endl;
     }
  }
} }

using namespace polymake;

/** @file expansive_motions
 *
 *  Calculate the extreme rays of the cone of expansive motions and the corresponding motions and patterns
 *  for a given framework.
 *
 *  @synopsis expansive_motions <file> 
 *
 *  @reading  FRAMEWORK, INFINITESIMAL_MOTIONS
 *  @writing  EXPANSIVE_MOTIONS, EXPANSIVE_PATTERNS, EXPANSIVE_MOTION_COORDINATES, EXPANSIVE_MOTION_CONE
 */

int main(int argc, const char *argv[]) {
  if (argc!=2) {
    cerr << "usage: " << argv[0] << " <file>" << endl;
    return 1;
  }
  try {
     Poly p(argv[1], ios::in | ios::out);

     polytope::expansive_motions(p);
  }
  catch (const std::exception& e) {
    cerr << e.what() << endl;
    return 1;
  }
  return 0;
}
