// File: tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3SegmentationCluster::UpdatePoints(gaps::R3Kdtree<gaps::R3SegmentationPoint*> const*)
int R3SegmentationCluster::
UpdatePoints(const R3Kdtree<R3SegmentationPoint *> *kdtree)
{
  // Get convenient variables
  double max_cluster_primitive_distance = (segmentation) ? segmentation->max_cluster_primitive_distance : 0;
  RNBoolean allow_outlier_points = (segmentation) ? segmentation->allow_outlier_points : TRUE;
  int min_cluster_points = (segmentation) ? segmentation->min_cluster_points : 0;
  
  // Empty points
  possible_affinity = 0;
  EmptyPoints();  

  // Find points near primitive
  if (seed_point) {
    // Find connected set of points near primitive
    static int mark = 1;
    RNArray<R3SegmentationPoint *> stack;
    InsertPoint(seed_point, 1.0);
    stack.Insert(seed_point);
    seed_point->mark = ++mark;
    while (!stack.IsEmpty()) {
      R3SegmentationPoint *point = stack.Tail();
      stack.RemoveTail();
      for (int i = 0; i < point->neighbors.NEntries(); i++) {
        R3SegmentationPoint *neighbor = point->neighbors.Kth(i);
        if (neighbor->mark == mark) continue;
        neighbor->mark = mark;
        RNScalar affinity = Affinity(neighbor);
        if (affinity <= 0) continue;
        possible_affinity += affinity;
        if (neighbor->cluster == this) continue;
        if (neighbor->cluster && (neighbor->cluster_affinity > 0.75 * affinity)) continue;
        InsertPoint(neighbor, affinity);
        stack.Insert(neighbor);
      }
    }
  }
  else if (kdtree) {
    // Find all points near primitive
    RNArray<R3SegmentationPoint *> points1;
    if (primitive.primitive_type == R3_SEGMENTATION_POINT_PRIMITIVE_TYPE)
      kdtree->FindAll(primitive.centroid, 0, max_cluster_primitive_distance, points1);
    else if (primitive.primitive_type == R3_SEGMENTATION_LINE_PRIMITIVE_TYPE)
      kdtree->FindAll(primitive.line, 0, max_cluster_primitive_distance, points1);
    else if (primitive.primitive_type == R3_SEGMENTATION_PLANE_PRIMITIVE_TYPE)
      kdtree->FindAll(primitive.plane, 0, max_cluster_primitive_distance, points1);
    else RNAbort("Unrecognized primitive type");

    // Check points
    if (allow_outlier_points) {
      if (points1.NEntries() < min_cluster_points) return 0;
    }
  
    // Insert points
    for (int i = 0; i < points1.NEntries(); i++) {
      R3SegmentationPoint *point = points1.Kth(i);
      RNScalar affinity = Affinity(point);
      if (affinity <= 0) continue;
      possible_affinity += affinity;
      if (point->cluster == this) continue;
      if (point->cluster && (point->cluster->points.NEntries() > 0)) {
        RNScalar ratio =(RNScalar) points1.NEntries() / (RNScalar) point->cluster->points.NEntries();
        if (ratio < 0.1) ratio = 0.1;
        if (ratio * affinity < point->cluster_affinity) continue;
      }
      InsertPoint(point, affinity);
    }
  }

  // Return success
  return 1;
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x108(%rsp)
movq %rsi, 0x100(%rsp)
movq 0x108(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x130(%rax)
je 0x40067
movq 0x78(%rsp), %rax
movq 0x130(%rax), %rax
movsd 0x58(%rax), %xmm0
movsd %xmm0, 0x70(%rsp)
jmp 0x40072
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
jmp 0x40072
movq 0x78(%rsp), %rax
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0xf8(%rsp)
cmpq $0x0, 0x130(%rax)
je 0x400a8
movq 0x78(%rsp), %rax
movq 0x130(%rax), %rax
movl 0xd4(%rax), %eax
movl %eax, 0x6c(%rsp)
jmp 0x400b3
movl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x400b3
movq 0x78(%rsp), %rax
movl 0x6c(%rsp), %ecx
movl %ecx, 0xf4(%rsp)
cmpq $0x0, 0x130(%rax)
je 0x400e2
movq 0x78(%rsp), %rax
movq 0x130(%rax), %rax
movl 0x38(%rax), %eax
movl %eax, 0x68(%rsp)
jmp 0x400ea
xorl %eax, %eax
movl %eax, 0x68(%rsp)
jmp 0x400ea
movq 0x78(%rsp), %rdi
movl 0x68(%rsp), %eax
movl %eax, 0xf0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x120(%rdi)
callq 0x3eef0
movq 0x78(%rsp), %rax
cmpq $0x0, (%rax)
je 0x40398
leaq 0xe0(%rsp), %rdi
callq 0x20260
movq 0x78(%rsp), %rdi
movq (%rdi), %rsi
movsd 0x1bceea(%rip), %xmm0   # 0x1fd020
callq 0x3ec40
jmp 0x4013d
movq 0x78(%rsp), %rax
movq (%rax), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x1f3d0
jmp 0x40154
movq 0x78(%rsp), %rax
movl 0x21f621(%rip), %ecx    # 0x25f780
addl $0x1, %ecx
movl %ecx, 0x21f618(%rip)    # 0x25f780
movq (%rax), %rax
movl %ecx, 0xd8(%rax)
leaq 0xe0(%rsp), %rdi
callq 0x3b4b0
movl %eax, 0x64(%rsp)
jmp 0x40184
movl 0x64(%rsp), %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
testb $0x1, %al
jne 0x40199
jmp 0x40386
leaq 0xe0(%rsp), %rdi
callq 0x476b0
movq %rax, 0x58(%rsp)
jmp 0x401ad
movq 0x58(%rsp), %rax
movq %rax, 0xc8(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x27f50
jmp 0x401c9
movl $0x0, 0xc4(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0x50(%rsp)
movq 0xc8(%rsp), %rdi
addq $0xb0, %rdi
callq 0x1ecf0
movl %eax, 0x54(%rsp)
jmp 0x401f9
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
cmpl %ecx, %eax
jge 0x40381
movq 0xc8(%rsp), %rdi
addq $0xb0, %rdi
movl 0xc4(%rsp), %esi
callq 0x1f6a0
movq %rax, 0x48(%rsp)
jmp 0x4022b
movq 0x48(%rsp), %rax
movq %rax, 0xb8(%rsp)
movq 0xb8(%rsp), %rax
movl 0xd8(%rax), %eax
cmpl 0x21f534(%rip), %eax    # 0x25f780
jne 0x40279
jmp 0x4036b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x1f730
jmp 0x40721
movq 0x78(%rsp), %rdi
movl 0x21f4fc(%rip), %ecx    # 0x25f780
movq 0xb8(%rsp), %rax
movl %ecx, 0xd8(%rax)
movq 0xb8(%rsp), %rsi
callq 0x3e2d0
movsd %xmm0, 0x40(%rsp)
jmp 0x402a7
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
xorps %xmm0, %xmm0
ucomisd 0xb0(%rsp), %xmm0
jb 0x402c9
jmp 0x4036b
movq 0x78(%rsp), %rcx
movsd 0xb0(%rsp), %xmm0
addsd 0x120(%rcx), %xmm0
movsd %xmm0, 0x120(%rcx)
movq 0xb8(%rsp), %rax
cmpq %rcx, 0xc0(%rax)
jne 0x402fa
jmp 0x4036b
movq 0xb8(%rsp), %rax
cmpq $0x0, 0xc0(%rax)
je 0x40335
movq 0xb8(%rsp), %rax
movsd 0xc8(%rax), %xmm0
movsd 0x1bdf7c(%rip), %xmm1   # 0x1fe2a0
mulsd 0xb0(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jbe 0x40335
jmp 0x4036b
movq 0x78(%rsp), %rdi
movq 0xb8(%rsp), %rsi
movsd 0xb0(%rsp), %xmm0
callq 0x3ec40
jmp 0x40352
movq 0xb8(%rsp), %rsi
leaq 0xe0(%rsp), %rdi
callq 0x1f3d0
jmp 0x40369
jmp 0x4036b
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0x401d4
jmp 0x40171
leaq 0xe0(%rsp), %rdi
callq 0x1f730
jmp 0x40707
cmpq $0x0, 0x100(%rsp)
je 0x40705
leaq 0xa0(%rsp), %rdi
callq 0x20260
movq 0x78(%rsp), %rax
cmpl $0x1, 0x40(%rax)
jne 0x40416
movq 0x78(%rsp), %rsi
movq 0x100(%rsp), %rdi
subq $-0x80, %rsi
movsd 0xf8(%rsp), %xmm1
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rdx
callq 0x47880
jmp 0x403eb
jmp 0x404a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xd8(%rsp)
movl %eax, 0xd4(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x1f730
jmp 0x40721
movq 0x78(%rsp), %rax
cmpl $0x2, 0x40(%rax)
jne 0x40452
movq 0x78(%rsp), %rsi
movq 0x100(%rsp), %rdi
addq $0x98, %rsi
movsd 0xf8(%rsp), %xmm1
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rdx
callq 0x47960
jmp 0x40450
jmp 0x404a2
movq 0x78(%rsp), %rax
cmpl $0x3, 0x40(%rax)
jne 0x4048e
movq 0x78(%rsp), %rsi
movq 0x100(%rsp), %rdi
addq $0xc8, %rsi
movsd 0xf8(%rsp), %xmm1
xorps %xmm0, %xmm0
leaq 0xa0(%rsp), %rdx
callq 0x479e0
jmp 0x4048c
jmp 0x404a0
leaq 0x1bde23(%rip), %rdi    # 0x1fe2b8
xorl %eax, %eax
callq 0x1778a0
jmp 0x4049e
jmp 0x404a0
jmp 0x404a2
jmp 0x404a4
cmpl $0x0, 0xf4(%rsp)
je 0x404eb
leaq 0xa0(%rsp), %rdi
callq 0x1ecf0
movl %eax, 0x3c(%rsp)
jmp 0x404c1
movl 0x3c(%rsp), %eax
cmpl 0xf0(%rsp), %eax
jge 0x404e9
movl $0x0, 0x114(%rsp)
movl $0x1, 0x9c(%rsp)
jmp 0x406e7
jmp 0x404eb
movl $0x0, 0x98(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0x34(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x1ecf0
movl %eax, 0x38(%rsp)
jmp 0x40514
movl 0x34(%rsp), %eax
movl 0x38(%rsp), %ecx
cmpl %ecx, %eax
jge 0x406dc
movl 0x98(%rsp), %esi
leaq 0xa0(%rsp), %rdi
callq 0x1f6a0
movq %rax, 0x28(%rsp)
jmp 0x4053f
movq 0x78(%rsp), %rdi
movq 0x28(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0x90(%rsp), %rsi
callq 0x3e2d0
movsd %xmm0, 0x20(%rsp)
jmp 0x40566
movsd 0x20(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x88(%rsp), %xmm0
jb 0x40588
jmp 0x406c6
movq 0x78(%rsp), %rcx
movsd 0x88(%rsp), %xmm0
addsd 0x120(%rcx), %xmm0
movsd %xmm0, 0x120(%rcx)
movq 0x90(%rsp), %rax
cmpq %rcx, 0xc0(%rax)
jne 0x405bc
jmp 0x406c6
movq 0x90(%rsp), %rax
cmpq $0x0, 0xc0(%rax)
je 0x406a7
movq 0x90(%rsp), %rax
movq 0xc0(%rax), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x1c(%rsp)
jmp 0x405f0
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
jle 0x406a7
leaq 0xa0(%rsp), %rdi
callq 0x1ecf0
movl %eax, 0x18(%rsp)
jmp 0x40610
movl 0x18(%rsp), %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x90(%rsp), %rax
movq 0xc0(%rax), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x14(%rsp)
jmp 0x4063c
movsd 0x8(%rsp), %xmm0
movl 0x14(%rsp), %eax
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x1bc9d9(%rip), %xmm0   # 0x1fd038
ucomisd 0x80(%rsp), %xmm0
jbe 0x4067b
movsd 0x1bc9c6(%rip), %xmm0   # 0x1fd038
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm1
mulsd 0x88(%rsp), %xmm1
movq 0x90(%rsp), %rax
movsd 0xc8(%rax), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x406a5
jmp 0x406c6
jmp 0x406a7
movq 0x78(%rsp), %rdi
movq 0x90(%rsp), %rsi
movsd 0x88(%rsp), %xmm0
callq 0x3ec40
jmp 0x406c4
jmp 0x406c6
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0x404f6
movl $0x0, 0x9c(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0x1f730
movl 0x9c(%rsp), %eax
testl %eax, %eax
je 0x40703
jmp 0x40701
jmp 0x40712
jmp 0x40705
jmp 0x40707
movl $0x1, 0x114(%rsp)
movl 0x114(%rsp), %eax
addq $0x118, %rsp            # imm = 0x118
retq
movq 0xd8(%rsp), %rdi
callq 0x16ca0
nop