// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Isect.cpp
// Function: gaps::R2Intersects(gaps::R2Ray const&, gaps::R2Circle const&, gaps::R2Point*, gaps::R2Vector*, double*)
RNClassID R2Intersects(const R2Ray& ray, const R2Circle& circle, 
    R2Point *hit_point1, R2Vector *hit_normal1, RNScalar *hit_t1)
{
    // Check if circle contains ray start point
    if (R2Contains(circle, ray.Start())) {
	if (hit_point1) *hit_point1 = ray.Start();
	if (hit_normal1) *hit_normal1 = ray.Vector();
	if (hit_t1) *hit_t1 = 0.0;
	return R2_SPAN_CLASS_ID;
    }
    else {
	// Check if ray points towards circle
	R2Vector EO = circle.Center() - ray.Start();
	RNScalar v = EO.Dot(ray.Vector());
	if (RNIsNegativeOrZero(v)) {
	    // Ray points away from circle
	    return RN_NULL_CLASS_ID;
	}
	else {
	    // Compute possible intersection -- Graphics Gems I, p. 388    
	    RNScalar disc = circle.Radius() * circle.Radius() - (EO.Dot(EO) - v*v);
	    if (RNIsNegative(disc)) {
		// No intersection
		return RN_NULL_CLASS_ID;
	    }
	    else {
		// Ray intersects circle (it grazes if disc is zero)
		// Compute first intersection
		if (hit_t1 || hit_point1 || hit_normal1) {
		    RNScalar d = sqrt(disc);
		    RNScalar t = v - d;
		    if (hit_t1) *hit_t1 = t;
		    if (hit_point1) *hit_point1 = ray.Start() + t * ray.Vector();
		    if (hit_normal1) {
			R2Point p, *pp;
			if (hit_point1) pp = hit_point1;
			else { p = ray.Start() + t * ray.Vector(); pp = &p; }
			*hit_normal1 = (*pp - circle.Centroid()) / circle.Radius();
		    }
		}

		// Compute second intersection
		// if (hit_t2 || hit_point2 || hit_normal2) {
		//     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 R2_POINT_CLASS_ID;
	        else return R2_SPAN_CLASS_ID;
	    }
	}
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r8, %rbp
movq %rcx, %rbx
movq %rdx, %r12
movq %rsi, %r14
movq %rdi, %r15
leaq 0x28(%rdi), %r13
movq %rsi, %rdi
movq %r13, %rsi
callq 0x501d0
testl %eax, %eax
je 0x50e7d
testq %r12, %r12
je 0x50e57
movq %r12, %rdi
movq %r13, %rsi
callq 0x5f6c4
testq %rbx, %rbx
je 0x50e67
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5fbfa
pushq $0x4
popq %rbx
testq %rbp, %rbp
je 0x51082
andq $0x0, (%rbp)
jmp 0x51082
movq %rbx, 0x10(%rsp)
leaq 0x8(%r14), %rsi
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rdx
callq 0x5f7c9
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5fa20
leaq 0x89d00(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm1
xorl %ebx, %ebx
ucomisd %xmm0, %xmm1
jae 0x51082
movsd 0x18(%r14), %xmm1
movsd %xmm1, (%rsp)
leaq 0x48(%rsp), %rdi
movq %rdi, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x5fa20
movsd 0x8(%rsp), %xmm3
leaq 0x89cc5(%rip), %rax     # 0xdaba8
movsd (%rsp), %xmm2
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
mulsd %xmm2, %xmm2
subsd %xmm0, %xmm2
movsd (%rax), %xmm0
xorpd 0x42128(%rip), %xmm0    # 0x93030
ucomisd %xmm2, %xmm0
ja 0x51082
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
jne 0x50f2a
testq %r12, %r12
jne 0x50f2a
testq %rbp, %rbp
je 0x5105b
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
movsd %xmm2, (%rsp)
jb 0x50f3f
sqrtsd %xmm2, %xmm0
jmp 0x50f5a
movapd %xmm2, %xmm0
callq 0xe350
movsd 0x8(%rsp), %xmm3
leaq 0x89c53(%rip), %rax     # 0xdaba8
movsd (%rsp), %xmm2
subsd %xmm0, %xmm3
testq %rbp, %rbp
je 0x50f68
movsd %xmm3, (%rbp)
testq %r12, %r12
movsd %xmm3, 0x8(%rsp)
je 0x50fb6
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movapd %xmm3, %xmm0
callq 0x5fbe5
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %rbx
callq 0x5f7b5
movq %r12, %rdi
movq %rbp, %rsi
callq 0x5f6c4
leaq 0x89bf7(%rip), %rax     # 0xdaba8
movsd (%rsp), %xmm2
testq %rbx, %rbx
je 0x5105b
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x5f58a
testq %r12, %r12
jne 0x51008
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x5fbe5
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x5f7b5
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5f6c4
movq %rbx, %r12
movq (%r14), %rax
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x38(%rax)
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x5f7c9
movsd 0x18(%r14), %xmm0
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5fde7
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x5fbfa
movsd (%rsp), %xmm2
leaq 0x89b4d(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm0
movapd 0x41fc9(%rip), %xmm1    # 0x93030
xorl %eax, %eax
ucomisd %xmm2, %xmm0
xorpd %xmm0, %xmm1
setb %al
ucomisd %xmm1, %xmm2
leal 0x1(%rax,%rax,2), %eax
pushq $0x4
popq %rbx
cmovael %eax, %ebx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq