// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %r8, %rbx
movq %rcx, %r12
movq %rdx, 0x8(%rsp)
movq %rdi, %r15
movq %r8, %rdi
movq %rsi, 0x10(%rsp)
callq 0x1d568
testl %eax, %eax
je 0x18c62
movq %rbx, 0x18(%rsp)
cmpl $0x0, 0x34(%r12)
jle 0x18ace
xorl %r14d, %r14d
movq 0x28(%r12), %rax
movq (%rax,%r14,8), %rbx
movl 0x14(%r15), %eax
cmpl %eax, 0x14(%rbx)
je 0x18abd
movl %eax, 0x14(%rbx)
movq (%r15), %rdi
movq (%rbx), %rsi
callq 0x25d8e
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x1d568
testl %eax, %eax
je 0x18abd
movl $0x40, %edi
callq 0xe440
movq %rax, %rbp
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movq %rax, %r13
addq $0x20, %r13
movq %r13, %rdi
callq 0x508cc
movl $0x3, (%rbp)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbp)
movq (%rbx), %rax
movq %rax, 0x18(%rbp)
movq (%r15), %rsi
movq (%rbx), %rdx
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x24280
movq %r13, %rdi
movq %rbx, %rsi
callq 0x50c3c
movq $0x0, 0x38(%rbp)
movq 0x8(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %rbp, %rsi
callq 0x7855a
incq %r14
movslq 0x34(%r12), %rax
cmpq %rax, %r14
jl 0x18a0f
cmpq $0x0, 0x8(%r12)
je 0x18b92
leaq 0x20(%rsp), %rdi
callq 0x3b2fc
leaq 0x28(%rsp), %r14
movq 0x18(%rsp), %rbx
leaq 0x8(%rbx), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x50c3c
leaq 0x40(%rsp), %r13
addq $0x20, %rbx
movq %r13, %rdi
movq %rbx, %rsi
callq 0x50c3c
movsd 0x18(%r12), %xmm0
movslq 0x20(%r12), %rax
movsd %xmm0, 0x40(%rsp,%rax,8)
movq 0x8(%r12), %rcx
leaq 0x20(%rsp), %r8
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x189ca
movq %r14, %rdi
movq %rbp, %rsi
callq 0x50c3c
movq %r13, %rdi
movq %rbx, %rsi
callq 0x50c3c
movsd 0x18(%r12), %xmm0
movslq 0x20(%r12), %rax
movsd %xmm0, 0x28(%rsp,%rax,8)
movq 0x10(%r12), %rcx
leaq 0x20(%rsp), %r8
movq %r15, %rdi
movq 0x10(%rsp), %rsi
movq 0x8(%rsp), %rdx
callq 0x189ca
leaq 0x20(%rsp), %rdi
callq 0x3c198
jmp 0x18c62
cmpl $0x0, 0x44(%r12)
jle 0x18c62
xorl %r14d, %r14d
movq 0x38(%r12), %rax
movq (%rax,%r14,8), %rbx
movl 0x14(%r15), %eax
cmpl %eax, 0x14(%rbx)
je 0x18c51
movl %eax, 0x14(%rbx)
movq (%r15), %rdi
movq (%rbx), %rsi
callq 0x25d8e
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x1d568
testl %eax, %eax
je 0x18c51
movl $0x40, %edi
callq 0xe440
movq %rax, %r13
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movq %rax, %rbp
addq $0x20, %rbp
movq %rbp, %rdi
callq 0x508cc
movl $0x3, (%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r13)
movq (%rbx), %rax
movq %rax, 0x18(%r13)
movq (%r15), %rsi
movq (%rbx), %rdx
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x24280
movq %rbp, %rdi
movq %rbx, %rsi
callq 0x50c3c
movq $0x0, 0x38(%r13)
movq 0x8(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %r13, %rsi
callq 0x7855a
incq %r14
movslq 0x44(%r12), %rax
cmpq %rax, %r14
jl 0x18ba1
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x40, %esi
movq %r13, %rdi
jmp 0x18c98
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3c198
jmp 0x18c9d
movq %rax, %rbx
movl $0x40, %esi
movq %rbp, %rdi
callq 0xe450
movq %rbx, %rdi
callq 0xe7f0
nop