// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Line const&, gaps::R3Ray const&, gaps::R3Point*, double*)
RNClassID R3Intersects(const R3Line& line, const R3Ray& ray, 
    R3Point *hit_point, RNScalar *hit_t)
{
    // There's got to be a better way ???

    // Get vectors in more convenient form
    const R3Vector& v1 = ray.Vector();
    const R3Vector& v2 = line.Vector();

    // Compute useful intermediate values
    const RNScalar v1v1 = 1.0;  // v1.Dot(v1);
    const RNScalar v2v2 = 1.0;  // v2.Dot(v2);
    RNScalar v1v2 = v1.Dot(v2);
    RNScalar denom = v1v2*v1v2 - v1v1*v2v2;

    // Check if line and ray are parallel
    if (RNIsZero(denom)) {
	// Check if line and ray are coincident
	if (R3Contains(line, ray.Start())) {
	    // Line and ray are coincident
	    return R3_RAY_CLASS_ID;
	}
	else {
	    // Lines are parallel and do not intersect
	    return RN_NULL_CLASS_ID;
	}
    }
    else {
	// Find point on this line closest to other line
	const R3Vector p1 = ray.Start().Vector();
	const R3Vector p2 = line.Point().Vector();
	RNScalar p1v1 = v1.Dot(p1);
	RNScalar p2v2 = v2.Dot(p2);
	RNScalar p1v2 = v2.Dot(p1);
	RNScalar p2v1 = v1.Dot(p2);
	RNScalar s = (v1v2*p2v2 + v2v2*p1v1 - v1v2*p1v2 - v2v2*p2v1) / denom;
	R3Point point = ray.Start() + ray.Vector() * s;

	// Check if point is on other line
	if (R3Contains(line, point)) {
	    // Lines intersect at point
	    if (hit_point) *hit_point = point;
	    if (hit_t) *hit_t = s;
	    return R3_POINT_CLASS_ID;
	}
	else {
	    // Lines do not intersect
	    return RN_NULL_CLASS_ID;
	}
    }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rcx, %rbx
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r15
leaq 0x18(%rsi), %r13
leaq 0x18(%rdi), %rbp
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3d756
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x5f5f5(%rip), %xmm2    # 0xa2048
leaq 0xa313e(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x5f5ba(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jb 0x42a90
ucomisd %xmm2, %xmm0
jb 0x42a90
movq %r15, %rdi
movq %r12, %rsi
callq 0x4708f
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
leal (%rcx,%rcx,2), %eax
jmp 0x42bbd
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
movsd (%r12), %xmm0
movsd 0x8(%r12), %xmm1
movsd %xmm2, 0x30(%rsp)
movsd 0x10(%r12), %xmm2
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movsd %xmm3, 0x28(%rsp)
callq 0x3d4be
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movsd 0x10(%r15), %xmm2
leaq 0x68(%rsp), %r14
movq %r14, %rdi
callq 0x3d4be
movq %r13, %rdi
movq %rbx, %rsi
callq 0x3d756
movsd %xmm0, 0x20(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x3d756
movsd %xmm0, (%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x3d756
movsd %xmm0, 0x18(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x3d756
movsd 0x28(%rsp), %xmm1
movsd (%rsp), %xmm2
mulsd %xmm1, %xmm2
addsd 0x20(%rsp), %xmm2
movsd 0x18(%rsp), %xmm3
mulsd %xmm1, %xmm3
subsd %xmm3, %xmm2
subsd %xmm0, %xmm2
divsd 0x30(%rsp), %xmm2
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
movsd %xmm2, (%rsp)
movapd %xmm2, %xmm0
callq 0x3de8e
leaq 0x38(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3cd71
movq %r15, %rdi
movq %r14, %rsi
callq 0x4708f
testl %eax, %eax
je 0x42bbb
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x42ba1
leaq 0x38(%rsp), %rsi
callq 0x3cd94
movl $0x1, %eax
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x42bbd
movsd (%rsp), %xmm0
movsd %xmm0, (%rcx)
jmp 0x42bbd
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq