#  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: povray.rules,v 1.23 2004/11/05 21:50:51 gawrilow Exp $


INCLUDE
   polytope_properties.rules
   visual_geometry.rules


HELP $this/preferences/Visualization/povray
Generate a scene file for @see external.povray describing a 3- or 4-dimensional polytope as a wire model.


VISUAL : VERTICES, FACETS, GRAPH
LABEL povray.visual_geometry
PRECONDITION: AMBIENT_DIM, BOUNDED
   $BOUNDED && $AMBIENT_DIM<=3
BODY:
   my $filename=$this->name.".pov";
   if (open P, ">$filename") {
      warn_print( "writing output to file: $filename" );
   } else {
      die "can't create output file $filename: $!";
   }
   my @v=$this->dehomogenize($VERTICES);
   my @f=convert_to_float($FACETS);
   my ($nv,$nf)=($#v+1,$#f+1);
   my $ne=$nv+$nf-2; # Euler's formula allows to compute the number of edges
   my ($i,$objects,$max_norm_2)=(0,0,0);
   print P '#include "polymake-scene.pov"', "\n\n",
           '#declare Polytope_edges =', "\n",
           'object {', "\n";
   foreach my $neighbors (@$GRAPH) {
      my ($x,$y,$z) = split /\s+/,$v[$i];
      {  no integer;
	 my $norm_2=$x*$x+$y*$y+$z*$z;
	 $max_norm_2=$norm_2 if ($norm_2>$max_norm_2);
      }
      my $this_vertex="$x,$y,$z";
      $objects++;
      print P "  union { sphere { <$this_vertex>, thick }\n";
      $neighbors =~ /\{\s*(.*)\}/;
      foreach my $n (split /\s+/, $1) {
	 if ($n<$i) {
	    $_=$v[$n]; s/ +/,/g; chomp; my $this_neighbor=$_;
	    $objects++;
	    if ($objects<$nv+$ne) {
	       print P "  union { ";
	    } else { # for the last edge we do not need a new "union"
	       print P "          ";
	    }
	    print P "cylinder { <$this_vertex> <$this_neighbor> thick }\n";
	 } else {
	    last;
	 }
      }
      $i++;
   }
   my $scaling=do {
      no integer;
      1/sqrt($max_norm_2);
   };
   print P "  ", "}" x $objects, "\n\n",
           "object {\n",
           "  Polytope_edges\n",
           "  texture { T_Polytope_wires }\n",
           "  scale $scaling\n}\n";
   close P;
   1


INCLUDE
   povray_schlegel.rules


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