// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/apps/msh2df/../../pkgs/R3Shapes/R3Kdtree.cpp
// Function: gaps::R3Kdtree<Point*>::FindClosest(gaps::R3KdtreeNode<Point*>*, gaps::R3Box const&, Point*, gaps::R3Point const&, double, double, int (*)(Point*, Point*, void*), void*, Point*&, double&) const
void R3Kdtree<PtrType>::
FindClosest(R3KdtreeNode<PtrType> *node, const R3Box& node_box, 
  PtrType query_point, const R3Point& query_position, 
  RNScalar min_distance_squared, RNScalar max_distance_squared,
  int (*IsCompatible)(PtrType, PtrType, void *), void *compatible_data, 
  PtrType& closest_point, RNScalar& closest_distance_squared) const
{
  // Check if node is interior
  if (node->children[0]) {
    assert(node->children[1]);

    // Find and check axial distances from point to node box
    RNLength dx, dy, dz;
    if (RNIsGreater(query_position.X(), node_box.XMax())) dx = query_position.X() - node_box.XMax();
    else if (RNIsLess(query_position.X(), node_box.XMin())) dx = node_box.XMin()- query_position.X();
    else dx = 0.0;
    RNLength dx_squared = dx * dx;
    if (dx_squared >= closest_distance_squared) return;
    if (RNIsGreater(query_position.Y(), node_box.YMax())) dy = query_position.Y() - node_box.YMax();
    else if (RNIsLess(query_position.Y(), node_box.YMin())) dy = node_box.YMin()- query_position.Y();
    else dy = 0.0;
    RNLength dy_squared = dy * dy;
    if (dy_squared >= closest_distance_squared) return;
    if (RNIsGreater(query_position.Z(), node_box.ZMax())) dz = query_position.Z() - node_box.ZMax();
    else if (RNIsLess(query_position.Z(), node_box.ZMin())) dz = node_box.ZMin()- query_position.Z();
    else dz = 0.0;
    RNLength dz_squared = dz * dz;
    if (dz_squared >= closest_distance_squared) return;
    
    // Find and check actual distance from point to node box
    RNLength 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;
    if (distance_squared >= closest_distance_squared) return;

    // Compute distance from point to split plane
    RNLength side = query_position[node->split_dimension] - node->split_coordinate;

    // Search children nodes
    if (side <= 0) {
      // Search negative side first
      R3Box child_box(node_box);
      child_box[RN_HI][node->split_dimension] = node->split_coordinate;
      FindClosest(node->children[0], child_box, 
        query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data,
        closest_point, closest_distance_squared);
      if (side*side < closest_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_LO][node->split_dimension] = node->split_coordinate;
        FindClosest(node->children[1], child_box, 
          query_point, query_position, 
          min_distance_squared, max_distance_squared, 
          IsCompatible, compatible_data,
          closest_point, closest_distance_squared);
      }
    }
    else {
      // Search positive side first
      R3Box child_box(node_box);
      child_box[RN_LO][node->split_dimension] = node->split_coordinate;
      FindClosest(node->children[1], child_box, 
        query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data,
        closest_point, closest_distance_squared);
      if (side*side < closest_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_HI][node->split_dimension] = node->split_coordinate;
        FindClosest(node->children[0], child_box, 
          query_point, query_position, 
          min_distance_squared, max_distance_squared, 
          IsCompatible, compatible_data,
          closest_point, closest_distance_squared);
      }
    }
  }
  else {
    for (int i = 0; i < node->npoints; i++) {
      PtrType point = node->points[i];
      RNLength distance_squared = R3SquaredDistance(query_position, Position(point));
      if ((distance_squared >= min_distance_squared) && 
         (distance_squared <= closest_distance_squared)) {
        if (!IsCompatible || !query_point || IsCompatible(query_point, point, compatible_data)) {
          closest_distance_squared = distance_squared;
          closest_point = point;
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x18(%rsp)
movq %rdx, %r14
movq %rdi, 0x10(%rsp)
movq 0xf0(%rsp), %rdx
cmpq $0x0, 0x8(%rsi)
movq %rsi, 0x28(%rsp)
je 0x14600
movq %rcx, %rbp
movq %r9, %r12
movq %r8, %r13
movsd (%r8), %xmm0
leaq 0xcf70e(%rip), %rax     # 0xe3bc8
movsd (%rax), %xmm3
movapd %xmm0, %xmm2
subsd 0x20(%r14), %xmm2
ucomisd %xmm3, %xmm2
ja 0x144fa
movsd 0x8(%r14), %xmm1
movapd %xmm0, %xmm4
subsd %xmm1, %xmm4
movapd 0x85b8c(%rip), %xmm5    # 0x9a070
xorpd %xmm3, %xmm5
xorpd %xmm2, %xmm2
ucomisd %xmm4, %xmm5
jbe 0x144fa
subsd %xmm0, %xmm1
movapd %xmm1, %xmm2
movapd %xmm2, %xmm1
mulsd %xmm2, %xmm1
movsd (%rdx), %xmm0
ucomisd %xmm0, %xmm1
jae 0x148d4
movq %rdx, %r15
movsd 0x8(%r13), %xmm5
movapd %xmm5, %xmm4
subsd 0x28(%r14), %xmm4
ucomisd %xmm3, %xmm4
ja 0x14558
movsd 0x10(%r14), %xmm6
movapd %xmm5, %xmm7
subsd %xmm6, %xmm7
movapd 0x85b30(%rip), %xmm8    # 0x9a070
xorpd %xmm3, %xmm8
xorpd %xmm4, %xmm4
ucomisd %xmm7, %xmm8
jbe 0x14558
subsd %xmm5, %xmm6
movapd %xmm6, %xmm4
movapd %xmm4, %xmm5
mulsd %xmm4, %xmm5
ucomisd %xmm0, %xmm5
jae 0x148d4
movsd 0x10(%r13), %xmm7
movapd %xmm7, %xmm6
subsd 0x30(%r14), %xmm6
ucomisd %xmm3, %xmm6
ja 0x145ad
movsd 0x18(%r14), %xmm8
movapd %xmm7, %xmm9
xorpd 0x85add(%rip), %xmm3    # 0x9a070
subsd %xmm8, %xmm9
xorpd %xmm6, %xmm6
ucomisd %xmm9, %xmm3
jbe 0x145ad
subsd %xmm7, %xmm8
movapd %xmm8, %xmm6
movapd %xmm6, %xmm3
mulsd %xmm6, %xmm3
ucomisd %xmm0, %xmm3
jae 0x148d4
xorpd %xmm7, %xmm7
ucomisd %xmm7, %xmm4
jne 0x145d7
jp 0x145d7
ucomisd %xmm7, %xmm6
jne 0x145d7
jnp 0x146d3
ucomisd %xmm7, %xmm2
jne 0x146b1
jp 0x146b1
ucomisd %xmm7, %xmm6
jne 0x146b1
jp 0x146b1
movapd %xmm5, %xmm1
jmp 0x146d3
movq %r8, %rbp
movq %rdx, %r15
movq %r9, 0x20(%rsp)
testq %r9, %r9
setne %al
movq %rcx, 0x30(%rsp)
testq %rcx, %rcx
setne %r12b
andb %al, %r12b
xorl %r13d, %r13d
leaq 0x38(%rsp), %r14
movslq 0x128(%rsi), %rax
cmpq %rax, %r13
jge 0x148d4
movq 0x28(%rsi,%r13,8), %rbx
movq %r14, %rdi
movq 0x10(%rsp), %rsi
movq %rbx, %rdx
callq 0x13a9a
movq %rbp, %rdi
movq %r14, %rsi
callq 0x1e1bd
ucomisd 0x18(%rsp), %xmm0
jb 0x146a4
movsd (%r15), %xmm1
ucomisd %xmm0, %xmm1
jb 0x146a4
testb %r12b, %r12b
je 0x14694
movq 0x30(%rsp), %rdi
movq %rbx, %rsi
movq 0xe0(%rsp), %rdx
movsd %xmm0, 0x8(%rsp)
callq *0x20(%rsp)
movsd 0x8(%rsp), %xmm0
testl %eax, %eax
je 0x146a4
movsd %xmm0, (%r15)
movq 0xe8(%rsp), %rax
movq %rbx, (%rax)
incq %r13
movq 0x28(%rsp), %rsi
jmp 0x14628
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm2
jne 0x146cb
jp 0x146cb
ucomisd %xmm6, %xmm4
jne 0x146cb
jp 0x146cb
movapd %xmm3, %xmm1
jmp 0x146d3
addsd %xmm5, %xmm1
addsd %xmm3, %xmm1
ucomisd %xmm0, %xmm1
jae 0x148d4
movslq 0x20(%rsi), %rax
movsd (%r13,%rax,8), %xmm1
subsd 0x18(%rsi), %xmm1
xorpd %xmm0, %xmm0
movsd %xmm1, 0x20(%rsp)
ucomisd %xmm1, %xmm0
movq %rsi, %rbx
jae 0x147eb
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x35c9e
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x40(%rsp,%rax,8)
movq 0x10(%rbx), %rsi
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %r12, %rbx
movq %r12, %r9
pushq %r15
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
callq 0x14468
addq $0x20, %rsp
movsd 0x20(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd (%r15), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x147e6
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x35c9e
movq 0x28(%rsp), %rcx
movsd 0x18(%rcx), %xmm0
movslq 0x20(%rcx), %rax
movsd %xmm0, 0x90(%rsp,%rax,8)
movq 0x8(%rcx), %rsi
subq $0x8, %rsp
leaq 0x78(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %rbp, %rcx
movq %r13, %r8
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %rbx, %r9
pushq %r15
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
callq 0x14468
addq $0x20, %rsp
leaq 0x70(%rsp), %rdi
callq 0x36966
jmp 0x148ca
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0x35c9e
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x58(%rsp,%rax,8)
movq 0x8(%rbx), %rsi
subq $0x8, %rsp
movq 0x18(%rsp), %rdi
leaq 0x40(%rsp), %rdx
movq %rbp, %rcx
movq %r13, %r8
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %r12, %rbx
movq %r12, %r9
pushq %r15
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
callq 0x14468
addq $0x20, %rsp
movsd 0x20(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd (%r15), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x148ca
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x35c9e
movq 0x28(%rsp), %rcx
movsd 0x18(%rcx), %xmm0
movslq 0x20(%rcx), %rax
movsd %xmm0, 0x78(%rsp,%rax,8)
movq 0x10(%rcx), %rsi
subq $0x8, %rsp
leaq 0x78(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %rbp, %rcx
movq %r13, %r8
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %rbx, %r9
pushq %r15
pushq 0xf8(%rsp)
pushq 0xf8(%rsp)
callq 0x14468
addq $0x20, %rsp
leaq 0x70(%rsp), %rdi
callq 0x36966
leaq 0x38(%rsp), %rdi
callq 0x36966
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x36966
jmp 0x14910
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x36966
jmp 0x14910
jmp 0x14908
jmp 0x1490d
movq %rax, %rbx
jmp 0x14910
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x36966
movq %rbx, %rdi
callq 0xe820
nop