// 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, int (*)(Point*, Point*, void*), void*, gaps::RNArray<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 max_points,
  int (*IsCompatible)(PtrType, PtrType, void *), void *compatible_data, 
  RNArray<PtrType>& points, RNLength *distances_squared) const
{
  // Update max distance squared
  if (points.NEntries() == max_points) {
    max_distance_squared = distances_squared[max_points-1];
  }

  // 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 > max_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 > max_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 > max_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 > max_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) || (side*side <= max_distance_squared)) {
      // Search negative side 
      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, max_points, IsCompatible, compatible_data,
        points, distances_squared);
    }
    if ((side >= 0) || (side*side <= max_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, max_points, IsCompatible, compatible_data,
        points, distances_squared);
    }
  }
  else {
    // Search points
    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 <= max_distance_squared)) {

        // Check if point is compatible
        if (!IsCompatible || !query_point || IsCompatible(query_point, point, compatible_data)) {

          // Find slot for point (points are sorted by distance)
          int slot = 0;
          while (slot < points.NEntries()) {
            if (distance_squared < distances_squared[slot]) break;
            slot++;
          }
          
          // Insert point and distance into sorted arrays
          if (slot < max_points) {
            int first = points.NEntries();
            if (first >= max_points) first = max_points-1;
            for (int j = first; j > slot; j--) distances_squared[j] = distances_squared[j-1];
            distances_squared[slot] = distance_squared;
            points.InsertKth(point, slot);
            points.Truncate(max_points);
          }
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x88, %rsp
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x38(%rsp)
movq %r8, 0x18(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, %r14
movq %rsi, %r12
movq %rdi, %r13
movq 0xd8(%rsp), %rbx
movq 0xd0(%rsp), %rax
movq %r9, 0x10(%rsp)
cmpl %r9d, 0xc(%rax)
jne 0x13ebe
movslq 0x10(%rsp), %rax
movsd -0x8(%rbx,%rax,8), %xmm0
movsd %xmm0, 0x8(%rsp)
cmpq $0x0, 0x8(%r12)
je 0x14022
movq 0x18(%rsp), %rax
movsd (%rax), %xmm0
leaq 0xcfcee(%rip), %rax     # 0xe3bc8
movsd (%rax), %xmm2
movapd %xmm0, %xmm1
subsd 0x20(%r14), %xmm1
ucomisd %xmm2, %xmm1
ja 0x13f1a
movsd 0x8(%r14), %xmm3
movapd %xmm0, %xmm4
subsd %xmm3, %xmm4
movapd 0x8616c(%rip), %xmm5    # 0x9a070
xorpd %xmm2, %xmm5
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm5
jbe 0x13f1a
subsd %xmm0, %xmm3
movapd %xmm3, %xmm1
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
ucomisd 0x8(%rsp), %xmm0
ja 0x142df
movq 0x18(%rsp), %rax
movsd 0x8(%rax), %xmm4
movapd %xmm4, %xmm3
subsd 0x28(%r14), %xmm3
ucomisd %xmm2, %xmm3
ja 0x13f74
movsd 0x10(%r14), %xmm5
movapd %xmm4, %xmm6
subsd %xmm5, %xmm6
movapd 0x86112(%rip), %xmm7    # 0x9a070
xorpd %xmm2, %xmm7
xorpd %xmm3, %xmm3
ucomisd %xmm6, %xmm7
jbe 0x13f74
subsd %xmm4, %xmm5
movapd %xmm5, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
ucomisd 0x8(%rsp), %xmm4
ja 0x142df
movq 0x18(%rsp), %rax
movsd 0x10(%rax), %xmm6
movapd %xmm6, %xmm5
subsd 0x30(%r14), %xmm5
ucomisd %xmm2, %xmm5
ja 0x13fcd
movsd 0x18(%r14), %xmm7
movapd %xmm6, %xmm8
xorpd 0x860bb(%rip), %xmm2    # 0x9a070
subsd %xmm7, %xmm8
xorpd %xmm5, %xmm5
ucomisd %xmm8, %xmm2
jbe 0x13fcd
subsd %xmm6, %xmm7
movapd %xmm7, %xmm5
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
ucomisd 0x8(%rsp), %xmm2
ja 0x142df
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm3
jne 0x13ff9
jp 0x13ff9
ucomisd %xmm6, %xmm5
jne 0x13ff9
jnp 0x1418b
ucomisd %xmm6, %xmm1
jne 0x14169
jp 0x14169
ucomisd %xmm6, %xmm5
jne 0x14169
jp 0x14169
movapd %xmm4, %xmm0
jmp 0x1418b
cmpq $0x0, 0xc0(%rsp)
setne %al
cmpq $0x0, 0x20(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0x30(%rsp)
movq 0x10(%rsp), %rax
decl %eax
movl %eax, 0x44(%rsp)
xorl %r14d, %r14d
movq %r12, 0x48(%rsp)
movslq 0x128(%r12), %rax
cmpq %rax, %r14
jge 0x142df
movq 0x28(%r12,%r14,8), %r15
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x13a9a
movq 0x18(%rsp), %rdi
movq %rbp, %rsi
callq 0x1e1bd
ucomisd 0x38(%rsp), %xmm0
jb 0x14161
movapd %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x14161
cmpb $0x0, 0x30(%rsp)
je 0x140d8
movq 0x20(%rsp), %rdi
movq %r15, %rsi
movq 0xc8(%rsp), %rdx
movsd %xmm1, 0x28(%rsp)
callq *0xc0(%rsp)
movsd 0x28(%rsp), %xmm1
testl %eax, %eax
je 0x14161
movq 0xd0(%rsp), %rax
movl 0xc(%rax), %eax
testl %eax, %eax
movl $0x0, %edx
cmovgl %eax, %edx
xorl %ecx, %ecx
cmpq %rcx, %rdx
je 0x14106
movsd (%rbx,%rcx,8), %xmm0
ucomisd %xmm1, %xmm0
ja 0x14104
incq %rcx
jmp 0x140ef
movl %ecx, %edx
cmpl 0x10(%rsp), %edx
jge 0x14161
cmpl 0x10(%rsp), %eax
cmovgel 0x44(%rsp), %eax
cltq
movslq %edx, %rcx
cmpq %rcx, %rax
jle 0x1412f
movsd -0x8(%rbx,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rax,8)
decq %rax
jmp 0x1411a
movl %edx, %eax
movsd %xmm1, (%rbx,%rax,8)
movq %r13, %rbp
movq 0xd0(%rsp), %r13
movq %r13, %rdi
movq %r15, %rsi
callq 0x6cf8c
movq %r13, %rdi
movq %rbp, %r13
movq 0x48(%rsp), %r12
movq 0x10(%rsp), %rsi
callq 0x6d138
incq %r14
jmp 0x14050
xorpd %xmm5, %xmm5
ucomisd %xmm5, %xmm1
jne 0x14183
jp 0x14183
ucomisd %xmm5, %xmm3
jne 0x14183
jp 0x14183
movapd %xmm2, %xmm0
jmp 0x1418b
addsd %xmm4, %xmm0
addsd %xmm2, %xmm0
ucomisd 0x8(%rsp), %xmm0
ja 0x142df
movslq 0x20(%r12), %rax
movq 0x18(%rsp), %rcx
movsd (%rcx,%rax,8), %xmm1
subsd 0x18(%r12), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
movapd %xmm1, %xmm2
mulsd %xmm1, %xmm2
jae 0x141cf
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jb 0x14258
movsd %xmm2, 0x28(%rsp)
movsd %xmm1, 0x30(%rsp)
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x35c9e
movsd 0x18(%r12), %xmm0
movslq 0x20(%r12), %rax
movsd %xmm0, 0x70(%rsp,%rax,8)
movq 0x8(%r12), %rsi
movq %r13, %rdi
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
movsd 0x38(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq 0x10(%rsp), %r9
pushq %rbx
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
callq 0x13e62
addq $0x20, %rsp
leaq 0x50(%rsp), %rdi
callq 0x36966
movsd 0x30(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jae 0x1426e
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jb 0x142df
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
callq 0x35c9e
movsd 0x18(%r12), %xmm0
movslq 0x20(%r12), %rax
movsd %xmm0, 0x58(%rsp,%rax,8)
movq 0x10(%r12), %rsi
movq %r13, %rdi
movq %r15, %rdx
movq 0x20(%rsp), %rcx
movq 0x18(%rsp), %r8
movsd 0x38(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq 0x10(%rsp), %r9
pushq %rbx
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
pushq 0xd8(%rsp)
callq 0x13e62
addq $0x20, %rsp
leaq 0x50(%rsp), %rdi
callq 0x36966
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x142f3
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x36966
movq %rbx, %rdi
callq 0xe820