// File: 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:
subq $0x158, %rsp            # imm = 0x158
movq %rdi, 0x148(%rsp)
movq %rsi, 0x140(%rsp)
movq 0x140(%rsp), %rdi
callq 0x392b0
movq %rax, %rsi
leaq 0x128(%rsp), %rdi
callq 0x6fcf0
movq 0x148(%rsp), %rdi
callq 0x3c0f0
movq %rax, %rsi
leaq 0x110(%rsp), %rdi
callq 0x6fcf0
movsd 0xab0ff(%rip), %xmm0    # 0x133008
movsd %xmm0, 0x108(%rsp)
movsd 0xab0ee(%rip), %xmm0    # 0x133008
movsd %xmm0, 0x100(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xf8(%rsp)
movsd 0xf8(%rsp), %xmm0
movsd 0xf8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xab0b9(%rip), %xmm1    # 0x133018
addsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0xf0(%rsp), %xmm0
leaq 0xf0564(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x87fbf
movq 0x148(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x140(%rsp), %rdi
callq 0x39250
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x86a60
movsd %xmm0, 0x150(%rsp)
jmp 0x882d5
movq 0x140(%rsp), %rdi
callq 0x39250
movq %rax, %rsi
leaq 0xd8(%rsp), %rdi
callq 0x36d10
movq 0x148(%rsp), %rdi
callq 0x3c0d0
movq %rax, %rsi
leaq 0xc0(%rsp), %rdi
callq 0x36d10
leaq 0x128(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xb8(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xb0(%rsp)
leaq 0x110(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xa8(%rsp)
leaq 0x128(%rsp), %rdi
leaq 0xc0(%rsp), %rsi
callq 0x702c0
movsd %xmm0, 0xa0(%rsp)
movsd 0xf8(%rsp), %xmm1
movsd 0xb0(%rsp), %xmm2
movsd 0xaaf7d(%rip), %xmm0    # 0x133008
mulsd 0xb8(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0xf8(%rsp), %xmm0
movsd 0xa8(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xa0(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
leaq 0xf03e4(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x382c0
cmpl $0x0, %eax
je 0x88135
movq 0x140(%rsp), %rdi
callq 0x39250
movq %rax, %rdi
movq 0x148(%rsp), %rsi
callq 0x86c70
movsd %xmm0, 0x150(%rsp)
jmp 0x882d5
movsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x140(%rsp), %rdi
callq 0x37a50
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
leaq 0xf037f(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm2
callq 0x81b80
cmpl $0x0, %eax
je 0x8819a
movq 0x140(%rsp), %rdi
callq 0x39260
movq %rax, %rdi
movq 0x148(%rsp), %rsi
callq 0x86c70
movsd %xmm0, 0x150(%rsp)
jmp 0x882d5
movsd 0xf8(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm2
movsd 0xaae54(%rip), %xmm0    # 0x133008
mulsd 0xb0(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0xf8(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm2
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd 0xa8(%rsp), %xmm1
subsd %xmm1, %xmm0
divsd 0xf0(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movq 0x140(%rsp), %rdi
callq 0x39250
movq %rax, 0x8(%rsp)
movsd 0x98(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0x140(%rsp), %rdi
callq 0x392b0
movsd (%rsp), %xmm0
movq %rax, %rsi
leaq 0x60(%rsp), %rdi
callq 0x13800
movq 0x8(%rsp), %rsi
leaq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x6f110
movq 0x148(%rsp), %rdi
callq 0x3c0d0
movq %rax, 0x18(%rsp)
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movq 0x148(%rsp), %rdi
callq 0x3c0f0
movsd 0x10(%rsp), %xmm0
movq %rax, %rsi
leaq 0x30(%rsp), %rdi
callq 0x13800
movq 0x18(%rsp), %rsi
leaq 0x48(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x6f110
leaq 0x78(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0x86c40
movsd %xmm0, 0x150(%rsp)
movsd 0x150(%rsp), %xmm0
addq $0x158, %rsp            # imm = 0x158
retq
nopw %cs:(%rax,%rax)