#! /usr/bin/perl

push(@INC, grep(-f "$_/tfm.pl", split(':', $ENV{"PATH"})));
require 'tfm.pl';

# cyl2cave.pl
# Simple program to construct the ProjectionData entries
# for a 3-panel cylindrical screen, e.g. SGI Reality Center.
# Stuart Levy, NCSA, University of Illinois, 1999-2001.  slevy@ncsa.uiuc.edu
# Feel free to use this code; please retain credit.
# If you make improvements, please pass them back to the author.

# Parameters:
#   hfov  vfov  offset  size  yoff
#
#  hfov   : horizontal FOV (degrees) of each of the 3 panels
#  vfov   : vertical FOV (degrees) of each panel
#  offset : dihedral angle (deg) between planes of adjacent panels
#  size   : Z-distance from design viewpoint (= Z=0 plane)
#		to plane of front panel, in CAVE units.
#		Sets scale of all dimensions.
#  yoff   : CAVE Y value of center of screen.

# Names the three panels
#   Screen2  ( left side )
#   Screen0  ( center )
#   Screen1  ( right )
# for no very good reason.


$hfov = $ARGV[0] || 53.57;
$vfov = $ARGV[1] || 40.0;
$offset = $ARGV[2] || 48.21;	# Dihedral angle

$size = $ARGV[3] || 7;
$yoff = $ARGV[4] || 4;

$hx = $size * &tandeg( $hfov/2 );
$hy = $size * &tandeg( $vfov/2 );
@lrot = &tfm( 'y', $offset );
@rrot = &tfm( 'y', -$offset );

print <<EOF;
## Computed by:
#	$0 $hfov $vfov  $offset  $size  $yoff
# Horiz FOV  $hfov degrees
# Vert  FOV  $vfov degrees
# Offset (dihedral angle between panels) $offset degrees
# Z-Distance from viewpoint to front panel plane  $size (feet?)
# Y-offset (= Y component of head position) $yoff (feet?)


EOF

@ll = (-$hx, $yoff-$hy, -$size);
@ul = (-$hx, $yoff+$hy, -$size);
@lr = ($hx, $yoff-$hy, -$size);

printf <<EOF, &v3mmul(@ll, @lrot), &v3mmul(@ul, @lrot), &v3mmul(@lr, @lrot);
# Left panel
ProjectionData Screen2  both wall  %.2f %.2f %.2f  %.2f %.2f %.2f  %.2f %.2f %.2f
EOF

printf <<EOF, @ll, @ul, @lr;
# Central panel
ProjectionData Screen0  both wall  %.2f %.2f %.2f  %.2f %.2f %.2f  %.2f %.2f %.2f
EOF

printf <<EOF, &v3mmul(@ll, @rrot), &v3mmul(@ul, @rrot), &v3mmul(@lr, @rrot);
# Right panel
ProjectionData Screen1  both wall  %.2f %.2f %.2f  %.2f %.2f %.2f  %.2f %.2f %.2f
EOF

