// File: /workspace/github_cpp_makefile_star2_O2/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 0x38586
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x55ee7(%rip), %xmm2    # 0x93020
leaq 0x9da68(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm3
movapd 0x55ee4(%rip), %xmm1    # 0x93030
xorpd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
jb 0x3d176
ucomisd %xmm2, %xmm3
jb 0x3d176
movq %r15, %rdi
movq %r12, %rsi
callq 0x41022
xorl %ecx, %ecx
testl %eax, %eax
setne %cl
leal (%rcx,%rcx,2), %eax
jmp 0x3d282
movq %r14, 0x8(%rsp)
movq %rbx, 0x10(%rsp)
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movsd %xmm2, 0x30(%rsp)
movsd %xmm0, 0x28(%rsp)
callq 0x212f8
leaq 0x68(%rsp), %r14
movq %r14, %rdi
movq %r15, %rsi
callq 0x212f8
movq %r13, %rdi
movq %rbx, %rsi
callq 0x38586
movsd %xmm0, 0x20(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
callq 0x38586
movsd %xmm0, (%rsp)
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x38586
movsd %xmm0, 0x18(%rsp)
movq %r13, %rdi
movq %r14, %rsi
callq 0x38586
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 0x38a02
leaq 0x38(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x37e13
movq %r15, %rdi
movq %r14, %rsi
callq 0x41022
testl %eax, %eax
je 0x3d280
movq 0x8(%rsp), %rdi
testq %rdi, %rdi
je 0x3d268
leaq 0x38(%rsp), %rsi
callq 0x37e36
pushq $0x1
popq %rax
movq 0x10(%rsp), %rcx
testq %rcx, %rcx
je 0x3d282
movsd (%rsp), %xmm0
movsd %xmm0, (%rcx)
jmp 0x3d282
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq