// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Graphics/R3SceneNode.cpp
// Function: gaps::R3SceneNode::FindClosest(gaps::R3Point const&, gaps::R3SceneNode**, gaps::R3Material**, gaps::R3Shape**, gaps::R3Point*, gaps::R3Vector*, double*, double, double) const
RNBoolean R3SceneNode::
FindClosest(const R3Point& point,
  R3SceneNode **hit_node, R3Material **hit_material, R3Shape **hit_shape,
  R3Point *hit_point, R3Vector *hit_normal, RNLength *hit_d,
  RNLength min_d, RNLength max_d) const
{
  // Temporary variables
  RNBoolean found = FALSE;
  RNScalar d;

  // Check if bounding box is within max_d
  RNScalar bbox_d = R3Distance(point, BBox());
  if (bbox_d > max_d) return FALSE;

  // Apply inverse transformation to point
  R3Point node_point = point;
  node_point.InverseTransform(transformation);

  // Apply inverse transform to min_d and max_d
  RNScalar scale = transformation.ScaleFactor();
  if (RNIsZero(scale)) return FALSE;
  if (min_d < RN_INFINITY) min_d /= scale;
  if (max_d < RN_INFINITY) max_d /= scale;

  // Find closest element 
  for (int i = 0; i < elements.NEntries(); i++) {
    R3SceneElement *element = elements.Kth(i);
    if (element->FindClosest(node_point, hit_shape, hit_point, hit_normal, &d, min_d, max_d)) {
      if ((d >= min_d) && (d <= max_d)) {
        if (hit_node) *hit_node = (R3SceneNode *) this;
        if (hit_material) *hit_material = element->Material();
        if (hit_d) *hit_d = d;
        found = TRUE;
        max_d = d;
      }
    }
  }

  // Find closest reference 
  for (int i = 0; i < references.NEntries(); i++) {
    R3SceneReference *reference = references.Kth(i);
    R3Scene *referenced_scene = reference->ReferencedScene();
    if (referenced_scene) {
      if (referenced_scene->FindClosest(node_point, hit_node, hit_material, hit_shape, hit_point, hit_normal, &d, min_d, max_d)) {
        if ((d >= min_d) && (d <= max_d)) {
          if (hit_d) *hit_d = d;
          found = TRUE;
          max_d = d;
        }
      }
    }
  }

  // Find closest node
  for (int i = 0; i < children.NEntries(); i++) {
    R3SceneNode *child = children.Kth(i);
    if (child->FindClosest(node_point, hit_node, hit_material, hit_shape, hit_point, hit_normal, &d, min_d, max_d)) {
      if ((d >= min_d) && (d <= max_d)) {
        if (hit_d) *hit_d = d;
        found = TRUE;
        max_d = d;
      }
    }
  }

  // Transform hit into parent's coordinate system
  if (found) {
    if (hit_point) hit_point->Transform(transformation);
    if (hit_normal) hit_normal->Transform(transformation);
    if (hit_normal) hit_normal->Normalize();
    if (hit_d) *hit_d *= scale;
  }

  // Return if found a point
  return found;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movapd %xmm1, (%rsp)
movaps %xmm0, 0x10(%rsp)
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r12
movq %rdx, 0x38(%rsp)
movq %rsi, %r15
movq %rdi, %r13
movsd 0x170(%rdi), %xmm0
ucomisd 0x9fd42(%rip), %xmm0    # 0xdbbb0
jne 0x3be7a
jp 0x3be7a
movq %r13, %rdi
callq 0x3c2b8
leaq 0x168(%r13), %rsi
movq %r15, %rdi
callq 0x4e35f
xorl %ebp, %ebp
ucomisd (%rsp), %xmm0
ja 0x3c27b
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x753da
leaq 0x58(%r13), %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x75cf2
movq %r15, %rdi
callq 0x6fada
leaq 0x102214(%rip), %rax    # 0x13e0d8
movsd (%rax), %xmm2
movapd 0x9e670(%rip), %xmm1    # 0xda540
xorpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x3bee4
ucomisd %xmm0, %xmm2
jae 0x3c279
movq %r15, 0x58(%rsp)
movq %r12, 0x30(%rsp)
movq 0xb0(%rsp), %r10
leaq 0x1021eb(%rip), %rax    # 0x13e0e8
movsd (%rax), %xmm2
movapd 0x10(%rsp), %xmm3
movapd %xmm3, %xmm1
divsd %xmm0, %xmm1
movapd %xmm3, %xmm4
cmpltsd %xmm2, %xmm4
andpd %xmm4, %xmm1
andnpd %xmm3, %xmm4
orpd %xmm1, %xmm4
movapd (%rsp), %xmm5
movapd %xmm5, %xmm3
divsd %xmm0, %xmm3
movapd %xmm5, %xmm1
cmpltsd %xmm2, %xmm1
andpd %xmm1, %xmm3
andnpd %xmm5, %xmm1
orpd %xmm3, %xmm1
cmpl $0x0, 0x3c(%r13)
movsd %xmm0, 0x10(%rsp)
movapd %xmm4, 0x40(%rsp)
jle 0x3c032
xorl %r12d, %r12d
xorl %ebp, %ebp
movq 0x30(%r13), %rax
movq (%rax,%r12,8), %r15
movq %r15, %rdi
leaq 0x60(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r10, %r8
leaq 0x28(%rsp), %r9
movapd %xmm4, %xmm0
movapd %xmm1, (%rsp)
callq 0x3d8e8
testl %eax, %eax
je 0x3bff7
movsd 0x28(%rsp), %xmm2
movapd 0x40(%rsp), %xmm4
ucomisd %xmm4, %xmm2
jb 0x3c00d
movapd (%rsp), %xmm1
ucomisd %xmm2, %xmm1
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
jb 0x3c020
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x3bfca
movq %r13, (%rax)
movq 0x30(%rsp), %rcx
testq %rcx, %rcx
je 0x3bfdb
movq 0x8(%r15), %rax
movq %rax, (%rcx)
movl $0x1, %ebp
movq 0xb8(%rsp), %rax
testq %rax, %rax
je 0x3bff1
movsd %xmm2, (%rax)
movapd %xmm2, %xmm1
jmp 0x3c020
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
movapd 0x40(%rsp), %xmm4
jmp 0x3c01b
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
movapd (%rsp), %xmm1
incq %r12
movslq 0x3c(%r13), %rax
cmpq %rax, %r12
jl 0x3bf62
jmp 0x3c034
xorl %ebp, %ebp
cmpl $0x0, 0x4c(%r13)
jle 0x3c12e
xorl %r15d, %r15d
movq 0x30(%rsp), %r12
movq 0x40(%r13), %rax
movq (%rax,%r15,8), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3c11e
leaq 0x60(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r12, %rcx
movq %r14, %r8
movq %rbx, %r9
movapd %xmm4, %xmm0
leaq 0x28(%rsp), %rax
pushq %rax
pushq %r10
movapd %xmm1, 0x10(%rsp)
callq 0x2907a
addq $0x10, %rsp
testl %eax, %eax
je 0x3c105
cmpq $0x0, 0xb8(%rsp)
sete %al
movsd 0x28(%rsp), %xmm3
movapd %xmm3, %xmm1
movapd (%rsp), %xmm0
cmpnlepd %xmm0, %xmm1
movapd 0x40(%rsp), %xmm4
movapd %xmm4, %xmm2
cmpnlepd %xmm3, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %ecx
testb $0x1, %cl
movl $0x1, %edx
cmovel %edx, %ebp
movapd %xmm0, %xmm1
jne 0x3c0d9
movapd %xmm3, %xmm1
orb %al, %cl
testb $0x1, %cl
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
jne 0x3c11e
movq 0xb8(%rsp), %rax
movsd %xmm3, (%rax)
movl $0x1, %ebp
movapd %xmm3, %xmm1
jmp 0x3c11e
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
movapd 0x40(%rsp), %xmm4
movapd (%rsp), %xmm1
incq %r15
movslq 0x4c(%r13), %rax
cmpq %rax, %r15
jl 0x3c047
cmpl $0x0, 0x2c(%r13)
jle 0x3c21a
xorl %r15d, %r15d
movl $0x1, %r12d
movapd %xmm1, (%rsp)
movq 0x20(%r13), %rax
movq (%rax,%r15,8), %rdi
leaq 0x60(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %r14, %r8
movq %rbx, %r9
movapd %xmm4, %xmm0
leaq 0x28(%rsp), %rax
pushq %rax
pushq %r10
callq 0x3be32
addq $0x10, %rsp
testl %eax, %eax
je 0x3c1f1
cmpq $0x0, 0xb8(%rsp)
sete %al
movsd 0x28(%rsp), %xmm3
movapd %xmm3, %xmm1
movapd (%rsp), %xmm0
cmpnlepd %xmm0, %xmm1
movapd 0x40(%rsp), %xmm4
movapd %xmm4, %xmm2
cmpnlepd %xmm3, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %ecx
testb $0x1, %cl
cmovel %r12d, %ebp
movapd %xmm0, %xmm1
jne 0x3c1c5
movapd %xmm3, %xmm1
orb %al, %cl
testb $0x1, %cl
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
jne 0x3c20a
movq 0xb8(%rsp), %rax
movsd %xmm3, (%rax)
movl $0x1, %ebp
movapd %xmm3, %xmm1
jmp 0x3c20a
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
movapd 0x40(%rsp), %xmm4
movapd (%rsp), %xmm1
incq %r15
movslq 0x2c(%r13), %rax
cmpq %rax, %r15
jl 0x3c142
testl %ebp, %ebp
je 0x3c279
testq %rbx, %rbx
movq 0x58(%rsp), %r14
je 0x3c241
movq %rbx, %rdi
movq %r14, %rsi
callq 0x75ce0
movq 0xb0(%rsp), %r10
movsd 0x10(%rsp), %xmm0
testq %r10, %r10
je 0x3c262
movq %r10, %rdi
movq %r14, %rsi
movq %r10, %rbx
callq 0x769de
movq %rbx, %rdi
callq 0x76174
movsd 0x10(%rsp), %xmm0
movq 0xb8(%rsp), %rax
testq %rax, %rax
je 0x3c27b
mulsd (%rax), %xmm0
movsd %xmm0, (%rax)
jmp 0x3c27b
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq