// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateObject(gaps::R3SurfelScene*, gaps::R3SurfelPointSet*, gaps::R3SurfelObject*, char const*, gaps::R3SurfelNode*, char const*, int, int)
R3SurfelObject *
CreateObject(R3SurfelScene *scene, 
  R3SurfelPointSet *pointset, 
  R3SurfelObject *parent_object, const char *object_name, 
  R3SurfelNode *parent_node, const char *node_name, 
  RNBoolean copy_surfels, RNBoolean release_copied_blocks)
{
  // Get parent object
  if (!parent_object) parent_object = scene->RootObject();

  // Create object
  R3SurfelObject *object = new R3SurfelObject(object_name);
    
  // Insert object into scene
  scene->InsertObject(object, parent_object);

  // Check if should create copy of surfels for new node
  if (copy_surfels) {
    // Create node
    R3SurfelNode *node = CreateNode(scene, pointset,
      parent_node, node_name, copy_surfels, release_copied_blocks);
    if (!node) { delete object; return NULL; }
    
    // Insert node into object
    object->InsertNode(node);
  }
  else {
    // Get surfel tree
    R3SurfelTree *tree = scene->Tree();
    if (!tree) return NULL;
    
    // Split nodes
    RNArray<R3SurfelNode *> nodes;
    tree->SplitNodes(*pointset, &nodes);
    if (nodes.IsEmpty()) { delete object; return NULL; }
    
    // Move nodes into object
    for (int i = 0; i < nodes.NEntries(); i++) {
      R3SurfelNode *node = nodes.Kth(i);
      R3SurfelObject *old_object = node->Object();
      if (old_object) old_object->RemoveNode(node);
      object->InsertNode(node);
    }
  }

  // Update properties
  object->UpdateProperties();

  // Return object
  return object;
}

// Assembly:
subq $0xc8, %rsp
movl 0xd8(%rsp), %eax
movl 0xd0(%rsp), %eax
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rcx, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
movq %r9, 0x90(%rsp)
cmpq $0x0, 0xa8(%rsp)
jne 0x35835
movq 0xb8(%rsp), %rdi
callq 0x31050
movq %rax, 0xa8(%rsp)
movl $0xf8, %edi
callq 0x13570
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
movq 0xa0(%rsp), %rsi
callq 0x189f0
jmp 0x35859
movq 0x38(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0xb8(%rsp), %rdi
movq 0x88(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq (%rdi), %rax
callq *0x18(%rax)
cmpl $0x0, 0xd0(%rsp)
je 0x3593f
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0x98(%rsp), %rdx
movq 0x90(%rsp), %rcx
movl 0xd0(%rsp), %r8d
movl 0xd8(%rsp), %r9d
callq 0x33d70
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x35928
movq 0x88(%rsp), %rax
movq %rax, 0x30(%rsp)
cmpq $0x0, %rax
je 0x358f2
movq 0x30(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, 0xc0(%rsp)
jmp 0x35aee
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
movl $0xf8, %esi
callq 0x13580
jmp 0x35afe
movq 0x88(%rsp), %rdi
movq 0x70(%rsp), %rsi
callq 0x1ac10
jmp 0x35ad1
movq 0xb8(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x68(%rsp)
cmpq $0x0, 0x68(%rsp)
jne 0x3596a
movq $0x0, 0xc0(%rsp)
jmp 0x35aee
leaq 0x58(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x1b960
movq 0x28(%rsp), %rdx
movq 0x68(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq (%rdi), %rax
movq 0x28(%rax), %rax
xorl %ecx, %ecx
callq *%rax
jmp 0x35998
leaq 0x58(%rsp), %rdi
callq 0x31da0
movl %eax, 0x24(%rsp)
jmp 0x359a8
movl 0x24(%rsp), %eax
cmpl $0x0, %eax
je 0x35a08
movq 0x88(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x359cf
movq 0x18(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movq $0x0, 0xc0(%rsp)
movl $0x1, 0x54(%rsp)
jmp 0x35ab9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x1b9a0
jmp 0x35afe
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x10(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x16d50
movl %eax, 0x14(%rsp)
jmp 0x35a28
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
cmpl %ecx, %eax
jge 0x35ab1
movl 0x50(%rsp), %esi
leaq 0x58(%rsp), %rdi
callq 0x310e0
movq %rax, 0x8(%rsp)
jmp 0x35a49
movq 0x8(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x5f2c0
movq %rax, (%rsp)
jmp 0x35a67
movq (%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
je 0x35a8b
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x191d0
jmp 0x35a89
jmp 0x35a8b
movq 0x88(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x1ac10
jmp 0x35a9f
jmp 0x35aa1
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x35a10
movl $0x0, 0x54(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x1b9a0
movl 0x54(%rsp), %eax
testl %eax, %eax
je 0x35acf
jmp 0x35acd
jmp 0x35aee
jmp 0x35ad1
movq 0x88(%rsp), %rdi
callq 0x1b880
movq 0x88(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
addq $0xc8, %rsp
retq
movq 0x80(%rsp), %rdi
callq 0x13970
nopl (%rax,%rax)