// File: /workspace/github_cpp_makefile_star2_O3/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)
movl $0x1, %ebp
cmpl $0x0, 0x3c(%rsi)
jle 0x394e1
movq %rsi, %r15
xorl %ebx, %ebx
movapd %xmm0, 0x30(%rsp)
movq 0x30(%r15), %rax
movq (%rax,%rbx,8), %rsi
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movsd 0x10(%rsp), %xmm1
movapd 0x20(%rsp), %xmm2
callq *0x60(%rax)
testl %eax, %eax
je 0x394df
incq %rbx
movslq 0x3c(%r15), %rax
cmpq %rax, %rbx
movapd 0x30(%rsp), %xmm0
jl 0x39130
testl %eax, %eax
setle %al
cmpl $0x0, 0x4c(%r15)
setg %cl
orb %al, %cl
jne 0x394e1
xorpd %xmm4, %xmm4
xorl %r14d, %r14d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd %xmm4, 0x50(%rsp)
movq 0x30(%r15), %rax
movq (%rax,%r14,8), %rbx
movq %rbx, %rdi
callq 0x37452
xorps %xmm1, %xmm1
cvtsi2sdl 0x3c(%r15), %xmm1
divsd %xmm1, %xmm0
movsd 0x18(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x18(%rsp)
movq %rbx, %rdi
callq 0x3742e
movsd 0x50(%rsp), %xmm4
addsd %xmm0, %xmm4
incq %r14
movslq 0x3c(%r15), %rax
cmpq %rax, %r14
jl 0x3918f
movsd 0x18(%rsp), %xmm5
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm4
jne 0x391f6
jnp 0x394f5
ucomisd %xmm0, %xmm5
movl $0x1, %ebp
jne 0x39207
jnp 0x394e1
movapd 0x30(%rsp), %xmm3
mulsd %xmm5, %xmm3
xorpd %xmm1, %xmm1
movapd 0x20(%rsp), %xmm2
cmpltpd %xmm2, %xmm1
cmpltpd %xmm3, %xmm2
andpd %xmm1, %xmm2
movd %xmm2, %eax
testb $0x1, %al
jne 0x39237
movapd %xmm3, 0x20(%rsp)
movsd 0x10(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3925f
ucomisd %xmm0, %xmm4
jbe 0x3925f
mulsd 0x10(%rsp), %xmm5
divsd %xmm4, %xmm5
minsd 0x20(%rsp), %xmm5
movapd %xmm5, 0x20(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x32620
movl 0x3c(%r15), %eax
testl %eax, %eax
jle 0x39401
xorl %edx, %edx
leaq 0x58(%rsp), %r12
movq %r15, 0x48(%rsp)
movq 0x30(%r15), %rcx
movq (%rcx,%rdx,8), %r14
cmpl $0x0, 0x4c(%r14)
jle 0x393f2
movq %rdx, 0x30(%rsp)
xorl %r15d, %r15d
movq 0x40(%r14), %rax
movq (%rax,%r15,8), %rbp
movl 0xb4(%rbp), %eax
testl %eax, %eax
jne 0x392d4
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %rbp, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x392dc
movl 0xb4(%rbp), %eax
incl %eax
movl %eax, 0xb4(%rbp)
movq %rbp, %rdi
callq 0x2d0ea
ucomisd 0xa409c(%rip), %xmm0    # 0xdd388
jne 0x392f4
jnp 0x393d4
movapd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
ucomisd 0xa3e3c(%rip), %xmm1    # 0xdd148
jae 0x3935f
cmpl $0x0, 0x8(%rbp)
jle 0x3936c
xorl %ebx, %ebx
xorl %r13d, %r13d
callq 0xaba84
ucomisd 0x10(%rsp), %xmm0
ja 0x3934d
movq (%rbp), %rdx
addq %rbx, %rdx
movq %r12, %rdi
movq %rbp, %rsi
callq 0x31fda
leaq 0x68(%rsp), %rdi
movq %r12, %rsi
callq 0x342e2
movq %r12, %rdi
callq 0x3209a
incq %r13
movslq 0x8(%rbp), %rax
addq $0x30, %rbx
cmpq %rax, %r13
jl 0x39319
jmp 0x3936c
leaq 0x68(%rsp), %rdi
movq %rbp, %rsi
callq 0x32c90
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbx
movl 0xb4(%rbp), %eax
cmpl $0x1, %eax
jne 0x393a0
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movl 0x28(%rbx), %ecx
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x393d4
movl 0xb4(%rbp), %eax
decl %eax
movl %eax, 0xb4(%rbp)
jne 0x393d4
testb $0x2, 0x89(%rbp)
je 0x393d4
movq (%rbx), %rax
movq %rbx, %rdi
movq %rbp, %rsi
callq *0x18(%rax)
movq %rbp, %rdi
callq 0x2ca06
movl $0xc8, %esi
movq %rbp, %rdi
callq 0x134f0
incq %r15
movslq 0x4c(%r14), %rax
cmpq %rax, %r15
jl 0x3929c
movq 0x48(%rsp), %r15
movl 0x3c(%r15), %eax
movq 0x30(%rsp), %rdx
incq %rdx
movslq %eax, %rcx
cmpq %rcx, %rdx
jl 0x39281
movl $0xc8, %edi
callq 0x134e0
movl $0x1, %ebp
movq %rax, %r12
leaq 0x68(%rsp), %rsi
movq %rax, %rdi
callq 0x2b84e
movq %r12, %rdi
callq 0x2dfb8
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq %r12, %rsi
callq *0x10(%rax)
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0x28(%rax)
movq %r15, %rdi
callq 0x3818c
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbx
movl 0xb4(%r12), %eax
cmpl $0x1, %eax
jne 0x39486
movq (%rbx), %rax
movq 0x8(%rbx), %rdx
movl 0x28(%rbx), %ecx
movq %rbx, %rdi
movq %r12, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x394be
movl 0xb4(%r12), %eax
decl %eax
movl %eax, 0xb4(%r12)
jne 0x394be
testb $0x2, 0x89(%r12)
je 0x394be
movq (%rbx), %rax
movq %rbx, %rdi
movq %r12, %rsi
callq *0x18(%rax)
movq %r12, %rdi
callq 0x2ca06
movl $0xc8, %esi
movq %r12, %rdi
callq 0x134f0
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x394d3
orb $0x1, 0x1e8(%rax)
leaq 0x68(%rsp), %rdi
callq 0x3279c
jmp 0x394e1
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %ebp
jmp 0x394e1
movq %rax, %rbx
movl $0xc8, %esi
movq %r12, %rdi
callq 0x134f0
jmp 0x3952a
jmp 0x39527
jmp 0x39527
jmp 0x39527
jmp 0x39527
jmp 0x39527
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x3209a
jmp 0x3952a
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3279c
movq %rbx, %rdi
callq 0x138b0