// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelTree.cpp
// Function: gaps::R3SurfelTree::SplitBlocks(gaps::R3SurfelNode*, gaps::R3SurfelPointSet&, gaps::RNArray<gaps::R3SurfelBlock*>*, gaps::RNArray<gaps::R3SurfelBlock*>*)
int R3SurfelTree::
SplitBlocks(R3SurfelNode *node, R3SurfelPointSet& pointset,
  RNArray<R3SurfelBlock *> *blocksA, RNArray<R3SurfelBlock *> *blocksB)
{
  // Just checking
  assert(node);
  assert(node->Tree() == this);
  assert(node->tree_index >= 0);
  assert(nodes.Kth(node->tree_index) == node);
  int status = 0;
  
  // Create array of blocks containing points in point set
  RNArray<R3SurfelBlock *> *pointset_blocks = pointset.Blocks();;
  if (!pointset_blocks) return 0;

  // Create constraint to include only marked points
  const R3SurfelMarkConstraint constraint(TRUE, FALSE);

  // Split blocks 
  for (int i = 0; i < pointset_blocks->NEntries(); i++) {
    R3SurfelBlock *block = pointset_blocks->Kth(i);
    if (block->Node() != node) continue;
    block->SetMarks(FALSE);
    pointset.SetMarks(TRUE);
    R3SurfelBlock *blockA, *blockB;
    if (SplitBlock(node, block, constraint, &blockA, &blockB)) status = 1;
    if (blocksA && blockA) blocksA->Insert(blockA);
    if (blocksB && blockB) blocksB->Insert(blockB);
  }

  // Delete array of blocks containing points in point set
  delete pointset_blocks;

  // Return whether any blocks were split
  return status;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movq %r8, 0x88(%rsp)
movq 0xa8(%rsp), %rax
movq %rax, 0x30(%rsp)
movl $0x0, 0x84(%rsp)
movq 0x98(%rsp), %rdi
callq 0x59a70
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x65571
movl $0x0, 0xb4(%rsp)
jmp 0x65725
leaq 0x68(%rsp), %rdi
movl $0x1, %esi
xorl %edx, %edx
callq 0x53e40
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x28(%rsp)
movq 0x78(%rsp), %rdi
callq 0x16d50
movl %eax, 0x2c(%rsp)
jmp 0x655a2
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x656e4
movq 0x78(%rsp), %rdi
movl 0x64(%rsp), %esi
callq 0x436f0
movq %rax, 0x20(%rsp)
jmp 0x655c7
movq 0x20(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
callq 0x43710
movq %rax, 0x18(%rsp)
jmp 0x655e2
movq 0x18(%rsp), %rax
cmpq 0xa0(%rsp), %rax
je 0x65613
jmp 0x656d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x43a40
jmp 0x65734
movq 0x48(%rsp), %rdi
xorl %esi, %esi
callq 0x4bcd0
jmp 0x65621
movq 0x98(%rsp), %rdi
movq (%rdi), %rax
movq 0xa8(%rax), %rax
movl $0x1, %esi
callq *%rax
jmp 0x6563c
movq 0x30(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq 0x48(%rsp), %rdx
movq (%rdi), %rax
movq 0x50(%rax), %rax
leaq 0x68(%rsp), %rcx
leaq 0x40(%rsp), %r8
leaq 0x38(%rsp), %r9
callq *%rax
movl %eax, 0x14(%rsp)
jmp 0x6566c
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
je 0x65680
movl $0x1, 0x84(%rsp)
cmpq $0x0, 0x90(%rsp)
je 0x656a9
cmpq $0x0, 0x40(%rsp)
je 0x656a9
movq 0x90(%rsp), %rdi
movq 0x40(%rsp), %rsi
callq 0x436c0
jmp 0x656a7
jmp 0x656a9
cmpq $0x0, 0x88(%rsp)
je 0x656d2
cmpq $0x0, 0x38(%rsp)
je 0x656d2
movq 0x88(%rsp), %rdi
movq 0x38(%rsp), %rsi
callq 0x436c0
jmp 0x656d0
jmp 0x656d2
jmp 0x656d4
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x6558a
movq 0x78(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x6570d
movq 0x8(%rsp), %rdi
callq 0x43730
movq 0x8(%rsp), %rdi
movl $0x10, %esi
callq 0x13580
movl 0x84(%rsp), %eax
movl %eax, 0xb4(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x43a40
movl 0xb4(%rsp), %eax
addq $0xb8, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0x13970
nop