// File: tomfunkhouser[P]gaps/apps/scn2cam/scn2cam.cpp
// Function: ObjectCoverageScore(gaps::R3Camera const&, gaps::R3Scene*, gaps::R3SceneNode*)
static RNScalar
ObjectCoverageScore(const R3Camera& camera, R3Scene *scene, R3SceneNode *node = NULL)
{
  // Allocate points
  const int max_nsamples = 1024;
  const int target_nsamples = 512;
  static R3Point samples[max_nsamples];
  static int nsamples = 0;

  // Check if same node as last time
  // (NOTE: THIS WILL NOT PARALLELIZE)
  static R3SceneNode *last_node = NULL;
  if (last_node != node) {
    last_node = node;
    nsamples = 0;

    // Generate samples on surface of node
    RNArea total_area = 0;
    for (int j = 0; j < node->NElements(); j++) {
      R3SceneElement *element = node->Element(j);
      for (int k = 0; k < element->NShapes(); k++) {
        R3Shape *shape = element->Shape(k);
        RNScalar area = shape->Area();
        total_area += area;
      }
    }

    // Check total area
    if (RNIsZero(total_area)) return 0;

    // Generate samples 
    for (int i = 0; i < node->NElements(); i++) {
      R3SceneElement *element = node->Element(i);
      for (int j = 0; j < element->NShapes(); j++) {
        R3Shape *shape = element->Shape(j);
        if (shape->ClassID() == R3TriangleArray::CLASS_ID()) {
          R3TriangleArray *triangles = (R3TriangleArray *) shape;
          for (int k = 0; k < triangles->NTriangles(); k++) {
            R3Triangle *triangle = triangles->Triangle(k);
            RNScalar area = triangle->Area();
            RNScalar real_nsamples = target_nsamples * area / total_area;
            int nsamples = (int) real_nsamples;
            if (RNRandomScalar() < (real_nsamples - nsamples)) nsamples++;
            for (int m = 0; m < nsamples; m++) {
              if (nsamples >= max_nsamples) break;
              R3Point sample = triangle->RandomPoint();
              samples[nsamples++] = sample;
            }
          }
        }
      }
    }
  }

  // Check number of samples
  if (nsamples == 0) return 0;

  // Count how many samples are visible
  int nvisible = 0;
  for (int i = 0; i < nsamples; i++) {
    const R3Point& sample = samples[i];
    R3Ray ray(camera.Origin(), sample);
    const RNScalar tolerance_t = 0.01;
    RNScalar max_t = R3Distance(camera.Origin(), sample) + tolerance_t;
    RNScalar hit_t = FLT_MAX;
    R3SceneNode *hit_node = NULL;
    if (scene->Intersects(ray, &hit_node, NULL, NULL, NULL, NULL, &hit_t, 0, max_t)) {
      if ((hit_node == node) && (RNIsEqual(hit_t, max_t, tolerance_t))) nvisible++;
    }
  }

  // Compute score as fraction of samples that are visible
  RNScalar score = (RNScalar) nvisible/ (RNScalar) nsamples;

  // Return score
  return score;
}

// Assembly:
subq $0x178, %rsp            # imm = 0x178
movq %rdi, 0x168(%rsp)
movq %rsi, 0x160(%rsp)
movq %rdx, 0x158(%rsp)
movl $0x400, 0x154(%rsp) # imm = 0x400
movl $0x200, 0x150(%rsp) # imm = 0x200
cmpb $0x0, 0x207134(%rip)    # 0x235c50
jne 0x2eb7b
leaq 0x20712b(%rip), %rdi    # 0x235c50
callq 0x242d0
cmpl $0x0, %eax
je 0x2eb7b
leaq 0x20111a(%rip), %rax    # 0x22fc50
movq %rax, 0x38(%rsp)
jmp 0x2eb3d
movq 0x38(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0xd9f30
jmp 0x2eb4e
movq 0x30(%rsp), %rax
addq $0x18, %rax
leaq 0x2010f2(%rip), %rcx    # 0x22fc50
addq $0x6000, %rcx           # imm = 0x6000
cmpq %rcx, %rax
movq %rax, 0x38(%rsp)
jne 0x2eb3d
leaq 0x2070da(%rip), %rdi    # 0x235c50
callq 0x23640
movq 0x2070de(%rip), %rax    # 0x235c60
cmpq 0x158(%rsp), %rax
je 0x2ef73
movq 0x158(%rsp), %rax
movq %rax, 0x2070c1(%rip)    # 0x235c60
movl $0x0, 0x2070af(%rip)    # 0x235c58
xorps %xmm0, %xmm0
movsd %xmm0, 0x138(%rsp)
movl $0x0, 0x134(%rsp)
movl 0x134(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq 0x158(%rsp), %rdi
callq 0x32480
movl %eax, %ecx
movl 0x2c(%rsp), %eax
cmpl %ecx, %eax
jge 0x2ecd4
movq 0x158(%rsp), %rdi
movl 0x134(%rsp), %esi
callq 0x331c0
movq %rax, 0x128(%rsp)
movl $0x0, 0x124(%rsp)
movl 0x124(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x128(%rsp), %rdi
callq 0x331f0
movl %eax, %ecx
movl 0x28(%rsp), %eax
cmpl %ecx, %eax
jge 0x2ecbc
movq 0x128(%rsp), %rdi
movl 0x124(%rsp), %esi
callq 0x33210
movq %rax, 0x118(%rsp)
movq 0x118(%rsp), %rdi
movq (%rdi), %rax
callq *0x58(%rax)
movsd %xmm0, 0x110(%rsp)
movsd 0x110(%rsp), %xmm0
addsd 0x138(%rsp), %xmm0
movsd %xmm0, 0x138(%rsp)
movl 0x124(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x124(%rsp)
jmp 0x2ec0d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x148(%rsp)
movl %eax, 0x144(%rsp)
leaq 0x206f9e(%rip), %rdi    # 0x235c50
callq 0x23620
jmp 0x2f11f
jmp 0x2ecbe
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0x2ebc0
movsd 0x138(%rsp), %xmm0
leaq 0x1fb5a4(%rip), %rax    # 0x22a288
movsd (%rax), %xmm1
callq 0x33240
cmpl $0x0, %eax
je 0x2ed03
xorps %xmm0, %xmm0
movsd %xmm0, 0x170(%rsp)
jmp 0x2f10e
movl $0x0, 0x10c(%rsp)
movl 0x10c(%rsp), %eax
movl %eax, 0x24(%rsp)
movq 0x158(%rsp), %rdi
callq 0x32480
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0x2ef71
movq 0x158(%rsp), %rdi
movl 0x10c(%rsp), %esi
callq 0x331c0
movq %rax, 0x100(%rsp)
movl $0x0, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0x100(%rsp), %rdi
callq 0x331f0
movl %eax, %ecx
movl 0x20(%rsp), %eax
cmpl %ecx, %eax
jge 0x2ef59
movq 0x100(%rsp), %rdi
movl 0xfc(%rsp), %esi
callq 0x33210
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
movq (%rdi), %rax
callq *0xb8(%rax)
movl %eax, 0x1c(%rsp)
callq 0xb9d80
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jne 0x2ef41
movq 0xf0(%rsp), %rax
movq %rax, 0xe8(%rsp)
movl $0x0, 0xe4(%rsp)
movl 0xe4(%rsp), %eax
movl %eax, 0x18(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x332a0
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0x2ef3f
movq 0xe8(%rsp), %rdi
movl 0xe4(%rsp), %esi
callq 0x332c0
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rdi
movq (%rdi), %rax
callq *0x58(%rax)
movsd %xmm0, 0xd0(%rsp)
movsd 0x18c257(%rip), %xmm0   # 0x1bb098
mulsd 0xd0(%rsp), %xmm0
divsd 0x138(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
cvttsd2si 0xc8(%rsp), %eax
movl %eax, 0xc4(%rsp)
callq 0x11f4d0
movaps %xmm0, %xmm1
movsd 0xc8(%rsp), %xmm0
cvtsi2sdl 0xc4(%rsp), %xmm2
subsd %xmm2, %xmm0
ucomisd %xmm1, %xmm0
jbe 0x2eea1
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
movl $0x0, 0xc0(%rsp)
movl 0xc0(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jge 0x2ef27
cmpl $0x400, 0xc4(%rsp) # imm = 0x400
jl 0x2eecb
jmp 0x2ef27
movq 0xd8(%rsp), %rsi
movq (%rsi), %rax
leaq 0xa8(%rsp), %rdi
callq *0x218(%rax)
movl 0xc4(%rsp), %eax
movl %eax, %ecx
addl $0x1, %ecx
movl %ecx, 0xc4(%rsp)
cltq
leaq 0x200d50(%rip), %rdi    # 0x22fc50
imulq $0x18, %rax, %rax
addq %rax, %rdi
leaq 0xa8(%rsp), %rsi
callq 0xda5d0
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0x2eeac
jmp 0x2ef29
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x2ede0
jmp 0x2ef41
jmp 0x2ef43
movl 0xfc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xfc(%rsp)
jmp 0x2ed5b
jmp 0x2ef5b
movl 0x10c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x10c(%rsp)
jmp 0x2ed0e
jmp 0x2ef73
cmpl $0x0, 0x206cde(%rip)    # 0x235c58
jne 0x2ef8d
xorps %xmm0, %xmm0
movsd %xmm0, 0x170(%rsp)
jmp 0x2f10e
movl $0x0, 0xa4(%rsp)
movl $0x0, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0x206ca8(%rip), %eax    # 0x235c58
jge 0x2f0e4
movslq 0xa0(%rsp), %rcx
leaq 0x200c8b(%rip), %rax    # 0x22fc50
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movq %rax, 0x98(%rsp)
movq 0x168(%rsp), %rdi
callq 0x332f0
movq %rax, %rsi
movq 0x98(%rsp), %rdx
leaq 0x68(%rsp), %rdi
callq 0xd9870
movsd 0x18c022(%rip), %xmm0   # 0x1bb020
movsd %xmm0, 0x60(%rsp)
movq 0x168(%rsp), %rdi
callq 0x332f0
movq %rax, %rdi
movq 0x98(%rsp), %rsi
callq 0x81750
movsd 0x18bff7(%rip), %xmm1   # 0x1bb020
addsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x18c005(%rip), %xmm0   # 0x1bb040
movsd %xmm0, 0x50(%rsp)
movq $0x0, 0x48(%rsp)
movq 0x160(%rsp), %rdi
movsd 0x58(%rsp), %xmm1
leaq 0x68(%rsp), %rsi
leaq 0x48(%rsp), %rdx
xorl %eax, %eax
movl %eax, %r9d
leaq 0x50(%rsp), %rax
xorps %xmm0, %xmm0
movq %r9, %rcx
movq %r9, %r8
movq $0x0, (%rsp)
movq %rax, 0x8(%rsp)
callq 0x388b0
cmpl $0x0, %eax
je 0x2f0cc
movq 0x48(%rsp), %rax
cmpq 0x158(%rsp), %rax
jne 0x2f0ca
movsd 0x50(%rsp), %xmm0
movsd 0x58(%rsp), %xmm1
movsd 0x18bf71(%rip), %xmm2   # 0x1bb020
callq 0x33300
cmpl $0x0, %eax
je 0x2f0ca
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x2f0cc
jmp 0x2f0ce
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0x2efa3
cvtsi2sdl 0xa4(%rsp), %xmm0
cvtsi2sdl 0x206b63(%rip), %xmm1 # 0x235c58
divsd %xmm1, %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x170(%rsp)
movsd 0x170(%rsp), %xmm0
addq $0x178, %rsp            # imm = 0x178
retq
movq 0x148(%rsp), %rdi
callq 0x24250
nopl (%rax)