##--!perl -w
use CGI; my $cgi = new CGI;
use strict;
use warnings;

#input files
#output files
#open b_file, ">browser.txt" or die "could not open the write file browser.txt\n";
#open(STDERR,">log.txt") or die "Failed to redirect STDERR to log.txt";


#--- general, including parameters in the basic action cycle ---
  my $line_in1;
  my $basic_action;	#basic action as viewed from this perspective
  my $fixed_params;	#fixed parameters not dependent on the viewing perspective
  my $duration;		#"5s"=5 second duration for the complete basic action, without repeats
  my $repeat_count;	#number of times the basic action is repeated

  my $ScreenWidth;
  my $ScreenHeight;
#  my $timeout;
#  my $msg;	#-- for errors

#--- stage and audience (viewer) ---
  my $frontStageLeft_x;
  my $frontStageRight_x;
  my $frontStageLeft_y;
  my $frontStageRight_y;
  my $StageWidth;	#stage width in mm - for now x dimension only
  my $StageWidth_2;	#/2, centre=0
  my $backStageLeft_x;
  my $backStageRight_x;	#horizontal distance of backstage (horizon) corners
  my $backStageLeft_y;
  my $backStageRight_y;	#horizontal distance of backstage (horizon) corners

  my $AspectRatio1 = 2;	#screen client ratio, for assigning height of stage
  my $ProsceniumHeight;	#height above stage front that is visible
  my $maxFontSize;	#600
  my $maxFontSize_2;	#/2
  my $maxFontSize_3;	#/3
  my $maxFontSize_4;	#/4

#  my $ViewDepth;
#  my $ViewHeight; 	#viewer eye relative to stage front and stage floor
  my $viewBox_str;	#the 4 params as a string
  my $viewBox_y1;
  my $viewBox_x2;
  my $viewBox_y2;	#"0 0 24500 13200"
#--- temp params;
  my $x1;
  my $x2;
  my $y1;
  my $y2;
  my $i1;
  my $i2;
  my $s1;

#--- actor: skeleton dimensions ---
  my $joint_radius;	#ball: single size for all joints? (diameter/2)
  my $joint_radius_2;	#maybe for overlap to show the joint? (radius/2)
  my $hip_length;	#triangle from waist to 2 hip_joints: length
  my $hip_width_2;	#width
  my $waist_radius;	#ball: radius (larger than joint_radius)
  my $torso_length;	#chest_length + shoulder_height
  my $shoulder_joint_pos;	#shoulder joint pos in % of shoulder length
  my $chest_length;	#triangle - shoulder_joints, waist
  my $neck_length;	#2nd triangle - shoulder_joints, neck
  my $neck_radius;	#joint atop 2nd triangle to head
  my $left_foot_length;	# length
  my $left_foot_front;	#from ankle to tip of toes
  my $left_foot_heel;	#from ankle to heel
  my $ankle_pos;	#ankle pos in % from toes to heel
  my $right_foot_length;
  my $right_foot_front;
  my $right_foot_heel;
  my $left_lower_leg_length;
  my $right_lower_leg_length;
  my $left_upper_leg_length;
  my $right_upper_leg_length;
  my $shoulder_height;	#upper triangle, shoulder_joints and neck: length
  my $shoulder_width_2;	#width
  my $left_upper_arm_length;
  my $right_upper_arm_length;
  my $left_lower_arm_length;
  my $right_lower_arm_length;
  my $left_hand_length;
#  my $left_hand_width;
#  my $left_hand_depth;
  my $right_hand_length;
#  my $right_hand_width;
#  my $right_hand_depth;
  my $head_length;
  my $head_depth;	#distance from eye to head_length line - from neck
  my $eye_height;
  my $eye_pos;		#eye position in % of head_length, from neck to eye_height
#--- actor: joint rotation angles ---
  my $waist_angle;		#"rotate forward to bring hips (plus knees?) closer to shoulders - or arch back, left to right horizontal axis"
  my $left_hip_joint_angle;	#"left, rot: front"
  my $right_hip_joint_angle;	#"right, rot: front"
  my $left_knee_angle;		#left rot lower leg to back
  my $right_knee_angle;		#right rot lower leg to back
  my $left_ankle_angle;		#foot up toward knee
  my $right_ankle_angle;	#foot up toward knee
  my $left_shoulder_angle;	#upper arm up in front
  my $right_shoulder_angle;	#upper arm up in front
  my $left_elbow_angle;		#lower arm toward upper arm
  my $right_elbow_angle;	#lower arm toward upper arm
  my $left_wrist_angle;		#hand side to side
  my $right_wrist_angle;	#hand side to side
  my $neck_angle;		#head forward toward waist
  my $eye_forward;	#line of sight up or down
  my $eye_sideways;	#line of sight toward right or left

#--- actor: keyTimes for joint rotation angles ---
  my $key_time;	#same for all, due to perspective calculations
  my $lower_body_times = "0; 1";
  my $upper_body_times = "0; 1";
  my $hip_times = "0; 1";
  my $chest_times = "0; 1";
  my $head_times = "0; 1";
#--- actor: keyTimes for position on stage ---
  my $stage_pos_times;

#--- actor: position on stage ---
  my $stage_width_x;	#left & right relative to audience
  my $stage_depth_y;	#front & back relative to audience
  my $stage_direction;	#0=facing audience, increase counterclockwise
  my $stage_height;	#height of waist above stage (usually claculated)
  my $forward_leaning;	#0=vertical (for humans), increases for forward leaning
  my $sideways_leaning;	#0=vertical (for humans), increases for leaning to the right
  my $waist2stage_pos_x;
  my $waist2stage_pos_y;	#relative to the stage

#  my $torso2stage_dir_angle;	#0=facing audience, increase counterclockwise (same keyTimes as width, depth, and height)"
#  my $torso2stage_sideways_angle;	#0=vertical (for humans), increases for leaning to the right"
#  my $waist2stage_height;	#height of waist above stage (same keyTimes as width, depth, and direction)"


#--- basic action cycle -----------
# open a_file, "<c:\inetpub\wwwroot\b_action\action\skill_1.txt" or die "could not open the basic action file skill_1.txt\n";
# while ($line_in1 = <a_file>) {
#    eval $line_in1;
#}
# close(a_file);
#$basic_action="output.txt";
#$fixed_params="fixed_p.txt";
$duration = "5s";
$repeat_count = 2;
#------------------------------------------------------------------------------#
# open fp_file, "<$fixed_params" or die "could not open the file for parameters that are not dependent on the viewing perspective: $fixed_params\n";
# while ($line_in1 = <fp_file>) {
#    eval $line_in1;
#}
# close(fp_file);

 $ankle_pos = 0.70;
 $eye_pos = 0.65;
 $head_length = 250.00;
 $head_depth = 120.00;
 $hip_length = 200.00;
 $hip_width_2 = 170.00;  #1/2 hip width at hip_joint level
 $joint_radius = 50;
 $neck_radius = 110;
 $shoulder_width_2 = 230.00;  #1/2 chest width at shoulder level - chest_length
 $shoulder_joint_pos = 0.85;	#shoulder joint pos in % of chest_length
# $stage_pos_times
 $stage_depth_y = 2000;		#front & back relative to audience (same keyTimes as width, direction, and height)"
 $stage_width_x = 2000;		#left & right relative to audience (same keyTimes as depth, direction, and height)"
# $torso2stage_dir_angle = 45;	#0=facing audience, increase counterclockwise (same keyTimes as width, depth, and height)"
# $torso2stage_sideways_angle = 0;	#0=vertical (for humans), increases for leaning to the right"
# $waist2stage_height = 1000;	#height of waist above stage (same keyTimes as width, depth, and direction)"
 $waist_radius = 100.00;
 $waist2stage_pos_x = 1000;
 $waist2stage_pos_y = 1000;

 $forward_leaning = 0;

# $StageDepth = 10000.00;	#depth of the stage in mm, front to back, front=0"
# $StageWidth = 5000.00;	#width of the stage in mm, left to right, centre=0"
# $ViewDepth = 10000.00;	#audience eye (camera) to centre front of stage in mm"
# $ViewHeight = 2000.00;	#audience eye (camera) above stage floor, in mm"
# $ViewWidth_x = 0.00;	#right & left - parallel to stage-front and stage_width_x"
# $ViewDepth_y = -10000.00;	#front & back at right angles to stage-front, parallel to stage_depth_y"

 $backStageLeft_x = 50;
 $backStageRight_x = 1250;	#horizontal distance of backstage (horizon) corners
 $backStageLeft_y = 50;
 $backStageRight_y = 50;	#horizontal distance of backstage (horizon) corners
 $frontStageLeft_x = 50;
 $frontStageRight_x = 1250;	#horizontal distance of frontstage (horizon) corners
 $frontStageLeft_y = 50;
 $frontStageRight_y = 50;	#horizontal distance of frontstage (horizon) corners

 $ScreenWidth = 1920;	#as set for the Dell monitor
 $ScreenHeight = 1080;


 $torso_length = 530;
 $left_foot_length = 35.662969343026;
 $right_foot_length = 12.2497233425778;

#------------------------------------------------------------------------------#
# open ba_file, "<$basic_action" or die "could not open the read file that presents the basic action from the viewing perspective: $basic_action\n";
# while ($line_in1 = <ba_file>) {
#    eval $line_in1;
#}
# close(ba_file);
$key_time = "0; 120; 340; 560; 760; 880; 1100; 1320; 1520; ";
#$torso_length = "529.993748310915; 529.997034955883; 529.999901690638; 529.998429442351; 529.993748310915; 529.997034955883; 529.999901690638; 529.998429442351; 529.993748310915; ";
#$neck_diameter = "110; 110; 110; 110; 110; 110; 110; 110; 110; ";
#$head_length = "250; 250; 250; 250; 250; 250; 250; 250; 250; ";
$left_upper_arm_length = "354.760562068937; 333.365882322041; 262.695900155361; 161.731745451652; 150.98959189689; 92.9584749060582; 73.2522778648544; 68.8346249653627; 64.2626585329382; ";
$right_upper_arm_length = "354.760562068937; 218.412411030721; 172.11132861338; 161.731745451652; 150.98959189689; 141.883805320983; 111.805964355577; 68.8346249653627; 64.2626585329382; ";
$left_lower_arm_length = "81.8334343675137; 28.0208404267962; 10.1066531726495; 5.65156873198571; 1.77879722654787; 0.994690785799013; 0.35876849633986; 0.122847035124456; 0.0386653645619262; ";
$right_lower_arm_length = "81.8334343675137; 45.7606757649528; 16.5051180427897; 5.65156873198571; 1.77879722654787; 0.609083483077086; 0.219686327136808; 0.122847035124456; 0.0386653645619262; ";
$left_hand_length = "62.9487956673182; 21.5544926359971; 7.77434859434577; 4.34736056306593; 1.36830555888297; 0.765146758306933; 0.275975766415277; 0.0944977193265047; 0.0297425881245587; ";
$right_hand_length = "62.9487956673182; 35.2005198191944; 12.6962446482998; 4.34736056306593; 1.36830555888297; 0.468525756213143; 0.168989482412929; 0.0944977193265047; 0.0297425881245587; ";
$left_upper_leg_length = "263.845160721481; 151.335367035096; 148.028326171549; 139.101125770694; 139.101125770694; 138.910492512226; 137.200273840654; 105.101507370035; 60.2837480524279; ";
$right_upper_leg_length = "432.258605561518; 421.179845504952; 421.179845504952; 417.080952201061; 413.02194904504; 409.002447828711; 379.220466105076; 356.350673649932; 334.860098440929; ";
$left_lower_leg_length = "229.430574540419; 131.595971334866; 128.720283627434; 120.957500670169; 120.957500670169; 120.791732619327; 119.304585948395; 91.3926151043783; 52.4206504803721; ";
$right_lower_leg_length = "400; 399.45181390183; 394.533899220416; 302.230501119862; 173.352293788922; 99.4307909047293; 97.2579895625257; 91.3926151043783; 91.3926151043783; ";
#$left_foot_length = "335.662969343026; 417.294922178433; 94.2012536217329; 77.067600696598; 3.49650661929476; 0.475129802836077; 0.0538333781280448; 0.0349916957832291; 0.0435015426257278; ";
#$right_foot_length = "12.2497233425778; 15.2288092992602; 1.72546164133131; 1.12155006686535; 1.39430676189219; 1.7333968439696; 0.391301564067608; 0.320130269312672; 0.0145241008617228; ";
$waist_angle = "271; 271; 271; 271; 271; 271; 271; 271; 271; ";
$neck_angle = "178; 178; 178; 178; 178; 178; 178; 178; 178; ";
$left_shoulder_angle = "359; 359; 359; 359; 359; 359; 359; 359; 359; ";
$right_shoulder_angle = "359; 359; 359; 359; 359; 359; 359; 359; 359; ";
$left_elbow_angle = "201; 87; 161; 180; 158; 180; 198; 272; 201; ";
$right_elbow_angle = "201; 180; 161; 87; 158; 272; 198; 180; 201; ";
$left_wrist_angle = "201; 87; 161; 180; 158; 180; 198; 272; 201; ";
$right_wrist_angle = "201; 180; 161; 87; 158; 272; 198; 180; 201; ";
$left_hip_joint_angle = "359; 359; 359; 359; 359; 359; 359; 359; 359; ";
$right_hip_joint_angle = "359; 359; 359; 359; 359; 359; 359; 359; 359; ";
$left_knee_angle = "180; 180; 180; 180; 180; 180; 180; 180; 180; ";
$right_knee_angle = "180; 180; 180; 180; 180; 180; 180; 180; 180; ";
$left_ankle_angle = "180; 180; 180; 0; 180; 180; 180; 180; 180; ";
$right_ankle_angle = "180; 180; 180; 180; 180; 180; 180; 0; 180; ";
$eye_forward = "0; 0; 0; 0; 0; 0; 0; 0; 0; ";
#------------------------------------------------------------------------------#
 $chest_length = $torso_length * $shoulder_joint_pos;
 $neck_length = $torso_length - $chest_length;
 $left_foot_front = $left_foot_length * $ankle_pos;
 $left_foot_heel = $left_foot_length - $left_foot_front;
 $right_foot_front = $right_foot_length * $ankle_pos;
 $right_foot_heel = $right_foot_length - $right_foot_front;
 $eye_height = $head_length * $eye_pos;

#------------------------------------------------------------------------------#
#  Note: Valid parameters for the cookie() call include:
#  -----------------------------------------------------
#
#	-domain		a partial or complete domain name for which the
#				cookie is valid. Like '.devdaily.com'
#				for the entire domain (www.devdaily.com,
#				smtp.devdaily.com, etc.), or 'www.devdaily.com'
#				to refer to one server.
#	-expires	(optional) The expiration date for this cookie.
#				+60s	60 seconds from now
#				+20m	20 minutes from now
#				+5h	5 hours from now
#				+1d	1 day from now
#				-3d	3 days ago
#				now	immediately
#				+6M	6 months from now
#				+1y	1 year from now
#				Monday, 28-Dec-98 12:30:00 GMT
#					expire at this specific date & time
#	-name		the name of the cookie (req'd)
#	-path		(optional) If you specify this attribute, the 
#				browser will check it against your script's URL
#				before returning the cookie.
#				The default is set to '/'.
#	-secure		(optional) The cookie will only be sent if the CGI
#				request is occurring on a secure channel.
#	-value		(req'd) The value of your cookie. This can be a 
#				scalar value, array reference, or hash
#				reference.
#
#$cookie = $cgi->cookie(-name=>'MY_COOKIE',
#			 -value=>'BEST_COOKIE=chocolatechip',
#			 -expires=>'+4h',
#			 -path=>'/');
#------------------------------------------------------------------------------#


#  $ScreenWidth =  $cgi->cookie(-domain=>'Humble',-name=>'ScreenWidth');
#  $ScreenHeight = $cgi->cookie(-domain=>'Humble',-name=>'ScreenHeight');
#  $AspectRatio1 = $ScreenWidth / $ScreenHeight;
#print l_file "Monitor: $ScreenWidth, $ScreenHeight, $AspectRatio1\n";
#--- stage and audience (viewer) ---

#  $key_time;	#same for all, due to perspective calculations
  $lower_body_times = $key_time;
  $upper_body_times = $key_time;
  $hip_times = $key_time;
  $chest_times = $key_time;
  $head_times = $key_time;
  $stage_pos_times = $key_time;

  #stage data: StageDepth, StageWidth, ViewDepth, ViewHeight
  $x1 = (($backStageRight_x - $backStageLeft_x) / 10);
 my $backStage_1x = $backStageLeft_x + $x1;
 my $backStage_2x = $backStageLeft_x + 2*$x1;
 my $backStage_3x = $backStageLeft_x + 3*$x1;
 my $backStage_4x = $backStageLeft_x + 4*$x1;
 my $backStage_5x = $backStageLeft_x + 5*$x1;
 my $backStage_6x = $backStageLeft_x + 6*$x1;
 my $backStage_7x = $backStageLeft_x + 7*$x1;
 my $backStage_8x = $backStageLeft_x + 8*$x1;
 my $backStage_9x = $backStageLeft_x + 9*$x1;
  $y1 = (($backStageRight_y - $backStageLeft_y) / 10);
 my $backStage_1y = $backStageLeft_y + $y1;
 my $backStage_2y = $backStageLeft_y + 2*$y1;
 my $backStage_3y = $backStageLeft_y + 3*$y1;
 my $backStage_4y = $backStageLeft_y + 4*$y1;
 my $backStage_5y = $backStageLeft_y + 5*$y1;
 my $backStage_6y = $backStageLeft_y + 6*$y1;
 my $backStage_7y = $backStageLeft_y + 7*$y1;
 my $backStage_8y = $backStageLeft_y + 8*$y1;
 my $backStage_9y = $backStageLeft_y + 9*$y1;

  $x1 = (($frontStageRight_x - $frontStageLeft_x) / 10);
 my $frontStage_1x = $frontStageLeft_x + $x1;
 my $frontStage_2x = $frontStageLeft_x + 2*$x1;
 my $frontStage_3x = $frontStageLeft_x + 3*$x1;
 my $frontStage_4x = $frontStageLeft_x + 4*$x1;
 my $frontStage_5x = $frontStageLeft_x + 5*$x1;
 my $frontStage_6x = $frontStageLeft_x + 6*$x1;
 my $frontStage_7x = $frontStageLeft_x + 7*$x1;
 my $frontStage_8x = $frontStageLeft_x + 8*$x1;
 my $frontStage_9x = $frontStageLeft_x + 9*$x1;
  $y1 = (($frontStageRight_y - $frontStageLeft_y) / 10);
 my $frontStage_1y = $frontStageLeft_y + $y1;
 my $frontStage_2y = $frontStageLeft_y + 2*$y1;
 my $frontStage_3y = $frontStageLeft_y + 3*$y1;
 my $frontStage_4y = $frontStageLeft_y + 4*$y1;
 my $frontStage_5y = $frontStageLeft_y + 5*$y1;
 my $frontStage_6y = $frontStageLeft_y + 6*$y1;
 my $frontStage_7y = $frontStageLeft_y + 7*$y1;
 my $frontStage_8y = $frontStageLeft_y + 8*$y1;
 my $frontStage_9y = $frontStageLeft_y + 9*$y1;


 $StageWidth = ($frontStageRight_x - $frontStageLeft_x);
 $StageWidth_2 = $StageWidth / 2;

#--- svg display dimensions ---
 $viewBox_x2 = 2450; #$StageWidth;	#24500?	right margin 
 $viewBox_y1 = 0;		#minimum y coordinate - top of screen
 $viewBox_y2 = 5000; #$viewBox_x2 / $AspectRatio1; #13200=bottom of screen - has to be inverted for stage-floor=0
 $viewBox_str = "0 0 $viewBox_x2 $viewBox_y2";

# use mm as dimensions, using the same scale for horizontal and vertical
# use 1mm per graphing unit, 1000 units per m 
# --- stage dimensions -- proscenium opening ---
  $maxFontSize = $StageWidth / 40;
  $maxFontSize_2 = $maxFontSize/2;	#/2
  $maxFontSize_3 = $maxFontSize/3;	#/3
  $maxFontSize_4 = $maxFontSize/4;	#/4
#--- y dimension -- height of the stage plus room for headers and footers
  $ProsceniumHeight = $StageWidth / $AspectRatio1;
  $y1 = $viewBox_y2 - $ProsceniumHeight;
  $y2 = $y1 / 4;
#  $StageFrontFloor = $ProsceniumHeight + $y2*2;

#----------------------------------------------------------------------

#--- pose of actor, relative to the waist ---
#--- pose with joint rotation angles radiating out from the waist ------


#print b_file "Content-Type: image/svg+xml\n\n";
print "Content-Type: image/svg+xml\n\n";
#print b_file <<EOF;
print <<EOF;
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Copyright&copy; 2005. Task Re-engineering Inc. All rights reserved. -->
<?xml-stylesheet href="/styles/SVG.css" type="text/css" ?>


<svg width="$ScreenWidth" height="$ScreenHeight"
 viewBox="$viewBox_str"
 version="1.1"
 xmlns="http://www.w3.org/2000/svg"
 xmlns:xlink="http://www.w3.org/1999/xlink"
 xmlns:ev="http://www.w3.org/2001/xml-events"
 preserveAspectRatio="xMinYMin slice">

<!-- *** matrix y inversion to standard Cartesian *** -->

  <!-- *** horizon and side lines for stage front and rear *** -->
  <g>
    <line x1="$frontStageLeft_x" y1="$frontStageLeft_y" x2="$frontStageRight_x" y2="$frontStageRight_y" 
	style="stroke:red;stroke-width:3;stroke-opacity:.5;" />
    <line x1="$backStageLeft_x" y1="$backStageLeft_y" x2="$backStageRight_x" y2="$backStageRight_y" 
	style="stroke:red;stroke-width:3;stroke-opacity:.5;" />
    <line x1="$frontStageLeft_x" y1="$frontStageLeft_y" x2="$backStageLeft_x" y2="$backStageLeft_y" 
	style="stroke:red;stroke-width:3;stroke-opacity:.5;" />
    <line x1="$frontStageRight_x" y1="$frontStageRight_y" x2="$backStageRight_x" y2="$backStageRight_y" 
	style="stroke:red;stroke-width:3;stroke-opacity:.5;" />
  </g>

  <!-- *** render stage lines front to rear *** -->
  <g>
    <line x1="$frontStage_1x" y1="$frontStage_1y" x2="$backStage_1x" y2="$backStage_1y" 
	style="fill:green;stroke:blue;stroke-width:1;fill-opacity:.2;"/>
    <line x1="$frontStage_2x" y1="$frontStage_2y" x2="$backStage_2x" y2="$backStage_2y" 
	style="fill:green;stroke:blue;stroke-width:1;fill-opacity:.2;"/>
    <line x1="$frontStage_3x" y1="$frontStage_3y" x2="$backStage_3x" y2="$backStage_3y" 
	style="fill:green;stroke:blue;stroke-width:1;fill-opacity:.2;"/>
    <line x1="$frontStage_4x" y1="$frontStage_4y" x2="$backStage_4x" y2="$backStage_4y" 
	style="fill:green;stroke:blue;stroke-width:1;fill-opacity:.2;"/>
    <line x1="$frontStage_5x" y1="$frontStage_5y" x2="$backStage_5x" y2="$backStage_5y" 
	style="fill:green;stroke:blue;stroke-width:1;fill-opacity:.2;"/>
    <line x1="$frontStage_6x" y1="$frontStage_6y" x2="$backStage_6x" y2="$backStage_6y" 
	style="fill:green;stroke:blue;stroke-width:1;fill-opacity:.2;"/>
    <line x1="$frontStage_7x" y1="$frontStage_7y" x2="$backStage_7x" y2="$backStage_7y" 
	style="fill:green;stroke:blue;stroke-width:1;fill-opacity:.2;"/>
    <line x1="$frontStage_8x" y1="$frontStage_8y" x2="$backStage_8x" y2="$backStage_8y" 
	style="fill:green;stroke:blue;stroke-width:1;fill-opacity:.2;"/>
    <line x1="$frontStage_9x" y1="$frontStage_9y" x2="$backStage_9x" y2="$backStage_9y" 
	style="fill:green;stroke:blue;stroke-width:1;fill-opacity:.2;"/>
  </g>

  <!-- *** render actor position on stage *** -->

EOF

# the graphing starts from the waist (centre of gravity)
# 5 layers, successively nested
# 1 ----- waist, using stage_height and forward_leaning, sideways leaning
# 2 ----- hips and shoulders, waist rotations (forward, sideways, twist)
# 3 ----- upper legs and arms
# 4 ----- lower legs and arms
# 5 ----- feet and hands

#print b_file <<EOF;
print <<EOF;

<!--1 *** waist, centered on position on stage *** -->
<g transform="translate($waist2stage_pos_x,$waist2stage_pos_y)">
 <circle cx="0" cy="0" r="$waist_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
<!-- rotate about position-pole forward_leaning *** -->
<g>
<animateTransform attributeName="transform" type="rotate"
 keyTimes="$stage_pos_times" values="$forward_leaning"
 dur="$duration" repeatCount="$repeat_count"
 calcMode="linear" fill="freeze" attributeType="XML" />

  <!--2a *** hips, do not rotate about waist - rotate chest instead (2c) *** -->

    <!--3a *** obscured (back) upper right leg: shift to bottom edge of the hip, then rotate at the hip-joint *** -->
    <g transform="translate(-$hip_width_2,$hip_length)">
    <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
    <g>
    <animateTransform attributeName="transform" type="rotate"
     keyTimes="$lower_body_times" values="$right_hip_joint_angle"
     dur="$duration" repeatCount="$repeat_count"
     calcMode="linear" fill="freeze" attributeType="XML" />
      <g>
      <line x1="0" y1="0" x2="0" style="stroke:black;fill:brown;fill-opacity:.3;">
       <animate attributeName="y2"
        keyTimes="$lower_body_times" values="$right_upper_leg_length"
        dur="$duration" repeatCount="$repeat_count"
        calcMode="linear" fill="freeze" attributeType="XML" />
      </line>

      <!--4a *** obscured (back) lower right leg: shift to bottom edge of the upper leg, then rotate at the knee-joint *** -->
      <g transform="translate(0,$right_upper_leg_length)">
      <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
      <g>
      <animateTransform attributeName="transform" type="rotate"
       keyTimes="$lower_body_times" values="$right_knee_angle"
       dur="$duration" repeatCount="$repeat_count"
       calcMode="linear" fill="freeze" attributeType="XML" />
        <g>
        <line x1="0" y1="0" x2="0" style="stroke:black;fill:brown;fill-opacity:.3;">
         <animate attributeName="y2"
          keyTimes="$lower_body_times" values="$right_upper_leg_length"
          dur="$duration" repeatCount="$repeat_count"
          calcMode="linear" fill="freeze" attributeType="XML" />
        </line>

        <!--5a *** obscured (back) right foot: shift to bottom edge of the lower leg, then rotate at the ankle-joint *** -->
        <g transform="translate(0,$right_lower_leg_length)">
        <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
        <g>
        <animateTransform attributeName="transform" type="rotate"
         keyTimes="$lower_body_times" values="$right_ankle_angle"
         dur="$duration" repeatCount="$repeat_count"
         calcMode="linear" fill="freeze" attributeType="XML" />
          <g>
          <line x1="0" y1="0" x2="$right_foot_length" y2="0" style="stroke:black;fill:brown;fill-opacity:.3;">
           <animate attributeName="y2"
            keyTimes="$lower_body_times" values="$right_upper_leg_length"
            dur="$duration" repeatCount="$repeat_count"
            calcMode="linear" fill="freeze" attributeType="XML" />
          </line>

        </g></g></g><!--/5a *** obscured right foot *** -->
      </g></g></g><!--/4a *** obscured lower leg *** -->
    </g></g></g><!--/3a *** obscured upper leg *** -->

  <!--2a *** hips *** -->
   <polygon points="0,0 -$hip_width_2,$hip_length $hip_width_2,$hip_length" style="stroke:black;fill:blue;fill-opacity:.3;" />

    <!--3b *** front upper right leg: shift to bottom edge of the hip, then rotate at the hip-joint *** -->
    <g transform="translate($hip_width_2,$hip_length)">
    <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
    <g>
    <animateTransform attributeName="transform" type="rotate"
     keyTimes="$lower_body_times" values="$left_hip_joint_angle"
     dur="$duration" repeatCount="$repeat_count"
     calcMode="linear" fill="freeze" attributeType="XML" />
      <g>
      <line x1="0" y1="0" x2="0" style="stroke:black;fill:brown;fill-opacity:.3;">
       <animate attributeName="y2"
        keyTimes="$lower_body_times" values="$right_upper_leg_length"
        dur="$duration" repeatCount="$repeat_count"
        calcMode="linear" fill="freeze" attributeType="XML" />
      </line>

      <!--4b *** front lower right leg: shift to bottom edge of the upper leg, then rotate at the knee-joint *** -->
      <g transform="translate(0,$left_upper_leg_length)">
      <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
      <g>
      <animateTransform attributeName="transform" type="rotate"
       keyTimes="$lower_body_times" values="$left_knee_angle"
       dur="$duration" repeatCount="$repeat_count"
       calcMode="linear" fill="freeze" attributeType="XML" />
        <g>
        <line x1="0" y1="0" x2="0" y2="$left_lower_leg_length" style="stroke:black;fill:brown;fill-opacity:.3;">
         <animate attributeName="y2"
          keyTimes="$lower_body_times" values="$right_upper_leg_length"
          dur="$duration" repeatCount="$repeat_count"
          calcMode="linear" fill="freeze" attributeType="XML" />
        </line>

        <!--5b *** front right foot: shift to bottom edge of the lower leg, then rotate at the ankle-joint *** -->
        <g transform="translate(0,$left_lower_leg_length)">
        <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
        <g>
        <animateTransform attributeName="transform" type="rotate"
         keyTimes="$lower_body_times" values="$left_ankle_angle"
         dur="$duration" repeatCount="$repeat_count"
         calcMode="linear" fill="freeze" attributeType="XML" />
          <g>
          <line x1="-$left_foot_length" y1="0" x2="0" y2="0" style="stroke:black;fill:brown;fill-opacity:.3;">
           <animate attributeName="y2"
            keyTimes="$lower_body_times" values="$right_upper_leg_length"
            dur="$duration" repeatCount="$repeat_count"
            calcMode="linear" fill="freeze" attributeType="XML" />
          </line>

        </g></g></g><!--/5b *** front right foot *** -->
      </g></g></g><!--/4b *** front lower leg *** -->
    </g></g></g><!--/3b *** front upper leg *** -->
  <!--/2a *** hips *** -->

  <!--2c *** chest-shoulders, rotate about waist, using waist_angle *** -->
  <g>
  <animateTransform attributeName="transform" type="rotate"
   keyTimes="$chest_times" values="$waist_angle"
   dur="$duration" repeatCount="$repeat_count"
   calcMode="linear" fill="freeze" attributeType="XML" />

    <!--3c *** obscured (back) upper right arm: shift to line between the shoulder, then rotate at the shoulder-joint *** -->
    <g transform="translate(-$shoulder_width_2,-$chest_length)">
    <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
    <g>
    <animateTransform attributeName="transform" type="rotate"
     keyTimes="$upper_body_times" values="$right_shoulder_angle"
     dur="$duration" repeatCount="$repeat_count"
     calcMode="linear" fill="freeze" attributeType="XML" />
      <g>
      <line x1="0" y1="0" x2="0" y2="$right_upper_arm_length" style="stroke:black;fill:brown;fill-opacity:.3;">
       <animate attributeName="y2"
        keyTimes="$lower_body_times" values="$right_upper_leg_length"
        dur="$duration" repeatCount="$repeat_count"
        calcMode="linear" fill="freeze" attributeType="XML" />
      </line>

      <!--4c *** obscured (back) lower right arm: shift to bottom edge of the upper arm, then rotate at the elbow-joint *** -->
      <g transform="translate(0,$right_upper_arm_length)">
      <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
      <g>
      <animateTransform attributeName="transform" type="rotate"
       keyTimes="$upper_body_times" values="$right_elbow_angle"
       dur="$duration" repeatCount="$repeat_count"
       calcMode="linear" fill="freeze" attributeType="XML" />
        <g>
        <line x1="0" y1="0" x2="0" y2="$right_lower_arm_length" style="stroke:black;fill:brown;fill-opacity:.3;">
         <animate attributeName="y2"
          keyTimes="$lower_body_times" values="$right_upper_leg_length"
          dur="$duration" repeatCount="$repeat_count"
          calcMode="linear" fill="freeze" attributeType="XML" />
        </line>

        <!--5c *** obscured (back) right hand: shift to bottom edge of the lower arm, then rotate at the wrist-joint *** -->
        <g transform="translate(0,$right_lower_arm_length)">
        <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
        <g>
        <animateTransform attributeName="transform" type="rotate"
         keyTimes="$upper_body_times" values="$right_wrist_angle"
         dur="$duration" repeatCount="$repeat_count"
         calcMode="linear" fill="freeze" attributeType="XML" />
          <g>
          <line x1="0" y1="0" x2="0" y2="$right_hand_length" style="stroke:black;fill:brown;fill-opacity:.3;">
           <animate attributeName="y2"
            keyTimes="$lower_body_times" values="$right_upper_leg_length"
            dur="$duration" repeatCount="$repeat_count"
            calcMode="linear" fill="freeze" attributeType="XML" />
          </line>

        </g></g></g><!--/5c *** obscured right hand *** -->
      </g></g></g><!--/4c *** obscured lower arm *** -->
    </g></g></g><!--/3c *** obscured upper arm *** -->

  <!--2c *** shoulders *** -->
   <polygon points="0,0 -$shoulder_width_2,chest_length, 0,torso_length $shoulder_width_2,chest_length" style="stroke:black;fill:blue;fill-opacity:.3;" />

    <!--3d *** front upper left arm: shift to top edge of the shoulder, then rotate at the shoulder-joint *** -->
    <g transform="translate($shoulder_width_2,-$chest_length)">
    <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
    <g>
    <animateTransform attributeName="transform" type="rotate"
     keyTimes="$upper_body_times" values="$left_shoulder_angle"
     dur="$duration" repeatCount="$repeat_count"
     calcMode="linear" fill="freeze" attributeType="XML" />
      <g>
      <line x1="0" y1="0" x2="0" y2="$left_upper_arm_length" style="stroke:black;fill:brown;fill-opacity:.3;">
       <animate attributeName="y2"
        keyTimes="$lower_body_times" values="$right_upper_leg_length"
        dur="$duration" repeatCount="$repeat_count"
        calcMode="linear" fill="freeze" attributeType="XML" />
      </line>

      <!--4d *** front lower left arm: shift to bottom edge of the upper arm, then rotate at the elbow-joint *** -->
      <g transform="translate(0,$left_upper_arm_length)">
      <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
      <g>
      <animateTransform attributeName="transform" type="rotate"
       keyTimes="$upper_body_times" values="$left_elbow_angle"
       dur="$duration" repeatCount="$repeat_count"
       calcMode="linear" fill="freeze" attributeType="XML" />
        <g>
        <line x1="0" y1="0" x2="$joint_radius" y2="$left_lower_arm_length" style="stroke:black;fill:brown;fill-opacity:.3;">
         <animate attributeName="y2"
          keyTimes="$lower_body_times" values="$right_upper_leg_length"
          dur="$duration" repeatCount="$repeat_count"
          calcMode="linear" fill="freeze" attributeType="XML" />
        </line>

        <!--5d *** front left hand: shift to bottom edge of the lower arm, then rotate at the wrist-joint *** -->
        <g transform="translate(0,$left_lower_arm_length)">
        <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
        <g>
        <animateTransform attributeName="transform" type="rotate"
         keyTimes="$upper_body_times" values="$left_wrist_angle"
         dur="$duration" repeatCount="$repeat_count"
         calcMode="linear" fill="freeze" attributeType="XML" />
          <g>
          <line x1="0" y1="0" x2="0" y2="$left_hand_length" style="stroke:black;fill:brown;fill-opacity:.3;">
           <animate attributeName="y2"
            keyTimes="$lower_body_times" values="$right_upper_leg_length"
            dur="$duration" repeatCount="$repeat_count"
            calcMode="linear" fill="freeze" attributeType="XML" />
          </line>

        </g></g></g><!--/5d *** front left hand *** -->
      </g></g></g><!--/4d *** front lower arm *** -->
    </g></g></g><!--/3d *** front upper arm *** -->

    <!--3e *** head on neck: shift to top edge of the shoulder, then rotate at the neck *** -->
    <g transform="translate(0,-$torso_length)">
    <circle cx="0" cy="0" r="$neck_radius" style="stroke:black;fill:yellow;fill-opacity:.3;" />
    <g>
    <animateTransform attributeName="transform" type="rotate"
     keyTimes="$upper_body_times" values="$neck_angle"
     dur="$duration" repeatCount="$repeat_count"
     calcMode="linear" fill="freeze" attributeType="XML" />
    <g transform="translate($joint_radius,-$joint_radius/4)">
     <polygon points="0,0 -$head_depth," y="-$head_length" width="$head_depth" height="$head_length" style="stroke:black;fill:brown;fill-opacity:.3;" />

      <!--4e *** single eye: shift to front of head, position below top, then rotate the eye for viewpoint of eyes *** -->
      <g transform="translate(-$head_depth,-$eye_height)">
      <circle cx="0" cy="0" r="$joint_radius" style="stroke:black;fill:red;fill-opacity:.7;" />
      <g>
      <animateTransform attributeName="transform" type="rotate"
       keyTimes="$head_times" values="$eye_forward"
       dur="$duration" repeatCount="$repeat_count"
       calcMode="linear" fill="freeze" attributeType="XML" />
       <line x1="0" y1="0" x2="-800" y2="0" stroke="red" />
EOF
#print b_file "       <text x=\"-800\" y=\"-20\" fill=\"black\" font-size=\"";
print "       <text x=\"-800\" y=\"-20\" fill=\"black\" font-size=\"";
$s1=$maxFontSize/4;
#print b_file "$s1 px\">line of sight</text>zz";
print "$s1 px\">line of sight</text>zz";
#print b_file <<EOF;
print <<EOF;

      </g></g><!--/4e *** eye *** -->
    </g></g></g><!--/3e *** head on neck *** -->

  </g><!--/2c *** shoulders *** -->

</g></g><!--/1 *** waist *** -->

<!-- *** matrix y inversion to standard Cartesian *** -->
</svg>



EOF

close STDERR;
#close(b_file);
#close(log_file);

