/* 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.
*/

#ifndef _POLYMAKE_SPRING_EMBEDDER_H
#define _POLYMAKE_SPRING_EMBEDDER_H "$Project: polymake $$Id: SpringEmbedder.h,v 1.5 2005/09/22 17:12:12 gawrilow Exp $"

#if defined(__GNUC__) && __GNUC__==3 && __GNUC_MINOR__<3 && !defined(__APPLE__) && !defined(__COMO__)
#pragma interface
#endif

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

namespace polymake { namespace graph {

struct se_edge_attr {
   double wanted_length, inv_wanted_length, angle, real_edge_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_objective, opt_objective_factor,
      opt_next
   };

protected:
   double scale, rep, obj_factor;
   bool obj_function;
   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 apply_objective_function(const Matrix<double>& V, const Vector<double>& Obj);

   void set_repulsion(double r) { rep = r; }
   void set_objective_factor(double of) { obj_factor = of; }

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

   const double get_repulsion() { return rep; }
   const double get_obj_factor() { return obj_factor;}
   const bool has_obj_function() { return obj_function;}

   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;
      scale=1;
   }

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

} }

#endif // _POLYMAKE_SPRING_EMBEDDER_H

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