// File: /workspace/github_cpp_makefile_star2_O3/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 0x48b4d
testl %eax, %eax
je 0x43ba4
testq %r12, %r12
je 0x43b66
movq %r12, %rdi
movq %r13, %rsi
callq 0x3c89a
testq %rbx, %rbx
je 0x43b8a
addq $0x18, %r13
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x3dc00
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3d752
movl $0x4, %ebp
testq %r14, %r14
je 0x43d52
movq $0x0, (%r14)
jmp 0x43d52
leaq 0x8(%r15), %rsi
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rdx
callq 0x3c78e
leaq 0x18(%r13), %rsi
movq %rbp, %rdi
movq %rsi, 0x18(%rsp)
callq 0x3d252
leaq 0xa1fc8(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
xorl %ebp, %ebp
ucomisd %xmm0, %xmm1
jae 0x43d52
movsd 0x20(%r15), %xmm1
movsd %xmm1, 0x10(%rsp)
leaq 0x38(%rsp), %rdi
movq %rdi, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x3d252
movsd 0x8(%rsp), %xmm3
movsd 0x10(%rsp), %xmm2
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
mulsd %xmm2, %xmm2
subsd %xmm0, %xmm2
leaq 0xa1f72(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
movapd 0x5e3ee(%rip), %xmm0    # 0xa2020
xorpd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
ja 0x43d52
testq %rbx, %rbx
jne 0x43c53
testq %r12, %r12
jne 0x43c53
testq %r14, %r14
je 0x43d39
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
jb 0x43c6c
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
jmp 0x43c7b
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 0x3d962
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x3c877
testq %r14, %r14
je 0x43cbb
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r14)
testq %r12, %r12
je 0x43ccd
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x3c89a
testq %rbx, %rbx
je 0x43d1c
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 0x3c78e
movsd 0x20(%r15), %xmm0
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x3dcac
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3d752
leaq 0xa1e75(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
movapd 0x5e2f1(%rip), %xmm0    # 0xa2020
xorpd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm2
xorl %eax, %eax
ucomisd %xmm2, %xmm1
setb %al
ucomisd %xmm0, %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