#  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::Color;

# default color for the finite [[POINTS]] of a tropical polytope
custom $polytopegenerators = "255 255 0";

package Visual::TropicalPolytope;

use Polymake::Struct (
   [ '@ISA' => 'Container' ],
   [ '$Polytope' => '#%' ],
);

options %decorations = (
   %Visual::Polygons::decorations,

   # Flexible<Int> The visualization is always in affine coordinates. This chooses the chart.
   Chart => 0,

   # [complete color] Flexible<Color> color of the finite [[POINTS]] of a polytope
   GeneratorColor => $Visual::Color::polytopegenerators,

   # [complete color] Flexible<Color> color of the finite [[PSEUDOVERTICES]] of a polytope
   PseudovertexColor => $Visual::Color::vertices,
   
   # String Labels of [[PSEUDOVERTICES]] of a polytope
   PseudovertexLabels => 'hidden',        
   
   # Flexible<Float> Thickness of [[PSEUDOVERTICES]] of a tropical polytope
   PseudovertexThickness => undef,
);

object Polytope {

# creates an array of colors to distinguish generators and
# pseudo-vertices of a tropical polytope
sub pseudovertex_colors($$$$) {
   my ($points, $pseudovertices, $p_color, $pv_color) = @_;
   my $indices = points_in_pseudovertices($points, $pseudovertices);
   map { $indices->contains($_) ? $pv_color : $p_color } (0..$pseudovertices->rows()-1);
}

# @category Visualization
# Visualize the subdivision of the polytope induced by [[POINTS]].
# @options %Visual::TropicalPolytope::decorations
# @return fan::Visual::PolyhedralFan

user_method VISUAL(%Visual::TropicalPolytope::decorations) {
   my ($this, $decor) = @_;

   my $chart = delete $decor->{Chart};

   my $used_pvs = new Set<Int>();
   for my $i (0 .. $this->POLYTOPE_MAXIMAL_COVECTOR_CELLS->rows()-1) {
      $used_pvs += $this->POLYTOPE_MAXIMAL_COVECTOR_CELLS->row($i);
   }

   my $vertices = new Matrix<Scalar>($this->PSEUDOVERTICES->minor($used_pvs,All));
   my $cells = $this->POLYTOPE_MAXIMAL_COVECTOR_CELLS->minor(All, $used_pvs);

   # the order is crucial here
   my @pseudovertex_colors = pseudovertex_colors($this->POINTS, $vertices, delete $decor->{PseudovertexColor}, delete $decor->{GeneratorColor});
   foreach (keys %$decor) {
      $decor->{s/Pseudovertex/Vertex/r} = delete $decor->{$_};
   }
   my @codedecor = Visual::get_code_decor_keys($decor, "Vertex");
   $decor = scalar(@codedecor) ? Visual::decor_subset($decor,$used_pvs,\@codedecor) : $decor;

   $vertices = tdehomog($vertices,$chart);

   my $p = new fan::PolyhedralComplex(VERTICES=>$vertices, MAXIMAL_POLYTOPES=>$cells);
   $p->VISUAL(VertexColor => \@pseudovertex_colors, $decor);
}


# @category Visualization
# Visualize the subdivision of the torus induced by [[POINTS]].
# @options %Visual::TropicalPolytope::decorations
# @return fan::Visual::PolyhedralFan
user_method VISUAL_SUBDIVISION(%Visual::TropicalPolytope::decorations) {
   my ($this, $decor) = @_;

   my $chart = delete $decor->{"Chart"};

   my $vertices = tdehomog($this->PSEUDOVERTICES,$chart);
   my $cells = $this->MAXIMAL_COVECTOR_CELLS;
   my @pseudovertex_colors = pseudovertex_colors($this->POINTS, $this->PSEUDOVERTICES, delete $decor->{PseudovertexColor}, delete $decor->{GeneratorColor});
   foreach (keys %$decor) {
      $decor->{s/Pseudovertex/Vertex/r} = delete $decor->{$_};
   }

   my $p = new fan::PolyhedralComplex(VERTICES=>$vertices, MAXIMAL_POLYTOPES=>$cells);
   $p->VISUAL(VertexColor => \@pseudovertex_colors, $decor);
}


# @category Visualization
# Visualize the arrangement of hyperplanes with apices in the [[POINTS]] of the tropical polytope.
# @return fan::Visual::PolyhedralFan
user_method VISUAL_HYPERPLANE_ARRANGEMENT(%Visual::Polygons::decorations) : VERTICES, PROJECTIVE_AMBIENT_DIM {
   my ($this, $decor) = @_;
   points2hypersurface($this->POINTS)->VISUAL($decor);
}
precondition : PROJECTIVE_AMBIENT_DIM { $this->PROJECTIVE_AMBIENT_DIM<=3 }

}


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