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

# Visualize the [[LATTICE_POINTS]] of a polytope
# @options %Visual::PointSet::decorations
# @return Visual::Polytope
# @example Visualizes the lattice points of the threedimensional cube.
# > cube(3)->VISUAL->LATTICE;

user_method LATTICE(%Visual::PointSet::decorations) {
    my ($self, $decor)=@_;
    my $VP=$self->basis_solid;
    $VP->VertexStyle //= "hidden";
    my $P=$self->Polytope;
    if ($P->CONE_DIM > 2) {
        $VP->FacetStyle //= "hidden";
    }
    my $vert = $P->VERTICES;
    my $lp = $P->LATTICE_POINTS;
    my $nlv = new Matrix<Float>(map { convert_to<Float>($_) } grep {!is_integral($_)} @$vert);
    my $points = new Matrix<Float>(0,$P->CONE_DIM);
    $points /= convert_to<Float>($P->LATTICE_POINTS) if ($P->N_LATTICE_POINTS);
    $points /= $nlv if ($nlv->rows);
    push @{$self->elements} , new Visual::PointSet(
                                 Name => "Lattice points and vertices of ". $P->name,
                                 Points => dehomogenize($points),
                                 PointLabels => "hidden",
                                 PointColor => sub { $_[0] < $P->N_LATTICE_POINTS ? $Visual::Color::LatticePointColor :
                                                     $Visual::Color::vertices },
                                 $decor,
                                );
    visualize($self);
}

# Visualize the [[LATTICE_POINTS]] of a polytope in different colors (interior / boundary / vertices)
# @options %Visual::PointSet::decorations
# @return Visual::Polytope
# @example Creates the threedimensional unit cube scaled by 1.5 and displays the colored version of its lattice points
# > cube(3,(3/2),0)->VISUAL->LATTICE_COLORED;

user_method LATTICE_COLORED(%Visual::PointSet::decorations) {
    my ($self, $decor)=@_;
    my $VP=$self->basis_solid;
    $VP->VertexStyle //= "hidden";
    my $P=$self->Polytope;
    if ($P->CONE_DIM > 2) {
        $VP->FacetStyle //= "hidden";
    }
    $P->provide("LATTICE_POINTS_GENERATORS");
    my $vert = $P->VERTICES;
    my $nlv = new Matrix<Float>(map { convert_to<Float>($_) } grep {!is_integral($_)} @$vert);
    my $points = new Matrix<Float>(0, $P->CONE_DIM);
    $points /= convert_to<Float>($P->INTERIOR_LATTICE_POINTS) if ($P->N_INTERIOR_LATTICE_POINTS);
    $points /= convert_to<Float>($P->BOUNDARY_LATTICE_POINTS) if ($P->N_BOUNDARY_LATTICE_POINTS);
    $points /= $nlv if ($nlv->rows);
    push @{$self->elements} , new Visual::PointSet(
                                 Name => "Lattice points and vertices of ". $P->name,
                                 Points => dehomogenize($points),
                                 PointLabels => "hidden",
                                 PointColor => sub { $_[0] < $P->N_INTERIOR_LATTICE_POINTS ? $Visual::Color::InteriorLatticePointColor :
                                                     $_[0] < $P->N_LATTICE_POINTS ? $Visual::Color::BoundaryLatticePointColor :
                                                     $Visual::Color::vertices },
                                 $decor,
                                );
    visualize($self);
}

package Visual::Color;

# Color for the visualization of interior lattice points
custom $InteriorLatticePointColor="30 250 30";

# Color for the visualization of boundary lattice points
custom $BoundaryLatticePointColor="70 150 70";

# Color for the visualization of lattice points
custom $LatticePointColor="70 150 70";


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