// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateObject(gaps::R3SurfelScene*, gaps::R3SurfelNode*, gaps::R3SurfelConstraint const*, gaps::R3SurfelObject*, char const*, gaps::R3SurfelNode*, char const*, int, int)
R3SurfelObject *
CreateObject(R3SurfelScene *scene, 
  R3SurfelNode *source_node, const R3SurfelConstraint *constraint,
  R3SurfelObject *parent_object, const char *object_name, 
  R3SurfelNode *parent_node, const char *node_name, 
  RNBoolean copy_surfels, RNBoolean release_copied_blocks)
{
  // Get tree
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return NULL;

  // Get source node
  if (!source_node) source_node = tree->RootNode();

  // Get parent object
  if (!parent_object) parent_object = scene->RootObject();

  // Create object
  R3SurfelObject *object = new R3SurfelObject(object_name);
  if (!object) return NULL;

  // Insert object into scene
  scene->InsertObject(object, parent_object);

  // Check if should copy surfels
  if (copy_surfels) {
    // Create node
    R3SurfelNode *node = CreateNode(scene, source_node, constraint,
      parent_node, node_name, copy_surfels, release_copied_blocks);
    if (!node) return NULL;
    
    // Insert node into object
    object->InsertNode(node);
    
  }
  else {
    // Split leaf nodes
    RNArray<R3SurfelNode *> nodes;
    tree->SplitLeafNodes(source_node, *constraint, &nodes);
    if (nodes.IsEmpty()) return NULL;

    // Insert nodes satisfying constraint 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 0xe0(%rsp), %eax
movl 0xd8(%rsp), %eax
movq 0xd0(%rsp), %rax
movq %rdi, 0xb8(%rsp)
movq %rsi, 0xb0(%rsp)
movq %rdx, 0xa8(%rsp)
movq %rcx, 0xa0(%rsp)
movq %r8, 0x98(%rsp)
movq %r9, 0x90(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x16ce0
movq %rax, 0x88(%rsp)
cmpq $0x0, 0x88(%rsp)
jne 0x35b8e
movq $0x0, 0xc0(%rsp)
jmp 0x35e4a
cmpq $0x0, 0xb0(%rsp)
jne 0x35bae
movq 0x88(%rsp), %rdi
callq 0x31030
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xa0(%rsp)
jne 0x35bce
movq 0xb8(%rsp), %rdi
callq 0x31050
movq %rax, 0xa0(%rsp)
movl $0xf8, %edi
callq 0x13570
movq %rax, %rdi
movq %rdi, %rax
movq %rax, 0x38(%rsp)
movq 0x98(%rsp), %rsi
callq 0x189f0
jmp 0x35bf2
movq 0x38(%rsp), %rax
movq %rax, 0x80(%rsp)
cmpq $0x0, 0x80(%rsp)
jne 0x35c3d
movq $0x0, 0xc0(%rsp)
jmp 0x35e4a
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
movl $0xf8, %esi
callq 0x13580
jmp 0x35e5a
movq 0xb8(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq 0xa0(%rsp), %rdx
movq (%rdi), %rax
callq *0x18(%rax)
cmpl $0x0, 0xd8(%rsp)
je 0x35cd9
movq 0xb8(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq 0x90(%rsp), %rcx
movq 0xd0(%rsp), %r8
movl 0xd8(%rsp), %r9d
movl 0xe0(%rsp), %eax
movl %eax, (%rsp)
callq 0x34120
movq %rax, 0x68(%rsp)
cmpq $0x0, 0x68(%rsp)
jne 0x35cc2
movq $0x0, 0xc0(%rsp)
jmp 0x35e4a
movq 0x80(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x1ac10
jmp 0x35e2d
leaq 0x58(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x1b960
movq 0x30(%rsp), %rcx
movq 0x88(%rsp), %rdi
movq 0xb0(%rsp), %rsi
movq 0xa8(%rsp), %rdx
movq (%rdi), %rax
movq 0x30(%rax), %rax
xorl %r8d, %r8d
callq *%rax
jmp 0x35d13
leaq 0x58(%rsp), %rdi
callq 0x31da0
movl %eax, 0x2c(%rsp)
jmp 0x35d23
movl 0x2c(%rsp), %eax
cmpl $0x0, %eax
je 0x35d62
movq $0x0, 0xc0(%rsp)
movl $0x1, 0x54(%rsp)
jmp 0x35e15
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x1b9a0
jmp 0x35e5a
movl $0x0, 0x50(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x24(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x16d50
movl %eax, 0x28(%rsp)
jmp 0x35d82
movl 0x24(%rsp), %eax
movl 0x28(%rsp), %ecx
cmpl %ecx, %eax
jge 0x35e0d
movl 0x50(%rsp), %esi
leaq 0x58(%rsp), %rdi
callq 0x310e0
movq %rax, 0x18(%rsp)
jmp 0x35da3
movq 0x18(%rsp), %rax
movq %rax, 0x48(%rsp)
movq 0x48(%rsp), %rdi
xorl %edx, %edx
movl %edx, %esi
callq 0x5f2c0
movq %rax, 0x10(%rsp)
jmp 0x35dc2
movq 0x10(%rsp), %rax
movq %rax, 0x40(%rsp)
cmpq $0x0, 0x40(%rsp)
je 0x35de7
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x191d0
jmp 0x35de5
jmp 0x35de7
movq 0x80(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0x1ac10
jmp 0x35dfb
jmp 0x35dfd
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0x35d6a
movl $0x0, 0x54(%rsp)
leaq 0x58(%rsp), %rdi
callq 0x1b9a0
movl 0x54(%rsp), %eax
testl %eax, %eax
je 0x35e2b
jmp 0x35e29
jmp 0x35e4a
jmp 0x35e2d
movq 0x80(%rsp), %rdi
callq 0x1b880
movq 0x80(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rax
addq $0xc8, %rsp
retq
movq 0x78(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)