// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshSearchTree.cpp
// Function: gaps::R3MeshSearchTree::FindClosest(gaps::R3Point const&, gaps::R3Vector const&, gaps::R3MeshIntersection&, double, double&, int (*)(gaps::R3Point const&, gaps::R3Vector const&, gaps::R3Mesh*, gaps::R3MeshFace*, void*), void*, gaps::R3MeshSearchTreeNode*, gaps::R3Box const&) const
void R3MeshSearchTree::
FindClosest(const R3Point& query_position, const R3Vector& query_normal, R3MeshIntersection& closest, 
  RNScalar min_distance_squared, RNScalar& max_distance_squared, 
  int (*IsCompatible)(const R3Point&, const R3Vector&, R3Mesh *, R3MeshFace *, void *), void *compatible_data,
  R3MeshSearchTreeNode *node, const R3Box& node_box) const
{
  // Compute distance (squared) from query point to node bbox
  RNScalar distance_squared = DistanceSquared(query_position, node_box, max_distance_squared);
  if (distance_squared >= max_distance_squared) return;

  // Update based on distance to each big face
  for (int i = 0; i < node->big_faces.NEntries(); i++) {
    // Get face container and check mark
    R3MeshSearchTreeFace *face_container = node->big_faces[i];
    if (face_container->mark == mark) continue;
    face_container->mark = mark;
  
    // Find closest point in mesh face
    FindClosest(query_position, query_normal, closest, 
      min_distance_squared, max_distance_squared, 
      IsCompatible, compatible_data, face_container->face);
  }

  // Check if node is interior
  if (node->children[0]) {
    assert(node->children[1]);
    assert(node->small_faces.IsEmpty());

    // Compute distance from query point to split plane
    RNScalar 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(query_position, query_normal, closest, 
        min_distance_squared, max_distance_squared, IsCompatible, compatible_data,
        node->children[0], child_box);
      if (side*side < max_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_LO][node->split_dimension] = node->split_coordinate;
        FindClosest(query_position, query_normal, closest, 
          min_distance_squared, max_distance_squared, IsCompatible, compatible_data,
          node->children[1], child_box);
      }
    }
    else {
      // Search positive side first
      R3Box child_box(node_box);
      child_box[RN_LO][node->split_dimension] = node->split_coordinate;
      FindClosest(query_position, query_normal, closest, 
        min_distance_squared, max_distance_squared, IsCompatible, compatible_data,
        node->children[1], child_box);
      if (side*side < max_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_HI][node->split_dimension] = node->split_coordinate;
        FindClosest(query_position, query_normal, closest, 
          min_distance_squared, max_distance_squared, IsCompatible, compatible_data,
          node->children[0], child_box);
      }
    }
  }
  else {
    // Update based on distance to each small face
    for (int i = 0; i < node->small_faces.NEntries(); i++) {
      // Get face container and check mark
      R3MeshSearchTreeFace *face_container = node->small_faces[i];
      if (face_container->mark == mark) continue;
      face_container->mark = mark;

      // Find closest point in mesh face
      FindClosest(query_position, query_normal, closest, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data, face_container->face);
    }
  }
}

// Assembly:
pushq %rbx
subq $0x200, %rsp            # imm = 0x200
movq 0x220(%rsp), %rax
movq 0x218(%rsp), %rax
movq 0x210(%rsp), %rax
movq %rdi, 0x1f8(%rsp)
movq %rsi, 0x1f0(%rsp)
movq %rdx, 0x1e8(%rsp)
movq %rcx, 0x1e0(%rsp)
movsd %xmm0, 0x1d8(%rsp)
movq %r8, 0x1d0(%rsp)
movq %r9, 0x1c8(%rsp)
movq 0x1f8(%rsp), %rdi
movq %rdi, 0xa0(%rsp)
movq 0x1f0(%rsp), %rsi
movq 0x220(%rsp), %rdx
movq 0x1d0(%rsp), %rax
movsd (%rax), %xmm0
callq 0x1d390
movsd %xmm0, 0x1c0(%rsp)
movsd 0x1c0(%rsp), %xmm0
movq 0x1d0(%rsp), %rax
ucomisd (%rax), %xmm0
jb 0x1d8af
jmp 0x1df80
movl $0x0, 0x1bc(%rsp)
movl 0x1bc(%rsp), %eax
movl %eax, 0x9c(%rsp)
movq 0x218(%rsp), %rdi
addq $0x28, %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x9c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1d9a5
movq 0x218(%rsp), %rdi
addq $0x28, %rdi
movl 0x1bc(%rsp), %esi
callq 0x21860
movq 0xa0(%rsp), %rcx
movq (%rax), %rax
movq %rax, 0x1b0(%rsp)
movq 0x1b0(%rsp), %rax
movl 0x14(%rax), %eax
cmpl 0x14(%rcx), %eax
jne 0x1d927
jmp 0x1d98f
movq 0xa0(%rsp), %rdi
movl 0x14(%rdi), %ecx
movq 0x1b0(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x1f0(%rsp), %rsi
movq 0x1e8(%rsp), %rdx
movq 0x1e0(%rsp), %rcx
movsd 0x1d8(%rsp), %xmm0
movq 0x1d0(%rsp), %r8
movq 0x1c8(%rsp), %r9
movq 0x210(%rsp), %r10
movq 0x1b0(%rsp), %rax
movq (%rax), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1c250
movl 0x1bc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1bc(%rsp)
jmp 0x1d8ba
movq 0x218(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1de8e
movq 0x1f0(%rsp), %rdi
movq 0x218(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17e60
movq 0x218(%rsp), %rax
subsd 0x18(%rax), %xmm0
movsd %xmm0, 0x1a8(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x1a8(%rsp), %xmm0
jb 0x1dc4b
movq 0x220(%rsp), %rsi
leaq 0x170(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x67100
movq 0x80(%rsp), %rdi
movq 0x218(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movl $0x1, %esi
callq 0x17820
movq %rax, 0x90(%rsp)
jmp 0x1da47
movq 0x90(%rsp), %rdi
movq 0x218(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x78(%rsp)
jmp 0x1da66
movq 0xa0(%rsp), %rdi
movq 0x78(%rsp), %rax
movsd 0x88(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1f0(%rsp), %rsi
movq 0x1e8(%rsp), %rdx
movq 0x1e0(%rsp), %rcx
movsd 0x1d8(%rsp), %xmm0
movq 0x1d0(%rsp), %r8
movq 0x1c8(%rsp), %r9
movq 0x210(%rsp), %r10
movq 0x218(%rsp), %rax
movq 0x8(%rax), %r11
movq %rsp, %rax
leaq 0x170(%rsp), %rbx
movq %rbx, 0x10(%rax)
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1d800
jmp 0x1dae2
movsd 0x1a8(%rsp), %xmm1
mulsd 0x1a8(%rsp), %xmm1
movq 0x1d0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1dc27
movq 0x220(%rsp), %rsi
leaq 0x128(%rsp), %rdi
callq 0x67100
jmp 0x1db21
movq 0x218(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
leaq 0x128(%rsp), %rdi
xorl %esi, %esi
callq 0x17820
movq %rax, 0x70(%rsp)
jmp 0x1db4a
movq 0x70(%rsp), %rdi
movq 0x218(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x60(%rsp)
jmp 0x1db66
movq 0xa0(%rsp), %rdi
movq 0x60(%rsp), %rax
movsd 0x68(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1f0(%rsp), %rsi
movq 0x1e8(%rsp), %rdx
movq 0x1e0(%rsp), %rcx
movsd 0x1d8(%rsp), %xmm0
movq 0x1d0(%rsp), %r8
movq 0x1c8(%rsp), %r9
movq 0x210(%rsp), %r10
movq 0x218(%rsp), %rax
movq 0x10(%rax), %r11
movq %rsp, %rax
leaq 0x128(%rsp), %rbx
movq %rbx, 0x10(%rax)
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1d800
jmp 0x1dbdf
leaq 0x128(%rsp), %rdi
callq 0x17670
jmp 0x1dc27
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x168(%rsp)
movl %eax, 0x164(%rsp)
jmp 0x1dc39
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x168(%rsp)
movl %eax, 0x164(%rsp)
leaq 0x128(%rsp), %rdi
callq 0x17670
jmp 0x1dc39
leaq 0x170(%rsp), %rdi
callq 0x17670
jmp 0x1de89
leaq 0x170(%rsp), %rdi
callq 0x17670
jmp 0x1df89
movq 0x220(%rsp), %rsi
leaq 0xf0(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0x67100
movq 0x48(%rsp), %rdi
movq 0x218(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x50(%rsp)
xorl %esi, %esi
callq 0x17820
movq %rax, 0x58(%rsp)
jmp 0x1dc8b
movq 0x58(%rsp), %rdi
movq 0x218(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x40(%rsp)
jmp 0x1dca7
movq 0xa0(%rsp), %rdi
movq 0x40(%rsp), %rax
movsd 0x50(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1f0(%rsp), %rsi
movq 0x1e8(%rsp), %rdx
movq 0x1e0(%rsp), %rcx
movsd 0x1d8(%rsp), %xmm0
movq 0x1d0(%rsp), %r8
movq 0x1c8(%rsp), %r9
movq 0x210(%rsp), %r10
movq 0x218(%rsp), %rax
movq 0x10(%rax), %r11
movq %rsp, %rax
leaq 0xf0(%rsp), %rbx
movq %rbx, 0x10(%rax)
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1d800
jmp 0x1dd20
movsd 0x1a8(%rsp), %xmm1
mulsd 0x1a8(%rsp), %xmm1
movq 0x1d0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1de68
movq 0x220(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
callq 0x67100
jmp 0x1dd5f
movq 0x218(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x30(%rsp)
leaq 0xb8(%rsp), %rdi
movl $0x1, %esi
callq 0x17820
movq %rax, 0x38(%rsp)
jmp 0x1dd8b
movq 0x38(%rsp), %rdi
movq 0x218(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x28(%rsp)
jmp 0x1dda7
movq 0xa0(%rsp), %rdi
movq 0x28(%rsp), %rax
movsd 0x30(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1f0(%rsp), %rsi
movq 0x1e8(%rsp), %rdx
movq 0x1e0(%rsp), %rcx
movsd 0x1d8(%rsp), %xmm0
movq 0x1d0(%rsp), %r8
movq 0x1c8(%rsp), %r9
movq 0x210(%rsp), %r10
movq 0x218(%rsp), %rax
movq 0x8(%rax), %r11
movq %rsp, %rax
leaq 0xb8(%rsp), %rbx
movq %rbx, 0x10(%rax)
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1d800
jmp 0x1de20
leaq 0xb8(%rsp), %rdi
callq 0x17670
jmp 0x1de68
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x168(%rsp)
movl %eax, 0x164(%rsp)
jmp 0x1de77
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x168(%rsp)
movl %eax, 0x164(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x17670
jmp 0x1de77
leaq 0xf0(%rsp), %rdi
callq 0x17670
jmp 0x1de89
leaq 0xf0(%rsp), %rdi
callq 0x17670
jmp 0x1df89
jmp 0x1df80
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x218(%rsp), %rdi
addq $0x38, %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x1df7e
movq 0x218(%rsp), %rdi
addq $0x38, %rdi
movl 0xb4(%rsp), %esi
callq 0x21860
movq 0xa0(%rsp), %rcx
movq (%rax), %rax
movq %rax, 0xa8(%rsp)
movq 0xa8(%rsp), %rax
movl 0x14(%rax), %eax
cmpl 0x14(%rcx), %eax
jne 0x1df00
jmp 0x1df68
movq 0xa0(%rsp), %rdi
movl 0x14(%rdi), %ecx
movq 0xa8(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x1f0(%rsp), %rsi
movq 0x1e8(%rsp), %rdx
movq 0x1e0(%rsp), %rcx
movsd 0x1d8(%rsp), %xmm0
movq 0x1d0(%rsp), %r8
movq 0x1c8(%rsp), %r9
movq 0x210(%rsp), %r10
movq 0xa8(%rsp), %rax
movq (%rax), %rax
movq %r10, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x1c250
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x1de99
jmp 0x1df80
addq $0x200, %rsp            # imm = 0x200
popq %rbx
retq
movq 0x168(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)