#  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: visual.rules 7578 2007-01-21 22:48:26Z gawrilow $


###############################################################################
#
#  some common colors
#
package Visual::Color;

# topic: customize/Visual::Color
# All colors defined here are RGB values in integer representation (0..255)
# You may use symbolic names defined in the file $RGBtxt as well.

# file with symbolic color names (as shipped with X11 system)
custom $RGBtxt_path;

CONFIGURE {
   $RGBtxt_path="";
   foreach my $dir (qw( /usr/X11 /usr/X11R6 /usr/local/X11 /usr/local/X11R6 /usr/openwin )) {
      if (-f (my $f="$dir/lib/X11/rgb.txt")) {
	 $RGBtxt_path=$f; last;
      }
   }
   warn_print("file rgb.txt not found - no symbolic color names will be allowed") unless $RGBtxt_path;
   1;  # go on compiling anyway
}

# facets in famous polymake-green
custom $facets="119 236 158";

# edges of facets and graphs: black
custom $edges="0 0 0";

# vertices: red
custom $vertices="255 0 0";

###############################################################################
package default;

require Visual;
require VisualUtilities;
require VisualPointSet;
require VisualPolygon;

# category: Visualization
# args: [ "title" ], VisualObject, ... , decorations ...
#
# Create a composite drawing of several @a VisualObject's.  Optional @a title will replace
# the default one taken from the first object.  Trailing decorations (like @see VisualAttr.VertexColor)
# are applied to all components as default values.

user_function compose($@) {
   my $title= !ref($_[0]) && shift;
   my %decor;
   for (my $i=1; $i<=$#_; ++$i) {
      if (is_keyword($_[$i])) {
	 %decor=splice @_, $i;  last;
      }
   }
   my $first;
   if (instanceof Visual::Object($_[0]) && !keys %decor) {
      $first=shift;
      $first->Title=$title if $title;
      push @{$first->attached}, @_;
   } else {
      $first=new Visual::Container(Title => ($title || "untitled"), defaults => \%decor, @_);
   }
   visualize($first);
}

function printable(Visual::Object) {
   visualize(shift); ()
}

function RGB2float {	# (R, G, B) or [ R, G, B ] or "R G B" in int representation => "R G B" in floating-point representation
   no integer;
   sprintf "%8.6f %8.6f %8.6f", map { $_/255 } (ref($_[0]) ? @{$_[0]} : @_==3 ? @_ : $_[0]=~/\d+/g)
}

function RGB2hex {	# (R, G, B) or [ R, G, B ] or "R G B" in int representation => "#RRGGBB" in X Windows-style hex representation
   sprintf "#%02X%02X%02X", (ref($_[0]) ? @{$_[0]} : @_==3 ? @_ : $_[0]=~/\d+/g);
}

function flatten_indices(Visual::Embedding) { () }

function detect_dynamic(Visual::Embedding) {
   defuse_magic($_[0]);
}

sub enforce_static_in_attached {
   foreach my $att (@{$_[0]->attached}) {
      enforce_static($att);
   }
}

sub enforce_static_coord {
   my ($vis, $attr)=@_;
   if (my $dyn=detect_dynamic($vis->$attr)) {
      $dyn->compute;
   }
   &enforce_static_in_attached;
}

function enforce_static(Visual::Object) {
   enforce_static_in_attached($_[0]);
}

function enforce_static(Visual::Container) {
   my $c=shift;
   foreach my $vis (@{$c->elements}, @{$c->attached}) {
      enforce_static($vis);
   }
}

function enforce_static(Visual::PointSet) {
   enforce_static_coord($_[0], "Vertices");
}

function enforce_static($) {
   croak( "don't know how to disable dynamic coordinates in ", (ref($_[0]) || "'$_[0]'") ) unless is_ARRAY($_[0]);
   foreach (@{$_[0]}) {
      enforce_static($_);
   }
}

# category: Visualization
# Suppress creation of dynamic (interactive) scenes.
# Currently affects graph spring embedder (@c VISUAL_GRAPH) and Schlegel diagrams of polytopes (@c SCHLEGEL).
# args: VisualObject

user_function static(Visual::Object) {
   visualize(enforce_static($_[0]));
}

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