// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelTree.cpp
// Function: gaps::R3SurfelTree::CreateMultiresolutionNodes(double, double, double)
int R3SurfelTree::
CreateMultiresolutionNodes(RNScalar min_complexity, RNScalar min_resolution, RNScalar min_multiresolution_factor)
{
  // Just checking
  if (min_resolution <= 0) return 0;
  if (min_multiresolution_factor >= 1) return 0;

  // Make copy of list of nodes (since will change as edit)
  RNArray<R3SurfelNode *> copy;
  for (int i = 0; i < NNodes(); i++) {
    R3SurfelNode *node = Node(i);
    copy.Insert(node);
  }

  // Insert extra levels of tree based on resolution differences
  for (int i = 0; i < copy.NEntries(); i++) {
    R3SurfelNode *node = copy.Kth(i);
    if (node == RootNode()) continue;
    RNScalar node_resolution = node->Resolution();
    if (node_resolution <= min_resolution) continue;
    RNScalar node_complexity = node->Complexity();
    if (node_complexity < min_complexity) continue;
    R3SurfelNode *parent = node->Parent();
    if (!parent) continue;
    RNScalar parent_resolution = parent->Resolution();
    if (parent_resolution <= 0) parent_resolution = min_multiresolution_factor * min_resolution;
    RNScalar multiresolution_factor = parent_resolution / node_resolution;
    while (multiresolution_factor < min_multiresolution_factor) {
      // Create a new node and insert into tree between parent and node
      R3SurfelNode *new_node = new R3SurfelNode();
      InsertNode(new_node, parent);
      node->SetParent(new_node);

      // Create multiresoluiton blocks for new node
      if (!CreateMultiresolutionBlocks(new_node, 1, 0, min_multiresolution_factor * node_resolution)) {
        // Unroll changes
        node->SetParent(parent);
        RemoveNode(new_node);
        delete new_node;
        break;
      }

      // Update everything
      node = new_node;
      node_complexity = node->Complexity();
      if (node_complexity < min_complexity) break;
      node_resolution = node->Resolution();
      if (node_resolution < min_resolution) break;
      multiresolution_factor = parent_resolution / node_resolution;  
      if (multiresolution_factor < min_multiresolution_factor) break;
    }
  }

  // Return success
  return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x60, %rsp
movsd %xmm0, 0x28(%rsp)
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd %xmm1, 0x10(%rsp)
ucomisd %xmm1, %xmm0
jae 0x39b27
movsd %xmm2, 0x8(%rsp)
ucomisd 0xa482b(%rip), %xmm2    # 0xde150
jae 0x39b27
movq %rdi, %rbx
leaq 0x18(%rsp), %rdi
callq 0xaaf34
cmpl $0x0, 0x24(%rbx)
jle 0x39966
xorl %r15d, %r15d
leaq 0x18(%rsp), %r14
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rsi
movl 0x24(%rsp), %edx
movq %r14, %rdi
callq 0xaafca
incq %r15
movslq 0x24(%rbx), %rax
cmpq %rax, %r15
jl 0x39946
cmpl $0x0, 0x24(%rsp)
jle 0x39b18
movsd 0x10(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
movapd %xmm0, 0x40(%rsp)
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq (%rax,%r13,8), %r15
movq 0x18(%rbx), %rax
cmpq (%rax), %r15
je 0x39b07
movq %r15, %rdi
callq 0x37730
movapd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
movsd %xmm1, (%rsp)
ucomisd %xmm1, %xmm0
jae 0x39b07
movq %r15, %rdi
callq 0x3770e
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x39b07
movq 0x28(%r15), %r14
testq %r14, %r14
je 0x39b07
movq %r14, %rdi
callq 0x37730
movapd %xmm0, %xmm2
cmpnlesd 0xa4991(%rip), %xmm2    # 0xde388
movapd %xmm2, %xmm1
andpd %xmm0, %xmm1
andnpd 0x40(%rsp), %xmm2
orpd %xmm1, %xmm2
movapd %xmm2, 0x50(%rsp)
divsd (%rsp), %xmm2
movapd %xmm2, 0x30(%rsp)
movsd 0x8(%rsp), %xmm0
ucomisd 0x30(%rsp), %xmm0
jbe 0x39b07
movl $0xd0, %edi
callq 0x134e0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
callq 0x37448
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x39272
movq (%r15), %rax
movq %r15, %rdi
movq %r12, %rsi
callq *0x20(%rax)
movsd (%rsp), %xmm2
mulsd 0x8(%rsp), %xmm2
movq (%rbx), %rax
xorpd %xmm1, %xmm1
movq %rbx, %rdi
movq %r12, %rsi
movsd 0xa46d3(%rip), %xmm0    # 0xde150
callq *0x60(%rax)
testl %eax, %eax
je 0x39aba
movq %r12, %rdi
callq 0x3770e
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x39ab6
movq %r12, %rdi
callq 0x37730
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
movsd (%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x39adf
xorl %eax, %eax
jmp 0x39afc
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x393e0
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
xorl %eax, %eax
jmp 0x39aff
movapd 0x50(%rsp), %xmm2
divsd %xmm1, %xmm2
movsd 0x8(%rsp), %xmm0
movapd %xmm2, 0x30(%rsp)
ucomisd %xmm2, %xmm0
setbe %al
movq %r12, %r15
testb %al, %al
jne 0x39a1a
incq %r13
movslq 0x24(%rsp), %rax
cmpq %rax, %r13
jl 0x39986
leaq 0x18(%rsp), %rdi
callq 0xaaf9e
movl $0x1, %eax
addq $0x60, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x39b4f
jmp 0x39b4f
jmp 0x39b4f
jmp 0x39b4f
movq %rax, %rbx
movl $0xd0, %esi
movq %r12, %rdi
callq 0x134f0
jmp 0x39b52
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0xaaf9e
movq %rbx, %rdi
callq 0x138b0