// File: tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: CreateLoResNodes(gaps::R3SurfelScene*, char const*, char const*, double)
static int
CreateLoResNodes(R3SurfelScene *scene,
  const char *lores_scene_filename, const char *lores_database_filename,
  RNLength max_distance = FLT_MAX)
{
  // Open lores scene
  R3SurfelScene lores_scene;
  if (!lores_scene.OpenFile(lores_scene_filename, lores_database_filename, "r", "r"))
    return 0;

  // Get convenient variables
  R3SurfelTree *tree = scene->Tree();
  if (!tree) { lores_scene.CloseFile(); return 0; }
  R3SurfelDatabase *database = tree->Database();
  if (!database) { lores_scene.CloseFile(); return 0; }
  R3SurfelTree *lores_tree = lores_scene.Tree();
  if (!lores_tree) { lores_scene.CloseFile(); return 0; }
  R3SurfelDatabase *lores_database = lores_tree->Database();
  if (!lores_database) { lores_scene.CloseFile(); return 0; }

  // Allocate pointset for each node
  if (tree->NNodes() == 0) { lores_scene.CloseFile(); return 0; }
  R3SurfelPointSet *pointsets = new R3SurfelPointSet [ tree->NNodes() ];
  if (!pointsets) { lores_scene.CloseFile(); return 0; }
  
  // Associate each lores surfel with a leaf node
  for (int i = 0; i < lores_tree->NNodes(); i++) {
    R3SurfelNode *lores_node = lores_tree->Node(i);
    for (int j = 0; j < lores_node->NBlocks(); j++) {
      R3SurfelBlock *lores_block = lores_node->Block(j);

      // Read source block
      lores_database->ReadBlock(lores_block);

      // Update pointsets associated with leaf nodes
      for (int k = 0; k < lores_block->NSurfels(); k++) {
        const R3Surfel *lores_surfel = lores_block->Surfel(k);
        R3Point lores_position = lores_block->SurfelPosition(k);
        R3SurfelNode *closest_node = FindClosestLeafNode(tree, lores_position, max_distance);
        if (!closest_node) continue;
        int node_index = closest_node->TreeIndex();
        pointsets[node_index].InsertPoint(R3SurfelPoint(lores_block, lores_surfel));
      }

      // Release source block
      lores_database->ReleaseBlock(lores_block);
    }
  }
      
  // Create parents for leaf nodes with lores surfels
  RNArray<R3SurfelNode *> original_nodes;
  for (int i = 0; i < tree->NNodes(); i++) 
    original_nodes.Insert(tree->Node(i));
  for (int i = 0; i < original_nodes.NEntries(); i++) {
    R3SurfelNode *node = original_nodes.Kth(i);
    if (node->NParts() > 0) continue;
    if (node == tree->RootNode()) continue;
    if (pointsets[i].NPoints() <= 0) continue;
    R3SurfelNode *parent_node = node->Parent();
    if (!parent_node) continue;
    printf("%d %.0f\n", pointsets[i].NPoints(), node->Complexity());
    char node_name[1024] = { '\0' };
    if (node->Name()) sprintf(node_name, "%s_LORES", node->Name());
    else sprintf(node_name, "LORES");
    R3SurfelNode *interior_node = CreateNode(scene, &pointsets[i], parent_node, node_name, TRUE);
    if (!interior_node) continue;
    node->SetParent(interior_node);
  }

  // Delete pointsets
  delete [] pointsets;
  
  // Close lores scene file
  lores_scene.CloseFile();   

  // Return success
  return 1;
}

// Assembly:
subq $0x828, %rsp            # imm = 0x828
movq %rdi, 0x818(%rsp)
movq %rsi, 0x810(%rsp)
movq %rdx, 0x808(%rsp)
movsd %xmm0, 0x800(%rsp)
xorl %eax, %eax
movl %eax, %esi
leaq 0x610(%rsp), %rdi
movq %rdi, 0x128(%rsp)
callq 0x6fe50
movq 0x128(%rsp), %rdi
movq 0x810(%rsp), %rsi
movq 0x808(%rsp), %rdx
leaq 0x1dea13(%rip), %r8     # 0x2021d3
movq %r8, %rcx
callq 0x76760
movl %eax, 0x134(%rsp)
jmp 0x237d1
movl 0x134(%rsp), %eax
cmpl $0x0, %eax
jne 0x23811
movl $0x0, 0x824(%rsp)
movl $0x1, 0x600(%rsp)
jmp 0x241e9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x608(%rsp)
movl %eax, 0x604(%rsp)
jmp 0x24205
movq 0x818(%rsp), %rdi
callq 0x26780
movq %rax, 0x5f8(%rsp)
cmpq $0x0, 0x5f8(%rsp)
jne 0x2385f
xorl %eax, %eax
movl %eax, %esi
leaq 0x610(%rsp), %rdi
callq 0x76a60
jmp 0x23844
movl $0x0, 0x824(%rsp)
movl $0x1, 0x600(%rsp)
jmp 0x241e9
movq 0x5f8(%rsp), %rdi
callq 0x26790
movq %rax, 0x5f0(%rsp)
cmpq $0x0, 0x5f0(%rsp)
jne 0x238ad
xorl %eax, %eax
movl %eax, %esi
leaq 0x610(%rsp), %rdi
callq 0x76a60
jmp 0x23892
movl $0x0, 0x824(%rsp)
movl $0x1, 0x600(%rsp)
jmp 0x241e9
leaq 0x610(%rsp), %rdi
callq 0x26780
movq %rax, 0x5e8(%rsp)
cmpq $0x0, 0x5e8(%rsp)
jne 0x238fb
xorl %eax, %eax
movl %eax, %esi
leaq 0x610(%rsp), %rdi
callq 0x76a60
jmp 0x238e0
movl $0x0, 0x824(%rsp)
movl $0x1, 0x600(%rsp)
jmp 0x241e9
movq 0x5e8(%rsp), %rdi
callq 0x26790
movq %rax, 0x5e0(%rsp)
cmpq $0x0, 0x5e0(%rsp)
jne 0x23949
xorl %eax, %eax
movl %eax, %esi
leaq 0x610(%rsp), %rdi
callq 0x76a60
jmp 0x2392e
movl $0x0, 0x824(%rsp)
movl $0x1, 0x600(%rsp)
jmp 0x241e9
movq 0x5f8(%rsp), %rdi
callq 0x26330
movl %eax, 0x124(%rsp)
jmp 0x2395f
movl 0x124(%rsp), %eax
cmpl $0x0, %eax
jne 0x23999
xorl %eax, %eax
movl %eax, %esi
leaq 0x610(%rsp), %rdi
callq 0x76a60
jmp 0x2397e
movl $0x0, 0x824(%rsp)
movl $0x1, 0x600(%rsp)
jmp 0x241e9
movq 0x5f8(%rsp), %rdi
callq 0x26330
movl %eax, 0x120(%rsp)
jmp 0x239af
movl 0x120(%rsp), %eax
cltq
movq %rax, 0x108(%rsp)
movl $0x60, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
addq $0x8, %rdi
movq $-0x1, %rax
cmovbq %rax, %rdi
testb %cl, %cl
cmovneq %rax, %rdi
movq %rdi, 0x110(%rsp)
callq 0x16040
movq %rax, 0x118(%rsp)
jmp 0x239fa
movq 0x108(%rsp), %rax
movq 0x118(%rsp), %rcx
movq %rax, (%rcx)
addq $0x8, %rcx
movq %rcx, 0x100(%rsp)
cmpq $0x0, %rax
je 0x23a81
movq 0x100(%rsp), %rax
movq 0x108(%rsp), %rcx
imulq $0x60, %rcx, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, 0xf0(%rsp)
movq %rax, 0xf8(%rsp)
movq 0xf8(%rsp), %rdi
movq %rdi, 0xe8(%rsp)
callq 0x552d0
jmp 0x23a60
movq 0xf0(%rsp), %rcx
movq 0xe8(%rsp), %rax
addq $0x60, %rax
cmpq %rcx, %rax
movq %rax, 0xf8(%rsp)
jne 0x23a49
movq 0x100(%rsp), %rax
movq %rax, 0x5d8(%rsp)
cmpq $0x0, 0x5d8(%rsp)
jne 0x23b4d
xorl %eax, %eax
movl %eax, %esi
leaq 0x610(%rsp), %rdi
callq 0x76a60
jmp 0x23ab3
movl $0x0, 0x824(%rsp)
movl $0x1, 0x600(%rsp)
jmp 0x241e9
movq 0x100(%rsp), %rcx
movq %rax, %rsi
movq 0xe8(%rsp), %rax
movq %rsi, 0x608(%rsp)
movl %edx, 0x604(%rsp)
cmpq %rax, %rcx
movq %rax, 0xe0(%rsp)
je 0x23b33
movq 0xe0(%rsp), %rdi
addq $-0x60, %rdi
movq %rdi, 0xd8(%rsp)
callq 0x55520
movq 0x100(%rsp), %rcx
movq 0xd8(%rsp), %rax
cmpq %rcx, %rax
movq %rax, 0xe0(%rsp)
jne 0x23afd
movq 0x110(%rsp), %rsi
movq 0x118(%rsp), %rdi
callq 0x16e20
jmp 0x24205
movl $0x0, 0x5d4(%rsp)
movl 0x5d4(%rsp), %eax
movl %eax, 0xd0(%rsp)
movq 0x5e8(%rsp), %rdi
callq 0x26330
movl %eax, 0xd4(%rsp)
jmp 0x23b7c
movl 0xd0(%rsp), %eax
movl 0xd4(%rsp), %ecx
cmpl %ecx, %eax
jge 0x23e06
movq 0x5e8(%rsp), %rdi
movl 0x5d4(%rsp), %esi
callq 0x26350
movq %rax, 0xc8(%rsp)
jmp 0x23bb0
movq 0xc8(%rsp), %rax
movq %rax, 0x5c8(%rsp)
movl $0x0, 0x5c4(%rsp)
movl 0x5c4(%rsp), %eax
movl %eax, 0xc4(%rsp)
movq 0x5c8(%rsp), %rdi
callq 0x27710
movl %eax, %ecx
movl 0xc4(%rsp), %eax
cmpl %ecx, %eax
jge 0x23dee
movq 0x5c8(%rsp), %rdi
movl 0x5c4(%rsp), %esi
callq 0x27730
movq %rax, 0xb8(%rsp)
jmp 0x23c15
movq 0xb8(%rsp), %rax
movq %rax, 0x5b8(%rsp)
movq 0x5e0(%rsp), %rdi
movq 0x5b8(%rsp), %rsi
callq 0x27890
jmp 0x23c3c
movl $0x0, 0x5b4(%rsp)
movl 0x5b4(%rsp), %eax
movl %eax, 0xb4(%rsp)
movq 0x5b8(%rsp), %rdi
callq 0x267d0
movl %eax, %ecx
movl 0xb4(%rsp), %eax
cmpl %ecx, %eax
jge 0x23dbf
movq 0x5b8(%rsp), %rdi
movl 0x5b4(%rsp), %esi
callq 0x267e0
movq %rax, 0x5a8(%rsp)
movq 0x5b8(%rsp), %rsi
movl 0x5b4(%rsp), %edx
leaq 0x590(%rsp), %rdi
callq 0x2a150
jmp 0x23cad
movq 0x5f8(%rsp), %rdi
movsd 0x800(%rsp), %xmm0
leaq 0x590(%rsp), %rsi
callq 0x199c0
movq %rax, 0xa8(%rsp)
jmp 0x23cd5
movq 0xa8(%rsp), %rax
movq %rax, 0x588(%rsp)
cmpq $0x0, 0x588(%rsp)
jne 0x23cf5
jmp 0x23d83
movq 0x588(%rsp), %rdi
callq 0x2a250
movl %eax, 0xa4(%rsp)
jmp 0x23d0b
movl 0xa4(%rsp), %eax
movl %eax, 0x584(%rsp)
movq 0x5d8(%rsp), %rax
movslq 0x584(%rsp), %rcx
leaq (%rcx,%rcx,2), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq %rax, 0x98(%rsp)
movq 0x5b8(%rsp), %rsi
movq 0x5a8(%rsp), %rdx
leaq 0x570(%rsp), %rdi
callq 0x54ba0
jmp 0x23d5b
movq 0x98(%rsp), %rdi
movq (%rdi), %rax
movq 0x68(%rax), %rax
leaq 0x570(%rsp), %rsi
callq *%rax
jmp 0x23d76
leaq 0x570(%rsp), %rdi
callq 0x54c80
movl 0x5b4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5b4(%rsp)
jmp 0x23c47
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x608(%rsp)
movl %eax, 0x604(%rsp)
leaq 0x570(%rsp), %rdi
callq 0x54c80
jmp 0x24205
movq 0x5e0(%rsp), %rdi
movq 0x5b8(%rsp), %rsi
callq 0x26820
jmp 0x23dd6
jmp 0x23dd8
movl 0x5c4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c4(%rsp)
jmp 0x23bcb
jmp 0x23df0
movl 0x5d4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5d4(%rsp)
jmp 0x23b58
leaq 0x560(%rsp), %rdi
callq 0x276f0
jmp 0x23e15
movl $0x0, 0x55c(%rsp)
movl 0x55c(%rsp), %eax
movl %eax, 0x90(%rsp)
movq 0x5f8(%rsp), %rdi
callq 0x26330
movl %eax, 0x94(%rsp)
jmp 0x23e44
movl 0x90(%rsp), %eax
movl 0x94(%rsp), %ecx
cmpl %ecx, %eax
jge 0x23ec6
movq 0x5f8(%rsp), %rdi
movl 0x55c(%rsp), %esi
callq 0x26350
movq %rax, 0x88(%rsp)
jmp 0x23e74
movq 0x88(%rsp), %rsi
leaq 0x560(%rsp), %rdi
callq 0x29c70
jmp 0x23e8b
jmp 0x23e8d
movl 0x55c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x55c(%rsp)
jmp 0x23e20
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x608(%rsp)
movl %eax, 0x604(%rsp)
leaq 0x560(%rsp), %rdi
callq 0x277c0
jmp 0x24205
movl $0x0, 0x558(%rsp)
movl 0x558(%rsp), %eax
movl %eax, 0x84(%rsp)
leaq 0x560(%rsp), %rdi
callq 0x26490
movl %eax, %ecx
movl 0x84(%rsp), %eax
cmpl %ecx, %eax
jge 0x24134
movl 0x558(%rsp), %esi
leaq 0x560(%rsp), %rdi
callq 0x264a0
movq %rax, 0x78(%rsp)
jmp 0x23f18
movq 0x78(%rsp), %rax
movq %rax, 0x550(%rsp)
movq 0x550(%rsp), %rdi
callq 0x26380
cmpl $0x0, %eax
jle 0x23f3c
jmp 0x2411e
movq 0x550(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x5f8(%rsp), %rdi
callq 0x276d0
movq %rax, 0x70(%rsp)
jmp 0x23f5d
movq 0x68(%rsp), %rax
movq 0x70(%rsp), %rcx
cmpq %rcx, %rax
jne 0x23f71
jmp 0x2411e
movq 0x5d8(%rsp), %rdi
movslq 0x558(%rsp), %rax
imulq $0x60, %rax, %rax
addq %rax, %rdi
callq 0x29df0
cmpl $0x0, %eax
jg 0x23f97
jmp 0x2411e
movq 0x550(%rsp), %rdi
callq 0x2a260
movq %rax, 0x60(%rsp)
jmp 0x23fab
movq 0x60(%rsp), %rax
movq %rax, 0x548(%rsp)
cmpq $0x0, 0x548(%rsp)
jne 0x23fc8
jmp 0x2411e
movq 0x5d8(%rsp), %rdi
movslq 0x558(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x5, %rax
addq %rax, %rdi
callq 0x29df0
movl %eax, 0x54(%rsp)
movq 0x550(%rsp), %rdi
callq 0x5cb40
movsd %xmm0, 0x58(%rsp)
jmp 0x24001
movsd 0x58(%rsp), %xmm0
movl 0x54(%rsp), %esi
leaq 0x1d4d40(%rip), %rdi    # 0x1f8d52
movb $0x1, %al
callq 0x16090
jmp 0x2401b
leaq 0x140(%rsp), %rdi
xorl %esi, %esi
movl $0x400, %edx            # imm = 0x400
callq 0x163a0
movq 0x550(%rsp), %rdi
callq 0x2a270
movq %rax, 0x48(%rsp)
jmp 0x24043
movq 0x48(%rsp), %rax
cmpq $0x0, %rax
je 0x24089
leaq 0x140(%rsp), %rax
movq %rax, 0x38(%rsp)
movq 0x550(%rsp), %rdi
callq 0x2a270
movq %rax, 0x40(%rsp)
jmp 0x2406f
movq 0x40(%rsp), %rdx
movq 0x38(%rsp), %rdi
leaq 0x1d4cdb(%rip), %rsi    # 0x1f8d5b
movb $0x0, %al
callq 0x16130
jmp 0x2409f
leaq 0x140(%rsp), %rdi
leaq 0x1d4cc6(%rip), %rsi    # 0x1f8d5e
movb $0x0, %al
callq 0x16130
movq 0x818(%rsp), %rdi
movq 0x5d8(%rsp), %rsi
movslq 0x558(%rsp), %rax
leaq (%rax,%rax,2), %rax
shlq $0x5, %rax
addq %rax, %rsi
movq 0x548(%rsp), %rdx
leaq 0x140(%rsp), %rcx
movl $0x1, %r9d
movl %r9d, %r8d
callq 0x86830
movq %rax, 0x30(%rsp)
jmp 0x240e7
movq 0x30(%rsp), %rax
movq %rax, 0x138(%rsp)
cmpq $0x0, 0x138(%rsp)
jne 0x24101
jmp 0x2411e
movq 0x550(%rsp), %rdi
movq 0x138(%rsp), %rsi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
jmp 0x2411c
jmp 0x2411e
movl 0x558(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x558(%rsp)
jmp 0x23ed1
movq 0x5d8(%rsp), %rax
movq %rax, 0x28(%rsp)
cmpq $0x0, %rax
je 0x241b3
movq 0x28(%rsp), %rcx
movq %rcx, %rax
addq $-0x8, %rax
movq %rax, 0x10(%rsp)
movq -0x8(%rcx), %rax
movq %rax, 0x18(%rsp)
imulq $0x60, %rax, %rdx
movq %rcx, %rax
addq %rdx, %rax
cmpq %rax, %rcx
movq %rax, 0x20(%rsp)
je 0x2419c
movq 0x20(%rsp), %rdi
addq $-0x60, %rdi
movq %rdi, 0x8(%rsp)
callq 0x55520
movq 0x28(%rsp), %rcx
movq 0x8(%rsp), %rax
cmpq %rcx, %rax
movq %rax, 0x20(%rsp)
jne 0x24175
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rax
imulq $0x60, %rax, %rsi
addq $0x8, %rsi
callq 0x16e20
xorl %eax, %eax
movl %eax, %esi
leaq 0x610(%rsp), %rdi
callq 0x76a60
jmp 0x241c6
movl $0x1, 0x824(%rsp)
movl $0x1, 0x600(%rsp)
leaq 0x560(%rsp), %rdi
callq 0x277c0
leaq 0x610(%rsp), %rdi
callq 0x706b0
movl 0x824(%rsp), %eax
addq $0x828, %rsp            # imm = 0x828
retq
leaq 0x610(%rsp), %rdi
callq 0x706b0
movq 0x608(%rsp), %rdi
callq 0x16ca0
nop