// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3SurfelViewer.cpp
// Function: gaps::R3SurfelViewer::ReleaseCoarsestBlocks(double)
void R3SurfelViewer::
ReleaseCoarsestBlocks(RNScalar max_complexity)
{
  // Just checking
  if (!scene) return;

  // Get convenient variables
  R3SurfelTree *tree = scene->Tree();
  if (!tree) return;

  // Seed breadth first search with root nodes
  RNQueue<R3SurfelNode *> queue;
  for (int i = 0; i < tree->NNodes(); i++) {
    R3SurfelNode *node = tree->Node(i);
    queue.Insert(node);
  }

  // Visit nodes in breadth first search reading blocks
  RNScalar total_complexity = 0;
  while (!queue.IsEmpty()) {
    R3SurfelNode *node = queue.Pop();
    if (total_complexity + node->Complexity() > max_complexity) break;
    total_complexity += node->Complexity();
    node->ReleaseBlocks();
    for (int i = 0; i < node->NParts(); i++) {
      R3SurfelNode *part = node->Part(i);
      queue.Push(part);
    }
  }

  // Invalidate VBO buffers
  InvalidateVBO();
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0xc0(%rsp)
movsd %xmm0, 0xb8(%rsp)
movq 0xc0(%rsp), %rax
movq %rax, 0x50(%rsp)
cmpq $0x0, 0x8(%rax)
jne 0x38e31
jmp 0x39063
movq 0x50(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x2f020
movq %rax, 0xb0(%rsp)
cmpq $0x0, 0xb0(%rsp)
jne 0x38e57
jmp 0x39063
leaq 0x98(%rsp), %rdi
callq 0x3eaf0
movl $0x0, 0x94(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x48(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x2f030
movl %eax, 0x4c(%rsp)
jmp 0x38e8d
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x38f0d
movq 0xb0(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x2f1d0
movq %rax, 0x40(%rsp)
jmp 0x38eb4
movq 0x40(%rsp), %rax
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x3eb00
jmp 0x38ed2
jmp 0x38ed4
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x38e6f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x3ec00
jmp 0x3906b
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
leaq 0x98(%rsp), %rdi
callq 0x3eb50
movl %eax, 0x3c(%rsp)
jmp 0x38f29
movl 0x3c(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x38f3e
jmp 0x39043
leaq 0x98(%rsp), %rdi
callq 0x3eb70
movq %rax, 0x30(%rsp)
jmp 0x38f52
movq 0x30(%rsp), %rax
movq %rax, 0x68(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x68(%rsp), %rdi
callq 0x58960
movsd %xmm0, 0x28(%rsp)
jmp 0x38f7a
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm0
addsd %xmm1, %xmm0
ucomisd 0xb8(%rsp), %xmm0
jbe 0x38f9a
jmp 0x39043
movq 0x68(%rsp), %rdi
callq 0x58960
movsd %xmm0, 0x18(%rsp)
jmp 0x38fac
movsd 0x18(%rsp), %xmm1
movsd 0x70(%rsp), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x68(%rsp), %rdi
xorl %esi, %esi
callq 0x59bd0
jmp 0x38fd0
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x10(%rsp)
movq 0x68(%rsp), %rdi
callq 0x2f260
movl %eax, 0x14(%rsp)
jmp 0x38ff0
movl 0x10(%rsp), %eax
movl 0x14(%rsp), %ecx
cmpl %ecx, %eax
jge 0x3903e
movq 0x68(%rsp), %rdi
movl 0x64(%rsp), %esi
callq 0x3eba0
movq %rax, 0x8(%rsp)
jmp 0x39011
movq 0x8(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rsi
leaq 0x98(%rsp), %rdi
callq 0x3ebd0
jmp 0x3902f
jmp 0x39031
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x38fd8
jmp 0x38f16
movq 0x50(%rsp), %rdi
movq (%rdi), %rax
movq 0x378(%rax), %rax
callq *%rax
jmp 0x39056
leaq 0x98(%rsp), %rdi
callq 0x3ec00
addq $0xc8, %rsp
retq
movq 0x88(%rsp), %rdi
callq 0x292e0
nopl (%rax,%rax)