// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: LoadSurfels(gaps::R3SurfelScene*, char const*, char const*, char const*, char const*, char const*)
static R3SurfelNode *
LoadSurfels(R3SurfelScene *scene, const char *surfels_filename, 
  const char *object_name, const char *parent_object_name,
  const char *node_name, const char *parent_node_name)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

  // Get surfel tree
  R3SurfelTree *tree = scene->Tree();
  if (!tree) {
    RNFail("Scene has no tree\n");
    return NULL;
  }    

  // Get surfel database
  R3SurfelDatabase *database = tree->Database();
  if (!database) {
    RNFail("Scene has no database\n");
    return NULL;
  }    

  // Find parent object
  R3SurfelObject *parent_object = NULL;
  if (parent_object_name && 
      strcmp(parent_object_name, "None") && 
      strcmp(parent_object_name, "none") && 
      strcmp(parent_object_name, "NONE")) {
    parent_object = scene->FindObjectByName(parent_object_name);
    if (!parent_object) {
      RNFail("Unable to find parent object with name %s\n", parent_object_name);
      return NULL;
    }
  }

  // Find parent node
  R3SurfelNode *parent_node = tree->FindNodeByName(parent_node_name);
  if (!parent_node) {
    RNFail("Unable to find parent node with name %s\n", parent_node_name);
    return NULL;
  }

  // Create node
  R3SurfelNode *node = new R3SurfelNode(node_name);
  if (!node) {
    RNFail("Unable to allocate node for %s\n", surfels_filename);
    return NULL;
  }

  // Insert node into tree
  tree->InsertNode(node, parent_node);

  // Create block
  R3SurfelBlock *block = new R3SurfelBlock();
  if (!block) {
    RNFail("Unable to allocate block for %s\n", surfels_filename);
    return NULL;
  }

  // Read block
  if (!block->ReadFile(surfels_filename)) {
    RNFail("Unable to read block from %s\n", surfels_filename);
    return NULL;
  }

  // Extract subset of surfels
  if (aerial_only || terrestrial_only) {
    // Create subset
    R3SurfelPointSet *subset = new R3SurfelPointSet();

    // Fill subset
    for (int i = 0; i < block->NSurfels(); i++) {
      const R3Surfel *surfel = block->Surfel(i);
      if (surfel->IsAerial() && terrestrial_only) continue;
      if (!surfel->IsAerial() && aerial_only) continue;
      R3SurfelPoint point(block, surfel);
      subset->InsertPoint(point);
    }

    // Replace block with subset
    // Note: it is important to delete subset first, since it references block
    R3SurfelBlock *subset_block = new R3SurfelBlock(subset);
    delete subset;
    delete block;
    block = subset_block;
  }

  // Update block properties
  block->UpdateProperties();

  // Insert block into database
  database->InsertBlock(block);

  // Insert block into node
  node->InsertBlock(block);

  // Update node properties
  node->UpdateProperties();

  // Create object
  if (object_name && 
      strcmp(object_name, "None") && 
      strcmp(object_name, "none") && 
      strcmp(object_name, "NONE") && 
      parent_object) {
    // Create object
    R3SurfelObject *object = new R3SurfelObject(object_name);
    if (!object) {
      RNFail("Unable to create object\n");
      return NULL;
    }

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

    // Insert node into object
    object->InsertNode(node);
      
    // Update object properties
    object->UpdateProperties();
  }

  // Release block
  database->ReleaseBlock(block);

  // Print statistics
  if (print_verbose) {
    printf("Loaded surfels from %s ...\n", surfels_filename);
    printf("  Time = %.2f seconds\n", start_time.Elapsed());
    printf("  # Surfels = %g\n", node->Complexity());
    fflush(stdout);
  }

  // Return node
  return node;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %r13
movq %r8, %rbx
movq %rcx, %r15
movq %rdx, 0x20(%rsp)
movq %rsi, %r12
movq %rdi, %rbp
leaq 0x38(%rsp), %r14
movq %r14, %rdi
callq 0xc0cf4
movq %r14, %rdi
xorl %esi, %esi
callq 0x15a10
movq %rbp, 0x10(%rsp)
movq 0x8(%rbp), %rbp
testq %rbp, %rbp
je 0x1ac69
movq 0x10(%rbp), %r14
testq %r14, %r14
je 0x1adc6
testq %r15, %r15
je 0x1ac75
leaq 0xecb51(%rip), %rsi     # 0x107760
movq %r15, %rdi
callq 0x158b0
testl %eax, %eax
je 0x1ac75
leaq 0xecb43(%rip), %rsi     # 0x107765
movq %r15, %rdi
callq 0x158b0
testl %eax, %eax
je 0x1ac75
leaq 0xecb35(%rip), %rsi     # 0x10776a
movq %r15, %rdi
callq 0x158b0
testl %eax, %eax
je 0x1ac75
movq 0x10(%rsp), %rdi
movq %r15, %rsi
callq 0x428ca
movq %rax, (%rsp)
testq %rax, %rax
jne 0x1ac7d
leaq 0xecad7(%rip), %rdi     # 0x107735
xorl %r14d, %r14d
movq %r15, %rsi
jmp 0x1adf8
leaq 0xeca89(%rip), %rdi     # 0x1076f9
jmp 0x1adcd
movq $0x0, (%rsp)
movq %rbp, %rdi
movq %r13, %rsi
callq 0x3a6a0
testq %rax, %rax
je 0x1addc
movq %rax, %r15
movq %r14, 0x8(%rsp)
movl $0xd0, %edi
callq 0x15610
movq %rax, %r14
movq %rax, %rdi
movq %rbx, %rsi
callq 0x387f0
movq %rbp, %rdi
movq %r14, %rsi
movq %r15, %rdx
callq 0x3a596
movl $0xc8, %edi
callq 0x15610
movq %rax, %rbp
movq %rax, %rdi
callq 0x2da64
movq %rbp, %rdi
movq %r12, %rsi
callq 0x30b86
testl %eax, %eax
je 0x1adeb
movl 0x1520cb(%rip), %eax    # 0x16cdb8
orl 0x1520c1(%rip), %eax    # 0x16cdb4
je 0x1ae04
movq %r12, 0x18(%rsp)
movl $0x60, %edi
callq 0x15610
movq %rax, %r13
movq %rax, %rdi
callq 0x34b40
movl 0x8(%rbp), %eax
testl %eax, %eax
jle 0x1ad88
movl 0x152098(%rip), %ecx    # 0x16cdb8
xorl %r15d, %r15d
leaq 0x28(%rsp), %rbx
xorl %r12d, %r12d
movq (%rbp), %rdx
movb 0x2f(%rdx,%r15), %sil
testb $0x2, %sil
je 0x1ad3e
testl %ecx, %ecx
jne 0x1ad79
testb $0x2, %sil
jne 0x1ad4d
cmpl $0x0, 0x152069(%rip)    # 0x16cdb4
jne 0x1ad79
addq %r15, %rdx
movq %rbx, %rdi
movq %rbp, %rsi
callq 0x344fa
movq (%r13), %rax
movq %r13, %rdi
movq %rbx, %rsi
callq *0x68(%rax)
movq %rbx, %rdi
callq 0x345ba
movl 0x152042(%rip), %ecx    # 0x16cdb8
movl 0x8(%rbp), %eax
incq %r12
movslq %eax, %rdx
addq $0x30, %r15
cmpq %rdx, %r12
jl 0x1ad2b
movl $0xc8, %edi
callq 0x15610
movq %rax, %rbx
movq %rax, %rdi
movq %r13, %rsi
callq 0x2dd6e
movq (%r13), %rax
movq %r13, %rdi
callq *0x8(%rax)
movq %rbp, %rdi
callq 0x2ef26
movl $0xc8, %esi
movq %rbp, %rdi
callq 0x15620
movq 0x18(%rsp), %r12
jmp 0x1ae07
leaq 0xec9e4(%rip), %rdi     # 0x1077b1
xorl %r14d, %r14d
xorl %eax, %eax
callq 0xc3e02
jmp 0x1af72
leaq 0xec929(%rip), %rdi     # 0x10770c
xorl %r14d, %r14d
movq %r13, %rsi
jmp 0x1adf8
leaq 0xec9d6(%rip), %rdi     # 0x1077c8
xorl %r14d, %r14d
movq %r12, %rsi
xorl %eax, %eax
callq 0xc3e02
jmp 0x1af72
movq %rbp, %rbx
movq %rbx, %rdi
callq 0x304d8
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
callq *0x10(%rax)
movq (%r14), %rax
movq %r14, %rdi
movq %rbx, %rsi
callq *0x28(%rax)
movq %r14, %rdi
callq 0x39820
movq 0x20(%rsp), %r15
testq %r15, %r15
je 0x1aeb8
leaq 0xec91e(%rip), %rsi     # 0x107760
movq %r15, %rdi
callq 0x158b0
testl %eax, %eax
je 0x1aeb8
leaq 0xec910(%rip), %rsi     # 0x107765
movq %r15, %rdi
callq 0x158b0
testl %eax, %eax
je 0x1aeb8
leaq 0xec902(%rip), %rsi     # 0x10776a
movq %r15, %rdi
callq 0x158b0
cmpq $0x0, (%rsp)
je 0x1aeb8
testl %eax, %eax
je 0x1aeb8
movl $0xf8, %edi
callq 0x15610
movq %rax, %r13
movq %rax, %rdi
movq %r15, %rsi
callq 0x3fe08
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
movq %r13, %rsi
movq (%rsp), %rdx
callq *0x18(%rax)
movq %r13, %rdi
movq %r14, %rsi
callq 0x41076
movq %r13, %rdi
callq 0x415c4
movl 0xb4(%rbx), %eax
cmpl $0x1, %eax
jne 0x1aee5
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %rbx, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x1af1b
movl 0xb4(%rbx), %eax
decl %eax
movl %eax, 0xb4(%rbx)
jne 0x1af1b
testb $0x2, 0x89(%rbx)
je 0x1af1b
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movq %rbx, %rsi
callq *0x18(%rax)
movq %rbx, %rdi
callq 0x2ef26
movl $0xc8, %esi
movq %rbx, %rdi
callq 0x15620
cmpl $0x0, 0x151e9a(%rip)    # 0x16cdbc
je 0x1af72
leaq 0xec8bb(%rip), %rdi     # 0x1077e6
movq %r12, %rsi
xorl %eax, %eax
callq 0x15080
leaq 0x38(%rsp), %rdi
callq 0xc0d56
leaq 0xec66c(%rip), %rdi     # 0x1075b2
movb $0x1, %al
callq 0x15080
movq %r14, %rdi
callq 0x38ac2
leaq 0xec8a6(%rip), %rdi     # 0x107802
movb $0x1, %al
callq 0x15080
movq 0x150ff6(%rip), %rax    # 0x16bf60
movq (%rax), %rdi
callq 0x15750
movq %r14, %rax
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %r15
movl $0xf8, %esi
jmp 0x1afa3
movq %rax, %r15
movl $0xc8, %esi
movq %rbx, %rdi
jmp 0x1afc0
movq %rax, %r15
movl $0x60, %esi
movq %r13, %rdi
jmp 0x1afc0
movq %rax, %r15
movl $0xc8, %esi
movq %rbp, %rdi
jmp 0x1afc0
movq %rax, %r15
movl $0xd0, %esi
movq %r14, %rdi
callq 0x15620
jmp 0x1afd4
movq %rax, %r15
leaq 0x28(%rsp), %rdi
callq 0x345ba
movq %r15, %rdi
callq 0x15ad0