/*
 * hyperbolaMovie.c
 *
 * Generate the script for the wise movie. 
 *  This proram generates two files: 
 *        hyperbola.wf:  Generates a flight path file that Partiview can read.
 *                       Format: x y z Rx Ry Rz fov
 *
 *        hyperbola.sh:  The shell script that is run in Partiview via the command 'async ./spiral.sh'
 *                       This file can have any valid Partiview commands. 
 *
 * Originally programmed by Brian Abbott
 * Heavily modified by Ryan Wyatt
 *
 */

#include <stdio.h>
#include <math.h>

#define PI 3.14159
#define DEGTORAD PI/180.0
#define RADTODEG 180.0/PI

int main(void)
{
	char tFilename[36]="./Hyperbola/t_filename";
	char lFilename[36]="./Hyperbola/l_filename";
	char fFilename[36]="./Hyperbola/f_filename";
	char rFilename[36]="./Hyperbola/r_filename";
	char bFilename[36]="./Hyperbola/b_filename";
	float almostTotal, frame, nearest, total;
	float xinc, xincLast, xstart;
	float x=0.0, y=0.0, z=0.0, fov=50.0;
	float rx=0.0, ry=0.0, rz=0.0;
	float factor;
	//float angle, r, a, xoffset;
	FILE *out, *outwf, *outsh;

	outwf = fopen("hyperbola.wf", "w");
	outsh = fopen("hyperbola.sh", "w");
	//printf("echo snapset -n 57 %s\n", filename);
	//fprintf(outsh, "echo snapset -n 57 %s\n", filename);

	/* Fly hyperbola through the data set */
	/* where xy = (nearest approach to origin)^2 */
	nearest = 1200.0;

	/* Total number of frames */
	/* 24s x 30fps = 900f */
	total = 900.0;
	/* Begin braking at this frame */
	almostTotal = 860.0;
	
	/* Set inital position along the x axis */
	xstart = 100000.0;
	x = xstart;
	xinc = 4.5 * x / total ;

	frame = 0;

	while(frame < total) {

		y = nearest*nearest/x;
		z = 0.00;

		rx = -1.0 * RADTODEG * atan2(y,x);
		ry = 90.0;
		rz = 90.0;
		
		fprintf(outwf, "%8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.1f\n", x, y, z, rx, ry, rz, fov);
		
		fprintf(outsh, "echo jump %8.6f %8.6f %8.6f %8.6f %8.6f %8.6f %8.1f\n", x, y, z, rx, ry, rz, fov);
		fprintf(outsh, "echo update\n");
		fprintf(outsh, "echo subcam  bc0    0  90  0 45 45 45 45 1.0\n");
		fprintf(outsh, "echo snapshot -n %4.0f %s\n", frame, tFilename);
		fprintf(outsh, "echo subcam Êbc2   90   0  0 45 45 45 45 1.0\n");
		fprintf(outsh, "echo snapshot -n %4.0f %s\n", frame, lFilename);
		fprintf(outsh, "echo subcam Êbc1    0   0  0 45 45 45 45 1.0\n");
		fprintf(outsh, "echo snapshot -n %4.0f %s\n", frame, fFilename);
		fprintf(outsh, "echo subcam Êbc4  270 Ê 0 Ê0 45 45Ê45Ê45Ê1.0\n");
		fprintf(outsh, "echo snapshot -n %4.0f %s\n", frame, rFilename);
		fprintf(outsh, "echo subcam Êbc3 Ê180  Ê0 Ê0 45 45Ê45Ê45Ê1.0\n"); 
		fprintf(outsh, "echo snapshot -n %4.0f %s\n", frame, bFilename);

		frame = frame + 1;
		if (frame < almostTotal) {
			xinc = 4.5 * (sqrt(x*x+y*y)) / total;
			xincLast = xinc;
			} else {
			xinc = xincLast * ((total-frame)/(total-almostTotal));
		}
		x = x - xinc;

		}

	fclose(outsh);
	fclose(outwf);
	return(0);
}
