// File: 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:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0xf8(%rsp)
movq %rsi, 0xf0(%rsp)
movq %rdx, 0xe8(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x3c0f0
movq %rax, %rsi
leaq 0xd0(%rsp), %rdi
callq 0x6fcf0
movq 0xf0(%rsp), %rdi
callq 0x3c0f0
movq %rax, %rsi
leaq 0xb8(%rsp), %rdi
callq 0x6fcf0
movsd 0xb9cd7(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xb0(%rsp)
movsd 0xb9cc6(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xa8(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xb9c91(%rip), %xmm1    # 0x133018
addsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
leaq 0xff13c(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x793fe
movq 0xf8(%rsp), %rax
movq %rax, 0x8(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x3c0d0
movq 0x8(%rsp), %rdi
movq %rax, %rsi
callq 0x82770
cmpl $0x0, %eax
je 0x793ee
movl $0x2, 0x104(%rsp)
jmp 0x79597
movl $0x0, 0x104(%rsp)
jmp 0x79597
movq 0xf8(%rsp), %rdi
callq 0x3c0d0
movq %rax, %rsi
leaq 0x80(%rsp), %rdi
callq 0x36d10
movq 0xf0(%rsp), %rdi
callq 0x3c0d0
movq %rax, %rsi
leaq 0x68(%rsp), %rdi
callq 0x36d10
leaq 0xd0(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x60(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x58(%rsp)
leaq 0xb8(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x50(%rsp)
leaq 0xd0(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x48(%rsp)
movsd 0xa0(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0xb9b56(%rip), %xmm0    # 0x133008
mulsd 0x60(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0xa0(%rsp), %xmm0
movsd 0x50(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x48(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movq 0xf8(%rsp), %rdi
callq 0x3c0d0
movq %rax, (%rsp)
movq 0xf8(%rsp), %rdi
callq 0x3c0f0
movq %rax, %rsi
movsd 0x40(%rsp), %xmm0
leaq 0x10(%rsp), %rdi
callq 0x70b80
movq (%rsp), %rsi
leaq 0x28(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x6f110
movq 0xf0(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x82770
cmpl $0x0, %eax
je 0x7958c
cmpq $0x0, 0xe8(%rsp)
je 0x7957f
movq 0xe8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x6f1e0
movl $0x1, 0x104(%rsp)
jmp 0x79597
movl $0x0, 0x104(%rsp)
movl 0x104(%rsp), %eax
addq $0x108, %rsp            # imm = 0x108
retq
nopw %cs:(%rax,%rax)