// File: tomfunkhouser[P]gaps/apps/sflprocess/sflprocess.cpp
// Function: CreateMultiresolutionBlocks(gaps::R3SurfelScene*, char const*, double, double)
static int
CreateMultiresolutionBlocks(R3SurfelScene *scene, const char *node_name,
  RNScalar multiresolution_factor, RNScalar max_node_complexity)
{
  // Start statistics
  RNTime start_time;
  start_time.Read();

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

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

  // Check node
  if (node) {
    // Create multiresolution starting at node
    tree->CreateMultiresolutionBlocks(node, multiresolution_factor, max_node_complexity);

    // Print statistics
    if (print_verbose) {
      printf("Created multiresolution blocks for nodes starting at %s ...\n", node_name);
      printf("  Time = %.2f seconds\n", start_time.Elapsed());
      printf("  # Nodes = %d\n", tree->NNodes());
      printf("  # Blocks = %d\n", tree->Database()->NBlocks());
      fflush(stdout);
    }
  }
  else {
    // Create multiresolution starting at all root nodes
    tree->CreateMultiresolutionBlocks(multiresolution_factor, max_node_complexity);

    // Print statistics
    if (print_verbose) {
      printf("Created multiresolution blocks for all nodes  ...\n");
      printf("  Time = %.2f seconds\n", start_time.Elapsed());
      printf("  # Nodes = %d\n", tree->NNodes());
      printf("  # Blocks = %d\n", tree->Database()->NBlocks());
      fflush(stdout);
    }
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x48, %rsp
movq %rdi, 0x38(%rsp)
movq %rsi, 0x30(%rsp)
movsd %xmm0, 0x28(%rsp)
movsd %xmm1, 0x20(%rsp)
leaq 0x10(%rsp), %rdi
callq 0x16dd30
leaq 0x10(%rsp), %rdi
callq 0x263f0
movq 0x38(%rsp), %rdi
callq 0x26780
movq %rax, 0x8(%rsp)
cmpq $0x0, 0x8(%rsp)
jne 0x250b0
leaq 0x1d3ac2(%rip), %rdi    # 0x1f8b5e
movb $0x0, %al
callq 0x172cb0
movl $0x0, 0x44(%rsp)
jmp 0x25252
movq $0x0, (%rsp)
movq 0x30(%rsp), %rdi
leaq 0x1d3ad1(%rip), %rsi    # 0x1f8b95
callq 0x16a80
cmpl $0x0, %eax
je 0x2510a
movq 0x8(%rsp), %rdi
movq 0x30(%rsp), %rsi
callq 0x60280
movq %rax, (%rsp)
cmpq $0x0, (%rsp)
jne 0x25108
movq 0x30(%rsp), %rsi
leaq 0x1d3aa5(%rip), %rdi    # 0x1f8b99
movb $0x0, %al
callq 0x172cb0
movl $0x0, 0x44(%rsp)
jmp 0x25252
jmp 0x2510a
cmpq $0x0, (%rsp)
je 0x251b7
movq 0x8(%rsp), %rdi
movq (%rsp), %rsi
movsd 0x28(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movq (%rdi), %rax
xorps %xmm2, %xmm2
callq *0x60(%rax)
cmpl $0x0, 0x23f686(%rip)    # 0x2647c0
je 0x251b2
movq 0x30(%rsp), %rsi
leaq 0x1d3d3f(%rip), %rdi    # 0x1f8e87
movb $0x0, %al
callq 0x16090
leaq 0x10(%rsp), %rdi
callq 0x16ddf0
leaq 0x1d341f(%rip), %rdi    # 0x1f857f
movb $0x1, %al
callq 0x16090
movq 0x8(%rsp), %rdi
callq 0x26330
movl %eax, %esi
leaq 0x1d3543(%rip), %rdi    # 0x1f86bd
movb $0x0, %al
callq 0x16090
movq 0x8(%rsp), %rdi
callq 0x26790
movq %rax, %rdi
callq 0x267a0
movl %eax, %esi
leaq 0x1d3531(%rip), %rdi    # 0x1f86cd
movb $0x0, %al
callq 0x16090
movq 0x235de6(%rip), %rax    # 0x25af90
movq (%rax), %rdi
callq 0x168c0
jmp 0x2524a
movq 0x8(%rsp), %rdi
movsd 0x28(%rsp), %xmm0
movsd 0x20(%rsp), %xmm1
movq (%rdi), %rax
callq *0x68(%rax)
cmpl $0x0, 0x23f5eb(%rip)    # 0x2647c0
je 0x25248
leaq 0x1d3ce6(%rip), %rdi    # 0x1f8ec4
movb $0x0, %al
callq 0x16090
leaq 0x10(%rsp), %rdi
callq 0x16ddf0
leaq 0x1d3389(%rip), %rdi    # 0x1f857f
movb $0x1, %al
callq 0x16090
movq 0x8(%rsp), %rdi
callq 0x26330
movl %eax, %esi
leaq 0x1d34ad(%rip), %rdi    # 0x1f86bd
movb $0x0, %al
callq 0x16090
movq 0x8(%rsp), %rdi
callq 0x26790
movq %rax, %rdi
callq 0x267a0
movl %eax, %esi
leaq 0x1d349b(%rip), %rdi    # 0x1f86cd
movb $0x0, %al
callq 0x16090
movq 0x235d50(%rip), %rax    # 0x25af90
movq (%rax), %rdi
callq 0x168c0
jmp 0x2524a
movl $0x1, 0x44(%rsp)
movl 0x44(%rsp), %eax
addq $0x48, %rsp
retq
nopl (%rax,%rax)