<commit_before>#include "Sphere.h"
#include <cmath>

Sphere::Sphere(const Vector3& centre, float radius, Material* material) :
    centre(centre), radius(radius)
{
    this->material = material;
}

const Vector3& Sphere::getCentre() const
{
    return centre;
}

bool Sphere::hit(const Ray& ray, float tMin, float tMax, float time, HitRecord& record) const
{
    Vector3 temp = ray.origin() - centre;

    // Solve quadratic equation to check for intersection
    double a = ray.direction().dot(ray.direction());
    double b = 2 * ray.direction().dot(temp);
    double c = temp.dot(temp) - (radius * radius);
    double discriminant = b * b - 4 * a * c;

    // Two stage check to see if ray intersects a sphere
    if (discriminant > 0) // stage one
    {
        discriminant = sqrt(discriminant);
        double t = (-b - discriminant) / (2 * a);
        if (t < tMin)
            t = (-b - discriminant) / (2 * a);
        // Check if ray is correct desired distance from sphere
        if (t < tMin || t > tMax)
        {
            return false;
        }
        else // ray hits the sphere!
        {
            // Update hit record here!
            record.t = t;
            record.pointOfIntersection = ray.origin() + (ray.direction() * t);
            record.normal = ray.origin() + (t * ray.direction()) - centre;
            record.normal = record.normal.normalise(); // normalise unit vector to get just direction
            record.texCoord = computeTexCoord(record.pointOfIntersection);

            record.hitShape = reinterpret_cast<const Shape*>(this);

            return true;
        }
    }
    else
    {
        return false;
    }
}

bool Sphere::shadowHit(const Ray& ray, float tMin, float tMax,
    float time, const Shape*& occludingShape) const
{
    Vector3 temp = ray.origin() - centre;

    double a = ray.direction().dot(ray.direction());
    double b = 2 * ray.direction().dot(temp);
    double c = temp.dot(temp) - (radius * radius);

    double discriminant = b * b - 4 * a * c;

    if (discriminant > 0)
    {
        discriminant = sqrt(discriminant);
        double t = (-b - discriminant) / (2 * a);
        if (t < tMin)
            t = (-b - discriminant) / (2 * a);
        if (t < tMin || t > tMax)<commit_msg>Reverse sphere normal if ray is refractive<commit_after>#include "Sphere.h"
#include <cmath>

Sphere::Sphere(const Vector3& centre, float radius, Material* material) :
    centre(centre), radius(radius)
{
    this->material = material;
}

const Vector3& Sphere::getCentre() const
{
    return centre;
}

bool Sphere::hit(const Ray& ray, float tMin, float tMax, float time, HitRecord& record) const
{
    Vector3 temp = ray.origin() - centre;

    // Solve quadratic equation to check for intersection
    double a = ray.direction().dot(ray.direction());
    double b = 2 * ray.direction().dot(temp);
    double c = temp.dot(temp) - (radius * radius);
    double discriminant = b * b - 4 * a * c;

    // Two stage check to see if ray intersects a sphere
    if (discriminant > 0) // stage one
    {
        discriminant = sqrt(discriminant);
        double t = (-b - discriminant) / (2 * a);
        if (t < tMin)
            t = (-b - discriminant) / (2 * a);
        // Check if ray is correct desired distance from sphere
        if (t < tMin || t > tMax)
        {
            return false;
        }
        else // ray hits the sphere!
        {
            // Update hit record here!
            record.t = t;
            record.pointOfIntersection = ray.origin() + (ray.direction() * t);
            record.normal = ray.origin() + (t * ray.direction()) - centre;
            record.normal = record.normal.normalise(); // normalise unit vector to get just direction
            // If refraction ray, reverse the normal
            if (ray.direction().dot(record.normal) > 0)
                record.normal = -record.normal;
            record.texCoord = computeTexCoord(record.pointOfIntersection);

            record.hitShape = reinterpret_cast<const Shape*>(this);
            return true;
        }
    }
    else
    {
        return false;
    }
}

bool Sphere::shadowHit(const Ray& ray, float tMin, float tMax,
    float time, const Shape*& occludingShape) const
{
    Vector3 temp = ray.origin() - centre;

    double a = ray.direction().dot(ray.direction());
    double b = 2 * ray.direction().dot(temp);
    double c = temp.dot(temp) - (radius * radius);

    double discriminant = b * b - 4 * a * c;

    if (discriminant > 0)
    {
        discriminant = sqrt(discriminant);
        double t = (-b - discriminant) / (2 * a);
        if (t < tMin)
            t = (-b - discriminant) / (2 * a);
        if (t < tMin || t > tMax)
{'fraction_non_alphanumeric': 0.12922422954303933, 'fraction_numerical': 0.0061636556854410204, 'mean_word_length': 2.1165562913907285, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5150679', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1407, 'n_words': 438}