// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Utils/../../pkgs/R3Shapes/R3Kdtree.cpp
// Function: gaps::R3Kdtree<gaps::R3SegmentationPoint*>::FindAll(gaps::R3KdtreeNode<gaps::R3SegmentationPoint*>*, gaps::R3Box const&, gaps::R3SegmentationPoint*, gaps::R3Point const&, double, double, int (*)(gaps::R3SegmentationPoint*, gaps::R3SegmentationPoint*, void*), void*, gaps::RNArray<gaps::R3SegmentationPoint*>&) const
void R3Kdtree<PtrType>::
FindAll(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, 
  RNArray<PtrType>& points) 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;
    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;
      FindAll(node->children[0], child_box, query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data, points);
    }
    if ((side >= 0) || (side*side <= max_distance_squared)) {
      R3Box child_box(node_box);
      child_box[RN_LO][node->split_dimension] = node->split_coordinate;
      FindAll(node->children[1], child_box, query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data, points);
    }
  }
  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)) {
        if (!IsCompatible || !query_point || IsCompatible(query_point, point, compatible_data)) {
          points.Insert(point);
        }
      }
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %r8, %r14
movq %rsi, %r15
movq %rdi, 0x20(%rsp)
cmpq $0x0, 0x8(%rsi)
je 0x2c795
movq %rdx, %r12
movq %rcx, %rbp
movq %r9, %r13
movsd (%r14), %xmm0
leaq 0x12f717(%rip), %rax    # 0x15bd68
movsd (%rax), %xmm2
movapd %xmm0, %xmm1
subsd 0x20(%rdx), %xmm1
ucomisd %xmm2, %xmm1
ja 0x2c691
movsd 0x8(%r12), %xmm3
movapd %xmm0, %xmm4
subsd %xmm3, %xmm4
movapd 0xc1e15(%rip), %xmm5    # 0xee490
xorpd %xmm2, %xmm5
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm5
jbe 0x2c691
subsd %xmm0, %xmm3
movapd %xmm3, %xmm1
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
ucomisd 0x8(%rsp), %xmm0
ja 0x2c976
movsd 0x8(%r14), %xmm4
movapd %xmm4, %xmm3
subsd 0x28(%r12), %xmm3
ucomisd %xmm2, %xmm3
ja 0x2c6e9
movsd 0x10(%r12), %xmm5
movapd %xmm4, %xmm6
subsd %xmm5, %xmm6
movapd 0xc1dbd(%rip), %xmm7    # 0xee490
xorpd %xmm2, %xmm7
xorpd %xmm3, %xmm3
ucomisd %xmm6, %xmm7
jbe 0x2c6e9
subsd %xmm4, %xmm5
movapd %xmm5, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
ucomisd 0x8(%rsp), %xmm4
ja 0x2c976
movsd 0x10(%r14), %xmm6
movapd %xmm6, %xmm5
subsd 0x30(%r12), %xmm5
ucomisd %xmm2, %xmm5
ja 0x2c740
movsd 0x18(%r12), %xmm7
movapd %xmm6, %xmm8
xorpd 0xc1d68(%rip), %xmm2    # 0xee490
subsd %xmm7, %xmm8
xorpd %xmm5, %xmm5
ucomisd %xmm8, %xmm2
jbe 0x2c740
subsd %xmm6, %xmm7
movapd %xmm7, %xmm5
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
ucomisd 0x8(%rsp), %xmm2
ja 0x2c976
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm3
jne 0x2c76c
jp 0x2c76c
ucomisd %xmm6, %xmm5
jne 0x2c76c
jnp 0x2c84e
ucomisd %xmm6, %xmm1
jne 0x2c82c
jp 0x2c82c
ucomisd %xmm6, %xmm5
jne 0x2c82c
jp 0x2c82c
movapd %xmm4, %xmm0
jmp 0x2c84e
movq %r9, 0x18(%rsp)
testq %r9, %r9
setne %al
movq %rcx, 0x10(%rsp)
testq %rcx, %rcx
setne %r13b
andb %al, %r13b
xorl %ebp, %ebp
leaq 0x30(%rsp), %r12
movslq 0x128(%r15), %rax
cmpq %rax, %rbp
jge 0x2c976
movq 0x28(%r15,%rbp,8), %rbx
movq %r12, %rdi
movq 0x20(%rsp), %rsi
movq %rbx, %rdx
callq 0x1a086
movq %r14, %rdi
movq %r12, %rsi
callq 0x60e51
ucomisd 0x28(%rsp), %xmm0
jb 0x2c827
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2c827
testb %r13b, %r13b
je 0x2c817
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
movq 0xa0(%rsp), %rdx
callq *0x18(%rsp)
testl %eax, %eax
je 0x2c827
movq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1a04c
incq %rbp
jmp 0x2c7b6
xorpd %xmm5, %xmm5
ucomisd %xmm5, %xmm1
jne 0x2c846
jp 0x2c846
ucomisd %xmm5, %xmm3
jne 0x2c846
jp 0x2c846
movapd %xmm2, %xmm0
jmp 0x2c84e
addsd %xmm4, %xmm0
addsd %xmm2, %xmm0
ucomisd 0x8(%rsp), %xmm0
ja 0x2c976
movslq 0x20(%r15), %rax
movsd (%r14,%rax,8), %xmm1
subsd 0x18(%r15), %xmm1
xorpd %xmm0, %xmm0
ucomisd %xmm1, %xmm0
movapd %xmm1, %xmm2
mulsd %xmm1, %xmm2
jae 0x2c888
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jb 0x2c900
movsd %xmm2, 0x10(%rsp)
movsd %xmm1, 0x18(%rsp)
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7bef2
movsd 0x18(%r15), %xmm0
movslq 0x20(%r15), %rax
movsd %xmm0, 0x50(%rsp,%rax,8)
movq 0x8(%r15), %rsi
movq 0x20(%rsp), %rdi
movq %rbx, %rdx
movq %rbp, %rcx
movq %r14, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq %r13, %r9
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
callq 0x2c60c
popq %rax
popq %rcx
leaq 0x30(%rsp), %rdi
callq 0x7cbba
movsd 0x18(%rsp), %xmm1
movsd 0x10(%rsp), %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jae 0x2c916
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jb 0x2c976
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x7bef2
movsd 0x18(%r15), %xmm0
movslq 0x20(%r15), %rax
movsd %xmm0, 0x38(%rsp,%rax,8)
movq 0x10(%r15), %rsi
movq 0x20(%rsp), %rdi
movq %rbx, %rdx
movq %rbp, %rcx
movq %r14, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq %r13, %r9
pushq 0xa8(%rsp)
pushq 0xa8(%rsp)
callq 0x2c60c
popq %rax
popq %rcx
leaq 0x30(%rsp), %rdi
callq 0x7cbba
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2c987
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x7cbba
movq %rbx, %rdi
callq 0x15af0