// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rcx, %r13
movq %rdx, 0x10(%rsp)
movq %rdi, 0x18(%rsp)
movq %rsi, 0x50(%rsp)
movq %rsi, %rdi
callq 0x34c8e
testq %rax, %rax
je 0x3a886
movq %rax, %r15
cmpl $0x0, 0xc(%rax)
jle 0x3a88a
xorl %ebx, %ebx
leaq 0x20(%rsp), %r14
leaq 0x30(%rsp), %r12
movl $0x0, 0xc(%rsp)
movq %r15, 0x48(%rsp)
movq %r13, 0x40(%rsp)
movq (%r15), %rax
movq (%rax,%rbx,8), %rbp
movq %r14, %rdi
callq 0xaaf34
movq %r12, %rdi
callq 0xaaf34
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
movq 0x50(%rsp), %rdx
movq %r14, %rcx
movq %r12, %r8
callq *0x48(%rax)
testl %eax, %eax
je 0x3a831
movl $0xd0, %edi
callq 0x134e0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
callq 0x37448
movl $0xd0, %edi
callq 0x134e0
movq %rax, %r14
movq %rax, %rdi
xorl %esi, %esi
callq 0x37448
cmpl $0x0, 0x2c(%rsp)
jle 0x3a798
xorl %r13d, %r13d
movq 0x20(%rsp), %rax
movq (%rax,%r13,8), %r15
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x30(%rax)
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x28(%rax)
incq %r13
movslq 0x2c(%rsp), %rax
cmpq %rax, %r13
jl 0x3a768
cmpl $0x0, 0x3c(%rsp)
jle 0x3a7d1
xorl %r13d, %r13d
movq 0x30(%rsp), %rax
movq (%rax,%r13,8), %r15
movq (%rbp), %rax
movq %rbp, %rdi
movq %r15, %rsi
callq *0x30(%rax)
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x28(%rax)
incq %r13
movslq 0x3c(%rsp), %rax
cmpq %rax, %r13
jl 0x3a7a2
movq 0x18(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
callq 0x39272
movq 0x48(%rsp), %r15
movq 0x40(%rsp), %r13
movq 0x18(%rsp), %rdi
movq %r14, %rsi
movq %rbp, %rdx
callq 0x39272
cmpq $0x0, 0x10(%rsp)
je 0x3a813
movq 0x10(%rsp), %rdi
movl 0xc(%rdi), %edx
movq %r12, %rsi
callq 0xaafca
movl $0x1, 0xc(%rsp)
testq %r13, %r13
je 0x3a85a
movl 0xc(%r13), %edx
movq %r13, %rdi
movq %r14, %rsi
callq 0xaafca
jmp 0x3a85a
cmpl $0x0, 0x2c(%rsp)
jle 0x3a847
movq 0x10(%rsp), %rax
movq %rax, %rdi
testq %rax, %rax
jne 0x3a84f
jmp 0x3a85a
movq %r13, %rdi
testq %r13, %r13
je 0x3a85a
movl 0xc(%rdi), %edx
movq %rbp, %rsi
callq 0xaafca
leaq 0x30(%rsp), %r12
movq %r12, %rdi
callq 0xaaf9e
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0xaaf9e
incq %rbx
movslq 0xc(%r15), %rax
cmpq %rax, %rbx
jl 0x3a6f8
jmp 0x3a892
xorl %eax, %eax
jmp 0x3a8ab
movl $0x0, 0xc(%rsp)
movq %r15, %rdi
callq 0xaaf9e
movl $0x10, %esi
movq %r15, %rdi
callq 0x134f0
movl 0xc(%rsp), %eax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xd0, %esi
movq %r12, %rdi
jmp 0x3a8d2
movq %rax, %rbx
movl $0xd0, %esi
movq %r14, %rdi
callq 0x134f0
jmp 0x3a8e9
jmp 0x3a8e6
movq %rax, %rbx
jmp 0x3a8f3
jmp 0x3a8e6
jmp 0x3a8e6
jmp 0x3a8e6
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0xaaf9e
leaq 0x20(%rsp), %rdi
callq 0xaaf9e
movq %rbx, %rdi
callq 0x138b0
nop