// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x18(%rdi), %rsi
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x3831c
leaq 0x18(%rbx), %rsi
leaq 0x88(%rsp), %r12
movq %r12, %rdi
callq 0x3831c
movq %r15, %rdi
movq %r12, %rsi
callq 0x38586
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x4f0ee(%rip), %xmm2    # 0x93020
leaq 0x96c6f(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm3
movapd 0x4f0eb(%rip), %xmm1    # 0x93030
xorpd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
jb 0x43fb3
ucomisd %xmm2, %xmm3
jb 0x43fb3
leaq 0x28(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x38586
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x43f75
addq $0x30, %rbx
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x37d29
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x38586
xorpd %xmm1, %xmm1
movq %rbx, %rdi
movq %r14, %rsi
ucomisd %xmm0, %xmm1
ja 0x440ec
callq 0x43240
jmp 0x440f1
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
movsd %xmm2, 0x20(%rsp)
movsd %xmm0, 0x18(%rsp)
callq 0x212f8
leaq 0xa0(%rsp), %r12
movq %r12, %rdi
movq %rbx, %rsi
callq 0x212f8
leaq 0x28(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x38586
movsd %xmm0, (%rsp)
leaq 0x88(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x38586
movsd %xmm0, 0x10(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x38586
movsd %xmm0, 0x8(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x38586
movapd %xmm0, %xmm1
movsd 0x18(%rsp), %xmm3
movapd %xmm3, %xmm0
movsd 0x10(%rsp), %xmm5
mulsd %xmm5, %xmm0
movsd (%rsp), %xmm4
addsd %xmm4, %xmm0
movapd %xmm3, %xmm2
movsd 0x8(%rsp), %xmm6
mulsd %xmm6, %xmm2
subsd %xmm2, %xmm0
subsd %xmm1, %xmm0
movsd 0x20(%rsp), %xmm2
divsd %xmm2, %xmm0
mulsd %xmm3, %xmm4
addsd %xmm5, %xmm4
mulsd %xmm1, %xmm3
subsd %xmm3, %xmm4
subsd %xmm6, %xmm4
divsd %xmm2, %xmm4
movsd %xmm4, (%rsp)
xorpd %xmm1, %xmm1
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
ucomisd %xmm0, %xmm1
jae 0x440a3
callq 0x37770
jmp 0x440a8
callq 0x37b5c
xorpd %xmm0, %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x440d5
movapd %xmm1, %xmm0
ucomisd 0x48(%rbx), %xmm1
jae 0x440d1
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x37770
jmp 0x440e2
addq $0x30, %rbx
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
callq 0x37b5c
leaq 0x58(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x4321a
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq