// File: 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:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xe0(%rsp)
movq %rdx, 0xd8(%rsp)
movq %rcx, 0xd0(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x3c0e0
movq %rax, 0xc8(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x3c0f0
movq %rax, 0xc0(%rsp)
movsd 0xb99ff(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xb8(%rsp)
movsd 0xb99ee(%rip), %xmm0    # 0x133008
movsd %xmm0, 0xb0(%rsp)
movq 0xc8(%rsp), %rdi
movq 0xc0(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
movsd 0xa8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xb99b9(%rip), %xmm1    # 0x133018
addsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
leaq 0xfee64(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x796d6
movq 0xe8(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x3c0c0
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x82770
cmpl $0x0, %eax
je 0x796c6
movl $0x3, 0xf4(%rsp)
jmp 0x7988e
movl $0x0, 0xf4(%rsp)
jmp 0x7988e
movq 0xe0(%rsp), %rdi
callq 0x3c0c0
movq %rax, %rsi
leaq 0x88(%rsp), %rdi
callq 0x36d10
movq 0xe8(%rsp), %rdi
callq 0x3c0d0
movq %rax, %rsi
leaq 0x70(%rsp), %rdi
callq 0x36d10
movq 0xc8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x68(%rsp)
movq 0xc0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x60(%rsp)
movq 0xc0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x58(%rsp)
movq 0xc8(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x50(%rsp)
movsd 0xa8(%rsp), %xmm1
movsd 0x60(%rsp), %xmm2
movsd 0xb987e(%rip), %xmm0    # 0x133008
mulsd 0x68(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0xa8(%rsp), %xmm0
movsd 0x58(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x50(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x48(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x3c0c0
movq %rax, 0x8(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x3c0e0
movq %rax, %rsi
movsd 0x48(%rsp), %xmm0
leaq 0x18(%rsp), %rdi
callq 0x70b80
movq 0x8(%rsp), %rsi
leaq 0x30(%rsp), %rdi
leaq 0x18(%rsp), %rdx
callq 0x6f110
movq 0xe8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x82770
cmpl $0x0, %eax
je 0x79883
cmpq $0x0, 0xd8(%rsp)
je 0x79859
movq 0xd8(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0x6f1e0
cmpq $0x0, 0xd0(%rsp)
je 0x79876
movsd 0x48(%rsp), %xmm0
movq 0xd0(%rsp), %rax
movsd %xmm0, (%rax)
movl $0x1, 0xf4(%rsp)
jmp 0x7988e
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
addq $0xf8, %rsp
retq
nopl (%rax)