// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshSearchTree.cpp
// Function: gaps::R3MeshSearchTree::FindAll(gaps::R3Shape const&, gaps::RNArray<gaps::R3MeshIntersection*>&, gaps::R3MeshSearchTreeNode*, gaps::R3Box const&) const
void R3MeshSearchTree::
FindAll(const R3Shape& shape, RNArray<R3MeshIntersection *>& hits, 
  R3MeshSearchTreeNode *node, const R3Box& node_box) const
{
  // Check if shape intersect node box
  if (!R3Intersects(shape, node_box)) 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;
  
    // Check face bbox
    if (!R3Intersects(shape, mesh->FaceBBox(face_container->face))) continue;

    // Check face intersection precisely 
    // Not implemented yet ???

    // Add hit to result
    R3MeshIntersection *hit = new R3MeshIntersection();
    hit->type = R3_MESH_FACE_TYPE;
    hit->vertex = NULL;
    hit->edge = NULL;
    hit->face = face_container->face;
    hit->point = mesh->FaceCentroid(face_container->face);
    hit->t = 0;
    hits.Insert(hit);    
  }

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

    // Search children
    R3Box child_box;
    child_box = node_box;
    child_box[RN_HI][node->split_dimension] = node->split_coordinate;
    FindAll(shape, hits, node->children[0], child_box);
    child_box = node_box;
    child_box[RN_LO][node->split_dimension] = node->split_coordinate;
    FindAll(shape, hits, node->children[1], 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;

      // Check face bbox
      if (!R3Intersects(shape, mesh->FaceBBox(face_container->face))) continue;

      // Check face intersection precisely 
      // Not implemented yet ???

      // Add hit to result
      R3MeshIntersection *hit = new R3MeshIntersection();
      hit->type = R3_MESH_FACE_TYPE;
      hit->vertex = NULL;
      hit->edge = NULL;
      hit->face = face_container->face;
      hit->point = mesh->FaceCentroid(face_container->face);
      hit->t = 0;
      hits.Insert(hit);    
    }
  }
}

// Assembly:
subq $0x148, %rsp            # imm = 0x148
movq %rdi, 0x140(%rsp)
movq %rsi, 0x138(%rsp)
movq %rdx, 0x130(%rsp)
movq %rcx, 0x128(%rsp)
movq %r8, 0x120(%rsp)
movq 0x140(%rsp), %rax
movq %rax, 0x70(%rsp)
movq 0x138(%rsp), %rdi
movq 0x120(%rsp), %rsi
callq 0x21a80
cmpl $0x0, %eax
jne 0x2010b
jmp 0x20647
movl $0x0, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
movl %eax, 0x6c(%rsp)
movq 0x128(%rsp), %rdi
addq $0x28, %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x6c(%rsp), %eax
cmpl %ecx, %eax
jge 0x202e3
movq 0x128(%rsp), %rdi
addq $0x28, %rdi
movl 0x11c(%rsp), %esi
callq 0x21860
movq 0x70(%rsp), %rcx
movq (%rax), %rax
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rax
movl 0x14(%rax), %eax
cmpl 0x14(%rcx), %eax
jne 0x2017d
jmp 0x202a5
movq 0x70(%rsp), %rax
movl 0x14(%rax), %edx
movq 0x110(%rsp), %rcx
movl %edx, 0x14(%rcx)
movq 0x138(%rsp), %rcx
movq %rcx, 0x60(%rsp)
movq (%rax), %rdi
movq 0x110(%rsp), %rax
movq (%rax), %rsi
callq 0x3a630
movq 0x60(%rsp), %rdi
movq %rax, %rsi
callq 0x21a80
cmpl $0x0, %eax
jne 0x201c7
jmp 0x202a5
movl $0x40, %edi
callq 0xf4c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x58(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0x17ed0
jmp 0x201f5
movq 0x70(%rsp), %rax
movq 0x58(%rsp), %rcx
movq %rcx, 0x108(%rsp)
movq 0x108(%rsp), %rcx
movl $0x3, (%rcx)
movq 0x108(%rsp), %rcx
movq $0x0, 0x8(%rcx)
movq 0x108(%rsp), %rcx
movq $0x0, 0x10(%rcx)
movq 0x110(%rsp), %rcx
movq (%rcx), %rdx
movq 0x108(%rsp), %rcx
movq %rdx, 0x18(%rcx)
movq (%rax), %rsi
movq 0x110(%rsp), %rax
movq (%rax), %rdx
leaq 0xe0(%rsp), %rdi
callq 0x37790
movq 0x108(%rsp), %rdi
addq $0x20, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x935a0
movq 0x108(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rax)
movq 0x130(%rsp), %rdi
movq 0x108(%rsp), %rsi
callq 0x219e0
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0x20116
movq 0x58(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
movl $0x40, %esi
callq 0xf4d0
jmp 0x2064f
movq 0x128(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x20476
leaq 0xa8(%rsp), %rdi
movq %rdi, 0x50(%rsp)
callq 0x67090
movq 0x50(%rsp), %rdi
movq 0x120(%rsp), %rsi
callq 0x17760
jmp 0x2031c
movq 0x128(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x40(%rsp)
leaq 0xa8(%rsp), %rdi
movl $0x1, %esi
callq 0x17820
movq %rax, 0x48(%rsp)
jmp 0x20348
movq 0x48(%rsp), %rdi
movq 0x128(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x38(%rsp)
jmp 0x20364
movq 0x70(%rsp), %rdi
movq 0x38(%rsp), %rax
movsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x138(%rsp), %rsi
movq 0x130(%rsp), %rdx
movq 0x128(%rsp), %rax
movq 0x8(%rax), %rcx
leaq 0xa8(%rsp), %r8
callq 0x200b0
jmp 0x203a3
movq 0x120(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x17760
jmp 0x203ba
movq 0x128(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
leaq 0xa8(%rsp), %rdi
xorl %esi, %esi
callq 0x17820
movq %rax, 0x30(%rsp)
jmp 0x203e3
movq 0x30(%rsp), %rdi
movq 0x128(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x20(%rsp)
jmp 0x203ff
movq 0x70(%rsp), %rdi
movq 0x20(%rsp), %rax
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x138(%rsp), %rsi
movq 0x130(%rsp), %rdx
movq 0x128(%rsp), %rax
movq 0x10(%rax), %rcx
leaq 0xa8(%rsp), %r8
callq 0x200b0
jmp 0x2043e
leaq 0xa8(%rsp), %rdi
callq 0x17670
jmp 0x20647
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x17670
jmp 0x2064f
movl $0x0, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
movl %eax, 0x1c(%rsp)
movq 0x128(%rsp), %rdi
addq $0x38, %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0x20645
movq 0x128(%rsp), %rdi
addq $0x38, %rdi
movl 0xa4(%rsp), %esi
callq 0x21860
movq 0x70(%rsp), %rcx
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rax
movl 0x14(%rax), %eax
cmpl 0x14(%rcx), %eax
jne 0x204e8
jmp 0x2060a
movq 0x70(%rsp), %rax
movl 0x14(%rax), %edx
movq 0x98(%rsp), %rcx
movl %edx, 0x14(%rcx)
movq 0x138(%rsp), %rcx
movq %rcx, 0x10(%rsp)
movq (%rax), %rdi
movq 0x98(%rsp), %rax
movq (%rax), %rsi
callq 0x3a630
movq 0x10(%rsp), %rdi
movq %rax, %rsi
callq 0x21a80
cmpl $0x0, %eax
jne 0x20532
jmp 0x2060a
movl $0x40, %edi
callq 0xf4c0
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x30(%rdi)
movaps %xmm0, 0x20(%rdi)
movaps %xmm0, 0x10(%rdi)
movaps %xmm0, (%rdi)
callq 0x17ed0
jmp 0x20560
movq 0x70(%rsp), %rax
movq 0x8(%rsp), %rcx
movq %rcx, 0x90(%rsp)
movq 0x90(%rsp), %rcx
movl $0x3, (%rcx)
movq 0x90(%rsp), %rcx
movq $0x0, 0x8(%rcx)
movq 0x90(%rsp), %rcx
movq $0x0, 0x10(%rcx)
movq 0x98(%rsp), %rcx
movq (%rcx), %rdx
movq 0x90(%rsp), %rcx
movq %rdx, 0x18(%rcx)
movq (%rax), %rsi
movq 0x98(%rsp), %rax
movq (%rax), %rdx
leaq 0x78(%rsp), %rdi
callq 0x37790
movq 0x90(%rsp), %rdi
addq $0x20, %rdi
leaq 0x78(%rsp), %rsi
callq 0x935a0
movq 0x90(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x38(%rax)
movq 0x130(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x219e0
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x20481
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x100(%rsp)
movl %eax, 0xfc(%rsp)
movl $0x40, %esi
callq 0xf4d0
jmp 0x2064f
jmp 0x20647
addq $0x148, %rsp            # imm = 0x148
retq
movq 0x100(%rsp), %rdi
callq 0xf890
nopl (%rax)