// File: IsaacLaw12[P]RayTracer/./src/Sphere.cpp
// Function: Sphere::intersect_ray(Ray&, Eigen::Matrix<double, 3, 1, 0, 3, 1>&)
double Sphere::intersect_ray(Ray& ray, Eigen::Vector3d &hit_normal){
    const Eigen::Vector3d &ray_pt = ray.get_point();
    const Eigen::Vector3d &ray_dir = ray.get_dir();
    double t_value = -1;
    double v = (sphere_center - ray_pt).dot(ray_dir);
    double c = (sphere_center - ray_pt).norm();
    // Either the ray went the wrong way, or there was no intersection
    if (v < 0 || ((radius*radius) < (c*c - v*v))){
        return t_value;
    }
    double dr = (ray_pt + (v * ray_dir) - sphere_center).norm();
    double intersection_offset = sqrt( radius*radius - dr*dr);
    t_value = v - intersection_offset;

    Eigen::Vector3d intersection_point = ray_pt + ray_dir * t_value;
    hit_normal = (intersection_point - sphere_center).normalized();
    return t_value;
}

// Assembly:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x1d8(%rsp)
movq %rsi, 0x1d0(%rsp)
movq %rdx, 0x1c8(%rsp)
movq 0x1d8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x5cb90
movq %rax, 0x1c0(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x5cba0
movq 0x10(%rsp), %rsi
movq %rax, 0x1b8(%rsp)
movsd 0x217bd(%rip), %xmm0    # 0x5de60
movsd %xmm0, 0x1b0(%rsp)
addq $0x1b8, %rsi            # imm = 0x1B8
movq 0x1c0(%rsp), %rdx
leaq 0x190(%rsp), %rdi
callq 0x9e80
movq 0x1b8(%rsp), %rsi
leaq 0x190(%rsp), %rdi
callq 0x3ce10
movq 0x10(%rsp), %rsi
movsd %xmm0, 0x1a8(%rsp)
addq $0x1b8, %rsi            # imm = 0x1B8
movq 0x1c0(%rsp), %rdx
leaq 0x170(%rsp), %rdi
callq 0x9e80
leaq 0x170(%rsp), %rdi
callq 0x32400
movsd %xmm0, 0x188(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x1a8(%rsp), %xmm0
ja 0x3c789
movq 0x10(%rsp), %rax
movsd 0x190(%rax), %xmm1
mulsd 0x190(%rax), %xmm1
movsd 0x188(%rsp), %xmm0
movsd 0x188(%rsp), %xmm3
movsd 0x1a8(%rsp), %xmm2
mulsd 0x1a8(%rsp), %xmm2
movq %xmm2, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm2
mulsd %xmm3, %xmm0
addsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x3c7a0
movsd 0x1b0(%rsp), %xmm0
movsd %xmm0, 0x1e0(%rsp)
jmp 0x3c926
movq 0x1c0(%rsp), %rax
movq %rax, (%rsp)
movq 0x1b8(%rsp), %rdx
leaq 0xb8(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0xa310
movq (%rsp), %rsi
leaq 0xe0(%rsp), %rdi
leaq 0xb8(%rsp), %rdx
callq 0xa2a0
movq 0x10(%rsp), %rdx
addq $0x1b8, %rdx            # imm = 0x1B8
leaq 0x118(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3ce80
leaq 0x118(%rsp), %rdi
callq 0x3cef0
movq 0x10(%rsp), %rax
movsd %xmm0, 0x168(%rsp)
movsd 0x190(%rax), %xmm0
movsd 0x190(%rax), %xmm2
movsd 0x168(%rsp), %xmm1
mulsd 0x168(%rsp), %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0x42c0
movsd %xmm0, 0xb0(%rsp)
movsd 0x1a8(%rsp), %xmm0
subsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0x1b0(%rsp)
movq 0x1c0(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0x1b8(%rsp), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x1b0(%rsp), %rdx
callq 0x1ed20
movq 0x8(%rsp), %rsi
leaq 0x68(%rsp), %rdi
leaq 0x48(%rsp), %rdx
callq 0x1ecb0
leaq 0x98(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x3cf60
movq 0x10(%rsp), %rdx
addq $0x1b8, %rdx            # imm = 0x1B8
leaq 0x18(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x9e80
leaq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x9ef0
movq 0x1c8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x9e50
movsd 0x1b0(%rsp), %xmm0
movsd %xmm0, 0x1e0(%rsp)
movsd 0x1e0(%rsp), %xmm0
addq $0x1e8, %rsp            # imm = 0x1E8
retq
nopw (%rax,%rax)