// File: 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:
subq $0x228, %rsp            # imm = 0x228
movq %rdi, 0x220(%rsp)
movq %rsi, 0x218(%rsp)
movq 0x220(%rsp), %rdi
movq %rdi, 0x58(%rsp)
movq 0x218(%rsp), %rax
movsd 0xe8(%rax), %xmm0
addsd 0xe8(%rdi), %xmm0
movsd %xmm0, 0xe8(%rdi)
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x54(%rsp)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movq 0x58(%rsp), %rdi
movl %eax, %ecx
movl 0x54(%rsp), %eax
addl %ecx, %eax
movl %eax, 0x214(%rsp)
addq $0x8, %rdi
callq 0x1ecf0
movq 0x58(%rsp), %rdi
cvtsi2sd %eax, %xmm0
addq $0xf0, %rdi
callq 0x1734b0
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
cvtsi2sd %eax, %xmm0
movq 0x218(%rsp), %rsi
addq $0xf0, %rsi
leaq 0x1f8(%rsp), %rdi
callq 0x3b580
movq 0x58(%rsp), %rdi
addq $0xf0, %rdi
leaq 0x1f8(%rsp), %rsi
callq 0x1733b0
cmpl $0x0, 0x214(%rsp)
jle 0x3f7bc
movq 0x58(%rsp), %rsi
addq $0xf0, %rsi
cvtsi2sdl 0x214(%rsp), %xmm0
leaq 0x1e0(%rsp), %rdi
callq 0x3b5c0
jmp 0x3f7d0
leaq 0x1e0(%rsp), %rdi
leaq 0x62be85(%rip), %rsi    # 0x66b650
callq 0x1731b0
movq 0x58(%rsp), %rdi
addq $0xf0, %rdi
leaq 0x1e0(%rsp), %rsi
callq 0x173370
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, %ecx
movq 0x58(%rsp), %rax
cvtsi2sd %ecx, %xmm0
mulsd 0x108(%rax), %xmm0
movsd %xmm0, 0x108(%rax)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, %ecx
movq 0x58(%rsp), %rax
cvtsi2sd %ecx, %xmm0
movq 0x218(%rsp), %rcx
movsd 0x108(%rcx), %xmm2
movsd 0x108(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x108(%rax)
cmpl $0x0, 0x214(%rsp)
jle 0x3f882
movq 0x58(%rsp), %rax
movsd 0x108(%rax), %xmm0
cvtsi2sdl 0x214(%rsp), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x3f88d
xorps %xmm0, %xmm0
movsd %xmm0, 0x48(%rsp)
jmp 0x3f88d
movq 0x58(%rsp), %rsi
movsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x108(%rsi)
addq $0x40, %rsi
movq %rsi, 0x40(%rsp)
leaq 0x138(%rsp), %rdi
callq 0x3c070
movq 0x218(%rsp), %rsi
addq $0x40, %rsi
leaq 0x90(%rsp), %rdi
callq 0x3c070
jmp 0x3f8d1
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x3c(%rsp)
jmp 0x3f8e5
movl 0x3c(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x30(%rsp)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x38(%rsp)
jmp 0x3f90a
movsd 0x30(%rsp), %xmm0
movq 0x40(%rsp), %rdi
movl 0x38(%rsp), %eax
cvtsi2sd %eax, %xmm1
leaq 0x138(%rsp), %rsi
leaq 0x90(%rsp), %rdx
callq 0x3ccb0
jmp 0x3f934
leaq 0x90(%rsp), %rdi
callq 0x47670
leaq 0x138(%rsp), %rdi
callq 0x47670
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x2c(%rsp)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, %ecx
movl 0x2c(%rsp), %eax
addl %ecx, %eax
cmpl $0x0, %eax
jle 0x3fbf4
movq 0x58(%rsp), %rax
movl 0x110(%rax), %eax
movq 0x218(%rsp), %rcx
cmpl 0x110(%rcx), %eax
jne 0x3faa5
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, %ecx
movq 0x58(%rsp), %rax
cvtsi2sd %ecx, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x118(%rax), %xmm0
movsd %xmm0, 0x20(%rsp)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movsd 0x18(%rsp), %xmm0
movsd 0x20(%rsp), %xmm2
movq 0x58(%rsp), %rdi
cvtsi2sd %eax, %xmm1
movq 0x218(%rsp), %rax
mulsd 0x118(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rdi)
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x28(%rsp)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl 0x28(%rsp), %ecx
movl %eax, %edx
movq 0x58(%rsp), %rax
addl %edx, %ecx
cvtsi2sd %ecx, %xmm1
movsd 0x118(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
jmp 0x3fbf2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
jmp 0x3fa93
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
leaq 0x90(%rsp), %rdi
callq 0x47670
leaq 0x138(%rsp), %rdi
callq 0x47670
jmp 0x3fe23
movq 0x218(%rsp), %rax
cmpl $0x0, 0x110(%rax)
jl 0x3fae3
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x14(%rsp)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jle 0x3fb52
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movq 0x58(%rsp), %rdi
cvtsi2sd %eax, %xmm0
mulsd 0x118(%rdi), %xmm0
movsd %xmm0, 0x118(%rdi)
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x10(%rsp)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl 0x10(%rsp), %ecx
movl %eax, %edx
movq 0x58(%rsp), %rax
addl %edx, %ecx
cvtsi2sd %ecx, %xmm1
movsd 0x118(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
jmp 0x3fbf0
movq 0x58(%rsp), %rax
movq 0x218(%rsp), %rcx
movl 0x110(%rcx), %ecx
movl %ecx, 0x110(%rax)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movq 0x58(%rsp), %rdi
cvtsi2sd %eax, %xmm0
movq 0x218(%rsp), %rax
movsd 0x118(%rax), %xmm2
movsd 0x118(%rdi), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rdi)
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0xc(%rsp)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl 0xc(%rsp), %ecx
movl %eax, %edx
movq 0x58(%rsp), %rax
addl %edx, %ecx
cvtsi2sd %ecx, %xmm1
movsd 0x118(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
jmp 0x3fbf2
jmp 0x3fbf4
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x8(%rsp)
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, %ecx
movl 0x8(%rsp), %eax
shll $0x2, %ecx
cmpl %ecx, %eax
jge 0x3fd13
movl $0x0, 0x80(%rsp)
movq 0x58(%rsp), %rdi
movl 0x80(%rsp), %eax
movl %eax, 0x4(%rsp)
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, %ecx
movl 0x4(%rsp), %eax
cmpl %ecx, %eax
jge 0x3fd11
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
movl 0x80(%rsp), %esi
callq 0x1f6a0
movq 0x58(%rsp), %rdi
movq %rax, 0x78(%rsp)
movq 0x78(%rsp), %rsi
callq 0x3e2d0
movsd %xmm0, 0x70(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x70(%rsp), %xmm0
jbe 0x3fc9d
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x58(%rsp), %rax
movsd 0x70(%rsp), %xmm0
movq 0x78(%rsp), %rcx
subsd 0xc8(%rcx), %xmm0
addsd 0x120(%rax), %xmm0
movsd %xmm0, 0x120(%rax)
movsd 0x70(%rsp), %xmm0
movq 0x78(%rsp), %rcx
subsd 0xc8(%rcx), %xmm0
addsd 0x128(%rax), %xmm0
movsd %xmm0, 0x128(%rax)
movsd 0x70(%rsp), %xmm0
movq 0x78(%rsp), %rax
movsd %xmm0, 0xc8(%rax)
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x3fc33
jmp 0x3fd13
jmp 0x3fd15
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x3b4b0
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3fd37
jmp 0x3fdb8
movq 0x218(%rsp), %rdi
addq $0x8, %rdi
callq 0x476b0
movq %rax, 0x68(%rsp)
movq 0x218(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x3e080
movq 0x58(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x3e2d0
movsd %xmm0, 0x60(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x60(%rsp), %xmm0
jbe 0x3fd88
xorps %xmm0, %xmm0
movsd %xmm0, 0x60(%rsp)
movq 0x58(%rsp), %rdi
movsd 0x60(%rsp), %xmm0
addsd 0x120(%rdi), %xmm0
movsd %xmm0, 0x120(%rdi)
movq 0x68(%rsp), %rsi
movsd 0x60(%rsp), %xmm0
callq 0x3ec40
jmp 0x3fd15
movq 0x218(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0xe8(%rax)
movq 0x218(%rsp), %rdi
addq $0xf0, %rdi
leaq 0x62b86f(%rip), %rsi    # 0x66b650
callq 0x173370
movq 0x58(%rsp), %rdi
movq 0x218(%rsp), %rax
xorps %xmm0, %xmm0
movsd %xmm0, 0x108(%rax)
movq 0x218(%rsp), %rax
movq %rdi, 0x18(%rax)
addq $0x20, %rdi
movq 0x218(%rsp), %rsi
callq 0x476c0
addq $0x228, %rsp            # imm = 0x228
retq
movq 0x88(%rsp), %rdi
callq 0x16ca0