// File: /workspace/github_cpp_makefile_star2_O1/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 0x2d4dc
movq %rdi, %r14
leaq 0x18(%rdi), %rbx
leaq -0x57(%rip), %rsi       # 0x2d341
movq %rbx, %rdi
callq 0xc4ec4
leaq 0x10(%rsp), %r15
movq %r15, %rdi
callq 0xc4b50
movq %rsp, %r12
movq %r12, %rdi
callq 0xc4b50
cmpl $0x0, 0x24(%r14)
jle 0x2d484
xorl %r13d, %r13d
movq 0x18(%r14), %rax
movq (%rax,%r13,8), %rsi
movslq 0x30(%r14), %rax
testq %rax, %rax
setg %cl
cmpq %rax, %r13
setl %al
testb %al, %cl
je 0x2d3f8
movq %r14, 0x130(%rsi)
movl 0x1c(%rsp), %edx
movl %edx, 0x138(%rsi)
movq %r15, %rdi
jmp 0x2d46f
movl 0x38(%r14), %eax
testl %eax, %eax
jle 0x2d405
cmpl %eax, 0x14(%rsi)
jl 0x2d468
movsd 0x40(%r14), %xmm0
ucomisd 0xddc45(%rip), %xmm0    # 0x10b058
jbe 0x2d41f
ucomisd 0xe8(%rsi), %xmm0
ja 0x2d468
movsd 0x48(%r14), %xmm0
ucomisd 0xddc2b(%rip), %xmm0    # 0x10b058
jbe 0x2d456
movsd 0x120(%rsi), %xmm1
movsd 0x128(%rsi), %xmm2
divsd %xmm1, %xmm2
cmpeqsd 0xddc0c(%rip), %xmm1    # 0x10b058
andnpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
ja 0x2d468
movl 0x34(%r14), %eax
testl %eax, %eax
jle 0x2d3e2
cmpl %eax, 0x1c(%rsp)
jl 0x2d3e2
movl 0xc(%rsp), %edx
movq %r12, %rdi
callq 0xc4be6
incq %r13
movslq 0x24(%r14), %rax
cmpq %rax, %r13
jl 0x2d3c6
cmpl $0x0, 0xc(%rsp)
jle 0x2d4bd
xorl %r15d, %r15d
movq (%rsp), %rax
movq (%rax,%r15,8), %r14
testq %r14, %r14
je 0x2d4b0
movq %r14, %rdi
callq 0x2ac1c
movl $0x140, %esi            # imm = 0x140
movq %r14, %rdi
callq 0x15620
incq %r15
movslq 0xc(%rsp), %rax
cmpq %rax, %r15
jl 0x2d48e
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
callq 0xc4b64
movq %rsp, %rdi
callq 0xc4bba
leaq 0x10(%rsp), %rdi
callq 0xc4bba
movl $0x1, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
jmp 0x2d4f6
movq %rax, %rbx
jmp 0x2d501
movq %rax, %rbx
movq %rsp, %rdi
callq 0xc4bba
leaq 0x10(%rsp), %rdi
callq 0xc4bba
movq %rbx, %rdi
callq 0x15ae0
nop