#  Copyright (c) 1997-2004
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de
#
#  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.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: Geomview.pm,v 1.5 2004/10/26 19:00:06 gawrilow Exp $

use JavaView;	# we reuse some routines from there

package Geomview::File;
use Struct (
   [ new => '$' ],
   [ '$workfile' => '#1' ],
   '@geom',
);

my $setup=<<'.';
(bbox-draw "world" no)
.

sub print_it {
   my ($self)=@_;
   open my $file, ">", $self->workfile
      or die ref($self), "::print_it: could not write file ", $self->workfile, ": $!\n";
   print $file $setup, map { $_->toString } @{$self->geom};
   close $file;
}

package Geomview::geom;
@ISA=qw( JavaView::File );

sub header {
   my ($self)=@_;
   my $id=$self->{TITLE};
   $id =~ s/\s/_/g;
   "(geometry $id { LIST\n";
}

sub trailer {
   "} # end LIST\n)\n";
}

package Geomview::element;

sub new {
   my $class=shift;
   my $self=bless { @_ }, $class;
   $self;
}

sub toString {
   my ($self)=@_;
   "{" .
   ( defined($self->{appearance}) && " appearance { $self->{appearance} }\n" ) .
   $self->contents .
   "}\n";
}

sub points {
   my ($self)=@_;
   join("", @{$self->{points}});
}

sub colored_points {
   my ($self)=@_;
   my ($p, $c)=@$self{qw( points pointColors )};
   is_array($c)
   ? join("", map { chomp $p->[$_]; "$p->[$_] $c->[$_]\n" } 0..$#$p)
   : join("", map { chomp $p->[$_]; "$p->[$_] $c\n" } 0..$#$p)
}

sub lines {
   my ($self, $key)=@_;
   join("", map { scalar(@$_) . " @$_\n" } @{$self->{$key}})
}

sub colored_lines {
   my ($self, $key, $color_key)=@_;
   my ($f, $c)=@$self{$key, $color_key};
   is_array($c)
   ? join("", map { scalar(@{$f->[$_]}) . " @{$f->[$_]} $c->[$_]\n" } 0..$#$f)
   : join("", map { scalar(@{$f->[$_]}) . " @{$f->[$_]} $c\n" } 0..$#$f)
}

package Geomview::OFF;
@ISA=qw( Geomview::element );

sub dim {
   my ($self)=@_;
   ( $self->{dim}==2 ? "nOFF 2\n" : "OFF\n" ) .
   scalar(@{$self->{points}}) . " " . scalar(@{$self->{facets}}) . " 0\n"
}

sub contents {
   my ($self)=@_;
   ( defined $self->{pointColors}
     ? "C" . $self->dim . $self->colored_points
     : $self->dim . $self->points ) .
   ( defined $self->{facetColors}
     ? $self->colored_lines('facets', 'facetColors')
     : $self->lines('facets') )
}

package Geomview::SKEL;
@ISA=qw( Geomview::element );

sub dim {
   my ($self)=@_;
   ( $self->{dim}==2 ? "nSKEL 2\n" : "SKEL\n" ) .
   scalar(@{$self->{points}}) . " " . scalar(@{$self->{lines}}) . "\n"
}

sub contents {
   my ($self)=@_;
   $self->dim . $self->points .
   ( defined $self->{lineColors}
     ? $self->colored_lines('lines', 'lineColors')
     : $self->lines('lines') )
}

###########################################################################################
#
#  Implementation of the Viewer interface
#
package Geomview::Viewer;

use BackgroundViewer;
use Struct [ '@ISA' => 'BackgroundViewer' ];

sub new {
   my $self=&_new;
   $self->graphics=new Geomview::File($self->tempfile.".gcl");
   $self;
}

sub command {
   my $self=shift;
   "$geomview " . $self->graphics->workfile;
}

sub new_drawing {
   my $self=(shift)->active;
   push @{$self->graphics->geom}, new Geomview::geom(@_ ? (TITLE => shift) : ());
   $self;
}

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

1

# Local Variables:
# c-basic-offset:3
# End:
