// File: /workspace/github_cpp_makefile_star2_O2/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 $0x68, %rsp
movapd %xmm1, (%rsp)
movapd %xmm0, 0x20(%rsp)
movq %r9, %rbx
movq %r8, %r14
movq %rcx, %r12
movq %rdx, 0x38(%rsp)
movq %rsi, %r15
movq %rdi, %r13
callq 0x386ba
movq %r15, %rdi
movq %rax, %rsi
callq 0x483ab
xorl %ebp, %ebp
ucomisd (%rsp), %xmm0
ja 0x3853e
leaq 0x50(%rsp), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x6b884
leaq 0x58(%r13), %r15
movq %rbp, %rdi
movq %r15, %rsi
callq 0x6becc
movq %r15, %rdi
callq 0x668fe
leaq 0xf6f1e(%rip), %rax     # 0x12f0f8
movsd (%rax), %xmm2
movapd 0x8d34a(%rip), %xmm1    # 0xc5530
xorpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x381fa
ucomisd %xmm0, %xmm2
jae 0x3853c
movq %r15, 0x48(%rsp)
movq %r12, 0x30(%rsp)
movq 0xa0(%rsp), %r10
leaq 0xf6ef5(%rip), %rax     # 0x12f108
movsd (%rax), %xmm2
movapd 0x20(%rsp), %xmm4
movapd %xmm4, %xmm1
divsd %xmm0, %xmm1
movapd %xmm4, %xmm3
cmpltsd %xmm2, %xmm3
andpd %xmm3, %xmm1
andnpd %xmm4, %xmm3
orpd %xmm1, %xmm3
movapd (%rsp), %xmm5
movapd %xmm5, %xmm4
divsd %xmm0, %xmm4
movapd %xmm5, %xmm1
cmpltsd %xmm2, %xmm1
andpd %xmm1, %xmm4
andnpd %xmm5, %xmm1
orpd %xmm4, %xmm1
xorl %r12d, %r12d
xorl %ebp, %ebp
movsd %xmm0, 0x40(%rsp)
movapd %xmm3, 0x20(%rsp)
movslq 0x3c(%r13), %rax
cmpq %rax, %r12
jge 0x3832c
movq 0x30(%r13), %rax
movq (%rax,%r12,8), %r15
movq %r15, %rdi
leaq 0x50(%rsp), %rsi
movq %r14, %rdx
movq %rbx, %rcx
movq %r10, %r8
leaq 0x18(%rsp), %r9
movapd %xmm3, %xmm0
movapd %xmm1, (%rsp)
callq 0x397dc
testl %eax, %eax
je 0x38307
movsd 0x18(%rsp), %xmm0
movapd 0x20(%rsp), %xmm3
ucomisd %xmm3, %xmm0
jb 0x38317
movapd (%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq 0xa0(%rsp), %r10
jb 0x38324
movq 0x38(%rsp), %rax
testq %rax, %rax
je 0x382dc
movq %r13, (%rax)
movq 0x30(%rsp), %rcx
testq %rcx, %rcx
je 0x382ed
movq 0x8(%r15), %rax
movq %rax, (%rcx)
movq 0xa8(%rsp), %rax
testq %rax, %rax
je 0x382fe
movsd %xmm0, (%rax)
pushq $0x1
popq %rbp
movapd %xmm0, %xmm1
jmp 0x38324
movq 0xa0(%rsp), %r10
movapd 0x20(%rsp), %xmm3
jmp 0x3831f
movq 0xa0(%rsp), %r10
movapd (%rsp), %xmm1
incq %r12
jmp 0x3826d
xorl %r15d, %r15d
movq 0x30(%rsp), %r12
movslq 0x4c(%r13), %rax
cmpq %rax, %r15
jge 0x3840e
movq 0x40(%r13), %rax
movq (%rax,%r15,8), %rax
movq (%rax), %rdi
testq %rdi, %rdi
je 0x38406
leaq 0x50(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r12, %rcx
movq %r14, %r8
movq %rbx, %r9
movapd %xmm3, %xmm0
leaq 0x18(%rsp), %rax
pushq %rax
pushq %r10
movapd %xmm1, 0x10(%rsp)
callq 0x27fca
popq %rcx
popq %rdx
testl %eax, %eax
je 0x383f3
cmpq $0x0, 0xa8(%rsp)
sete %al
movsd 0x18(%rsp), %xmm0
movapd %xmm0, %xmm1
movapd (%rsp), %xmm4
cmpnlepd %xmm4, %xmm1
movapd 0x20(%rsp), %xmm3
movapd %xmm3, %xmm2
cmpnlepd %xmm0, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %ecx
testb $0x1, %cl
pushq $0x1
popq %rdx
cmovel %edx, %ebp
movapd %xmm4, %xmm1
jne 0x383cf
movapd %xmm0, %xmm1
orb %al, %cl
testb $0x1, %cl
movq 0xa0(%rsp), %r10
jne 0x38406
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax)
pushq $0x1
popq %rbp
movapd %xmm0, %xmm1
jmp 0x38406
movq 0xa0(%rsp), %r10
movapd 0x20(%rsp), %xmm3
movapd (%rsp), %xmm1
incq %r15
jmp 0x38334
xorl %r15d, %r15d
pushq $0x1
popq %r12
movslq 0x2c(%r13), %rax
cmpq %rax, %r15
jge 0x384e3
movq 0x20(%r13), %rax
movq (%rax,%r15,8), %rdi
leaq 0x50(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq 0x30(%rsp), %rcx
movq %r14, %r8
movq %rbx, %r9
movapd %xmm3, %xmm0
leaq 0x18(%rsp), %rax
pushq %rax
pushq %r10
movapd %xmm1, 0x10(%rsp)
callq 0x38162
popq %rcx
popq %rdx
testl %eax, %eax
je 0x384c8
cmpq $0x0, 0xa8(%rsp)
sete %al
movsd 0x18(%rsp), %xmm0
movapd %xmm0, %xmm1
movapd (%rsp), %xmm4
cmpnlepd %xmm4, %xmm1
movapd 0x20(%rsp), %xmm3
movapd %xmm3, %xmm2
cmpnlepd %xmm0, %xmm2
orpd %xmm1, %xmm2
movd %xmm2, %ecx
testb $0x1, %cl
cmovel %r12d, %ebp
movapd %xmm4, %xmm1
jne 0x384a4
movapd %xmm0, %xmm1
orb %al, %cl
testb $0x1, %cl
movq 0xa0(%rsp), %r10
jne 0x384db
movq 0xa8(%rsp), %rax
movsd %xmm0, (%rax)
movl %r12d, %ebp
movapd %xmm0, %xmm1
jmp 0x384db
movq 0xa0(%rsp), %r10
movapd 0x20(%rsp), %xmm3
movapd (%rsp), %xmm1
incq %r15
jmp 0x38415
testl %ebp, %ebp
je 0x3853c
testq %rbx, %rbx
movq 0x48(%rsp), %r14
je 0x38504
movq %rbx, %rdi
movq %r14, %rsi
callq 0x6beba
movq 0xa0(%rsp), %r10
testq %r10, %r10
je 0x3851f
movq %r10, %rdi
movq %r14, %rsi
movq %r10, %rbx
callq 0x6c8e0
movq %rbx, %rdi
callq 0x6c336
movq 0xa8(%rsp), %rax
testq %rax, %rax
je 0x3853e
movsd (%rax), %xmm0
mulsd 0x40(%rsp), %xmm0
movsd %xmm0, (%rax)
jmp 0x3853e
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop