#  Copyright (c) 1997-2018
#  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.
#-------------------------------------------------------------------------------


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

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

package Visual::Color;

custom $polytopegenerators="255 255 0"; 

options %Visual::TropicalPolytope::decorations=(
	%Visual::Polygons::decorations,
    
	# Flexible<Int> The visualization is always in affine coordinates. This chooses the chart.
	Chart => 0,
    
    # Flexible<Color> color of the finite [[POINTS]] of a polytope 
    GeneratorColor => $Visual::Color::polytopegenerators,

    # Flexible<Color> color of the finite [[PSEUDOVERTICES]] of a polytope 
    PseudovertexColor => $Visual::Color::vertices,
);

object Polytope {


# @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 = $$decor{"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);

                my @pseudovertex_colors=choose_colors($this->POINTS,$vertices,$$decor{"PseudovertexColor"},$$decor{"GeneratorColor"});

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

                delete $$decor{"GeneratorColor"};
                delete $$decor{"PseudovertexColor"};
		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 = $$decor{"Chart"};
		delete($$decor{"Chart"});

		my $vertices = tdehomog($this->PSEUDOVERTICES,$chart);
		my $cells = $this->MAXIMAL_COVECTOR_CELLS;
                my @pseudovertex_colors=choose_colors($this->POINTS,$this->PSEUDOVERTICES,$$decor{"PseudovertexColor"},$$decor{"GeneratorColor"});
                delete $$decor{"GeneratorColor"};
                delete $$decor{"PseudovertexColor"};
		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:
