// File: /workspace/github_cpp_makefile_star2_O3/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 $0xd8, %rsp
movq %rsi, %rbx
movq %rdi, %r14
leaq 0x18(%rdi), %rsi
leaq 0x30(%rsp), %r15
movq %r15, %rdi
callq 0x3cfd6
leaq 0x18(%rbx), %rsi
leaq 0x90(%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 0x57e0a(%rip), %xmm2    # 0xa2048
leaq 0x9b953(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x57dcf(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
ucomisd %xmm1, %xmm2
jb 0x4a2cd
ucomisd %xmm2, %xmm0
jb 0x4a2cd
leaq 0x30(%rsp), %rdi
leaq 0x90(%rsp), %rsi
callq 0x3d252
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x4a281
addq $0x30, %rbx
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3c78e
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
callq 0x3d252
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jbe 0x4a408
leaq 0xa8(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3c78e
movq %r15, %rdi
jmp 0x4a443
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd %xmm2, 0x28(%rsp)
movsd 0x10(%r14), %xmm2
leaq 0x78(%rsp), %r15
movq %r15, %rdi
movsd %xmm3, 0x20(%rsp)
callq 0x3cfc6
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
movsd 0x10(%rbx), %xmm2
leaq 0xc0(%rsp), %r12
movq %r12, %rdi
callq 0x3cfc6
leaq 0x30(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x3d252
movsd %xmm0, 0x8(%rsp)
leaq 0x90(%rsp), %rbp
movq %rbp, %rdi
movq %r12, %rsi
callq 0x3d252
movsd %xmm0, 0x18(%rsp)
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3d252
movsd %xmm0, 0x10(%rsp)
movq %r13, %rdi
movq %r12, %rsi
callq 0x3d252
movapd %xmm0, %xmm1
movsd 0x20(%rsp), %xmm3
movapd %xmm3, %xmm0
movsd 0x18(%rsp), %xmm5
mulsd %xmm5, %xmm0
movsd 0x8(%rsp), %xmm4
addsd %xmm4, %xmm0
movapd %xmm3, %xmm2
movsd 0x10(%rsp), %xmm6
mulsd %xmm6, %xmm2
subsd %xmm2, %xmm0
subsd %xmm1, %xmm0
movsd 0x28(%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, 0x8(%rsp)
xorpd %xmm1, %xmm1
leaq 0x60(%rsp), %rdi
movq %r14, %rsi
ucomisd %xmm0, %xmm1
jae 0x4a3d9
callq 0x3c132
jmp 0x4a3de
callq 0x3c542
xorpd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jae 0x4a419
movapd %xmm1, %xmm0
ucomisd 0x48(%rbx), %xmm1
jae 0x4a415
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c132
jmp 0x4a426
movq %rbx, %rdi
movq %r14, %rsi
callq 0x4942a
jmp 0x4a448
addq $0x30, %rbx
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x3c542
leaq 0xa8(%rsp), %rbx
leaq 0x60(%rsp), %rsi
leaq 0x48(%rsp), %rdx
movq %rbx, %rdi
callq 0x3c78e
movq %rbx, %rdi
callq 0x3d0cc
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq