<commit_before>#include <limits>
#include "RayTracer.h"
#include "Image.h"
#include "Object.h"
#include "Sphere.h"
#include "Intersection.h"
#include "Light.h"

using namespace std;

RayTracer::RayTracer(int width_, int height_, int maxReflections_, int superSamples_,
 int depthComplexity_) : width(width_), height(height_),
 maxReflections(maxReflections_), superSamples(superSamples_),
 depthComplexity(depthComplexity_) {
   cameraPosition = Vector(0.0, 0.0, 100.0);
   focalPointLength = 100.0;
   dispersion = 5.0f;
}

RayTracer::~RayTracer() {
   for (vector<Object*>::iterator itr = objects.begin(); itr < objects.end(); itr++) {
      delete *itr;
   }

   for (vector<Light*>::iterator itr = lights.begin(); itr < lights.end(); itr++) {
      delete *itr;
   }
}

void RayTracer::traceRays(string fileName) {
   Image image(width, height);

   for (int x = 0; x < width; x++) {
      // Update percent complete.
      float percentage = x/(float)width * 100;
      cout << '\r' << (int)percentage << '%';
      fflush(stdout);

      for (int y = 0; y < height; y++) {
         image.pixel(x, y, castRayForPixel(x, y));
      }
   }

   cout << "\rDone!" << endl;

   image.WriteTga(fileName.c_str(), true);
}

Color RayTracer::castRayForPixel(int x, int y) {
   /**
    * All of these values are "on the focal plane." That is to say rays will
    * be cast from the camera position towards points created from these
    * values.
    */
   double rayX = (x - width / 2)/2.0;
   double rayY = (y - height / 2)/2.0;
   double pixelWidth = rayX - (x + 1 - width / 2)/2.0;
   double sampleWidth = pixelWidth / superSamples;
   double sampleStartX = rayX - pixelWidth/2.0;
   double sampleStartY = rayY - pixelWidth/2.0;
   double sampleWeight = 1.0 / (superSamples * superSamples);
   Color color;

   for (int x = 0; x < superSamples; x++) {
      for (int y = 0; y < superSamples; y++) {
         Vector imagePlanePoint = Vector(sampleStartX + (x * sampleWidth),
          sampleStartY + (y * sampleWidth), 0);

         color = color + (castRayWithXY(imagePlanePoint) * sampleWeight);
      }
   }

   return color;
}

Color RayTracer::castRayWithXY(Vector direction) {
   Color color;
   // Divide by projection distance.
   direction.x /= 500.0;
   direction.y /= 500.0;
   direction.z = -1;

   direction = direction.normalize();
   Vector aimed = cameraPosition + (direction * focalPointLength);

   for (int i = 0; i < depthComplexity; i++) {
      Ray viewRay(cameraPosition, direction, maxReflections);
      Vector disturbance(
       (dispersion / RAND_MAX) * (1.0f * rand()),
       (dispersion / RAND_MAX) * (1.0f * rand()),
       0.0f);

      viewRay.origin = viewRay.origin + disturbance;
      viewRay.direction = aimed - viewRay.origin;
      viewRay.direction = viewRay.direction.normalize();

      color = color + (castRay(viewRay) * (1 / (float)depthComplexity));
   }

   return color;<commit_msg>Don't do depth if complexity == 1<commit_after>#include <limits>
#include "RayTracer.h"
#include "Image.h"
#include "Object.h"
#include "Sphere.h"
#include "Intersection.h"
#include "Light.h"

using namespace std;

RayTracer::RayTracer(int width_, int height_, int maxReflections_, int superSamples_,
 int depthComplexity_) : width(width_), height(height_),
 maxReflections(maxReflections_), superSamples(superSamples_),
 depthComplexity(depthComplexity_) {
   cameraPosition = Vector(0.0, 0.0, 100.0);
   focalPointLength = 100.0;
   dispersion = 5.0f;
}

RayTracer::~RayTracer() {
   for (vector<Object*>::iterator itr = objects.begin(); itr < objects.end(); itr++) {
      delete *itr;
   }

   for (vector<Light*>::iterator itr = lights.begin(); itr < lights.end(); itr++) {
      delete *itr;
   }
}

void RayTracer::traceRays(string fileName) {
   Image image(width, height);

   for (int x = 0; x < width; x++) {
      // Update percent complete.
      float percentage = x/(float)width * 100;
      cout << '\r' << (int)percentage << '%';
      fflush(stdout);

      for (int y = 0; y < height; y++) {
         image.pixel(x, y, castRayForPixel(x, y));
      }
   }

   cout << "\rDone!" << endl;

   image.WriteTga(fileName.c_str(), true);
}

Color RayTracer::castRayForPixel(int x, int y) {
   /**
    * All of these values are "on the focal plane." That is to say rays will
    * be cast from the camera position towards points created from these
    * values.
    */
   double rayX = (x - width / 2)/2.0;
   double rayY = (y - height / 2)/2.0;
   double pixelWidth = rayX - (x + 1 - width / 2)/2.0;
   double sampleWidth = pixelWidth / superSamples;
   double sampleStartX = rayX - pixelWidth/2.0;
   double sampleStartY = rayY - pixelWidth/2.0;
   double sampleWeight = 1.0 / (superSamples * superSamples);
   Color color;

   for (int x = 0; x < superSamples; x++) {
      for (int y = 0; y < superSamples; y++) {
         Vector imagePlanePoint = Vector(sampleStartX + (x * sampleWidth),
          sampleStartY + (y * sampleWidth), 0);

         color = color + (castRayWithXY(imagePlanePoint) * sampleWeight);
      }
   }

   return color;
}

Color RayTracer::castRayWithXY(Vector direction) {
   Color color;
   // Divide by projection distance.
   direction.x /= 500.0;
   direction.y /= 500.0;
   direction.z = -1;

   direction = direction.normalize();
   Vector aimed = cameraPosition + (direction * focalPointLength);

   for (int i = 0; i < depthComplexity; i++) {
      Ray viewRay(cameraPosition, direction, maxReflections);

      if (depthComplexity > 1) {
         Vector disturbance(
          (dispersion / RAND_MAX) * (1.0f * rand()),
          (dispersion / RAND_MAX) * (1.0f * rand()),
          0.0f);

         viewRay.origin = viewRay.origin + disturbance;
         viewRay.direction = aimed - viewRay.origin;
         viewRay.direction = viewRay.direction.normalize();
      }

      color = color + (castRay(viewRay) * (1 / (float)depthComplexity));
   }

   return color;
{'fraction_non_alphanumeric': 0.1434102003029793, 'fraction_numerical': 0.0188520451102508, 'mean_word_length': 2.94293297942933, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 10, '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': '6458289', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1946, 'n_words': 517}