#! /usr/bin/perl

BEGIN { require "../../../../perl/lib/Sitedefs.pm"; }

use ErrorLog;
use CGITracker;
use Trigrp;
use PageTemplate;

my $navfile = "$Sitedefs::ROOTPATH/geometry/navigation.html";

sub PixelsToEPoint {
    my ($group,$trisys,$px,$py) = @_;
    my ($ex,$ey,$ez);
    if ($group eq '236') {
	($ex,$ey) = $trisys->FromPixels($px,$py);
	$ez = 0;
    } else {
	($ey,$ez) = $trisys->FromPixels($px,$py);
	$ex = sqrt(1 - $ey*$ey - $ez*$ez);
    }
    return ($ex,$ey,$ez);
}


sub MyWriteImages {
    my ($group,$px,$py,$rx,$ry,$rz,$trisys,$polysys) = @_;

    my ($ex,$ey,$ez) = PixelsToEPoint($group,$trisys,$px,$py);

    my $trigif  = "tmp/tri.$$.gif";
    my $polygif = "tmp/poly.$$.gif";

    Trigrp::WriteImages($trigif,$polygif, $group,
			$ex, $ey, $ez,
			$rx, $ry, $rz,
			$trisys,
			$polysys->{PIXELWIDTH},
			$polysys->{PIXELHEIGHT});

    return ($trigif, $polygif, $ex,$ey,$ez);
}

$q = new CGITracker;
$tpl = new PageTemplate;

$rx = $q->param('rx');
$ry = $q->param('ry');
$rz = $q->param('rz');
$oldtrix = $q->param('oldx');
$oldtriy = $q->param('oldy');

$groupno = $q->param('groupno');

# Check to see if one of the group buttons was pressed.
foreach $p ($q->param()) {
    if ( ($n) = ($p =~ /^group(\d)/) ) {
	$groupno = $n;
    }
}

if ($groupno eq "") {
    $groupno = '6';
}
$group = "23" . $groupno; 

if ($groupno eq '6') {
    $rx = $ry = $rz = 0;
}

my $trisys = Trigrp::Trisys($groupno);
my $triwidth = $trisys->{PIXELWIDTH};
my $triheight = $trisys->{PIXELHEIGHT};

my $polysys = Trigrp::Polysys($groupno);

my ($ex,$ey,$ez) = (0.196,0.205128,0); ##JEFF: added defaults for ex ey ez

if (! $q->param()) {                  
    $trigif = "data/tri.gif";
    $polygif = "data/poly.gif";
} elsif ($trix = $q->param('triangle.x')) {
    $triy = $q->param('triangle.y');
    ($trigif, $polygif,$ex,$ey,$ez) =
	MyWriteImages($group, $trix,$triy,
		    $rx, $ry,$rz, $trisys, $polysys,
		    $ex,$ey,$ez);
} elsif ($polyx = $q->param('tiling.x')) {
    $polyy = $q->param('tiling.y');

    my ($ix,$iy) = $polysys->FromPixels($polyx,$polyy);

    $rx += $ix * $Trigrp::pi / 4;
    $ry += $iy * $Trigrp::pi / 4;

   ($trigif, $polygif,$ex,$ey,$ez) =
       MyWriteImages($group, $trix=$oldtrix,$triy=$oldtriy,
		   $rx, $ry, $rz, $trisys, $polysys);
} else {
    ($trix,$triy) = $trisys->ToPixels(0.2, 0.2);
    if (! $rx) { $rx = 0; }
    if (! $ry) { $ry = 0; }
    if (! $rz) { $rz = 0; }
    
    ($trigif, $polygif,$ex,$ey,$ez) =
	MyWriteImages($group, $trix,$triy,
		    $rx,$ry,$rz, $trisys, $polysys);
		    
}

$hiddenfields = <<EndOfStuff
<input type="hidden" name="oldx" value="$trix">
<input type="hidden" name="oldy" value="$triy">
<input type="hidden" name="rx" value="$rx">
<input type="hidden" name="ry" value="$ry">
<input type="hidden" name="rz" value="$rz">
<input type="hidden" name="groupno" value="$groupno">
EndOfStuff
    ;

$buttontable = "<table><tr>\n";
for ($i=6; $i>=3; --$i) {
    $buttonimg = "$i" . (($i == $groupno) ? "down" : "up") . ".gif";
    $buttontable .= "  <td>\n";
    $buttontable .= "    <input type=\"image\"\n";
    $buttontable .= "           src=\"images/$buttonimg\"\n";
    $buttontable .= "           height=\"36\" width=\"36\"\n";
    $buttontable .= "           name=\"group$i\">\n";
    if ($i == 6) {
	$buttontable .= "  &nbsp;&nbsp;\n";
    }
    $buttontable .= "  </td>\n";
}
$buttontable .= "</tr></table>";

$command =
    "TRIGRP[Group[$group],EPoint[$ex,$ey,$ez],Rot[$rx,$ry,$rz]]";

if ($group ne '236') {
    $triangle_heading =
	"Click in this triangle to<br>move the bending point<br>";
    $tiling_heading =
	"Click on this picture to<br>rotate the polyhedron<br>";
    $tiling_picture = <<EndOfStuff
<input
   type="image"
   name="tiling"
   src="$polygif"
   width=$Trigrp::polywidth
   height=$Trigrp::polyheight
   align="middle">
EndOfStuff
    ;
} else {
    $triangle_heading =
	"Click in this triangle to<br>change the coloring<br>";
    $tiling_heading = "";
    $tiling_picture = <<EndOfStuff
<img
   src="$polygif"
   width=$Trigrp::polywidth
   height=$Trigrp::polyheight
   align="middle">
EndOfStuff
    ;
}




#$q->Print($q->start_html());
$q->Print($tpl->Header("Sciencu U: Interactive Triangle Tiling Maker"));

open(NAV, "<$navfile");
while (<NAV>) {
    $q->Print($_);
}
close(NAV);



$q->Print($tpl->PreContent());

$q->Print(<<EndOfStuff
<center>
<h1>Interactive Triangle Tiling Maker</h1>
</center>
<p>
<hr>
<p>
<form action="trigrp.cgi" method="GET">
<center>
<table width="100%" cellpadding="15">
<tr>
<td width="60"></td>

<td align="center">

<table>
<tr>
<td align=center>
<center>
$triangle_heading
<input
   type="image"
   name="triangle"
   src="$trigif"
   width="$triwidth"
   height="$triheight"
   align="middle">
</center>
</td>
<td align=center>
<center>
$tiling_heading
$tiling_picture
<br>
<table width="100%" style="background: #ffffdd" cellpadding="5">
<tr>

<td>
$buttontable
</td>
<td>
<a href="$Sitedefs::ROOTURL/studio/studio.cgi?$command=add">
<img src="$Sitedefs::ROOT/pix/addstudio.gif" width="60" height="20"
border="0" alt="Add this picture to your Science U Studio"></a>
</td>

<!--<td width="50%" align="center">
<a href="$Sitedefs::ROOTURL/studio/studio.cgi?$command=add&focus=tshirt">
<img src="$Sitedefs::ROOT/pix/maketee.gif" width="60" height="20"
border="0" alt="Make a T-Shirt with this image"></a>
</td>-->

</tr></table>
</center>
</td>
</tr>
</table>
$hiddenfields
</form>
</td>

</tr>
</table>


</td>

</center>

EndOfStuff
	  );

$q->Print($tpl->Footer());
$q->EndPrint();
