// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateNode(gaps::R3SurfelScene*, gaps::R3SurfelPointSet*, gaps::R3SurfelNode*, char const*, int, int)
R3SurfelNode *
CreateNode(R3SurfelScene *scene, R3SurfelPointSet *pointset, 
  R3SurfelNode *parent_node, const char *node_name, 
  RNBoolean copy_surfels, RNBoolean release_copied_blocks)
{
  // Initialize result
  R3SurfelNode *node = NULL;

  // Get tree
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return NULL;

  // Get parent node
  if (!parent_node) parent_node = tree->RootNode();

  // Check if should create copy of surfels for new node
  if (copy_surfels) {
    // Create block
    R3SurfelBlock *block = CreateBlock(scene, pointset);
    if (!block) return NULL;

    // Create node
    node = new R3SurfelNode(node_name);

    // Insert block
    node->InsertBlock(block);

    // Update properties
    node->UpdateProperties();

    // Insert node
    tree->InsertNode(node, parent_node);

    // Release block
    if (release_copied_blocks) tree->Database()->ReleaseBlock(block);
  }
  else {
#if 0
    // Mark pointset
    pointset->SetMarks(TRUE);

    // Create constraint
    R3SurfelMarkConstraint constraint(TRUE, FALSE);
    
    // Split nodes based on constraint
    node = CreateNode(scene, tree->RootNode(), &constraint, parent_node, node_name, FALSE);

    // Unmark pointset
    pointset->SetMarks(FALSE);
    for (int i = 0; i < node->NParts(); i++) {
      R3SurfelNode *part = node->Part(i);
      part->SetMarks(FALSE);
    }
#else
    // Get surfel tree
    R3SurfelTree *tree = scene->Tree();
    if (!tree) return NULL;

    // Split leaf nodes
    RNArray<R3SurfelNode *> nodes;
    if (!tree->SplitNodes(*pointset, &nodes)) return NULL;
    if (nodes.IsEmpty()) return NULL;

    // Create node
    node = new R3SurfelNode(node_name);
    if (!node) return NULL;
    
    // Insert node into tree
    tree->InsertNode(node, parent_node);

    // Move nodes satisfying constraint into node
    for (int i = 0; i < nodes.NEntries(); i++) {
      R3SurfelNode *child = nodes.Kth(i);
      child->SetParent(node);
    }
    
    // Update properties
    node->UpdateProperties();
#endif
  }

  // Return node
  return node;
}

// Assembly:
subq $0xb8, %rsp
movq %rdi, 0xa8(%rsp)
movq %rsi, 0xa0(%rsp)
movq %rdx, 0x98(%rsp)
movq %rcx, 0x90(%rsp)
movl %r8d, 0x8c(%rsp)
movl %r9d, 0x88(%rsp)
movq $0x0, 0x80(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x33dde
movq $0x0, 0xb0(%rsp)
jmp 0x34106
cmpq $0x0, 0x98(%rsp)
jne 0x33dfb
movq 0x78(%rsp), %rdi
callq 0x31030
movq %rax, 0x98(%rsp)
cmpl $0x0, 0x8c(%rsp)
je 0x33ef3
movq 0xa8(%rsp), %rdi
movq 0xa0(%rsp), %rsi
xorl %edx, %edx
callq 0x33be0
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x33e3e
movq $0x0, 0xb0(%rsp)
jmp 0x34106
movl $0xd0, %edi
callq 0x13570
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x30(%rsp)
movq 0x90(%rsp), %rsi
callq 0x5e770
jmp 0x33e62
movq 0x30(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq (%rdi), %rax
callq *0x28(%rax)
movq 0x80(%rsp), %rdi
callq 0x606f0
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x620c0
cmpl $0x0, 0x88(%rsp)
je 0x33eee
movq 0x78(%rsp), %rdi
callq 0x16d10
movq %rax, %rdi
movq 0x70(%rsp), %rsi
callq 0x311c0
jmp 0x33eee
movq 0x30(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
movl $0xd0, %esi
callq 0x13580
jmp 0x34116
jmp 0x340f6
movq 0xa8(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x58(%rsp)
cmpq $0x0, 0x58(%rsp)
jne 0x33f1e
movq $0x0, 0xb0(%rsp)
jmp 0x34106
leaq 0x48(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x1b960
movq 0x20(%rsp), %rdx
movq 0x58(%rsp), %rdi
movq 0xa0(%rsp), %rsi
movq (%rdi), %rax
movq 0x28(%rax), %rax
xorl %ecx, %ecx
callq *%rax
movl %eax, 0x2c(%rsp)
jmp 0x33f50
movl 0x2c(%rsp), %eax
cmpl $0x0, %eax
jne 0x33f85
movq $0x0, 0xb0(%rsp)
movl $0x1, 0x44(%rsp)
jmp 0x340d2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
jmp 0x340ea
leaq 0x48(%rsp), %rdi
callq 0x31da0
movl %eax, 0x1c(%rsp)
jmp 0x33f95
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
je 0x33fb7
movq $0x0, 0xb0(%rsp)
movl $0x1, 0x44(%rsp)
jmp 0x340d2
movl $0xd0, %edi
callq 0x13570
movq %rax, 0x10(%rsp)
jmp 0x33fc8
movq 0x10(%rsp), %rdi
movq 0x90(%rsp), %rsi
callq 0x5e770
jmp 0x33fdc
movq 0x10(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x80(%rsp)
jne 0x3402f
movq $0x0, 0xb0(%rsp)
movl $0x1, 0x44(%rsp)
jmp 0x340d2
movq 0x10(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x68(%rsp)
movl %eax, 0x64(%rsp)
movl $0xd0, %esi
callq 0x13580
jmp 0x340ea
movq 0x58(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x620c0
jmp 0x3404b
movl $0x0, 0x40(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x8(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x16d50
movl %eax, 0xc(%rsp)
jmp 0x3406b
movl 0x8(%rsp), %eax
movl 0xc(%rsp), %ecx
cmpl %ecx, %eax
jge 0x340bb
movl 0x40(%rsp), %esi
leaq 0x48(%rsp), %rdi
callq 0x310e0
movq %rax, (%rsp)
jmp 0x3408b
movq (%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
jmp 0x340ac
jmp 0x340ae
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x34053
movq 0x80(%rsp), %rdi
callq 0x606f0
jmp 0x340ca
movl $0x0, 0x44(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x1b9a0
movl 0x44(%rsp), %eax
testl %eax, %eax
je 0x340e8
jmp 0x340e6
jmp 0x34106
jmp 0x340f6
leaq 0x48(%rsp), %rdi
callq 0x1b9a0
jmp 0x34116
movq 0x80(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
addq $0xb8, %rsp
retq
movq 0x68(%rsp), %rdi
callq 0x13970