// File: /workspace/github_cpp_makefile_star2_O2/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 $0xe8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x18(%rdi), %rsi
leaq 0x18(%rsp), %r15
movq %r15, %rdi
callq 0x3831c
leaq 0x18(%rbx), %rsi
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
callq 0x3831c
movq %r15, %rdi
movq %r12, %rsi
callq 0x38586
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x4ec6f(%rip), %xmm2    # 0x93020
leaq 0x967f0(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm3
movapd 0x4ec6c(%rip), %xmm1    # 0x93030
xorpd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
jb 0x44442
ucomisd %xmm2, %xmm3
jb 0x44442
leaq 0x18(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
callq 0x38586
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x44606
leaq 0x30(%r14), %r12
leaq 0x68(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x37d29
leaq 0x30(%rbx), %r15
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
callq 0x37d29
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x38586
ucomisd 0x4edb4(%rip), %xmm0    # 0x931e8
jbe 0x44650
movq %r12, %rdi
jmp 0x4464b
leaq 0x68(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movapd %xmm2, (%rsp)
movapd %xmm0, 0x40(%rsp)
callq 0x212f8
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x212f8
leaq 0x18(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x38586
movapd %xmm0, 0x30(%rsp)
leaq 0xa0(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x38586
movapd %xmm0, 0x80(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x38586
movapd %xmm0, 0x90(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x38586
movapd 0x40(%rsp), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd 0x30(%rsp), %xmm2
movapd %xmm2, %xmm3
movapd 0x80(%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 0x90(%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 0x4459b
movq %r14, %rdi
movq %rbx, %rsi
callq 0x432fc
movsd %xmm0, (%rsp)
leaq 0x30(%r14), %rdi
movq %rbx, %rsi
callq 0x432fc
movsd %xmm0, 0x40(%rsp)
movq %rbx, %rdi
movq %r14, %rsi
callq 0x432fc
movsd %xmm0, 0x30(%rsp)
addq $0x30, %rbx
movq %rbx, %rdi
movq %r14, %rsi
callq 0x432fc
movsd (%rsp), %xmm1
minsd 0x4ea9a(%rip), %xmm1    # 0x93018
movsd 0x40(%rsp), %xmm2
minsd %xmm1, %xmm2
movsd 0x30(%rsp), %xmm1
minsd %xmm2, %xmm1
minsd %xmm1, %xmm0
jmp 0x44692
ucomisd %xmm1, %xmm3
jb 0x4452f
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 0x4452f
testb $0x1, %al
jne 0x4452f
leaq 0xd0(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movapd %xmm3, (%rsp)
callq 0x37180
leaq 0xb8(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
movapd (%rsp), %xmm0
callq 0x37180
movq %r15, %rdi
movq %r14, %rsi
jmp 0x4468d
leaq 0x30(%rbx), %r15
leaq 0x30(%r14), %r12
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x37d29
leaq 0x50(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x37d29
leaq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x38586
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4466e
movq %r14, %rdi
movq %rbx, %rsi
jmp 0x4468d
leaq 0x18(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x38586
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x446a4
movq %r14, %rdi
jmp 0x4468a
leaq 0x18(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x38586
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x446a4
movq %r12, %rdi
movq %r15, %rsi
callq 0x4321a
addq $0xe8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, %rdi
movq %rbx, %rsi
callq 0x43240
jmp 0x44692