#  Copyright (c) 1997-2021
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://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.
#-------------------------------------------------------------------------------

package Visual::TVD;

# cutoff factor for visualizing a tropical Voronoi diagram
custom $cutoff=new Rational(20);


# Voronoi diagram with respect to the tropical metric in the tropical projective torus.
# Its combinatorics is controlled by a [[POLYTROPE_PARTITION]].
# See P. Criado, M. Joswig, P. Santos: Tropical bisectors and Voronoi diagrams, arXiv:1906.10950
#
# @example The following computes a tropical Voronoi diagram of three [[SITES]] in the tropical 3-torus.
# > $T= new VoronoiDiagram(SITES=>[[-4,-4,0,0],[-3,0,2,0],[-2,-5,-2,0]]);
# > print $T->POLYTROPE_PARTITION->size();
# | 134
declare object VoronoiDiagram {

  file_suffix tvd

  # The sites of the tropical Voronoi diagram.
  property SITES : Matrix<Rational>;

  # Number of sites of the diagram.
  property N_SITES : Int;

  rule N_SITES : SITES {
    $this->N_SITES= $this->SITES->rows;
  }

  # Number of dimensions of the diagram. One less than the number of coordinates.
  property AMBIENT_DIM : Int;

  rule AMBIENT_DIM : SITES {
    $this->AMBIENT_DIM= $this->SITES->cols-1;
  }

  # Representation of the tropical Voronoi diagram.
  # Each such polyhedron is a domain in which the distance to the set of sites $S$ is a minimum of linear functions.
  # This list of regions is represented as an array of pairs of matrices.
  # The first matrix in each pair represents the region itself (a polytrope) as a shortest path matrix.
  # The second matrix (the labels) gives the index of the site $s\in S$ with maximum $s_j-s_i$ such that the cone $\{x:x_i-s_i<= x_k-s_k <= x_j-s_j \forall k\in [d+1]\}$ intersects this cell (or $-1$ if no such index exists).
  # Then, in this region, $dist(x,S)$ is a minimum of the linear functions $(x_j-s_j)-(x_i-s_i)$ for each $s$ labelled with $(i,j)$.
  # @example Here is one polytrope cell.
  # > $T= new VoronoiDiagram(SITES=>[[-4,-4,0,0],[-3,0,2,0],[-2,-5,-2,0]]);
  # > print $T->POLYTROPE_PARTITION->[0];
  # | <0 inf inf inf
  # | -4 0 2 0
  # | -5 inf 0 inf
  # | -4 inf inf 0
  # | >
  # | <-1 1 -1 -1
  # | -1 -1 -1 -1
  # | -1 -1 -1 -1
  # | -1 -1 -1 -1
  # | >
  property POLYTROPE_PARTITION : Array<Pair<Matrix<Rational>, Matrix<Int>>>;

  rule POLYTROPE_PARTITION : SITES {
      compute_polytrope_partition($this);
  }

  user_method VISUAL : AMBIENT_DIM, SITES, POLYTROPE_PARTITION {
      my $this= shift;
      
      my @result = visualizable_cells($this->SITES, $this->AMBIENT_DIM, $this->POLYTROPE_PARTITION, $Visual::TVD::cutoff);
      compose(map {$_->VISUAL} @result);
  }
}

# Local Variables:
# mode: perl
# cperl-indent-level: 3
# indent-tabs-mode:nil
# End:
