#  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.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: splitstree.rules 7533 2006-12-20 23:55:58Z gawrilow $

###########################################################################################
#
#  Object type to visualize with SplitsTree
#

package Visual::FiniteMetricSpace;

use Struct (
   [ '@ISA' => 'Visual::Object' ],
   '$metric',
   [ '$taxa' => 'unify_labels(#%)', default => 'undef' ],
);

object TightSpan;

# category: Visualization
# Visualize the splits of a finite metric space (that is, a planar image of a tight span).  Calls SplitsTree.
# return: Visual::FiniteMetricSpace

user_method VISUAL_SPLITS () {
   my $this=shift;
   my $M= new Visual::FiniteMetricSpace(Name => $this->name,
					metric => $this->METRIC,
					taxa => $this->TAXA);
   visualize($M);
}


package SplitsTree::Viewer;

# path to SplitsTree
custom $splitstree;

CONFIGURE {
   $splitstree=find_via_path("SplitsTree")
   or die "SplitsTree package not found\n";
}

###########################################################################################
#
#  Static (file-based) implementation of the Viewer interface
#

use SplitsTree;
use BackgroundViewer;

use Struct [ '@ISA' => 'Poly::SimpleViewer' ];

sub new_drawing {
   my ($self, $title)=@_;
   $self->new_instance;
   $self->graphics=new SplitsTree::File($self->tempfile.".nex", $title);
   $self;
}

sub command {
   $splitstree . " -i " .  shift->graphics->workfile;
}

# category: Visualization
# Use @see external.SplitsTree to show planar images of tight spans.
label splitstree

global_method splitstree.tight_span: draw(Visual::FiniteMetricSpace) {
   my ($self, $M)=@_;
   $self->graphics->metric=$M->metric;
   $self->graphics->taxa=$M->taxa;
}


package SplitsTree::File::Writer;
import Visual::FileWriter(suffix => ".nex");

sub new_drawing {
   my ($self, $title)=@_;
   $self->title=$title;
   $self;
}


package default;

# Category: Visualization
# Call @see external.SplitsTree with the given visual objects.
# args: VisualObject, ...
# option: File => "filename" | "AUTO"
#	  Only create a NEXUS format file, don't start the GUI.
#
#	  The @c .nex suffix is automatically added to the file name.
#
#	  Specify @a AUTO if you want the filename be automatically derived from the drawing title.
#
#         You can also use any expression allowed for the perl @c open function,
#         like "-" for terminal output, "&HANDLE" for an already opened file handle,
#         or "| program" for a pipe.

user_function splitstree(Visual::Object+, { File => undef }) {
   visualize_explicit(@_, "SplitsTree");
}


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