// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelTree.cpp
// Function: gaps::R3SurfelTree::SplitNodes(gaps::R3SurfelPointSet&, gaps::RNArray<gaps::R3SurfelNode*>*, gaps::RNArray<gaps::R3SurfelNode*>*)
int R3SurfelTree::
SplitNodes(R3SurfelPointSet& pointset,
  RNArray<R3SurfelNode *> *nodesA, RNArray<R3SurfelNode *> *nodesB)
{
  // Initialize return value (whether any node was split)
  int status = 0;

  // Create array of nodes containing points in point set
  RNArray<R3SurfelNode *> *pointset_nodes = pointset.Nodes();
  if (!pointset_nodes) return 0;

  // Split each node
  for (int i = 0; i < pointset_nodes->NEntries(); i++) {
    R3SurfelNode *node = pointset_nodes->Kth(i);

    // Split blocks in leaf node
    RNArray<R3SurfelBlock *> blocksA, blocksB;
    if (SplitBlocks(node, pointset, &blocksA, &blocksB)) {
      // Create new nodes
      R3SurfelNode *nodeA = new R3SurfelNode();
      R3SurfelNode *nodeB = new R3SurfelNode();
      
      // Move blocks from node to nodeA
      for (int i = 0; i < blocksA.NEntries(); i++) {
        R3SurfelBlock *block = blocksA.Kth(i);
        node->RemoveBlock(block);
        nodeA->InsertBlock(block);
      }
      
      // Move blocks from node to nodeB
      for (int i = 0; i < blocksB.NEntries(); i++) {
        R3SurfelBlock *block = blocksB.Kth(i);
        node->RemoveBlock(block);
        nodeB->InsertBlock(block);
      }
      
      // Insert new nodes into tree
      InsertNode(nodeA, node);
      InsertNode(nodeB, node);
      
      // Update return values
      if (nodesA) nodesA->Insert(nodeA);
      if (nodesB) nodesB->Insert(nodeB);
      status = 1;
    }
    else {
      if (blocksA.NEntries() > 0) {
        // Update return value
        if (nodesA) nodesA->Insert(node);
      }
      else {
        // Update return value
        if (nodesB) nodesB->Insert(node);
      }
    }
  }

  // Delete array of nodes containing points in point set
  delete pointset_nodes;

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

// Assembly:
subq $0xf8, %rsp
movq %rdi, 0xe8(%rsp)
movq %rsi, 0xe0(%rsp)
movq %rdx, 0xd8(%rsp)
movq %rcx, 0xd0(%rsp)
movq 0xe8(%rsp), %rax
movq %rax, 0x48(%rsp)
movl $0x0, 0xcc(%rsp)
movq 0xe0(%rsp), %rdi
callq 0x59c10
movq %rax, 0xc0(%rsp)
cmpq $0x0, 0xc0(%rsp)
jne 0x64a8f
movl $0x0, 0xf4(%rsp)
jmp 0x64ea3
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x44(%rsp)
movq 0xc0(%rsp), %rdi
callq 0x16d50
movl %eax, %ecx
movl 0x44(%rsp), %eax
cmpl %ecx, %eax
jge 0x64e6c
movq 0xc0(%rsp), %rdi
movl 0xbc(%rsp), %esi
callq 0x310e0
movq %rax, 0xb0(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x436b0
leaq 0x90(%rsp), %rdi
callq 0x436b0
jmp 0x64af8
movq 0x48(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xe0(%rsp), %rdx
movq (%rdi), %rax
movq 0x48(%rax), %rax
leaq 0xa0(%rsp), %rcx
leaq 0x90(%rsp), %r8
callq *%rax
movl %eax, 0x40(%rsp)
jmp 0x64b2c
movl 0x40(%rsp), %eax
cmpl $0x0, %eax
je 0x64db6
movl $0xd0, %edi
callq 0x13570
movq %rax, 0x38(%rsp)
jmp 0x64b4a
movq 0x38(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5e770
jmp 0x64b5a
movq 0x38(%rsp), %rax
movq %rax, 0x78(%rsp)
movl $0xd0, %edi
callq 0x13570
movq %rax, 0x30(%rsp)
jmp 0x64b75
movq 0x30(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5e770
jmp 0x64b85
movq 0x30(%rsp), %rax
movq %rax, 0x70(%rsp)
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
movl %eax, 0x28(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x16d50
movl %eax, 0x2c(%rsp)
jmp 0x64bb2
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x64ca5
movl 0x6c(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x436f0
movq %rax, 0x20(%rsp)
jmp 0x64bda
movq 0x20(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0xb0(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq (%rdi), %rax
movq 0x30(%rax), %rax
callq *%rax
jmp 0x64bfc
movq 0x78(%rsp), %rdi
movq 0x60(%rsp), %rsi
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
jmp 0x64c11
jmp 0x64c13
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x64b97
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
jmp 0x64e5d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
jmp 0x64e50
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
movl $0xd0, %esi
callq 0x13580
jmp 0x64e50
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
movl $0xd0, %esi
callq 0x13580
jmp 0x64e50
movl $0x0, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x18(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x16d50
movl %eax, 0x1c(%rsp)
jmp 0x64cc8
movl 0x18(%rsp), %eax
movl 0x1c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x64d35
movl 0x5c(%rsp), %esi
leaq 0x90(%rsp), %rdi
callq 0x436f0
movq %rax, 0x10(%rsp)
jmp 0x64cec
movq 0x10(%rsp), %rax
movq %rax, 0x50(%rsp)
movq 0xb0(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq (%rdi), %rax
movq 0x30(%rax), %rax
callq *%rax
jmp 0x64d0e
movq 0x70(%rsp), %rdi
movq 0x50(%rsp), %rsi
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
jmp 0x64d23
jmp 0x64d25
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x64cad
movq 0x48(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0xb0(%rsp), %rdx
callq 0x620c0
jmp 0x64d4e
movq 0x48(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0xb0(%rsp), %rdx
callq 0x620c0
jmp 0x64d67
cmpq $0x0, 0xd8(%rsp)
je 0x64d88
movq 0xd8(%rsp), %rdi
movq 0x78(%rsp), %rsi
callq 0x1bee0
jmp 0x64d86
jmp 0x64d88
cmpq $0x0, 0xd0(%rsp)
je 0x64da9
movq 0xd0(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1bee0
jmp 0x64da7
jmp 0x64da9
movl $0x1, 0xcc(%rsp)
jmp 0x64e20
leaq 0xa0(%rsp), %rdi
callq 0x16d50
movl %eax, 0xc(%rsp)
jmp 0x64dc9
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
jle 0x64df8
cmpq $0x0, 0xd8(%rsp)
je 0x64df6
movq 0xd8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1bee0
jmp 0x64df4
jmp 0x64df6
jmp 0x64e1e
cmpq $0x0, 0xd0(%rsp)
je 0x64e1c
movq 0xd0(%rsp), %rdi
movq 0xb0(%rsp), %rsi
callq 0x1bee0
jmp 0x64e1a
jmp 0x64e1c
jmp 0x64e1e
jmp 0x64e20
leaq 0x90(%rsp), %rdi
callq 0x43730
leaq 0xa0(%rsp), %rdi
callq 0x43730
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x64a9a
leaq 0x90(%rsp), %rdi
callq 0x43730
leaq 0xa0(%rsp), %rdi
callq 0x43730
jmp 0x64eb2
movq 0xc0(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x64e95
movq (%rsp), %rdi
callq 0x1b9a0
movq (%rsp), %rdi
movl $0x10, %esi
callq 0x13580
movl 0xcc(%rsp), %eax
movl %eax, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
addq $0xf8, %rsp
retq
movq 0x88(%rsp), %rdi
callq 0x13970
nop