// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3SegmentationCluster::R3SegmentationCluster(gaps::R3SegmentationCluster*, gaps::R3SegmentationCluster*)
R3SegmentationCluster::
R3SegmentationCluster(R3SegmentationCluster *child1, R3SegmentationCluster *child2)
  : seed_point(NULL),
    points(),
    parent(NULL),
    children(),
    pairs(),
    primitive(),
    area(0),
    color(0,0,0),
    timestamp(0),
    category_identifier(-1),
    category_confidence(0),
    possible_affinity(0),
    total_affinity(0),
    segmentation(NULL),
    segmentation_index(-1)
{
  // Assign seed point
  seed_point = child1->seed_point;

  // Update primitive
  primitive.Update(child1->primitive, child2->primitive, child1->points.NEntries(), child2->points.NEntries());

  // Update color
  if (child1->points.NEntries() + child2->points.NEntries() > 0) {
    color += child1->points.NEntries() * child1->color;
    color += child2->points.NEntries() * child2->color;
    color /= child1->points.NEntries() + child2->points.NEntries();
  }

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

  // Update timestamp
  timestamp = 0.5*(child1->timestamp + child2->timestamp);

  // Update area
  area = child1->area + child2->area;

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

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

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

// Assembly:
subq $0x288, %rsp            # imm = 0x288
movq %rdi, 0x280(%rsp)
movq %rsi, 0x278(%rsp)
movq %rdx, 0x270(%rsp)
movq 0x280(%rsp), %rdi
movq %rdi, 0xb0(%rsp)
movq $0x0, (%rdi)
addq $0x8, %rdi
movq %rdi, 0xa8(%rsp)
callq 0x20260
movq 0xb0(%rsp), %rdi
movq $0x0, 0x18(%rdi)
addq $0x20, %rdi
movq %rdi, 0xb8(%rsp)
callq 0x47650
jmp 0x3d6aa
movq 0xb0(%rsp), %rdi
addq $0x30, %rdi
movq %rdi, 0xa0(%rsp)
callq 0x47660
jmp 0x3d6c5
movq 0xb0(%rsp), %rdi
addq $0x40, %rdi
movq %rdi, 0x98(%rsp)
xorl %esi, %esi
callq 0x3bfc0
jmp 0x3d6e2
movq 0xb0(%rsp), %rdi
movq $0x0, 0xe8(%rdi)
addq $0xf0, %rdi
xorps %xmm2, %xmm2
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x1731f0
jmp 0x3d70c
movq 0xb0(%rsp), %rax
movq $0x0, 0x108(%rax)
movl $0xffffffff, 0x110(%rax) # imm = 0xFFFFFFFF
movq $0x0, 0x118(%rax)
movq $0x0, 0x120(%rax)
movq $0x0, 0x128(%rax)
movq $0x0, 0x130(%rax)
movl $0xffffffff, 0x138(%rax) # imm = 0xFFFFFFFF
movq 0x278(%rsp), %rcx
movq (%rcx), %rcx
movq %rcx, (%rax)
addq $0x40, %rax
movq %rax, 0x90(%rsp)
movq 0x278(%rsp), %rsi
addq $0x40, %rsi
leaq 0x1b8(%rsp), %rdi
callq 0x3c070
jmp 0x3d794
movq 0x270(%rsp), %rsi
addq $0x40, %rsi
leaq 0x110(%rsp), %rdi
callq 0x3c070
jmp 0x3d7af
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x8c(%rsp)
jmp 0x3d7c9
movl 0x8c(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x80(%rsp)
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x88(%rsp)
jmp 0x3d7f7
movsd 0x80(%rsp), %xmm0
movq 0x90(%rsp), %rdi
movl 0x88(%rsp), %eax
cvtsi2sd %eax, %xmm1
leaq 0x1b8(%rsp), %rsi
leaq 0x110(%rsp), %rdx
callq 0x3ccb0
jmp 0x3d82a
leaq 0x110(%rsp), %rdi
callq 0x47670
leaq 0x1b8(%rsp), %rdi
callq 0x47670
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x7c(%rsp)
jmp 0x3d85b
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x78(%rsp)
jmp 0x3d872
movl 0x78(%rsp), %ecx
movl 0x7c(%rsp), %eax
addl %ecx, %eax
cmpl $0x0, %eax
jle 0x3da3f
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x74(%rsp)
jmp 0x3d89c
movl 0x74(%rsp), %eax
cvtsi2sd %eax, %xmm0
movq 0x278(%rsp), %rsi
addq $0xf0, %rsi
leaq 0xf8(%rsp), %rdi
callq 0x3b580
jmp 0x3d8c2
movq 0xb0(%rsp), %rdi
addq $0xf0, %rdi
leaq 0xf8(%rsp), %rsi
callq 0x1733b0
jmp 0x3d8e0
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x70(%rsp)
jmp 0x3d8f7
movl 0x70(%rsp), %eax
cvtsi2sd %eax, %xmm0
movq 0x270(%rsp), %rsi
addq $0xf0, %rsi
leaq 0xe0(%rsp), %rdi
callq 0x3b580
jmp 0x3d91d
movq 0xb0(%rsp), %rdi
addq $0xf0, %rdi
leaq 0xe0(%rsp), %rsi
callq 0x1733b0
jmp 0x3d93b
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x6c(%rsp)
jmp 0x3d952
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x68(%rsp)
jmp 0x3d969
movq 0xb0(%rsp), %rdi
movl 0x68(%rsp), %ecx
movl 0x6c(%rsp), %eax
addl %ecx, %eax
cvtsi2sd %eax, %xmm0
addq $0xf0, %rdi
callq 0x1734f0
jmp 0x3d98d
jmp 0x3da3f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
jmp 0x3e058
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
jmp 0x3e04b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
jmp 0x3e03e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
jmp 0x3e031
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
jmp 0x3da2d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x268(%rsp)
movl %eax, 0x264(%rsp)
leaq 0x110(%rsp), %rdi
callq 0x47670
leaq 0x1b8(%rsp), %rdi
callq 0x47670
jmp 0x3e031
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x64(%rsp)
jmp 0x3da56
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x60(%rsp)
jmp 0x3da6d
movl 0x60(%rsp), %ecx
movl 0x64(%rsp), %eax
addl %ecx, %eax
cmpl $0x0, %eax
jle 0x3dd97
movq 0x278(%rsp), %rax
movl 0x110(%rax), %eax
movq 0x270(%rsp), %rcx
cmpl 0x110(%rcx), %eax
jne 0x3dbb9
movq 0xb0(%rsp), %rax
movq 0x278(%rsp), %rcx
movl 0x110(%rcx), %ecx
movl %ecx, 0x110(%rax)
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x5c(%rsp)
jmp 0x3dad5
movq 0xb0(%rsp), %rax
movl 0x5c(%rsp), %ecx
cvtsi2sd %ecx, %xmm0
movq 0x278(%rsp), %rcx
movsd 0x118(%rcx), %xmm2
movsd 0x118(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x58(%rsp)
jmp 0x3db24
movq 0xb0(%rsp), %rax
movl 0x58(%rsp), %ecx
cvtsi2sd %ecx, %xmm0
movq 0x270(%rsp), %rcx
movsd 0x118(%rcx), %xmm2
movsd 0x118(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x54(%rsp)
jmp 0x3db73
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x50(%rsp)
jmp 0x3db8a
movq 0xb0(%rsp), %rax
movl 0x50(%rsp), %edx
movl 0x54(%rsp), %ecx
addl %edx, %ecx
cvtsi2sd %ecx, %xmm1
movsd 0x118(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
jmp 0x3dd95
movq 0x270(%rsp), %rax
cmpl $0x0, 0x110(%rax)
jl 0x3dc08
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x4c(%rsp)
jmp 0x3dbe1
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x48(%rsp)
jmp 0x3dbf8
movl 0x4c(%rsp), %eax
movl 0x48(%rsp), %ecx
cmpl %ecx, %eax
jle 0x3dcd0
movq 0xb0(%rsp), %rax
movq 0x278(%rsp), %rcx
movl 0x110(%rcx), %ecx
movl %ecx, 0x110(%rax)
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x44(%rsp)
jmp 0x3dc3b
movq 0xb0(%rsp), %rax
movl 0x44(%rsp), %ecx
cvtsi2sd %ecx, %xmm0
movq 0x278(%rsp), %rcx
movsd 0x118(%rcx), %xmm2
movsd 0x118(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x40(%rsp)
jmp 0x3dc8a
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x3c(%rsp)
jmp 0x3dca1
movq 0xb0(%rsp), %rax
movl 0x3c(%rsp), %edx
movl 0x40(%rsp), %ecx
addl %edx, %ecx
cvtsi2sd %ecx, %xmm1
movsd 0x118(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
jmp 0x3dd93
movq 0xb0(%rsp), %rax
movq 0x270(%rsp), %rcx
movl 0x110(%rcx), %ecx
movl %ecx, 0x110(%rax)
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x38(%rsp)
jmp 0x3dd03
movq 0xb0(%rsp), %rax
movl 0x38(%rsp), %ecx
cvtsi2sd %ecx, %xmm0
movq 0x270(%rsp), %rcx
movsd 0x118(%rcx), %xmm2
movsd 0x118(%rax), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x34(%rsp)
jmp 0x3dd52
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x30(%rsp)
jmp 0x3dd69
movq 0xb0(%rsp), %rax
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %ecx
addl %edx, %ecx
cvtsi2sd %ecx, %xmm1
movsd 0x118(%rax), %xmm0
divsd %xmm1, %xmm0
movsd %xmm0, 0x118(%rax)
jmp 0x3dd95
jmp 0x3dd97
movq 0xb0(%rsp), %rax
movq 0x278(%rsp), %rcx
movsd 0x108(%rcx), %xmm1
movq 0x270(%rsp), %rcx
addsd 0x108(%rcx), %xmm1
movsd 0x1bf269(%rip), %xmm0   # 0x1fd030
mulsd %xmm1, %xmm0
movsd %xmm0, 0x108(%rax)
movq 0x278(%rsp), %rcx
movsd 0xe8(%rcx), %xmm0
movq 0x270(%rsp), %rcx
addsd 0xe8(%rcx), %xmm0
movsd %xmm0, 0xe8(%rax)
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x3b4b0
movl %eax, 0x2c(%rsp)
jmp 0x3de12
movl 0x2c(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3de27
jmp 0x3deeb
movq 0x278(%rsp), %rdi
addq $0x8, %rdi
callq 0x476b0
movq %rax, 0x20(%rsp)
jmp 0x3de3f
movq 0x20(%rsp), %rax
movq %rax, 0xd8(%rsp)
movq 0x278(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0x3e080
jmp 0x3de63
movq 0xb0(%rsp), %rdi
movq 0xd8(%rsp), %rsi
callq 0x3e2d0
movsd %xmm0, 0x18(%rsp)
jmp 0x3de80
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
xorps %xmm0, %xmm0
ucomisd 0xd0(%rsp), %xmm0
jbe 0x3dea9
xorps %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
movq 0xb0(%rsp), %rdi
movsd 0xd0(%rsp), %xmm1
movsd 0x120(%rdi), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x120(%rdi)
movq 0xd8(%rsp), %rsi
movsd 0xd0(%rsp), %xmm0
callq 0x3ec40
jmp 0x3dee6
jmp 0x3ddfb
jmp 0x3deed
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x3b4b0
movl %eax, 0x14(%rsp)
jmp 0x3df04
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x3df19
jmp 0x3dfdb
movq 0x270(%rsp), %rdi
addq $0x8, %rdi
callq 0x476b0
movq %rax, 0x8(%rsp)
jmp 0x3df31
movq 0x8(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x270(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x3e080
jmp 0x3df55
movq 0xb0(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x3e2d0
movsd %xmm0, (%rsp)
jmp 0x3df71
movsd (%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
xorps %xmm0, %xmm0
ucomisd 0xc0(%rsp), %xmm0
jbe 0x3df99
xorps %xmm0, %xmm0
movsd %xmm0, 0xc0(%rsp)
movq 0xb0(%rsp), %rdi
movsd 0xc0(%rsp), %xmm1
movsd 0x120(%rdi), %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x120(%rdi)
movq 0xc8(%rsp), %rsi
movsd 0xc0(%rsp), %xmm0
callq 0x3ec40
jmp 0x3dfd6
jmp 0x3deed
movq 0xb0(%rsp), %rdi
movq 0x278(%rsp), %rax
movq %rdi, 0x18(%rax)
movq 0x270(%rsp), %rax
movq %rdi, 0x18(%rax)
addq $0x20, %rdi
movq 0x278(%rsp), %rsi
callq 0x476c0
jmp 0x3e00e
movq 0xb0(%rsp), %rdi
addq $0x20, %rdi
movq 0x270(%rsp), %rsi
callq 0x476c0
jmp 0x3e029
addq $0x288, %rsp            # imm = 0x288
retq
movq 0x98(%rsp), %rdi
callq 0x47670
movq 0xa0(%rsp), %rdi
callq 0x47690
movq 0xb8(%rsp), %rdi
callq 0x476a0
movq 0xa8(%rsp), %rdi
callq 0x1f730
movq 0x268(%rsp), %rdi
callq 0x16ca0
nopw %cs:(%rax,%rax)