// File: tomfunkhouser[P]gaps/apps/msh2df/../../pkgs/R3Shapes/R3Kdtree.cpp
// Function: gaps::R3Kdtree<Point*>::FindClosest(gaps::R3KdtreeNode<Point*>*, gaps::R3Box const&, Point*, gaps::R3Point const&, double, double, int (*)(Point*, Point*, void*), void*, Point*&, double&) const
void R3Kdtree<PtrType>::
FindClosest(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, 
  PtrType& closest_point, RNScalar& closest_distance_squared) 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 >= closest_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 >= closest_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 >= closest_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 >= closest_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) {
      // Search negative side first
      R3Box child_box(node_box);
      child_box[RN_HI][node->split_dimension] = node->split_coordinate;
      FindClosest(node->children[0], child_box, 
        query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data,
        closest_point, closest_distance_squared);
      if (side*side < closest_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_LO][node->split_dimension] = node->split_coordinate;
        FindClosest(node->children[1], child_box, 
          query_point, query_position, 
          min_distance_squared, max_distance_squared, 
          IsCompatible, compatible_data,
          closest_point, closest_distance_squared);
      }
    }
    else {
      // Search positive side first
      R3Box child_box(node_box);
      child_box[RN_LO][node->split_dimension] = node->split_coordinate;
      FindClosest(node->children[1], child_box, 
        query_point, query_position, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data,
        closest_point, closest_distance_squared);
      if (side*side < closest_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_HI][node->split_dimension] = node->split_coordinate;
        FindClosest(node->children[0], child_box, 
          query_point, query_position, 
          min_distance_squared, max_distance_squared, 
          IsCompatible, compatible_data,
          closest_point, closest_distance_squared);
      }
    }
  }
  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 <= closest_distance_squared)) {
        if (!IsCompatible || !query_point || IsCompatible(query_point, point, compatible_data)) {
          closest_distance_squared = distance_squared;
          closest_point = point;
        }
      }
    }
  }
}

// Assembly:
pushq %rbx
subq $0x270, %rsp            # imm = 0x270
movq 0x290(%rsp), %rax
movq 0x288(%rsp), %rax
movq 0x280(%rsp), %rax
movq %rdi, 0x268(%rsp)
movq %rsi, 0x260(%rsp)
movq %rdx, 0x258(%rsp)
movq %rcx, 0x250(%rsp)
movq %r8, 0x248(%rsp)
movsd %xmm0, 0x240(%rsp)
movsd %xmm1, 0x238(%rsp)
movq %r9, 0x230(%rsp)
movq 0x268(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x260(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1af00
movq 0x248(%rsp), %rdi
callq 0x178f0
movsd %xmm0, 0xc0(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1a010
movaps %xmm0, %xmm1
movsd 0xc0(%rsp), %xmm0
leaq 0x16bf25(%rip), %rax    # 0x1864f0
movsd (%rax), %xmm2
callq 0x19fe0
cmpl $0x0, %eax
je 0x1a61a
movq 0x248(%rsp), %rdi
callq 0x178f0
movsd %xmm0, 0xb8(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1a010
movaps %xmm0, %xmm1
movsd 0xb8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x228(%rsp)
jmp 0x1a6aa
movq 0x248(%rsp), %rdi
callq 0x178f0
movsd %xmm0, 0xb0(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1a060
movaps %xmm0, %xmm1
movsd 0xb0(%rsp), %xmm0
leaq 0x16bea0(%rip), %rax    # 0x1864f0
movsd (%rax), %xmm2
callq 0x1a030
cmpl $0x0, %eax
je 0x1a69c
movq 0x258(%rsp), %rdi
callq 0x1a060
movsd %xmm0, 0xa8(%rsp)
movq 0x248(%rsp), %rdi
callq 0x178f0
movaps %xmm0, %xmm1
movsd 0xa8(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x228(%rsp)
jmp 0x1a6a8
xorps %xmm0, %xmm0
movsd %xmm0, 0x228(%rsp)
jmp 0x1a6aa
movsd 0x228(%rsp), %xmm0
mulsd 0x228(%rsp), %xmm0
movsd %xmm0, 0x210(%rsp)
movsd 0x210(%rsp), %xmm0
movq 0x290(%rsp), %rax
ucomisd (%rax), %xmm0
jb 0x1a6e1
jmp 0x1b03c
movq 0x248(%rsp), %rdi
callq 0x17900
movsd %xmm0, 0xa0(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1a080
movaps %xmm0, %xmm1
movsd 0xa0(%rsp), %xmm0
leaq 0x16bdd9(%rip), %rax    # 0x1864f0
movsd (%rax), %xmm2
callq 0x19fe0
cmpl $0x0, %eax
je 0x1a766
movq 0x248(%rsp), %rdi
callq 0x17900
movsd %xmm0, 0x98(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1a080
movaps %xmm0, %xmm1
movsd 0x98(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x220(%rsp)
jmp 0x1a7f6
movq 0x248(%rsp), %rdi
callq 0x17900
movsd %xmm0, 0x90(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1a0a0
movaps %xmm0, %xmm1
movsd 0x90(%rsp), %xmm0
leaq 0x16bd54(%rip), %rax    # 0x1864f0
movsd (%rax), %xmm2
callq 0x1a030
cmpl $0x0, %eax
je 0x1a7e8
movq 0x258(%rsp), %rdi
callq 0x1a0a0
movsd %xmm0, 0x88(%rsp)
movq 0x248(%rsp), %rdi
callq 0x17900
movaps %xmm0, %xmm1
movsd 0x88(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x220(%rsp)
jmp 0x1a7f4
xorps %xmm0, %xmm0
movsd %xmm0, 0x220(%rsp)
jmp 0x1a7f6
movsd 0x220(%rsp), %xmm0
mulsd 0x220(%rsp), %xmm0
movsd %xmm0, 0x208(%rsp)
movsd 0x208(%rsp), %xmm0
movq 0x290(%rsp), %rax
ucomisd (%rax), %xmm0
jb 0x1a82d
jmp 0x1b03c
movq 0x248(%rsp), %rdi
callq 0x17910
movsd %xmm0, 0x80(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1a0c0
movaps %xmm0, %xmm1
movsd 0x80(%rsp), %xmm0
leaq 0x16bc8d(%rip), %rax    # 0x1864f0
movsd (%rax), %xmm2
callq 0x19fe0
cmpl $0x0, %eax
je 0x1a8ac
movq 0x248(%rsp), %rdi
callq 0x17910
movsd %xmm0, 0x78(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1a0c0
movaps %xmm0, %xmm1
movsd 0x78(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x218(%rsp)
jmp 0x1a930
movq 0x248(%rsp), %rdi
callq 0x17910
movsd %xmm0, 0x70(%rsp)
movq 0x258(%rsp), %rdi
callq 0x1a0e0
movaps %xmm0, %xmm1
movsd 0x70(%rsp), %xmm0
leaq 0x16bc14(%rip), %rax    # 0x1864f0
movsd (%rax), %xmm2
callq 0x1a030
cmpl $0x0, %eax
je 0x1a922
movq 0x258(%rsp), %rdi
callq 0x1a0e0
movsd %xmm0, 0x68(%rsp)
movq 0x248(%rsp), %rdi
callq 0x17910
movaps %xmm0, %xmm1
movsd 0x68(%rsp), %xmm0
subsd %xmm1, %xmm0
movsd %xmm0, 0x218(%rsp)
jmp 0x1a92e
xorps %xmm0, %xmm0
movsd %xmm0, 0x218(%rsp)
jmp 0x1a930
movsd 0x218(%rsp), %xmm0
mulsd 0x218(%rsp), %xmm0
movsd %xmm0, 0x200(%rsp)
movsd 0x200(%rsp), %xmm0
movq 0x290(%rsp), %rax
ucomisd (%rax), %xmm0
jb 0x1a967
jmp 0x1b03c
xorps %xmm0, %xmm0
movsd %xmm0, 0x1f8(%rsp)
movsd 0x220(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1a9b2
jp 0x1a9b2
movsd 0x218(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1a9b2
jp 0x1a9b2
movsd 0x210(%rsp), %xmm0
movsd %xmm0, 0x1f8(%rsp)
jmp 0x1aa52
movsd 0x228(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1a9ee
jp 0x1a9ee
movsd 0x218(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1a9ee
jp 0x1a9ee
movsd 0x208(%rsp), %xmm0
movsd %xmm0, 0x1f8(%rsp)
jmp 0x1aa50
movsd 0x228(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1aa2a
jp 0x1aa2a
movsd 0x220(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x1aa2a
jp 0x1aa2a
movsd 0x200(%rsp), %xmm0
movsd %xmm0, 0x1f8(%rsp)
jmp 0x1aa4e
movsd 0x210(%rsp), %xmm0
addsd 0x208(%rsp), %xmm0
addsd 0x200(%rsp), %xmm0
movsd %xmm0, 0x1f8(%rsp)
jmp 0x1aa50
jmp 0x1aa52
movsd 0x1f8(%rsp), %xmm0
movq 0x290(%rsp), %rax
ucomisd (%rax), %xmm0
jb 0x1aa6e
jmp 0x1b03c
movq 0x248(%rsp), %rdi
movq 0x260(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17e60
movq 0x260(%rsp), %rax
subsd 0x18(%rax), %xmm0
movsd %xmm0, 0x1f0(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x1f0(%rsp), %xmm0
jb 0x1acd6
movq 0x258(%rsp), %rsi
leaq 0x1b8(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x67100
movq 0x60(%rsp), %rdi
movq 0x260(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
movl $0x1, %esi
callq 0x17820
movq %rax, %rdi
movq 0x260(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movsd 0x58(%rsp), %xmm0
movq 0xc8(%rsp), %rdi
movq 0x60(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x260(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x250(%rsp), %rcx
movq 0x248(%rsp), %r8
movsd 0x240(%rsp), %xmm0
movsd 0x238(%rsp), %xmm1
movq 0x230(%rsp), %r9
movq 0x280(%rsp), %r10
movq 0x288(%rsp), %r11
movq 0x290(%rsp), %rbx
movq %rsp, %rax
movq %rbx, 0x10(%rax)
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1a510
jmp 0x1ab77
movsd 0x1f0(%rsp), %xmm1
mulsd 0x1f0(%rsp), %xmm1
movq 0x290(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1acb2
movq 0x258(%rsp), %rsi
leaq 0x170(%rsp), %rdi
callq 0x67100
jmp 0x1abb6
movq 0x260(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
leaq 0x170(%rsp), %rdi
movq %rdi, 0x50(%rsp)
xorl %esi, %esi
callq 0x17820
movq %rax, %rdi
movq 0x260(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movsd 0x48(%rsp), %xmm0
movq 0xc8(%rsp), %rdi
movq 0x50(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x260(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x250(%rsp), %rcx
movq 0x248(%rsp), %r8
movsd 0x240(%rsp), %xmm0
movsd 0x238(%rsp), %xmm1
movq 0x230(%rsp), %r9
movq 0x280(%rsp), %r10
movq 0x288(%rsp), %r11
movq 0x290(%rsp), %rbx
movq %rsp, %rax
movq %rbx, 0x10(%rax)
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1a510
jmp 0x1ac6a
leaq 0x170(%rsp), %rdi
callq 0x17670
jmp 0x1acb2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0x1acc4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x170(%rsp), %rdi
callq 0x17670
jmp 0x1acc4
leaq 0x1b8(%rsp), %rdi
callq 0x17670
jmp 0x1aefb
leaq 0x1b8(%rsp), %rdi
callq 0x17670
jmp 0x1b045
movq 0x258(%rsp), %rsi
leaq 0x138(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x67100
movq 0x40(%rsp), %rdi
movq 0x260(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x38(%rsp)
xorl %esi, %esi
callq 0x17820
movq %rax, %rdi
movq 0x260(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movsd 0x38(%rsp), %xmm0
movq 0xc8(%rsp), %rdi
movq 0x40(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x260(%rsp), %rax
movq 0x10(%rax), %rsi
movq 0x250(%rsp), %rcx
movq 0x248(%rsp), %r8
movsd 0x240(%rsp), %xmm0
movsd 0x238(%rsp), %xmm1
movq 0x230(%rsp), %r9
movq 0x280(%rsp), %r10
movq 0x288(%rsp), %r11
movq 0x290(%rsp), %rbx
movq %rsp, %rax
movq %rbx, 0x10(%rax)
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1a510
jmp 0x1ad9c
movsd 0x1f0(%rsp), %xmm1
mulsd 0x1f0(%rsp), %xmm1
movq 0x290(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1aeda
movq 0x258(%rsp), %rsi
leaq 0x100(%rsp), %rdi
callq 0x67100
jmp 0x1addb
movq 0x260(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0x100(%rsp), %rdi
movq %rdi, 0x30(%rsp)
movl $0x1, %esi
callq 0x17820
movq %rax, %rdi
movq 0x260(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movsd 0x28(%rsp), %xmm0
movq 0xc8(%rsp), %rdi
movq 0x30(%rsp), %rdx
movsd %xmm0, (%rax)
movq 0x260(%rsp), %rax
movq 0x8(%rax), %rsi
movq 0x250(%rsp), %rcx
movq 0x248(%rsp), %r8
movsd 0x240(%rsp), %xmm0
movsd 0x238(%rsp), %xmm1
movq 0x230(%rsp), %r9
movq 0x280(%rsp), %r10
movq 0x288(%rsp), %r11
movq 0x290(%rsp), %rbx
movq %rsp, %rax
movq %rbx, 0x10(%rax)
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1a510
jmp 0x1ae92
leaq 0x100(%rsp), %rdi
callq 0x17670
jmp 0x1aeda
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
jmp 0x1aee9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x1b0(%rsp)
movl %eax, 0x1ac(%rsp)
leaq 0x100(%rsp), %rdi
callq 0x17670
jmp 0x1aee9
leaq 0x138(%rsp), %rdi
callq 0x17670
jmp 0x1aefb
leaq 0x138(%rsp), %rdi
callq 0x17670
jmp 0x1b045
jmp 0x1b03c
movl $0x0, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
movq 0x260(%rsp), %rcx
cmpl 0x128(%rcx), %eax
jge 0x1b03a
movq 0xc8(%rsp), %rsi
movq 0x260(%rsp), %rax
movslq 0xfc(%rsp), %rcx
movq 0x28(%rax,%rcx,8), %rax
movq %rax, 0xf0(%rsp)
movq 0x248(%rsp), %rax
movq %rax, 0x20(%rsp)
movq 0xf0(%rsp), %rdx
leaq 0xd0(%rsp), %rdi
callq 0x18bc0
movq 0x20(%rsp), %rdi
leaq 0xd0(%rsp), %rsi
callq 0x304d0
movsd %xmm0, 0xe8(%rsp)
movsd 0xe8(%rsp), %xmm0
ucomisd 0x240(%rsp), %xmm0
jb 0x1b022
movsd 0xe8(%rsp), %xmm1
movq 0x290(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jb 0x1b022
cmpq $0x0, 0x230(%rsp)
je 0x1aff8
cmpq $0x0, 0x250(%rsp)
je 0x1aff8
movq 0x230(%rsp), %rax
movq 0x250(%rsp), %rdi
movq 0xf0(%rsp), %rsi
movq 0x280(%rsp), %rdx
callq *%rax
cmpl $0x0, %eax
je 0x1b020
movsd 0xe8(%rsp), %xmm0
movq 0x290(%rsp), %rax
movsd %xmm0, (%rax)
movq 0xf0(%rsp), %rcx
movq 0x288(%rsp), %rax
movq %rcx, (%rax)
jmp 0x1b022
jmp 0x1b024
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x1af0b
jmp 0x1b03c
addq $0x270, %rsp            # imm = 0x270
popq %rbx
retq
movq 0x1b0(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)
nopl (%rax)