// File: /workspace/github_cpp_makefile_star2_O2/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 0x42b58
testl %eax, %eax
je 0x3e733
testq %r12, %r12
je 0x3e6fa
movq %r12, %rdi
movq %r13, %rsi
callq 0x37e36
testq %rbx, %rbx
je 0x3e71e
addq $0x18, %r13
leaq 0x38(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x38c72
movq %rbx, %rdi
movq %r15, %rsi
callq 0x3887c
pushq $0x4
popq %rbp
testq %r14, %r14
je 0x3e8dc
andq $0x0, (%r14)
jmp 0x3e8dc
leaq 0x8(%r15), %rsi
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rdx
callq 0x37d29
leaq 0x18(%r13), %rsi
movq %rbp, %rdi
movq %rsi, 0x18(%rsp)
callq 0x38586
leaq 0x9c449(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm1
xorl %ebp, %ebp
ucomisd %xmm0, %xmm1
jae 0x3e8dc
movsd 0x20(%r15), %xmm1
movsd %xmm1, 0x10(%rsp)
leaq 0x38(%rsp), %rdi
movq %rdi, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x38586
movsd 0x8(%rsp), %xmm3
movsd 0x10(%rsp), %xmm2
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
mulsd %xmm2, %xmm2
subsd %xmm0, %xmm2
leaq 0x9c3f3(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm1
movapd 0x5486f(%rip), %xmm0    # 0x93030
xorpd %xmm1, %xmm0
ucomisd %xmm2, %xmm0
ja 0x3e8dc
testq %rbx, %rbx
jne 0x3e7e2
testq %r12, %r12
jne 0x3e7e2
testq %r14, %r14
je 0x3e8c5
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
movsd %xmm2, 0x10(%rsp)
jb 0x3e7f8
sqrtsd %xmm2, %xmm0
jmp 0x3e807
movapd %xmm2, %xmm0
callq 0xe350
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 0x38a02
leaq 0x20(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x37e13
testq %r14, %r14
je 0x3e847
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r14)
testq %r12, %r12
je 0x3e859
leaq 0x20(%rsp), %rsi
movq %r12, %rdi
callq 0x37e36
testq %rbx, %rbx
je 0x3e8a8
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 0x37d29
movsd 0x20(%r15), %xmm0
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
callq 0x38d1e
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3887c
leaq 0x9c2f9(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm1
movapd 0x54775(%rip), %xmm0    # 0x93030
xorpd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm2
xorl %eax, %eax
ucomisd %xmm2, %xmm1
setb %al
ucomisd %xmm0, %xmm2
leal 0x1(%rax,%rax,2), %eax
pushq $0x4
popq %rbp
cmovael %eax, %ebp
movl %ebp, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq