// File: /workspace/github_cpp_makefile_star2_O1/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 0x5862f
testl %eax, %eax
je 0x59443
testq %r12, %r12
je 0x59418
movq %r12, %rdi
movq %r13, %rsi
callq 0x68f66
testq %rbx, %rbx
je 0x59428
movq %rbx, %rdi
movq %r15, %rsi
callq 0x6950a
movl $0x4, %ebx
testq %rbp, %rbp
je 0x5964d
movq $0x0, (%rbp)
jmp 0x5964d
movq %rbx, 0x10(%rsp)
leaq 0x8(%r14), %rsi
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rdx
callq 0x6906b
movq %rbx, %rdi
movq %r15, %rsi
callq 0x692f6
leaq 0x8c72a(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
xorl %ebx, %ebx
ucomisd %xmm0, %xmm1
jae 0x5964d
movsd 0x18(%r14), %xmm1
movsd %xmm1, (%rsp)
leaq 0x48(%rsp), %rdi
movq %rdi, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x692f6
movsd 0x8(%rsp), %xmm3
leaq 0x8c6ef(%rip), %rax     # 0xe5b98
movsd (%rsp), %xmm2
movapd %xmm3, %xmm1
mulsd %xmm3, %xmm1
subsd %xmm1, %xmm0
mulsd %xmm2, %xmm2
subsd %xmm0, %xmm2
movsd (%rax), %xmm0
xorpd 0x48b52(%rip), %xmm0    # 0xa2020
ucomisd %xmm2, %xmm0
ja 0x5964d
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
jne 0x594f0
testq %r12, %r12
jne 0x594f0
testq %rbp, %rbp
je 0x59624
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
movsd %xmm2, (%rsp)
jb 0x59508
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
jmp 0x59523
movapd %xmm2, %xmm0
callq 0xe340
movsd 0x8(%rsp), %xmm3
leaq 0x8c67a(%rip), %rax     # 0xe5b98
movsd (%rsp), %xmm2
subsd %xmm0, %xmm3
testq %rbp, %rbp
je 0x59531
movsd %xmm3, (%rbp)
testq %r12, %r12
movsd %xmm3, 0x8(%rsp)
je 0x5957f
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movapd %xmm3, %xmm0
callq 0x694f6
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %rbx, %rdx
movq 0x10(%rsp), %rbx
callq 0x69057
movq %r12, %rdi
movq %rbp, %rsi
callq 0x68f66
leaq 0x8c61e(%rip), %rax     # 0xe5b98
movsd (%rsp), %xmm2
testq %rbx, %rbx
je 0x59624
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
callq 0x68e2c
testq %r12, %r12
jne 0x595d1
leaq 0x28(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x694f6
leaq 0x18(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x69057
movq %rbx, %rdi
movq %r15, %rsi
callq 0x68f66
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 0x6906b
movsd 0x18(%r14), %xmm0
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x696f7
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x6950a
movsd (%rsp), %xmm2
leaq 0x8c574(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x489f0(%rip), %xmm1    # 0xa2020
xorl %eax, %eax
ucomisd %xmm2, %xmm0
xorpd %xmm0, %xmm1
setb %al
ucomisd %xmm1, %xmm2
leal 0x1(%rax,%rax,2), %eax
movl $0x4, %ebx
cmovael %eax, %ebx
movl %ebx, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq