// File: /workspace/github_cpp_makefile_star2_O2/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, 0x10(%rsp)
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %r13
leaq 0x28(%rdi), %rsi
leaq 0x68(%rsp), %rdi
callq 0x51538
leaq 0x28(%r15), %rbp
leaq 0x58(%rsp), %rdi
movq %rbp, %rsi
callq 0x51538
leaq 0x10(%r13), %rbx
movups 0x20(%r13), %xmm0
movaps %xmm0, (%rsp)
movsd 0x20(%r15), %xmm0
movapd %xmm0, 0x40(%rsp)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x5fa20
leaq 0x8a176(%rip), %r14     # 0xdaba8
movsd (%r14), %xmm2
movapd 0x425f1(%rip), %xmm1    # 0x93030
xorpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x50a7d
ucomisd %xmm0, %xmm2
jb 0x50a7d
movq %r13, %rdi
movq %rbp, %rsi
callq 0x4f917
testl %eax, %eax
je 0x50af2
movq %r13, %rdi
movq %r15, %rsi
callq 0x4f7bc
testl %eax, %eax
je 0x50b41
pushq $0x3
popq %rax
jmp 0x50b58
leaq 0x10(%r15), %rdi
leaq 0x68(%rsp), %rsi
movapd %xmm0, 0x20(%rsp)
callq 0x5fa20
movapd %xmm0, 0x30(%rsp)
leaq 0x58(%rsp), %rsi
movq %rbx, %rdi
callq 0x5fa20
movapd 0x42584(%rip), %xmm3    # 0x93030
movapd 0x20(%rsp), %xmm4
movapd %xmm4, %xmm1
xorpd %xmm3, %xmm1
movsd (%r14), %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 0x50af6
xorl %eax, %eax
jmp 0x50b58
testq %r12, %r12
je 0x50b20
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movapd %xmm0, (%rsp)
callq 0x5eeee
movq %r12, %rdi
movq %rbx, %rsi
callq 0x5f6c4
movapd (%rsp), %xmm0
movq 0x10(%rsp), %rax
testq %rax, %rax
je 0x50b2e
movhpd %xmm0, (%rax)
pushq $0x1
popq %rax
movq 0x18(%rsp), %rcx
testq %rcx, %rcx
je 0x50b58
movlpd %xmm0, (%rcx)
jmp 0x50b58
movq %r13, %rdi
movq %rbp, %rsi
callq 0x4faab
movl %eax, %ecx
xorl %eax, %eax
testl %ecx, %ecx
setne %al
shll $0x2, %eax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq