// File: /workspace/github_cpp_makefile_star2_O1/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 0x2b4ff
movsd 0x58(%rax), %xmm0
jmp 0x2b503
xorpd %xmm0, %xmm0
movsd %xmm0, 0x10(%rsp)
testq %rax, %rax
je 0x2b51b
cmpl $0x0, 0xd4(%rax)
setne %r15b
jmp 0x2b51e
movb $0x1, %r15b
testq %rax, %rax
je 0x2b529
movl 0x38(%rax), %r12d
jmp 0x2b52c
xorl %r12d, %r12d
movq $0x0, 0x120(%rbx)
movq %rbx, %rdi
callq 0x2ac8a
cmpq $0x0, (%rbx)
je 0x2b68f
movq %rsp, %rdi
callq 0xc4b50
movq (%rbx), %rsi
movsd 0xdfaf4(%rip), %xmm0    # 0x10b050
movq %rbx, %rdi
callq 0x2aa70
movq (%rbx), %rsi
movq %rsp, %rdi
movl 0xc(%rdi), %edx
callq 0xc4be6
movl 0x1450f0(%rip), %eax    # 0x170668
incl %eax
movl %eax, 0x1450e8(%rip)    # 0x170668
movq (%rbx), %rcx
movl %eax, 0xd8(%rcx)
movl 0xc(%rsp), %eax
testl %eax, %eax
je 0x2b671
movq %rsp, %r14
movq (%rsp), %rcx
movslq %eax, %rsi
movq -0x8(%rcx,%rsi,8), %r12
decl %esi
movq %r14, %rdi
callq 0xc4d5a
cmpl $0x0, 0xbc(%r12)
jle 0x2b665
xorl %r13d, %r13d
movq 0xb0(%r12), %rax
movq (%rax,%r13,8), %r15
movl 0x145096(%rip), %eax    # 0x170668
cmpl %eax, 0xd8(%r15)
je 0x2b651
movl %eax, 0xd8(%r15)
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2a6b4
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x2b651
movsd 0x120(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x120(%rbx)
movq 0xc0(%r15), %rax
cmpq %rbx, %rax
je 0x2b651
testq %rax, %rax
je 0x2b637
movsd 0xc8(%r15), %xmm1
movapd %xmm0, %xmm2
mulsd 0xe0c5f(%rip), %xmm2    # 0x10c290
ucomisd %xmm2, %xmm1
ja 0x2b651
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2aa70
movl 0xc(%rsp), %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0xc4be6
incq %r13
movslq 0xbc(%r12), %rax
cmpq %rax, %r13
jl 0x2b5c0
movl 0xc(%rsp), %eax
testl %eax, %eax
jne 0x2b598
movq %rsp, %rdi
callq 0xc4bba
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 0x2b67e
movq %rsp, %rdi
callq 0xc4b50
movl 0x40(%rbx), %eax
cmpl $0x3, %eax
je 0x2b6fb
cmpl $0x2, %eax
je 0x2b6d1
cmpl $0x1, %eax
jne 0x2b725
leaq 0x80(%rbx), %rsi
movq %rsp, %rdx
xorpd %xmm0, %xmm0
movq %r14, %rdi
movsd 0x10(%rsp), %xmm1
callq 0x2ea9a
jmp 0x2b733
movq 0x50(%r14), %rsi
testq %rsi, %rsi
je 0x2b733
leaq 0x98(%rbx), %rcx
movq %rsp, %r8
xorpd %xmm0, %xmm0
movq %r14, %rdi
movq %r14, %rdx
movsd 0x10(%rsp), %xmm1
callq 0x2f0d6
jmp 0x2b733
movq 0x50(%r14), %rsi
testq %rsi, %rsi
je 0x2b733
leaq 0xc8(%rbx), %rcx
movq %rsp, %r8
xorpd %xmm0, %xmm0
movq %r14, %rdi
movq %r14, %rdx
movsd 0x10(%rsp), %xmm1
callq 0x2f2ba
jmp 0x2b733
leaq 0xe0b74(%rip), %rdi     # 0x10c2a0
xorl %eax, %eax
callq 0xc78f2
cmpl %r12d, 0xc(%rsp)
setl %al
testb %al, %r15b
je 0x2b74f
movq %rsp, %rdi
callq 0xc4bba
xorl %ebp, %ebp
jmp 0x2b67e
cmpl $0x0, 0xc(%rsp)
jle 0x2b7f9
xorl %r15d, %r15d
movq (%rsp), %rax
movq (%rax,%r15,8), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2a6b4
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
jae 0x2b7e8
movsd 0x120(%rbx), %xmm1
addsd %xmm0, %xmm1
movsd %xmm1, 0x120(%rbx)
movq 0xc0(%r14), %rax
cmpq %rbx, %rax
je 0x2b7e8
testq %rax, %rax
je 0x2b7dd
movl 0x14(%rax), %eax
testl %eax, %eax
jle 0x2b7dd
xorps %xmm1, %xmm1
cvtsi2sdl 0xc(%rsp), %xmm1
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
divsd %xmm2, %xmm1
movsd 0xdf84e(%rip), %xmm2    # 0x10b010
maxsd %xmm1, %xmm2
movapd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd 0xc8(%r14), %xmm2
ucomisd %xmm1, %xmm2
ja 0x2b7e8
movq %rbx, %rdi
movq %r14, %rsi
callq 0x2aa70
incq %r15
movslq 0xc(%rsp), %rax
cmpq %rax, %r15
jl 0x2b75d
movq %rsp, %rdi
callq 0xc4bba
jmp 0x2b67e
jmp 0x2b80e
jmp 0x2b80e
jmp 0x2b80e
jmp 0x2b80e
movq %rax, %rbx
movq %rsp, %rdi
callq 0xc4bba
movq %rbx, %rdi
callq 0x15ae0
nop