// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshSearchTree.cpp
// Function: gaps::R3MeshSearchTree::FindIntersection(gaps::R3Ray 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::
FindIntersection(const R3Ray& ray, R3MeshIntersection& closest, 
  RNScalar min_t, RNScalar& max_t, 
  int (*IsCompatible)(const R3Point&, const R3Vector&, R3Mesh *, R3MeshFace *, void *), void *compatible_data,
  R3MeshSearchTreeNode *node, const R3Box& node_box) const
{
  // Find intersection with bounding box
  RNScalar node_box_t;
  if (!R3Intersects(ray, node_box, NULL, NULL, &node_box_t)) return;
  if (node_box_t > max_t) return;

  // Update based on closest intersection 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
    FindIntersection(ray, closest, min_t, max_t, 
      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 along ray to split plane
    RNScalar side = ray.Start()[node->split_dimension] - node->split_coordinate;
    RNScalar vec = ray.Vector()[node->split_dimension];
    RNScalar plane_t = (side*vec < 0) ? -side/vec : RN_INFINITY;
    
    // Search children nodes
    if (side <= 0) {
      // Search negative side first
      if (plane_t >= min_t) {
        R3Box child_box(node_box);
        child_box[RN_HI][node->split_dimension] = node->split_coordinate;
        FindIntersection(ray, closest, min_t, max_t,
          IsCompatible, compatible_data, node->children[0], child_box);
      }
      if (plane_t < max_t) {
        R3Box child_box(node_box);
        child_box[RN_LO][node->split_dimension] = node->split_coordinate;
        FindIntersection(ray, closest, min_t, max_t, 
          IsCompatible, compatible_data, node->children[1], child_box);
      }
    }
    else {
      // Search positive side first
      if (plane_t >= min_t) {
        R3Box child_box(node_box);
        child_box[RN_LO][node->split_dimension] = node->split_coordinate;
        FindIntersection(ray, closest, min_t, max_t, 
          IsCompatible, compatible_data, node->children[1], child_box);
      }
      if (plane_t < max_t) {
        R3Box child_box(node_box);
        child_box[RN_HI][node->split_dimension] = node->split_coordinate;
        FindIntersection(ray, closest, min_t, max_t,
          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
      FindIntersection(ray, closest, min_t, max_t,
        IsCompatible, compatible_data, face_container->face);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %r9, 0x10(%rsp)
movq %r8, 0x8(%rsp)
movq %rcx, %r15
movsd %xmm0, (%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
movq 0xa8(%rsp), %rsi
leaq 0x60(%rsp), %r8
movq %r13, %rdi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x1a611
testl %eax, %eax
je 0x19039
movsd 0x60(%rsp), %xmm0
ucomisd (%r15), %xmm0
ja 0x19039
movq 0xa0(%rsp), %rbx
movl 0x34(%rbx), %eax
testl %eax, %eax
jle 0x18ddc
xorl %r14d, %r14d
movq 0x28(%rbx), %rcx
movq (%rcx,%r14,8), %r10
movl 0x14(%rbp), %ecx
cmpl %ecx, 0x14(%r10)
je 0x18dd1
movl %ecx, 0x14(%r10)
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movsd 0x8(%rsp), %xmm0
movq %r15, %rcx
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r9
pushq (%r10)
callq 0x18b90
addq $0x10, %rsp
movl 0x34(%rbx), %eax
incq %r14
movslq %eax, %rcx
cmpq %rcx, %r14
jl 0x18d8d
cmpq $0x0, 0x8(%rbx)
je 0x18f0b
movslq 0x20(%rbx), %rax
movsd (%r13,%rax,8), %xmm0
movsd 0x18(%r13,%rax,8), %xmm1
subsd 0x18(%rbx), %xmm0
movapd 0x9122a(%rip), %xmm2    # 0xaa030
xorpd %xmm0, %xmm2
ucomisd %xmm0, %xmm2
movapd %xmm0, %xmm3
mulsd %xmm1, %xmm3
divsd %xmm1, %xmm2
leaq 0xd6d97(%rip), %rax     # 0xefbb8
xorpd %xmm0, %xmm0
cmpltsd %xmm0, %xmm3
andpd %xmm3, %xmm2
movsd (%rax), %xmm0
andnpd %xmm0, %xmm3
orpd %xmm2, %xmm3
jae 0x18f6d
ucomisd (%rsp), %xmm3
movq 0xa8(%rsp), %r14
jb 0x18eae
movapd %xmm3, 0x50(%rsp)
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x3aa88
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x20(%rsp,%rax,8)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movsd (%rsp), %xmm0
movq %r15, %rcx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
leaq 0x18(%rsp), %rax
pushq %rax
pushq 0x10(%rbx)
callq 0x18d20
addq $0x10, %rsp
leaq 0x18(%rsp), %rdi
callq 0x3b8a8
movapd 0x50(%rsp), %xmm3
movsd (%r15), %xmm0
ucomisd %xmm3, %xmm0
jbe 0x19039
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x3aa88
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x38(%rsp,%rax,8)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movsd (%rsp), %xmm0
movq %r15, %rcx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
leaq 0x18(%rsp), %rax
pushq %rax
pushq 0x8(%rbx)
callq 0x18d20
addq $0x10, %rsp
jmp 0x1902f
movl 0x44(%rbx), %eax
testl %eax, %eax
jle 0x19039
xorl %r14d, %r14d
movq 0x38(%rbx), %rcx
movq (%rcx,%r14,8), %r10
movl 0x14(%rbp), %ecx
cmpl %ecx, 0x14(%r10)
je 0x18f5d
movl %ecx, 0x14(%r10)
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movsd 0x8(%rsp), %xmm0
movq %r15, %rcx
movq 0x10(%rsp), %r8
movq 0x18(%rsp), %r9
pushq (%r10)
callq 0x18b90
addq $0x10, %rsp
movl 0x44(%rbx), %eax
incq %r14
movslq %eax, %rcx
cmpq %rcx, %r14
jl 0x18f19
jmp 0x19039
ucomisd (%rsp), %xmm3
movq 0xa8(%rsp), %r14
jb 0x18fdb
movapd %xmm3, 0x50(%rsp)
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x3aa88
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x38(%rsp,%rax,8)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movsd (%rsp), %xmm0
movq %r15, %rcx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
leaq 0x18(%rsp), %rax
pushq %rax
pushq 0x8(%rbx)
callq 0x18d20
addq $0x10, %rsp
leaq 0x18(%rsp), %rdi
callq 0x3b8a8
movapd 0x50(%rsp), %xmm3
movsd (%r15), %xmm0
ucomisd %xmm3, %xmm0
jbe 0x19039
leaq 0x18(%rsp), %rdi
movq %r14, %rsi
callq 0x3aa88
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x20(%rsp,%rax,8)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movsd (%rsp), %xmm0
movq %r15, %rcx
movq 0x8(%rsp), %r8
movq 0x10(%rsp), %r9
leaq 0x18(%rsp), %rax
pushq %rax
pushq 0x10(%rbx)
callq 0x18d20
addq $0x10, %rsp
leaq 0x18(%rsp), %rdi
callq 0x3b8a8
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1904e
jmp 0x1904e
jmp 0x1904e
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x3b8a8
movq %rbx, %rdi
callq 0xe7f0
nop