// File: /workspace/github_cpp_makefile_star2_O1/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 0xa181e(%rip), %xmm0    # 0xddba0
jne 0x3c38e
jp 0x3c38e
movq %r13, %rdi
callq 0x3c7ca
leaq 0x168(%r13), %rsi
movq %r15, %rdi
callq 0x4eeff
xorl %ebp, %ebp
ucomisd (%rsp), %xmm0
ja 0x3c78d
leaq 0x60(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x762ca
leaq 0x58(%r13), %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x76be8
movq %r15, %rdi
callq 0x70922
leaq 0x103d00(%rip), %rax    # 0x1400d8
movsd (%rax), %xmm2
movapd 0xa014c(%rip), %xmm1    # 0xdc530
xorpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x3c3fa
xorl %ebp, %ebp
ucomisd %xmm0, %xmm2
jae 0x3c78d
movq %r15, 0x58(%rsp)
movq %r12, 0x30(%rsp)
movq 0xb0(%rsp), %r10
leaq 0x103cd5(%rip), %rax    # 0x1400e8
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 0x3c548
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 0x3dd98
testl %eax, %eax
je 0x3c50d
movsd 0x28(%rsp), %xmm2
movapd 0x40(%rsp), %xmm4
ucomisd %xmm4, %xmm2
jb 0x3c523
movapd (%rsp), %xmm1
ucomisd %xmm2, %xmm1
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
jb 0x3c536
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x3c4e0
movq %r13, (%rax)
movq 0x30(%rsp), %rcx
testq %rcx, %rcx
je 0x3c4f1
movq 0x8(%r15), %rax
movq %rax, (%rcx)
movl $0x1, %ebp
movq 0xb8(%rsp), %rax
testq %rax, %rax
je 0x3c507
movsd %xmm2, (%rax)
movapd %xmm2, %xmm1
jmp 0x3c536
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
movapd 0x40(%rsp), %xmm4
jmp 0x3c531
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
movapd (%rsp), %xmm1
incq %r12
movslq 0x3c(%r13), %rax
cmpq %rax, %r12
jl 0x3c478
jmp 0x3c54a
xorl %ebp, %ebp
cmpl $0x0, 0x4c(%r13)
jle 0x3c644
xorl %r15d, %r15d
movq 0x30(%rsp), %r12
movq 0x40(%r13), %rax
movq (%rax,%r15,8), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x3c634
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 0x2913a
addq $0x10, %rsp
testl %eax, %eax
je 0x3c61b
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 0x3c5ef
movapd %xmm3, %xmm1
orb %al, %cl
testb $0x1, %cl
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
jne 0x3c634
movq 0xb8(%rsp), %rax
movsd %xmm3, (%rax)
movl $0x1, %ebp
movapd %xmm3, %xmm1
jmp 0x3c634
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 0x3c55d
cmpl $0x0, 0x2c(%r13)
jle 0x3c730
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 0x3c346
addq $0x10, %rsp
testl %eax, %eax
je 0x3c707
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 0x3c6db
movapd %xmm3, %xmm1
orb %al, %cl
testb $0x1, %cl
movsd 0x10(%rsp), %xmm0
movq 0xb0(%rsp), %r10
jne 0x3c720
movq 0xb8(%rsp), %rax
movsd %xmm3, (%rax)
movl $0x1, %ebp
movapd %xmm3, %xmm1
jmp 0x3c720
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 0x3c658
testl %ebp, %ebp
je 0x3c78d
testq %rbx, %rbx
movq 0x58(%rsp), %r14
je 0x3c757
movq %rbx, %rdi
movq %r14, %rsi
callq 0x76bd6
movq 0xb0(%rsp), %r10
movsd 0x10(%rsp), %xmm0
testq %r10, %r10
je 0x3c778
movq %r10, %rdi
movq %r14, %rsi
movq %r10, %rbx
callq 0x7792e
movq %rbx, %rdi
callq 0x77074
movsd 0x10(%rsp), %xmm0
movq 0xb8(%rsp), %rax
testq %rax, %rax
je 0x3c78d
mulsd (%rax), %xmm0
movsd %xmm0, (%rax)
movl %ebp, %eax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq