/* Copyright (c) 1997-2005
   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: 2D_tools.tcc 6357 2005-09-22 17:12:12Z gawrilow $"

#include <ext/hash_map>

namespace polymake { namespace topaz {

template <typename Complex>
bool C2_is_BOS (const Complex& C)
{
   typedef typename Complex::value_type Facet;

   DIMENSION_CHECK(C.empty(), "C2_is_BOS: Complex is empty.");
   
   // compute the hasse diagram, vertex set and test whether C is a pure 2-complex
   Set<int> V;
   for (typename Entire<Complex>::const_iterator c_it=entire(C); !c_it.at_end(); ++c_it) {
      V += *c_it;
      DIMENSION_CHECK_WRAP(c_it->size()>3, "C2_is_BOS: Dimension of " << *c_it << " is greater 2.");

      if (c_it->size()!=3) {
	 // cout << "C2_is_BOS: Complex is not pure" << endl;
	 return false;
      }
   }
   HasseDiagram<> HD=pure_hasse_diagram(C);

   // check whether C is a pseudo_manifold and compute the boundary B
   std::list< Set<int> > B;
   bool is_PM = is_pseudo_manifold(HD, back_inserter(B));
   if (!is_PM) {
      // cout << "C2_is_BOS: Complex is not a pseudo manifold" << endl;
      return false;
   }

   // check whether B is a sphere or empty
   const bool B_is_empty = B.empty();
   if (!B_is_empty && !C1_is_BOS(B)) {
      // cout << "C2_is_BOS: Boundary is not a sphere or empty" << endl;
      return false;
   }

   // S:= C + B*v for a vertex v not contained in C
   // note: S is a sphere <=> C is ball or sphere
   // check euler char of S
   // if (B.empty())  S = -1 + #vert - #edges_of_C + #C
   // else            S = -1 + (#vert + 1) - (#edges_of_C + #B) + (#C + #B)
   int euler_char= V.size() - HD.nodes_of_dim(-2).size() + C.size();
   if (B_is_empty) --euler_char;

   if (euler_char!=1) {
      // cout << "C2_is_BOS: Euler characteristic = " << euler_char << " != 1" << endl;
      return false;
   }

   return true;
}

template <typename Complex>
bool C2_is_manifold (const Complex& C)
{
   DIMENSION_CHECK(C.empty(), "C2_is_manifold: Complex is empty.");

   // compute the vertex set and test whether C is a pure 2-complex
   Set<int> V;
   for (typename Entire<Complex>::const_iterator c_it=entire(C); !c_it.at_end(); ++c_it) {
      V += *c_it;
      DIMENSION_CHECK_WRAP(c_it->size()>3, "C2_is_manifold: Dimension of " << *c_it << " is greater 2.");

      if (c_it->size()!=3)  // complex is not pure
	 return false;
   }

   // iterate over the vertices and test if their links are 1-balls or 1-spheres
   for (Entire< Set<int> >::iterator it=entire(V); !it.at_end(); ++it)
      if ( !C1_is_BOS(link(C,scalar2set(*it))) )
	 return false;

   return true;
}

} }

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