// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::EstimateSurfelColors(gaps::R3SurfelScene*)
int
EstimateSurfelColors(R3SurfelScene *scene)
{
  // Get surfel tree
  R3SurfelTree *tree = scene->Tree();
  if (!tree) {
    RNFail("Scene has no surfel tree\n");
    return 0;
  }    

  // Get surfel database
  R3SurfelDatabase *database = tree->Database();
  if (!database) {
    RNFail("Scene has no surfel database\n");
    return 0;
  }

  // Estimate color very every surfel
  for (int i = 0; i < tree->NNodes(); i++) {
    R3SurfelNode *node = tree->Node(i);
    for (int j = 0; j < node->NBlocks(); j++) {
      R3SurfelBlock *block = node->Block(j);

      // Read block
      if (!database->ReadBlock(block)) continue;

      // Estimate color
      for (int k = 0; k < block->NSurfels(); k++) {
        R3Point position = block->SurfelPosition(k);
        R3Vector normal = block->SurfelNormal(k);
        RNScalar timestamp = block->SurfelTimestamp(k);
        RNRgb color = EstimateColor(scene, position, normal, timestamp);
        block->SetSurfelColor(k, color);
      }

      // Release block
      database->ReleaseBlock(block);
    }
  }

  // Return success
  return 1;
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq 0x98(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x90(%rsp)
cmpq $0x0, 0x90(%rsp)
jne 0x393ad
leaq 0x16b79a(%rip), %rdi    # 0x1a4b30
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0xa4(%rsp)
jmp 0x39574
movq 0x90(%rsp), %rdi
callq 0x16d10
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
jne 0x393eb
leaq 0x16b893(%rip), %rdi    # 0x1a4c67
movb $0x0, %al
callq 0x1464b0
movl $0x0, 0xa4(%rsp)
jmp 0x39574
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0xc(%rsp)
movq 0x90(%rsp), %rdi
callq 0x16cf0
movl %eax, %ecx
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
jge 0x39569
movq 0x90(%rsp), %rdi
movl 0x84(%rsp), %esi
callq 0x30a90
movq %rax, 0x78(%rsp)
movl $0x0, 0x74(%rsp)
movl 0x74(%rsp), %eax
movl %eax, 0x8(%rsp)
movq 0x78(%rsp), %rdi
callq 0x186b0
movl %eax, %ecx
movl 0x8(%rsp), %eax
cmpl %ecx, %eax
jge 0x39551
movq 0x78(%rsp), %rdi
movl 0x74(%rsp), %esi
callq 0x186d0
movq %rax, 0x68(%rsp)
movq 0x88(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x31140
cmpl $0x0, %eax
jne 0x3948c
jmp 0x39541
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x4(%rsp)
movq 0x68(%rsp), %rdi
callq 0x1c0e0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x3952f
movq 0x68(%rsp), %rsi
movl 0x64(%rsp), %edx
leaq 0x48(%rsp), %rdi
callq 0x316c0
movq 0x68(%rsp), %rsi
movl 0x64(%rsp), %edx
leaq 0x30(%rsp), %rdi
callq 0x31800
movq 0x68(%rsp), %rdi
movl 0x64(%rsp), %esi
callq 0x43dc0
movsd %xmm0, 0x28(%rsp)
movq 0x98(%rsp), %rsi
movsd 0x28(%rsp), %xmm0
leaq 0x10(%rsp), %rdi
leaq 0x48(%rsp), %rdx
leaq 0x30(%rsp), %rcx
callq 0x38e00
movq 0x68(%rsp), %rdi
movl 0x64(%rsp), %esi
leaq 0x10(%rsp), %rdx
callq 0x4b800
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x39494
movq 0x88(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x311c0
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0x3943d
jmp 0x39553
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x393f6
movl $0x1, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
addq $0xa8, %rsp
retq
nopw %cs:(%rax,%rax)