#  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: javaview.rules 7572 2007-01-19 11:47:12Z thilosch $

INCLUDE
   javaview_configure

CONFIGURE : javaview_configure

use JavaView;

# Category: Visualization
# Use @see external.JavaView - default tool for the visualization of 3- or 4-dimensional geometric objects.
#
# You can also use it for graph visualization.  The 3-d spring embedder starts with random node placement, so
# you have a chance to get various embeddings when trying it several times.
label javaview

prefer javaview.geometry

INCLUDE
   javaview_interactive.rules

###########################################################################################

package JavaView;

# background color for JavaView Viewer
custom $bgColor="255 255 255";

###########################################################################################

package JavaView::Material;

# diffuse reflection of polygons
custom $diffuseColor   = "204 204 204";

#emissive color of polygons
custom $emissiveColor  = "0 0 0";

# shininess of polygons
custom $shininess      = 10.0;

# specular color of polygons
custom $specularColor  = "255 255 255";

# transparency of polygons and solids
custom $transparency   = 0.8;


###########################################################################################
#
#  Basic class implementing the required back-end interface.
#
#  The program start-up is delegated to one of two viewer implementations,
#  Static or Interactive, chosen depending on the available JavaView version.

package JavaView::Viewer;

### use the interactive viewer if available

if ($rulefiles{"javaview_interactive.rules"}) {
   @ISA=qw( JavaView::Viewer::Interactive );
} else {
   require JavaViewStaticViewer;
   @ISA=qw( JavaView::Viewer::Static );
}

###########################################################################################
#
#  Drawing methods for graphical primitives defined in this module
#

using namespaces 'Visual::Utilities';

# prepare for customizable material ...
sub material {
  return (material_diffuseColor  => $JavaView::Material::diffuseColor,
	  material_emissiveColor => $JavaView::Material::emissiveColor,
	  material_shininess     => $JavaView::Material::shininess,
	  material_specularColor => $JavaView::Material::specularColor);
}

global_method javaview.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 JavaView::geometry(@params));
}

global_method javaview.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=(parse_color($P->FacetColor))[1])) {
      push @params, faces_color => $color;
   }

   if (@{$P->Vertices} >= 3) {
      push @params, facet_style($P);
      $self->append(new JavaView::solid(@params));

   } else {
      for (my $i=0; $i<$#params; $i+=2) {
	 $params[$i] =~ s/^face/line/;
      }
      $self->append(new JavaView::wire(@params));
   }
}

global_method javaview.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"), 
		  facet_style($P),
		  edges($P),
		  visible($P),
		);

   $self->append(new JavaView::solid(@params));
}

###########################################################################################
#
#  Direct writing to a JVX file without starting a GUI
#

package JavaView::File::Writer;
import Visual::FileWriter(suffix => ".jvx");

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

sub DESTROY { }

package default;

# Category: Visualization
# Call @see external.JavaView with the given visual objects.
# args: VisualObject, ...
# option: File => "filename" | "AUTO"
#         Store the object in a @c JVX file instead of starting the interactive GUI.
#         The @c .jvx 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 javaview(Visual::Object+, { File => undef }) {
   visualize_explicit(@_, "JavaView");
}


# Local Variables:
# mode: perl
# End:
