// File: /workspace/github_cpp_makefile_star2_O2/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, %r15
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %r8, %r14
movq %rcx, %r12
movq %rsi, %rbp
movq %rdi, 0x18(%rsp)
cmpq $0x0, 0x8(%rsi)
je 0x14d53
movsd (%r14), %xmm0
leaq 0xd3fcf(%rip), %rax     # 0xe8be8
movsd (%rax), %xmm2
movapd %xmm0, %xmm1
subsd 0x20(%rdx), %xmm1
ucomisd %xmm2, %xmm1
ja 0x14c57
movsd 0x8(%rdx), %xmm3
movapd %xmm0, %xmm4
subsd %xmm3, %xmm4
movapd 0x8942f(%rip), %xmm5    # 0x9e070
xorpd %xmm2, %xmm5
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm5
jbe 0x14c57
subsd %xmm0, %xmm3
movapd %xmm3, %xmm1
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x14fe1
movsd 0x8(%r14), %xmm4
movapd %xmm4, %xmm3
subsd 0x28(%rdx), %xmm3
ucomisd %xmm2, %xmm3
ja 0x14cab
movsd 0x10(%rdx), %xmm5
movapd %xmm4, %xmm6
subsd %xmm5, %xmm6
movapd 0x893db(%rip), %xmm7    # 0x9e070
xorpd %xmm2, %xmm7
xorpd %xmm3, %xmm3
ucomisd %xmm6, %xmm7
jbe 0x14cab
subsd %xmm4, %xmm5
movapd %xmm5, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
ucomisd 0x10(%rsp), %xmm4
jae 0x14fe1
movsd 0x10(%r14), %xmm6
movapd %xmm6, %xmm5
subsd 0x30(%rdx), %xmm5
ucomisd %xmm2, %xmm5
ja 0x14cfe
movsd 0x18(%rdx), %xmm7
movapd %xmm6, %xmm8
xorpd 0x8938a(%rip), %xmm2    # 0x9e070
subsd %xmm7, %xmm8
xorpd %xmm5, %xmm5
ucomisd %xmm8, %xmm2
jbe 0x14cfe
subsd %xmm6, %xmm7
movapd %xmm7, %xmm5
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
ucomisd 0x10(%rsp), %xmm2
jae 0x14fe1
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm3
jne 0x14d2a
jp 0x14d2a
ucomisd %xmm6, %xmm5
jne 0x14d2a
jnp 0x14e05
ucomisd %xmm6, %xmm1
jne 0x14de3
jp 0x14de3
ucomisd %xmm6, %xmm5
jne 0x14de3
jp 0x14de3
movapd %xmm4, %xmm0
jmp 0x14e05
movq %r15, 0x30(%rsp)
testq %r15, %r15
setne %al
movq %r12, 0x28(%rsp)
testq %r12, %r12
setne %r15b
andb %al, %r15b
leaq 0x70(%rsp), %rbx
xorl %r12d, %r12d
movslq 0x128(%rbp), %rax
cmpq %rax, %r12
jge 0x14fe1
movq 0x28(%rbp,%r12,8), %r13
movq %rbx, %rdi
movq 0x18(%rsp), %rsi
movq %r13, %rdx
callq 0x1502a
movq %r14, %rdi
movq %rbx, %rsi
callq 0x226dd
ucomisd 0x20(%rsp), %xmm0
jb 0x14dde
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x14dde
testb %r15b, %r15b
je 0x14fe4
movq 0x28(%rsp), %rdi
movq %r13, %rsi
movq 0xe0(%rsp), %rdx
callq *0x30(%rsp)
testl %eax, %eax
jne 0x14fe4
incq %r12
jmp 0x14d75
xorpd %xmm5, %xmm5
ucomisd %xmm5, %xmm1
jne 0x14dfd
jp 0x14dfd
ucomisd %xmm5, %xmm3
jne 0x14dfd
jp 0x14dfd
movapd %xmm2, %xmm0
jmp 0x14e05
addsd %xmm4, %xmm0
addsd %xmm2, %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x14fe1
movslq 0x20(%rbp), %rax
movsd (%r14,%rax,8), %xmm1
subsd 0x18(%rbp), %xmm1
xorpd %xmm0, %xmm0
movsd %xmm1, 0x30(%rsp)
ucomisd %xmm1, %xmm0
movq %rdx, 0x28(%rsp)
leaq 0x70(%rsp), %rbx
movq %rbx, %rdi
movq %rdx, %rsi
jae 0x14f07
callq 0x3a0e6
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)
movq 0x18(%rsp), %rdi
movq %rbx, %rdx
movq %r12, %rcx
movq %r14, %r8
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %r15, %r9
callq 0x14bd4
movq %rax, %r13
testq %rax, %rax
jne 0x14fd1
movsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14ff9
leaq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x3a0e6
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)
leaq 0x38(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %r12, %rcx
movq %r14, %r8
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %r15, %r9
callq 0x14bd4
jmp 0x14fc4
callq 0x3a0e6
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)
movq 0x18(%rsp), %rdi
movq %rbx, %rdx
movq %r12, %rcx
movq %r14, %r8
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %r15, %r9
callq 0x14bd4
movq %rax, %r13
testq %rax, %rax
jne 0x14fd1
movsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x14ff9
leaq 0x38(%rsp), %rdi
movq 0x28(%rsp), %rsi
callq 0x3a0e6
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)
leaq 0x38(%rsp), %rdx
movq 0x18(%rsp), %rdi
movq %r12, %rcx
movq %r14, %r8
movsd 0x20(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %r15, %r9
callq 0x14bd4
movq %rax, %r13
leaq 0x38(%rsp), %rdi
callq 0x3ad9e
xorl %ebx, %ebx
leaq 0x70(%rsp), %rdi
callq 0x3ad9e
testb %bl, %bl
je 0x14fe4
xorl %r13d, %r13d
movq %r13, %rax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %bl
jmp 0x14fd3
jmp 0x14fff
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x3ad9e
jmp 0x15017
jmp 0x15014
jmp 0x15014
jmp 0x15014
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x3ad9e
movq %rbx, %rdi
callq 0xe820
nop