// File: 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:
subq $0x298, %rsp            # imm = 0x298
movq 0x2a0(%rsp), %rax
movq %rdi, 0x288(%rsp)
movq %rsi, 0x280(%rsp)
movq %rdx, 0x278(%rsp)
movq %rcx, 0x270(%rsp)
movq %r8, 0x268(%rsp)
movsd %xmm0, 0x260(%rsp)
movsd %xmm1, 0x258(%rsp)
movq %r9, 0x250(%rsp)
movq 0x288(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x280(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1cdcd
movq 0x268(%rsp), %rdi
callq 0x1b340
movsd %xmm0, 0xd0(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1cf50
movaps %xmm0, %xmm1
movsd 0xd0(%rsp), %xmm0
leaq 0x175136(%rip), %rax    # 0x191510
movsd (%rax), %xmm2
callq 0x1cf20
cmpl $0x0, %eax
je 0x1c429
movq 0x268(%rsp), %rdi
callq 0x1b340
movsd %xmm0, 0xc8(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1cf50
movaps %xmm0, %xmm1
movsd 0xc8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x248(%rsp)
jmp 0x1c4b9
movq 0x268(%rsp), %rdi
callq 0x1b340
movsd %xmm0, 0xc0(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1cfa0
movaps %xmm0, %xmm1
movsd 0xc0(%rsp), %xmm0
leaq 0x1750b1(%rip), %rax    # 0x191510
movsd (%rax), %xmm2
callq 0x1cf70
cmpl $0x0, %eax
je 0x1c4ab
movq 0x278(%rsp), %rdi
callq 0x1cfa0
movsd %xmm0, 0xb8(%rsp)
movq 0x268(%rsp), %rdi
callq 0x1b340
movaps %xmm0, %xmm1
movsd 0xb8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x248(%rsp)
jmp 0x1c4b7
xorps %xmm0, %xmm0
movsd %xmm0, 0x248(%rsp)
jmp 0x1c4b9
movsd 0x248(%rsp), %xmm0
mulsd 0x248(%rsp), %xmm0
movsd %xmm0, 0x230(%rsp)
movsd 0x230(%rsp), %xmm0
ucomisd 0x258(%rsp), %xmm0
jb 0x1c4f9
movq $0x0, 0x290(%rsp)
jmp 0x1cef8
movq 0x268(%rsp), %rdi
callq 0x1b350
movsd %xmm0, 0xb0(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1cfc0
movaps %xmm0, %xmm1
movsd 0xb0(%rsp), %xmm0
leaq 0x174fe1(%rip), %rax    # 0x191510
movsd (%rax), %xmm2
callq 0x1cf20
cmpl $0x0, %eax
je 0x1c57e
movq 0x268(%rsp), %rdi
callq 0x1b350
movsd %xmm0, 0xa8(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1cfc0
movaps %xmm0, %xmm1
movsd 0xa8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x240(%rsp)
jmp 0x1c60e
movq 0x268(%rsp), %rdi
callq 0x1b350
movsd %xmm0, 0xa0(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1cfe0
movaps %xmm0, %xmm1
movsd 0xa0(%rsp), %xmm0
leaq 0x174f5c(%rip), %rax    # 0x191510
movsd (%rax), %xmm2
callq 0x1cf70
cmpl $0x0, %eax
je 0x1c600
movq 0x278(%rsp), %rdi
callq 0x1cfe0
movsd %xmm0, 0x98(%rsp)
movq 0x268(%rsp), %rdi
callq 0x1b350
movaps %xmm0, %xmm1
movsd 0x98(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x240(%rsp)
jmp 0x1c60c
xorps %xmm0, %xmm0
movsd %xmm0, 0x240(%rsp)
jmp 0x1c60e
movsd 0x240(%rsp), %xmm0
mulsd 0x240(%rsp), %xmm0
movsd %xmm0, 0x228(%rsp)
movsd 0x228(%rsp), %xmm0
ucomisd 0x258(%rsp), %xmm0
jb 0x1c64e
movq $0x0, 0x290(%rsp)
jmp 0x1cef8
movq 0x268(%rsp), %rdi
callq 0x1b360
movsd %xmm0, 0x90(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1d000
movaps %xmm0, %xmm1
movsd 0x90(%rsp), %xmm0
leaq 0x174e8c(%rip), %rax    # 0x191510
movsd (%rax), %xmm2
callq 0x1cf20
cmpl $0x0, %eax
je 0x1c6d3
movq 0x268(%rsp), %rdi
callq 0x1b360
movsd %xmm0, 0x88(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1d000
movaps %xmm0, %xmm1
movsd 0x88(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x238(%rsp)
jmp 0x1c75d
movq 0x268(%rsp), %rdi
callq 0x1b360
movsd %xmm0, 0x80(%rsp)
movq 0x278(%rsp), %rdi
callq 0x1d020
movaps %xmm0, %xmm1
movsd 0x80(%rsp), %xmm0
leaq 0x174e07(%rip), %rax    # 0x191510
movsd (%rax), %xmm2
callq 0x1cf70
cmpl $0x0, %eax
je 0x1c74f
movq 0x278(%rsp), %rdi
callq 0x1d020
movsd %xmm0, 0x78(%rsp)
movq 0x268(%rsp), %rdi
callq 0x1b360
movaps %xmm0, %xmm1
movsd 0x78(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x238(%rsp)
jmp 0x1c75b
xorps %xmm0, %xmm0
movsd %xmm0, 0x238(%rsp)
jmp 0x1c75d
movsd 0x238(%rsp), %xmm0
mulsd 0x238(%rsp), %xmm0
movsd %xmm0, 0x220(%rsp)
movsd 0x220(%rsp), %xmm0
ucomisd 0x258(%rsp), %xmm0
jb 0x1c79d
movq $0x0, 0x290(%rsp)
jmp 0x1cef8
xorps %xmm0, %xmm0
movsd %xmm0, 0x218(%rsp)
movsd 0x240(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1c7e8
jp 0x1c7e8
movsd 0x238(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1c7e8
jp 0x1c7e8
movsd 0x230(%rsp), %xmm0
movsd %xmm0, 0x218(%rsp)
jmp 0x1c888
movsd 0x248(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1c824
jp 0x1c824
movsd 0x238(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1c824
jp 0x1c824
movsd 0x228(%rsp), %xmm0
movsd %xmm0, 0x218(%rsp)
jmp 0x1c886
movsd 0x248(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1c860
jp 0x1c860
movsd 0x240(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1c860
jp 0x1c860
movsd 0x220(%rsp), %xmm0
movsd %xmm0, 0x218(%rsp)
jmp 0x1c884
movsd 0x230(%rsp), %xmm0
addsd 0x228(%rsp), %xmm0
addsd 0x220(%rsp), %xmm0
movsd %xmm0, 0x218(%rsp)
jmp 0x1c886
jmp 0x1c888
movsd 0x218(%rsp), %xmm0
ucomisd 0x258(%rsp), %xmm0
jb 0x1c8ad
movq $0x0, 0x290(%rsp)
jmp 0x1cef8
movq 0x268(%rsp), %rdi
movq 0x280(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x1c240
movq 0x280(%rsp), %rax
subsd 0x18(%rax), %xmm0
movsd %xmm0, 0x210(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x210(%rsp), %xmm0
jb 0x1cb5c
movq 0x278(%rsp), %rsi
leaq 0x1d8(%rsp), %rdi
movq %rdi, 0x68(%rsp)
callq 0x70db0
movq 0x68(%rsp), %rdi
movq 0x280(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x60(%rsp)
movl $0x1, %esi
callq 0x1b3c0
movq %rax, %rdi
movq 0x280(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x1b400
movsd 0x60(%rsp), %xmm0
movq 0xd8(%rsp), %rdi
movq 0x68(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x280(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x270(%rsp), %rcx
movq 0x268(%rsp), %r8
movsd 0x260(%rsp), %xmm0
movsd 0x258(%rsp), %xmm1
movq 0x250(%rsp), %r9
movq 0x2a0(%rsp), %r10
movq %rsp, %rax
movq %r10, (%rax)
callq 0x1c330
movq %rax, 0x70(%rsp)
jmp 0x1c9a3
movq 0x70(%rsp), %rax
movq %rax, 0x1d0(%rsp)
cmpq $0x0, 0x1d0(%rsp)
je 0x1c9f4
movq 0x1d0(%rsp), %rax
movq %rax, 0x290(%rsp)
movl $0x1, 0x1c0(%rsp)
jmp 0x1cb26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
jmp 0x1cb4a
movsd 0x210(%rsp), %xmm1
mulsd 0x210(%rsp), %xmm1
movsd 0x258(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1cb1b
movq 0x278(%rsp), %rsi
leaq 0x188(%rsp), %rdi
callq 0x70db0
jmp 0x1ca30
movq 0x280(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
leaq 0x188(%rsp), %rdi
movq %rdi, 0x50(%rsp)
xorl %esi, %esi
callq 0x1b3c0
movq %rax, %rdi
movq 0x280(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x1b400
movsd 0x48(%rsp), %xmm0
movq 0xd8(%rsp), %rdi
movq 0x50(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x280(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x270(%rsp), %rcx
movq 0x268(%rsp), %r8
movsd 0x260(%rsp), %xmm0
movsd 0x258(%rsp), %xmm1
movq 0x250(%rsp), %r9
movq 0x2a0(%rsp), %r10
movq %rsp, %rax
movq %r10, (%rax)
callq 0x1c330
movq %rax, 0x58(%rsp)
jmp 0x1cad1
movq 0x58(%rsp), %rax
movq %rax, 0x290(%rsp)
movl $0x1, 0x1c0(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x1b2c0
jmp 0x1cb26
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
leaq 0x188(%rsp), %rdi
callq 0x1b2c0
jmp 0x1cb4a
movl $0x0, 0x1c0(%rsp)
leaq 0x1d8(%rsp), %rdi
callq 0x1b2c0
movl 0x1c0(%rsp), %eax
testl %eax, %eax
je 0x1cb45
jmp 0x1cb40
jmp 0x1cef8
jmp 0x1cdc8
leaq 0x1d8(%rsp), %rdi
callq 0x1b2c0
jmp 0x1cf08
movq 0x278(%rsp), %rsi
leaq 0x150(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x70db0
movq 0x38(%rsp), %rdi
movq 0x280(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
xorl %esi, %esi
callq 0x1b3c0
movq %rax, %rdi
movq 0x280(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x1b400
movsd 0x30(%rsp), %xmm0
movq 0xd8(%rsp), %rdi
movq 0x38(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x280(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x270(%rsp), %rcx
movq 0x268(%rsp), %r8
movsd 0x260(%rsp), %xmm0
movsd 0x258(%rsp), %xmm1
movq 0x250(%rsp), %r9
movq 0x2a0(%rsp), %r10
movq %rsp, %rax
movq %r10, (%rax)
callq 0x1c330
movq %rax, 0x40(%rsp)
jmp 0x1cc0f
movq 0x40(%rsp), %rax
movq %rax, 0x148(%rsp)
cmpq $0x0, 0x148(%rsp)
je 0x1cc60
movq 0x148(%rsp), %rax
movq %rax, 0x290(%rsp)
movl $0x1, 0x1c0(%rsp)
jmp 0x1cd95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
jmp 0x1cdb6
movsd 0x210(%rsp), %xmm1
mulsd 0x210(%rsp), %xmm1
movsd 0x258(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1cd8a
movq 0x278(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x70db0
jmp 0x1cc9c
movq 0x280(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
leaq 0x110(%rsp), %rdi
movq %rdi, 0x20(%rsp)
movl $0x1, %esi
callq 0x1b3c0
movq %rax, %rdi
movq 0x280(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x1b400
movsd 0x18(%rsp), %xmm0
movq 0xd8(%rsp), %rdi
movq 0x20(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x280(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x270(%rsp), %rcx
movq 0x268(%rsp), %r8
movsd 0x260(%rsp), %xmm0
movsd 0x258(%rsp), %xmm1
movq 0x250(%rsp), %r9
movq 0x2a0(%rsp), %r10
movq %rsp, %rax
movq %r10, (%rax)
callq 0x1c330
movq %rax, 0x28(%rsp)
jmp 0x1cd40
movq 0x28(%rsp), %rax
movq %rax, 0x290(%rsp)
movl $0x1, 0x1c0(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x1b2c0
jmp 0x1cd95
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1c8(%rsp)
movl %eax, 0x1c4(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x1b2c0
jmp 0x1cdb6
movl $0x0, 0x1c0(%rsp)
leaq 0x150(%rsp), %rdi
callq 0x1b2c0
movl 0x1c0(%rsp), %eax
testl %eax, %eax
je 0x1cdb4
jmp 0x1cdaf
jmp 0x1cef8
jmp 0x1cdc8
leaq 0x150(%rsp), %rdi
callq 0x1b2c0
jmp 0x1cf08
jmp 0x1ceec
movl $0x0, 0x10c(%rsp)
movl 0x10c(%rsp), %eax
movq 0x280(%rsp), %rcx
cmpl 0x128(%rcx), %eax
jge 0x1ceea
movq 0xd8(%rsp), %rsi
movq 0x280(%rsp), %rax
movslq 0x10c(%rsp), %rcx
movq 0x28(%rax,%rcx,8), %rax
movq %rax, 0x100(%rsp)
movq 0x268(%rsp), %rax
movq %rax, 0x10(%rsp)
movq 0x100(%rsp), %rdx
leaq 0xe0(%rsp), %rdi
callq 0x1d040
movq 0x10(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x3a5d0
movsd %xmm0, 0xf8(%rsp)
movsd 0xf8(%rsp), %xmm0
ucomisd 0x260(%rsp), %xmm0
jb 0x1ced2
movsd 0xf8(%rsp), %xmm1
movsd 0x258(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x1ced2
cmpq $0x0, 0x250(%rsp)
je 0x1cebe
cmpq $0x0, 0x270(%rsp)
je 0x1cebe
movq 0x250(%rsp), %rax
movq 0x270(%rsp), %rdi
movq 0x100(%rsp), %rsi
movq 0x2a0(%rsp), %rdx
callq *%rax
cmpl $0x0, %eax
je 0x1ced0
movq 0x100(%rsp), %rax
movq %rax, 0x290(%rsp)
jmp 0x1cef8
jmp 0x1ced2
jmp 0x1ced4
movl 0x10c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x1cdd8
jmp 0x1ceec
movq $0x0, 0x290(%rsp)
movq 0x290(%rsp), %rax
addq $0x298, %rsp            # imm = 0x298
retq
movq 0x1c8(%rsp), %rdi
callq 0xf8a0
nopw %cs:(%rax,%rax)
nop