=head1 NAME

PDLA::Graphics::TriD -- PDLA 3D interface


 use PDLA::Graphics::TriD;
 # Generate a somewhat interesting sequence of points:
 $t = sequence(100)/10;
 $x = sin($t); $y = cos($t), $z = $t;
 $coords = cat($x, $y, $z)->xchg(0,1);
 my $red = cos(2*$t); my $green = sin($t); my $blue = $t;
 $colors = cat($red, $green, $blue)->xchg(0,1);
 # After each graph, let the user rotate and
 # wait for him to press 'q', then make new graph
 line3d($coords);       # $coords = (3,n,...)
 line3d($coords,$colors);  # $colors = (3,n,...)
 # Generate a somewhat interesting sequence of surfaces
 $surf1 = (rvals(100, 100) / 50)**2 + sin(xvals(100, 100) / 10);
 $surf2 = sqrt(rvals(zeroes(50,50))/2);
 $x = sin($surface); $y = cos($surface), $z = $surface;
 $coords = cat($x, $y, $z)->xchg(0,1);
 $red = cos(2*$surface); $green = sin($surface); $blue = $surface;
 $colors = cat($red, $green, $blue)->xchg(0,1);
 imagrgb([$red,$green,$blue]);     # 2-d piddles
 spheres3d([$x,$y,$z]);  # preliminary implementation

 hold3d(); # the following graphs are on top of each other and the previous
 $pic = grabpic3d(); # Returns the picture in a (3,$x,$y) float piddle (0..1).

 release3d(); # the next graph will again wipe out things.

=head1 WARNING

These modules are still in a somewhat unfocused state: don't use them yet
if you don't know how to make them work if they happen to do something


This module implements a generic 3D plotting interface for PDLA.
Points, lines and surfaces (among other objects) are supported.

With OpenGL, it is easy to manipulate the resulting 3D objects
with the mouse in real time - this helps data visualization a lot.

=for comment
With VRML, you can generate objects for everyone to see with e.g.
Silicon Graphics' Cosmo Player. You can find out more about VRML
at C<http://vrml.sgi.com/> or C<http://www.vrml.org/>


The default device for TriD is currently OpenGL.
You can specify a different device either in your program
or in the environment variable C<PDLA_3D_DEVICE>.
The one specified in the program takes priority.

The currently available devices are

=over 8

=item GL


=item GLpic

OpenGL but off-line (pixmap) rendering and writing to
a graphics file.

=item VRML (I< Not available this release >)

VRML objects rendering. This writes a VRML file describing the
scene. This VRML file can then be read with  a browser.



TriD  offers both on- and off-line visualization.
Currently the interface  w.r.t. this division is still much
in motion.

For OpenGL you can select either on- or off-line rendering.
VRML is currently always offline (this may change  later,
if someone bothers to write  the  java(script)  code to  contact
PDLA and wait for the next PDLA image over the network.


Specifying a set of coordinates is generally a context-dependent operation.
For a traditional 3D surface plot, you'll want two of the coordinates
to have just the xvals and yvals of the piddle, respectively.
For a line, you would generally want to have one coordinate held
at zero and the other advancing.

This module tries to make a reasonable way of specifying the context
while letting you do whatever you want by overriding the default

The alternative syntaxes for specifying a set of coordinates (or colors) are

   $piddle                             # MUST have 3 as first dim.

where C<CONTEXT> is a string describing in which context you wish these
piddles to be interpreted. Each routine specifies a default context
which is explained in the routines documentation.
Context is usually used only to understand what the user wants
when he/she specifies less than 3 piddles.

The following contexts are currently supported:

=over 8

=item SURF2D

A 2-D lattice. C< [$piddle] > is interpreted as the Z coordinate over
a lattice over the first dimension. Equivalent to
C<< [$piddle->xvals, $piddle->yvals, $piddle] >>.

=item POLAR2D

A 2-D polar coordinate system. C< [$piddle] > is interpreted as the
z coordinate over theta and r (theta = the first dimension of the piddle).

=item COLOR

A set of colors. C< [$piddle] > is interpreted as grayscale color
(equivalent to C< [$piddle,$piddle,$piddle] >).

=item LINE

A line made of 1 or 2 coordinates. C< [$piddle] > is interpreted as
C<< [$piddle->xvals,$piddle,0] >>. C< [$piddle1,$piddle2] > is interpreted as
C<< [$piddle1,$piddle2,$piddle1->xvals] >>.


What makes contexts useful is that if you want to plot points
instead of the full surface you plotted with


you don't need to start thinking about where to plot the points:


will do exactly the same.

=head2 Wrapping your head around 3d surface specifications

Let's begin by thnking about how you might make a 2d data plot.
If you sampled your data at regular intervals, you would have
a time serires y(t) = (y0, y1, y2, ...).  You could plot y vs t
by computing t0 = 0, t1 = dt, t2 = 2 * dt, and then plotting
(t0, y0), (t1, y1), etc.

Next suppose that you measured x(t) and y(t).  You can still
plot y vs t, but you can also plot y vs x by plotting (x0, y0),
(x1, y1), etc.  The x-values don't have to increase monotonically:
they could back-track on each other, for example, like the
latitude and longitude of a boat on a lake.  If you use plplot,
you would plot this data using
C<< $pl->xyplot($x, $y, PLOTTYPE => 'POINTS') >>.

Good.  Now let's add a third coordinate, z(t).  If you actually
sampled x and y at regular intervals, so that x and y lie on a
grid, then you can construct a grid for z(x, y), and you would
get a surface.  This is the situation in which you would use

Of course, your data is not required to be regularly gridded.
You could, for example, be measuring the flight path of a bat
flying after mosquitos, which could be wheeling and arching
all over the space.  This is what you might plot using 
C<line3d([$x, $y, $z])>.  You could plot the trajectories of
multiple bats, in which case C<$x>, C<$y>, and C<$z> would have
multiple columns, but in general you wouldn't expect them to be

Finally, imagine that you have an air squadron flying in
formation.  Your (x, y, z) data is not regularly gridded, but
the (x, y, z) data for each plane should be coordinated and
we can imagine that their flight path sweep out a surface.
We could draw this data using C<line3d([$x, $y, $z])>, where
each column in the variables corresponds to a different plane,
but it would also make sense to draw this data using
C<mesh3d([$x, $y, $z])>, since the planes' proximity to each
other should be fairly consistent.  In other words, it makes
sense to think of the planes as sweeping out a coordinated
surface, which C<mesh3d> would draw for you, whereas you would
not expect the trajectories of the various bats to describe a
meaningful surface (unless you're into fractals, perhaps).


 use PDLA;
 use PDLA::Graphics::TriD;

 # Draw out a trajectory in three-space
 $t = sequence(100)/10;
 $x = sin($t); $y = cos($t); $z = $t;

 # Plot the trajectory as (x(t), y(t), z(t))
 print "using line3d to plot a trajectory (press q when you're done twiddling)\n";
 line3d [$x,$y,$z];

 # If you give it a single piddle, it expects
 # the data to look like
 # ((x1, y1, z1), (x2, y2, z2), ...)
 # which is why we have to do the exchange:
 $coords = cat($x, $y, $z)->xchg(0,1);
 print "again, with a different coordinate syntax (press q when you're done twiddling)\n";
 line3d $coords;

 # Draw a regularly-gridded surface:
 $surface = sqrt(rvals(zeroes(50,50))/2);
 print "draw a mesh of a regularly-gridded surface using mesh3d\n";
 mesh3d [$surface];
 print "draw a regularly-gridded surface using imag3d\n";
 imag3d [$surface], {Lines=>0};

 # Draw a mobius strip:
 $two_pi = 8 * atan2(1,1);
 $t = sequence(50) / 50 * $two_pi;
 # We want two paths:
 $mobius1_x = cos($t) + 0.5 * sin($t/2);
 $mobius2_x = cos($t);
 $mobius3_x = cos($t) - 0.5 * sin($t/2);
 $mobius1_y = sin($t) + 0.5 * sin($t/2);
 $mobius2_y = sin($t);
 $mobius3_y = sin($t) - 0.5 * sin($t/2);
 $mobius1_z = $t - $two_pi/2;
 $mobius2_z = zeroes($t);
 $mobius3_z = $two_pi/2 - $t;

 $mobius_x = cat($mobius1_x, $mobius2_x, $mobius3_x);
 $mobius_y = cat($mobius1_y, $mobius2_y, $mobius3_y);
 $mobius_z = cat($mobius1_z, $mobius2_z, $mobius3_z);

 $mobius_surface = cat($mobius_x, $mobius_y, $mobius_z)->mv(2,0);

 print "A mobius strip using line3d one way\n";
 line3d $mobius_surface;
 print "A mobius strip using line3d the other way\n";
 line3d $mobius_surface->xchg(1,2);
 print "A mobius strip using mesh3d\n";
 mesh3d $mobius_surface;
 print "The same mobius strip using imag3d\n";
 imag3d $mobius_surface, {Lines => 0};


Because using the whole object-oriented interface for doing
all your work might be cumbersome, the following shortcut
routines are supported:


=head2 line3d

=for ref

3D line plot, defined by a variety of contexts.

=for usage

 line3d piddle(3,x), {OPTIONS}
 line3d [CONTEXT], {OPTIONS}

=for example


 pdl> line3d [sqrt(rvals(zeroes(50,50))/2)]
 - Lines on surface
 pdl> line3d [$x,$y,$z]
 - Lines over X, Y, Z
 pdl> line3d $coords
 - Lines over the 3D coordinates in $coords.

Note: line plots differ from mesh plots in that lines
only go in one direction. If this is unclear try both!

See module documentation for more information on
contexts and options

=head2 imag3d

=for ref

3D rendered image plot, defined by a variety of contexts

=for usage

 imag3d piddle(3,x,y), {OPTIONS}
 imag3d [piddle,...], {OPTIONS}

=for example


 pdl> imag3d [sqrt(rvals(zeroes(50,50))/2)], {Lines=>0};

 - Rendered image of surface

See module documentation for more information on
contexts and options

=head2 mesh3d

=for ref

3D mesh plot, defined by a variety of contexts

=for usage

 mesh3d piddle(3,x,y), {OPTIONS}
 mesh3d [piddle,...], {OPTIONS}

=for example


 pdl> mesh3d [sqrt(rvals(zeroes(50,50))/2)]

 - mesh of surface

Note: a mesh is defined by two sets of lines at
right-angles (i.e. this is how is differs from

See module documentation for more information on
contexts and options

=head2 lattice3d

=for ref

alias for mesh3d

=head2 points3d

=for ref

3D points plot, defined by a variety of contexts

=for usage

 points3d piddle(3), {OPTIONS}
 points3d [piddle,...], {OPTIONS}

=for example


 pdl> points3d [sqrt(rvals(zeroes(50,50))/2)];
 - points on surface

See module documentation for more information on
contexts and options

=head2 spheres3d

=for ref

3D spheres plot (preliminary implementation)

=for usage

 spheres3d piddle(3), {OPTIONS}
 spheres3d [piddle,...], {OPTIONS}

=for example


 pdl> spheres3d ndcoords(10,10,10)->clump(1,2,3)  
 - lattice of spheres at coordinates on 10x10x10 grid

This is a preliminary implementation as a proof of
concept.  It has fixed radii for the spheres being
drawn and no control of color or transparency.

=head2 imagrgb

=for ref

2D RGB image plot (see also imag2d)

=for usage

 imagrgb piddle(3,x,y), {OPTIONS}
 imagrgb [piddle,...], {OPTIONS}

This would be used to plot an image, specifying
red, green and blue values at each point. Note:
contexts are very useful here as there are many
ways one might want to do this.

=for example


 pdl> $x=sqrt(rvals(zeroes(50,50))/2)
 pdl> imagrgb [0.5*sin(8*$x)+0.5,0.5*cos(8*$x)+0.5,0.5*cos(4*$x)+0.5]

=head2 imagrgb3d

=for ref

2D RGB image plot as an object inside a 3D space

=for usage

 imagrdb3d piddle(3,x,y), {OPTIONS}
 imagrdb3d [piddle,...], {OPTIONS}

The piddle gives the colors. The option allowed is Points,
which should give 4 3D coordinates for the corners of the polygon,
either as a piddle or as array ref.
The default is [[0,0,0],[1,0,0],[1,1,0],[0,1,0]].

=for example


 pdl> imagrgb3d $colors, {Points => [[0,0,0],[1,0,0],[1,0,1],[0,0,1]]};
 - plot on XZ plane instead of XY.

=head2 grabpic3d

=for ref

Grab a 3D image from the screen.

=for usage

 $pic = grabpic3d();

The returned piddle has dimensions (3,$x,$y) and is of type float
(currently). XXX This should be altered later.

=head2 hold3d, release3d

=for ref

Keep / don't keep the previous objects when plotting new 3D objects

=for usage




=head2 keeptwiddling3d, nokeeptwiddling3d

=for ref

Wait / don't wait for 'q' after displaying a 3D image.

Usually, when showing 3D images, the user is given a chance
to rotate it and then press 'q' for the next image. However,
sometimes (for e.g. animation) this is undesirable and it is
more desirable to just run one step of the event loop at
a time.

=for usage




When an image is added to the screen, keep twiddling it until
user explicitly presses 'q'.

=for example

 $o = imag3d($c);
 while(1) {
 	$c .= nextfunc($c);
 	twiddle3d();		# animate one step, then return.

=head2 twiddle3d

=for ref

Wait for the user to rotate the image in 3D space.

Let the user rotate the image in 3D space, either for one step
or until (s)he presses 'q', depending on the 'keeptwiddling3d'
setting. If 'keeptwiddling3d' is not set the routine returns
immediately and indicates that a 'q' event was received by
returning 1. If the only events received were mouse events,
returns 0.


The key concepts (object types) of TriD are explained in the following:

=head2 Object

In this 3D abstraction, everything that you can "draw"
without using indices is an Object. That is, if you have a surface,
each vertex is not an object and neither is each segment of a long
curve. The whole curve (or a set of curves) is the lowest level Object.

Transformations and groups of Objects are also Objects.

A Window is simply an Object that has subobjects.

=head2 Twiddling

Because there is no eventloop in Perl yet and because it would
be hassleful to do otherwise, it is currently not possible to
e.g. rotate objects with your mouse when the console is expecting
input or the program is doing other things. Therefore, you need
to explicitly say "$window->twiddle()" in order to display anything.

=head1 OBJECTS

The following types of objects are currently supported.
Those that do not have a calling sequence described here should
have their own manual pages.

There are objects that are not mentioned here; they are either internal
to PDLA3D or in rapidly changing states. If you use them, you do so at
your own risk.

The syntax C<PDLA::Graphics::TriD::Scale(x,y,z)> here means that you create
an object like

	$c = new PDLA::Graphics::TriD::Scale($x,$y,$z);

=head2 PDLA::Graphics::TriD::LineStrip

This is just a line or a set of lines. The arguments are 3 1-or-more-D
piddles which describe the vertices of a continuous line and an 
optional color piddle (which is 1-D also and simply
defines the color between red and blue. This will probably change).

=head2 PDLA::Graphics::TriD::Lines

This is just a line or a set of lines. The arguments are 3 1-or-more-D
piddles where each contiguous pair of vertices describe a line segment 
and an optional color piddle (which is 1-D also and simply
defines the color between red and blue. This will probably change).

=head2 PDLA::Graphics::TriD::Image

This is a 2-dimensional RGB image consisting of colored
rectangles. With OpenGL, this is implemented by texturing so this should
be relatively memory and execution-time-friendly.

=head2 PDLA::Graphics::TriD::Lattice

This is a 2-D set of points connected by lines in 3-space.
The constructor takes as arguments 3 2-dimensional piddles.

=head2 PDLA::Graphics::TriD::Points

This is simply a set of points in 3-space. Takes as arguments
the x, y and z coordinates of the points as piddles.

=head2 PDLA::Graphics::TriD::Scale(x,y,z)


=head2 PDLA::Graphics::TriD::Translation(x,y,z)


=head2 PDLA::Graphics::TriD::Quaternion(c,x,y,z)

One way of representing rotations is with quaternions. See the appropriate
man page.

=head2 PDLA::Graphics::TriD::ViewPort

This is a special class: in order to obtain a new viewport, you
need to have an earlier viewport on hand. The usage is:

  $new_vp = $old_vp->new_viewport($x0,$y0,$x1,$y1);

where $x0 etc are the coordinates of the upper left and lower right
corners of the new viewport inside the previous (relative
to the previous viewport in the (0,1) range.

Every implementation-level window object should implement the new_viewport




# List of global variables
# $PDLA::Graphics::TriD::offline
# $PDLA::Graphics::TriD::Settings 
# $PDLA::Graphics::TriD::verbose
# $PDLA::Graphics::TriD::keeptwiddling
# $PDLA::Graphics::TriD::hold_on
# $PDLA::Graphics::TriD::curgraph
# $PDLA::Graphics::TriD::cur
# $PDLA::Graphics::TriD::create_window_sub
# $PDLA::Graphics::TriD::current_window
# '

package PDLA::Graphics::TriD::Basic;
package PDLA::Graphics::TriD;

use PDLA::Exporter;
use PDLA::Core '';  # barf
use vars qw/@ISA @EXPORT_OK %EXPORT_TAGS/;
@ISA = qw/PDLA::Exporter/;
@EXPORT_OK = qw/imag3d_ns imag3d line3d mesh3d lattice3d points3d
  spheres3d describe3d imagrgb imagrgb3d hold3d release3d
  keeptwiddling3d nokeeptwiddling3d
  twiddle3d grabpic3d tridsettings/;

#use strict;
use PDLA::Graphics::TriD::Object;
use PDLA::Graphics::TriD::Window;
use PDLA::Graphics::TriD::ViewPort;
use PDLA::Graphics::TriD::Graph;
use PDLA::Graphics::TriD::Quaternion;
use PDLA::Graphics::TriD::Objects;
use PDLA::Graphics::TriD::Rout;

# Then, see which display method are we using:

	my $dev;
	$dev ||= $::PDLA::Graphics::TriD::device; # First, take it from this variable.
	$dev ||= $::ENV{PDLA_3D_DEVICE};

        if(!defined $dev) {
#            warn "Default PDLA 3D device is GL (OpenGL):
# Set PDLA_3D_DEVICE=GL in your environment in order not to see this warning.
# You must have OpenGL or Mesa installed and the PDLA::Graphics::OpenGL extension
# compiled. Otherwise you will get strange warnings.";

           $dev = "GL";  # default GL works on all platforms now
	my $dv;
# The following is just a sanity check.
	for($dev) {
#		(/^OOGL$/  and $dv="PDLA::Graphics::TriD::OOGL") or
		(/^GL$/  and $dv="PDLA::Graphics::TriD::GL") or
		(/^GLpic$/  and $dv="PDLA::Graphics::TriD::GL" and $PDLA::Graphics::TriD::offline=1) or
		(/^VRML$/  and $dv="PDLA::Graphics::TriD::VRML" and $PDLA::Graphics::TriD::offline=1) or
		(barf "Invalid PDLA 3D device '$_' specified!");
	my $mod = $dv;
	$mod =~ s|::|//|g;
	print "dev = $dev mod=$mod\n" if($verbose);
	require "$mod.pm";
        my $verbose;

# currently only used by VRML backend
sub tridsettings {return $PDLA::Graphics::TriD::Settings}

# Allowable forms:
# x(3,..)  [x(..),y(..),z(..)]
sub realcoords {
	my($type,$c) = @_;
	if(ref $c ne "ARRAY") {
		if($c->getdim(0) != 3) {
			barf "If one piddle given for coordinate, must be (3,...) or have default interpretation";
		return $c ;
	if(!ref $c->[0]) {$type = shift @$c}
	if($#$c < 0 || $#$c>2) {
		barf "Must have 1..3 array members for coordinates";
	if($#$c == 0 and $type =~ /^SURF2D$/) {
		# surf2d -> this is z axis
		@$c = ($c->[0]->xvals,$c->[0]->yvals,$c->[0]);
	} elsif($#$c == 0 and $type eq "POLAR2D") {
		my $t = 6.283 * $c->[0]->xvals / ($c->[0]->getdim(0)-1);
		my $r = $c->[0]->yvals / ($c->[0]->getdim(1)-1);
		@$c = ($r * sin($t), $r * cos($t), $c->[0]);
	} elsif($#$c == 0 and $type eq "COLOR") {
		# color -> 1 piddle = grayscale
		@$c = ($c->[0], $c->[0], $c->[0]);
	} elsif($#$c == 0 and $type eq "LINE") {
		@$c = ($c->[0]->xvals, $c->[0], 0);
	} elsif($#$c == 1 and $type eq "LINE") {
		@$c = ($c->[0], $c->[1], $c->[0]->xvals);
	# XXX
	if($#$c != 2) {
		barf("Must have 3 coordinates if no interpretation (here '$type')");
	# allow a constant (either pdl or not) to be introduced in one dimension
	  if(ref($c->[$_]) ne "PDLA" or $c->[$_]->nelem==1){
	    $c->[$_] = $c->[$_]*(PDLA->ones($c->[($_+1)%3]->dims));
	my $g = PDLA->null;
	$g->dump if $PDLA::Graphics::TriD::verbose;
	return $g;

sub objplotcommand {
	my($object) = @_;
	my $win = PDLA::Graphics::TriD::get_current_window();
	my $world = $win->world();

sub checkargs {
	if(ref $_[$#_] eq "HASH" and $PDLA::Graphics::TriD::verbose) {

	  print "enter checkargs \n";
		for([KeepTwiddling,\&keeptwiddling3d]) {
		  print "checkargs >$_<\n";
			if(defined $_[$#_]{$_->[0]}) {
				&{$_->[1]}(delete $_[$#_]{$_->[0]});

*keeptwiddling3d = \&PDLA::keeptwiddling3d;
sub PDLA::keeptwiddling3d {
	$PDLA::Graphics::TriD::keeptwiddling = (defined $_[0] ? $_[0] : 1);
*nokeeptwiddling3d = \&PDLA::nokeeptwiddling3d;
sub PDLA::nokeeptwiddling3d {
	$PDLA::Graphics::TriD::keeptwiddling = 0 ;
*twiddle3d = \&PDLA::twiddle3d;
sub PDLA::twiddle3d {

sub graph_object {
	my($obj) = @_;
	if(!defined $obj or !ref $obj) {
		barf("Invalid object to TriD::graph_object");
	print "graph_object: calling get_new_graph\n" if($PDLA::debug_trid);
	my $g = get_new_graph();
	print "graph_object: back from get_new_graph\n" if($PDLA::debug_trid);

	my $name = $g->add_dataseries($obj);
	print "ADDED TO GRAPH: '$name'\n" if $PDLA::Graphics::TriD::verbose;

	return $obj;

# Plotting routines that use the whole viewport

sub PDLA::describe3d {
	require PDLA::Graphics::TriD::TextObjects;
	my ($text) = @_;
	my $win = PDLA::Graphics::TriD::get_current_window();
	my $imag = new PDLA::Graphics::TriD::Description($text);
#	$win->twiddle();

sub PDLA::imagrgb {
	require PDLA::Graphics::TriD::Image;
	my (@data) = @_; &checkargs;
	my $win = PDLA::Graphics::TriD::get_current_window();
	my $imag = new PDLA::Graphics::TriD::Image(@data);



# Plotting routines that use the 3D graph

# Call: line3d([$x,$y,$z],[$color]);
sub PDLA::line3d { 
    my $obj = new PDLA::Graphics::TriD::LineStrip(@_);
    print "line3d: object is $obj\n" if($PDLA::debug_trid);

sub PDLA::contour3d { 
#  &checkargs;
  require PDLA::Graphics::TriD::Contours;
  &graph_object(new PDLA::Graphics::TriD::Contours(@_));

# XXX Should enable different positioning...
sub PDLA::imagrgb3d { &checkargs;
	require PDLA::Graphics::TriD::Image;
	&graph_object(new PDLA::Graphics::TriD::Image(@_));

sub PDLA::imag3d_ns {  &checkargs;
	&graph_object(new PDLA::Graphics::TriD::SLattice(@_));

sub PDLA::imag3d { &checkargs;
	&graph_object(new PDLA::Graphics::TriD::SLattice_S(@_));

################ JNK 15mar11 added section start ###################
sub PDLA::STrigrid_S_imag3d { &checkargs;
  &graph_object(new PDLA::Graphics::TriD::STrigrid_S(@_)); }
sub PDLA::STrigrid_imag3d { &checkargs;
  &graph_object(new PDLA::Graphics::TriD::STrigrid(@_)); }
################ JNK 15mar11 added section finis ###################

sub PDLA::mesh3d { &checkargs;
	&graph_object(new PDLA::Graphics::TriD::Lattice(@_));

sub PDLA::points3d { &checkargs;
	&graph_object(new PDLA::Graphics::TriD::Points(@_));

sub PDLA::spheres3d { &checkargs;
	&graph_object(new PDLA::Graphics::TriD::Spheres(@_));

sub PDLA::grabpic3d {
	my $win = PDLA::Graphics::TriD::get_current_window();
	barf "backend doesn't support grabing the rendered scene"
	  unless $win->can('read_picture');
	my $pic = $win->read_picture();
	return ($pic->float) / 255;

$PDLA::Graphics::TriD::hold_on = 0;

sub PDLA::hold3d {$PDLA::Graphics::TriD::hold_on =(!defined $_[0] ? 1 : $_[0]);}
sub PDLA::release3d {$PDLA::Graphics::TriD::hold_on = 0;}


sub get_new_graph {
    print "get_new_graph: calling PDLA::Graphics::TriD::get_current_window...\n" if($PDLA::debug_trid);
	my $win = PDLA::Graphics::TriD::get_current_window();

    print "get_new_graph: calling get_current_graph...\n" if($PDLA::debug_trid);
	my $g = get_current_graph($win);
    print "get_new_graph: back get_current_graph returned $g...\n" if($PDLA::debug_trid);

	if(!$PDLA::Graphics::TriD::hold_on) {

	return $g;

sub get_current_graph {
   my $win = shift;
	my $g = $win->current_viewport()->graph();

	if(!defined $g) {
		$g = new PDLA::Graphics::TriD::Graph();
	return $g;

# $PDLA::Graphics::TriD::cur = {};
# $PDLA::Graphics::TriD::create_window_sub = undef;
sub get_current_window {
  my $opts = shift @_;
  my $win = $PDLA::Graphics::TriD::cur;

  if(!defined $win) {
	 if(!$PDLA::Graphics::TriD::create_window_sub) {
		barf("PDLA::Graphics::TriD must be used with a display mechanism: for example PDLA::Graphics::TriD::GL!\n");
	 print "get_current_window - creating window...\n" if($PDLA::debug_trid);
	 $win = new PDLA::Graphics::TriD::Window($opts);

	 print "get_current_window - calling set_material...\n" if($PDLA::debug_trid);
	 $win->set_material(new PDLA::Graphics::TriD::Material);
	 $PDLA::Graphics::TriD::current_window = $win;
	 $PDLA::Graphics::TriD::cur = $win
  return $PDLA::Graphics::TriD::current_window;

# Get the current graphbox
sub get_current_graphbox {
        die "get_current_graphbox: ERROR graphbox is not implemented! \n";
	my $graph = $PDLA::Graphics::TriD::curgraph;
	if(!defined $graph) {
		$graph = new PDLA::Graphics::TriD::Graph();
		$PDLA::Graphics::TriD::curgraph = $graph;
	return $graph;

sub twiddle_current {
	my $win = get_current_window();

package PDLA::Graphics::TriD::Material;

sub new {
  my ($type,%ops) = @_;
  my $this = bless {}, $type;
  for (['Shine',40],
       ['Emissive',[0,0,0]]) {
    if (!defined $ops{$_->[0]}) {
      $this->{$_->[0]} = $_->[1];
    } else {
      $this->{$_->[0]} = $ops{$_->[0]};
  return $this;

package PDLA::Graphics::TriD::BoundingBox;
use base qw/PDLA::Graphics::TriD::Object/;
use fields qw/Box/;

sub new { 
  my($type,$x0,$y0,$z0,$x1,$y1,$z1) = @_;
  my $this = $type->SUPER::new();
  $this->{Box} = [$x0,$y0,$z0,$x1,$y1,$z1];

sub normalize {my($this,$x0,$y0,$z0,$x1,$y1,$z1) = @_;
	$this = $this->{Box};
	my $trans = PDLA::Graphics::TriD::Transformation->new();
	my $sx = ($x1-$x0)/($this->[3]-$this->[0]);
	my $sy = ($y1-$y0)/($this->[4]-$this->[1]);
	my $sz = ($z1-$z0)/($this->[5]-$this->[2]);
	return $trans;

package PDLA::Graphics::TriD::OneTransformation;
use fields qw/Args/;

sub new {
  my($type,@args) = @_;
  my $this = fields::new($type);

  $this->{Args} = [@args];


package PDLA::Graphics::TriD::Scale;
use base qw/PDLA::Graphics::TriD::OneTransformation/;

package PDLA::Graphics::TriD::Translation;
use base qw/PDLA::Graphics::TriD::OneTransformation/;

package PDLA::Graphics::TriD::Transformation;
use base qw/PDLA::Graphics::TriD::Object/;

#sub new {
#	my($type) = @_;
#	bless {},$type;

sub add_transformation {
	my($this,$trans) = @_;
	push @{$this->{Transforms}},$trans;

=head1 BUGS

Not enough is there yet.

=head1 AUTHOR

Copyright (C) 1997 Tuomas J. Lukka (lukka@husc.harvard.edu). Documentation
contributions from Karl Glazebrook (kgb@aaoepp.aao.gov.au).
All rights reserved. There is no warranty. You are allowed
to redistribute this software / documentation under certain
conditions. For details, see the file COPYING in the PDLA
distribution. If this file is separated from the PDLA distribution,
the copyright notice should be included in the file.