// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x218, %rsp            # imm = 0x218
movl %ecx, %r15d
movq %rdx, 0x40(%rsp)
movq %rsi, 0x18(%rsp)
movq %rdi, %r13
movl 0x121757(%rip), %esi    # 0x14b994
movl 0x121755(%rip), %edx    # 0x14b998
leaq 0x70(%rsp), %rdi
callq 0x7cb08
movl 0x121745(%rip), %eax    # 0x14b998
imull 0x12173a(%rip), %eax    # 0x14b994
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
testl %eax, %eax
je 0x2a6af
cvtsi2sd %eax, %xmm0
mulsd 0xafda0(%rip), %xmm0    # 0xda018
cvttsd2si %xmm0, %eax
testl %eax, %eax
je 0x2a6af
movl %eax, 0x3c(%rsp)
movq 0x18(%rsp), %rax
movq (%rax), %rax
movq %rax, 0x48(%rsp)
cmpb $0x1, 0x1265f8(%rip)    # 0x150894
jne 0x2a3d8
testb $0x1, 0x1265e7(%rip)    # 0x150890
jne 0x2a3d8
leaq 0x70(%rsp), %rdi
movsd 0xafdcc(%rip), %xmm0    # 0xda088
callq 0x7ea58
movl 0x130(%rsp), %ecx
movl 0x134(%rsp), %r8d
leaq 0x60(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4170c
leaq 0x140(%rsp), %rdi
leaq 0x60(%rsp), %rdx
movq %r13, %rsi
callq 0x3f166
movl %r15d, 0x38(%rsp)
xorl %eax, %eax
leaq 0x1e8(%rsp), %r13
leaq 0x140(%rsp), %rbp
leaq 0x121dd7(%rip), %r15    # 0x14c0e8
leaq 0x50(%rsp), %rbx
cmpl 0x134(%rsp), %eax
jge 0x2a55b
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x20(%rsp)
movq %rax, 0x58(%rsp)
movslq %eax, %r14
xorl %r12d, %r12d
cmpl 0x130(%rsp), %r12d
jge 0x2a3cc
cvtsi2sd %r12d, %xmm0
movq %r13, %rdi
movq %rbp, %rsi
movsd 0x20(%rsp), %xmm1
callq 0x3fe9c
andq $0x0, 0x50(%rsp)
movsd (%r15), %xmm1
xorpd %xmm0, %xmm0
movupd %xmm0, (%rsp)
xorpd %xmm0, %xmm0
movq 0x48(%rsp), %rdi
movq %r13, %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x3de64
testl %eax, %eax
setne %cl
movq 0x50(%rsp), %rax
testq %rax, %rax
setne %dl
andb %cl, %dl
cmpb $0x1, %dl
jne 0x2a3c4
cvtsi2sdl 0x8(%rax), %xmm0
movq 0x128(%rsp), %rax
movslq 0x138(%rsp), %rcx
imulq %r14, %rcx
addq %r12, %rcx
movsd %xmm0, (%rax,%rcx,8)
incq %r12
jmp 0x2a338
movq 0x58(%rsp), %rax
incl %eax
jmp 0x2a316
movss 0xafd30(%rip), %xmm3    # 0xda110
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
xorpd %xmm2, %xmm2
callq 0x22e10
movl $0x4100, %edi           # imm = 0x4100
callq 0x221e0
movl $0x1700, %edi           # imm = 0x1700
callq 0x22c90
callq 0x22c20
movq %r13, %rdi
xorl %esi, %esi
callq 0x4138a
movl 0x12157a(%rip), %edx    # 0x14b994
movl 0x121578(%rip), %ecx    # 0x14b998
xorl %edi, %edi
xorl %esi, %esi
callq 0x22200
movl $0xb71, %edi            # imm = 0xB71
callq 0x227f0
leaq 0x12c906(%rip), %rdi    # 0x156d40
xorl %esi, %esi
callq 0x442b0
movq 0x48(%rsp), %rdi
callq 0x2a6fd
leaq 0x12c8ee(%rip), %rdi    # 0x156d40
xorl %esi, %esi
callq 0x442b0
movl 0x121535(%rip), %ebp    # 0x14b994
testb $0x3, %bpl
je 0x2a478
pushq $0x1
popq %rsi
movl $0xd05, %edi            # imm = 0xD05
callq 0x223f0
movl 0x12151c(%rip), %ebp    # 0x14b994
movl 0x121519(%rip), %r12d   # 0x14b998
movl %ebp, %eax
imull %r12d, %eax
leal (%rax,%rax,2), %eax
movslq %eax, %rdi
callq 0x22040
movq %rax, %rbx
movq %rax, (%rsp)
xorl %r14d, %r14d
xorl %edi, %edi
xorl %esi, %esi
movl %ebp, %edx
movl %r12d, %ecx
movl $0x1907, %r8d           # imm = 0x1907
movl $0x1401, %r9d           # imm = 0x1401
callq 0x223a0
movl 0x1214de(%rip), %eax    # 0x14b998
movl 0x1214d4(%rip), %ecx    # 0x14b994
testl %ecx, %ecx
cmovlel %r14d, %ecx
testl %eax, %eax
cmovlel %r14d, %eax
movq %rbx, %rdx
cmpl %eax, %r14d
je 0x2a529
movl 0x138(%rsp), %esi
imull %r14d, %esi
movslq %esi, %rsi
shlq $0x3, %rsi
addq 0x128(%rsp), %rsi
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x2a524
movzbl (%rdx), %r8d
shll $0x10, %r8d
movzbl 0x1(%rdx), %r9d
shll $0x8, %r9d
orl %r8d, %r9d
movzbl 0x2(%rdx), %r8d
addq $0x3, %rdx
orl %r9d, %r8d
cvtsi2sd %r8d, %xmm0
movsd %xmm0, (%rsi,%rdi,8)
incq %rdi
jmp 0x2a4f0
incl %r14d
jmp 0x2a4cf
movq %rbx, %rdi
callq 0x22bf0
leaq 0x70(%rsp), %rdi
movsd 0xafb4a(%rip), %xmm1    # 0xda088
xorpd %xmm0, %xmm0
callq 0x80e46
leaq 0x70(%rsp), %rdi
movsd 0xafb2c(%rip), %xmm0    # 0xda080
callq 0x80f0c
jmp 0x2a56d
leaq 0x140(%rsp), %rdi
callq 0x3f1da
movl 0x38(%rsp), %r15d
movq 0x18(%rsp), %rax
movslq 0x14(%rax), %rbx
leaq (,%rbx,4), %rax
testq %rbx, %rbx
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0x22040
movq %rax, %r12
xorl %eax, %eax
testl %ebx, %ebx
movl $0x0, %ecx
cmovgl %ebx, %ecx
cmpq %rax, %rcx
je 0x2a5ab
andl $0x0, (%r12,%rax,4)
incq %rax
jmp 0x2a59c
movl 0x13c(%rsp), %eax
movq 0x128(%rsp), %rcx
xorl %edx, %edx
testl %eax, %eax
cmovlel %edx, %eax
movsd 0xafabf(%rip), %xmm0    # 0xda088
movsd 0xafa37(%rip), %xmm1    # 0xda008
cmpq %rdx, %rax
je 0x2a607
movsd (%rcx,%rdx,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x2a5e3
jnp 0x2a602
addsd %xmm1, %xmm2
cvttsd2si %xmm2, %esi
testl %esi, %esi
sets %dil
cmpl %esi, %ebx
setle %r8b
orb %dil, %r8b
jne 0x2a602
movl %esi, %esi
incl (%r12,%rsi,4)
incq %rdx
jmp 0x2a5d1
xorl %r13d, %r13d
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
xorl %ebp, %ebp
movslq %ebx, %rax
cmpq %rax, %r13
jge 0x2a68a
movq 0x18(%rsp), %rax
movq 0x8(%rax), %rax
movq (%rax,%r13,8), %rbx
testl %r15d, %r15d
je 0x2a63c
movq %rbx, %rdi
callq 0x2a194
testl %eax, %eax
je 0x2a67d
cmpq $0x0, 0x40(%rsp)
je 0x2a655
movq %rbx, %rdi
movq 0x40(%rsp), %rsi
callq 0x3dae4
testl %eax, %eax
je 0x2a67d
movl (%r12,%r13,4), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jle 0x2a67d
cltd
idivl %ecx
movl %eax, %edi
callq 0x2b6d9
movapd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
incl %ebp
incq %r13
movq 0x18(%rsp), %rax
movl 0x14(%rax), %ebx
jmp 0x2a616
cvtsi2sd %ebp, %xmm0
movsd 0x12133a(%rip), %xmm1   # 0x14b9d0
cmpltsd %xmm0, %xmm1
andpd 0x20(%rsp), %xmm1
movapd %xmm1, 0x20(%rsp)
movq %r12, %rdi
callq 0x22bf0
leaq 0x70(%rsp), %rdi
callq 0x7d64c
movaps 0x20(%rsp), %xmm0
addq $0x218, %rsp            # imm = 0x218
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2a6d4
jmp 0x2a6d4
movq %rax, %rbx
jmp 0x2a6eb
jmp 0x2a6db
movq %rax, %rbx
leaq 0x140(%rsp), %rdi
callq 0x3f1da
leaq 0x70(%rsp), %rdi
callq 0x7d64c
movq %rbx, %rdi
callq 0x230b0