// File: 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:
subq $0x1d8, %rsp            # imm = 0x1D8
movq 0x1e8(%rsp), %rax
movq 0x1e0(%rsp), %rax
movq %rdi, 0x1d0(%rsp)
movq %rsi, 0x1c8(%rsp)
movq %rdx, 0x1c0(%rsp)
movq %rcx, 0x1b8(%rsp)
movq %r8, 0x1b0(%rsp)
movsd %xmm0, 0x1a8(%rsp)
movsd %xmm1, 0x1a0(%rsp)
movq %r9, 0x198(%rsp)
movq 0x1d0(%rsp), %rax
movq %rax, 0xa0(%rsp)
movq 0x1c8(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x48af9
movq 0x1b0(%rsp), %rdi
callq 0x1efb0
movsd %xmm0, 0x98(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x48c60
movaps %xmm0, %xmm1
movsd 0x98(%rsp), %xmm0
leaq 0x220346(%rip), %rax    # 0x268748
movsd (%rax), %xmm2
callq 0x48c30
cmpl $0x0, %eax
je 0x48451
movq 0x1b0(%rsp), %rdi
callq 0x1efb0
movsd %xmm0, 0x90(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x48c60
movaps %xmm0, %xmm1
movsd 0x90(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x190(%rsp)
jmp 0x484e1
movq 0x1b0(%rsp), %rdi
callq 0x1efb0
movsd %xmm0, 0x88(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x48cb0
movaps %xmm0, %xmm1
movsd 0x88(%rsp), %xmm0
leaq 0x2202c1(%rip), %rax    # 0x268748
movsd (%rax), %xmm2
callq 0x48c80
cmpl $0x0, %eax
je 0x484d3
movq 0x1c0(%rsp), %rdi
callq 0x48cb0
movsd %xmm0, 0x80(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x1efb0
movaps %xmm0, %xmm1
movsd 0x80(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x190(%rsp)
jmp 0x484df
xorps %xmm0, %xmm0
movsd %xmm0, 0x190(%rsp)
jmp 0x484e1
movsd 0x190(%rsp), %xmm0
mulsd 0x190(%rsp), %xmm0
movsd %xmm0, 0x178(%rsp)
movsd 0x178(%rsp), %xmm0
ucomisd 0x1a0(%rsp), %xmm0
jbe 0x48515
jmp 0x48c1b
movq 0x1b0(%rsp), %rdi
callq 0x1efc0
movsd %xmm0, 0x78(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x48cd0
movaps %xmm0, %xmm1
movsd 0x78(%rsp), %xmm0
leaq 0x220203(%rip), %rax    # 0x268748
movsd (%rax), %xmm2
callq 0x48c30
cmpl $0x0, %eax
je 0x4858e
movq 0x1b0(%rsp), %rdi
callq 0x1efc0
movsd %xmm0, 0x70(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x48cd0
movaps %xmm0, %xmm1
movsd 0x70(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x188(%rsp)
jmp 0x48612
movq 0x1b0(%rsp), %rdi
callq 0x1efc0
movsd %xmm0, 0x68(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x48cf0
movaps %xmm0, %xmm1
movsd 0x68(%rsp), %xmm0
leaq 0x22018a(%rip), %rax    # 0x268748
movsd (%rax), %xmm2
callq 0x48c80
cmpl $0x0, %eax
je 0x48604
movq 0x1c0(%rsp), %rdi
callq 0x48cf0
movsd %xmm0, 0x60(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x1efc0
movaps %xmm0, %xmm1
movsd 0x60(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x188(%rsp)
jmp 0x48610
xorps %xmm0, %xmm0
movsd %xmm0, 0x188(%rsp)
jmp 0x48612
movsd 0x188(%rsp), %xmm0
mulsd 0x188(%rsp), %xmm0
movsd %xmm0, 0x170(%rsp)
movsd 0x170(%rsp), %xmm0
ucomisd 0x1a0(%rsp), %xmm0
jbe 0x48646
jmp 0x48c1b
movq 0x1b0(%rsp), %rdi
callq 0x1efd0
movsd %xmm0, 0x58(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x48d10
movaps %xmm0, %xmm1
movsd 0x58(%rsp), %xmm0
leaq 0x2200d2(%rip), %rax    # 0x268748
movsd (%rax), %xmm2
callq 0x48c30
cmpl $0x0, %eax
je 0x486bf
movq 0x1b0(%rsp), %rdi
callq 0x1efd0
movsd %xmm0, 0x50(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x48d10
movaps %xmm0, %xmm1
movsd 0x50(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x180(%rsp)
jmp 0x48743
movq 0x1b0(%rsp), %rdi
callq 0x1efd0
movsd %xmm0, 0x48(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x48d30
movaps %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
leaq 0x220059(%rip), %rax    # 0x268748
movsd (%rax), %xmm2
callq 0x48c80
cmpl $0x0, %eax
je 0x48735
movq 0x1c0(%rsp), %rdi
callq 0x48d30
movsd %xmm0, 0x40(%rsp)
movq 0x1b0(%rsp), %rdi
callq 0x1efd0
movaps %xmm0, %xmm1
movsd 0x40(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x180(%rsp)
jmp 0x48741
xorps %xmm0, %xmm0
movsd %xmm0, 0x180(%rsp)
jmp 0x48743
movsd 0x180(%rsp), %xmm0
mulsd 0x180(%rsp), %xmm0
movsd %xmm0, 0x168(%rsp)
movsd 0x168(%rsp), %xmm0
ucomisd 0x1a0(%rsp), %xmm0
jbe 0x48777
jmp 0x48c1b
xorps %xmm0, %xmm0
movsd %xmm0, 0x160(%rsp)
movsd 0x188(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x487c2
jp 0x487c2
movsd 0x180(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x487c2
jp 0x487c2
movsd 0x178(%rsp), %xmm0
movsd %xmm0, 0x160(%rsp)
jmp 0x48862
movsd 0x190(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x487fe
jp 0x487fe
movsd 0x180(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x487fe
jp 0x487fe
movsd 0x170(%rsp), %xmm0
movsd %xmm0, 0x160(%rsp)
jmp 0x48860
movsd 0x190(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4883a
jp 0x4883a
movsd 0x188(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x4883a
jp 0x4883a
movsd 0x168(%rsp), %xmm0
movsd %xmm0, 0x160(%rsp)
jmp 0x4885e
movsd 0x178(%rsp), %xmm0
addsd 0x170(%rsp), %xmm0
addsd 0x168(%rsp), %xmm0
movsd %xmm0, 0x160(%rsp)
jmp 0x48860
jmp 0x48862
movsd 0x160(%rsp), %xmm0
ucomisd 0x1a0(%rsp), %xmm0
jbe 0x4887b
jmp 0x48c1b
movq 0x1b0(%rsp), %rdi
movq 0x1c8(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x201e0
movq 0x1c8(%rsp), %rax
subsd 0x18(%rax), %xmm0
movsd %xmm0, 0x158(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x158(%rsp), %xmm0
jae 0x488dc
movsd 0x158(%rsp), %xmm1
mulsd 0x158(%rsp), %xmm1
movsd 0x1a0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x489ce
movq 0x1c0(%rsp), %rsi
leaq 0x120(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0xfc930
movq 0x38(%rsp), %rdi
movq 0x1c8(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
movl $0x1, %esi
callq 0x1ec30
movq %rax, %rdi
movq 0x1c8(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x1ec70
movsd 0x30(%rsp), %xmm0
movq 0xa0(%rsp), %rdi
movq 0x38(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x1c8(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x1b8(%rsp), %rcx
movq 0x1b0(%rsp), %r8
movsd 0x1a8(%rsp), %xmm0
movsd 0x1a0(%rsp), %xmm1
movq 0x198(%rsp), %r9
movq 0x1e0(%rsp), %r10
movq 0x1e8(%rsp), %r11
movq %rsp, %rax
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x48350
jmp 0x48999
leaq 0x120(%rsp), %rdi
callq 0x1ec20
jmp 0x489ce
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
leaq 0x120(%rsp), %rdi
callq 0x1ec20
jmp 0x48c23
movsd 0x158(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jae 0x48a05
movsd 0x158(%rsp), %xmm1
mulsd 0x158(%rsp), %xmm1
movsd 0x1a0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x48af4
movq 0x1c0(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0xfc930
movq 0x28(%rsp), %rdi
movq 0x1c8(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
xorl %esi, %esi
callq 0x1ec30
movq %rax, %rdi
movq 0x1c8(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x1ec70
movsd 0x20(%rsp), %xmm0
movq 0xa0(%rsp), %rdi
movq 0x28(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x1c8(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x1b8(%rsp), %rcx
movq 0x1b0(%rsp), %r8
movsd 0x1a8(%rsp), %xmm0
movsd 0x1a0(%rsp), %xmm1
movq 0x198(%rsp), %r9
movq 0x1e0(%rsp), %r10
movq 0x1e8(%rsp), %r11
movq %rsp, %rax
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x48350
jmp 0x48abf
leaq 0xd8(%rsp), %rdi
callq 0x1ec20
jmp 0x48af4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x1ec20
jmp 0x48c23
jmp 0x48c1b
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movq 0x1c8(%rsp), %rcx
cmpl 0x128(%rcx), %eax
jge 0x48c19
movq 0xa0(%rsp), %rsi
movq 0x1c8(%rsp), %rax
movslq 0xd4(%rsp), %rcx
movq 0x28(%rax,%rcx,8), %rax
movq %rax, 0xc8(%rsp)
movq 0x1b0(%rsp), %rax
movq %rax, 0x18(%rsp)
movq 0xc8(%rsp), %rdx
leaq 0xa8(%rsp), %rdi
callq 0x1f850
movq 0x18(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0xc0520
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm0
ucomisd 0x1a8(%rsp), %xmm0
jb 0x48c01
movsd 0xc0(%rsp), %xmm1
movsd 0x1a0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x48c01
cmpq $0x0, 0x198(%rsp)
je 0x48bea
cmpq $0x0, 0x1b8(%rsp)
je 0x48bea
movq 0x198(%rsp), %rax
movq 0x1b8(%rsp), %rdi
movq 0xc8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
callq *%rax
cmpl $0x0, %eax
je 0x48bff
movq 0x1e8(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x1f3d0
jmp 0x48c01
jmp 0x48c03
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x48b04
jmp 0x48c1b
addq $0x1d8, %rsp            # imm = 0x1D8
retq
movq 0x118(%rsp), %rdi
callq 0x16ca0