// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Dist.cpp
// Function: gaps::R3Distance(gaps::R3Line const&, gaps::R3Ray const&)
RNLength R3Distance(const R3Line& line, const R3Ray& ray)
{
    // NOTE: THIS HAS NOT BEEN TESTED, BUT SHOULD WORK
  
    // Get vectors in more convenient form
    const R3Vector v1 = ray.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, ray.Start());
    }
    else {
	// Find point on ray closest to line
	const R3Vector p1 = ray.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 ray, return distance to start
            return R3Distance(ray.Start(), line); 
        }
        else {
            // Return distance to closest point
            RNScalar t2 = (v1v2*p1v1 + v1v1*p2v2 - v1v2*p2v1 - v1v1*p1v2) / denom;
            R3Point point1 = ray.Start() + t1*ray.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 0x3c0e0
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 0xab4df(%rip), %xmm0    # 0x133008
movsd %xmm0, 0x108(%rsp)
movsd 0xab4ce(%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 0xab499(%rip), %xmm1    # 0x133018
addsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0xf0(%rsp), %xmm0
leaq 0xf0944(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x36b80
cmpl $0x0, %eax
je 0x87bdf
movq 0x148(%rsp), %rax
movq %rax, 0x28(%rsp)
movq 0x140(%rsp), %rdi
callq 0x3c0c0
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x86a60
movsd %xmm0, 0x150(%rsp)
jmp 0x87e92
movq 0x140(%rsp), %rdi
callq 0x3c0c0
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 0xab35d(%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 0xf07c4(%rip), %rax     # 0x1784e0
movsd (%rax), %xmm1
callq 0x382c0
cmpl $0x0, %eax
je 0x87d55
movq 0x140(%rsp), %rdi
callq 0x3c0c0
movq %rax, %rdi
movq 0x148(%rsp), %rsi
callq 0x86c70
movsd %xmm0, 0x150(%rsp)
jmp 0x87e92
movsd 0xf8(%rsp), %xmm1
movsd 0xb8(%rsp), %xmm2
movsd 0xab299(%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 0x3c0c0
movq %rax, 0x10(%rsp)
movsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x140(%rsp), %rdi
callq 0x3c0e0
movsd 0x8(%rsp), %xmm0
movq %rax, %rsi
leaq 0x60(%rsp), %rdi
callq 0x13800
movq 0x10(%rsp), %rsi
leaq 0x78(%rsp), %rdi
leaq 0x60(%rsp), %rdx
callq 0x6f110
movq 0x148(%rsp), %rdi
callq 0x3c0d0
movq %rax, 0x20(%rsp)
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x148(%rsp), %rdi
callq 0x3c0f0
movsd 0x18(%rsp), %xmm0
movq %rax, %rsi
leaq 0x30(%rsp), %rdi
callq 0x13800
movq 0x20(%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)