// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CullSurfels(gaps::R3SurfelScene*, gaps::R3SurfelConstraint const*)
int
CullSurfels(R3SurfelScene *scene, const R3SurfelConstraint *constraint)
{
  // Get surfel tree
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return 0;
  R3SurfelDatabase *database = tree->Database();
  if (!database) return 0;
  
  // Find all blocks
  RNArray<R3SurfelBlock *> blocks;
  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);
      blocks.Insert(block);
    }
  }

  // Cull every block by constraint
  for (int i = 0; i < blocks.NEntries(); i++) {
    R3SurfelBlock *block = blocks.Kth(i);
    R3SurfelNode *node = block->Node();
    R3SurfelBlock *blockA = NULL, *blockB = NULL;
    tree->SplitBlock(node, block, *constraint, &blockA, &blockB);
    if (blockA && (blockA->NSurfels() == 0)) delete blockA;
    if (blockB) delete blockB;
  }

  // Remove empty nodes
  if (!RemoveEmptyNodes(scene)) return 0;

  // Remove empty objects
  if (!RemoveEmptyObjects(scene)) return 0;

  // Return success
  return 1;
}

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xe0(%rsp)
movq 0xe8(%rsp), %rdi
callq 0x16ce0
movq %rax, 0xd8(%rsp)
cmpq $0x0, 0xd8(%rsp)
jne 0x352c7
movl $0x0, 0xf4(%rsp)
jmp 0x35608
movq 0xd8(%rsp), %rdi
callq 0x16d10
movq %rax, 0xd0(%rsp)
cmpq $0x0, 0xd0(%rsp)
jne 0x352f7
movl $0x0, 0xf4(%rsp)
jmp 0x35608
leaq 0xc0(%rsp), %rdi
callq 0x436b0
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0xd8(%rsp), %rdi
callq 0x16cf0
movl %eax, 0x60(%rsp)
jmp 0x3532d
movl 0x5c(%rsp), %eax
movl 0x60(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3542c
movq 0xd8(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x30a90
movq %rax, 0x50(%rsp)
jmp 0x35358
movq 0x50(%rsp), %rax
movq %rax, 0xa0(%rsp)
movl $0x0, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0xa0(%rsp), %rdi
callq 0x186b0
movl %eax, 0x4c(%rsp)
jmp 0x3538e
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x35414
movq 0xa0(%rsp), %rdi
movl 0x9c(%rsp), %esi
callq 0x186d0
movq %rax, 0x40(%rsp)
jmp 0x353b5
movq 0x40(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0x436c0
jmp 0x353d9
jmp 0x353db
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x35370
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x43730
jmp 0x35617
jmp 0x35416
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x3530f
movl $0x0, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x38(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x16d50
movl %eax, 0x3c(%rsp)
jmp 0x35455
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x35586
movl 0x8c(%rsp), %esi
leaq 0xc0(%rsp), %rdi
callq 0x436f0
movq %rax, 0x30(%rsp)
jmp 0x35480
movq 0x30(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdi
callq 0x43710
movq %rax, 0x28(%rsp)
jmp 0x354a1
movq 0x28(%rsp), %rax
movq %rax, 0x78(%rsp)
movq $0x0, 0x70(%rsp)
movq $0x0, 0x68(%rsp)
movq 0xd8(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0x80(%rsp), %rdx
movq 0xe0(%rsp), %rcx
movq (%rdi), %rax
movq 0x50(%rax), %rax
leaq 0x70(%rsp), %r8
leaq 0x68(%rsp), %r9
callq *%rax
jmp 0x354ef
cmpq $0x0, 0x70(%rsp)
je 0x3553b
movq 0x70(%rsp), %rdi
callq 0x1c0e0
movl %eax, 0x24(%rsp)
jmp 0x35507
movl 0x24(%rsp), %eax
cmpl $0x0, %eax
jne 0x3553b
movq 0x70(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x35539
movq 0x18(%rsp), %rdi
callq 0x4a1c0
movq 0x18(%rsp), %rdi
movl $0xc8, %esi
callq 0x13580
jmp 0x3553b
cmpq $0x0, 0x68(%rsp)
je 0x3556e
movq 0x68(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x3556c
movq 0x10(%rsp), %rdi
callq 0x4a1c0
movq 0x10(%rsp), %rdi
movl $0xc8, %esi
callq 0x13580
jmp 0x3556e
jmp 0x35570
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x35437
movq 0xe8(%rsp), %rdi
callq 0x34940
movl %eax, 0xc(%rsp)
jmp 0x35599
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
jne 0x355b7
movl $0x0, 0xf4(%rsp)
movl $0x1, 0x64(%rsp)
jmp 0x355fb
movq 0xe8(%rsp), %rdi
callq 0x344c0
movl %eax, 0x8(%rsp)
jmp 0x355ca
movl 0x8(%rsp), %eax
cmpl $0x0, %eax
jne 0x355e8
movl $0x0, 0xf4(%rsp)
movl $0x1, 0x64(%rsp)
jmp 0x355fb
movl $0x1, 0xf4(%rsp)
movl $0x1, 0x64(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x43730
movl 0xf4(%rsp), %eax
addq $0xf8, %rsp
retq
movq 0xb0(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)