// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Ray const&, gaps::R3Circle const&, gaps::R3Point*, gaps::R3Vector*, double*)
RNClassID R3Intersects(const R3Ray& ray, const R3Circle& circle, 
    R3Point *hit_point, R3Vector *hit_normal, RNScalar *hit_t)
{
    // Check for empty circle
    if (circle.IsEmpty()) return RN_NULL_CLASS_ID;
    
    // Find intersection of ray and plane
    R3Point p1;
    RNScalar t1;
    switch (R3Intersects(ray, circle.Plane(), &p1, &t1)) {
    case RN_NULL_CLASS_ID:
	// Ray does not intersect circle plane
	return RN_NULL_CLASS_ID;
	
    case R3_POINT_CLASS_ID:
    {
	// Ray intersects circle plane in a single point
	// Check whether circle contains intersection point
	RNScalar radius_squared = circle.Radius() * circle.Radius();
	R3Vector v = circle.Center() - p1;
	RNScalar distance_squared = v.X() * v.X() + v.Y() * v.Y() + v.Z() * v.Z();
	if (RNIsGreater(distance_squared, radius_squared)) return RN_NULL_CLASS_ID;
	if (hit_point) *hit_point = p1;
	if (hit_normal) *hit_normal = circle.Normal();
	if (hit_t) *hit_t = t1;
	return R3_POINT_CLASS_ID;
    }

    case R3_RAY_CLASS_ID: 
	// Ray lies in circle plane 
	// Intersect ray and circle in 2D
        // RNAbort("Case not implemented");
	return R3_SPAN_CLASS_ID;

    default:
	RNAbort("Invalid intersection result");
	break;
    }

    // Should never get here
    RNAbort("Should never get here");
    return RN_NULL_CLASS_ID;
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rcx, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x81c80
cmpl $0x0, %eax
je 0x7bc61
movl $0x0, 0xc4(%rsp)
jmp 0x7bea1
leaq 0x80(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x6eb40
movq 0xb8(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x81ca0
movq 0x38(%rsp), %rdi
movq 0x40(%rsp), %rdx
movq %rax, %rsi
leaq 0x78(%rsp), %rcx
callq 0x7ae90
movl %eax, %ecx
movl %ecx, 0x4c(%rsp)
testl %eax, %eax
je 0x7bccd
jmp 0x7bcb0
movl 0x4c(%rsp), %eax
subl $0x1, %eax
je 0x7bcdd
jmp 0x7bcbb
movl 0x4c(%rsp), %eax
subl $0x3, %eax
je 0x7be6d
jmp 0x7be7a
movl $0x0, 0xc4(%rsp)
jmp 0x7bea1
movq 0xb0(%rsp), %rdi
callq 0x3c2e0
movsd %xmm0, 0x8(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x3c2e0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x81cb0
movq %rax, %rsi
leaq 0x58(%rsp), %rdi
leaq 0x80(%rsp), %rdx
callq 0x6eeb0
leaq 0x58(%rsp), %rdi
callq 0x38c20
movsd %xmm0, 0x20(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x38c20
movsd %xmm0, 0x18(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x38c30
movsd %xmm0, 0x10(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x38c30
movsd 0x10(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
movaps %xmm0, %xmm3
movsd 0x20(%rsp), %xmm0
mulsd %xmm3, %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x38c40
movsd %xmm0, 0x30(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x38c40
movsd 0x28(%rsp), %xmm1
movaps %xmm0, %xmm2
movsd 0x30(%rsp), %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
leaq 0xfc703(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x3c090
cmpl $0x0, %eax
je 0x7bdfb
movl $0x0, 0xc4(%rsp)
jmp 0x7bea1
cmpq $0x0, 0xa8(%rsp)
je 0x7be1b
movq 0xa8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x6f1e0
cmpq $0x0, 0xa0(%rsp)
je 0x7be43
movq 0xb0(%rsp), %rdi
callq 0x78a80
movq %rax, %rsi
movq 0xa0(%rsp), %rdi
callq 0x70970
cmpq $0x0, 0x98(%rsp)
je 0x7be60
movsd 0x78(%rsp), %xmm0
movq 0x98(%rsp), %rax
movsd %xmm0, (%rax)
movl $0x1, 0xc4(%rsp)
jmp 0x7bea1
movl $0x4, 0xc4(%rsp)
jmp 0x7bea1
leaq 0xb9b1e(%rip), %rdi     # 0x13599f
movb $0x0, %al
callq 0xd4e20
leaq 0xb9afa(%rip), %rdi     # 0x135989
movb $0x0, %al
callq 0xd4e20
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
addq $0xc8, %rsp
retq