// File: /workspace/github_cpp_makefile_star2_O3/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 0x3755c
movsd 0x18(%rax), %xmm0
ucomisd 0x30(%rsp), %xmm0
ja 0x38b48
movq %rbx, %rdi
callq 0x3755c
movsd 0x28(%rsp), %xmm0
ucomisd 0x30(%rax), %xmm0
ja 0x38b48
movq %rbx, %rdi
callq 0x3755c
movsd (%r15), %xmm0
movsd 0x8(%r15), %xmm2
leaq 0xfb2c7(%rip), %rcx     # 0x133c08
movsd (%rcx), %xmm3
movapd %xmm0, %xmm1
subsd 0x20(%rax), %xmm1
ucomisd %xmm3, %xmm1
ja 0x3897f
movsd 0x8(%rax), %xmm4
movapd %xmm0, %xmm5
subsd %xmm4, %xmm5
movapd 0xa4a37(%rip), %xmm6    # 0xdd3a0
xorpd %xmm3, %xmm6
xorpd %xmm1, %xmm1
ucomisd %xmm5, %xmm6
jbe 0x3897f
subsd %xmm0, %xmm4
movapd %xmm4, %xmm1
movapd %xmm2, %xmm5
subsd 0x28(%rax), %xmm5
ucomisd %xmm3, %xmm5
ja 0x389ad
movsd 0x10(%rax), %xmm5
movapd %xmm2, %xmm4
subsd %xmm5, %xmm4
xorpd 0xa49fd(%rip), %xmm3    # 0xdd3a0
ucomisd %xmm4, %xmm3
jbe 0x389c1
subsd %xmm2, %xmm5
xorpd %xmm2, %xmm2
ucomisd %xmm2, %xmm5
jne 0x38aeb
jp 0x38aeb
mulsd %xmm1, %xmm1
movapd %xmm1, %xmm5
movsd 0x8(%rsp), %xmm2
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm2
jbe 0x389eb
movapd %xmm2, %xmm1
mulsd %xmm2, %xmm1
ucomisd %xmm1, %xmm5
ja 0x38b48
movl 0x3c(%rbx), %eax
testl %eax, %eax
je 0x38b08
xorpd %xmm1, %xmm1
movsd 0x20(%rsp), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x38a8f
movsd 0x8(%rsp), %xmm2
ucomisd %xmm1, %xmm2
movapd %xmm3, %xmm0
jbe 0x38a66
movsd 0x10(%rsp), %xmm2
ucomisd %xmm1, %xmm2
movapd %xmm3, %xmm0
jbe 0x38a66
xorps %xmm1, %xmm1
sqrtsd %xmm5, %xmm1
divsd 0x8(%rsp), %xmm1
movsd 0xa4709(%rip), %xmm0    # 0xdd148
subsd %xmm1, %xmm0
subsd 0x10(%rsp), %xmm3
movsd %xmm3, 0x38(%rsp)
movsd 0x18(%rsp), %xmm1
callq 0x138a0
mulsd 0x38(%rsp), %xmm0
addsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %rbx, %rdi
callq 0x37452
movsd 0x18(%rsp), %xmm1
mulsd 0xa46b6(%rip), %xmm1    # 0xdd138
ucomisd %xmm1, %xmm0
movl 0x3c(%rbx), %eax
ja 0x38b24
testl %eax, %eax
movsd 0x8(%rsp), %xmm0
jle 0x38b48
xorl %r13d, %r13d
movq 0x30(%rbx), %rax
movq (%rax,%r13,8), %rdx
movq (%r14), %rax
movq %r14, %rdi
movq %r12, %rsi
movq %r15, %rcx
movsd 0x28(%rsp), %xmm1
movsd 0x30(%rsp), %xmm2
movsd 0x20(%rsp), %xmm3
movsd 0x10(%rsp), %xmm4
movsd 0xa504c(%rip), %xmm5    # 0xddb20
callq *0x30(%rax)
movsd 0x8(%rsp), %xmm0
incq %r13
movslq 0x3c(%rbx), %rax
cmpq %rax, %r13
jl 0x38aa0
jmp 0x38b48
ucomisd %xmm2, %xmm1
mulsd %xmm5, %xmm5
jne 0x38afb
jnp 0x389c9
mulsd %xmm1, %xmm1
addsd %xmm1, %xmm5
jmp 0x389c9
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
testl %eax, %eax
jle 0x38b48
xorl %r15d, %r15d
movq 0x30(%rbx), %rax
movq (%rax,%r15,8), %rsi
movq (%r14), %rax
movq %r14, %rdi
callq *0x38(%rax)
incq %r15
movslq 0x3c(%rbx), %rax
cmpq %rax, %r15
jl 0x38b2b
addq $0x40, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq