// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Ray const&, gaps::R3Sphere const&, gaps::R3Point*, gaps::R3Vector*, double*)
RNClassID R3Intersects(const R3Ray& ray, const R3Sphere& sphere, 
    R3Point *hit_point1, R3Vector *hit_normal1, RNScalar *hit_t1)
{
    // Check if sphere contains ray start point
    RNBoolean start_inside = R3Contains(sphere, ray.Start());
    if (start_inside) {
      if (hit_point1) *hit_point1 = ray.Start();
      if (hit_normal1) *hit_normal1 = -ray.Vector();
      if (hit_t1) *hit_t1 = 0;
      return R3_SPAN_CLASS_ID;
    }

    // Check if ray points towards sphere
    R3Vector EO = sphere.Center() - ray.Start();
    RNScalar v = EO.Dot(ray.Vector());
    if (!start_inside && RNIsNegativeOrZero(v)) {
        // Ray points away from sphere
        return RN_NULL_CLASS_ID;
    }
    else {
        // Compute possible intersection -- Graphics Gems I, p. 388    
        RNScalar disc = sphere.Radius() * sphere.Radius() - (EO.Dot(EO) - v*v);
        if (RNIsNegative(disc)) {
	    // No intersection
	    return RN_NULL_CLASS_ID;
        }
        else {
            // Ray intersects sphere (it grazes if disc is zero)
            // Compute first intersection
            if (hit_t1 || hit_point1 || hit_normal1) {
                RNScalar d = sqrt(disc);
                RNScalar t = (start_inside) ? v + d : v - d;
                R3Point p = ray.Start() + t * ray.Vector();
                if (hit_t1) *hit_t1 = t;
                if (hit_point1) *hit_point1 = p;
                if (hit_normal1) *hit_normal1 = (p - sphere.Centroid()) / sphere.Radius();
            }

            // Compute second intersection
            // if (hit_t2 || hit_point2 || hit_normal2) {
            //     if (!start_inside) {
            //         t = v + d;
            //         if (hit_point2) *hit_point2 = ray.Start() + t * ray.Vector();
            //         if (hit_t2) *hit_t2 = t;
            //     }
            // }
            
            // Return result
            if (RNIsZero(disc)) return R3_POINT_CLASS_ID;
            else return R3_SPAN_CLASS_ID;
        }
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r8, %r14
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r13
movq %rsi, %rdi
movq %r13, %rsi
callq 0x4949b
testl %eax, %eax
je 0x441ed
testq %r12, %r12
je 0x441af
movq %r12, %rdi
movq %r13, %rsi
callq 0x3cd94
testq %rbx, %rbx
je 0x441d3
addq $0x18, %r13
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x3e154
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3dc6a
movl $0x4, %ebp
testq %r14, %r14
je 0x44397
movq $0x0, (%r14)
jmp 0x44397
leaq 0x8(%r15), %rsi
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rdx
callq 0x3cc88
leaq 0x18(%r13), %rsi
movq %rbp, %rdi
movq %rsi, 0x18(%rsp)
callq 0x3d756
leaq 0xa197f(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
xorl %ebp, %ebp
ucomisd %xmm0, %xmm1
jae 0x44397
movsd 0x20(%r15), %xmm1
movsd %xmm1, 0x10(%rsp)
leaq 0x38(%rsp), %rdi
movq %rdi, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x3d756
movsd 0x8(%rsp), %xmm3
leaq 0xa1943(%rip), %rax     # 0xe5b98
movsd 0x10(%rsp), %xmm2
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
mulsd %xmm2, %xmm2
subsd %xmm0, %xmm2
movsd (%rax), %xmm0
xorpd 0x5dda5(%rip), %xmm0    # 0xa2020
ucomisd %xmm2, %xmm0
ja 0x44397
testq %rbx, %rbx
jne 0x44298
testq %r12, %r12
jne 0x44298
testq %r14, %r14
je 0x4436e
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
jb 0x442b1
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
jmp 0x442c0
movapd %xmm2, %xmm0
callq 0xe340
movsd 0x8(%rsp), %xmm3
movq 0x18(%rsp), %rsi
subsd %xmm0, %xmm3
movsd %xmm3, 0x8(%rsp)
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movapd %xmm3, %xmm0
callq 0x3de8e
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3cd71
testq %r14, %r14
je 0x44300
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r14)
testq %r12, %r12
je 0x44312
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x3cd94
testq %rbx, %rbx
je 0x44361
movq (%r15), %rax
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq *0x68(%rax)
leaq 0x80(%rsp), %r12
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
movq %r14, %rdx
callq 0x3cc88
movsd 0x20(%r15), %xmm0
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x3e200
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3dc6a
movsd 0x10(%rsp), %xmm2
leaq 0xa182a(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x5dca6(%rip), %xmm1    # 0xa2020
xorl %eax, %eax
ucomisd %xmm2, %xmm0
xorpd %xmm0, %xmm1
setb %al
ucomisd %xmm1, %xmm2
leal 0x1(%rax,%rax,2), %eax
movl $0x4, %ebp
cmovael %eax, %ebp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq