// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3Segmentation::DeleteClusters()
int R3Segmentation::
DeleteClusters(void)
{
  // Check if should delete clusters
  if (!allow_outlier_points) return 1;
  
  // Sort clusters
  clusters.Sort(CompareClusters);

  // Separate viable from nonviable ones
  RNArray<R3SegmentationCluster *> viable_clusters;
  RNArray<R3SegmentationCluster *> nonviable_clusters;
  for (int i = 0; i < clusters.NEntries(); i++) {
    R3SegmentationCluster *cluster = clusters.Kth(i);

    // Check min_clusters
    if ((min_clusters <= 0) || (i >= min_clusters)) {
      // Check cluster points
      if (min_cluster_points > 0) {
        if (cluster->points.NEntries() < min_cluster_points) {
          nonviable_clusters.Insert(cluster);
          continue;
        }
      }

      // Check cluster area
      if (min_cluster_area > 0) {
        if (cluster->area < min_cluster_area) {
          nonviable_clusters.Insert(cluster);
          continue;
        }
      }

      // Check cluster coverage
      if (min_cluster_coverage > 0) {
        if (cluster->Coverage() < min_cluster_coverage) {
          nonviable_clusters.Insert(cluster);
          continue;
        }
      }

      // Check max_clusters
      if (max_clusters > 0) {
        if (viable_clusters.NEntries() >= max_clusters) {
          nonviable_clusters.Insert(cluster);
          continue;
        }
      }
    }
    
    // R3SegmentationCluster is viable
    cluster->segmentation = this;
    cluster->segmentation_index = viable_clusters.NEntries();    
    viable_clusters.Insert(cluster);
  }

  // Delete nonviable clusters
  for (int i = 0; i < nonviable_clusters.NEntries(); i++) {
    R3SegmentationCluster *cluster = nonviable_clusters.Kth(i);
    delete cluster;
  }

  // Replace clusters with viable ones
  clusters = viable_clusters;

  // Return success
  return 1;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x20, %rsp
cmpl $0x0, 0xd4(%rdi)
je 0x2af57
movq %rdi, %r14
leaq 0x18(%rdi), %rbx
leaq -0x55(%rip), %rsi       # 0x2adc1
movq %rbx, %rdi
callq 0xb21de
movq %rsp, %r15
movq %r15, %rdi
callq 0xb1eda
leaq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0xb1eda
xorl %r13d, %r13d
movslq 0x24(%r14), %rax
cmpq %rax, %r13
jge 0x2af05
movq 0x18(%r14), %rax
movq (%rax,%r13,8), %rsi
movslq 0x30(%r14), %rax
cmpq %rax, %r13
jge 0x2ae6d
movl 0xc(%rsp), %eax
movq %r14, 0x130(%rsi)
movl %eax, 0x138(%rsi)
movq %r15, %rdi
jmp 0x2aed4
movl 0x38(%r14), %eax
testl %eax, %eax
jle 0x2ae7d
movq %r12, %rdi
cmpl %eax, 0x14(%rsi)
jl 0x2aed4
movsd 0x40(%r14), %xmm0
ucomisd 0xc31bd(%rip), %xmm0    # 0xee048
jbe 0x2ae9a
ucomisd 0xe8(%rsi), %xmm0
movq %r12, %rdi
ja 0x2aed4
movsd 0x48(%r14), %xmm0
ucomisd 0xc31a0(%rip), %xmm0    # 0xee048
jbe 0x2aee1
movsd 0x120(%rsi), %xmm1
movsd 0x128(%rsi), %xmm2
divsd %xmm1, %xmm2
cmpeqsd 0xc3181(%rip), %xmm1    # 0xee048
andnpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
movq %r12, %rdi
jbe 0x2aee1
callq 0x1a04c
incq %r13
jmp 0x2ae39
movl 0x34(%r14), %ecx
testl %ecx, %ecx
setle %dl
movl 0xc(%rsp), %eax
cmpl %ecx, %eax
setl %cl
orb %dl, %cl
jne 0x2ae5b
movq %r12, %rdi
callq 0x1a04c
jmp 0x2aed9
xorl %r15d, %r15d
movslq 0x1c(%rsp), %rax
cmpq %rax, %r15
jge 0x2af3a
movq 0x10(%rsp), %rax
movq (%rax,%r15,8), %r14
testq %r14, %r14
je 0x2af28
movq %r14, %rdi
callq 0x28a24
movl $0x140, %esi            # imm = 0x140
movq %r14, %rdi
callq 0x15630
incq %r15
jmp 0x2af08
movq %rsp, %rsi
movq %rbx, %rdi
callq 0xb1eee
leaq 0x10(%rsp), %rdi
callq 0xb1f48
movq %rsp, %rdi
callq 0xb1f48
pushq $0x1
popq %rax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x2af6f
movq %rax, %rbx
jmp 0x2af7c
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0xb1f48
movq %rsp, %rdi
callq 0xb1f48
movq %rbx, %rdi
callq 0x15af0