// File: /workspace/github_cpp_makefile_star2_O2/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 $0x50, %rsp
movsd %xmm0, 0x18(%rsp)
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd %xmm1, 0x10(%rsp)
ucomisd %xmm1, %xmm0
jae 0x35593
ucomisd 0x92d93(%rip), %xmm2    # 0xc8148
jae 0x35593
movq %rdi, %rbx
movsd %xmm2, 0x8(%rsp)
leaq 0x20(%rsp), %r14
movq %r14, %rdi
callq 0x9a7ae
xorl %r15d, %r15d
movslq 0x24(%rbx), %rax
cmpq %rax, %r15
jge 0x353f2
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq %r14, %rdi
callq 0x1784c
incq %r15
jmp 0x353d4
movsd 0x10(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
xorl %r13d, %r13d
movslq 0x2c(%rsp), %rax
cmpq %rax, %r13
jge 0x35586
movq 0x20(%rsp), %rax
movq (%rax,%r13,8), %r15
movq 0x18(%rbx), %rax
cmpq (%rax), %r15
je 0x3557e
movq %r15, %rdi
callq 0x33680
movapd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
movsd %xmm1, (%rsp)
ucomisd %xmm1, %xmm0
jae 0x3557e
movq %r15, %rdi
callq 0x3365c
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3557e
movq 0x28(%r15), %r14
testq %r14, %r14
je 0x3557e
movq %r14, %rdi
callq 0x33680
movapd %xmm0, %xmm2
cmpnlesd 0x92efa(%rip), %xmm2    # 0xc8380
movapd %xmm2, %xmm1
andpd %xmm0, %xmm1
andnpd 0x30(%rsp), %xmm2
orpd %xmm1, %xmm2
movapd %xmm2, 0x40(%rsp)
movapd %xmm2, %xmm0
divsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
jbe 0x3557e
movl $0xd0, %edi
callq 0x134f0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
callq 0x33394
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x34e72
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 0x92c40(%rip), %xmm0    # 0xc8148
callq *0x60(%rax)
testl %eax, %eax
je 0x3555d
movq %r12, %rdi
callq 0x3365c
movsd 0x18(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x3557e
movq %r12, %rdi
callq 0x33680
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
ucomisd (%rsp), %xmm0
ja 0x3557e
movapd 0x40(%rsp), %xmm0
divsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq %r12, %r15
jbe 0x354a7
jmp 0x3557e
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x34fc0
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
incq %r13
jmp 0x35407
leaq 0x20(%rsp), %rdi
callq 0x9a81c
pushq $0x1
popq %rax
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x355bd
jmp 0x355bd
jmp 0x355bd
jmp 0x355bd
movq %rax, %rbx
movl $0xd0, %esi
movq %r12, %rdi
callq 0x13500
jmp 0x355c0
jmp 0x355bd
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x9a81c
movq %rbx, %rdi
callq 0x138f0