// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Isect.cpp
// Function: gaps::R3Intersects(gaps::R3Line const&, gaps::R3Line const&, gaps::R3Point*)
RNClassID R3Intersects(const R3Line& line1, const R3Line& line2, 
    R3Point *hit_point)
{
    // There's got to be a better way ???

    // Get vectors in more convenient form
    const R3Vector v1 = line1.Vector();
    const R3Vector v2 = line2.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 lines are parallel
    if (RNIsZero(denom)) {
	// Check if lines are coincident
	if (R3Contains(line1, line2.Point())) {
	    // Lines are coincident
	    return R3_LINE_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 = line1.Point().Vector();
	const R3Vector p2 = line2.Point().Vector();
	RNScalar p1v1 = v1.Dot(p1);
	RNScalar p2v2 = v2.Dot(p2);
	RNScalar p1v2 = v2.Dot(p1);
	RNScalar p2v1 = v1.Dot(p2);
	RNScalar t = (v1v2*p2v2 + v2v2*p1v1 - v1v2*p1v2 - v2v2*p2v1) / denom;
	R3Point point = line1.Point() + line1.Vector() * t;

	// Check if point is on other line
	if (R3Contains(line2, point)) {
	    // Lines intersect at point
	    if (hit_point) *hit_point = point;
	    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 $0xc8, %rsp
movq %rdx, %rbx
movq %rsi, %r14
movq %rdi, %r15
leaq 0x18(%rdi), %r12
leaq 0x38(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x3cfd6
leaq 0x18(%r14), %rsi
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
callq 0x3cfd6
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3d252
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x5fcfc(%rip), %xmm2    # 0xa2048
leaq 0xa3845(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x5fcc1(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jb 0x42389
ucomisd %xmm2, %xmm0
jb 0x42389
movq %r15, %rdi
movq %r14, %rsi
callq 0x4679f
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
addl %ebp, %ebp
jmp 0x424a7
movq %rbx, 0x8(%rsp)
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm1
movsd %xmm2, 0x30(%rsp)
movsd 0x10(%r15), %xmm2
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
movsd %xmm3, 0x28(%rsp)
callq 0x3cfc6
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
callq 0x3cfc6
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x3d252
movsd %xmm0, 0x20(%rsp)
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x3d252
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x3d252
movsd %xmm0, 0x10(%rsp)
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x3d252
movsd 0x28(%rsp), %xmm1
movsd 0x18(%rsp), %xmm2
mulsd %xmm1, %xmm2
addsd 0x20(%rsp), %xmm2
movsd 0x10(%rsp), %xmm3
mulsd %xmm1, %xmm3
subsd %xmm3, %xmm2
subsd %xmm0, %xmm2
divsd 0x30(%rsp), %xmm2
movapd %xmm2, %xmm0
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3d962
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3c877
movq %r14, %rdi
movq %r12, %rsi
callq 0x4679f
testl %eax, %eax
je 0x424a5
movl $0x1, %ebp
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x424a7
leaq 0x50(%rsp), %rsi
callq 0x3c89a
jmp 0x424a7
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq