// File: Lut99[P]RayTracer/src/lib/objects/Sphere.cpp
// Function: RayTracer::Sphere::hit(RayTracer::Ray const&, double, double, RayTracer::HitRecord&) const
bool Sphere::hit(const Ray& ray, double t_min, double t_max, HitRecord& record) const {
    Vec3 d_ray_sphere = ray.origin - this->center;
    double a = dot(ray.direction, ray.direction);
    double b = dot(d_ray_sphere, ray.direction);
    double c = dot(d_ray_sphere, d_ray_sphere) - this->radius * this->radius;

    double D = b*b - a*c;

    // Return the distance the ray has travelled until it hits the closest side of the sphere.
    if (D > 0) {
        // Compute tboth variations of the t. If t - sqrt(D) is true, take that one. Otherwise, take the other.
        double t = (-b - sqrtf64(D)) / a;
        if (t <= t_min || t >= t_max) {
            t = (-b + sqrtf64(D)) / a;
            if (t <= t_min || t >= t_max) {
                return false;
            }
        }

        record.t = t;
        record.t_min = t_min;
        record.t_max = t_max;
        record.hitpoint = ray.point(t);
        record.normal = this->normal(record);
        record.obj = (RenderObject*) this;
        record.material = this->material;

        return true;
    }
    return false;
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xc8(%rsp)
movq %rsi, 0xc0(%rsp)
movsd %xmm0, 0xb8(%rsp)
movsd %xmm1, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq 0xc8(%rsp), %rdx
movq %rdx, 0x18(%rsp)
movq 0xc0(%rsp), %rsi
addq $0x18, %rdx
leaq 0x88(%rsp), %rdi
callq 0x1558e0
movq 0xc0(%rsp), %rdi
addq $0x20, %rdi
movq 0xc0(%rsp), %rsi
addq $0x20, %rsi
callq 0x155c70
movsd %xmm0, 0x80(%rsp)
movq 0xc0(%rsp), %rsi
addq $0x20, %rsi
leaq 0x88(%rsp), %rdi
callq 0x155c70
movsd %xmm0, 0x78(%rsp)
leaq 0x88(%rsp), %rsi
movq %rsi, %rdi
callq 0x155c70
movq 0x18(%rsp), %rax
movaps %xmm0, %xmm1
movsd 0x80(%rax), %xmm0
movsd 0x80(%rax), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x78(%rsp), %xmm0
movsd 0x78(%rsp), %xmm2
movsd 0x80(%rsp), %xmm1
mulsd 0x70(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x15b423
movsd 0x78(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x68(%rsp), %xmm0
callq 0xd01b0
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
divsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x15b2dc
movsd 0x60(%rsp), %xmm0
ucomisd 0xb0(%rsp), %xmm0
jb 0x15b35b
movsd 0x78(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x68(%rsp), %xmm0
callq 0xd01b0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
addsd %xmm1, %xmm0
divsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jae 0x15b34c
movsd 0x60(%rsp), %xmm0
ucomisd 0xb0(%rsp), %xmm0
jb 0x15b359
movb $0x0, 0xd7(%rsp)
jmp 0x15b42b
jmp 0x15b35b
movsd 0x60(%rsp), %xmm0
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0xb8(%rsp), %xmm0
movq 0xa8(%rsp), %rax
movsd %xmm0, 0x8(%rax)
movsd 0xb0(%rsp), %xmm0
movq 0xa8(%rsp), %rax
movsd %xmm0, 0x10(%rax)
movq 0xc0(%rsp), %rsi
movsd 0x60(%rsp), %xmm0
leaq 0x40(%rsp), %rdi
callq 0x139c60
movq 0xa8(%rsp), %rdi
addq $0x18, %rdi
leaq 0x40(%rsp), %rsi
callq 0x1547a0
movq 0x18(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq (%rsi), %rax
leaq 0x20(%rsp), %rdi
callq *0x40(%rax)
movq 0xa8(%rsp), %rdi
addq $0x38, %rdi
leaq 0x20(%rsp), %rsi
callq 0x1547a0
movq 0x18(%rsp), %rax
movq 0xa8(%rsp), %rcx
movq %rax, 0x58(%rcx)
movq 0x88(%rax), %rcx
movq 0xa8(%rsp), %rax
movq %rcx, 0x60(%rax)
movb $0x1, 0xd7(%rsp)
jmp 0x15b42b
movb $0x0, 0xd7(%rsp)
movb 0xd7(%rsp), %al
andb $0x1, %al
addq $0xd8, %rsp
retq
nopl (%rax)