// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelUtils.cpp
// Function: gaps::CreateObjects(gaps::R3SurfelScene*, std::vector<int, std::allocator<int>> const&, gaps::R3SurfelObject*, gaps::R3SurfelNode*, int, int)
int
CreateObjects(R3SurfelScene *scene,
  const std::vector<int>& instance_identifiers,
  R3SurfelObject *parent_object, R3SurfelNode *parent_node,
  RNBoolean copy_surfels, RNBoolean release_copied_blocks)
{
  // Get convenient variables
  if (!scene) return 0;
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return 0;
  R3SurfelDatabase *database = tree->Database();
  if (!database) return 0;
  if (!parent_object) parent_object = scene->RootObject();
  if (!parent_object) return 0;
  if (!parent_node) parent_node = tree->RootNode();
  if (!parent_node) return 0;
  if (instance_identifiers.empty()) return 0;

  // Create list of object identifiers
  std::vector<int> object_identifiers;
  std::map<int, int> instance_identifier_map;
  for (unsigned int i = 0; i < instance_identifiers.size(); i++) {
    int instance_identifier = instance_identifiers[i];
    std::map<int, int>::iterator it = instance_identifier_map.find(instance_identifier);
    if (it != instance_identifier_map.end()) continue;
    instance_identifier_map.insert(std::pair<int, int>(instance_identifier, 0));
    object_identifiers.push_back(instance_identifier);
  }

  // Check list of object identifiers
  if (object_identifiers.empty()) return 0;
  
  // Read all blocks
  scene->ReadBlocks();
  
  // Create object for each identifier
  for (unsigned int i = 0; i < object_identifiers.size(); i++) {
    int object_identifier = object_identifiers[i];

    // Create pointset
    R3SurfelPointSet pointset;
    for (int i = 0; i < tree->NNodes(); i++) {
      R3SurfelNode *node = tree->Node(i);
      if (node->NParts() > 0) continue;
      for (int j = 0; j < node->NBlocks(); j++) {
        R3SurfelBlock *block = node->Block(j);
        for (int k = 0; k < block->NSurfels(); k++) {
          const R3Surfel *surfel = block->Surfel(k);

          // Get/check surfel identifier
          int surfel_identifier = surfel->Identifier();
          if (surfel_identifier < 0) continue;
          if (surfel_identifier >= (int) instance_identifiers.size()) continue;

          // Get/check instance identifier
          int instance_identifier = instance_identifiers[surfel_identifier];
          if (instance_identifier < 0) continue;
          if (instance_identifier != object_identifier) continue;

          // Add point to pointset
          R3SurfelPoint point(block, surfel);
          pointset.InsertPoint(point);
        }
      }
    }
    
    // Check pointset
    if (pointset.NPoints() == 0) continue;

    // Create object name
    char object_name[1024];
    sprintf(object_name, "OBJECT#%d", object_identifier);

    // Create object
    R3SurfelObject *object = CreateObject(scene, &pointset,
      parent_object, object_name, parent_node, object_name,
      copy_surfels, release_copied_blocks);
    if (!object) continue;
    
    // Set object identifier
    object->SetIdentifier(object_identifier);

    // Create PCA object property
    R3SurfelObjectProperty *pca = new R3SurfelObjectProperty(R3_SURFEL_OBJECT_PCA_PROPERTY, object);
    scene->InsertObjectProperty(pca);
  }

  // Release all blocks 
  scene->ReleaseBlocks();

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x508, %rsp            # imm = 0x508
xorl %ebp, %ebp
testq %rdi, %rdi
je 0x23e55
movq %rsi, %r12
movq 0x8(%rdi), %rsi
testq %rsi, %rsi
je 0x23e55
cmpq $0x0, 0x10(%rsi)
je 0x23e55
testq %rdx, %rdx
jne 0x23ac6
movq 0x10(%rdi), %rax
movq (%rax), %rdx
testq %rdx, %rdx
je 0x23e55
testq %rcx, %rcx
jne 0x23adb
movq 0x18(%rsi), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x23e55
movq (%r12), %rax
cmpq 0x8(%r12), %rax
je 0x23e55
movq %rsi, 0x38(%rsp)
movq %rdx, 0x40(%rsp)
movq %rcx, 0x48(%rsp)
movq %r8, 0x50(%rsp)
movq %r9, 0x58(%rsp)
movq %rdi, 0x8(%rsp)
xorps %xmm0, %xmm0
leaq 0x10(%rsp), %r14
movaps %xmm0, (%r14)
xorl %ebx, %ebx
movq %rbx, 0x10(%r14)
leaq 0x78(%rsp), %rbp
movl $0x0, (%rbp)
movq %rbx, 0x8(%rbp)
movq %rbp, 0x10(%rbp)
movq %rbp, 0x18(%rbp)
movq %rbx, 0x20(%rbp)
leaq 0x70(%rsp), %r15
leaq 0x100(%rsp), %r13
movl (%rax,%rbx,4), %eax
movl %eax, 0xa0(%rsp)
movq 0x80(%rsp), %rdi
testq %rdi, %rdi
je 0x23b8f
movq %rbp, %r8
movq %r8, %rcx
movq %rdi, %rdx
movl 0x20(%rdi), %esi
xorl %edi, %edi
cmpl %eax, %esi
setl %dil
movq %rdx, %r8
cmovlq %rcx, %r8
movq 0x10(%rdx,%rdi,8), %rdi
testq %rdi, %rdi
jne 0x23b5d
cmpq %rbp, %r8
je 0x23b8f
cmpl %eax, %esi
cmovlq %rcx, %rdx
cmpl 0x20(%rdx), %eax
jge 0x23bdc
movl %eax, 0x100(%rsp)
movl $0x0, 0x104(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x2abb0
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x23bcc
movl 0xa0(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x23bdc
movq %r14, %rdi
leaq 0xa0(%rsp), %rdx
callq 0x2acee
incl %ebx
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rbx, %rcx
ja 0x23b43
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x23e32
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
callq *0x160(%rax)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
movq 0x38(%rsp), %rbx
je 0x23e1d
leaq 0xa0(%rsp), %r14
leaq 0x100(%rsp), %r15
xorl %ecx, %ecx
xorl %r13d, %r13d
movq %r13, 0x30(%rsp)
movl (%rax,%rcx,4), %eax
movl %eax, 0x4(%rsp)
movq %r14, %rdi
callq 0x32620
movl 0x24(%rbx), %eax
testl %eax, %eax
jle 0x23d4c
xorl %edx, %edx
movq 0x18(%rbx), %rcx
movq (%rcx,%rdx,8), %rsi
cmpl $0x0, 0x3c(%rsi)
jg 0x23d3d
movl 0x4c(%rsi), %ecx
testl %ecx, %ecx
jle 0x23d3d
movq %rdx, 0x60(%rsp)
xorl %ebx, %ebx
movq %rsi, 0x68(%rsp)
movq 0x40(%rsi), %rax
movq (%rax,%rbx,8), %rbp
movl 0x8(%rbp), %eax
testl %eax, %eax
jle 0x23d21
xorl %r14d, %r14d
xorl %r15d, %r15d
movq (%rbp), %rdx
movslq 0x20(%rdx,%r14), %rcx
testq %rcx, %rcx
js 0x23d02
movq (%r12), %rsi
movq 0x8(%r12), %rdi
subq %rsi, %rdi
shrq $0x2, %rdi
cmpl %edi, %ecx
jge 0x23d02
movl (%rsi,%rcx,4), %ecx
testl %ecx, %ecx
js 0x23d02
cmpl 0x4(%rsp), %ecx
jne 0x23d02
addq %r14, %rdx
leaq 0x100(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x31fda
leaq 0xa0(%rsp), %rdi
movq %r13, %rsi
callq 0x342e2
movq %r13, %rdi
callq 0x3209a
movl 0x8(%rbp), %eax
incq %r15
movslq %eax, %rcx
addq $0x30, %r14
cmpq %rcx, %r15
jl 0x23ca2
movq 0x68(%rsp), %rsi
movl 0x4c(%rsi), %ecx
leaq 0x100(%rsp), %r15
incq %rbx
movslq %ecx, %rax
cmpq %rax, %rbx
jl 0x23c89
movq 0x38(%rsp), %rbx
movl 0x24(%rbx), %eax
movq 0x60(%rsp), %rdx
incq %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jl 0x23c60
cmpl $0x0, 0xb0(%rsp)
movq 0x30(%rsp), %r13
je 0x23ded
movq %r15, %rdi
leaq 0xb9df1(%rip), %rsi     # 0xddb5a
movl 0x4(%rsp), %edx
xorl %eax, %eax
callq 0x130e0
movq 0x8(%rsp), %rdi
leaq 0xa0(%rsp), %rsi
movq 0x40(%rsp), %rdx
movq %r15, %rcx
movq 0x48(%rsp), %r8
movq %r15, %r9
pushq 0x58(%rsp)
pushq 0x58(%rsp)
callq 0x23791
addq $0x10, %rsp
movq %rax, %rbp
testq %rax, %rax
je 0x23de8
movq (%rbp), %rax
movq %rbp, %rdi
movl 0x4(%rsp), %esi
callq *0x18(%rax)
movl $0x30, %edi
callq 0x134e0
movq %rax, %r13
movq %rax, %rdi
movl $0x1, %esi
movq %rbp, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3ef3c
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq %r13, %rsi
callq *0x40(%rax)
movq 0x30(%rsp), %r13
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
callq 0x3279c
incl %r13d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
movq %r13, %rcx
cmpq %r13, %rdx
ja 0x23c3f
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movl $0x1, %ebp
callq *0x168(%rax)
jmp 0x23e34
xorl %ebp, %ebp
leaq 0x70(%rsp), %rdi
callq 0x2ab60
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x23e55
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x134f0
movl %ebp, %eax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x23e85
movq %rax, %rbx
movl $0x30, %esi
movq %r13, %rdi
callq 0x134f0
jmp 0x23e9f
jmp 0x23e8a
jmp 0x23e85
jmp 0x23e85
jmp 0x23e8a
movq %rax, %rbx
jmp 0x23eac
movq %rax, %rbx
jmp 0x23e9f
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x3209a
leaq 0xa0(%rsp), %rdi
callq 0x3279c
leaq 0x70(%rsp), %rdi
callq 0x2ab60
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x23ecd
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x134f0
movq %rbx, %rdi
callq 0x138b0