// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq 0x130(%rdi), %rax
testq %rax, %rax
je 0x2923a
movsd 0x58(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl $0x0, 0xd4(%rax)
setne %r15b
movl 0x38(%rax), %r12d
jmp 0x2924a
xorl %r12d, %r12d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movb $0x1, %r15b
andq $0x0, 0x120(%rbx)
movq %rbx, %rdi
callq 0x28a76
cmpq $0x0, (%rbx)
je 0x29384
movq %rsp, %rdi
callq 0xb1eda
movq (%rbx), %rsi
movsd 0xc4d99(%rip), %xmm0    # 0xee010
movq %rbx, %rdi
callq 0x288ce
movq (%rbx), %rsi
movq %rsp, %rdi
callq 0x1a04c
movl 0x1323e8(%rip), %eax    # 0x15b678
incl %eax
movl %eax, 0x1323e0(%rip)    # 0x15b678
movq (%rbx), %rcx
movl %eax, 0xd8(%rcx)
movq %rsp, %r14
movslq 0xc(%rsp), %rax
testq %rax, %rax
je 0x29368
movq (%rsp), %rcx
movq -0x8(%rcx,%rax,8), %r12
movq %r14, %rdi
callq 0x1df5e
xorl %r13d, %r13d
movslq 0xbc(%r12), %rax
cmpq %rax, %r13
jge 0x292a4
movq 0xb0(%r12), %rax
movq (%rax,%r13,8), %r15
movl 0x132393(%rip), %eax    # 0x15b678
cmpl %eax, 0xd8(%r15)
je 0x29360
movl %eax, 0xd8(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x28526
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x29360
movsd 0x120(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x120(%rbx)
movq 0xc0(%r15), %rax
cmpq %rbx, %rax
je 0x29360
testq %rax, %rax
je 0x2934a
movsd 0xc8(%r15), %xmm1
movapd %xmm0, %xmm2
mulsd 0xc5ecc(%rip), %xmm2    # 0xef210
ucomisd %xmm2, %xmm1
ja 0x29360
movq %rbx, %rdi
movq %r15, %rsi
callq 0x288ce
movq %r14, %rdi
movq %r15, %rsi
callq 0x1a04c
incq %r13
jmp 0x292c6
movq %rsp, %rdi
callq 0xb1f48
pushq $0x1
popq %rbp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %rbp
testq %r14, %r14
je 0x29373
movq %rsp, %rdi
callq 0xb1eda
movl 0x40(%rbx), %eax
cmpl $0x3, %eax
je 0x293e2
cmpl $0x2, %eax
je 0x293c4
cmpl $0x1, %eax
jne 0x29400
leaq 0x80(%rbx), %rsi
movq %rsp, %rdx
xorpd %xmm0, %xmm0
movq %r14, %rdi
movsd 0x10(%rsp), %xmm1
callq 0x2c2b2
jmp 0x2940e
leaq 0x98(%rbx), %rsi
movq %rsp, %rdx
xorpd %xmm0, %xmm0
movq %r14, %rdi
movsd 0x10(%rsp), %xmm1
callq 0x2c308
jmp 0x2940e
leaq 0xc8(%rbx), %rsi
movq %rsp, %rdx
xorpd %xmm0, %xmm0
movq %r14, %rdi
movsd 0x10(%rsp), %xmm1
callq 0x2c32c
jmp 0x2940e
leaq 0xc5e19(%rip), %rdi     # 0xef220
xorl %eax, %eax
callq 0xb4478
movl 0xc(%rsp), %eax
cmpl %r12d, %eax
setl %cl
testb %cl, %r15b
je 0x2942c
movq %rsp, %rdi
callq 0xb1f48
xorl %ebp, %ebp
jmp 0x29373
xorl %r15d, %r15d
cltq
cmpq %rax, %r15
jge 0x294cb
movq (%rsp), %rax
movq (%rax,%r15,8), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x28526
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x294bf
movsd 0x120(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x120(%rbx)
movq 0xc0(%r14), %rax
cmpq %rbx, %rax
je 0x294bf
testq %rax, %rax
je 0x294b4
movl 0x14(%rax), %eax
testl %eax, %eax
jle 0x294b4
cvtsi2sdl 0xc(%rsp), %xmm1
cvtsi2sd %eax, %xmm2
divsd %xmm2, %xmm1
movsd 0xc4b8f(%rip), %xmm2    # 0xee028
maxsd %xmm1, %xmm2
movapd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd 0xc8(%r14), %xmm2
ucomisd %xmm1, %xmm2
ja 0x294bf
movq %rbx, %rdi
movq %r14, %rsi
callq 0x288ce
incq %r15
movl 0xc(%rsp), %eax
jmp 0x2942f
movq %rsp, %rdi
callq 0xb1f48
jmp 0x29373
jmp 0x294e0
jmp 0x294e0
jmp 0x294e0
jmp 0x294e0
movq %rax, %rbx
movq %rsp, %rdi
callq 0xb1f48
movq %rbx, %rdi
callq 0x15af0
nop