// File: /workspace/github_cpp_makefile_star2_O1/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 %r14d, %r14d
testq %rdi, %rdi
je 0x240d9
movq %rsi, %r12
movq %rdi, %rbx
movq 0x8(%rdi), %rsi
testq %rsi, %rsi
je 0x240d9
cmpq $0x0, 0x10(%rsi)
je 0x240d9
testq %rdx, %rdx
jne 0x23d4d
movq 0x10(%rbx), %rax
movq (%rax), %rdx
testq %rdx, %rdx
je 0x240d9
testq %rcx, %rcx
jne 0x23d62
movq 0x18(%rsi), %rax
movq (%rax), %rcx
testq %rcx, %rcx
je 0x240d9
movq (%r12), %rax
cmpq 0x8(%r12), %rax
je 0x240d9
movq %rsi, 0x30(%rsp)
movq %rdx, 0x38(%rsp)
movq %rcx, 0x40(%rsp)
movq %r8, 0x48(%rsp)
movq %r9, 0x50(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
xorl %ebp, %ebp
movq %rbp, 0x20(%rsp)
leaq 0x78(%rsp), %r13
movl $0x0, (%r13)
movq %rbp, 0x8(%r13)
movq %r13, 0x10(%r13)
movq %r13, 0x18(%r13)
movq %rbp, 0x20(%r13)
movq (%r12), %rax
cmpq %rax, 0x8(%r12)
je 0x23e90
leaq 0x70(%rsp), %r14
leaq 0x100(%rsp), %r15
movl (%rax,%rbp,4), %eax
movl %eax, 0xa0(%rsp)
movq 0x80(%rsp), %rdx
movq %r13, %rcx
testq %rdx, %rdx
je 0x23e0f
movq %r13, %rcx
xorl %esi, %esi
cmpl %eax, 0x20(%rdx)
setl %sil
cmovgeq %rdx, %rcx
movq 0x10(%rdx,%rsi,8), %rdx
testq %rdx, %rdx
jne 0x23df8
movq %r13, %rdx
cmpq %r13, %rcx
je 0x23e21
cmpl 0x20(%rcx), %eax
cmovlq %r13, %rcx
movq %rcx, %rdx
cmpq %r13, %rdx
jne 0x23e75
movl %eax, 0x100(%rsp)
movl $0x0, 0x104(%rsp)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2afcc
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x23e63
movl 0xa0(%rsp), %eax
movl %eax, (%rsi)
addq $0x4, %rsi
movq %rsi, 0x18(%rsp)
jmp 0x23e75
leaq 0x10(%rsp), %rdi
leaq 0xa0(%rsp), %rdx
callq 0x2b10c
incl %ebp
movq (%r12), %rax
movq 0x8(%r12), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rbp, %rcx
ja 0x23ddb
movq 0x10(%rsp), %rax
cmpq 0x18(%rsp), %rax
je 0x240b5
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x160(%rax)
movq 0x10(%rsp), %rax
cmpq %rax, 0x18(%rsp)
movq 0x30(%rsp), %r14
je 0x240a1
leaq 0xa0(%rsp), %r15
xorl %ecx, %ecx
xorl %r13d, %r13d
movq %rbx, 0x58(%rsp)
movq %r13, 0x28(%rsp)
movl (%rax,%rcx,4), %eax
movl %eax, 0xc(%rsp)
movq %r15, %rdi
callq 0x329b8
cmpl $0x0, 0x24(%r14)
jle 0x23fcc
xorl %ecx, %ecx
movq 0x18(%r14), %rax
movq %rcx, 0x60(%rsp)
movq (%rax,%rcx,8), %rcx
cmpl $0x0, 0x3c(%rcx)
jg 0x23fb2
cmpl $0x0, 0x4c(%rcx)
jle 0x23fb2
xorl %ebx, %ebx
movq %rcx, 0x68(%rsp)
movq 0x40(%rcx), %rax
movq (%rax,%rbx,8), %rbp
cmpl $0x0, 0x8(%rbp)
jle 0x23f9d
xorl %r15d, %r15d
xorl %r14d, %r14d
movq (%rbp), %rdx
movslq 0x20(%rdx,%r15), %rax
testq %rax, %rax
js 0x23f8d
movq (%r12), %rcx
movq 0x8(%r12), %rsi
subq %rcx, %rsi
shrq $0x2, %rsi
cmpl %esi, %eax
jge 0x23f8d
movl (%rcx,%rax,4), %eax
testl %eax, %eax
js 0x23f8d
cmpl 0xc(%rsp), %eax
jne 0x23f8d
addq %r15, %rdx
leaq 0x100(%rsp), %r13
movq %r13, %rdi
movq %rbp, %rsi
callq 0x323c4
leaq 0xa0(%rsp), %rdi
movq %r13, %rsi
callq 0x345d6
movq %r13, %rdi
callq 0x32470
incq %r14
movslq 0x8(%rbp), %rax
addq $0x30, %r15
cmpq %rax, %r14
jl 0x23f30
incq %rbx
movq 0x68(%rsp), %rcx
movslq 0x4c(%rcx), %rax
cmpq %rax, %rbx
jl 0x23f1c
movq 0x60(%rsp), %rcx
incq %rcx
movq 0x30(%rsp), %r14
movslq 0x24(%r14), %rax
cmpq %rax, %rcx
jl 0x23ef4
cmpl $0x0, 0xb0(%rsp)
movq 0x58(%rsp), %rbx
leaq 0xa0(%rsp), %r15
movq 0x28(%rsp), %r13
je 0x24079
leaq 0x100(%rsp), %r13
movq %r13, %rdi
leaq 0xbab6c(%rip), %rsi     # 0xdeb6a
movl 0xc(%rsp), %edx
xorl %eax, %eax
callq 0x130e0
movq %rbx, %rdi
movq %r15, %rsi
movq 0x38(%rsp), %rdx
movq %r13, %rcx
movq 0x40(%rsp), %r8
movq %r13, %r9
pushq 0x50(%rsp)
pushq 0x50(%rsp)
callq 0x23a03
addq $0x10, %rsp
movq %rax, %rbp
testq %rax, %rax
je 0x24074
movq (%rbp), %rax
movq %rbp, %rdi
movl 0xc(%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 0x3f2d4
movq (%rbx), %rax
movq %rbx, %rdi
movq %r13, %rsi
callq *0x40(%rax)
movq 0x28(%rsp), %r13
movq %r15, %rdi
callq 0x32b34
incl %r13d
movq 0x10(%rsp), %rax
movq 0x18(%rsp), %rdx
subq %rax, %rdx
sarq $0x2, %rdx
movq %r13, %rcx
cmpq %r13, %rdx
ja 0x23ed3
movq (%rbx), %rax
movl $0x1, %r14d
movq %rbx, %rdi
callq *0x168(%rax)
jmp 0x240b8
xorl %r14d, %r14d
leaq 0x70(%rsp), %rdi
callq 0x2af7c
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x240d9
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x134f0
movl %r14d, %eax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x24108
jmp 0x24108
movq %rax, %rbx
movl $0x30, %esi
movq %r13, %rdi
callq 0x134f0
jmp 0x24124
jmp 0x24108
jmp 0x2410f
movq %rax, %rbx
jmp 0x24131
jmp 0x2410f
movq %rax, %rbx
jmp 0x24124
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x32470
leaq 0xa0(%rsp), %rdi
callq 0x32b34
leaq 0x70(%rsp), %rdi
callq 0x2af7c
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x24152
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x134f0
movq %rbx, %rdi
callq 0x138b0