// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3MeshSearchTree.cpp
// Function: gaps::R3MeshSearchTree::FindClosest(gaps::R3Point const&, gaps::R3Vector 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::
FindClosest(const R3Point& query_position, const R3Vector& query_normal, R3MeshIntersection& closest, 
  RNScalar min_distance_squared, RNScalar& max_distance_squared, 
  int (*IsCompatible)(const R3Point&, const R3Vector&, R3Mesh *, R3MeshFace *, void *), void *compatible_data,
  R3MeshSearchTreeNode *node, const R3Box& node_box) const
{
  // Compute distance (squared) from query point to node bbox
  RNScalar distance_squared = DistanceSquared(query_position, node_box, max_distance_squared);
  if (distance_squared >= max_distance_squared) return;

  // Update based on distance 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
    FindClosest(query_position, query_normal, closest, 
      min_distance_squared, max_distance_squared, 
      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 from query point to split plane
    RNScalar side = query_position[node->split_dimension] - node->split_coordinate;

    // Search children nodes
    if (side <= 0) {
      // Search negative side first
      R3Box child_box(node_box);
      child_box[RN_HI][node->split_dimension] = node->split_coordinate;
      FindClosest(query_position, query_normal, closest, 
        min_distance_squared, max_distance_squared, IsCompatible, compatible_data,
        node->children[0], child_box);
      if (side*side < max_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_LO][node->split_dimension] = node->split_coordinate;
        FindClosest(query_position, query_normal, closest, 
          min_distance_squared, max_distance_squared, IsCompatible, compatible_data,
          node->children[1], child_box);
      }
    }
    else {
      // Search positive side first
      R3Box child_box(node_box);
      child_box[RN_LO][node->split_dimension] = node->split_coordinate;
      FindClosest(query_position, query_normal, closest, 
        min_distance_squared, max_distance_squared, IsCompatible, compatible_data,
        node->children[1], child_box);
      if (side*side < max_distance_squared) {
        R3Box child_box(node_box);
        child_box[RN_HI][node->split_dimension] = node->split_coordinate;
        FindClosest(query_position, query_normal, closest, 
          min_distance_squared, max_distance_squared, 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
      FindClosest(query_position, query_normal, closest, 
        min_distance_squared, max_distance_squared, 
        IsCompatible, compatible_data, face_container->face);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %r9, 0x18(%rsp)
movq %r8, %r14
movsd %xmm0, 0x10(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, %r12
movq %rsi, %r13
movq %rdi, %rbp
movq 0xe0(%rsp), %rdx
movsd (%r8), %xmm0
movsd %xmm0, 0x20(%rsp)
callq 0x17134
ucomisd 0x20(%rsp), %xmm0
jae 0x1753d
movq 0xd8(%rsp), %rbx
movl 0x34(%rbx), %eax
testl %eax, %eax
jle 0x172f5
xorl %r15d, %r15d
movq 0x28(%rbx), %rcx
movq (%rcx,%r15,8), %r10
movl 0x14(%rbp), %ecx
cmpl %ecx, 0x14(%r10)
je 0x172ea
movl %ecx, 0x14(%r10)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
movsd 0x10(%rsp), %xmm0
movq %r14, %r8
movq 0x18(%rsp), %r9
pushq (%r10)
pushq 0xd8(%rsp)
callq 0x165cc
addq $0x10, %rsp
movl 0x34(%rbx), %eax
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x172a3
cmpq $0x0, 0x8(%rbx)
je 0x173fa
movslq 0x20(%rbx), %rax
movsd (%r13,%rax,8), %xmm1
subsd 0x18(%rbx), %xmm1
xorpd %xmm0, %xmm0
movsd %xmm1, 0x20(%rsp)
ucomisd %xmm1, %xmm0
jae 0x1745f
leaq 0x60(%rsp), %rdi
movq 0xe0(%rsp), %r15
movq %r15, %rsi
callq 0x3aa88
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x68(%rsp,%rax,8)
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movsd 0x18(%rsp), %xmm0
movq %r14, %r8
movq 0x20(%rsp), %r9
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0x10(%rbx)
pushq 0xe8(%rsp)
callq 0x17240
addq $0x20, %rsp
movsd 0x20(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd (%r14), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x173f5
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x3aa88
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x48(%rsp,%rax,8)
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movsd 0x18(%rsp), %xmm0
movq %r14, %r8
movq 0x20(%rsp), %r9
leaq 0x30(%rsp), %rax
pushq %rax
pushq 0x8(%rbx)
pushq 0xe8(%rsp)
callq 0x17240
addq $0x20, %rsp
leaq 0x28(%rsp), %rdi
callq 0x3b8a8
jmp 0x17533
movl 0x44(%rbx), %eax
testl %eax, %eax
jle 0x1753d
xorl %r15d, %r15d
movq 0x38(%rbx), %rcx
movq (%rcx,%r15,8), %r10
movl 0x14(%rbp), %ecx
cmpl %ecx, 0x14(%r10)
je 0x1744f
movl %ecx, 0x14(%r10)
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x8(%rsp), %rcx
movsd 0x10(%rsp), %xmm0
movq %r14, %r8
movq 0x18(%rsp), %r9
pushq (%r10)
pushq 0xd8(%rsp)
callq 0x165cc
addq $0x10, %rsp
movl 0x44(%rbx), %eax
incq %r15
movslq %eax, %rcx
cmpq %rcx, %r15
jl 0x17408
jmp 0x1753d
leaq 0x60(%rsp), %rdi
movq 0xe0(%rsp), %r15
movq %r15, %rsi
callq 0x3aa88
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x80(%rsp,%rax,8)
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movsd 0x18(%rsp), %xmm0
movq %r14, %r8
movq 0x20(%rsp), %r9
leaq 0x68(%rsp), %rax
pushq %rax
pushq 0x8(%rbx)
pushq 0xe8(%rsp)
callq 0x17240
addq $0x20, %rsp
movsd 0x20(%rsp), %xmm1
mulsd %xmm1, %xmm1
movsd (%r14), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x17533
leaq 0x28(%rsp), %rdi
movq %r15, %rsi
callq 0x3aa88
movsd 0x18(%rbx), %xmm0
movslq 0x20(%rbx), %rax
movsd %xmm0, 0x30(%rsp,%rax,8)
subq $0x8, %rsp
movq %rbp, %rdi
movq %r13, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movsd 0x18(%rsp), %xmm0
movq %r14, %r8
movq 0x20(%rsp), %r9
leaq 0x30(%rsp), %rax
pushq %rax
pushq 0x10(%rbx)
pushq 0xe8(%rsp)
callq 0x17240
addq $0x20, %rsp
leaq 0x28(%rsp), %rdi
callq 0x3b8a8
leaq 0x60(%rsp), %rdi
callq 0x3b8a8
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3b8a8
jmp 0x17579
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x3b8a8
jmp 0x17579
jmp 0x17571
jmp 0x17576
movq %rax, %rbx
jmp 0x17579
movq %rax, %rbx
leaq 0x60(%rsp), %rdi
callq 0x3b8a8
movq %rbx, %rdi
callq 0xe7f0
nop