/* 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: is_manifold_h.cc,v 1.4 2004/12/08 09:34:56 gawrilow Exp $"

#include <Poly.h>
#include <is_sphere_h.h>
#include <argv_options.h>
#include <ctime>
#include <sstream>

namespace polymake {
namespace topaz {

namespace {

enum { in_seed, in_strategy, stable_rounds, quiet };

}

void is_manifold_h(std::ostream& os, Poly& p, const argv_option *options)
{
   const HasseDiagram<> HD = p.give("HASSE_DIAGRAM");
   const bool is_pmf = p.give("PSEUDO_MANIFOLD");
   if (!is_pmf) {
      if (!options[quiet])
	 os << "\nis_manifold_h: Complex is NOT a manifold.\n\n";
      p.take("MANIFOLD") << false;
      return;
   }

   const bool is_closed = p.give("CLOSED_PSEUDO_MANIFOLD");
   
   // get the time as initial seed
   time_t t;
   time(&t);
   const int seed = options[in_seed] ? atoi(options[in_seed]) : t;
   //   os << "seed: " << seed << endl;

   const int strategy= options[in_strategy] ? atoi(options[in_strategy]) : 0;
   const int n_stable_rounds= options[stable_rounds] ? atoi(options[stable_rounds]) : (HD.dim()-1) * 1000;

   for (Entire<sequence>::const_iterator n=entire(HD.nodes_of_dim(0)); !n.at_end(); ++n) {
      int lokal_strategy = strategy;
      const FacetList link_tmp = link_in_HD(HD,*n);
      const Array< Set<int> > link(link_tmp.size(), link_tmp.begin());

      bool is_bos= is_closed ? is_sphere_h(link, seed, lokal_strategy, n_stable_rounds)
	                     : is_ball_or_sphere_h(link, seed, lokal_strategy, n_stable_rounds);
      ++lokal_strategy;
      while (!is_bos && lokal_strategy <= 1) {
	 if (!options[quiet])
	    wrap(os) << "\nis_manifold_h: after " << n_stable_rounds
		     << " iterations without improvement:\nUnable to determine, whether link("
		     << HD.node(*n) << ") is a ball or a sphere.\n"
		     << "Trying strategy " << lokal_strategy << ".\n";
	 
	 is_bos= is_closed ? is_sphere_h(link, seed, lokal_strategy, n_stable_rounds)
	                   : is_ball_or_sphere_h(link, seed, lokal_strategy, n_stable_rounds);
	 ++lokal_strategy;
      }
      
      if (!is_bos) {
	 std::ostringstream e;
	 if (!options[quiet])
	    wrap(e) << "\nis_manifold_h: after " << n_stable_rounds
		    << " iterations without improvement:\nUnable to determine, whether link("
		    << HD.node(*n) << ") is a ball or a sphere.\n"
		    << "Exiting without result.\n";
	 throw std::runtime_error(e.str());
      }
   }

   if (!options[quiet])
      os << "\nis_manifold_h: Complex is a MANIFOLD.\n\n";
   p.take("MANIFOLD") << true;
}
   
} }

using namespace polymake;

/** @file is_manifold_h
 *
 *  Heuristic to determin if a complex is a manifold.
 *
 *  @synopsis is_manifold_h <file> [ -seed <s> -strategy {0|1} -stable_rounds <r> -quiet ]
 *
 *  @reading  HASSE_DIAGRAM, PSEUDO_MANIFOLD, CLOSED_PSEUDO_MANIFOLD
 *  @writing  may write MANIFOLD
 */

int main(int argc, const char* argv[])
{
   argv_option options[]={ {"-seed", true}, {"-strategy", true},
			   {"-stable_rounds", true}, {"-quiet", false} };
   if (!extract_options(argc,argv,1,options) || argc != 2) {
      cerr << "usage: " << argv[0] << " <file> [ -seed <s> -strategy {0|1} -stable_rounds <r> -quiet]" << endl;
      return 1;
   }
   try {
      Poly p(argv[1], ios::in | ios::out);
      topaz::is_manifold_h(std::cout, p, options);
   } 
   catch (const std::exception& e) {
     cerr << e.what() << endl;
     return 1;
   }
   return 0;
}


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