// File: /workspace/github_cpp_makefile_star2_O2/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 0x3831c
leaq 0x18(%r14), %rsi
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
callq 0x3831c
movq %r13, %rdi
movq %rbp, %rsi
callq 0x38586
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x56078(%rip), %xmm2    # 0x93020
leaq 0x9dbf9(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm3
movapd 0x56075(%rip), %xmm1    # 0x93030
xorpd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
jb 0x3cfe5
ucomisd %xmm2, %xmm3
jb 0x3cfe5
movq %r15, %rdi
movq %r14, %rsi
callq 0x41022
xorl %ebp, %ebp
testl %eax, %eax
setne %bpl
addl %ebp, %ebp
jmp 0x3d0e5
movq %rbx, 0x8(%rsp)
leaq 0xb0(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
movsd %xmm2, 0x30(%rsp)
movsd %xmm0, 0x28(%rsp)
callq 0x212f8
leaq 0x98(%rsp), %rbp
movq %rbp, %rdi
movq %r14, %rsi
callq 0x212f8
leaq 0x38(%rsp), %rdi
movq %r13, %rsi
callq 0x38586
movsd %xmm0, 0x20(%rsp)
leaq 0x68(%rsp), %rbx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x38586
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
movq %r13, %rsi
callq 0x38586
movsd %xmm0, 0x10(%rsp)
leaq 0x38(%rsp), %rdi
movq %rbp, %rsi
callq 0x38586
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 0x38a02
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x37e13
movq %r14, %rdi
movq %r12, %rsi
callq 0x41022
testl %eax, %eax
je 0x3d0e3
pushq $0x1
popq %rbp
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x3d0e5
leaq 0x50(%rsp), %rsi
callq 0x37e36
jmp 0x3d0e5
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq