// File: 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:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x108(%rsp)
movsd %xmm0, 0x100(%rsp)
movsd %xmm1, 0xf8(%rsp)
movsd %xmm2, 0xf0(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x78(%rsp)
xorps %xmm0, %xmm0
ucomisd 0xf8(%rsp), %xmm0
jb 0x62c35
movl $0x0, 0x114(%rsp)
jmp 0x630f9
movsd 0xf0(%rsp), %xmm0
movsd 0x141522(%rip), %xmm1   # 0x1a4168
ucomisd %xmm1, %xmm0
jb 0x62c5c
movl $0x0, 0x114(%rsp)
jmp 0x630f9
leaq 0xe0(%rsp), %rdi
callq 0x1b960
movl $0x0, 0xdc(%rsp)
movq 0x78(%rsp), %rdi
movl 0xdc(%rsp), %eax
movl %eax, 0x70(%rsp)
callq 0x16cf0
movl %eax, 0x74(%rsp)
jmp 0x62c8f
movl 0x70(%rsp), %eax
movl 0x74(%rsp), %ecx
cmpl %ecx, %eax
jge 0x62d05
movq 0x78(%rsp), %rdi
movl 0xdc(%rsp), %esi
callq 0x30a90
movq %rax, 0x68(%rsp)
jmp 0x62cb3
movq 0x68(%rsp), %rax
movq %rax, 0xc0(%rsp)
movq 0xc0(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x1bee0
jmp 0x62cd7
jmp 0x62cd9
movl 0xdc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xdc(%rsp)
jmp 0x62c74
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
jmp 0x630ea
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0x60(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x16d50
movl %eax, 0x64(%rsp)
jmp 0x62d2e
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
cmpl %ecx, %eax
jge 0x630d0
movl 0xbc(%rsp), %esi
leaq 0xe0(%rsp), %rdi
callq 0x310e0
movq %rax, 0x58(%rsp)
jmp 0x62d59
movq 0x78(%rsp), %rdi
movq 0x58(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rax
movq %rax, 0x48(%rsp)
callq 0x31030
movq %rax, 0x50(%rsp)
jmp 0x62d84
movq 0x48(%rsp), %rax
movq 0x50(%rsp), %rcx
cmpq %rcx, %rax
jne 0x62d98
jmp 0x630ba
movq 0xb0(%rsp), %rdi
callq 0x5ecf0
movsd %xmm0, 0x40(%rsp)
jmp 0x62dad
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm1
movsd 0xf8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x62dd9
jmp 0x630ba
movq 0xb0(%rsp), %rdi
callq 0x5ecb0
movsd %xmm0, 0x38(%rsp)
jmp 0x62dee
movsd 0x38(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm1
movsd 0x100(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x62e1a
jmp 0x630ba
movq 0xb0(%rsp), %rdi
callq 0x31100
movq %rax, 0x30(%rsp)
jmp 0x62e2e
movq 0x30(%rsp), %rax
movq %rax, 0x98(%rsp)
cmpq $0x0, 0x98(%rsp)
jne 0x62e4b
jmp 0x630ba
movq 0x98(%rsp), %rdi
callq 0x5ecf0
movsd %xmm0, 0x28(%rsp)
jmp 0x62e60
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x90(%rsp), %xmm0
jb 0x62e98
movsd 0xf0(%rsp), %xmm0
mulsd 0xf8(%rsp), %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x90(%rsp), %xmm0
divsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x88(%rsp), %xmm1
movsd 0xf0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x630b8
movl $0xd0, %edi
callq 0x13570
movq %rax, 0x20(%rsp)
jmp 0x62ee0
movq 0x20(%rsp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x5e770
jmp 0x62ef0
movq 0x78(%rsp), %rdi
movq 0x20(%rsp), %rax
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rsi
movq 0x98(%rsp), %rdx
callq 0x620c0
jmp 0x62f19
movq 0xb0(%rsp), %rdi
movq 0x80(%rsp), %rsi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
jmp 0x62f34
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %rsi
movsd 0xf0(%rsp), %xmm2
movsd 0xa8(%rsp), %xmm0
mulsd %xmm0, %xmm2
movq (%rdi), %rax
movq 0x60(%rax), %rax
movsd 0x141202(%rip), %xmm0   # 0x1a4168
xorps %xmm1, %xmm1
callq *%rax
movl %eax, 0x1c(%rsp)
jmp 0x62f71
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
jne 0x62ff4
movq 0xb0(%rsp), %rdi
movq 0x98(%rsp), %rsi
movq (%rdi), %rax
movq 0x20(%rax), %rax
callq *%rax
jmp 0x62f95
movq 0x78(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x62370
jmp 0x62fa9
movq 0x80(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpq $0x0, %rax
je 0x62fc7
movq 0x10(%rsp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
jmp 0x630b8
movq 0x20(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd0(%rsp)
movl %eax, 0xcc(%rsp)
movl $0xd0, %esi
callq 0x13580
jmp 0x630ea
movq 0x80(%rsp), %rax
movq %rax, 0xb0(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x5ecb0
movsd %xmm0, 0x8(%rsp)
jmp 0x63019
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm1
movsd 0x100(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x63042
jmp 0x630b8
movq 0xb0(%rsp), %rdi
callq 0x5ecf0
movsd %xmm0, (%rsp)
jmp 0x63056
movsd (%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm1
movsd 0xf8(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x6307e
jmp 0x630b8
movsd 0x90(%rsp), %xmm0
divsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x88(%rsp), %xmm1
movsd 0xf0(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x630b3
jmp 0x630b8
jmp 0x62eb3
jmp 0x630ba
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x62d10
movl $0x1, 0x114(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x1b9a0
jmp 0x630f9
leaq 0xe0(%rsp), %rdi
callq 0x1b9a0
jmp 0x63108
movl 0x114(%rsp), %eax
addq $0x118, %rsp            # imm = 0x118
retq
movq 0xd0(%rsp), %rdi
callq 0x13970
nopw %cs:(%rax,%rax)