// File: /workspace/github_cpp_makefile_star2_O3/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 0x2d0e1
movq %rdi, %r14
leaq 0x18(%rdi), %rbx
leaq -0x56(%rip), %rsi       # 0x2cf3a
movq %rbx, %rdi
callq 0xc38c2
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0xc352c
movq %rsp, %r12
movq %r12, %rdi
callq 0xc352c
cmpl $0x0, 0x24(%r14)
jle 0x2d089
xorl %r13d, %r13d
movq 0x18(%r14), %rax
movq (%rax,%r13,8), %rsi
movslq 0x30(%r14), %rax
cmpq %rax, %r13
jge 0x2cfe5
movl 0x1c(%rsp), %edx
movq %r14, 0x130(%rsi)
movl %edx, 0x138(%rsi)
movq %r15, %rdi
jmp 0x2d04a
movl 0x38(%r14), %eax
testl %eax, %eax
jle 0x2cff2
cmpl %eax, 0x14(%rsi)
jl 0x2d043
movsd 0x40(%r14), %xmm0
ucomisd 0xdc058(%rip), %xmm0    # 0x109058
jbe 0x2d00c
ucomisd 0xe8(%rsi), %xmm0
ja 0x2d043
movsd 0x48(%r14), %xmm0
ucomisd 0xdc03e(%rip), %xmm0    # 0x109058
jbe 0x2d061
movsd 0x120(%rsi), %xmm1
movsd 0x128(%rsi), %xmm2
divsd %xmm1, %xmm2
cmpeqsd 0xdc01f(%rip), %xmm1    # 0x109058
andnpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x2d061
movl 0xc(%rsp), %edx
movq %r12, %rdi
callq 0xc35c2
incq %r13
movslq 0x24(%r14), %rax
cmpq %rax, %r13
jl 0x2cfbe
jmp 0x2d089
movl 0x34(%r14), %eax
testl %eax, %eax
setle %cl
movl 0x1c(%rsp), %edx
cmpl %eax, %edx
setl %al
orb %cl, %al
jne 0x2cfd3
movl 0xc(%rsp), %edx
movq %r12, %rdi
callq 0xc35c2
jmp 0x2d04f
cmpl $0x0, 0xc(%rsp)
jle 0x2d0c2
xorl %r15d, %r15d
movq (%rsp), %rax
movq (%rax,%r15,8), %r14
testq %r14, %r14
je 0x2d0a8
movq %r14, %rdi
callq 0x2a8cc
movl $0x140, %esi            # imm = 0x140
movq %r14, %rdi
callq 0x15620
incq %r15
movslq 0xc(%rsp), %rax
cmpq %rax, %r15
jl 0x2d093
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0xc3540
movq %rsp, %rdi
callq 0xc3596
leaq 0x10(%rsp), %rdi
callq 0xc3596
movl $0x1, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x2d0fb
movq %rax, %rbx
jmp 0x2d106
movq %rax, %rbx
movq %rsp, %rdi
callq 0xc3596
leaq 0x10(%rsp), %rdi
callq 0xc3596
movq %rbx, %rdi
callq 0x15ad0