#  Copyright (c) 1997-2006
#  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 7161 2006-03-07 17:50:53Z gawrilow $

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;
use Struct (
   [ '@ISA' => 'JavaView::File' ],
   [ new => '$' ],
   [ '$title' => '#1' ],
   [ '$workfile' => 'undef' ]
);

sub header {
   my ($self)=@_;
   my $id=$self->title;
   if (length($id)) {
      $id =~ s/\s/_/g;
   } else {
      $id="unnamed";
   }
   "(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') )
}

1

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