// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3Segmentation::ReassignClusters()
int R3Segmentation::
ReassignClusters(void)
{
  // Iteratively update everything
  for (int iter = 0; iter < max_reassignment_iterations; iter++) {
    // Copy list of clusters
    RNArray<R3SegmentationCluster *> tmp = clusters;
    tmp.Sort(CompareClusters);

    // Will rebuild list of clusters
    clusters.Empty();

    // Update each cluster
    RNBoolean converged = TRUE;
    for (int i = 0; i < tmp.NEntries(); i++) {
      R3SegmentationCluster *cluster = tmp.Kth(i);
      int prev_npoints = cluster->points.NEntries();

      // Update cluster (including reassigning points)
      RNBoolean error = FALSE;
      if (!error && !cluster->UpdatePrimitive()) error = TRUE;
      if (!error && !cluster->UpdatePoints(kdtree)) error = TRUE; 
      if (!error && !cluster->UpdateColor()) error = TRUE; 
      if (!error && !cluster->UpdateTimestamp()) error = TRUE; 

      // Insert cluster
      cluster->segmentation = this;
      cluster->segmentation_index = clusters.NEntries();    
      if (!error) clusters.Insert(cluster);
      else delete cluster;

      // Check for convergence
      if (error || (prev_npoints != cluster->points.NEntries())) {
        converged = FALSE;
      }
    }

    // Check if converged
    if (converged) break;
  }

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
cmpl $0x0, 0xbc(%rdi)
jle 0x2cf0f
movq %rdi, %rbx
leaq 0x18(%rdi), %r14
xorl %r15d, %r15d
leaq 0x8(%rsp), %r12
movq %r14, 0x18(%rsp)
movq %r12, %rdi
movq %r14, %rsi
callq 0xc3534
movq %r12, %rdi
leaq 0x184(%rip), %rsi       # 0x2cf3a
callq 0xc38c2
movq %r14, %rdi
xorl %esi, %esi
callq 0xc378c
cmpl $0x0, 0x14(%rsp)
jle 0x2cf05
movq %r15, 0x20(%rsp)
movl $0x1, 0x4(%rsp)
xorl %r15d, %r15d
movq 0x8(%rsp), %rax
movq (%rax,%r15,8), %r13
leaq 0x8(%r13), %rbp
movl 0x14(%r13), %r14d
leaq 0x40(%r13), %r12
movq (%r13), %rsi
movq %r12, %rdi
movq %rbp, %rdx
callq 0x29040
cmpl $0x0, (%r12)
je 0x2ce58
movq 0x10(%rbx), %rsi
movq %r13, %rdi
callq 0x2b1ae
testl %eax, %eax
je 0x2ce58
movq %r13, %rdi
callq 0x2b50e
movq $0x0, 0x108(%r13)
movl 0x14(%r13), %eax
testq %rax, %rax
je 0x2ce98
testl %eax, %eax
jle 0x2ce80
movq (%rbp), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
addsd 0x68(%rsi), %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x2ce45
jmp 0x2ce84
movq %rbx, 0x130(%r13)
movl 0x24(%rbx), %eax
movl %eax, 0x138(%r13)
movq %r13, %rdi
callq 0x2a8cc
movl $0x140, %esi            # imm = 0x140
movq %r13, %rdi
callq 0x15620
jmp 0x2cebc
xorpd %xmm0, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x108(%r13)
movq %rbx, 0x130(%r13)
movl 0x24(%rbx), %edx
movl %edx, 0x138(%r13)
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0xc35c2
cmpl 0x14(%r13), %r14d
je 0x2cec4
movl $0x0, 0x4(%rsp)
incq %r15
movslq 0x14(%rsp), %rax
cmpq %rax, %r15
jl 0x2cde0
leaq 0x8(%rsp), %r12
movq %r12, %rdi
callq 0xc3596
cmpl $0x0, 0x4(%rsp)
movq 0x18(%rsp), %r14
movq 0x20(%rsp), %r15
jne 0x2cf0f
incl %r15d
cmpl 0xbc(%rbx), %r15d
jl 0x2cda1
jmp 0x2cf0f
leaq 0x8(%rsp), %rdi
callq 0xc3596
movl $0x1, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2cf25
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0xc3596
movq %rbx, %rdi
callq 0x15ad0