// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/msh2pts/../../pkgs/R3Shapes/R3Kdtree.cpp
// Function: gaps::R3Kdtree<Point*>::FindAny(gaps::R3KdtreeNode<Point*>*, gaps::R3Box const&, Point*, gaps::R3Point const&, double, double, int (*)(Point*, Point*, void*), void*) const
PtrType R3Kdtree<PtrType>::
FindAny(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) 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 >= max_distance_squared) return NULL;
    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 NULL;
    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 NULL;
    
    // 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 NULL;

    // 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;
      PtrType any_point = FindAny(node->children[0], child_box, 
        query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data);
      if (any_point) return any_point;
      if (side*side < max_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_LO][node->split_dimension] = node->split_coordinate;
        return FindAny(node->children[1], child_box, 
          query_point, query_position, 
          min_distance_squared, max_distance_squared, 
          IsCompatible, compatible_data);
      }
    }
    else {
      // Search positive side first
      R3Box child_box(node_box);
      child_box[RN_LO][node->split_dimension] = node->split_coordinate;
      PtrType any_point = FindAny(node->children[1], child_box, 
        query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data);
      if (any_point) return any_point;
      if (side*side < max_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_HI][node->split_dimension] = node->split_coordinate;
        return FindAny(node->children[0], child_box, 
          query_point, query_position, 
          min_distance_squared, max_distance_squared, 
          IsCompatible, compatible_data);

      }
    }
  }
  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 <= max_distance_squared)) {
        if (!IsCompatible || !query_point || IsCompatible(query_point, point, compatible_data)) {
          return point;
        }
      }
    }
  }

  // No point found
  return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movq %r9, 0x18(%rsp)
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %r8, %r14
movq %rcx, 0x10(%rsp)
movq %rsi, %rbp
movq %rdi, 0x20(%rsp)
cmpq $0x0, 0x8(%rsi)
je 0x15b9d
movq %rdx, %r13
movsd (%r14), %xmm0
leaq 0xdf16a(%rip), %rax     # 0xf4bc8
movsd (%rax), %xmm2
movapd %xmm0, %xmm1
subsd 0x20(%rdx), %xmm1
ucomisd %xmm2, %xmm1
ja 0x15a9d
movsd 0x8(%r13), %xmm3
movapd %xmm0, %xmm4
subsd %xmm3, %xmm4
movapd 0x985f9(%rip), %xmm5    # 0xae080
xorpd %xmm2, %xmm5
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm5
jbe 0x15a9d
subsd %xmm0, %xmm3
movapd %xmm3, %xmm1
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
ucomisd 0x8(%rsp), %xmm0
jae 0x15e3c
movsd 0x8(%r14), %xmm4
movapd %xmm4, %xmm3
subsd 0x28(%r13), %xmm3
ucomisd %xmm2, %xmm3
ja 0x15af3
movsd 0x10(%r13), %xmm5
movapd %xmm4, %xmm6
subsd %xmm5, %xmm6
movapd 0x985a3(%rip), %xmm7    # 0xae080
xorpd %xmm2, %xmm7
xorpd %xmm3, %xmm3
ucomisd %xmm6, %xmm7
jbe 0x15af3
subsd %xmm4, %xmm5
movapd %xmm5, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
ucomisd 0x8(%rsp), %xmm4
jae 0x15e3c
movsd 0x10(%r14), %xmm6
movapd %xmm6, %xmm5
subsd 0x30(%r13), %xmm5
ucomisd %xmm2, %xmm5
ja 0x15b48
movsd 0x18(%r13), %xmm7
movapd %xmm6, %xmm8
xorpd 0x98550(%rip), %xmm2    # 0xae080
subsd %xmm7, %xmm8
xorpd %xmm5, %xmm5
ucomisd %xmm8, %xmm2
jbe 0x15b48
subsd %xmm6, %xmm7
movapd %xmm7, %xmm5
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
ucomisd 0x8(%rsp), %xmm2
jae 0x15e3c
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm3
jne 0x15b74
jp 0x15b74
ucomisd %xmm6, %xmm5
jne 0x15b74
jnp 0x15c57
ucomisd %xmm6, %xmm1
jne 0x15c35
jp 0x15c35
ucomisd %xmm6, %xmm5
jne 0x15c35
jp 0x15c35
movapd %xmm4, %xmm0
jmp 0x15c57
cmpl $0x0, 0x128(%rbp)
jle 0x15e3c
cmpq $0x0, 0x18(%rsp)
setne %al
cmpq $0x0, 0x10(%rsp)
setne %r15b
andb %al, %r15b
leaq 0x38(%rsp), %r13
xorl %r12d, %r12d
movq 0x28(%rbp,%r12,8), %rbx
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
callq 0x15e80
movq %r14, %rdi
movq %r13, %rsi
callq 0x2478f
ucomisd 0x28(%rsp), %xmm0
jb 0x15c21
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x15c21
testb %r15b, %r15b
je 0x15e3e
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movq 0xe0(%rsp), %rdx
callq *0x18(%rsp)
testl %eax, %eax
jne 0x15e3e
incq %r12
movslq 0x128(%rbp), %rax
cmpq %rax, %r12
jl 0x15bc8
jmp 0x15e3c
xorpd %xmm5, %xmm5
ucomisd %xmm5, %xmm1
jne 0x15c4f
jp 0x15c4f
ucomisd %xmm5, %xmm3
jne 0x15c4f
jp 0x15c4f
movapd %xmm2, %xmm0
jmp 0x15c57
addsd %xmm4, %xmm0
addsd %xmm2, %xmm0
ucomisd 0x8(%rsp), %xmm0
jae 0x15e3c
movslq 0x20(%rbp), %rax
movsd (%r14,%rax,8), %xmm1
subsd 0x18(%rbp), %xmm1
xorpd %xmm0, %xmm0
movsd %xmm1, 0x30(%rsp)
leaq 0x38(%rsp), %rbx
movq %rbx, %rdi
movq %r13, %rsi
ucomisd %xmm1, %xmm0
jae 0x15d5d
callq 0x3eb08
movsd 0x18(%rbp), %xmm0
movslq 0x20(%rbp), %rax
movsd %xmm0, 0x40(%rsp,%rax,8)
movq 0x10(%rbp), %rsi
movq 0xe0(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movq %rbx, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq 0x18(%rsp), %r9
callq 0x15a12
movq %rax, %rbx
testq %rax, %rax
jne 0x15e26
movsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x15e32
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x3eb08
movsd 0x18(%rbp), %xmm0
movslq 0x20(%rbp), %rax
movsd %xmm0, 0x90(%rsp,%rax,8)
movq 0x8(%rbp), %rsi
movq 0xe0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x70(%rsp), %rdx
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rcx
movq %r14, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq 0x18(%rsp), %r9
callq 0x15a12
jmp 0x15e19
callq 0x3eb08
movsd 0x18(%rbp), %xmm0
movslq 0x20(%rbp), %rax
movsd %xmm0, 0x58(%rsp,%rax,8)
movq 0x8(%rbp), %rsi
movq 0xe0(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movq %rbx, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq 0x18(%rsp), %r9
callq 0x15a12
movq %rax, %rbx
testq %rax, %rax
jne 0x15e26
movsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x15e32
leaq 0x70(%rsp), %rdi
movq %r13, %rsi
callq 0x3eb08
movsd 0x18(%rbp), %xmm0
movslq 0x20(%rbp), %rax
movsd %xmm0, 0x78(%rsp,%rax,8)
movq 0x10(%rbp), %rsi
movq 0xe0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x70(%rsp), %rdx
movq 0x20(%rsp), %rdi
movq 0x10(%rsp), %rcx
movq %r14, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq 0x18(%rsp), %r9
callq 0x15a12
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3f928
leaq 0x38(%rsp), %rdi
callq 0x3f928
jmp 0x15e3e
leaq 0x38(%rsp), %rdi
callq 0x3f928
xorl %ebx, %ebx
movq %rbx, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x15e55
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3f928
jmp 0x15e6d
jmp 0x15e6a
jmp 0x15e6a
jmp 0x15e6a
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3f928
movq %rbx, %rdi
callq 0xe7f0
nop