// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3SegmentationCluster::InsertChild(gaps::R3SegmentationCluster*)
void R3SegmentationCluster::
InsertChild(R3SegmentationCluster *child)
{
  // Check
  assert(child != this);

  // Update area
  area += child->area;

  // Update color
  int n = points.NEntries() + child->points.NEntries();
  color *= points.NEntries();
  color += child->points.NEntries() * child->color;
  color = (n > 0) ? color / n : RNblack_rgb;

  // Update timestamp
  timestamp *= points.NEntries();
  timestamp += child->points.NEntries() * child->timestamp;
  timestamp = (n > 0) ? timestamp / n : 0;

  // Update primitive
  primitive.Update(this->primitive, child->primitive, this->points.NEntries(), child->points.NEntries());

  // Update category info
  if (points.NEntries() + child->points.NEntries() > 0) {
    if (category_identifier == child->category_identifier) {
      // Categories match -- average confidences
      category_confidence = (points.NEntries() * category_confidence + child->points.NEntries() * child->category_confidence);
      category_confidence /= points.NEntries() + child->points.NEntries();
    }
    else {
      // Categories don't match -- trust larger cluster
      if ((child->category_identifier < 0) || (points.NEntries() > child->points.NEntries())) {
        category_confidence = points.NEntries() * category_confidence;
        category_confidence /= points.NEntries() + child->points.NEntries();
      }
      else {
        category_identifier = child->category_identifier;
        category_confidence += child->points.NEntries() * child->category_confidence;
        category_confidence /= points.NEntries() + child->points.NEntries();
      }
    }
  }

  // Update affinities for current points
  if (points.NEntries() < 4 * child->points.NEntries()) {
    for (int i = 0; i < points.NEntries(); i++) {
      R3SegmentationPoint *point = points.Kth(i);
      RNScalar affinity = Affinity(point);
      if (affinity < 0) affinity = 0;
      possible_affinity += affinity - point->cluster_affinity;
      total_affinity += affinity - point->cluster_affinity;
      point->cluster_affinity = affinity;
    }
  }

  // Insert points from child
  while (!child->points.IsEmpty()) {
    R3SegmentationPoint *point = child->points.Tail();
    child->RemovePoint(point);
    RNScalar affinity = Affinity(point);
    if (affinity < 0) affinity = 0;
    possible_affinity += affinity;
    InsertPoint(point, affinity);
  }

  // Update child
  child->area = 0;
  child->color = RNblack_rgb;
  child->timestamp = 0;

  // Update hierarchy
  child->parent = this;
  children.Insert(child);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x178, %rsp            # imm = 0x178
movq %rsi, %rbx
movq %rdi, %r14
movsd 0xe8(%rsi), %xmm0
addsd 0xe8(%rdi), %xmm0
movsd %xmm0, 0xe8(%rdi)
movl 0x14(%rdi), %r15d
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movl $0xf0, %eax
leaq (%rdi,%rax), %r12
movl 0x14(%rsi), %ebp
movq %r12, %rdi
callq 0xc34dc
cvtsi2sdl 0x14(%rbx), %xmm2
movsd 0xf0(%rbx), %xmm0
mulsd %xmm2, %xmm0
movsd 0xf8(%rbx), %xmm1
mulsd %xmm2, %xmm1
mulsd 0x100(%rbx), %xmm2
leaq 0x10(%rsp), %r13
movq %r13, %rdi
callq 0xc33a0
movq %r12, %rdi
movq %r13, %rsi
callq 0xc3452
addl %r15d, %ebp
jle 0x2ad78
cvtsi2sd %ebp, %xmm3
movsd 0xf0(%r14), %xmm0
movsd 0xf8(%r14), %xmm1
divsd %xmm3, %xmm0
divsd %xmm3, %xmm1
movsd 0x100(%r14), %xmm2
movsd %xmm3, 0x8(%rsp)
divsd %xmm3, %xmm2
leaq 0x10(%rsp), %rdi
callq 0xc33a0
jmp 0x2ad96
leaq 0x546bd1(%rip), %rsi    # 0x571950
leaq 0x10(%rsp), %rdi
callq 0xc3382
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0xc3432
xorps %xmm1, %xmm1
cvtsi2sdl 0x14(%r14), %xmm1
mulsd 0x108(%r14), %xmm1
movsd %xmm1, 0x108(%r14)
testl %ebp, %ebp
jg 0x2adc8
xorpd %xmm0, %xmm0
jmp 0x2ade2
xorps %xmm0, %xmm0
cvtsi2sdl 0x14(%rbx), %xmm0
mulsd 0x108(%rbx), %xmm0
addsd %xmm1, %xmm0
divsd 0x8(%rsp), %xmm0
movl $0xf0, %r13d
movsd %xmm0, 0x108(%r14)
leaq 0x40(%r14), %r12
leaq 0xd0(%rsp), %rdi
movq %r12, %rsi
callq 0x28f4a
leaq 0x40(%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x28f4a
xorps %xmm0, %xmm0
cvtsi2sdl 0x14(%r14), %xmm0
xorps %xmm1, %xmm1
cvtsi2sdl 0x14(%rbx), %xmm1
leaq 0xd0(%rsp), %rsi
leaq 0x28(%rsp), %rdx
movq %r12, %rdi
callq 0x29722
leaq 0x30(%rsp), %rdi
callq 0x88520
leaq 0xd8(%rsp), %rdi
callq 0x88520
movl 0x14(%r14), %ecx
movl 0x14(%rbx), %eax
movl %eax, %edx
addl %ecx, %edx
jle 0x2aefb
movl 0x110(%rbx), %esi
cmpl %esi, 0x110(%r14)
jne 0x2ae9c
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
mulsd 0x118(%rbx), %xmm1
leaq 0x118(%r14), %rsi
mulsd 0x118(%r14), %xmm0
addsd %xmm1, %xmm0
jmp 0x2aeec
testl %esi, %esi
setns %dil
cmpl %eax, %ecx
setle %r8b
testb %dil, %r8b
jne 0x2aec6
xorps %xmm0, %xmm0
cvtsi2sd %ecx, %xmm0
leaq 0x118(%r14), %rsi
mulsd 0x118(%r14), %xmm0
jmp 0x2aeec
movl %esi, 0x110(%r14)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
leaq 0x118(%r14), %rsi
mulsd 0x118(%rbx), %xmm0
addsd 0x118(%r14), %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, (%rsi)
leal (,%rax,4), %edx
cmpl %edx, %ecx
setge %dl
testl %ecx, %ecx
setle %cl
orb %dl, %cl
jne 0x2af6f
xorl %r15d, %r15d
movq 0x8(%r14), %rax
movq (%rax,%r15,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x2a378
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movapd %xmm1, %xmm0
subsd 0xc8(%r12), %xmm0
movupd 0x120(%r14), %xmm2
unpcklpd %xmm0, %xmm0            # xmm0 = xmm0[0,0]
addpd %xmm2, %xmm0
movupd %xmm0, 0x120(%r14)
movsd %xmm1, 0xc8(%r12)
incq %r15
movslq 0x14(%r14), %rax
cmpq %rax, %r15
jl 0x2af13
movl 0x14(%rbx), %eax
addq %rbx, %r13
testl %eax, %eax
je 0x2afc9
movq 0x8(%rbx), %rcx
cltq
movq -0x8(%rcx,%rax,8), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x2a1f0
movq %r14, %rdi
movq %r12, %rsi
callq 0x2a378
xorpd %xmm1, %xmm1
maxsd %xmm0, %xmm1
movsd 0x120(%r14), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x120(%r14)
movq %r14, %rdi
movq %r12, %rsi
movapd %xmm1, %xmm0
callq 0x2a720
movl 0x14(%rbx), %eax
jmp 0x2af72
xorl %r15d, %r15d
movq %r15, 0xe8(%rbx)
leaq 0x546976(%rip), %rsi    # 0x571950
movq %r13, %rdi
callq 0xc3432
movq %r15, 0x108(%rbx)
movq %r14, 0x18(%rbx)
movl 0x2c(%r14), %edx
addq $0x20, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0xc35c2
addq $0x178, %rsp            # imm = 0x178
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0x30(%rsp), %rdi
callq 0x88520
jmp 0x2b024
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x88520
movq %rbx, %rdi
callq 0x15ad0
nop