// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshSearchTree.cpp
// Function: gaps::R3MeshSearchTree::DistanceSquared(gaps::R3Point const&, gaps::R3Box const&, double) const
RNScalar R3MeshSearchTree::
DistanceSquared(const R3Point& query_position, const R3Box& box, RNScalar max_distance_squared) const
{
  // Find and check axial distances from face to node box
  RNScalar dx, dy, dz;
  if (query_position.X() > box.XMax()) dx = query_position.X() - box.XMax();
  else if (query_position.X() < box.XMin()) dx = box.XMin()- query_position.X();
  else dx = 0.0;
  RNScalar dx_squared = dx * dx;
  if (dx_squared >= max_distance_squared) return dx_squared;
  if (query_position.Y() > box.YMax()) dy = query_position.Y() - box.YMax();
  else if (query_position.Y() < box.YMin()) dy = box.YMin()- query_position.Y();
  else dy = 0.0;
  RNScalar dy_squared = dy * dy;
  if (dy_squared >= max_distance_squared) return dy_squared;
  if (query_position.Z() > box.ZMax()) dz = query_position.Z() - box.ZMax();
  else if (query_position.Z() < box.ZMin()) dz = box.ZMin()- query_position.Z();
  else dz = 0.0;
  RNScalar dz_squared = dz * dz;
  if (dz_squared >= max_distance_squared) return dz_squared;
    
  // Find and check actual distance from face to node box
  RNScalar distance_squared = 0;
  if ((dy == 0.0) && (dz == 0.0)) distance_squared = dx_squared;
  else if ((dx == 0.0) && (dz == 0.0)) distance_squared = dy_squared;
  else if ((dx == 0.0) && (dy == 0.0)) distance_squared = dz_squared;
  else distance_squared = dx_squared + dy_squared + dz_squared;

  // Return distance squared
  return distance_squared;
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movsd %xmm0, 0xa0(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x178f0
movsd %xmm0, 0x60(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a010
movaps %xmm0, %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1d41f
movq 0xb0(%rsp), %rdi
callq 0x178f0
movsd %xmm0, 0x58(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a010
movaps %xmm0, %xmm1
movsd 0x58(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
jmp 0x1d491
movq 0xb0(%rsp), %rdi
callq 0x178f0
movsd %xmm0, 0x50(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a060
movsd 0x50(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1d483
movq 0xa8(%rsp), %rdi
callq 0x1a060
movsd %xmm0, 0x48(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x178f0
movaps %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x98(%rsp)
jmp 0x1d48f
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
jmp 0x1d491
movsd 0x98(%rsp), %xmm0
mulsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
ucomisd 0xa0(%rsp), %xmm0
jb 0x1d4d7
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
jmp 0x1d7e2
movq 0xb0(%rsp), %rdi
callq 0x17900
movsd %xmm0, 0x40(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a080
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1d53e
movq 0xb0(%rsp), %rdi
callq 0x17900
movsd %xmm0, 0x38(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a080
movaps %xmm0, %xmm1
movsd 0x38(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
jmp 0x1d5b0
movq 0xb0(%rsp), %rdi
callq 0x17900
movsd %xmm0, 0x30(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a0a0
movsd 0x30(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1d5a2
movq 0xa8(%rsp), %rdi
callq 0x1a0a0
movsd %xmm0, 0x28(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x17900
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
jmp 0x1d5ae
xorps %xmm0, %xmm0
movsd %xmm0, 0x90(%rsp)
jmp 0x1d5b0
movsd 0x90(%rsp), %xmm0
mulsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
ucomisd 0xa0(%rsp), %xmm0
jb 0x1d5ed
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
jmp 0x1d7e2
movq 0xb0(%rsp), %rdi
callq 0x17910
movsd %xmm0, 0x20(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a0c0
movaps %xmm0, %xmm1
movsd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1d654
movq 0xb0(%rsp), %rdi
callq 0x17910
movsd %xmm0, 0x18(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a0c0
movaps %xmm0, %xmm1
movsd 0x18(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
jmp 0x1d6c6
movq 0xb0(%rsp), %rdi
callq 0x17910
movsd %xmm0, 0x10(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1a0e0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x1d6b8
movq 0xa8(%rsp), %rdi
callq 0x1a0e0
movsd %xmm0, 0x8(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x17910
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
jmp 0x1d6c4
xorps %xmm0, %xmm0
movsd %xmm0, 0x88(%rsp)
jmp 0x1d6c6
movsd 0x88(%rsp), %xmm0
mulsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x70(%rsp), %xmm0
ucomisd 0xa0(%rsp), %xmm0
jb 0x1d703
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
jmp 0x1d7e2
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x90(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1d748
jp 0x1d748
movsd 0x88(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1d748
jp 0x1d748
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x1d7d3
movsd 0x98(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1d77e
jp 0x1d77e
movsd 0x88(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1d77e
jp 0x1d77e
movsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x1d7d1
movsd 0x98(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1d7b4
jp 0x1d7b4
movsd 0x90(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1d7b4
jp 0x1d7b4
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x1d7cf
movsd 0x80(%rsp), %xmm0
addsd 0x78(%rsp), %xmm0
addsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0x1d7d1
jmp 0x1d7d3
movsd 0x68(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm0
addq $0xc8, %rsp
retq
nopw %cs:(%rax,%rax)