#  Copyright (c) 1997-2015
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.polymake.org
#
#  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.
#-------------------------------------------------------------------------------

object polytope::Polytope {


# @category Visualization
# Visualize the [[BOUNDED_COMPLEX.GRAPH]] of a polyhedron.
# @option Int seed random seed value for the string embedder
# @return Visual::PolytopeGraph

user_method VISUAL_BOUNDED_GRAPH(%Visual::Graph::decorations, { seed => undef }) {
   my ($this, $decor, $seed)=@_;
   my $VG=$this->BOUNDED_COMPLEX->GRAPH->VISUAL( Name => $this->name,
					exists $decor->{NodeBorderColor} ? () : (NodeColor => $Visual::Color::vertices),
					$decor, $seed );
   visualize( new Visual::PolytopeGraph( Name => "Bounded graph of " . $this->name,
					 Polytope => $this,
					 bounded => 1,
					 $VG ));
}

}
package Visual::PolytopeGraph;

# Produce an edge coloring of a bounded graph from local data in the Hasse diagram.
# @return Visual::PolytopeGraph

user_method EDGE_COLORS {
   my ($self)=@_;
   my $ts=$self->Polytope;
   my $dim = $ts->CONE_DIM-2;
   my @dim_colors=map { 
      my $red = ($dim-$_)/($dim-1);
      new RGB($red,0.0,1-$red);
   } 1..$dim;
   my @edge_colors=map { $dim_colors[$_-1] } @{$ts->BOUNDED_COMPLEX->GRAPH->EDGE_COLORS};
   $self->basis_graph->merge(EdgeColor => \@edge_colors);
   visualize($self);
}




object polytope::TightSpan{


# @category Visualization
# This is a variation of [[Polytope::VISUAL_BOUNDED_GRAPH]] for the special case of a tight span.
# The vertices are embedded according to the [[METRIC]], the others are hanged in between.
# @options %Visual::Graph::decorations
# @option Int seed random seed value for the string embedder
# @option String norm which norm to use when calculating the distances between metric vectors ("max" or "square")
# @return Visual::Graph

user_method VISUAL_TIGHT_SPAN(%Visual::Graph::decorations, { norm => enum("max=default", "square"), seed => undef }) {
   my ($this, $decor, $options)=@_;
   my $BG=$this->BOUNDED_COMPLEX->GRAPH;
   my @metric_indices=@{$this->VERTICES_IN_METRIC};
   
   my (@tentacles, @tentacle_indices);
   for (my $i=0; $i<$this->N_VERTICES; ++$i) {
      if ((my $t=$metric_indices[$i])>=0  and  $BG->ADJACENCY->degree($i) ==1) {
	 push @tentacle_indices, $i;
	 push @tentacles, $t;
      }
   }
   my ($tentacle_graph, $edge_weights)=tentacle_graph(\@tentacles, $this->METRIC);
  
   my $node_colors=sub { $metric_indices[$_[0]]>=0 ? $Visual::Color::vertices : $Visual::Color::non_tax_vertices };
   
   my $float_vertices = convert_to<Float>($this->VERTICES);
   my $Graph=new Visual::Graph($decor, Name => "Tight Span of " . $this->name,
				Graph => $BG,
				Coord => bounded_embedder($BG->ADJACENCY,$float_vertices,$this->FAR_FACE,\@tentacle_indices,spring_embedding_3d($tentacle_graph, 'edge-weights' => $edge_weights, $options)->compute, $options->{norm} eq "max"),
				NodeLabels => $this->VERTEX_LABELS,
				NodeColor => $node_colors,
			      );
   visualize($Graph);
}

# @category Visualization
# Visualize the [[Polytope::BOUNDED_COMPLEX.GRAPH|BOUNDED_COMPLEX.GRAPH]] of a tight span.
# @options %Visual::Graph::decorations
# @option Int seed random seed value for the string embedder
# @return Visual::PolytopeGraph

user_method VISUAL_BOUNDED_GRAPH(%Visual::Graph::decorations, { seed => undef }) {
   my ($this, $decor, $seed)=@_;
   my $VB=$this->SUPER::VISUAL_BOUNDED_GRAPH($decor, $seed);
   $VB->basis_graph->merge(NodeColor => sub { $this->VERTICES_IN_METRIC->[$_[0]]<0 ? $Visual::Color::non_tax_vertices : undef });
   visualize($VB);
}

}

package Visual::Color;

# Color for the vertices of a tight span not corresponding to taxa
custom $non_tax_vertices="255 255 0";


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