// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Dist.cpp
// Function: gaps::R3Distance(gaps::R3Line const&, gaps::R3Span const&)
RNLength R3Distance(const R3Line& line, const R3Span& span)
{
    // NOTE: THIS HAS NOT BEEN TESTED, BUT SHOULD WORK
  
    // Get vectors in more convenient form
    const R3Vector v1 = span.Vector();
    const R3Vector v2 = line.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 parallel
    if (RNIsZero(denom)) {
        // Return distance to any point on ray
        return R3Distance(line, span.Start());
    }
    else {
	// Find point on span closest to line
	const R3Vector p1 = span.Start().Vector();
	const R3Vector p2 = line.Point().Vector();
	RNScalar p1v1 = v1.Dot(p1);
	RNScalar p2v2 = v2.Dot(p2);
	RNScalar p1v2 = v2.Dot(p1);
	RNScalar p2v1 = v1.Dot(p2);
	RNScalar t1 = (v1v2*p2v2 + v2v2*p1v1 - v1v2*p1v2 - v2v2*p2v1) / denom;
        if (RNIsNegativeOrZero(t1)) {
            // Closest point is behind start of span, return distance to start
            return R3Distance(span.Start(), line); 
        }
        else if (RNIsGreaterOrEqual(t1, span.Length())) {
            // Closest point is past end of span, return distance to end
            return R3Distance(span.End(), line); 
        }
        else {
            // Return distance to closest point within span
            RNScalar t2 = (v1v2*p1v1 + v1v1*p2v2 - v1v2*p2v1 - v1v1*p1v2) / denom;
            R3Point point1 = span.Start() + t1*span.Vector();
            R3Point point2 = line.Point() + t2*line.Vector();
            return R3Distance(point1, point2);
        }
    }
    
    // Commented out because compiler complains :(
    // RNAbort("Should not get here");
    // return 0.0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xf8, %rsp
movq %rsi, %r14
movq %rdi, %rbx
leaq 0x18(%rsi), %r12
leaq 0x80(%rsp), %r13
movq %r13, %rdi
movq %r12, %rsi
callq 0x3cfd6
leaq 0x18(%rbx), %r15
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3cfd6
movq %r13, %rdi
movq %rbp, %rsi
callq 0x3d252
movapd %xmm0, %xmm4
movapd %xmm0, %xmm3
mulsd %xmm0, %xmm3
addsd 0x582a0(%rip), %xmm3    # 0xa2048
leaq 0x9bde9(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x58265(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
ucomisd %xmm1, %xmm3
jb 0x49dcf
ucomisd %xmm3, %xmm0
jae 0x49f82
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movsd (%r14), %xmm0
movsd 0x8(%r14), %xmm1
movsd 0x10(%r14), %xmm2
leaq 0xe0(%rsp), %r13
movq %r13, %rdi
movapd %xmm3, (%rsp)
movapd %xmm4, 0x40(%rsp)
callq 0x3cfc6
movsd (%rbx), %xmm0
movsd 0x8(%rbx), %xmm1
movsd 0x10(%rbx), %xmm2
leaq 0xc8(%rsp), %rbp
movq %rbp, %rdi
callq 0x3cfc6
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x3d252
movsd %xmm0, 0x38(%rsp)
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x3d252
movsd %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x3d252
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x3d252
movsd 0x28(%rsp), %xmm8
movsd 0x30(%rsp), %xmm7
movsd 0x38(%rsp), %xmm6
movapd 0x40(%rsp), %xmm5
movapd (%rsp), %xmm4
movapd %xmm0, %xmm1
movapd %xmm5, %xmm0
mulsd %xmm7, %xmm0
addsd %xmm6, %xmm0
movapd %xmm5, %xmm2
mulsd %xmm8, %xmm2
subsd %xmm2, %xmm0
subsd %xmm1, %xmm0
divsd %xmm4, %xmm0
leaq 0x9bce0(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm2
ucomisd %xmm0, %xmm2
jae 0x49f82
movapd %xmm0, %xmm3
subsd 0x48(%r14), %xmm3
xorpd 0x58148(%rip), %xmm2    # 0xa2020
ucomisd %xmm2, %xmm3
jae 0x49f7e
movapd 0x58136(%rip), %xmm2    # 0xa2020
xorpd %xmm5, %xmm2
mulsd %xmm5, %xmm6
addsd %xmm7, %xmm6
mulsd %xmm1, %xmm2
addsd %xmm6, %xmm2
subsd %xmm8, %xmm2
divsd %xmm4, %xmm2
movapd %xmm2, (%rsp)
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq 0x18(%rsp), %rsi
callq 0x3d962
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3c877
leaq 0x50(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movapd (%rsp), %xmm0
callq 0x3d962
leaq 0x98(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x3c877
leaq 0x50(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x3c78e
movq %rbx, %rdi
callq 0x3d0cc
jmp 0x49f8d
addq $0x30, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4942a
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq