// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Line const&, gaps::R3Sphere const&, gaps::R3Point*)
RNClassID R3Intersects(const R3Line& line, const R3Sphere& sphere, 
    R3Point *hit_point)
{
    // Check if sphere contains line point
    if (R3Contains(sphere, line.Point())) {
	if (hit_point) *hit_point = line.Point();
	return R3_SPAN_CLASS_ID;
    }
    else {
	// Check if line points towards sphere
	R3Vector EO = sphere.Center() - line.Point();
	RNScalar v = EO.Dot(line.Vector());

	// 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 {
	    // Line intersects sphere (it grazes if disc is zero)
	    if (hit_point) {
		RNScalar d = sqrt(disc);
		RNScalar t = v - d;
		*hit_point = line.Point() + t * line.Vector();
	    }
	    
	    // Return result
	    if (RNIsZero(disc)) return R3_POINT_CLASS_ID;
	    else return R3_SPAN_CLASS_ID;
	}
    }
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq 0xa0(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x3c0d0
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x85dc0
cmpl $0x0, %eax
je 0x7a7c3
cmpq $0x0, 0x98(%rsp)
je 0x7a7b3
movq 0xa8(%rsp), %rdi
callq 0x3c0d0
movq %rax, %rsi
movq 0x98(%rsp), %rdi
callq 0x6f1e0
movl $0x4, 0xb4(%rsp)
jmp 0x7a9a1
movq 0xa0(%rsp), %rdi
callq 0x42710
movq %rax, 0x10(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x3c0d0
movq 0x10(%rsp), %rsi
movq %rax, %rdx
leaq 0x80(%rsp), %rdi
callq 0x6eeb0
movq 0xa8(%rsp), %rdi
callq 0x3c0f0
movq %rax, %rsi
leaq 0x80(%rsp), %rdi
callq 0x702c0
movsd %xmm0, 0x78(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x39f50
movsd %xmm0, 0x20(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x39f50
movsd %xmm0, 0x18(%rsp)
leaq 0x80(%rsp), %rsi
movq %rsi, %rdi
callq 0x702c0
movsd 0x18(%rsp), %xmm2
movaps %xmm0, %xmm3
movsd 0x20(%rsp), %xmm0
movsd 0x78(%rsp), %xmm1
movsd 0x78(%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, 0x70(%rsp)
movsd 0x70(%rsp), %xmm0
leaq 0xfdc24(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x38fd0
cmpl $0x0, %eax
je 0x7a8da
movl $0x0, 0xb4(%rsp)
jmp 0x7a9a1
cmpq $0x0, 0x98(%rsp)
je 0x7a96e
movsd 0x70(%rsp), %xmm0
callq 0xf390
movsd %xmm0, 0x68(%rsp)
movsd 0x78(%rsp), %xmm0
subsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x3c0d0
movq %rax, 0x8(%rsp)
movsd 0x60(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0xa8(%rsp), %rdi
callq 0x3c0f0
movsd (%rsp), %xmm0
movq %rax, %rsi
leaq 0x30(%rsp), %rdi
callq 0x13800
movq 0x8(%rsp), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x6f110
movq 0x98(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x6f1e0
movsd 0x70(%rsp), %xmm0
leaq 0xfdb65(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x7a996
movl $0x1, 0xb4(%rsp)
jmp 0x7a9a1
movl $0x4, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq