/* 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: rand_sphere.cc,v 1.28 2004/06/22 16:23:08 gawrilow Exp $"

#include <cstdlib>
#include <ctime>
#include <argv_options.h>
#include <Poly.h>
#include <Matrix.h>
#include <Vector.h>
#include <random_generators.h>
#include <argv_options.h>

namespace {
   enum { precision, seed };
}

namespace polymake { namespace polytope {

void rand_sphere(Poly& p, int d, int n, const argv_option *options)
{
   time_t t;
   const long s= options[seed] ? atol(options[seed]) : (time(&t), t);
   UniformRNG< Vector<double> > random_source(d, s);

   p.take("AMBIENT_DIM") << d;
   Matrix<double> Points(n, d+1);
   Points.col(0).fill(1);
   std::copy(random_source.begin(), entire(rows(Points.minor(All, range(1,d)))));
   p.take("POINTS") << std::setprecision( options[precision] ? atol(options[precision]) : 6 ) << Points;
   p.take("BOUNDED") << true;
}

} }

using namespace polymake;

/** @file rand_sphere
 *
 *  Produce a $d$-dimensional polytope with $n$ random vertices on the unit sphere.
 *  Floating-point coordinates are used.  Uniform distribution.
 *
 *  The default precision is set to 6 digits.
 *
 *  @synopsis rand_sphere <file> <dimension> <n> [ -precision <digits> ] [ -seed <s> ]
 *
 *  @writing  AMBIENT_DIM, POINTS, BOUNDED
 *
 *  @index clients Producing from scratch
 */

int main(int argc, const char *argv[])
{
   argv_option options[]={ {"-precision", true}, { "-seed", true } };
   if (!extract_options(argc, argv, 1, options) || argc!=4) {
      cerr << "usage: " << argv[0] << " <file> <dimension> <n_points> [ -precision <digits> -seed <s> ]" << endl;
      return 1;
   }
   try {
      const int
	 d(atoi(argv[2])), // dimension
	 n(atoi(argv[3])); // points
      
      // there is a certain natural restriction on the parameters
      if (d<2 || n<=d) {
         cerr << argv[0] << ": dimension >= 2 && #points > dimension" << endl;
         return 1;
      }

      Poly p(argv[1], ios::out | ios::trunc);
      polytope::rand_sphere(p, d, n, options);
   }
   catch (const std::exception& e) {
      cerr << e.what() << endl;
      return 1;
   }
   return 0;
}
