/* 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: is_ball_or_sphere_h.cc 10012 2011-01-12 23:19:57Z gawrilow $
*/

#include "polymake/client.h"
#include "polymake/topaz/is_sphere_h.h"
#include <sstream>

namespace polymake { namespace topaz {

bool is_ball_or_sphere_client(perl::Object p, bool check_for_sphere, perl::OptionSet options)
{
   const HasseDiagram HD = p.give("HASSE_DIAGRAM");

   int strategy=options["strategy"];
   int n_stable_rounds;
   if (!(options["stable_rounds"] >> n_stable_rounds))
      n_stable_rounds=(HD.dim()-1) * 1000;
   
   const bool verbose=options["verbose"];
   const RandomSeed seed(options["seed"]);
   UniformlyRandom<Integer> random_source(seed);

   bool is_ball_or_sphere= check_for_sphere ? is_sphere_h(HD,random_source,strategy,n_stable_rounds)
                                            : is_ball_or_sphere_h(HD,random_source,strategy,n_stable_rounds);
   while (!is_ball_or_sphere && ++strategy <= 1) {
      if (verbose)
         cout << "is_ball_or_sphere_h: after " << n_stable_rounds
              << " iterations without improvement:\nUnable to determine, whether the complex is a ball or a sphere.\n"
              << "Trying strategy " << strategy << "." << endl;
      is_ball_or_sphere = check_for_sphere ? is_sphere_h(HD,random_source,strategy,n_stable_rounds)
                                           : is_ball_or_sphere_h(HD,random_source,strategy,n_stable_rounds);
   }

   if (!is_ball_or_sphere) {
      std::ostringstream e;
      e << "is_ball_or_sphere_h: after " << n_stable_rounds
        << " iterations without improvement:\nUnable to determine, whether the complex is a ball or a sphere.\n";
      throw std::runtime_error(e.str());
   }

   return true;
}

Function4perl(&is_ball_or_sphere_client, "is_ball_or_sphere_h(SimplicialComplex $ { strategy=>0, stable_rounds=>undef, verbose=>0, seed=>undef })");

} }

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