// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Dist.cpp
// Function: gaps::R3Distance(gaps::R3Ray const&, gaps::R3Span const&)
RNLength R3Distance(const R3Ray& ray, const R3Span& span)
{
    // There's got to be a better way ???

    // Get vectors in more convenient form
    const R3Vector v1 = ray.Vector();
    const R3Vector v2 = span.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 ray and span are parallel
    if (RNIsZero(denom)) {
        // Look at directions of vectors, then check relative starts and stops
        RNScalar dot = v1.Dot(v2);
        if (dot > 0) {
          R3Vector vse = span.End() - ray.Start();
          if (v1.Dot(vse) < 0) return R3Distance(span.End(), ray.Start());
          else return R3Distance(span.End(), ray.Line());
        }
        else {
          R3Vector vss = span.Start() - ray.Start();
          if (v1.Dot(vss) < 0) return R3Distance(span.Start(), ray.Start());
          else return R3Distance(span.Start(), ray.Line());
        }
    }
    else {
	// Find closest points
	const R3Vector p1 = ray.Start().Vector();
	const R3Vector p2 = span.Start().Vector();
	RNScalar p1v1 = v1.Dot(p1);
	RNScalar p2v2 = v2.Dot(p2);
	RNScalar p1v2 = v2.Dot(p1);
	RNScalar p2v1 = v1.Dot(p2);
	RNScalar ray_t = (v1v2*p2v2 + v2v2*p1v1 - v1v2*p1v2 - v2v2*p2v1) / denom;
	RNScalar span_t = (v1v2*p1v1 + v1v1*p2v2 - v1v2*p2v1 - v1v1*p1v2) / denom;
	R3Point ray_point = (ray_t <= 0.0) ? ray.Start() : ray.Point(ray_t);
	R3Point span_point = (span_t <= 0.0) ? span.Start() : 
	    (span_t >= span.Length()) ? span.End() : span.Ray().Point(span_t);
	RNLength distance = R3Distance(ray_point, span_point);
	return distance;
    }
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x168(%rsp)
movq %rsi, 0x160(%rsp)
movq 0x168(%rsp), %rdi
callq 0x3c0e0
movq %rax, %rsi
leaq 0x148(%rsp), %rdi
callq 0x6fcf0
movq 0x160(%rsp), %rdi
callq 0x392b0
movq %rax, %rsi
leaq 0x130(%rsp), %rdi
callq 0x6fcf0
movsd 0xaa7bf(%rip), %xmm0    # 0x133008
movsd %xmm0, 0x128(%rsp)
movsd 0xaa7ae(%rip), %xmm0    # 0x133008
movsd %xmm0, 0x120(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x118(%rsp)
movsd 0x118(%rsp), %xmm0
movsd 0x118(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xaa779(%rip), %xmm1    # 0x133018
addsd %xmm1, %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x110(%rsp), %xmm0
leaq 0xefc24(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x88a94
leaq 0x148(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x108(%rsp)
movsd 0x108(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x889cb
movq 0x160(%rsp), %rdi
callq 0x39260
movq %rax, 0x38(%rsp)
movq 0x168(%rsp), %rdi
callq 0x3c0c0
movq 0x38(%rsp), %rsi
movq %rax, %rdx
leaq 0xf0(%rsp), %rdi
callq 0x6eeb0
leaq 0x148(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x702c0
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x88991
movq 0x160(%rsp), %rdi
callq 0x39260
movq %rax, 0x30(%rsp)
movq 0x168(%rsp), %rdi
callq 0x3c0c0
movq 0x30(%rsp), %rdi
movq %rax, %rsi
callq 0x86c40
movsd %xmm0, 0x170(%rsp)
jmp 0x88d2f
movq 0x160(%rsp), %rdi
callq 0x39260
movq %rax, 0x28(%rsp)
movq 0x168(%rsp), %rdi
callq 0x81c00
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x86c70
movsd %xmm0, 0x170(%rsp)
jmp 0x88d2f
movq 0x160(%rsp), %rdi
callq 0x39250
movq %rax, 0x20(%rsp)
movq 0x168(%rsp), %rdi
callq 0x3c0c0
movq 0x20(%rsp), %rsi
movq %rax, %rdx
leaq 0xd8(%rsp), %rdi
callq 0x6eeb0
leaq 0x148(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x702c0
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x88a5a
movq 0x160(%rsp), %rdi
callq 0x39250
movq %rax, 0x18(%rsp)
movq 0x168(%rsp), %rdi
callq 0x3c0c0
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x86c40
movsd %xmm0, 0x170(%rsp)
jmp 0x88d2f
movq 0x160(%rsp), %rdi
callq 0x39250
movq %rax, 0x10(%rsp)
movq 0x168(%rsp), %rdi
callq 0x81c00
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x86c70
movsd %xmm0, 0x170(%rsp)
jmp 0x88d2f
movq 0x168(%rsp), %rdi
callq 0x3c0c0
movq %rax, %rsi
leaq 0xc0(%rsp), %rdi
callq 0x36d10
movq 0x160(%rsp), %rdi
callq 0x39250
movq %rax, %rsi
leaq 0xa8(%rsp), %rdi
callq 0x36d10
leaq 0x148(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xa0(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x98(%rsp)
leaq 0x130(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x90(%rsp)
leaq 0x148(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x88(%rsp)
movsd 0x118(%rsp), %xmm1
movsd 0x98(%rsp), %xmm2
movsd 0xaa4a8(%rip), %xmm0    # 0x133008
mulsd 0xa0(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x118(%rsp), %xmm0
movsd 0x90(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x88(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd 0x110(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x118(%rsp), %xmm1
movsd 0xa0(%rsp), %xmm2
movsd 0xaa42d(%rip), %xmm0    # 0x133008
mulsd 0x98(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x118(%rsp), %xmm0
movsd 0x88(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0x90(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd 0x110(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x80(%rsp), %xmm0
jb 0x88c63
movq 0x168(%rsp), %rdi
callq 0x3c0c0
movq %rax, %rsi
leaq 0x60(%rsp), %rdi
callq 0x6eb90
jmp 0x88c7e
movq 0x168(%rsp), %rsi
movsd 0x80(%rsp), %xmm0
leaq 0x60(%rsp), %rdi
callq 0x6e4e0
xorps %xmm0, %xmm0
ucomisd 0x78(%rsp), %xmm0
jb 0x88ca5
movq 0x160(%rsp), %rdi
callq 0x39250
movq %rax, %rsi
leaq 0x48(%rsp), %rdi
callq 0x6eb90
jmp 0x88d0b
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x160(%rsp), %rdi
callq 0x37a50
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x88ce9
movq 0x160(%rsp), %rdi
callq 0x39260
movq %rax, %rsi
leaq 0x48(%rsp), %rdi
callq 0x6eb90
jmp 0x88d09
movq 0x160(%rsp), %rdi
callq 0x81b20
movq %rax, %rsi
movsd 0x78(%rsp), %xmm0
leaq 0x48(%rsp), %rdi
callq 0x6e4e0
jmp 0x88d0b
leaq 0x60(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x86c40
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x170(%rsp)
movsd 0x170(%rsp), %xmm0
addq $0x178, %rsp            # imm = 0x178
retq