// File: tomfunkhouser[P]gaps/pkgs/R3Graphics/R3SceneElement.cpp
// Function: gaps::R3SceneElement::Intersects(gaps::R3Ray const&, gaps::R3Shape**, gaps::R3Point*, gaps::R3Vector*, double*, double, double) const
RNBoolean R3SceneElement::
Intersects(const R3Ray& ray, R3Shape **hit_shape,
  R3Point *hit_point, R3Vector *hit_normal, RNScalar *hit_t,
  RNScalar min_t, RNScalar max_t) const
{
  // Variables
  RNScalar bbox_t;
  RNScalar closest_t = max_t;
  R3Point point;
  R3Vector normal;
  RNScalar t;

  // Check if ray intersects bounding box
  if (!R3Intersects(ray, BBox(), NULL, NULL, &bbox_t)) return FALSE;
  if (RNIsGreater(bbox_t, max_t)) return FALSE;

  // Intersect with shapes
  for (int i = 0; i < NShapes(); i++) {
    R3Shape *shape = Shape(i);
    if (shape->Intersects(ray, &point, &normal, &t)) {
      if ((t >= min_t) && (t <= closest_t)) {
        if (hit_shape) *hit_shape = shape;
        if (hit_point) *hit_point = point;
        if (hit_normal) *hit_normal = normal;
        if (hit_t) *hit_t = t;
        closest_t = t;
      }
    }
  }

  // Return whether hit any shape
  return (closest_t == max_t) ? FALSE : TRUE;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq %r9, 0x80(%rsp)
movsd %xmm0, 0x78(%rsp)
movsd %xmm1, 0x70(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x8(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
leaq 0x48(%rsp), %rdi
callq 0xcc860
leaq 0x30(%rsp), %rdi
callq 0xcd9c0
movq 0x8(%rsp), %rdi
movq 0xa0(%rsp), %rax
movq %rax, 0x10(%rsp)
callq 0x58430
movq 0x10(%rsp), %rdi
movq %rax, %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq 0x68(%rsp), %r8
movq %rcx, %rdx
callq 0x6bec0
cmpl $0x0, %eax
jne 0x597c5
movl $0x0, 0xb4(%rsp)
jmp 0x59940
movsd 0x68(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
leaq 0x19cad0(%rip), %rax    # 0x1f62a8
movsd (%rax), %xmm2
callq 0x585b0
cmpl $0x0, %eax
je 0x597f6
movl $0x0, 0xb4(%rsp)
jmp 0x59940
movl $0x0, 0x24(%rsp)
movq 0x8(%rsp), %rdi
movl 0x24(%rsp), %eax
movl %eax, 0x4(%rsp)
callq 0x2bbf0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x59915
movq 0x8(%rsp), %rdi
movl 0x24(%rsp), %esi
callq 0x2bc10
movq %rax, 0x18(%rsp)
movq 0x18(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq (%rdi), %rax
leaq 0x48(%rsp), %rdx
leaq 0x30(%rsp), %rcx
leaq 0x28(%rsp), %r8
callq *0x1d8(%rax)
cmpl $0x0, %eax
je 0x59903
movsd 0x28(%rsp), %xmm0
ucomisd 0x78(%rsp), %xmm0
jb 0x59901
movsd 0x28(%rsp), %xmm1
movsd 0x60(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x59901
cmpq $0x0, 0x98(%rsp)
je 0x5989e
movq 0x18(%rsp), %rcx
movq 0x98(%rsp), %rax
movq %rcx, (%rax)
cmpq $0x0, 0x90(%rsp)
je 0x598bb
movq 0x90(%rsp), %rdi
leaq 0x48(%rsp), %rsi
callq 0xccf00
cmpq $0x0, 0x88(%rsp)
je 0x598d8
movq 0x88(%rsp), %rdi
leaq 0x30(%rsp), %rsi
callq 0xce690
cmpq $0x0, 0x80(%rsp)
je 0x598f5
movsd 0x28(%rsp), %xmm0
movq 0x80(%rsp), %rax
movsd %xmm0, (%rax)
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
jmp 0x59903
jmp 0x59905
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x597fe
movsd 0x60(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movl $0x1, %eax
xorl %ecx, %ecx
ucomisd %xmm1, %xmm0
setnp %sil
sete %dl
testb %sil, %dl
cmovnel %ecx, %eax
movl %eax, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
nop