// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateNode(gaps::R3SurfelScene*, gaps::R3SurfelNode*, gaps::R3SurfelConstraint const*, gaps::R3SurfelNode*, char const*, int, int)
R3SurfelNode *
CreateNode(R3SurfelScene *scene, 
  R3SurfelNode *source_node, const R3SurfelConstraint *constraint,
  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 source node
  if (!source_node) source_node = tree->RootNode();

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

  // Check if should create copy of surfels for new node
  if (copy_surfels) {
    // Create point set
    R3SurfelPointSet *pointset = CreatePointSet(scene, source_node, constraint);
    if (!pointset) return NULL;
    
    // Create node
    node = CreateNode(scene, pointset, parent_node, node_name,
      copy_surfels, release_copied_blocks);
    
    // Delete point set
    delete pointset;
  }
  else {
    // Get surfel tree
    R3SurfelTree *tree = scene->Tree();
    if (!tree) return NULL;

    // Split leaf nodes
    RNArray<R3SurfelNode *> nodes;
    if (!tree->SplitLeafNodes(source_node, *constraint, &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();
  }

  // Return node
  return node;
}

// Assembly:
subq $0xc8, %rsp
movl 0xd0(%rsp), %eax
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rcx, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
movl %r9d, 0x94(%rsp)
movq $0x0, 0x88(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x80(%rsp)
jne 0x3419b
movq $0x0, 0xc0(%rsp)
jmp 0x3449c
cmpq $0x0, 0xb0(%rsp)
jne 0x341bb
movq 0x80(%rsp), %rdi
callq 0x31030
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xa0(%rsp)
jne 0x341db
movq 0x80(%rsp), %rdi
callq 0x31030
movq %rax, 0xa0(%rsp)
cmpl $0x0, 0x94(%rsp)
je 0x3427e
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
callq 0x32220
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0x34224
movq $0x0, 0xc0(%rsp)
jmp 0x3449c
movq 0xb8(%rsp), %rdi
movq 0x78(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movq 0x98(%rsp), %rcx
movl 0x94(%rsp), %r8d
movl 0xd0(%rsp), %r9d
callq 0x33d70
movq %rax, 0x88(%rsp)
movq 0x78(%rsp), %rax
movq %rax, 0x38(%rsp)
cmpq $0x0, %rax
je 0x34279
movq 0x38(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x3448c
movq 0xb8(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x342a9
movq $0x0, 0xc0(%rsp)
jmp 0x3449c
leaq 0x60(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x1b960
movq 0x28(%rsp), %rcx
movq 0x70(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq (%rdi), %rax
movq 0x30(%rax), %rax
xorl %r8d, %r8d
callq *%rax
movl %eax, 0x34(%rsp)
jmp 0x342e4
movl 0x34(%rsp), %eax
cmpl $0x0, %eax
jne 0x34319
movq $0x0, 0xc0(%rsp)
movl $0x1, 0x50(%rsp)
jmp 0x34468
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
jmp 0x34480
leaq 0x60(%rsp), %rdi
callq 0x31da0
movl %eax, 0x24(%rsp)
jmp 0x34329
movl 0x24(%rsp), %eax
cmpl $0x0, %eax
je 0x3434b
movq $0x0, 0xc0(%rsp)
movl $0x1, 0x50(%rsp)
jmp 0x34468
movl $0xd0, %edi
callq 0x13570
movq %rax, 0x18(%rsp)
jmp 0x3435c
movq 0x18(%rsp), %rdi
movq 0x98(%rsp), %rsi
callq 0x5e770
jmp 0x34370
movq 0x18(%rsp), %rax
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
jne 0x343c3
movq $0x0, 0xc0(%rsp)
movl $0x1, 0x50(%rsp)
jmp 0x34468
movq 0x18(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x58(%rsp)
movl %eax, 0x54(%rsp)
movl $0xd0, %esi
callq 0x13580
jmp 0x34480
movq 0x70(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0xa0(%rsp), %rdx
callq 0x620c0
jmp 0x343df
movl $0x0, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x10(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x16d50
movl %eax, 0x14(%rsp)
jmp 0x343ff
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
cmpl %ecx, %eax
jge 0x34451
movl 0x4c(%rsp), %esi
leaq 0x60(%rsp), %rdi
callq 0x310e0
movq %rax, 0x8(%rsp)
jmp 0x34420
movq 0x8(%rsp), %rax
movq %rax, 0x40(%rsp)
movq 0x40(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
jmp 0x34442
jmp 0x34444
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0x343e7
movq 0x88(%rsp), %rdi
callq 0x606f0
jmp 0x34460
movl $0x0, 0x50(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x1b9a0
movl 0x50(%rsp), %eax
testl %eax, %eax
je 0x3447e
jmp 0x3447c
jmp 0x3449c
jmp 0x3448c
leaq 0x60(%rsp), %rdi
callq 0x1b9a0
jmp 0x344ac
movq 0x88(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
addq $0xc8, %rsp
retq
movq 0x58(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)