/* Copyright (c) 1997-2006
   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.
*/

#ifndef _POLYMAKE_SPRING_EMBEDDER_H
#define _POLYMAKE_SPRING_EMBEDDER_H "$Project: polymake $$Id: SpringEmbedder.h 7487 2006-12-06 15:16:09Z gawrilow $"

#include <cmath>
#include <SpringEmbedderBase.h>
#include <vector>
#include <Matrix.h>
#include <Vector.h>
#include <Poly.h>

namespace polymake { namespace graph {

struct se_edge_attr {
   double wanted_length, inv_wanted_length;

   se_edge_attr() : wanted_length(1) { }
  
   explicit se_edge_attr(double d) : wanted_length(d) { }

   template <typename Input>
   friend Input& operator>> (GenericInput<Input>& is, se_edge_attr& me)
   {
      return is.top() >> me.wanted_length;
   }

   template <typename Output>
   friend Output& operator<< (GenericOutput<Output>& is, const se_edge_attr& me)
   {
      return is.top() << me.wanted_length;
   }

   bool operator== (const se_edge_attr& e) const
   {
      return wanted_length == e.wanted_length;
   }

   bool operator< (const se_edge_attr& e) const
   {
      return wanted_length < e.wanted_length;
   }

   bool operator> (const se_edge_attr& e) const
   {
      return wanted_length > e.wanted_length;
   }
};

struct se_traits {
   static const int dim=3;
   typedef se_node_attr<dim> node_attr;
   typedef Graph<node_attr, se_edge_attr, undirected> graph_type;
};

class SpringEmbedder
   : public SpringEmbedderBase<SpringEmbedder, se_traits> {
   typedef SpringEmbedderBase<SpringEmbedder, se_traits> _super;
   friend class SpringEmbedderBase<SpringEmbedder, se_traits>;
public:
   enum {
      opt_scale=_super::opt_next, opt_repulsion, opt_z_ordering, opt_z_factor, opt_skip_node_attr,
      opt_next
   };

protected:
   double scale, rep, z_factor;
   Vector<double> z_ordering;
   double z_min, z_max;
   Set<int> fixed_vertices;

public:
   explicit SpringEmbedder(const argv_option *options)
      : _super(options)
   {
      init_params(options);
   }

   void init_params(const argv_option *options);
   void adjust_edge_lengths(bool different_edge_lengths);

   void set_repulsion(double r) { rep = r; }
   void set_z_factor(double f) { z_factor = f; }
   void set_z_ordering(Poly& p, const argv_option *options);

   void set_fixed_vertices(const Set<int>& v) { fixed_vertices=v; }

   const double get_repulsion() { return rep; }
   const double get_z_factor() { return z_factor; }
   const bool has_z_ordering() { return !z_ordering.empty(); }

   template <typename Iterator>
   void start_points(Iterator src)
   {
      for (Entire< Nodes<graph_type> >::iterator n=entire(nodes(G));  !n.at_end();  ++n, ++src)
	 n->x=*src * scale;
      if (has_z_ordering()) {
	 z_min=-scale;
	 z_max=scale;
      }
      gravity=fixed_vertices.empty();
      barycenter.fill(0);
      scale=1;
   }

   void calculate_forces(std::vector<vector3>& forces);
};

} }

#endif // _POLYMAKE_SPRING_EMBEDDER_H

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