// File: /workspace/github_cpp_makefile_star2_O1/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 0x3d4ce
leaq 0x18(%rbx), %rsi
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
callq 0x3d4ce
movq %r15, %rdi
movq %r12, %rsi
callq 0x3d756
movapd %xmm0, %xmm3
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x56e14(%rip), %xmm2    # 0xa2048
leaq 0x9a95d(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x56dd9(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jb 0x4b2e3
ucomisd %xmm2, %xmm0
jb 0x4b2e3
leaq 0x18(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x3d756
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4b4cb
leaq 0x30(%r14), %r12
leaq 0x88(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x3cc88
leaq 0x30(%rbx), %r15
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x3cc88
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x3d756
ucomisd 0x56f26(%rip), %xmm0    # 0xa21e8
jbe 0x4b528
leaq 0x58(%rsp), %r14
movq %r14, %rdi
movq %r12, %rsi
movq %rbx, %rdx
callq 0x3cc88
movq %r14, %rdi
jmp 0x4b580
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 0x3d4be
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
movsd 0x10(%rbx), %xmm2
leaq 0x70(%rsp), %r12
movq %r12, %rdi
callq 0x3d4be
leaq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x3d756
movapd %xmm0, 0x30(%rsp)
leaq 0xc0(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3d756
movapd %xmm0, 0xa0(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3d756
movapd %xmm0, 0xb0(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3d756
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 0x4b458
movq %r14, %rdi
movq %rbx, %rsi
callq 0x49fde
movsd %xmm0, (%rsp)
leaq 0x30(%r14), %rdi
movq %rbx, %rsi
callq 0x49fde
movsd %xmm0, 0x40(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x49fde
movsd %xmm0, 0x30(%rsp)
addq $0x30, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x49fde
movsd (%rsp), %xmm1
minsd 0x56c05(%rip), %xmm1    # 0xa2040
movsd 0x40(%rsp), %xmm2
minsd %xmm1, %xmm2
movsd 0x30(%rsp), %xmm1
minsd %xmm2, %xmm1
minsd %xmm1, %xmm0
jmp 0x4b585
ucomisd %xmm1, %xmm3
jb 0x4b3ec
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 0x4b3ec
testb $0x1, %al
jne 0x4b3ec
leaq 0xf0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movapd %xmm3, (%rsp)
callq 0x3c03a
leaq 0xd8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movapd (%rsp), %xmm0
callq 0x3c03a
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
movq %r15, %rsi
movq %r14, %rdx
jmp 0x4b578
leaq 0x30(%rbx), %r15
leaq 0x30(%r14), %r12
leaq 0x88(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3cc88
leaq 0x70(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3cc88
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x3d756
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4b54e
leaq 0x58(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x3cc88
movq %r15, %rdi
jmp 0x4b580
leaq 0x18(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x3d756
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4b597
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
movq %r14, %rsi
jmp 0x4b575
leaq 0x18(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3d756
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4b597
leaq 0x58(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3cc88
movq %rbx, %rdi
callq 0x3d5c4
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 0x49f22
jmp 0x4b585