// File: /workspace/github_cpp_makefile_star2_O2/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 0x3831c
leaq 0x18(%rbx), %r15
leaq 0x68(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x3831c
movq %r13, %rdi
movq %rbp, %rsi
callq 0x38586
movapd %xmm0, %xmm2
mulsd %xmm0, %xmm2
addsd 0x4f524(%rip), %xmm2    # 0x93020
leaq 0x970a5(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm3
movapd 0x4f521(%rip), %xmm1    # 0x93030
xorpd %xmm3, %xmm1
ucomisd %xmm1, %xmm2
jb 0x43b23
ucomisd %xmm2, %xmm3
jae 0x43cad
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
leaq 0xe0(%rsp), %r13
movq %r13, %rdi
movq %r14, %rsi
movapd %xmm2, (%rsp)
movapd %xmm0, 0x40(%rsp)
callq 0x212f8
leaq 0xc8(%rsp), %rbp
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x212f8
leaq 0x80(%rsp), %r15
movq %r15, %rdi
movq %r13, %rsi
callq 0x38586
movsd %xmm0, 0x38(%rsp)
leaq 0x68(%rsp), %r12
movq %r12, %rdi
movq %rbp, %rsi
callq 0x38586
movsd %xmm0, 0x30(%rsp)
movq %r12, %rdi
movq %r13, %rsi
callq 0x38586
movsd %xmm0, 0x28(%rsp)
movq %r15, %rdi
movq %rbp, %rsi
callq 0x38586
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 0x96fb5(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm2
ucomisd %xmm0, %xmm2
jae 0x43cad
movapd %xmm0, %xmm3
subsd 0x48(%r14), %xmm3
xorpd 0x4f41d(%rip), %xmm2    # 0x93030
ucomisd %xmm2, %xmm3
jae 0x43ca9
movapd 0x4f40b(%rip), %xmm2    # 0x93030
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 0x38a02
leaq 0xb0(%rsp), %r12
movq %r12, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x37e13
leaq 0x98(%rsp), %r14
movq %r14, %rdi
movq 0x20(%rsp), %rsi
movapd (%rsp), %xmm0
callq 0x38a02
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
movq %r14, %rdx
callq 0x37e13
movq %r12, %rdi
movq %r15, %rsi
callq 0x4321a
jmp 0x43cb8
addq $0x30, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x43240
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq