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

    // Get vectors in more convenient form
    const R3Vector v1 = span1.Vector();
    const R3Vector v2 = span2.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 span1 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 ves = span2.Start() - span1.End();
          R3Vector vse = span2.End() - span1.Start();
          if (v1.Dot(ves) > 0) return R3Distance(span1.End(), span2.Start());
          else if (v1.Dot(vse) < 0) return R3Distance(span1.Start(), span2.End());
          else return R3Distance(span1.Start(), span2.Line());
        }
        else {
          R3Vector vee = span2.End() - span1.End();
          R3Vector vss = span2.Start() - span1.Start();
          if (v1.Dot(vss) < 0) return R3Distance(span1.Start(), span2.Start());
          else if (v1.Dot(vee) > 0) return R3Distance(span1.End(), span2.End());
          else return R3Distance(span1.Start(), span2.Line());
        }
    }
    else {
	// Find closest points
	const R3Vector p1 = span1.Start().Vector();
	const R3Vector p2 = span2.Start().Vector();
	RNScalar p1v1 = v1.Dot(p1);
	RNScalar p2v2 = v2.Dot(p2);
	RNScalar p1v2 = v2.Dot(p1);
	RNScalar p2v1 = v1.Dot(p2);
	RNScalar span1_t = (v1v2*p2v2 + v2v2*p1v1 - v1v2*p1v2 - v2v2*p2v1) / denom;
	RNScalar span2_t = (v1v2*p1v1 + v1v1*p2v2 - v1v2*p2v1 - v1v1*p1v2) / denom;
        if ((span1_t >= 0) && (span1_t <= span1.Length()) &&
            (span2_t >= 0) && (span2_t <= span2.Length())) {
	    return R3Distance(span1.Point(span1_t), span2.Point(span2_t));
        }
        else {
            // There has to be a better way!!!
            RNLength d1A = R3Distance(span1.Start(), span2);
            RNLength d1B = R3Distance(span1.End(), span2);
            RNLength d2A = R3Distance(span2.Start(), span1);
            RNLength d2B = R3Distance(span2.End(), span1);
            RNLength d = FLT_MAX;
            if (d1A < d) d = d1A;
            if (d1B < d) d = d1B;
            if (d2A < d) d = d2A;
            if (d2B < d) d = d2B;
            return d;
        }
    }
}

// Assembly:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x1d8(%rsp)
movq %rsi, 0x1d0(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x392b0
movq %rax, %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x6fcf0
movq 0x1d0(%rsp), %rdi
callq 0x392b0
movq %rax, %rsi
leaq 0x1a0(%rsp), %rdi
callq 0x6fcf0
movsd 0xa9caf(%rip), %xmm0    # 0x133008
movsd %xmm0, 0x198(%rsp)
movsd 0xa9c9e(%rip), %xmm0    # 0x133008
movsd %xmm0, 0x190(%rsp)
leaq 0x1b8(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x188(%rsp)
movsd 0x188(%rsp), %xmm0
movsd 0x188(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xa9c69(%rip), %xmm1    # 0x133018
addsd %xmm1, %xmm0
movsd %xmm0, 0x180(%rsp)
movsd 0x180(%rsp), %xmm0
leaq 0xef114(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x896bc
leaq 0x1b8(%rsp), %rdi
leaq 0x1a0(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0x178(%rsp)
movsd 0x178(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x89567
movq 0x1d0(%rsp), %rdi
callq 0x39250
movq %rax, 0x50(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x39260
movq 0x50(%rsp), %rsi
movq %rax, %rdx
leaq 0x160(%rsp), %rdi
callq 0x6eeb0
movq 0x1d0(%rsp), %rdi
callq 0x39260
movq %rax, 0x58(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x39250
movq 0x58(%rsp), %rsi
movq %rax, %rdx
leaq 0x148(%rsp), %rdi
callq 0x6eeb0
leaq 0x1b8(%rsp), %rdi
leaq 0x160(%rsp), %rsi
callq 0x702c0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x894d2
movq 0x1d8(%rsp), %rdi
callq 0x39260
movq %rax, 0x48(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x39250
movq 0x48(%rsp), %rdi
movq %rax, %rsi
callq 0x86c40
movsd %xmm0, 0x1e0(%rsp)
jmp 0x89a6b
leaq 0x1b8(%rsp), %rdi
leaq 0x148(%rsp), %rsi
callq 0x702c0
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x8952d
movq 0x1d8(%rsp), %rdi
callq 0x39250
movq %rax, 0x40(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x39260
movq 0x40(%rsp), %rdi
movq %rax, %rsi
callq 0x86c40
movsd %xmm0, 0x1e0(%rsp)
jmp 0x89a6b
movq 0x1d8(%rsp), %rdi
callq 0x39250
movq %rax, 0x38(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x81e20
movq 0x38(%rsp), %rdi
movq %rax, %rsi
callq 0x86c70
movsd %xmm0, 0x1e0(%rsp)
jmp 0x89a6b
movq 0x1d0(%rsp), %rdi
callq 0x39260
movq %rax, 0x28(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x39260
movq 0x28(%rsp), %rsi
movq %rax, %rdx
leaq 0x130(%rsp), %rdi
callq 0x6eeb0
movq 0x1d0(%rsp), %rdi
callq 0x39250
movq %rax, 0x30(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x39250
movq 0x30(%rsp), %rsi
movq %rax, %rdx
leaq 0x118(%rsp), %rdi
callq 0x6eeb0
leaq 0x1b8(%rsp), %rdi
leaq 0x118(%rsp), %rsi
callq 0x702c0
movaps %xmm0, %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x8962a
movq 0x1d8(%rsp), %rdi
callq 0x39250
movq %rax, 0x20(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x39250
movq 0x20(%rsp), %rdi
movq %rax, %rsi
callq 0x86c40
movsd %xmm0, 0x1e0(%rsp)
jmp 0x89a6b
leaq 0x1b8(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x702c0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x89682
movq 0x1d8(%rsp), %rdi
callq 0x39260
movq %rax, 0x18(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x39260
movq 0x18(%rsp), %rdi
movq %rax, %rsi
callq 0x86c40
movsd %xmm0, 0x1e0(%rsp)
jmp 0x89a6b
movq 0x1d8(%rsp), %rdi
callq 0x39250
movq %rax, 0x10(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x81e20
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x86c70
movsd %xmm0, 0x1e0(%rsp)
jmp 0x89a6b
movq 0x1d8(%rsp), %rdi
callq 0x39250
movq %rax, %rsi
leaq 0x100(%rsp), %rdi
callq 0x36d10
movq 0x1d0(%rsp), %rdi
callq 0x39250
movq %rax, %rsi
leaq 0xe8(%rsp), %rdi
callq 0x36d10
leaq 0x1b8(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xe0(%rsp)
leaq 0x1a0(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xd8(%rsp)
leaq 0x1a0(%rsp), %rdi
leaq 0x100(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xd0(%rsp)
leaq 0x1b8(%rsp), %rdi
leaq 0xe8(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xc8(%rsp)
movsd 0x188(%rsp), %xmm1
movsd 0xd8(%rsp), %xmm2
movsd 0xa9880(%rip), %xmm0    # 0x133008
mulsd 0xe0(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x188(%rsp), %xmm0
movsd 0xd0(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xc8(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd 0x180(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0x188(%rsp), %xmm1
movsd 0xe0(%rsp), %xmm2
movsd 0xa9805(%rip), %xmm0    # 0x133008
mulsd 0xd8(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x188(%rsp), %xmm0
movsd 0xc8(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xd0(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd 0x180(%rsp), %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xc0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x89941
movsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x37a50
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x89941
movsd 0xb8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x89941
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x37a50
movsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
jb 0x89941
movq 0x1d8(%rsp), %rsi
movsd 0xc0(%rsp), %xmm0
leaq 0xa0(%rsp), %rdi
callq 0x6d710
movq 0x1d0(%rsp), %rsi
movsd 0xb8(%rsp), %xmm0
leaq 0x88(%rsp), %rdi
callq 0x6d710
leaq 0xa0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x86c40
movsd %xmm0, 0x1e0(%rsp)
jmp 0x89a6b
movq 0x1d8(%rsp), %rdi
callq 0x39250
movq %rax, %rdi
movq 0x1d0(%rsp), %rsi
callq 0x86d80
movsd %xmm0, 0x80(%rsp)
movq 0x1d8(%rsp), %rdi
callq 0x39260
movq %rax, %rdi
movq 0x1d0(%rsp), %rsi
callq 0x86d80
movsd %xmm0, 0x78(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x39250
movq %rax, %rdi
movq 0x1d8(%rsp), %rsi
callq 0x86d80
movsd %xmm0, 0x70(%rsp)
movq 0x1d0(%rsp), %rdi
callq 0x39260
movq %rax, %rdi
movq 0x1d8(%rsp), %rsi
callq 0x86d80
movsd %xmm0, 0x68(%rsp)
movsd 0xa9648(%rip), %xmm0    # 0x133020
movsd %xmm0, 0x60(%rsp)
movsd 0x80(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x89a02
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x78(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x89a20
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x70(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x89a3e
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x68(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x89a5c
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x1e0(%rsp)
movsd 0x1e0(%rsp), %xmm0
addq $0x1e8, %rsp            # imm = 0x1E8
retq
nopl (%rax)