// File: /workspace/github_cpp_makefile_star2_O2/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
movq %r9, 0x48(%rsp)
movq %r8, 0x40(%rsp)
movq %rcx, 0x8(%rsp)
movq %rdx, 0x10(%rsp)
xorl %ebx, %ebx
testq %rdi, %rdi
je 0x228c9
movq 0x8(%rdi), %rdx
testq %rdx, %rdx
je 0x228c9
cmpq $0x0, 0x10(%rdx)
je 0x228c9
movq %rsi, %r12
cmpq $0x0, 0x10(%rsp)
jne 0x225fd
movq 0x10(%rdi), %rax
movq (%rax), %rax
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x228c9
cmpq $0x0, 0x8(%rsp)
jne 0x2261a
movq 0x18(%rdx), %rax
movq (%rax), %rax
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x228c9
movq (%r12), %rax
movq 0x8(%r12), %rcx
cmpq %rcx, %rax
je 0x228c9
movq %rdx, 0x18(%rsp)
movq %rdi, (%rsp)
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %rbx
movaps %xmm0, (%rbx)
andq $0x0, 0x10(%rbx)
leaq 0xd8(%rsp), %r14
andl $0x0, (%r14)
andq $0x0, 0x8(%r14)
movq %r14, 0x10(%r14)
movq %r14, 0x18(%r14)
andq $0x0, 0x20(%r14)
xorl %ebp, %ebp
leaq 0xd0(%rsp), %r15
leaq 0x70(%rsp), %r13
movl %ebp, %ebp
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rbp, %rcx
jbe 0x226d2
movl (%rax,%rbp,4), %eax
movl %eax, 0x70(%rsp)
movq %r15, %rdi
movq %r13, %rsi
callq 0x28b4c
cmpq %r14, %rax
jne 0x226c5
movl 0x70(%rsp), %eax
movl %eax, 0x100(%rsp)
andl $0x0, 0x104(%rsp)
movq %r15, %rdi
leaq 0x100(%rsp), %rsi
callq 0x28b9e
movq %rbx, %rdi
movq %r13, %rsi
callq 0x284e6
incl %ebp
movq (%r12), %rax
movq 0x8(%r12), %rcx
jmp 0x22672
movq 0x20(%rsp), %rax
cmpq 0x28(%rsp), %rax
je 0x228b0
movq (%rsp), %rbx
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x160(%rax)
xorl %edx, %edx
movq 0x18(%rsp), %r14
movl %edx, %edx
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
subq %rax, %rcx
sarq $0x2, %rcx
cmpq %rdx, %rcx
jbe 0x2289f
movq %rdx, 0x50(%rsp)
movl (%rax,%rdx,4), %ebp
leaq 0x70(%rsp), %rdi
callq 0x2f52c
xorl %ecx, %ecx
movslq 0x24(%r14), %rax
cmpq %rax, %rcx
jge 0x227f6
movq 0x18(%r14), %rax
movq %rcx, 0x58(%rsp)
movq (%rax,%rcx,8), %rcx
cmpl $0x0, 0x3c(%rcx)
jle 0x22758
movq 0x58(%rsp), %rcx
incq %rcx
movq 0x18(%rsp), %r14
jmp 0x22729
xorl %edx, %edx
movq %rcx, 0x60(%rsp)
movslq 0x4c(%rcx), %rax
cmpq %rax, %rdx
jge 0x22749
movq 0x40(%rcx), %rax
movq %rdx, 0x68(%rsp)
movq (%rax,%rdx,8), %rbx
xorl %r14d, %r14d
xorl %r13d, %r13d
movslq 0x8(%rbx), %rax
cmpq %rax, %r13
jge 0x227e4
movq (%rbx), %rdx
movslq 0x20(%rdx,%r14), %rax
testq %rax, %rax
js 0x227db
movq (%r12), %rcx
movq 0x8(%r12), %rsi
subq %rcx, %rsi
shrq $0x2, %rsi
cmpl %esi, %eax
jge 0x227db
movl (%rcx,%rax,4), %eax
testl %eax, %eax
js 0x227db
cmpl %ebp, %eax
jne 0x227db
addq %r14, %rdx
leaq 0x100(%rsp), %r15
movq %r15, %rdi
movq %rbx, %rsi
callq 0x2f11c
leaq 0x70(%rsp), %rdi
movq %r15, %rsi
callq 0x30c26
movq %r15, %rdi
callq 0x2f17a
incq %r13
addq $0x30, %r14
jmp 0x2277b
movq 0x68(%rsp), %rdx
incq %rdx
movq 0x60(%rsp), %rcx
jmp 0x2275f
cmpl $0x0, 0x80(%rsp)
je 0x22885
leaq 0x100(%rsp), %rbx
movq %rbx, %rdi
leaq 0xa633c(%rip), %rsi     # 0xc8b52
movl %ebp, %edx
xorl %eax, %eax
callq 0x130e0
movq (%rsp), %rdi
leaq 0x70(%rsp), %rsi
movq 0x10(%rsp), %rdx
movq %rbx, %rcx
movq 0x8(%rsp), %r8
movq %rbx, %r9
pushq 0x48(%rsp)
pushq 0x48(%rsp)
callq 0x222a7
popq %rcx
popq %rdx
movq %rax, %rbx
testq %rax, %rax
je 0x22885
movq (%rbx), %rax
movq %rbx, %rdi
movl %ebp, %esi
callq *0x18(%rax)
pushq $0x30
popq %rdi
callq 0x134f0
movq %rax, %rbp
movq %rax, %rdi
pushq $0x1
popq %rsi
movq %rbx, %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x3a124
movq (%rsp), %rdi
movq (%rdi), %rax
movq %rbp, %rsi
callq *0x40(%rax)
leaq 0x70(%rsp), %rdi
callq 0x2f6a8
movq 0x50(%rsp), %rdx
incl %edx
movq (%rsp), %rbx
jmp 0x226f9
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x168(%rax)
pushq $0x1
popq %rbx
jmp 0x228b2
xorl %ebx, %ebx
leaq 0xd0(%rsp), %rdi
callq 0x28982
leaq 0x20(%rsp), %rdi
callq 0x28b18
movl %ebx, %eax
addq $0x508, %rsp            # imm = 0x508
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x228f7
movq %rax, %rbx
pushq $0x30
popq %rsi
movq %rbp, %rdi
callq 0x13500
jmp 0x22911
jmp 0x228f7
jmp 0x2290e
jmp 0x228f7
jmp 0x2290e
movq %rax, %rbx
jmp 0x2291b
movq %rax, %rbx
leaq 0x100(%rsp), %rdi
callq 0x2f17a
jmp 0x22911
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x2f6a8
leaq 0xd0(%rsp), %rdi
callq 0x28982
leaq 0x20(%rsp), %rdi
callq 0x28b18
movq %rbx, %rdi
callq 0x138f0