// File: 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:
subq $0x218, %rsp            # imm = 0x218
movq 0x228(%rsp), %rax
movq 0x220(%rsp), %rax
movq %rdi, 0x210(%rsp)
movq %rsi, 0x208(%rsp)
movq %rdx, 0x200(%rsp)
movsd %xmm0, 0x1f8(%rsp)
movq %rcx, 0x1f0(%rsp)
movq %r8, 0x1e8(%rsp)
movq %r9, 0x1e0(%rsp)
movq 0x210(%rsp), %rax
movq %rax, 0xa8(%rsp)
movq 0x208(%rsp), %rdi
movq 0x228(%rsp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x1d8(%rsp), %r8
movq %rcx, %rdx
callq 0x241f0
cmpl $0x0, %eax
jne 0x209de
jmp 0x2114b
movsd 0x1d8(%rsp), %xmm0
movq 0x1f0(%rsp), %rax
ucomisd (%rax), %xmm0
jbe 0x209fa
jmp 0x2114b
movl $0x0, 0x1d4(%rsp)
movl 0x1d4(%rsp), %eax
movl %eax, 0xa4(%rsp)
movq 0x220(%rsp), %rdi
addq $0x28, %rdi
callq 0x178e0
movl %eax, %ecx
movl 0xa4(%rsp), %eax
cmpl %ecx, %eax
jge 0x20ae3
movq 0x220(%rsp), %rdi
addq $0x28, %rdi
movl 0x1d4(%rsp), %esi
callq 0x21860
movq 0xa8(%rsp), %rcx
movq (%rax), %rax
movq %rax, 0x1c8(%rsp)
movq 0x1c8(%rsp), %rax
movl 0x14(%rax), %eax
cmpl 0x14(%rcx), %eax
jne 0x20a72
jmp 0x20acd
movq 0xa8(%rsp), %rdi
movl 0x14(%rdi), %ecx
movq 0x1c8(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x208(%rsp), %rsi
movq 0x200(%rsp), %rdx
movsd 0x1f8(%rsp), %xmm0
movq 0x1f0(%rsp), %rcx
movq 0x1e8(%rsp), %r8
movq 0x1e0(%rsp), %r9
movq 0x1c8(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
callq 0x206e0
movl 0x1d4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x1d4(%rsp)
jmp 0x20a05
movq 0x220(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x21066
movq 0x208(%rsp), %rdi
callq 0x21ab0
movq %rax, %rdi
movq 0x220(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17e60
movq 0x220(%rsp), %rax
subsd 0x18(%rax), %xmm0
movsd %xmm0, 0x1c0(%rsp)
movq 0x208(%rsp), %rdi
callq 0x21ac0
movq %rax, %rdi
movq 0x220(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x21ad0
movsd %xmm0, 0x1b8(%rsp)
movsd 0x1c0(%rsp), %xmm1
mulsd 0x1b8(%rsp), %xmm1
xorps %xmm0, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x20ba4
movsd 0x1c0(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
divsd 0x1b8(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
jmp 0x20bb8
leaq 0x165955(%rip), %rax    # 0x186500
movsd (%rax), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x1b0(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x1c0(%rsp), %xmm0
jb 0x20e29
movsd 0x1b0(%rsp), %xmm0
ucomisd 0x1f8(%rsp), %xmm0
jb 0x20d07
movq 0x228(%rsp), %rsi
leaq 0x178(%rsp), %rdi
movq %rdi, 0x80(%rsp)
callq 0x67100
movq 0x80(%rsp), %rdi
movq 0x220(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x88(%rsp)
movl $0x1, %esi
callq 0x17820
movq %rax, 0x90(%rsp)
jmp 0x20c43
movq 0x90(%rsp), %rdi
movq 0x220(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x78(%rsp)
jmp 0x20c62
movq 0xa8(%rsp), %rdi
movq 0x78(%rsp), %rax
movsd 0x88(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x208(%rsp), %rsi
movq 0x200(%rsp), %rdx
movsd 0x1f8(%rsp), %xmm0
movq 0x1f0(%rsp), %rcx
movq 0x1e8(%rsp), %r8
movq 0x1e0(%rsp), %r9
movq 0x220(%rsp), %rax
movq 0x8(%rax), %r10
movq %rsp, %rax
leaq 0x178(%rsp), %r11
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x20950
jmp 0x20cd2
leaq 0x178(%rsp), %rdi
callq 0x17670
jmp 0x20d07
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x178(%rsp), %rdi
callq 0x17670
jmp 0x21153
movsd 0x1b0(%rsp), %xmm1
movq 0x1f0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x20e24
movq 0x228(%rsp), %rsi
leaq 0x130(%rsp), %rdi
movq %rdi, 0x60(%rsp)
callq 0x67100
movq 0x60(%rsp), %rdi
movq 0x220(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x68(%rsp)
xorl %esi, %esi
callq 0x17820
movq %rax, 0x70(%rsp)
jmp 0x20d66
movq 0x70(%rsp), %rdi
movq 0x220(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x58(%rsp)
jmp 0x20d82
movq 0xa8(%rsp), %rdi
movq 0x58(%rsp), %rax
movsd 0x68(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x208(%rsp), %rsi
movq 0x200(%rsp), %rdx
movsd 0x1f8(%rsp), %xmm0
movq 0x1f0(%rsp), %rcx
movq 0x1e8(%rsp), %r8
movq 0x1e0(%rsp), %r9
movq 0x220(%rsp), %rax
movq 0x10(%rax), %r10
movq %rsp, %rax
leaq 0x130(%rsp), %r11
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x20950
jmp 0x20def
leaq 0x130(%rsp), %rdi
callq 0x17670
jmp 0x20e24
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x17670
jmp 0x21153
jmp 0x21061
movsd 0x1b0(%rsp), %xmm0
ucomisd 0x1f8(%rsp), %xmm0
jb 0x20f3f
movq 0x228(%rsp), %rsi
leaq 0xf8(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0x67100
movq 0x40(%rsp), %rdi
movq 0x220(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x48(%rsp)
xorl %esi, %esi
callq 0x17820
movq %rax, 0x50(%rsp)
jmp 0x20e81
movq 0x50(%rsp), %rdi
movq 0x220(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x38(%rsp)
jmp 0x20e9d
movq 0xa8(%rsp), %rdi
movq 0x38(%rsp), %rax
movsd 0x48(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x208(%rsp), %rsi
movq 0x200(%rsp), %rdx
movsd 0x1f8(%rsp), %xmm0
movq 0x1f0(%rsp), %rcx
movq 0x1e8(%rsp), %r8
movq 0x1e0(%rsp), %r9
movq 0x220(%rsp), %rax
movq 0x10(%rax), %r10
movq %rsp, %rax
leaq 0xf8(%rsp), %r11
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x20950
jmp 0x20f0a
leaq 0xf8(%rsp), %rdi
callq 0x17670
jmp 0x20f3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0x17670
jmp 0x21153
movsd 0x1b0(%rsp), %xmm1
movq 0x1f0(%rsp), %rax
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2105f
movq 0x228(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x67100
movq 0x20(%rsp), %rdi
movq 0x220(%rsp), %rax
movsd 0x18(%rax), %xmm0
movsd %xmm0, 0x28(%rsp)
movl $0x1, %esi
callq 0x17820
movq %rax, 0x30(%rsp)
jmp 0x20fa1
movq 0x30(%rsp), %rdi
movq 0x220(%rsp), %rax
movl 0x20(%rax), %esi
callq 0x17800
movq %rax, 0x18(%rsp)
jmp 0x20fbd
movq 0xa8(%rsp), %rdi
movq 0x18(%rsp), %rax
movsd 0x28(%rsp), %xmm0
movsd %xmm0, (%rax)
movq 0x208(%rsp), %rsi
movq 0x200(%rsp), %rdx
movsd 0x1f8(%rsp), %xmm0
movq 0x1f0(%rsp), %rcx
movq 0x1e8(%rsp), %r8
movq 0x1e0(%rsp), %r9
movq 0x220(%rsp), %rax
movq 0x8(%rax), %r10
movq %rsp, %rax
leaq 0xc0(%rsp), %r11
movq %r11, 0x8(%rax)
movq %r10, (%rax)
callq 0x20950
jmp 0x2102a
leaq 0xc0(%rsp), %rdi
callq 0x17670
jmp 0x2105f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x170(%rsp)
movl %eax, 0x16c(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x17670
jmp 0x21153
jmp 0x21061
jmp 0x2114b
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x14(%rsp)
movq 0x220(%rsp), %rdi
addq $0x38, %rdi
callq 0x178e0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0x21149
movq 0x220(%rsp), %rdi
addq $0x38, %rdi
movl 0xbc(%rsp), %esi
callq 0x21860
movq 0xa8(%rsp), %rcx
movq (%rax), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movl 0x14(%rax), %eax
cmpl 0x14(%rcx), %eax
jne 0x210d8
jmp 0x21133
movq 0xa8(%rsp), %rdi
movl 0x14(%rdi), %ecx
movq 0xb0(%rsp), %rax
movl %ecx, 0x14(%rax)
movq 0x208(%rsp), %rsi
movq 0x200(%rsp), %rdx
movsd 0x1f8(%rsp), %xmm0
movq 0x1f0(%rsp), %rcx
movq 0x1e8(%rsp), %r8
movq 0x1e0(%rsp), %r9
movq 0xb0(%rsp), %rax
movq (%rax), %rax
movq %rax, (%rsp)
callq 0x206e0
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x21071
jmp 0x2114b
addq $0x218, %rsp            # imm = 0x218
retq
movq 0x170(%rsp), %rdi
callq 0xf890