// File: /workspace/github_cpp_makefile_star2_O1/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, 0x18(%rsp)
movq %rdi, 0x8(%rsp)
cmpl $0x0, 0x3c(%rsi)
je 0x3987e
movq %rsi, %r12
cmpl $0x0, 0x3c(%rsi)
setg %bl
movapd %xmm0, 0x40(%rsp)
jle 0x39523
movq 0x30(%r12), %rax
movq (%rax), %rsi
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movsd 0x18(%rsp), %xmm1
movapd 0x20(%rsp), %xmm2
callq *0x60(%rax)
testl %eax, %eax
je 0x39519
movl $0x1, %r14d
movslq 0x3c(%r12), %rax
cmpq %rax, %r14
setl %bl
movapd 0x40(%rsp), %xmm0
jge 0x39523
movq 0x30(%r12), %rax
movq (%rax,%r14,8), %rsi
movq 0x8(%rsp), %rdi
movq (%rdi), %rax
movsd 0x18(%rsp), %xmm1
movapd 0x20(%rsp), %xmm2
callq *0x60(%rax)
incq %r14
testl %eax, %eax
jne 0x394df
xorl %eax, %eax
testb %bl, %bl
jne 0x39883
cmpl $0x0, 0x4c(%r12)
jg 0x3987e
cmpl $0x0, 0x3c(%r12)
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
xorpd %xmm4, %xmm4
jle 0x395a1
xorl %ebx, %ebx
movsd %xmm0, 0x10(%rsp)
movsd %xmm4, 0x30(%rsp)
movq 0x30(%r12), %rax
movq (%rax,%rbx,8), %r15
movq %r15, %rdi
callq 0x37730
xorps %xmm1, %xmm1
cvtsi2sdl 0x3c(%r12), %xmm1
divsd %xmm1, %xmm0
movsd 0x10(%rsp), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
movq %r15, %rdi
callq 0x3770e
movsd 0x30(%rsp), %xmm4
addsd %xmm0, %xmm4
incq %rbx
movslq 0x3c(%r12), %rax
cmpq %rax, %rbx
jl 0x3954d
movsd 0x10(%rsp), %xmm5
ucomisd 0xa4dd9(%rip), %xmm4    # 0xde388
jne 0x395b7
jnp 0x3987e
xorpd %xmm0, %xmm0
ucomisd %xmm0, %xmm5
jne 0x395c7
jnp 0x3987e
movapd 0x40(%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 0x395f7
movapd %xmm3, 0x20(%rsp)
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3961f
ucomisd %xmm0, %xmm4
jbe 0x3961f
mulsd 0x18(%rsp), %xmm5
divsd %xmm4, %xmm5
minsd 0x20(%rsp), %xmm5
movapd %xmm5, 0x20(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x329b8
cmpl $0x0, 0x3c(%r12)
jle 0x397b4
xorl %ecx, %ecx
leaq 0x58(%rsp), %r15
movq %r12, 0x38(%rsp)
movq 0x30(%r12), %rax
movq %rcx, 0x30(%rsp)
movq (%rax,%rcx,8), %r14
cmpl $0x0, 0x4c(%r14)
jle 0x39799
xorl %ebx, %ebx
movq 0x40(%r14), %rax
movq (%rax,%rbx,8), %r13
cmpl $0x0, 0xb4(%r13)
jne 0x3968e
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq 0x8(%rdi), %rdx
movl 0x28(%rdi), %ecx
movq %r13, %rsi
callq *0x88(%rax)
testl %eax, %eax
je 0x39695
incl 0xb4(%r13)
movq %r13, %rdi
callq 0x2d4ca
ucomisd 0xa4ce3(%rip), %xmm0    # 0xde388
jne 0x396ad
jnp 0x39789
movapd 0x20(%rsp), %xmm1
divsd %xmm0, %xmm1
movsd %xmm1, 0x10(%rsp)
ucomisd 0xa4a8b(%rip), %xmm1    # 0xde150
jae 0x39719
cmpl $0x0, 0x8(%r13)
jle 0x39726
xorl %ebp, %ebp
xorl %r12d, %r12d
callq 0xacf78
ucomisd 0x10(%rsp), %xmm0
ja 0x39707
movq (%r13), %rdx
addq %rbp, %rdx
movq %r15, %rdi
movq %r13, %rsi
callq 0x323c4
leaq 0x68(%rsp), %rdi
movq %r15, %rsi
callq 0x345d6
movq %r15, %rdi
callq 0x32470
incq %r12
movslq 0x8(%r13), %rax
addq $0x30, %rbp
cmpq %rax, %r12
jl 0x396d3
jmp 0x39726
leaq 0x68(%rsp), %rdi
movq %r13, %rsi
callq 0x3302e
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rbp
cmpl $0x1, 0xb4(%r13)
jne 0x39754
movq (%rbp), %rax
movq 0x8(%rbp), %rdx
movl 0x28(%rbp), %ecx
movq %rbp, %rdi
movq %r13, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x39789
decl 0xb4(%r13)
jne 0x39789
testb $0x2, 0x89(%r13)
je 0x39789
movq (%rbp), %rax
movq %rbp, %rdi
movq %r13, %rsi
callq *0x18(%rax)
movq %r13, %rdi
callq 0x2ce2e
movl $0xc8, %esi
movq %r13, %rdi
callq 0x134f0
incq %rbx
movslq 0x4c(%r14), %rax
cmpq %rax, %rbx
jl 0x3965c
movq 0x30(%rsp), %rcx
incq %rcx
movq 0x38(%rsp), %r12
movslq 0x3c(%r12), %rax
cmpq %rax, %rcx
jl 0x39641
movl $0xc8, %edi
callq 0x134e0
movq %rax, %r15
leaq 0x68(%rsp), %rsi
movq %rax, %rdi
callq 0x2bc76
movq %r15, %rdi
callq 0x2e364
movq 0x8(%rsp), %rax
movq 0x10(%rax), %rdi
movq (%rdi), %rax
movq %r15, %rsi
callq *0x10(%rax)
movq (%r12), %rax
movq %r12, %rdi
movq %r15, %rsi
callq *0x28(%rax)
movq %r12, %rdi
callq 0x384ee
movq 0x8(%rsp), %rax
movq 0x10(%rax), %r14
cmpl $0x1, 0xb4(%r15)
jne 0x3982b
movq (%r14), %rax
movq 0x8(%r14), %rdx
movl 0x28(%r14), %ecx
movq %r14, %rdi
movq %r15, %rsi
callq *0x90(%rax)
testl %eax, %eax
je 0x3985f
decl 0xb4(%r15)
jne 0x3985f
testb $0x2, 0x89(%r15)
je 0x3985f
movq (%r14), %rax
movq %r14, %rdi
movq %r15, %rsi
callq *0x18(%rax)
movq %r15, %rdi
callq 0x2ce2e
movl $0xc8, %esi
movq %r15, %rdi
callq 0x134f0
movq 0x8(%rsp), %rax
movq 0x8(%rax), %rax
testq %rax, %rax
je 0x39874
orb $0x1, 0x1e8(%rax)
leaq 0x68(%rsp), %rdi
callq 0x32b34
movl $0x1, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0xc8, %esi
movq %r15, %rdi
callq 0x134f0
jmp 0x398c3
jmp 0x398c0
jmp 0x398c0
jmp 0x398c0
jmp 0x398c0
jmp 0x398c0
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x32470
jmp 0x398c3
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x32b34
movq %rbx, %rdi
callq 0x138b0
nop