// File: /workspace/github_cpp_makefile_star2_O1/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 $0xb8, %rsp
movq %r9, %rbx
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %r8, %r15
movq %rcx, 0x18(%rsp)
movq %rsi, %rbp
movq %rdi, 0x20(%rsp)
cmpq $0x0, 0x8(%rsi)
je 0x15d31
movq %rdx, %r13
movsd (%r15), %xmm0
leaq 0xdefd6(%rip), %rax     # 0xf4bc8
movsd (%rax), %xmm2
movapd %xmm0, %xmm1
subsd 0x20(%rdx), %xmm1
ucomisd %xmm2, %xmm1
ja 0x15c31
movsd 0x8(%r13), %xmm3
movapd %xmm0, %xmm4
subsd %xmm3, %xmm4
movapd 0x98465(%rip), %xmm5    # 0xae080
xorpd %xmm2, %xmm5
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm5
jbe 0x15c31
subsd %xmm0, %xmm3
movapd %xmm3, %xmm1
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x15f1c
movsd 0x8(%r15), %xmm4
movapd %xmm4, %xmm3
subsd 0x28(%r13), %xmm3
ucomisd %xmm2, %xmm3
ja 0x15c87
movsd 0x10(%r13), %xmm5
movapd %xmm4, %xmm6
subsd %xmm5, %xmm6
movapd 0x9840f(%rip), %xmm7    # 0xae080
xorpd %xmm2, %xmm7
xorpd %xmm3, %xmm3
ucomisd %xmm6, %xmm7
jbe 0x15c87
subsd %xmm4, %xmm5
movapd %xmm5, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
ucomisd 0x10(%rsp), %xmm4
jae 0x15f1c
movsd 0x10(%r15), %xmm6
movapd %xmm6, %xmm5
subsd 0x30(%r13), %xmm5
ucomisd %xmm2, %xmm5
ja 0x15cdc
movsd 0x18(%r13), %xmm7
movapd %xmm6, %xmm8
xorpd 0x983bc(%rip), %xmm2    # 0xae080
subsd %xmm7, %xmm8
xorpd %xmm5, %xmm5
ucomisd %xmm8, %xmm2
jbe 0x15cdc
subsd %xmm6, %xmm7
movapd %xmm7, %xmm5
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
ucomisd 0x10(%rsp), %xmm2
jae 0x15f1c
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm3
jne 0x15d08
jp 0x15d08
ucomisd %xmm6, %xmm5
jne 0x15d08
jnp 0x15e17
ucomisd %xmm6, %xmm1
jne 0x15df5
jp 0x15df5
ucomisd %xmm6, %xmm5
jne 0x15df5
jp 0x15df5
movapd %xmm4, %xmm0
jmp 0x15e17
cmpl $0x0, 0x128(%rbp)
setg %r12b
jle 0x15de1
movq %r15, %r14
movq %rbx, 0x40(%rsp)
testq %rbx, %rbx
setne %al
cmpq $0x0, 0x18(%rsp)
setne %cl
andb %al, %cl
movb %cl, 0x30(%rsp)
xorl %r15d, %r15d
leaq 0x80(%rsp), %r13
movq 0x28(%rbp,%r15,8), %rbx
movq %r13, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
callq 0x1605e
movq %r14, %rdi
movq %r13, %rsi
callq 0x24f5f
ucomisd 0x28(%rsp), %xmm0
jb 0x15dc6
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x15dc6
cmpb $0x0, 0x30(%rsp)
je 0x15dbd
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
movq 0xf0(%rsp), %rdx
callq *0x40(%rsp)
testl %eax, %eax
je 0x15dc6
xorl %eax, %eax
movq %rbx, 0x38(%rsp)
jmp 0x15dc8
movb $0x1, %al
testb %al, %al
je 0x15de1
incq %r15
movslq 0x128(%rbp), %rax
cmpq %rax, %r15
setl %r12b
jl 0x15d6a
jmp 0x15de1
testb $0x1, %r12b
movq 0x38(%rsp), %r14
je 0x15f25
jmp 0x15f28
xorpd %xmm5, %xmm5
ucomisd %xmm5, %xmm1
jne 0x15e0f
jp 0x15e0f
ucomisd %xmm5, %xmm3
jne 0x15e0f
jp 0x15e0f
movapd %xmm2, %xmm0
jmp 0x15e17
addsd %xmm4, %xmm0
addsd %xmm2, %xmm0
ucomisd 0x10(%rsp), %xmm0
jae 0x15f1c
movslq 0x20(%rbp), %rax
movsd (%r15,%rax,8), %xmm1
subsd 0x18(%rbp), %xmm1
xorpd %xmm0, %xmm0
movsd %xmm1, 0x30(%rsp)
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movq %r13, %rsi
ucomisd %xmm1, %xmm0
jae 0x15f3d
callq 0x3f4c0
movsd 0x18(%rbp), %xmm0
movslq 0x20(%rbp), %rax
movsd %xmm0, 0x88(%rsp,%rax,8)
movq 0x10(%rbp), %rsi
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movq %r14, %rdx
movq 0x18(%rsp), %rcx
movq %r15, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %rbx, %r9
callq 0x15ba8
movq %rax, %r14
testq %rax, %rax
jne 0x16009
movsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1602a
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
callq 0x3f4c0
movsd 0x18(%rbp), %xmm0
movslq 0x20(%rbp), %rax
movsd %xmm0, 0x68(%rsp,%rax,8)
movq 0x8(%rbp), %rsi
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x48(%rsp), %rdx
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rcx
movq %r15, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %rbx, %r9
callq 0x15ba8
jmp 0x15ffc
xorl %eax, %eax
xorl %r14d, %r14d
testb %al, %al
je 0x15f28
xorl %r14d, %r14d
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x3f4c0
movsd 0x18(%rbp), %xmm0
movslq 0x20(%rbp), %rax
movsd %xmm0, 0xa0(%rsp,%rax,8)
movq 0x8(%rbp), %rsi
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
movq 0x20(%rsp), %rdi
movq %r14, %rdx
movq 0x18(%rsp), %rcx
movq %r15, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %rbx, %r9
callq 0x15ba8
movq %rax, %r14
testq %rax, %rax
jne 0x16009
movsd 0x30(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1602a
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
callq 0x3f4c0
movsd 0x18(%rbp), %xmm0
movslq 0x20(%rbp), %rax
movsd %xmm0, 0x50(%rsp,%rax,8)
movq 0x10(%rbp), %rsi
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
leaq 0x48(%rsp), %rdx
movq 0x20(%rsp), %rdi
movq 0x18(%rsp), %rcx
movq %r15, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movq %rbx, %r9
callq 0x15ba8
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x40318
xorl %ebx, %ebx
leaq 0x80(%rsp), %rdi
callq 0x40318
testb %bl, %bl
je 0x16023
movb $0x1, %al
jmp 0x15f21
xorl %eax, %eax
jmp 0x15f21
movb $0x1, %bl
jmp 0x1600b
jmp 0x16030
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x40318
jmp 0x16048
jmp 0x16045
jmp 0x16045
jmp 0x16045
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x40318
movq %rbx, %rdi
callq 0xe7f0
nop