// File: /workspace/github_cpp_makefile_star2_O2/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
cvtsi2sd %r15d, %xmm0
movl $0xf0, %r13d
leaq (%rdi,%r13), %r12
movl 0x14(%rsi), %ebp
movq %r12, %rdi
callq 0xb1e8c
cvtsi2sdl 0x14(%rbx), %xmm0
addq %rbx, %r13
leaq 0x10(%rsp), %rdi
movq %r13, 0x8(%rsp)
movq %r13, %rsi
callq 0x26c7b
movq %r12, %rdi
leaq 0x10(%rsp), %rsi
callq 0xb1e02
addl %r15d, %ebp
jle 0x28e53
cvtsi2sd %ebp, %xmm0
leaq 0x10(%rsp), %rdi
movq %r12, %rsi
movsd %xmm0, (%rsp)
callq 0x26c4f
jmp 0x28e6d
leaq 0x535b16(%rip), %rsi    # 0x55e970
leaq 0x10(%rsp), %rdi
callq 0xb1d34
cvtsi2sd %ebp, %xmm0
movsd %xmm0, (%rsp)
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0xb1de2
cvtsi2sdl 0x14(%r14), %xmm1
mulsd 0x108(%r14), %xmm1
movsd %xmm1, 0x108(%r14)
testl %ebp, %ebp
jg 0x28e9c
xorpd %xmm0, %xmm0
jmp 0x28eb2
cvtsi2sdl 0x14(%rbx), %xmm0
mulsd 0x108(%rbx), %xmm0
addsd %xmm1, %xmm0
divsd (%rsp), %xmm0
movsd %xmm0, 0x108(%r14)
leaq 0x40(%r14), %r12
leaq 0xd0(%rsp), %rdi
movq %r12, %rsi
callq 0x2732a
leaq 0x40(%rbx), %rsi
leaq 0x28(%rsp), %rdi
callq 0x2732a
cvtsi2sdl 0x14(%r14), %xmm0
cvtsi2sdl 0x14(%rbx), %xmm1
leaq 0xd0(%rsp), %rsi
leaq 0x28(%rsp), %rdx
movq %r12, %rdi
callq 0x27a5a
leaq 0x30(%rsp), %rdi
callq 0x7cbba
leaq 0xd8(%rsp), %rdi
callq 0x7cbba
movl 0x14(%r14), %eax
movl 0x14(%rbx), %ecx
movl %ecx, %edx
addl %eax, %edx
jle 0x28fb0
movl 0x110(%rbx), %esi
cmpl %esi, 0x110(%r14)
jne 0x28f5a
cvtsi2sd %eax, %xmm0
cvtsi2sd %ecx, %xmm1
mulsd 0x118(%rbx), %xmm1
leaq 0x118(%r14), %rsi
mulsd 0x118(%r14), %xmm0
addsd %xmm1, %xmm0
jmp 0x28fa4
testl %esi, %esi
setns %dil
cmpl %ecx, %eax
setle %r8b
testb %dil, %r8b
jne 0x28f81
cvtsi2sd %eax, %xmm0
leaq 0x118(%r14), %rsi
mulsd 0x118(%r14), %xmm0
jmp 0x28fa4
movl %esi, 0x110(%r14)
cvtsi2sd %ecx, %xmm0
leaq 0x118(%r14), %rsi
mulsd 0x118(%rbx), %xmm0
addsd 0x118(%r14), %xmm0
cvtsi2sd %edx, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, (%rsi)
shll $0x2, %ecx
cmpl %ecx, %eax
jge 0x2906b
xorl %r13d, %r13d
cltq
cmpq %rax, %r13
jge 0x2906b
movq 0x8(%r14), %rax
movq (%rax,%r13,8), %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x28526
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 %r13
movl 0x14(%r14), %eax
jmp 0x28fbe
movq 0x8(%rbx), %rcx
movq -0x8(%rcx,%rax,8), %r12
movq %rbx, %rdi
movq %r12, %rsi
callq 0x28406
movq %r14, %rdi
movq %r12, %rsi
callq 0x28526
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 0x288ce
movslq 0x14(%rbx), %rax
testq %rax, %rax
jne 0x2901f
andq $0x0, 0xe8(%rbx)
leaq 0x5358ed(%rip), %rsi    # 0x55e970
movq 0x8(%rsp), %rdi
callq 0xb1de2
andq $0x0, 0x108(%rbx)
movq %r14, 0x18(%rbx)
addq $0x20, %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x1a04c
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 0x7cbba
jmp 0x290cc
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x7cbba
movq %rbx, %rdi
callq 0x15af0
nop