#  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 7540 2006-12-21 21:04:18Z gawrilow $

###############################################################################
#
#  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}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 } : $default_thickness } 0..($items-1);
	 } else {
	    while ($items) {
	       push @th, $get_style->($items) =~ $Visual::thickness_re ? do { $seen=1; $1*$default_thickness } : $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*$default_thickness );
      }
   }
   return ();
}

sub labels {
   my ($P, $label_name, $key)=@_;
   if (defined (my $labels=$P->$label_name)) {
      return ("${key}s_labels_list" => $labels);
   }
   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 @_, "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*$JavaView::default::lines_thickness;
      }
   }
   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:
