// File: 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:
subq $0x98, %rsp
movq %rdi, 0x90(%rsp)
movq 0x90(%rsp), %rax
movq %rax, 0x40(%rsp)
movl $0x0, 0x8c(%rsp)
movq 0x40(%rsp), %rcx
movl 0x8c(%rsp), %eax
cmpl 0xbc(%rcx), %eax
jge 0x444b5
movq 0x40(%rsp), %rsi
addq $0x18, %rsi
leaq 0x78(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x47d60
movq 0x38(%rsp), %rdi
leaq 0x27d(%rip), %rsi       # 0x444d0
callq 0x173cc0
jmp 0x4425a
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
xorl %esi, %esi
callq 0x173a50
jmp 0x4426c
movl $0x1, 0x68(%rsp)
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
movl %eax, 0x30(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x1ecf0
movl %eax, 0x34(%rsp)
jmp 0x44294
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jge 0x4446e
movl 0x64(%rsp), %esi
leaq 0x78(%rsp), %rdi
callq 0x20270
movq %rax, 0x28(%rsp)
jmp 0x442b9
movq 0x28(%rsp), %rax
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x24(%rsp)
jmp 0x442d7
movl 0x24(%rsp), %eax
movl %eax, 0x54(%rsp)
movl $0x0, 0x50(%rsp)
cmpl $0x0, 0x50(%rsp)
jne 0x4432e
movq 0x58(%rsp), %rdi
callq 0x40730
movl %eax, 0x20(%rsp)
jmp 0x442fe
movl 0x20(%rsp), %eax
cmpl $0x0, %eax
jne 0x4432e
movl $0x1, 0x50(%rsp)
jmp 0x4432e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x70(%rsp)
movl %eax, 0x6c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x476a0
jmp 0x444c2
cmpl $0x0, 0x50(%rsp)
jne 0x4435f
movq 0x40(%rsp), %rax
movq 0x58(%rsp), %rdi
movq 0x10(%rax), %rsi
callq 0x40020
movl %eax, 0x1c(%rsp)
jmp 0x4434e
movl 0x1c(%rsp), %eax
cmpl $0x0, %eax
jne 0x4435f
movl $0x1, 0x50(%rsp)
cmpl $0x0, 0x50(%rsp)
jne 0x44387
movq 0x58(%rsp), %rdi
callq 0x40780
movl %eax, 0x18(%rsp)
jmp 0x44376
movl 0x18(%rsp), %eax
cmpl $0x0, %eax
jne 0x44387
movl $0x1, 0x50(%rsp)
cmpl $0x0, 0x50(%rsp)
jne 0x443af
movq 0x58(%rsp), %rdi
callq 0x40860
movl %eax, 0x14(%rsp)
jmp 0x4439e
movl 0x14(%rsp), %eax
cmpl $0x0, %eax
jne 0x443af
movl $0x1, 0x50(%rsp)
movq 0x40(%rsp), %rdi
movq 0x58(%rsp), %rax
movq %rdi, 0x130(%rax)
addq $0x18, %rdi
callq 0x1ecf0
movl %eax, 0x10(%rsp)
jmp 0x443cf
movl 0x10(%rsp), %ecx
movq 0x58(%rsp), %rax
movl %ecx, 0x138(%rax)
cmpl $0x0, 0x50(%rsp)
jne 0x443fc
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
movq 0x58(%rsp), %rsi
callq 0x476c0
jmp 0x443fa
jmp 0x44427
movq 0x58(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x44425
movq 0x8(%rsp), %rdi
callq 0x3ee60
movq 0x8(%rsp), %rdi
movl $0x140, %esi            # imm = 0x140
callq 0x16750
jmp 0x44427
cmpl $0x0, 0x50(%rsp)
jne 0x44454
movl 0x54(%rsp), %eax
movl %eax, (%rsp)
movq 0x58(%rsp), %rdi
addq $0x8, %rdi
callq 0x1ecf0
movl %eax, 0x4(%rsp)
jmp 0x44449
movl (%rsp), %eax
movl 0x4(%rsp), %ecx
cmpl %ecx, %eax
je 0x4445c
movl $0x0, 0x68(%rsp)
jmp 0x4445e
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x4427c
cmpl $0x0, 0x68(%rsp)
je 0x4447f
movl $0x2, 0x4c(%rsp)
jmp 0x44487
movl $0x0, 0x4c(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x476a0
movl 0x4c(%rsp), %eax
testl %eax, %eax
je 0x4449d
jmp 0x4449b
jmp 0x444b5
jmp 0x4449f
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0x44217
movl $0x1, %eax
addq $0x98, %rsp
retq
movq 0x70(%rsp), %rdi
callq 0x16ca0
nopl (%rax)