// File: /workspace/github_cpp_makefile_star2_O2/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 0x1b73f
testl %eax, %eax
je 0x1721d
movq %rbx, 0x18(%rsp)
xorl %r13d, %r13d
movslq 0x34(%r12), %rax
cmpq %rax, %r13
jge 0x17181
movq 0x28(%r12), %rax
movq (%rax,%r13,8), %r14
movl 0x14(%r15), %eax
cmpl %eax, 0x14(%r14)
je 0x17179
movl %eax, 0x14(%r14)
movq (%r15), %rdi
movq (%r14), %rsi
callq 0x2286c
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x1b73f
testl %eax, %eax
je 0x17179
pushq $0x40
popq %rdi
callq 0xe450
movq %rax, %rbx
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 0x48bde
movl $0x3, (%rbx)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%rbx)
movq (%r14), %rdx
movq %rdx, 0x18(%rbx)
movq (%r15), %rsi
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x210ae
movq %rbp, %rdi
movq %r14, %rsi
callq 0x48eca
andq $0x0, 0x38(%rbx)
movq 0x8(%rsp), %rdi
movq %rbx, %rsi
callq 0x1378a
incq %r13
jmp 0x170c7
cmpq $0x0, 0x8(%r12)
je 0x1722c
leaq 0x20(%rsp), %rbx
movq %rbx, %rdi
callq 0x35c5a
movq %rbx, %rdi
movq 0x18(%rsp), %rbx
movq %rbx, %rsi
callq 0x1358c
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 0x1708e
leaq 0x20(%rsp), %rdi
movq %rbx, %rsi
callq 0x1358c
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 0x1708e
leaq 0x20(%rsp), %rdi
callq 0x36966
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %ebp, %ebp
movslq 0x44(%r12), %rax
cmpq %rax, %rbp
jge 0x1721d
movq 0x38(%r12), %rax
movq (%rax,%rbp,8), %r14
movl 0x14(%r15), %eax
cmpl %eax, 0x14(%r14)
je 0x172df
movl %eax, 0x14(%r14)
movq (%r15), %rdi
movq (%r14), %rsi
callq 0x2286c
movq %rax, %rdi
movq 0x10(%rsp), %rsi
callq 0x1b73f
testl %eax, %eax
je 0x172df
pushq $0x40
popq %rdi
callq 0xe450
movq %rax, %r13
xorps %xmm0, %xmm0
movaps %xmm0, (%rax)
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, 0x30(%rax)
movq %rax, %rbx
addq $0x20, %rbx
movq %rbx, %rdi
callq 0x48bde
movl $0x3, (%r13)
xorps %xmm0, %xmm0
movups %xmm0, 0x8(%r13)
movq (%r14), %rdx
movq %rdx, 0x18(%r13)
movq (%r15), %rsi
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x210ae
movq %rbx, %rdi
movq %r14, %rsi
callq 0x48eca
andq $0x0, 0x38(%r13)
movq 0x8(%rsp), %rdi
movq %r13, %rsi
callq 0x1378a
incq %rbp
jmp 0x1722e
movq %rax, %r14
leaq 0x20(%rsp), %rdi
callq 0x36966
jmp 0x1730f
movq %rax, %r14
pushq $0x40
popq %rsi
movq %r13, %rdi
jmp 0x1730a
movq %rax, %r14
pushq $0x40
popq %rsi
movq %rbx, %rdi
callq 0xe460
movq %r14, %rdi
callq 0xe820
nop