// File: /workspace/github_cpp_makefile_star2_O3/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, %r15
movq %rsi, %r14
movq %rdi, %r13
leaq 0x28(%rdi), %r12
movq %rsi, %rdi
movq %r12, %rsi
callq 0x57980
testl %eax, %eax
je 0x5871a
testq %r15, %r15
je 0x586ef
movq %r15, %rdi
movq %r12, %rsi
callq 0x681de
testq %rbx, %rbx
je 0x586ff
movq %rbx, %rdi
movq %r13, %rsi
callq 0x68720
movl $0x4, %ebx
testq %rbp, %rbp
je 0x5892f
movq $0x0, (%rbp)
jmp 0x5892f
movq %rbx, 0x10(%rsp)
leaq 0x8(%r14), %rsi
leaq 0x48(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rdx
callq 0x682e3
movq %rbx, %rdi
movq %r13, %rsi
callq 0x68540
leaq 0x8d453(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm1
xorl %ebx, %ebx
ucomisd %xmm0, %xmm1
jae 0x5892f
movsd 0x18(%r14), %xmm1
movsd %xmm1, (%rsp)
leaq 0x48(%rsp), %rdi
movq %rdi, %rsi
movsd %xmm0, 0x8(%rsp)
callq 0x68540
movsd 0x8(%rsp), %xmm3
leaq 0x8d418(%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 0x4987b(%rip), %xmm0    # 0xa2020
ucomisd %xmm2, %xmm0
ja 0x5892f
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
jne 0x587c7
testq %r15, %r15
jne 0x587c7
testq %rbp, %rbp
je 0x58906
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm2
movsd %xmm2, (%rsp)
jb 0x587df
xorps %xmm0, %xmm0
sqrtsd %xmm2, %xmm0
jmp 0x587ee
movapd %xmm2, %xmm0
callq 0xe340
movsd 0x8(%rsp), %xmm3
subsd %xmm0, %xmm3
testq %rbp, %rbp
je 0x587fc
movsd %xmm3, (%rbp)
testq %r15, %r15
je 0x58857
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movapd %xmm3, %xmm0
callq 0x6870c
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x682cf
movq %r15, %rdi
movq %r13, %rsi
callq 0x681de
cmpq $0x0, 0x10(%rsp)
movsd (%rsp), %xmm2
leaq 0x8d353(%rip), %rax     # 0xe5b98
je 0x58906
leaq 0x28(%rsp), %rdi
callq 0x680a4
jmp 0x588b3
testq %rbx, %rbx
movsd (%rsp), %xmm2
leaq 0x8d332(%rip), %rax     # 0xe5b98
je 0x58906
leaq 0x28(%rsp), %r15
movq %r15, %rdi
movsd %xmm3, 0x8(%rsp)
callq 0x680a4
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movsd 0x8(%rsp), %xmm0
callq 0x6870c
leaq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x682cf
movq %r15, %rdi
movq %r13, %rsi
callq 0x681de
movq (%r14), %rax
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq *0x38(%rax)
leaq 0x38(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x682e3
movsd 0x18(%r14), %xmm0
leaq 0x18(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x6890d
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
callq 0x68720
movsd (%rsp), %xmm2
leaq 0x8d292(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x4970e(%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