#  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: VisualUtilities.pm,v 1.3 2006/03/07 17:50:53 gawrilow Exp $

###############################################################################
#
#  Visual::Utilities - a collection of visualization utilities
#

package Visual::Utilities;

sub colors {
   my ($P, $items, $color_name, $default_color, $key)=@_;
   my @params;
   if (defined (my $get_color=$P->$color_name)) {
      if (is_code($get_color)) {
	 my @colors;
	 if (is_numeric($items)) {
	    @colors=map { (parse_color($get_color->($_)))[1] || $default_color } 0..($items-1)
	 } else {
	    while ($items) {
	       push @colors, (parse_color($get_color->($items)))[1] || $default_color;
	       ++$items;
	    }
	 }
	 push @params, "${key}Set_color_flag" => "show",
	               "${key}s_colors_list" => \@colors;
      } elsif (my @color=parse_color($get_color)) {
	 push @params, "${key}s_color" => $color[1];
      }
   }

   $color_name =~ /Color$/;
   my $style_name="$`Style";
   my $get_style=$P->$style_name;
   if (!is_code($get_style)  &&  $get_style =~ $Visual::hidden_re) {
      push @params, "${key}Set_${key}_flag" => "hide";
      if ($style_name =~ /^Face/) {
	 push @params, faceSet_backface_flag => "show";
      }
   } elsif (!@params && defined($default_color)) {
      push @params, "${key}s_color" => $default_color;
   }
   return @params;
}

sub thicknesses {
   my ($P, $items, $style_name, $default_thickness, $key)=@_;
   if (defined (my $get_style=$P->$style_name)) {
      if (is_code($get_style)) {
	 my ($seen, @th);
	 if (is_numeric($items)) {
	    @th=map { $get_style->($_) =~ $Visual::thickness_re ? do { $seen=1; $1 } : $default_thickness } 0..($items-1);
	 } else {
	    while ($items) {
	       push @th, $get_style->($items) =~ $Visual::thickness_re ? do { $seen=1; $1 } : $default_thickness;
	       ++$items;
	    }
	 }
	 if ($seen) {
	    return ( "${key}Set_thicknesses_flag" => "show",
		     "${key}Set_thicknesses_list" => \@th );
	 }
      } elsif ($get_style =~ $Visual::thickness_re) {
	 return ( "${key}s_thickness" => $1 );
      }
   }
   return ();
}

sub labels {
   my ($P, $items, $label_name, $key)=@_;
   if (defined (my $labels=$P->$label_name)) {
      if (is_array($labels)) {
	 return "${key}s_labels_list" => $labels;
      } elsif (is_code($labels)) {
	 return "${key}s_labels_list" => [ map { $labels->($_) } 0..($items-1) ];
      } elsif ($labels eq "hidden") {
	 return "${key}s_labels_list" => [ (" ") x $items ];
      }
   }
   return ();
}

sub visible {
   my ($P)=@_;
   return $P->Hidden
          ? (visibleFlag => "hide")
          : ();
}

sub vertex_colors {
   if (@_==1) {
      push @_, scalar @{$_[0]->Vertices}, "VertexColor";
   }
   push @_, $Visual::Color::vertices, "point";
   &colors;
}

sub vertex_thicknesses {
   if (@_==1) {
      push @_, scalar @{$_[0]->Vertices}, "VertexStyle";
   }
   push @_, $JavaView::default::points_thickness, "point";
   &thicknesses;
}


sub vertex_labels {
   if (@_==1) {
      push @_, scalar @{$_[0]->Vertices}, "VertexLabels";
   }
   push @_, "point";
   &labels;
}

sub edges {
   my ($P, $color_name)=@_;
   $color_name ||= "EdgeColor";
   my @params;
   if (defined (my $color=(parse_color($P->$color_name))[1])) {
      push @params, edges_color => $color;
   }
   $color_name =~ /Color$/;
   my $style_name="$`Style";
   if (defined (my $style=$P->$style_name)) {
      if ($style =~ $Visual::hidden_re) {
	 push @params, faceSet_edge_flag => 'hide';
      } elsif ($style =~ $Visual::thickness_re) {
	 push @params, edges_thickness => $1;
      }
   }
   return @params;
}

sub facet_colors {
   if (@_==1) {
      push @_, scalar @{$_[0]->Facets}, "FacetColor";
   }
   push @_, $Visual::Color::facets, "face";
   &colors;
}

sub facet_style {
  my ($P) = @_;
  my @params;
  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;
    }
  }
  return @params;
}

1

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