#  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.
#-------------------------------------------------------------------------------

REQUIRE
  pdfviewer.rules

package Sketch;

CREDIT sketch
  Free Graphics Software for the TeX, LaTeX, and PSTricks Community
  http://www.frontiernet.net/~eugene.ressler/

# sketch executable
custom $sketch;

CONFIGURE_OPT {
   find_program($sketch, qw( sketch ), { prompt => "the Sketch executable" });
}

package Sketch::default;

# coordinates of the viewpoint
custom @view_point=(10,11,9);

# coordinates of a point on the view axis: default is the origin
custom @view_direction=(0,0,0);

# camera orientation: default is y-axis towards zenith
custom @view_up=(0,1,0);

# scaling factor
custom $scale=2;


package Sketch::Viewer;

use Sketch;
use BackgroundViewer;
use Visual::Transformation;

# The default behavior is to have Sketch produce LaTeX output, then call PDFLaTeX
# and finally a PDF viewer.

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

sub file_suffix { ".sk" }

sub new {
   unless (defined($sketch) && defined($Visual::pdflatex) && defined($Visual::pdfviewer)) {
      die <<'.';
requires Sketch executable and PDFLaTeX and PDF viewer;
please specify the output File option or call reconfigure("common::sketch.rules", "common::pdfviewer.rules");
.
   }
   &_new;
}

sub command {
   my ($self, $filename)=@_;
   my $latextemplate = $self->tempfile.".tex";
   open my $fh, ">", $latextemplate;
   print $fh $self->write_latextemplate;
   
   my $pdfout = $self->tempfile.".pdf";
   my $pdfout_dir = $self->tempfile->dirname;
   my $pdfout_name = $self->tempfile->basename;
   "$sketch -t $latextemplate $filename 2>/dev/null | $Visual::pdflatex --output-directory=$pdfout_dir --jobname=$pdfout_name 1>/dev/null ; $Visual::pdfviewer $pdfout 1>/dev/null 2>/dev/null";
}

sub new_drawing {
   my ($self, $title, $vis)=@_;
   $self->SUPER::new_drawing($title);
   if (defined (my $rep=$vis->representative)) {
      $self->graphics->transform=Visual::Transformation->get_transformation_matrix($rep);
   }
   $self;
}

sub append {
   my $self=shift;
   $self->graphics->append(@_);
}

sub write_latextemplate {
my $text = <<'|';
%
%---------------------------------------------------------------------------------
%
% Polymake LaTeX template for Sketch-Output
% Copyright (c) 1997-2018
% Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
% http://www.polymake.org
\documentclass[12pt]{article}
\usepackage[x11names,rgb]{xcolor}
\usepackage{tikz}
\usepackage{ifthen}
\usepackage{amsmath}

\usetikzlibrary{snakes}
\usetikzlibrary{arrows}
\usetikzlibrary{shapes}
\usetikzlibrary{backgrounds}

\pagestyle{empty}

\usepackage[papersize={640pt, 480pt}, margin=0pt, marginratio=1:1]{geometry}

\newsavebox{\sketchoutput}
\savebox{\sketchoutput}{
%%SKETCH_OUTPUT%%
}
\newlength{\scaledwidth}
\settowidth{\scaledwidth}{\resizebox{!}{400pt}{\usebox{\sketchoutput}}}

\begin{document}
|
#\rlap{\smash{\raisebox{-2.5\baselineskip}{\includegraphics[height=3\baselineskip]{%
#  $text .= "$Polymake::Resources/logo/png/logo.png";
#}} }}
#\vspace*{37.5pt}
$text .=<<'|';
\centering
\ifthenelse{\lengthtest{\scaledwidth < 500pt}}{
  \resizebox{!}{400pt}{\usebox{\sketchoutput}}
}{
  \resizebox{500pt}{!}{\usebox{\sketchoutput}}
}
\end{document}
|
return $text;
}


# @category Visualization
# Create a Sketch drawing, write it to a file suitable for TikZ output.
label sketch

# The following are rarely used.
# Much more common are the methods in apps/{graph,polytope,...}/rules/sketch.rules

global_method sketch.geometry: draw(Visual::PointSet, @) {
   my ($self, $PointSet)=@_;
   $self->append(new Sketch::PointSet($PointSet));
}

global_method sketch.geometry: draw(Visual::Wire, @) {
   my ($self, $G)=@_;
   $self->append(new Sketch::Wire($G));
}

global_method sketch.geometry: draw(Visual::Polygon, @) {
   my ($self, $P)=@_;
   $self->append(new Sketch::Solid($P));
}

global_method sketch.geometry: draw(Visual::Polygons, @) {
   my ($self, $P)=@_;
   $self->append(new Sketch::Solid($P));
}


package Sketch::File::Writer;
import Visual::FileWriter;

sub file_suffix { ".sk" }

sub new_drawing {
   my ($self, $title, $vis)=@_;
   if (defined (my $rep=$vis->representative)) {
      $self->graphics->transform=Visual::Transformation->get_transformation_matrix($rep);
   }
   $self->title=$title;
   $self;
}

sub DESTROY { }


package application;

# @category Visualization
# Produce a Sketch input file with given visual objects.
# @param Visual::Object vis_obj ... objects to display
#
# @option String File "filename" or "AUTO"
#
#  For the file name you can use any expression allowed for the ''open'' function,
#  including "-" for terminal output, "&HANDLE" for an already opened file handle,
#  or "| program" for a pipe.  Real file names are automatically completed with the ''.sk'' suffix if needed.
#
#  An automatically generated file name is displayed in the verbose mode.

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


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