#  Copyright (c) 1997-2010
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.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: tight_span.rules 9535 2010-03-05 17:13:17Z benmuell $

# Bounded subcomplex of an unbounded polyhedron, which is associated with a finite metric space.
# The tight span is 1-dimensional if and only if the metric is tree-like.  In this sense, the tight
# span captures the deviation of the metric from a tree-like one.

declare object TightSpan<Scalar=Rational> : Polytope<Scalar> {


# @category Phylogenetic Analysis
# Finite metric space encoded as a (symmetric) distance matrix.

property METRIC : Matrix<Scalar>;


# @category Phylogenetic Analysis
# Coherent splits.

property COHERENT_SPLITS : Array< Pair<Set,Set> >;


# @category Phylogenetic Analysis
# Labels for the rows and columns of the [[METRIC]] space.
# Default TAXA are just consecutive numbers.

property TAXA : Array<String>;


# @category Phylogenetic Analysis
# Row indices in the [[METRIC]] matrix or -1
property VERTICES_IN_METRIC : Array<Int>;

# @notest  Rule defined "in stock" - currently without use
rule VERTICES_IN_METRIC : VertexPerm.VERTICES_IN_METRIC, VertexPerm.PERMUTATION {
   $this->VERTICES_IN_METRIC=permuted($this->VertexPerm->VERTICES_IN_METRIC, $this->VertexPerm->PERMUTATION);
}
weight 1.10;

rule INEQUALITIES : METRIC {
   $this->INEQUALITIES = metric2poly($this->METRIC);
}

rule COHERENT_SPLITS : METRIC {
   $this->COHERENT_SPLITS = metric2splits($this->METRIC);
}

rule TAXA : METRIC {
   $this->TAXA=[ 0 .. $this->METRIC->rows-1 ];
}
weight 0.10;

rule VERTICES_IN_METRIC : METRIC, VERTICES {
   $this->VERTICES_IN_METRIC = vertices_in_metric($this->VERTICES,$this->METRIC);
}
weight 1.10;

rule VERTEX_LABELS : TAXA, VERTICES_IN_METRIC {
   $this->VERTEX_LABELS=[ map { $_>=0 ? $this->TAXA->[$_] : " " } @{$this->VERTICES_IN_METRIC} ];
}
weight 0.10;


rule BOUNDED_GRAPH.EDGE_COLORS : BOUNDED_GRAPH.ADJACENCY, HASSE_DIAGRAM, FAR_FACE, DIM {
   edge_colored_bounded_graph($this->BOUNDED_GRAPH,$this->HASSE_DIAGRAM, $this->FAR_FACE, $this->DIM/2)
}
weight 4.10;


# @category Visualization
# This is a variation of [[Polytope::VISUAL_BOUNDED_GRAPH]] for the special case of a tight span.
# The tropical vertices are embedded according to the [[METRIC]], the others are hanged in between.
# @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_GRAPH->ADJACENCY;
   my $n_nodes = 10;
   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->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,
				Adjacency => $BG,
				Coord => bounded_embedder($BG,$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_GRAPH|BOUNDED_GRAPH]] of a tight span.
# @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:
# c-basic-offset:3
# mode: perl
# End:
