#  Copyright (c) 1997-2007
#  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: jreality.rules 7550 2007-01-10 12:25:54Z gawrilow $

INCLUDE 
  jreality_configure

CONFIGURE : jreality_configure

# Category: Visualization
# Use jreality, the experimental 3-D visualization software
label jreality

require JRealityViewer;

package JReality::Viewer;

using namespaces 'Visual::Utilities';

global_method jreality.geometry: draw(Visual::PointSet, @) {
  my ($self, $P) = @_;
  my @params = (   name => $P->Name,
		   points => $P->Points,
		   pointSet_dim => $P->Dim,
		   vertex_labels($P),
		   vertex_colors($P),
		   vertex_thicknesses($P),
		   visible($P),
	       );
  $self->append(new JReality::geometry(@params));
}

global_method jreality.geometry: draw(Visual::Polygon, @) {
  my ($self, $P)=@_;
  my $face=$P->Facet;
  if ($face) {
    $face =~ s/\{\s*(.*)\s*\}/$1/;
  } else {
    $face=join(" ", 0..$#{$P->Vertices});
  }
  
  my @params = ( name => $P->Name,
		 points => $P->Vertices,
		 pointSet_dim => $P->Dim,
		 faces => [ $face ],
		 faceSet_backface_flag => "show",
		 vertex_labels($P),
		 vertex_colors($P),
		 vertex_thicknesses($P),
		 edges($P),
		 visible($P),
	       );
  
  if (defined (my $color=(Visual::parse_color($P->FacetColor))[1])) {
    push @params, faces_color => $color;
  }
  
  if ($#{$P->Vertices} >= 2) {
    if (defined (my $style=$P->FacetStyle)) {
      if ($style =~ $Visual::hidden_re) {	
	push @params, faceSet_face_flag => "hide",
	  faceSet_backface_flag => "show";
      } elsif($style =~ $Visual::transparency_re) {
	push @params, material_flag => "show",
	  material_transparency => $1;
      }
    }
    $self->append(new JReality::solid(@params));
    
  } else {
    for (my $i=0; $i<$#params; $i+=2) {
      $params[$i] =~ s/^face/line/;
    }
    $self->append(new JReality::wire(@params));
  }
}

global_method jreality.geometry: draw(Visual::Polygons, @) {
   my ($self, $P)=@_;
   my @params = ( name => $P->Name,
		  points => $P->Vertices,
		  pointSet_dim => $P->Dim,
		  faces => [ map { $_->Facet =~ /\{(.*)\}/ ? $1 : $_->Facet } @{$P->Polygons} ],
		  faceSet_backface_flag => "show",
		  vertex_labels($P), 
		  vertex_colors($P),
		  vertex_thicknesses($P),
		  facet_colors($P, scalar @{$P->Polygons}, "FacetColor"), 
		  edges($P),
		  visible($P)
		);
  $self->append(new JReality::solid(@params));
}


###########################################################################################
#
#  Direct writing to a bsh file without starting a GUI. The bsh file contains beanshell
#  code to be read by jreality.
#

package JReality::File::Writer;
import Visual::FileWriter(suffix => ".bsh");

sub new_drawing {
   my ($self, $title)=@_;
   $self->title=$title;
   $self;
}

sub DESTROY { }

package default;

# Category: Visualization
# Use jReality to display given visual objects.
# args: VisualObject, ...
# option: File => "filename" | "AUTO"
#         Store the object in a @c BSH file instead of starting the interactive GUI.
#         The @c .bsh suffix is automatically added to the file name.
#
#         Specify @a AUTO if you want the filename be automatically derived from the drawing title.
#
#         You can also use any expression allowed for the perl @c open function,
#         like "-" for terminal output, "&HANDLE" for an already opened file handle,
#         or "| program" for a pipe.

user_function jreality(Visual::Object+, { File => undef }) {
   my $opts=pop @_;
   # FIXME: suppress interactive dragging etc.
   enforce_static($_) for @_;
   visualize_explicit(@_, $opts, "JReality");
}


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