// File: /workspace/github_cpp_makefile_star2_O3/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x108, %rsp            # imm = 0x108
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x18(%rdi), %rsi
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x3cfd6
leaq 0x18(%rbx), %rsi
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
callq 0x3cfd6
movq %r15, %rdi
movq %r12, %rsi
callq 0x3d252
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x57914(%rip), %xmm2    # 0xa2048
leaq 0x9b45d(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x578d9(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jb 0x4a7e3
ucomisd %xmm2, %xmm0
jb 0x4a7e3
leaq 0x18(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3d252
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4a9cb
leaq 0x30(%r14), %r12
leaq 0x88(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3c78e
leaq 0x30(%rbx), %r15
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3c78e
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x3d252
ucomisd 0x57a26(%rip), %xmm0    # 0xa21e8
jbe 0x4aa28
leaq 0x58(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3c78e
movq %r14, %rdi
jmp 0x4aa80
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movapd %xmm2, (%rsp)
movsd 0x10(%r14), %xmm2
leaq 0x88(%rsp), %r15
movq %r15, %rdi
movapd %xmm3, 0x40(%rsp)
callq 0x3cfc6
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
movsd 0x10(%rbx), %xmm2
leaq 0x70(%rsp), %r12
movq %r12, %rdi
callq 0x3cfc6
leaq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x3d252
movapd %xmm0, 0x30(%rsp)
leaq 0xc0(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3d252
movapd %xmm0, 0xa0(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3d252
movapd %xmm0, 0xb0(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3d252
movapd 0x40(%rsp), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd 0x30(%rsp), %xmm2
movapd %xmm2, %xmm3
movapd 0xa0(%rsp), %xmm4
unpcklpd %xmm4, %xmm3            # xmm3 = xmm3[0],xmm4[0]
unpcklpd %xmm2, %xmm4            # xmm4 = xmm4[0],xmm2[0]
mulpd %xmm1, %xmm3
movapd %xmm1, %xmm2
addpd %xmm4, %xmm3
movapd %xmm0, %xmm1
movapd 0xb0(%rsp), %xmm4
unpcklpd %xmm4, %xmm1            # xmm1 = xmm1[0],xmm4[0]
mulpd %xmm2, %xmm1
subpd %xmm1, %xmm3
unpcklpd %xmm0, %xmm4            # xmm4 = xmm4[0],xmm0[0]
subpd %xmm4, %xmm3
movapd (%rsp), %xmm0
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
divpd %xmm0, %xmm3
movapd %xmm3, %xmm0
unpckhpd %xmm3, %xmm0            # xmm0 = xmm0[1],xmm3[1]
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jae 0x4a958
movq %r14, %rdi
movq %rbx, %rsi
callq 0x494e6
movsd %xmm0, (%rsp)
leaq 0x30(%r14), %rdi
movq %rbx, %rsi
callq 0x494e6
movsd %xmm0, 0x40(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x494e6
movsd %xmm0, 0x30(%rsp)
addq $0x30, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x494e6
movsd (%rsp), %xmm1
minsd 0x57705(%rip), %xmm1    # 0xa2040
movsd 0x40(%rsp), %xmm2
minsd %xmm1, %xmm2
movsd 0x30(%rsp), %xmm1
minsd %xmm2, %xmm1
minsd %xmm1, %xmm0
jmp 0x4aa85
ucomisd %xmm1, %xmm3
jb 0x4a8ec
movsd 0x48(%rbx), %xmm1
movhpd 0x48(%r14), %xmm1       # xmm1 = xmm1[0],mem[0]
movapd %xmm3, %xmm2
cmpnlepd %xmm1, %xmm2
movmskpd %xmm2, %eax
movl %eax, %ecx
shrb %cl
jne 0x4a8ec
testb $0x1, %al
jne 0x4a8ec
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movapd %xmm3, (%rsp)
callq 0x3bb46
leaq 0xd8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movapd (%rsp), %xmm0
callq 0x3bb46
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
jmp 0x4aa78
leaq 0x30(%rbx), %r15
leaq 0x30(%r14), %r12
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3c78e
leaq 0x70(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3c78e
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x3d252
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4aa4e
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x3c78e
movq %r15, %rdi
jmp 0x4aa80
leaq 0x18(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3d252
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4aa97
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x4aa75
leaq 0x18(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3d252
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4aa97
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3c78e
movq %rbx, %rdi
callq 0x3d0cc
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4942a
jmp 0x4aa85