<commit_before>#include "Camera.h"

Camera::Camera(ifstream &f)
{
	this->grayscale = false;
	this->isLight = false;
	this->isVisible = false;
	this->hasTexture = false;
	this->aa = NO_AA;
	this->imageHeight = 240;
	this->imageWidth = 320;
	this->name = "";
	while(!f.eof())
	{
		string line;
		getline(f,line);

		queue<string> lineContents;
		explode(line," ",&lineContents);
		
		if(lineContents.size() == 0) continue;
		string word = lineContents.front();
		lineContents.pop();

		if(word[0] == '#' || line[0] == '\n' || line[0] == '\r') continue;

		//words with three arguments
		if(word == "origin" || word == "direction" || word == "up")
		{
			double num1 = 0;
			double num2 = 0;
			double num3 = 0;

			if(lineContents.size() < 3) break;

			num1 = (double)atof(lineContents.front().c_str());
			lineContents.pop();
			num2 = (double)atof(lineContents.front().c_str());
			lineContents.pop();
			num3 = (double)atof(lineContents.front().c_str());
			lineContents.pop();

			if(word == "origin") //read in the origin coordinates
			{
				this->origin.x = num1;
				this->origin.y = num2;
				this->origin.z = num3;
			}
			else if(word == "direction")
			{
				this->direction.x = num1;
				this->direction.y = num2;
				this->direction.z = num3;
			}
			else if(word == "up")
			{
				this->up.x = num1;
				this->up.y = num2;
				this->up.z = num3;
			}
		}

		//words with two arguments
		else if(word == "z" || word == "imagesize")
		{
			double num1 = 0;
			double num2 = 0;

			if(lineContents.size() < 2) break;

			num1 = (double)atof(lineContents.front().c_str());
			lineContents.pop();
			num2 = (double)atof(lineContents.front().c_str());
			lineContents.pop();

			if(word == "z") {
				this->zmin = num1;
				this->zmax = num2;
			}
			else if (word == "imagesize") {
				this->imageWidth = (int)num1;
				this->imageHeight = (int)num2;
			}
		}

		//words with one argument
		else if(word == "width" || word == "perspective" || word == "grayscale" 
				|| word == "aa"	|| word == "name")
		{
			string sNum;
			double num = 0;

			if(lineContents.size() < 1) break;

			sNum = lineContents.front();
			lineContents.pop();
			bool bool1 = (sNum[0] != '0') ? true : false;
			num = (double)atof(sNum.c_str());

			if(word == "width")				this->width = num;
			else if(word == "perspective")	this->perspective = bool1;
			else if(word == "grayscale")	this->grayscale = bool1;
			else if(word == "name")			this->name = sNum;
			else if(word == "aa")
			{
				if(sNum == "none") this->aa = NO_AA;
				else if(sNum == "fsaa4") this->aa = FSAA_4;
				else if(sNum == "fsaa16") this->aa = FSAA_16;
				else if(sNum == "naive-average") this->aa = NAIVE_AVERAGE;
			}
		}
		else break;
	}
	if(this->perspective && this->zmin == 0)
		printf("Warning: Perspective camera with a zmin of zero.\n");
}

void Camera::renderScene(string filename, int cameraNum) {
	BMP image;
	image.SetSize(this->imageWidth,this->imageHeight);
    image.SetBitDepth(32);
	int numSamples;
	if(aa == FSAA_4) numSamples = 4;
	else if(aa == FSAA_16) numSamples = 16;
	else numSamples = 1;
	int sqrtNumSamples = (int)sqrt((float)numSamples);
	double aspect = (double)imageHeight/(double)imageWidth;

	double width = this->width;
	double height = this->width * aspect;
	double width2 = width/2.f;
	double height2 = height/2.f;
	double deltawidth = 1/(imageWidth * sqrtNumSamples);
	double deltaheight = 1/(imageHeight * sqrtNumSamples);

	double gapWidth2 = 1.f/((double)imageWidth)/2.f;
	double leftGap = 1.f/(imageWidth * sqrtNumSamples*2); //Gap between edge of "square" and leftmost sample.
	double gapBetweenSamples = 2*leftGap;

	for(int y = 0; y < this->imageHeight; y++)
	{
		for(int x = 0; x < this->imageWidth; x++)
		{
			bool isPerspective = this->perspective; //False if orthogonal, true if perspective

			double uc = -1*width2 + width*x/imageWidth;
			double vr = -1*height2 + height*y/imageHeight;

			norm(this->direction);
			norm(this->up);

			Vector vLeft = cross3(this->direction,this->up); 

			//Point on image plane
			Point pPointOnImagePlane = this->origin + this->zmin * this->direction + uc * vLeft + vr * this->up;
			
			Vector vCamToImagePlane; //Vector from the camera to the image plane

			if(isPerspective) //If camera is perspective
			{
				vCamToImagePlane = pPointOnImagePlane - this->origin;
			}
			else //If camera is orthogonal
			{
				vCamToImagePlane = this->direction;
			}
			//vCamToImagePlane = c->direction;

			Ray* r = new Ray(); //Ray from the camera to the image plane
			r->dir = vCamToImagePlane;
			
			if(isPerspective)
				r->start = this->origin;
			else
				r->start = pPointOnImagePlane;

			bool lightT = false;

			Color col;
			if(aa == FSAA_4 || aa == FSAA_16) {
				Color* colors = new Color[numSamples];
				for(int x2 = 0; x2 < sqrtNumSamples; x2++) {
					double uc2 = uc - gapWidth2 + leftGap + (x2-1)*gapBetweenSamples;
					for(int y2 = 0; y2 < sqrtNumSamples; y2++) {
						double vr2 = vr - gapWidth2 + leftGap + (y2-1)*gapBetweenSamples;
						pPointOnImagePlane = this->origin + this->zmin * this->direction + uc2 * vLeft + vr2 * this->up;
						if(isPerspective) r->dir = pPointOnImagePlane - this->origin;
						if(!isPerspective) r->start = pPointOnImagePlane;
						colors[x2*sqrtNumSamples + y2] = raytrace(r,lightT);
						//printf("uc = %f, vr = %f\n",uc2,vr2);
					}
				}
				col = Color::averageValues(colors,numSamples);
				delete colors;
			}
			else col = raytrace(r,lightT);
			if(DIAGNOSTIC_STATUS == IS_LIT) col.adjustColorForDiagnosticIsLit();<commit_msg>Made some syntax clearer<commit_after>#include "Camera.h"

Camera::Camera(ifstream &f)
{
	this->grayscale = false;
	this->isLight = false;
	this->isVisible = false;
	this->hasTexture = false;
	this->aa = NO_AA;
	this->imageHeight = 240;
	this->imageWidth = 320;
	this->name = "";
	while(!f.eof())
	{
		string line;
		getline(f,line);

		queue<string> lineContents;
		explode(line," ",&lineContents);
		
		if(lineContents.size() == 0) continue;
		string word = lineContents.front();
		lineContents.pop();

		if(word[0] == '#' || line[0] == '\n' || line[0] == '\r') continue;

		//words with three arguments
		if(word == "origin" || word == "direction" || word == "up")
		{
			double num1 = 0;
			double num2 = 0;
			double num3 = 0;

			if(lineContents.size() < 3) break;

			num1 = (double)atof(lineContents.front().c_str());
			lineContents.pop();
			num2 = (double)atof(lineContents.front().c_str());
			lineContents.pop();
			num3 = (double)atof(lineContents.front().c_str());
			lineContents.pop();

			if(word == "origin") //read in the origin coordinates
			{
				this->origin.x = num1;
				this->origin.y = num2;
				this->origin.z = num3;
			}
			else if(word == "direction")
			{
				this->direction.x = num1;
				this->direction.y = num2;
				this->direction.z = num3;
			}
			else if(word == "up")
			{
				this->up.x = num1;
				this->up.y = num2;
				this->up.z = num3;
			}
		}

		//words with two arguments
		else if(word == "z" || word == "imagesize")
		{
			double num1 = 0;
			double num2 = 0;

			if(lineContents.size() < 2) break;

			num1 = (double)atof(lineContents.front().c_str());
			lineContents.pop();
			num2 = (double)atof(lineContents.front().c_str());
			lineContents.pop();

			if(word == "z") {
				this->zmin = num1;
				this->zmax = num2;
			}
			else if (word == "imagesize") {
				this->imageWidth = (int)num1;
				this->imageHeight = (int)num2;
			}
		}

		//words with one argument
		else if(word == "width" || word == "perspective" || word == "grayscale" 
				|| word == "aa"	|| word == "name")
		{
			string sNum;
			double num = 0;

			if(lineContents.size() < 1) break;

			sNum = lineContents.front();
			lineContents.pop();
			bool bool1 = (sNum[0] != '0') ? true : false;
			num = (double)atof(sNum.c_str());

			if(word == "width")				this->width = num;
			else if(word == "perspective")	this->perspective = bool1;
			else if(word == "grayscale")	this->grayscale = bool1;
			else if(word == "name")			this->name = sNum;
			else if(word == "aa")
			{
				if(sNum == "none") this->aa = NO_AA;
				else if(sNum == "fsaa4") this->aa = FSAA_4;
				else if(sNum == "fsaa16") this->aa = FSAA_16;
				else if(sNum == "naive-average") this->aa = NAIVE_AVERAGE;
			}
		}
		else break;
	}
	if(this->perspective && this->zmin == 0)
		printf("Warning: Perspective camera with a zmin of zero.\n");
}

void Camera::renderScene(string filename, int cameraNum) {
	BMP image;
	image.SetSize(this->imageWidth,this->imageHeight);
    image.SetBitDepth(32);
	int numSamples;
	if(aa == FSAA_4) numSamples = 4;
	else if(aa == FSAA_16) numSamples = 16;
	else numSamples = 1;
	int sqrtNumSamples = (int)sqrt((float)numSamples);
	double aspect = (double)imageHeight/(double)imageWidth;

	double width = this->width;
	double height = this->width * aspect;
	double width2 = width/2.f;
	double height2 = height/2.f;
	double deltawidth = 1/(imageWidth * sqrtNumSamples);
	double deltaheight = 1/(imageHeight * sqrtNumSamples);

	double gapWidth2 = 1.f/((double)imageWidth)/2.f;
	double leftGap = 1.f/(imageWidth * sqrtNumSamples*2); //Gap between edge of "square" and leftmost sample.
	double gapBetweenSamples = 2*leftGap;

	for(int y = 0; y < this->imageHeight; y++)
	{
		for(int x = 0; x < this->imageWidth; x++)
		{
			bool isPerspective = this->perspective; //False if orthogonal, true if perspective

			double xCoord = -1*width2 + width*x/imageWidth;
			double yCoord = -1*height2 + height*y/imageHeight;

			norm(this->direction);
			norm(this->up);

			Vector vLeft = cross3(this->direction,this->up); 

			//Point on image plane
			Point pPointOnImagePlane = this->origin + this->zmin * this->direction + xCoord * vLeft + yCoord * this->up;
			
			Vector vCamToImagePlane; //Vector from the camera to the image plane

			if(isPerspective) //If camera is perspective
			{
				vCamToImagePlane = pPointOnImagePlane - this->origin;
			}
			else //If camera is orthogonal
			{
				vCamToImagePlane = this->direction;
			}
			//vCamToImagePlane = c->direction;

			Ray* r = new Ray(); //Ray from the camera to the image plane
			r->dir = vCamToImagePlane;
			
			if(isPerspective)
				r->start = this->origin;
			else
				r->start = pPointOnImagePlane;

			bool lightT = false;

			Color col;
			if(aa == FSAA_4 || aa == FSAA_16) {
				Color* colors = new Color[numSamples];
				for(int x2 = 0; x2 < sqrtNumSamples; x2++) {
					double adjXCoord = xCoord - gapWidth2 + leftGap + (x2-1)*gapBetweenSamples;
					for(int y2 = 0; y2 < sqrtNumSamples; y2++) {
						double adjYCoord = yCoord - gapWidth2 + leftGap + (y2-1)*gapBetweenSamples;
						pPointOnImagePlane = this->origin + this->zmin * this->direction + adjXCoord * vLeft + adjYCoord * this->up;
						if(isPerspective) r->dir = pPointOnImagePlane - this->origin;
						if(!isPerspective) r->start = pPointOnImagePlane;
						colors[x2*sqrtNumSamples + y2] = raytrace(r,lightT);
					}
				}
				col = Color::averageValues(colors,numSamples);
				delete colors;
			}
			else col = raytrace(r,lightT);
			if(DIAGNOSTIC_STATUS == IS_LIT) col.adjustColorForDiagnosticIsLit();
{'fraction_non_alphanumeric': 0.164066023270497, 'fraction_numerical': 0.018760710742310813, 'mean_word_length': 3.2369124952235384, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 125, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2304146', 'n_tokens_mistral': 4801, 'n_tokens_neox': 3998, 'n_words': 916}