// File: /workspace/github_cpp_makefile_star2_O3/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, 0x28(%rsp)
xorl %eax, %eax
xorpd %xmm0, %xmm0
movsd %xmm1, 0x10(%rsp)
ucomisd %xmm1, %xmm0
jae 0x39775
ucomisd 0xa3bc3(%rip), %xmm2    # 0xdd148
jae 0x39775
movq %rdi, %rbx
movsd %xmm2, 0x8(%rsp)
leaq 0x18(%rsp), %rdi
callq 0xa9ca8
cmpl $0x0, 0x24(%rbx)
jle 0x395cc
xorl %r15d, %r15d
leaq 0x18(%rsp), %r14
movq 0x18(%rbx), %rax
movq (%rax,%r15,8), %rsi
movl 0x24(%rsp), %edx
movq %r14, %rdi
callq 0xa9d3e
incq %r15
movslq 0x24(%rbx), %rax
cmpq %rax, %r15
jl 0x395ac
cmpl $0x0, 0x24(%rsp)
jle 0x39766
movsd 0x10(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
movapd %xmm0, 0x30(%rsp)
xorl %r13d, %r13d
movq 0x18(%rsp), %rax
movq (%rax,%r13,8), %r15
movq 0x18(%rbx), %rax
cmpq (%rax), %r15
je 0x39755
movq %r15, %rdi
callq 0x37452
movapd %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
movsd %xmm1, (%rsp)
ucomisd %xmm1, %xmm0
jae 0x39755
movq %r15, %rdi
callq 0x3742e
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x39755
movq 0x28(%r15), %r14
testq %r14, %r14
je 0x39755
movq %r14, %rdi
callq 0x37452
movapd %xmm0, %xmm2
cmpnlesd 0xa3d2b(%rip), %xmm2    # 0xdd388
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 0x39755
movl $0xd0, %edi
callq 0x134e0
movq %rax, %r12
movq %rax, %rdi
xorl %esi, %esi
callq 0x3715c
movq %rbx, %rdi
movq %r12, %rsi
movq %r14, %rdx
callq 0x38f02
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 0xa3a69(%rip), %xmm0    # 0xdd148
callq *0x60(%rax)
testl %eax, %eax
je 0x39734
movq %r12, %rdi
callq 0x3742e
movsd 0x28(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x39755
movq %r12, %rdi
callq 0x37452
movsd %xmm0, (%rsp)
movsd 0x10(%rsp), %xmm0
ucomisd (%rsp), %xmm0
ja 0x39755
movapd 0x40(%rsp), %xmm0
divsd (%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
movq %r12, %r15
jbe 0x3967e
jmp 0x39755
movq (%r15), %rax
movq %r15, %rdi
movq %r14, %rsi
callq *0x20(%rax)
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3905c
movq (%r12), %rax
movq %r12, %rdi
callq *0x8(%rax)
incq %r13
movslq 0x24(%rsp), %rax
cmpq %rax, %r13
jl 0x395ec
leaq 0x18(%rsp), %rdi
callq 0xa9d12
movl $0x1, %eax
addq $0x50, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x3979f
jmp 0x3979f
jmp 0x3979f
jmp 0x3979f
movq %rax, %rbx
movl $0xd0, %esi
movq %r12, %rdi
callq 0x134f0
jmp 0x397a2
jmp 0x3979f
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0xa9d12
movq %rbx, %rdi
callq 0x138b0