#  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.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: visual.rules,v 1.5 2004/10/26 19:00:52 gawrilow Exp $

package Visual::Color;

# graph edges: black
custom $graph="0 0 0";

require VisualGraph;

###############################################################################
#
# Home-maid 3-d spring embedder with variations
#
package Visual::GraphEmbedding;

# Tuning parameters for the graph embedder.
# Almost all are multiplicative factors; the comments describe
# the effect of INCREASING of the corresponding parameter.
custom %graph_parameters=(
# scale => 1,           # the average edge length increases	
  inertion => 0.1,      # the computations might converge more slowly, but more steadily
# viscosity => 1,       # improves the convergence at the risk of being stuck at a bad local minimum
# 'max-iterations' => 10000,  # The iteration limit. Not multiplicative!
  balance => 10,        # the non-neighbor nodes are more spread away from each other
);

# Additional tuning parameters for the interactive spring embedder.
custom %interactive_parameters=(
  'eps' => 1e-2         # the movement to be accepted as "no movement"
);

@ISA=qw( Visual::Embedding );

sub compute {
   my $pkg=ref($_[0]);
   $_[0]->unbless;		# switch off array overloading!
   my ($obj, $graph_section) = splice @{$_[0]}, 0, 2;
   my %params=(%Visual::GraphEmbedding::graph_parameters, @{$_[0]});
   client("spring_embedder", $obj, $graph_section, $_[0],
	  map { ("-$_", (defined($params{$_}) ? ($params{$_}) : ())) } keys %params);
   $_[0];
}

sub get_n_vertices {
   my ($self)=@_;
   my ($obj, $graph)=@$self[0,1];	# see the command line in compute()
   if (is_object($graph) and my $meth=$graph->can("get_number_nodes")) {
      &$meth($graph);
   } elsif (is_array($graph) || !ref($graph) && defined ($graph=$obj->lookup($graph))) {
      scalar @$graph;
   } else {
      0;
   }
}

package Visual::LazyGraphEmbedding;
@ISA=qw( Visual::GraphEmbedding Visual::LazyEmbedding );

sub unbless {
   bless $_[0], "Visual::GraphEmbedding";
}

package Visual::HDEmbedding;
@ISA=qw( Visual::Embedding );

sub compute {
   my $pkg=ref($_[0]);
   $_[0]->unbless;		# switch off array overloading!
   my ($obj, $lattice_section) = splice @{$_[0]}, 0, 2;
   client("hd_embedder", $obj, $lattice_section, $_[0], @{$_[0]});
   $_[0];
}

package Visual::LazyHDEmbedding;
@ISA=qw( Visual::HDEmbedding Visual::LazyEmbedding );

sub unbless {
   bless $_[0], "Visual::HDEmbedding";
}


package default;

# Object, SECTION, optional parameters ... => Visual::LazyGraphEmbedding
function spring_embedding_3d {
   new Visual::LazyGraphEmbedding(@_);
}

# Object, SECTION, optional parameters ... => Visual::LazyHDEmbedding
function hd_embedding {
   new Visual::LazyHDEmbedding(@_);
}

function detect_dynamic(Visual::GraphEmbedding) {
   $_[0]->unbless;
}

function dim(Visual::GraphEmbedding) { 3 }

function dim(Visual::HDEmbedding) { 2 }


# Local Variables:
# mode: perl
# c-basic-offset:3
# End:
