// File: tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelTree.cpp
// Function: gaps::R3SurfelTree::CreateMultiresolutionBlocks(gaps::R3SurfelNode*, double, double, double)
int R3SurfelTree::
CreateMultiresolutionBlocks(R3SurfelNode *node, RNScalar multiresolution_factor, RNScalar max_complexity, RNScalar max_resolution)
{
  // Check node
  if (node->NParts() == 0) return 1;

  // Create multiresolution blocks for parts
  for (int i = 0; i < node->NParts(); i++) {
    R3SurfelNode *part = node->Part(i);
    if (!CreateMultiresolutionBlocks(part, multiresolution_factor, max_complexity, max_resolution)) return 0;
  }

  // Check if already have blocks
  if (node->NBlocks() > 0) return 1;

  // Compute some statistics
  RNScalar total_complexity = 0;
  RNScalar mean_resolution = 0;
  for (int i = 0; i < node->NParts(); i++) {
    R3SurfelNode *part = node->Part(i);
    mean_resolution += part->Resolution() / node->NParts();
    total_complexity += part->Complexity();
  }

  // Check statistics
  if (total_complexity == 0) return 1;
  if (mean_resolution == 0) return 1;
  
  // Compute target resolution
  RNScalar target_resolution = multiresolution_factor * mean_resolution;
  if ((max_resolution > 0) && (target_resolution > max_resolution)) {
    target_resolution = max_resolution;
  }
  if ((max_complexity > 0) && (total_complexity > 0)) {
    RNScalar max_res = max_complexity * mean_resolution / total_complexity;
    if (target_resolution > max_res) target_resolution = max_res;
  }

  // Construct set with surfels sampled from blocks of parts
  R3SurfelPointSet set;
  for (int i = 0; i < node->NParts(); i++) {
    R3SurfelNode *part = node->Part(i);
    for (int j = 0; j < part->NBlocks(); j++) {
      R3SurfelBlock *block = part->Block(j);
          
      // Read block
      database->ReadBlock(block);
          
      // Compute subsampling probability based on block resolution
      RNScalar block_resolution = block->Resolution();
      if (block_resolution == 0) continue;
      RNScalar probability = target_resolution / block_resolution;
          
      // Insert surfels from block
      if (probability >= 1) {
        // Insert all surfels from block
        set.InsertPoints(block);
      }
      else {
        // Insert subset of surfels from block
        for (int k = 0; k < block->NSurfels(); k++) {
          if (RNRandomScalar() > probability) continue;
          const R3Surfel *surfel = block->Surfel(k);
          R3SurfelPoint point(block, surfel);
          set.InsertPoint(point);
        }
      }

      // Release block
      database->ReleaseBlock(block);
    }
  }
      
  // Create block from set
  R3SurfelBlock *block = new R3SurfelBlock(&set);
  if (!block) return 0;

  // Update block properties
  block->UpdateProperties();
  
  // Insert block into database
  database->InsertBlock(block);
        
  // Insert block into node
  node->InsertBlock(block);

  // Update node properties
  node->UpdateProperties();
        
  // Release block
  database->ReleaseBlock(block);

  // Mark scene as dirty
  if (scene) scene->SetDirty();

  // Return success
  return 1;
}

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x198(%rsp)
movq %rsi, 0x190(%rsp)
movsd %xmm0, 0x188(%rsp)
movsd %xmm1, 0x180(%rsp)
movsd %xmm2, 0x178(%rsp)
movq 0x198(%rsp), %rax
movq %rax, 0x60(%rsp)
movq 0x190(%rsp), %rdi
callq 0x31660
cmpl $0x0, %eax
jne 0x624d1
movl $0x1, 0x1a4(%rsp)
jmp 0x62b6c
movl $0x0, 0x174(%rsp)
movl 0x174(%rsp), %eax
movl %eax, 0x5c(%rsp)
movq 0x190(%rsp), %rdi
callq 0x31660
movl %eax, %ecx
movl 0x5c(%rsp), %eax
cmpl %ecx, %eax
jge 0x62575
movq 0x190(%rsp), %rdi
movl 0x174(%rsp), %esi
callq 0x43680
movq 0x60(%rsp), %rdi
movq %rax, 0x168(%rsp)
movq 0x168(%rsp), %rsi
movsd 0x188(%rsp), %xmm0
movsd 0x180(%rsp), %xmm1
movsd 0x178(%rsp), %xmm2
movq (%rdi), %rax
callq *0x60(%rax)
cmpl $0x0, %eax
jne 0x6255d
movl $0x0, 0x1a4(%rsp)
jmp 0x62b6c
jmp 0x6255f
movl 0x174(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x174(%rsp)
jmp 0x624dc
movq 0x190(%rsp), %rdi
callq 0x186b0
cmpl $0x0, %eax
jle 0x62597
movl $0x1, 0x1a4(%rsp)
jmp 0x62b6c
xorps %xmm0, %xmm0
movsd %xmm0, 0x160(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x158(%rsp)
movl $0x0, 0x154(%rsp)
movl 0x154(%rsp), %eax
movl %eax, 0x58(%rsp)
movq 0x190(%rsp), %rdi
callq 0x31660
movl %eax, %ecx
movl 0x58(%rsp), %eax
cmpl %ecx, %eax
jge 0x62671
movq 0x190(%rsp), %rdi
movl 0x154(%rsp), %esi
callq 0x43680
movq %rax, 0x148(%rsp)
movq 0x148(%rsp), %rdi
callq 0x5ecf0
movsd %xmm0, 0x50(%rsp)
movq 0x190(%rsp), %rdi
callq 0x31660
movsd 0x50(%rsp), %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
addsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x158(%rsp)
movq 0x148(%rsp), %rdi
callq 0x5ecb0
addsd 0x160(%rsp), %xmm0
movsd %xmm0, 0x160(%rsp)
movl 0x154(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x154(%rsp)
jmp 0x625ba
movsd 0x160(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x62695
jp 0x62695
movl $0x1, 0x1a4(%rsp)
jmp 0x62b6c
movsd 0x158(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x626b9
jp 0x626b9
movl $0x1, 0x1a4(%rsp)
jmp 0x62b6c
movsd 0x188(%rsp), %xmm0
mulsd 0x158(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
movsd 0x178(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6270c
movsd 0x140(%rsp), %xmm0
ucomisd 0x178(%rsp), %xmm0
jbe 0x6270c
movsd 0x178(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
movsd 0x180(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6277c
movsd 0x160(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x6277c
movsd 0x180(%rsp), %xmm0
mulsd 0x158(%rsp), %xmm0
divsd 0x160(%rsp), %xmm0
movsd %xmm0, 0x138(%rsp)
movsd 0x140(%rsp), %xmm0
ucomisd 0x138(%rsp), %xmm0
jbe 0x6277a
movsd 0x138(%rsp), %xmm0
movsd %xmm0, 0x140(%rsp)
jmp 0x6277c
leaq 0xd8(%rsp), %rdi
callq 0x55df0
movl $0x0, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0x190(%rsp), %rdi
callq 0x31660
movl %eax, 0x4c(%rsp)
jmp 0x627b2
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x62a45
movq 0x190(%rsp), %rdi
movl 0xd4(%rsp), %esi
callq 0x43680
movq %rax, 0x40(%rsp)
jmp 0x627dd
movq 0x40(%rsp), %rax
movq %rax, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
movl 0xb4(%rsp), %eax
movl %eax, 0x38(%rsp)
movq 0xb8(%rsp), %rdi
callq 0x186b0
movl %eax, 0x3c(%rsp)
jmp 0x62813
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x62a2d
movq 0xb8(%rsp), %rdi
movl 0xb4(%rsp), %esi
callq 0x186d0
movq %rax, 0x30(%rsp)
jmp 0x6283e
movq 0x60(%rsp), %rax
movq 0x30(%rsp), %rcx
movq %rcx, 0xa8(%rsp)
movq 0x10(%rax), %rdi
movq 0xa8(%rsp), %rsi
callq 0x31140
jmp 0x62863
movq 0xa8(%rsp), %rdi
callq 0x4aa00
movsd %xmm0, 0x28(%rsp)
jmp 0x62878
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x628b9
jp 0x628b9
jmp 0x62a17
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
jmp 0x62b5d
movsd 0x140(%rsp), %xmm0
divsd 0xa0(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
movsd 0x141883(%rip), %xmm1   # 0x1a4168
ucomisd %xmm1, %xmm0
jb 0x62907
movq 0xa8(%rsp), %rsi
leaq 0xd8(%rsp), %rdi
callq 0x56a60
jmp 0x62902
jmp 0x629fd
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x20(%rsp)
movq 0xa8(%rsp), %rdi
callq 0x1c0e0
movl %eax, 0x24(%rsp)
jmp 0x62930
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jge 0x629fb
callq 0x144f30
movsd %xmm0, 0x18(%rsp)
jmp 0x6294d
movsd 0x18(%rsp), %xmm0
ucomisd 0x98(%rsp), %xmm0
jbe 0x62960
jmp 0x629c2
movq 0xa8(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x43660
movq %rax, 0x10(%rsp)
jmp 0x6297b
movq 0x10(%rsp), %rax
movq %rax, 0x88(%rsp)
movq 0xa8(%rsp), %rsi
movq 0x88(%rsp), %rdx
leaq 0x78(%rsp), %rdi
callq 0x556c0
jmp 0x629a4
leaq 0xd8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x59090
jmp 0x629b8
leaq 0x78(%rsp), %rdi
callq 0x557a0
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x62912
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x557a0
jmp 0x62b5d
jmp 0x629fd
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0xa8(%rsp), %rsi
callq 0x311c0
jmp 0x62a15
jmp 0x62a17
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x627f5
jmp 0x62a2f
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0x62794
movl $0xc8, %edi
callq 0x13570
movq %rax, 0x8(%rsp)
jmp 0x62a56
movq 0x8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
callq 0x48200
jmp 0x62a6a
movq 0x8(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0x62abc
movl $0x0, 0x1a4(%rsp)
movl $0x1, 0x6c(%rsp)
jmp 0x62b4e
movq 0x8(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc8(%rsp)
movl %eax, 0xc4(%rsp)
movl $0xc8, %esi
callq 0x13580
jmp 0x62b5d
movq 0x70(%rsp), %rdi
callq 0x4c380
jmp 0x62ac8
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x70(%rsp), %rsi
movq (%rdi), %rax
movq 0x10(%rax), %rax
callq *%rax
jmp 0x62ae1
movq 0x190(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq (%rdi), %rax
movq 0x28(%rax), %rax
callq *%rax
jmp 0x62af9
movq 0x190(%rsp), %rdi
callq 0x606f0
jmp 0x62b08
movq 0x60(%rsp), %rax
movq 0x10(%rax), %rdi
movq 0x70(%rsp), %rsi
callq 0x311c0
jmp 0x62b1d
movq 0x60(%rsp), %rax
cmpq $0x0, 0x8(%rax)
je 0x62b3b
movq 0x60(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x1bea0
jmp 0x62b39
jmp 0x62b3b
movl $0x1, 0x1a4(%rsp)
movl $0x1, 0x6c(%rsp)
leaq 0xd8(%rsp), %rdi
callq 0x56040
jmp 0x62b6c
leaq 0xd8(%rsp), %rdi
callq 0x56040
jmp 0x62b7b
movl 0x1a4(%rsp), %eax
addq $0x1a8, %rsp            # imm = 0x1A8
retq
movq 0xc8(%rsp), %rdi
callq 0x13970
nopl (%rax,%rax)