// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshSearchTree.cpp
// Function: gaps::R3MeshSearchTree::FindAll(gaps::R3Point const&, gaps::R3Vector const&, gaps::RNArray<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::
FindAll(const R3Point& query_position, const R3Vector& query_normal, RNArray<R3MeshIntersection *>& hits, 
  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;

  // Check 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 point in mesh face
    FindAll(query_position, query_normal, hits, 
      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;
      FindAll(query_position, query_normal, hits, 
        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;
        FindAll(query_position, query_normal, hits, 
          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;
      FindAll(query_position, query_normal, hits, 
        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;
        FindAll(query_position, query_normal, hits, 
          min_distance_squared, max_distance_squared, IsCompatible, compatible_data,
          node->children[0], child_box);
      }
    }
  }
  else {
    // Check 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 point in mesh face
      FindAll(query_position, query_normal, hits, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data, face_container->face);
    }
  }
}

// Assembly:
subq $0x1f8, %rsp            # imm = 0x1F8
movq 0x208(%rsp), %rax
movq 0x200(%rsp), %rax
movq %rdi, 0x1f0(%rsp)
movq %rsi, 0x1e8(%rsp)
movq %rdx, 0x1e0(%rsp)
movq %rcx, 0x1d8(%rsp)
movsd %xmm0, 0x1d0(%rsp)
movsd %xmm1, 0x1c8(%rsp)
movq %r8, 0x1c0(%rsp)
movq %r9, 0x1b8(%rsp)
movq 0x1f0(%rsp), %rdi
movq %rdi, 0x90(%rsp)
movq 0x1e8(%rsp), %rsi
movq 0x208(%rsp), %rdx
movsd 0x1c8(%rsp), %xmm0
callq 0x1d390
movsd %xmm0, 0x1b0(%rsp)
movsd 0x1b0(%rsp), %xmm0
ucomisd 0x1c8(%rsp), %xmm0
jb 0x1f849
jmp 0x1fef4
movl $0x0, 0x1ac(%rsp)
movl 0x1ac(%rsp), %eax
movl %eax, 0x8c(%rsp)
movq 0x200(%rsp), %rdi
addq $0x28, %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x8c(%rsp), %eax
cmpl %ecx, %eax
jge 0x1f93b
movq 0x200(%rsp), %rdi
addq $0x28, %rdi
movl 0x1ac(%rsp), %esi
callq 0x21860
movq 0x90(%rsp), %rcx
movq (%rax), %rax
movq %rax, 0x1a0(%rsp)
movq 0x1a0(%rsp), %rax
movl 0x14(%rax), %eax
cmpl 0x14(%rcx), %eax
jne 0x1f8c1
jmp 0x1f925
movq 0x90(%rsp), %rdi
movl 0x14(%rdi), %ecx
movq 0x1a0(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x1e8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movq 0x1d8(%rsp), %rcx
movsd 0x1d0(%rsp), %xmm0
movsd 0x1c8(%rsp), %xmm1
movq 0x1c0(%rsp), %r8
movq 0x1b8(%rsp), %r9
movq 0x1a0(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
callq 0x1e570
movl 0x1ac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1ac(%rsp)
jmp 0x1f854
movq 0x200(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x1fe06
movq 0x1e8(%rsp), %rdi
movq 0x200(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17e60
movq 0x200(%rsp), %rax
subsd 0x18(%rax), %xmm0
movsd %xmm0, 0x198(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x198(%rsp), %xmm0
jb 0x1fbcc
movq 0x208(%rsp), %rsi
leaq 0x160(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x67100
movq 0x70(%rsp), %rdi
movq 0x200(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x78(%rsp)
movl $0x1, %esi
callq 0x17820
movq %rax, 0x80(%rsp)
jmp 0x1f9d4
movq 0x80(%rsp), %rdi
movq 0x200(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x68(%rsp)
jmp 0x1f9f3
movq 0x90(%rsp), %rdi
movq 0x68(%rsp), %rax
movsd 0x78(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1e8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movq 0x1d8(%rsp), %rcx
movsd 0x1d0(%rsp), %xmm0
movsd 0x1c8(%rsp), %xmm1
movq 0x1c0(%rsp), %r8
movq 0x1b8(%rsp), %r9
movq 0x200(%rsp), %rax
movq 0x8(%rax), %r10
movq %rsp, %rax
leaq 0x160(%rsp), %r11
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1f7a0
jmp 0x1fa69
movsd 0x198(%rsp), %xmm1
mulsd 0x198(%rsp), %xmm1
movsd 0x1c8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1fba8
movq 0x208(%rsp), %rsi
leaq 0x118(%rsp), %rdi
callq 0x67100
jmp 0x1faa5
movq 0x200(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x58(%rsp)
leaq 0x118(%rsp), %rdi
xorl %esi, %esi
callq 0x17820
movq %rax, 0x60(%rsp)
jmp 0x1face
movq 0x60(%rsp), %rdi
movq 0x200(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x50(%rsp)
jmp 0x1faea
movq 0x90(%rsp), %rdi
movq 0x50(%rsp), %rax
movsd 0x58(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1e8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movq 0x1d8(%rsp), %rcx
movsd 0x1d0(%rsp), %xmm0
movsd 0x1c8(%rsp), %xmm1
movq 0x1c0(%rsp), %r8
movq 0x1b8(%rsp), %r9
movq 0x200(%rsp), %rax
movq 0x10(%rax), %r10
movq %rsp, %rax
leaq 0x118(%rsp), %r11
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1f7a0
jmp 0x1fb60
leaq 0x118(%rsp), %rdi
callq 0x17670
jmp 0x1fba8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x1fbba
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0x118(%rsp), %rdi
callq 0x17670
jmp 0x1fbba
leaq 0x160(%rsp), %rdi
callq 0x17670
jmp 0x1fe01
leaq 0x160(%rsp), %rdi
callq 0x17670
jmp 0x1fefc
movq 0x208(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x67100
movq 0x38(%rsp), %rdi
movq 0x200(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
xorl %esi, %esi
callq 0x17820
movq %rax, 0x48(%rsp)
jmp 0x1fc0c
movq 0x48(%rsp), %rdi
movq 0x200(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x30(%rsp)
jmp 0x1fc28
movq 0x90(%rsp), %rdi
movq 0x30(%rsp), %rax
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1e8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movq 0x1d8(%rsp), %rcx
movsd 0x1d0(%rsp), %xmm0
movsd 0x1c8(%rsp), %xmm1
movq 0x1c0(%rsp), %r8
movq 0x1b8(%rsp), %r9
movq 0x200(%rsp), %rax
movq 0x10(%rax), %r10
movq %rsp, %rax
leaq 0xe0(%rsp), %r11
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1f7a0
jmp 0x1fc9e
movsd 0x198(%rsp), %xmm1
mulsd 0x198(%rsp), %xmm1
movsd 0x1c8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x1fde0
movq 0x208(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x67100
jmp 0x1fcda
movq 0x200(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
leaq 0xa8(%rsp), %rdi
movl $0x1, %esi
callq 0x17820
movq %rax, 0x28(%rsp)
jmp 0x1fd06
movq 0x28(%rsp), %rdi
movq 0x200(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x18(%rsp)
jmp 0x1fd22
movq 0x90(%rsp), %rdi
movq 0x18(%rsp), %rax
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x1e8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movq 0x1d8(%rsp), %rcx
movsd 0x1d0(%rsp), %xmm0
movsd 0x1c8(%rsp), %xmm1
movq 0x1c0(%rsp), %r8
movq 0x1b8(%rsp), %r9
movq 0x200(%rsp), %rax
movq 0x8(%rax), %r10
movq %rsp, %rax
leaq 0xa8(%rsp), %r11
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x1f7a0
jmp 0x1fd98
leaq 0xa8(%rsp), %rdi
callq 0x17670
jmp 0x1fde0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
jmp 0x1fdef
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x158(%rsp)
movl %eax, 0x154(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x17670
jmp 0x1fdef
leaq 0xe0(%rsp), %rdi
callq 0x17670
jmp 0x1fe01
leaq 0xe0(%rsp), %rdi
callq 0x17670
jmp 0x1fefc
jmp 0x1fef4
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x200(%rsp), %rdi
addq $0x38, %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x1fef2
movq 0x200(%rsp), %rdi
addq $0x38, %rdi
movl 0xa4(%rsp), %esi
callq 0x21860
movq 0x90(%rsp), %rcx
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
movl 0x14(%rax), %eax
cmpl 0x14(%rcx), %eax
jne 0x1fe78
jmp 0x1fedc
movq 0x90(%rsp), %rdi
movl 0x14(%rdi), %ecx
movq 0x98(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x1e8(%rsp), %rsi
movq 0x1e0(%rsp), %rdx
movq 0x1d8(%rsp), %rcx
movsd 0x1d0(%rsp), %xmm0
movsd 0x1c8(%rsp), %xmm1
movq 0x1c0(%rsp), %r8
movq 0x1b8(%rsp), %r9
movq 0x98(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
callq 0x1e570
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x1fe11
jmp 0x1fef4
addq $0x1f8, %rsp            # imm = 0x1F8
retq
movq 0x158(%rsp), %rdi
callq 0xf890
nopl (%rax)