// File: /workspace/github_cpp_makefile_star2_O3/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 $0x78, %rsp
movq %r9, 0x18(%rsp)
movsd %xmm1, 0x8(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %r8, %r14
movq %rcx, 0x10(%rsp)
movq %rsi, %r15
movq %rdi, 0x20(%rsp)
cmpq $0x0, 0x8(%rsi)
je 0x2ea07
movq %rdx, %rbx
movsd (%r14), %xmm0
leaq 0x14047b(%rip), %rax    # 0x16ed48
movsd (%rax), %xmm2
movapd %xmm0, %xmm1
subsd 0x20(%rdx), %xmm1
ucomisd %xmm2, %xmm1
ja 0x2e90b
movsd 0x8(%rbx), %xmm3
movapd %xmm0, %xmm4
subsd %xmm3, %xmm4
movapd 0xdab9b(%rip), %xmm5    # 0x109490
xorpd %xmm2, %xmm5
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm5
jbe 0x2e90b
subsd %xmm0, %xmm3
movapd %xmm3, %xmm1
movapd %xmm1, %xmm0
mulsd %xmm1, %xmm0
ucomisd 0x8(%rsp), %xmm0
ja 0x2eaa5
movsd 0x8(%r14), %xmm4
movapd %xmm4, %xmm3
subsd 0x28(%rbx), %xmm3
ucomisd %xmm2, %xmm3
ja 0x2e95f
movsd 0x10(%rbx), %xmm5
movapd %xmm4, %xmm6
subsd %xmm5, %xmm6
movapd 0xdab47(%rip), %xmm7    # 0x109490
xorpd %xmm2, %xmm7
xorpd %xmm3, %xmm3
ucomisd %xmm6, %xmm7
jbe 0x2e95f
subsd %xmm4, %xmm5
movapd %xmm5, %xmm3
movapd %xmm3, %xmm4
mulsd %xmm3, %xmm4
ucomisd 0x8(%rsp), %xmm4
ja 0x2eaa5
movsd 0x10(%r14), %xmm6
movapd %xmm6, %xmm5
subsd 0x30(%rbx), %xmm5
ucomisd %xmm2, %xmm5
ja 0x2e9b2
movsd 0x18(%rbx), %xmm7
movapd %xmm6, %xmm8
xorpd 0xdaaf6(%rip), %xmm2    # 0x109490
subsd %xmm7, %xmm8
xorpd %xmm5, %xmm5
ucomisd %xmm8, %xmm2
jbe 0x2e9b2
subsd %xmm6, %xmm7
movapd %xmm7, %xmm5
movapd %xmm5, %xmm2
mulsd %xmm5, %xmm2
ucomisd 0x8(%rsp), %xmm2
ja 0x2eaa5
xorpd %xmm6, %xmm6
ucomisd %xmm6, %xmm3
jne 0x2e9de
jp 0x2e9de
ucomisd %xmm6, %xmm5
jne 0x2e9de
jnp 0x2ead6
ucomisd %xmm6, %xmm1
jne 0x2eab4
jp 0x2eab4
ucomisd %xmm6, %xmm5
jne 0x2eab4
jp 0x2eab4
movapd %xmm4, %xmm0
jmp 0x2ead6
cmpl $0x0, 0x128(%r15)
jle 0x2eaa5
cmpq $0x0, 0x18(%rsp)
setne %al
cmpq $0x0, 0x10(%rsp)
setne %r12b
andb %al, %r12b
xorl %ebp, %ebp
leaq 0x40(%rsp), %rbx
movq 0x28(%r15,%rbp,8), %r13
movq %rbx, %rdi
movq 0x20(%rsp), %rsi
movq %r13, %rdx
callq 0x1abbc
movq %r14, %rdi
movq %rbx, %rsi
callq 0x69807
ucomisd 0x28(%rsp), %xmm0
jb 0x2ea96
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jb 0x2ea96
testb %r12b, %r12b
je 0x2ea83
movq 0x10(%rsp), %rdi
movq %r13, %rsi
movq 0xb0(%rsp), %rdx
callq *0x18(%rsp)
testl %eax, %eax
je 0x2ea96
movq 0xb8(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %r13, %rsi
callq 0xc35c2
incq %rbp
movslq 0x128(%r15), %rax
cmpq %rax, %rbp
jl 0x2ea32
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorpd %xmm5, %xmm5
ucomisd %xmm5, %xmm1
jne 0x2eace
jp 0x2eace
ucomisd %xmm5, %xmm3
jne 0x2eace
jp 0x2eace
movapd %xmm2, %xmm0
jmp 0x2ead6
addsd %xmm4, %xmm0
addsd %xmm2, %xmm0
ucomisd 0x8(%rsp), %xmm0
ja 0x2eaa5
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 0x2eb0c
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jb 0x2eb8a
movsd %xmm2, 0x30(%rsp)
movsd %xmm1, 0x38(%rsp)
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x87700
movsd 0x18(%r15), %xmm0
movslq 0x20(%r15), %rax
movsd %xmm0, 0x60(%rsp,%rax,8)
movq 0x8(%r15), %rsi
movq 0x20(%rsp), %rdi
movq %r13, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq 0x18(%rsp), %r9
pushq 0xb8(%rsp)
pushq 0xb8(%rsp)
callq 0x2e884
addq $0x10, %rsp
leaq 0x40(%rsp), %rdi
callq 0x88520
movsd 0x38(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm1
jae 0x2eba4
movsd 0x8(%rsp), %xmm0
ucomisd %xmm2, %xmm0
jb 0x2eaa5
leaq 0x40(%rsp), %r13
movq %r13, %rdi
movq %rbx, %rsi
callq 0x87700
movsd 0x18(%r15), %xmm0
movslq 0x20(%r15), %rax
movsd %xmm0, 0x48(%rsp,%rax,8)
movq 0x10(%r15), %rsi
movq 0x20(%rsp), %rdi
movq %r13, %rdx
movq 0x10(%rsp), %rcx
movq %r14, %r8
movsd 0x28(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
movq 0x18(%rsp), %r9
pushq 0xb8(%rsp)
pushq 0xb8(%rsp)
callq 0x2e884
addq $0x10, %rsp
leaq 0x40(%rsp), %rdi
callq 0x88520
jmp 0x2eaa5
jmp 0x2ec11
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x88520
movq %rbx, %rdi
callq 0x15ad0