// File: tomfunkhouser[P]gaps/apps/scn2cam/scn2cam.cpp
// Function: SceneCoverageScore(gaps::R3Camera const&, gaps::R3Scene*, gaps::R3SceneNode*, int)
static RNScalar
SceneCoverageScore(const R3Camera& camera, R3Scene *scene, R3SceneNode *subtree = NULL, RNBoolean suncg = FALSE)
{
  // Allocate image for scoring
  R2Grid image(width, height);

  // Compute maximum number of pixels in image
  int max_pixel_count = width * height;
  if (max_pixel_count == 0) return 0;

  // Compute minimum number of pixels per object
  int min_pixel_count_per_object = min_visible_fraction * max_pixel_count;
  if (min_pixel_count_per_object == 0) return 0;

  // Render image
  RenderImage(image, camera, scene, scene->Root(), NULL, NODE_INDEX_IMAGE);

  // Allocate buffer for counting visible pixels of nodes
  int *node_pixel_counts = new int [ scene->NNodes() ];
  for (int i = 0; i < scene->NNodes(); i++) node_pixel_counts[i] = 0;
  
  // Log counts of pixels visible on each node
  for (int i = 0; i < image.NEntries(); i++) {      
    RNScalar value = image.GridValue(i);
    if (value == R2_GRID_UNKNOWN_VALUE) continue;
    int node_index = (int) (value + 0.5);
    if ((node_index < 0) || (node_index >= scene->NNodes())) continue;
    node_pixel_counts[node_index]++;
  }

  // Compute score
  RNScalar sum = 0;
  int node_count = 0;
  for (int i = 0; i < scene->NNodes(); i++) {
    R3SceneNode *node = scene->Node(i);
    if (suncg && !IsObject(node)) continue;
    if (subtree && !node->IsDecendent(subtree)) continue;
    if (node_pixel_counts[i] <= min_pixel_count_per_object) continue;
    sum += log(node_pixel_counts[i] / min_pixel_count_per_object);
    node_count++;
  }

  // Compute score (log of product of number of pixels visible in each object)
  RNScalar score = (node_count > min_visible_objects) ? sum : 0;

  // Delete pixel counts
  delete [] node_pixel_counts;

  // Return score
  return score;
}

// Assembly:
subq $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, 0x1c8(%rsp)
movq %rsi, 0x1c0(%rsp)
movq %rdx, 0x1b8(%rsp)
movl %ecx, 0x1b4(%rsp)
movl 0x1fa92c(%rip), %esi    # 0x229a88
movl 0x1fa92a(%rip), %edx    # 0x229a8c
leaq 0xe0(%rsp), %rdi
callq 0xf12b0
movl 0x1fa913(%rip), %eax    # 0x229a88
imull 0x1fa910(%rip), %eax    # 0x229a8c
movl %eax, 0xdc(%rsp)
cmpl $0x0, 0xdc(%rsp)
jne 0x2f1a9
xorps %xmm0, %xmm0
movsd %xmm0, 0x1d0(%rsp)
movl $0x1, 0xd8(%rsp)
jmp 0x2f5db
movsd 0x1fa927(%rip), %xmm0   # 0x229ad8
cvtsi2sdl 0xdc(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xd4(%rsp)
cmpl $0x0, 0xd4(%rsp)
jne 0x2f1ef
xorps %xmm0, %xmm0
movsd %xmm0, 0x1d0(%rsp)
movl $0x1, 0xd8(%rsp)
jmp 0x2f5db
movq 0x1c8(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x1c0(%rsp), %rdi
movq %rdi, 0x70(%rsp)
callq 0x33510
movq %rax, 0x78(%rsp)
jmp 0x2f215
movq 0x78(%rsp), %rcx
movq 0x70(%rsp), %rdx
movq 0x68(%rsp), %rsi
xorl %r9d, %r9d
movl %r9d, %r8d
leaq 0xe0(%rsp), %rdi
callq 0x2f610
jmp 0x2f239
movq 0x1c0(%rsp), %rdi
callq 0x321c0
movl %eax, 0x64(%rsp)
jmp 0x2f24c
movl 0x64(%rsp), %eax
cltq
movl $0x4, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x23040
movq %rax, 0x58(%rsp)
jmp 0x2f274
movq 0x58(%rsp), %rax
movq %rax, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x321c0
movl %eax, 0x54(%rsp)
jmp 0x2f2aa
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2f306
movq 0xb8(%rsp), %rax
movslq 0xb4(%rsp), %rcx
movl $0x0, (%rax,%rcx,4)
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x2f28c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xf2760
jmp 0x2f5f9
movl $0x0, 0xb0(%rsp)
movl 0xb0(%rsp), %eax
movl %eax, 0x48(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x33520
movl %eax, 0x4c(%rsp)
jmp 0x2f32f
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2f406
movl 0xb0(%rsp), %esi
leaq 0xe0(%rsp), %rdi
callq 0x33540
movsd %xmm0, 0x40(%rsp)
jmp 0x2f35b
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
movsd 0x18bd15(%rip), %xmm1   # 0x1bb090
ucomisd %xmm1, %xmm0
jne 0x2f385
jp 0x2f385
jmp 0x2f3f0
movsd 0x18bca3(%rip), %xmm0   # 0x1bb030
addsd 0xa8(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xa4(%rsp)
cmpl $0x0, 0xa4(%rsp)
jl 0x2f3d5
movl 0xa4(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x321c0
movl %eax, 0x3c(%rsp)
jmp 0x2f3c9
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jl 0x2f3d7
jmp 0x2f3f0
movq 0xb8(%rsp), %rax
movslq 0xa4(%rsp), %rcx
movl (%rax,%rcx,4), %edx
addl $0x1, %edx
movl %edx, (%rax,%rcx,4)
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x2f311
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
movl $0x0, 0x94(%rsp)
movl $0x0, 0x90(%rsp)
movl 0x90(%rsp), %eax
movl %eax, 0x30(%rsp)
movq 0x1c0(%rsp), %rdi
callq 0x321c0
movl %eax, 0x34(%rsp)
jmp 0x2f446
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jge 0x2f563
movq 0x1c0(%rsp), %rdi
movl 0x90(%rsp), %esi
callq 0x32430
movq %rax, 0x28(%rsp)
jmp 0x2f471
movq 0x28(%rsp), %rax
movq %rax, 0x88(%rsp)
cmpl $0x0, 0x1b4(%rsp)
je 0x2f4a9
movq 0x88(%rsp), %rdi
callq 0x2e9e0
movl %eax, 0x24(%rsp)
jmp 0x2f49b
movl 0x24(%rsp), %eax
cmpl $0x0, %eax
jne 0x2f4a9
jmp 0x2f54d
cmpq $0x0, 0x1b8(%rsp)
je 0x2f4da
movq 0x88(%rsp), %rdi
movq 0x1b8(%rsp), %rsi
callq 0x60fc0
movl %eax, 0x20(%rsp)
jmp 0x2f4cf
movl 0x20(%rsp), %eax
cmpl $0x0, %eax
jne 0x2f4da
jmp 0x2f54d
movq 0xb8(%rsp), %rax
movslq 0x90(%rsp), %rcx
movl (%rax,%rcx,4), %eax
cmpl 0xd4(%rsp), %eax
jg 0x2f4f8
jmp 0x2f54d
movq 0xb8(%rsp), %rax
movslq 0x90(%rsp), %rcx
movl (%rax,%rcx,4), %eax
movl 0xd4(%rsp), %ecx
cltd
idivl %ecx
movl %eax, %edi
callq 0x33560
movsd %xmm0, 0x18(%rsp)
jmp 0x2f524
movsd 0x18(%rsp), %xmm0
addsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x2f428
cvtsi2sdl 0x94(%rsp), %xmm0
ucomisd 0x1fa54c(%rip), %xmm0   # 0x229ac0
jbe 0x2f587
movsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x2f592
xorps %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
jmp 0x2f592
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0xb8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x2f5be
movq 0x8(%rsp), %rdi
callq 0x23d70
movsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x1d0(%rsp)
movl $0x1, 0xd8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0xf2760
movsd 0x1d0(%rsp), %xmm0
addq $0x1d8, %rsp            # imm = 0x1D8
retq
movq 0xc8(%rsp), %rdi
callq 0x24250
nopw %cs:(%rax,%rax)