// File: /workspace/github_cpp_makefile_star2_O3/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 0x2b1ea
movsd 0x58(%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
cmpl $0x0, 0xd4(%rax)
setne %r15b
movl 0x38(%rax), %r12d
jmp 0x2b1fa
xorl %r12d, %r12d
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
movb $0x1, %r15b
movq $0x0, 0x120(%rbx)
movq %rbx, %rdi
callq 0x2a93a
cmpq $0x0, (%rbx)
je 0x2b35d
movq %rsp, %rdi
callq 0xc352c
movq (%rbx), %rsi
movsd 0xdde26(%rip), %xmm0    # 0x109050
movq %rbx, %rdi
callq 0x2a720
movq (%rbx), %rsi
movq %rsp, %rdi
movl 0xc(%rdi), %edx
callq 0xc35c2
movl 0x143412(%rip), %eax    # 0x16e658
incl %eax
movl %eax, 0x14340a(%rip)    # 0x16e658
movq (%rbx), %rcx
movl %eax, 0xd8(%rcx)
movl 0xc(%rsp), %eax
testl %eax, %eax
je 0x2b33f
movq %rsp, %r14
movq (%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r12
decl %esi
movq %r14, %rdi
callq 0xc374c
cmpl $0x0, 0xbc(%r12)
jle 0x2b333
xorl %r13d, %r13d
movq 0xb0(%r12), %rax
movq (%rax,%r13,8), %r15
movl 0x1433b8(%rip), %eax    # 0x16e658
cmpl %eax, 0xd8(%r15)
je 0x2b31f
movl %eax, 0xd8(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2a378
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x2b31f
movsd 0x120(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x120(%rbx)
movq 0xc0(%r15), %rax
cmpq %rbx, %rax
je 0x2b31f
testq %rax, %rax
je 0x2b305
movsd 0xc8(%r15), %xmm1
movapd %xmm0, %xmm2
mulsd 0xdef91(%rip), %xmm2    # 0x10a290
ucomisd %xmm2, %xmm1
ja 0x2b31f
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2a720
movl 0xc(%rsp), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0xc35c2
incq %r13
movslq 0xbc(%r12), %rax
cmpq %rax, %r13
jl 0x2b28e
movl 0xc(%rsp), %eax
testl %eax, %eax
jne 0x2b266
movq %rsp, %rdi
callq 0xc3596
movl $0x1, %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x1, %ebp
testq %r14, %r14
je 0x2b34c
movq %rsp, %rdi
callq 0xc352c
movl 0x40(%rbx), %eax
cmpl $0x3, %eax
je 0x2b3c9
cmpl $0x2, %eax
je 0x2b39f
cmpl $0x1, %eax
jne 0x2b3f3
leaq 0x80(%rbx), %rsi
movq %rsp, %rdx
xorpd %xmm0, %xmm0
movq %r14, %rdi
movsd 0x10(%rsp), %xmm1
callq 0x2e5d0
jmp 0x2b401
movq 0x50(%r14), %rsi
testq %rsi, %rsi
je 0x2b401
leaq 0x98(%rbx), %rcx
movq %rsp, %r8
xorpd %xmm0, %xmm0
movq %r14, %rdi
movq %r14, %rdx
movsd 0x10(%rsp), %xmm1
callq 0x2ec26
jmp 0x2b401
movq 0x50(%r14), %rsi
testq %rsi, %rsi
je 0x2b401
leaq 0xc8(%rbx), %rcx
movq %rsp, %r8
xorpd %xmm0, %xmm0
movq %r14, %rdi
movq %r14, %rdx
movsd 0x10(%rsp), %xmm1
callq 0x2ee0a
jmp 0x2b401
leaq 0xdeea6(%rip), %rdi     # 0x10a2a0
xorl %eax, %eax
callq 0xc5fc2
movl 0xc(%rsp), %eax
cmpl %r12d, %eax
setl %cl
testb %cl, %r15b
je 0x2b41f
movq %rsp, %rdi
callq 0xc3596
xorl %ebp, %ebp
jmp 0x2b34c
testl %eax, %eax
jle 0x2b4c6
xorl %r15d, %r15d
movq (%rsp), %rax
movq (%rax,%r15,8), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a378
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x2b4b5
movsd 0x120(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x120(%rbx)
movq 0xc0(%r14), %rax
cmpq %rbx, %rax
je 0x2b4b5
testq %rax, %rax
je 0x2b4aa
movl 0x14(%rax), %eax
testl %eax, %eax
jle 0x2b4aa
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm2, %xmm1
movsd 0xddb81(%rip), %xmm2    # 0x109010
maxsd %xmm1, %xmm2
movapd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd 0xc8(%r14), %xmm2
ucomisd %xmm1, %xmm2
ja 0x2b4b5
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a720
incq %r15
movslq 0xc(%rsp), %rax
cmpq %rax, %r15
jl 0x2b42a
movq %rsp, %rdi
callq 0xc3596
jmp 0x2b34c
jmp 0x2b4db
jmp 0x2b4db
jmp 0x2b4db
jmp 0x2b4db
movq %rax, %rbx
movq %rsp, %rdi
callq 0xc3596
movq %rbx, %rdi
callq 0x15ad0