// File: 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:
subq $0x138, %rsp            # imm = 0x138
movq %rdi, 0x128(%rsp)
movq %rsi, 0x120(%rsp)
movq %rdx, 0x118(%rsp)
movq %rcx, 0x110(%rsp)
movq %r8, 0x108(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x128(%rsp), %rdi
callq 0x3c0c0
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x85dc0
movl %eax, 0x104(%rsp)
cmpl $0x0, 0x104(%rsp)
je 0x7c68a
cmpq $0x0, 0x118(%rsp)
je 0x7c623
movq 0x128(%rsp), %rdi
callq 0x3c0c0
movq %rax, %rsi
movq 0x118(%rsp), %rdi
callq 0x6f1e0
cmpq $0x0, 0x110(%rsp)
je 0x7c660
movq 0x128(%rsp), %rdi
callq 0x3c0e0
movq %rax, %rsi
leaq 0xe8(%rsp), %rdi
callq 0x70fa0
movq 0x110(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x70970
cmpq $0x0, 0x108(%rsp)
je 0x7c67a
movq 0x108(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, (%rax)
movl $0x4, 0x134(%rsp)
jmp 0x7c9a6
movq 0x120(%rsp), %rdi
callq 0x42710
movq %rax, 0x28(%rsp)
movq 0x128(%rsp), %rdi
callq 0x3c0c0
movq 0x28(%rsp), %rsi
movq %rax, %rdx
leaq 0xd0(%rsp), %rdi
callq 0x6eeb0
movq 0x128(%rsp), %rdi
callq 0x3c0e0
movq %rax, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x702c0
movsd %xmm0, 0xc8(%rsp)
cmpl $0x0, 0x104(%rsp)
jne 0x7c71c
movsd 0xc8(%rsp), %xmm0
leaq 0xfbde2(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x382c0
cmpl $0x0, %eax
je 0x7c71c
movl $0x0, 0x134(%rsp)
jmp 0x7c9a6
movq 0x120(%rsp), %rdi
callq 0x39f50
movsd %xmm0, 0x20(%rsp)
movq 0x120(%rsp), %rdi
callq 0x39f50
movsd %xmm0, 0x18(%rsp)
leaq 0xd0(%rsp), %rsi
movq %rsi, %rdi
callq 0x702c0
movsd 0x18(%rsp), %xmm2
movaps %xmm0, %xmm3
movsd 0x20(%rsp), %xmm0
movsd 0xc8(%rsp), %xmm1
movsd 0xc8(%rsp), %xmm4
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm4, %xmm1
addsd %xmm3, %xmm1
movq %xmm1, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm0
leaq 0xfbd16(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0x7c7e8
movl $0x0, 0x134(%rsp)
jmp 0x7c9a6
cmpq $0x0, 0x108(%rsp)
jne 0x7c80d
cmpq $0x0, 0x118(%rsp)
jne 0x7c80d
cmpq $0x0, 0x110(%rsp)
je 0x7c970
movsd 0xc0(%rsp), %xmm0
callq 0xf390
movsd %xmm0, 0xb8(%rsp)
cmpl $0x0, 0x104(%rsp)
je 0x7c848
movsd 0xc8(%rsp), %xmm0
addsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x7c860
movsd 0xc8(%rsp), %xmm0
subsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
movq 0x128(%rsp), %rdi
callq 0x3c0c0
movq %rax, 0x8(%rsp)
movsd 0xb0(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x128(%rsp), %rdi
callq 0x3c0e0
movsd (%rsp), %xmm0
movq %rax, %rsi
leaq 0x80(%rsp), %rdi
callq 0x13800
movq 0x8(%rsp), %rsi
leaq 0x98(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x6f110
cmpq $0x0, 0x108(%rsp)
je 0x7c8eb
movsd 0xb0(%rsp), %xmm0
movq 0x108(%rsp), %rax
movsd %xmm0, (%rax)
cmpq $0x0, 0x118(%rsp)
je 0x7c90b
movq 0x118(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x6f1e0
cmpq $0x0, 0x110(%rsp)
je 0x7c96e
movq 0x120(%rsp), %rsi
movq (%rsi), %rax
leaq 0x38(%rsp), %rdi
callq *0x68(%rax)
leaq 0x50(%rsp), %rdi
leaq 0x98(%rsp), %rsi
leaq 0x38(%rsp), %rdx
callq 0x6eeb0
movq 0x120(%rsp), %rdi
callq 0x39f50
leaq 0x68(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x711f0
movq 0x110(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x70970
jmp 0x7c970
movsd 0xc0(%rsp), %xmm0
leaq 0xfbb60(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x7c99b
movl $0x1, 0x134(%rsp)
jmp 0x7c9a6
movl $0x4, 0x134(%rsp)
movl 0x134(%rsp), %eax
addq $0x138, %rsp            # imm = 0x138
retq
nopw %cs:(%rax,%rax)