// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::XYDistanceSquared(gaps::R3Point const&, gaps::R3Box const&)
RNLength 
XYDistanceSquared(const R3Point& point, const R3Box& box)
{
  // Find axial distances in XY plane
  RNLength dx, dy;
  if (RNIsGreater(point.X(), box.XMax())) dx = point.X() - box.XMax();
  else if (RNIsLess(point.X(), box.XMin())) dx = box.XMin()- point.X();
  else dx = 0.0;
  if (RNIsGreater(point.Y(), box.YMax())) dy = point.Y() - box.YMax();
  else if (RNIsLess(point.Y(), box.YMin())) dy = box.YMin()- point.Y();
  else dy = 0.0;
    
  // Return square of distance in XY between point and closest point in box 
  if (dy == 0.0) return dx*dx;
  else if (dx == 0.0) return dy*dy;
  else return dx*dx + dy*dy;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq %rsi, 0x50(%rsp)
movq 0x58(%rsp), %rdi
callq 0x187b0
movsd %xmm0, 0x38(%rsp)
movq 0x50(%rsp), %rdi
callq 0x44e00
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
leaq 0x1b91d0(%rip), %rax    # 0x1fc548
movsd (%rax), %xmm2
callq 0x44e60
cmpl $0x0, %eax
je 0x433b5
movq 0x58(%rsp), %rdi
callq 0x187b0
movsd %xmm0, 0x30(%rsp)
movq 0x50(%rsp), %rdi
callq 0x44e00
movaps %xmm0, %xmm1
movsd 0x30(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x43427
movq 0x58(%rsp), %rdi
callq 0x187b0
movsd %xmm0, 0x28(%rsp)
movq 0x50(%rsp), %rdi
callq 0x446e0
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
leaq 0x1b9169(%rip), %rax    # 0x1fc548
movsd (%rax), %xmm2
callq 0x44e90
cmpl $0x0, %eax
je 0x4341c
movq 0x50(%rsp), %rdi
callq 0x446e0
movsd %xmm0, 0x20(%rsp)
movq 0x58(%rsp), %rdi
callq 0x187b0
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x43425
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x43427
movq 0x58(%rsp), %rdi
callq 0x187c0
movsd %xmm0, 0x18(%rsp)
movq 0x50(%rsp), %rdi
callq 0x44e20
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
leaq 0x1b90f7(%rip), %rax    # 0x1fc548
movsd (%rax), %xmm2
callq 0x44e60
cmpl $0x0, %eax
je 0x4348e
movq 0x58(%rsp), %rdi
callq 0x187c0
movsd %xmm0, 0x10(%rsp)
movq 0x50(%rsp), %rdi
callq 0x44e20
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0x434fe
movq 0x58(%rsp), %rdi
callq 0x187c0
movsd %xmm0, 0x8(%rsp)
movq 0x50(%rsp), %rdi
callq 0x44700
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
leaq 0x1b9090(%rip), %rax    # 0x1fc548
movsd (%rax), %xmm2
callq 0x44e90
cmpl $0x0, %eax
je 0x434f3
movq 0x50(%rsp), %rdi
callq 0x44700
movsd %xmm0, (%rsp)
movq 0x58(%rsp), %rdi
callq 0x187c0
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0x434fc
xorps %xmm0, %xmm0
movsd %xmm0, 0x40(%rsp)
jmp 0x434fe
movsd 0x40(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x43523
jp 0x43523
movsd 0x48(%rsp), %xmm0
mulsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x4356e
movsd 0x48(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x43548
jp 0x43548
movsd 0x40(%rsp), %xmm0
mulsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x4356e
movsd 0x48(%rsp), %xmm0
movsd 0x48(%rsp), %xmm2
movsd 0x40(%rsp), %xmm1
mulsd 0x40(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
addq $0x68, %rsp
retq
nopl (%rax)