// File: /workspace/github_cpp_makefile_star2_O1/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %r9, 0x10(%rsp)
movq %r8, %r12
movq %rcx, %r15
movq %rdx, %rbp
movq %rsi, %r14
movq %rdi, %r13
testq %rdx, %rdx
jne 0x23a31
movq 0x10(%r13), %rax
movq (%rax), %rbp
movl $0xf8, %edi
callq 0x134e0
movq %rax, %rbx
movq %rax, %rdi
movq %r15, %rsi
callq 0x16bb8
movl 0x50(%rsp), %r15d
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
movq %rbp, %rdx
callq *0x18(%rax)
testl %r15d, %r15d
je 0x23a9a
movl 0x58(%rsp), %r9d
movq %r13, %rdi
movq %r14, %rsi
movq %r12, %rdx
movq 0x10(%rsp), %rcx
movl %r15d, %r8d
callq 0x22e2c
movq %rax, %r14
testq %rax, %rax
je 0x23b13
movq %rbx, %rdi
movq %r14, %rsi
callq 0x17e86
jmp 0x23b1c
movq 0x8(%r13), %r15
testq %r15, %r15
je 0x23b40
movq %rsp, %r12
movq %r12, %rdi
callq 0xaaf34
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
movq %r12, %rdx
xorl %ecx, %ecx
callq *0x28(%rax)
movl 0xc(%rsp), %ebp
testl %ebp, %ebp
je 0x23b2b
cmpl $0x0, 0xc(%rsp)
jle 0x23b34
xorl %r15d, %r15d
movq (%rsp), %rax
movq (%rax,%r15,8), %r14
movq %r14, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x37a16
testq %rax, %rax
je 0x23af9
movq %rax, %rdi
movq %r14, %rsi
callq 0x16fc4
movq %rbx, %rdi
movq %r14, %rsi
callq 0x17e86
incq %r15
movslq 0xc(%rsp), %rax
cmpq %rax, %r15
jl 0x23ad5
jmp 0x23b34
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
testq %r14, %r14
je 0x23b40
movq %rbx, %rdi
callq 0x183f8
jmp 0x23b42
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x8(%rax)
movq %rsp, %rdi
callq 0xaaf9e
testl %ebp, %ebp
jne 0x23b21
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x23b68
movq %rax, %r14
movl $0xf8, %esi
movq %rbx, %rdi
callq 0x134f0
jmp 0x23b73
movq %rax, %r14
movq %rsp, %rdi
callq 0xaaf9e
movq %r14, %rdi
callq 0x138b0