// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Surfels/R3SurfelNodeSet.cpp
// Function: gaps::R3SurfelNodeSet::InsertNodes(gaps::R3SurfelTree*, gaps::R3SurfelNode*, gaps::R3Point const&, double, double, double, double, double, double)
void R3SurfelNodeSet::
InsertNodes(R3SurfelTree *tree, R3SurfelNode *node,
  const R3Point& xycenter, RNLength xyradius, 
  RNCoord zmin, RNCoord zmax,
  RNScalar center_resolution, RNScalar perimeter_resolution,
  RNScalar focus_exponent)
{
  // Check if node is in infinite cylinder
  if (node->BBox().ZMin() > zmax) return;
  if (node->BBox().ZMax() < zmin) return;
  RNLength squared_xydistance = SquaredXYDistance(xycenter, node->BBox());
  if ((xyradius > 0) && (squared_xydistance > xyradius * xyradius)) return;

  // Check if this node is a leaf
  if (node->NParts() == 0) {
    // Simply insert leaf node
    InsertNode(node);
    return;
  }

  // Check if we are selecting interior nodes based on resolution
  if (center_resolution > 0) {
    // Compute the target resolution at this node position
    RNScalar target_resolution = center_resolution;
    if ((xyradius > 0) && (perimeter_resolution > 0)) {
      RNLength xydistance = sqrt(squared_xydistance);
      RNScalar t = 1 - xydistance / xyradius;
      target_resolution = perimeter_resolution + 
        (center_resolution - perimeter_resolution) * pow(t, focus_exponent);
    }

    // Check if this node is within a factor of two of target resolution
    if (node->Resolution() > 0.5 * target_resolution) {
      // Insert all parts of this node and don't recurse 
      for (int i = 0; i < node->NParts(); i++) InsertNode(node->Part(i));
      return;
    }
  }

  // Recurse to parts 
  for (int i = 0; i < node->NParts(); i++) {
    R3SurfelNode *part = node->Part(i);
    InsertNodes(tree, part, xycenter, xyradius, zmin, zmax, center_resolution, perimeter_resolution);
  }
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x40, %rsp
movsd %xmm5, 0x18(%rsp)
movsd %xmm4, 0x10(%rsp)
movsd %xmm3, 0x20(%rsp)
movsd %xmm2, 0x30(%rsp)
movsd %xmm1, 0x28(%rsp)
movsd %xmm0, 0x8(%rsp)
movq %rcx, %r15
movq %rdx, %rbx
movq %rsi, %r12
movq %rdi, %r14
movq %rdx, %rdi
callq 0x33720
movsd 0x18(%rax), %xmm0
ucomisd 0x30(%rsp), %xmm0
ja 0x349cf
movq %rbx, %rdi
callq 0x33720
movsd 0x28(%rsp), %xmm0
ucomisd 0x30(%rax), %xmm0
ja 0x349cf
movq %rbx, %rdi
callq 0x33720
movsd (%r15), %xmm0
leaq 0xf0307(%rip), %rcx     # 0x124c28
movsd (%rcx), %xmm2
movapd %xmm0, %xmm1
subsd 0x20(%rax), %xmm1
ucomisd %xmm2, %xmm1
ja 0x3495f
movsd 0x8(%rax), %xmm3
movapd %xmm0, %xmm4
subsd %xmm3, %xmm4
movapd 0x93a57(%rip), %xmm5    # 0xc83a0
xorpd %xmm2, %xmm5
xorpd %xmm1, %xmm1
ucomisd %xmm4, %xmm5
jbe 0x3495f
subsd %xmm0, %xmm3
movapd %xmm3, %xmm1
movsd 0x8(%r15), %xmm3
movapd %xmm3, %xmm5
subsd 0x28(%rax), %xmm5
ucomisd %xmm2, %xmm5
ja 0x34993
movsd 0x10(%rax), %xmm5
movapd %xmm3, %xmm4
subsd %xmm5, %xmm4
xorpd 0x93a17(%rip), %xmm2    # 0xc83a0
ucomisd %xmm4, %xmm2
jbe 0x349a7
subsd %xmm3, %xmm5
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm5
jne 0x34ac8
jp 0x34ac8
mulsd %xmm1, %xmm1
movapd %xmm1, %xmm5
xorpd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm2
ucomisd %xmm1, %xmm2
jbe 0x349dd
movsd 0x8(%rsp), %xmm1
mulsd %xmm1, %xmm1
ucomisd %xmm1, %xmm5
jbe 0x349dd
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
cmpl $0x0, 0x3c(%rbx)
je 0x34ae5
xorpd %xmm1, %xmm1
movsd 0x20(%rsp), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x34a76
movsd 0x8(%rsp), %xmm2
ucomisd %xmm1, %xmm2
movapd %xmm3, %xmm0
jbe 0x34a50
movsd 0x10(%rsp), %xmm2
ucomisd %xmm1, %xmm2
movapd %xmm3, %xmm0
jbe 0x34a50
sqrtsd %xmm5, %xmm1
divsd 0x8(%rsp), %xmm1
movsd 0x9371f(%rip), %xmm0    # 0xc8148
subsd %xmm1, %xmm0
subsd 0x10(%rsp), %xmm3
movsd %xmm3, 0x38(%rsp)
movsd 0x18(%rsp), %xmm1
callq 0x138e0
mulsd 0x38(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
callq 0x33680
movsd 0x18(%rsp), %xmm1
mulsd 0x936cc(%rip), %xmm1    # 0xc8138
ucomisd %xmm1, %xmm0
ja 0x34b01
xorl %r13d, %r13d
movslq 0x3c(%rbx), %rax
cmpq %rax, %r13
jge 0x349cf
movq 0x30(%rbx), %rax
movq (%rax,%r13,8), %rdx
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rcx
movsd 0x8(%rsp), %xmm0
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movsd 0x20(%rsp), %xmm3
movsd 0x10(%rsp), %xmm4
movsd 0x94058(%rip), %xmm5    # 0xc8b18
callq *0x30(%rax)
incq %r13
jmp 0x34a79
ucomisd %xmm2, %xmm1
mulsd %xmm5, %xmm5
jne 0x34ad8
jnp 0x349af
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm5
jmp 0x349af
movq (%r14), %rax
movq 0x38(%rax), %rax
movq %r14, %rdi
movq %rbx, %rsi
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmpq *%rax
xorl %r15d, %r15d
movslq 0x3c(%rbx), %rax
cmpq %rax, %r15
jge 0x349cf
movq 0x30(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq (%r14), %rax
movq %r14, %rdi
callq *0x38(%rax)
incq %r15
jmp 0x34b04
nop