// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Isect.cpp
// Function: gaps::R2Intersects(gaps::R2Ray const&, gaps::R2Ray const&, gaps::R2Point*, double*, double*)
RNClassID R2Intersects(const R2Ray& ray1, const R2Ray& ray2, 
    R2Point *hit_point, RNScalar *hit_t1, RNScalar *hit_t2)
{
    // Get variables in more convenient form
    const R2Vector p1 = ray1.Start().Vector();
    const R2Vector p2 = ray2.Start().Vector();
    const R2Vector& v1 = ray1.Vector();
    const R2Vector& v2 = ray2.Vector();
    const R2Vector& n1 = ray1.Normal();
    const R2Vector& n2 = ray2.Normal();
    const RNScalar c1 = ray1.Line().C();
    const RNScalar c2 = ray2.Line().C();

    // Check if ray1 and ray2 are parallel
    RNScalar denom = n1.Dot(v2);
    if (RNIsZero(denom)) {
	// Check if ray1 and ray2 are coincident
	if (R2Contains(ray1.Line(), ray2.Start())) {
	    // Ray1 and ray2 share the same line
	    if (R2Contains(v1, v2)) {
	        // Rays are colinear and in same direction
	        return R2_RAY_CLASS_ID;
	    }
	    else {
	        // Rays are colinear and in opposite direction
	        if (R2Contains(ray1, ray2.Start())) return R2_SPAN_CLASS_ID;
		else return RN_NULL_CLASS_ID;
	    }
	}
	else {
	    // Rays are parallel and do not intersect
	    return RN_NULL_CLASS_ID;
	}
    }
    else {
	// Find parametric values of intersection point 
        RNScalar t1 = (c2 + n2.Dot(p1)) / denom;
        RNScalar t2 = (c1 + n1.Dot(p2)) / -denom;

	// Check if intersection point is on ray2
	if (RNIsPositiveOrZero(t1) && RNIsPositiveOrZero(t2)) {
	    if (hit_point) *hit_point = ray2.Point(t2);
	    if (hit_t1) *hit_t1 = t1;
	    if (hit_t2) *hit_t2 = t2;
	    return R2_POINT_CLASS_ID;
	}
	else {
	    // Ray1 and ray2 do not intersect
	    return RN_NULL_CLASS_ID;
	}
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %r8, 0x18(%rsp)
movq %rcx, %r14
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r13
movsd 0x28(%rdi), %xmm0
movsd 0x30(%rdi), %xmm1
leaq 0x68(%rsp), %rdi
callq 0x683d2
movsd 0x28(%r15), %xmm0
movsd 0x30(%r15), %xmm1
leaq 0x58(%rsp), %rdi
callq 0x683d2
leaq 0x10(%r13), %rbp
movups 0x20(%r13), %xmm0
movaps %xmm0, (%rsp)
movsd 0x20(%r15), %xmm0
movapd %xmm0, 0x40(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x68540
leaq 0x8d8fa(%rip), %rbx     # 0xe5b98
movsd (%rbx), %xmm2
movapd 0x49d76(%rip), %xmm1    # 0xa2020
xorpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x58307
ucomisd %xmm0, %xmm2
jb 0x58307
leaq 0x28(%r15), %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x56ee8
testl %eax, %eax
je 0x5837b
movq %r13, %rdi
movq %r15, %rsi
callq 0x56cc8
movl %eax, %ecx
movl $0x3, %eax
testl %ecx, %ecx
jne 0x583c6
movq %r13, %rdi
movq %rbx, %rsi
callq 0x57150
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
setne %al
shll $0x2, %eax
jmp 0x583c6
leaq 0x10(%r15), %rdi
leaq 0x68(%rsp), %rsi
movapd %xmm0, 0x20(%rsp)
callq 0x68540
movapd %xmm0, 0x30(%rsp)
leaq 0x58(%rsp), %rsi
movq %rbp, %rdi
callq 0x68540
movapd 0x49cea(%rip), %xmm3    # 0xa2020
movapd 0x20(%rsp), %xmm4
movapd %xmm4, %xmm1
xorpd %xmm3, %xmm1
movsd (%rbx), %xmm2
xorpd %xmm3, %xmm2
movapd (%rsp), %xmm3
unpcklpd 0x40(%rsp), %xmm3       # xmm3 = xmm3[0],mem[0]
unpcklpd 0x30(%rsp), %xmm0       # xmm0 = xmm0[0],mem[0]
addpd %xmm3, %xmm0
unpcklpd %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0]
divpd %xmm1, %xmm0
pshufd $0x44, %xmm2, %xmm1     # xmm1 = xmm2[0,1,0,1]
cmpnlepd %xmm0, %xmm1
movmskpd %xmm1, %eax
testl %eax, %eax
je 0x5837f
xorl %eax, %eax
jmp 0x583c6
testq %r12, %r12
je 0x583a9
leaq 0x78(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movapd %xmm0, (%rsp)
callq 0x67978
movq %r12, %rdi
movq %r13, %rsi
callq 0x681de
movapd (%rsp), %xmm0
testq %r14, %r14
je 0x583b3
movhpd %xmm0, (%r14)
movl $0x1, %eax
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0x583c6
movlpd %xmm0, (%rcx)
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq