// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc8, %rsp
movapd %xmm2, 0x20(%rsp)
movsd %xmm1, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
pushq $0x1
popq %rbp
cmpl $0x0, 0x3c(%rsi)
je 0x35319
movq %rsi, %r14
xorl %ebx, %ebx
movapd %xmm0, 0x30(%rsp)
movslq 0x3c(%r14), %rax
cmpq %rax, %rbx
jge 0x350a8
movq 0x30(%r14), %rax
movq (%rax,%rbx,8), %rsi
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movapd 0x30(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
movapd 0x20(%rsp), %xmm2
callq *0x60(%rax)
incq %rbx
testl %eax, %eax
jne 0x3506c
xorl %ebp, %ebp
jmp 0x35319
cmpl $0x0, 0x4c(%r14)
jg 0x35319
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
xorl %ebx, %ebx
xorpd %xmm5, %xmm5
cltq
cmpq %rax, %rbx
jge 0x35115
movq 0x30(%r14), %rax
movq (%rax,%rbx,8), %r12
movq %r12, %rdi
movsd %xmm5, 0x50(%rsp)
callq 0x33680
cvtsi2sdl 0x3c(%r14), %xmm1
divsd %xmm1, %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movq %r12, %rdi
callq 0x3365c
movsd 0x50(%rsp), %xmm5
addsd %xmm0, %xmm5
incq %rbx
movl 0x3c(%r14), %eax
jmp 0x350c3
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm5
jne 0x35125
jnp 0x35319
movsd 0x18(%rsp), %xmm4
ucomisd %xmm0, %xmm4
jne 0x35137
jnp 0x35319
movapd 0x30(%rsp), %xmm3
mulsd %xmm4, %xmm3
xorpd %xmm1, %xmm1
movapd 0x20(%rsp), %xmm2
cmpltpd %xmm2, %xmm1
cmpltpd %xmm3, %xmm2
andpd %xmm1, %xmm2
movd %xmm2, %eax
testb $0x1, %al
jne 0x35167
movapd %xmm3, 0x20(%rsp)
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3518f
ucomisd %xmm0, %xmm5
jbe 0x3518f
mulsd 0x10(%rsp), %xmm4
divsd %xmm5, %xmm4
minsd 0x20(%rsp), %xmm4
movapd %xmm4, 0x20(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x2f52c
xorl %ecx, %ecx
leaq 0x58(%rsp), %r13
movq %r14, 0x48(%rsp)
movslq 0x3c(%r14), %rax
cmpq %rax, %rcx
jge 0x3529e
movq 0x30(%r14), %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %rbx
xorl %r15d, %r15d
movslq 0x4c(%rbx), %rax
cmpq %rax, %r15
jge 0x3528c
movq 0x40(%rbx), %rax
movq (%rax,%r15,8), %rbp
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq %rbp, %rsi
callq 0x20a64
movq %rbp, %rdi
callq 0x2aa12
ucomisd 0x93188(%rip), %xmm0    # 0xc8380
jne 0x35200
jnp 0x35284
movapd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
ucomisd 0x92f30(%rip), %xmm1    # 0xc8148
jae 0x35266
xorl %r14d, %r14d
xorl %r12d, %r12d
movslq 0x8(%rbp), %rax
cmpq %rax, %r12
jge 0x35273
callq 0x9c2ba
ucomisd 0x10(%rsp), %xmm0
ja 0x3525d
movq (%rbp), %rdx
addq %r14, %rdx
movq %r13, %rdi
movq %rbp, %rsi
callq 0x2f11c
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
callq 0x30c26
movq %r13, %rdi
callq 0x2f17a
incq %r12
addq $0x30, %r14
jmp 0x35220
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x2fb3e
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq %rbp, %rsi
callq 0x20aa0
incq %r15
jmp 0x351c2
movq 0x30(%rsp), %rcx
incq %rcx
movq 0x48(%rsp), %r14
jmp 0x351a5
movl $0xc8, %edi
callq 0x134f0
pushq $0x1
popq %rbp
movq %rax, %r12
leaq 0x68(%rsp), %rsi
movq %rax, %rdi
callq 0x297aa
movq %r12, %rdi
callq 0x2b540
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *0x10(%rax)
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
callq *0x28(%rax)
movq %r14, %rdi
callq 0x3424a
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq %r12, %rsi
callq 0x20aa0
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x3530f
orb $0x1, 0x1e8(%rax)
leaq 0x68(%rsp), %rdi
callq 0x2f6a8
movl %ebp, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xc8, %esi
movq %r12, %rdi
callq 0x13500
jmp 0x3535b
jmp 0x35358
jmp 0x35358
jmp 0x35358
jmp 0x35358
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x2f17a
jmp 0x3535b
jmp 0x35358
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x2f6a8
movq %rbx, %rdi
callq 0x138f0
nop