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

#include "polymake/client.h"
#include "polymake/Rational.h"
#include "polymake/Matrix.h"
#include "polymake/Vector.h"
#include "polymake/graph/HasseDiagram.h"
#include "polymake/Graph.h"
#include "polymake/FacetList.h"
#include "polymake/Array.h"
#include "polymake/linalg.h"
#include "polymake/polytope/bisector.h"
#include "polymake/vector"
#include "polymake/list"

namespace polymake { namespace polytope {

perl::Object stellar_all_faces(perl::Object p_in, int end_dim)
{
   Matrix<Rational> V=p_in.give("VERTICES");
   const Matrix<Rational> F=p_in.give("FACETS");
   const Vector<Rational> rel_int_point=p_in.give("REL_INT_POINT");
   FacetList VIF=p_in.give("VERTICES_IN_FACETS");
   const graph::HasseDiagram HD=p_in.give("HASSE_DIAGRAM");
   Graph<Undirected> DG=p_in.give("DUAL_GRAPH.ADJACENCY");

   const int dim = HD.dim();
   if (end_dim < 0)
      end_dim += dim;
   if (end_dim >= dim || end_dim <= 0)
      throw std::runtime_error("end dimension out of range");

   NodeMap<Undirected, Vector<Rational> > facet_normals(DG, rows(F).begin());

   int v_count = V.rows();
   int new_size = HD.node_range_of_dim(0).size();
   for (int d=dim-1; d>=end_dim; --d)
      new_size += HD.node_range_of_dim(d).size();
   V.resize(new_size, V.cols());

   // random access to the facets of VIF
   std::vector< FacetList::iterator > facets_of_VIF;
   facets_of_VIF.reserve(VIF.size());
   for (Entire<FacetList>::iterator fl_it=entire(VIF); !fl_it.at_end(); ++fl_it)
      facets_of_VIF.push_back(fl_it);

   // iterate over dimensions dim-1..end_dim and over all faces of each dimension
   // a new vertex is added for each face
   for (int d=dim-1; d>=end_dim; --d) {
      FacetList new_VIF;
      std::vector< FacetList::iterator > facets_of_new_VIF;
      facets_of_new_VIF.reserve(VIF.size() * (d+1));  // lower bound for #facets after stell-sd of faces dim-1, .., d

      Graph<Undirected> new_DG;
      NodeMap<Undirected, Vector<Rational> > new_facet_normals(new_DG);

      std::vector<int> corr_old_facet;
      corr_old_facet.reserve((d+1) * DG.nodes());
      Array< Set<int> > corr_new_facets(DG.nodes());

      int f_count = 0;
      for (Entire<sequence>::iterator it=entire(HD.node_range_of_dim(d)); !it.at_end(); ++it) {
         const Set<int>& face = HD.face(*it);

         // produce all relevant inequalities = each neighbour (in the dual graph) of the nodes
         // corresponding to the facets of star(face) produces one inequality
         Vector<Rational> m = average( rows(V.minor(face, All)) ) - rel_int_point;
         Rational t_max = Rational(3);

         Set<int> star_facets;
         for (FacetList::iteratorMax incid_facets=VIF.findMax(face); !incid_facets.at_end(); ++incid_facets)
            star_facets += incid_facets.index();

         for (Entire< Set<int> >::iterator st_it=entire(star_facets); !st_it.at_end(); ++st_it) {
            const Set<int> neighbors = DG.adjacent_nodes(*st_it)-star_facets;

            for (Entire< Set<int> >::const_iterator n_it=entire(neighbors); !n_it.at_end(); ++n_it) {
               const int v= (*facets_of_VIF[*st_it] * *facets_of_VIF[*n_it]).front();
               const Vector<Rational> inequ = bisector(facet_normals[*st_it], facet_normals[*n_it], V[v]);

               const Rational denominator = inequ * m;
               if (denominator) assign_min(t_max, -inequ * rel_int_point / denominator);
            }
         }

         // add new vertex
         const Rational scale = (t_max + 1) / 2;
         V[v_count] = rel_int_point + m * scale;

         // add new facets
         // iterate over all neighbours (in the dual graph) of the nodes
         // corresponding to the facets of star(face) and add a facet for each one
         for (Entire< Set<int> >::iterator st_it=entire(star_facets); !st_it.at_end(); ++st_it) {
            const Set<int> neighbors = DG.adjacent_nodes(*st_it)-star_facets;

            for (Entire< Set<int> >::const_iterator n_it=entire(neighbors); !n_it.at_end(); ++n_it) {
               Set<int> new_facet = (*facets_of_VIF[*st_it]) * (*facets_of_VIF[*n_it]);
               new_facet += v_count;

               new_VIF.insert(new_facet);
               facets_of_new_VIF.push_back(--new_VIF.end());

               if (d>end_dim) {  // add node to new_DG and store the facet inequality
                  Vector<Rational> equ = null_space(V.minor(new_facet,All))[0];
                  if (equ*rel_int_point < 0)
                     equ.negate();
                  new_facet_normals[new_DG.add_node()]=equ;

                  corr_old_facet.push_back(*st_it);
                  corr_new_facets[*st_it] += f_count;
               }

               ++f_count;
            }
         }
         ++v_count;

      }  // end of faces of dim d
        
      // update VIF
      VIF = new_VIF;
      facets_of_VIF = facets_of_new_VIF;

      if (d==end_dim)  // no update of DG
         break;

      // add adjacency information to new_DG and update DG
      FacetList ridges;
      std::vector< std::pair<int,int> > corresp_edge;
      corresp_edge.reserve(VIF.size()*(d+1)/2);  // lower bound for #ridges

      // iterate over new faces to determine adjacency
      for (Entire<FacetList>::iterator f_it=entire(VIF); !f_it.at_end(); ++f_it) {
         Set<int> old_neighbors = DG.adjacent_nodes(corr_old_facet[ f_it.index() ]);
         old_neighbors += corr_old_facet[ f_it.index() ];

         for (Entire< Set<int> >::const_iterator n_it=entire(old_neighbors); !n_it.at_end(); ++n_it)
            for (Entire< Set<int> >::iterator cand=entire(corr_new_facets[*n_it]); !cand.at_end(); ++cand)
               if (f_it.index() < *cand) {
                  const Set<int> ridge = *f_it * *facets_of_VIF[*cand];

                  if (ridge.size() >= dim-1) {  // potential ridge
                     if (ridges.insertMax(ridge)) {  // new potential ridge has been inserted -> remember corresponding edge
                        const int index = (--ridges.end()).index();

                        if (size_t(index) >= corresp_edge.size())
                           corresp_edge.resize(index+1);
                        corresp_edge[index].first = f_it.index();
                        corresp_edge[index].second = *cand;
                     }
                  }
               }
      }  // end iterate over faces

      // add edges to new_DG
      for (Entire<FacetList>::iterator r_it=entire(ridges);
           !r_it.at_end(); ++r_it)
         new_DG.edge(corresp_edge[r_it.index()].first,corresp_edge[r_it.index()].second);

      DG = new_DG;
      facet_normals = new_facet_normals;

   }  // end of d = dim-1..end_dim

   perl::Object p_out("Polytope<Rational>");
   p_out.set_description() << "Stellar subdivision of " << p_in.name() << " over all proper faces of dimension >= " << end_dim << endl;
   p_out.take("VERTICES") << V;
   p_out.take("VERTICES_IN_FACETS") << VIF;
   return p_out;
}

UserFunction4perl("# @category Producing a new polyhedron from others"
                  "# Perform a stellar subdivision of all proper faces, starting with the facets."
                  "# "
                  "# Parameter //d// specifies the lowest dimension of the faces to be divided."
                  "# It can also be negative, then treated as the co-dimension."
                  "# Default is 1, that is, the edges of the polytope."
                  "# @param Polytope P the input polytope"
                  "# @param Int d the lowest dimension of the faces to be divided;"
                  "#   negative values: treated as the co-dimension; default value: 1."
                  "# @return Polytope"
                  "# @author Nikolaus Witte",
                  &stellar_all_faces, "stellar_all_faces(Polytope; $=1)");
} }

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