// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3Segmentation::CreateClusters(int)
int R3Segmentation::
CreateClusters(int primitive_type)
{
  // Print debug message
  RNTime step_time;
  step_time.Read();
  if (print_progress) {
    printf("      SA %.3f %d\n", step_time.Elapsed(), points.NEntries());
    step_time.Read();
  }

  // Create clusters
  if (initialize_hierarchically) {
    if (!CreateSingletonClusters(primitive_type)) return 0;
    if (!MergeClusters()) return 0;
  }
  else {
    if (!CreateRegionGrowingClusters(primitive_type)) return 0;
  }

  // Check clusters
  if (clusters.IsEmpty()) return 0;

  // Print debug message
  if (print_progress) {
    printf("      SB %.3f %d %d %g\n", step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
    step_time.Read();
  }

  // Iteratively update clusters
  for (int i = 0; i < max_refinement_iterations; i++) {
    // Reassign points to clusters
    if (!ReassignClusters()) return 0;

    // Print debug message
    if (print_progress) {
      printf("      SC %d : %.3f %d %d %g\n", i, step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
      step_time.Read();
    }

    // Create clusters
    if (!allow_outlier_points) {
      if (!CreateRegionGrowingClusters(primitive_type)) return 0;
    }

    // Print debug message
    if (print_progress) {
      printf("      SD %d : %.3f %d %d %g\n", i, step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
      step_time.Read();
    }

    // Merge clusters
    if (!MergeClusters()) return 0;

    // Print debug message
    if (print_progress) {
      printf("      SE %d : %.3f %d %d %g\n", i, step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
      step_time.Read();
    }

    // Refine boundaries
    if (!RefineBoundaries()) return 0;

    // Print debug message
    if (print_progress) {
      printf("      SF %d : %.3f %d %d %g\n", i, step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
      step_time.Read();
    }
    
    // Delete clusters
    if (!DeleteClusters()) return 0;

    // Print debug message
    if (print_progress) {
      printf("      SG %d : %.3f %d %d %g\n", i, step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
      step_time.Read();
    }
  }

  // Split clusters
  // if (!SplitClusters()) return 0;

  // Print debug message
  if (print_progress) {
    printf("      SH %.3f %d %d %g\n", step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
    step_time.Read();
  }

  // Refine boundaries
  if (!RefineBoundaries()) return 0;

  // Print debug message
  if (print_progress) {
    printf("      SI %.3f %d %d %g\n", step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
    step_time.Read();
  }

  // Delete clusters
  if (!DeleteClusters()) return 0;

  // Print debug message
  if (print_progress) {
    printf("      SJ %.3f %d %d %g\n", step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
    step_time.Read();
  }

  // Sort clusters
  clusters.Sort(CompareClusters);

  // Merge small clusters
  if (!MergeSmallClusters()) return 0;

  // Print debug message
  if (print_progress) {
    printf("      SK %.3f %d %d %g\n", step_time.Elapsed(), clusters.NEntries(), NUnclusteredPoints(), Affinity());
    step_time.Read();
  }
  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebp
movq %rdi, %rbx
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0xb1992
movq %r14, %rdi
callq 0x19eb4
cmpl $0x0, 0xdc(%rbx)
je 0x2b83b
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0xb19cc
movl 0xc(%rbx), %esi
leaq 0xc3a51(%rip), %rdi     # 0xef27d
movb $0x1, %al
callq 0x15080
movq %r14, %rdi
callq 0x19eb4
cmpl $0x0, 0xd0(%rbx)
je 0x2b858
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2aa0e
movq %rbx, %rdi
callq 0x2af8c
jmp 0x2b862
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2aaf4
cmpl $0x0, 0x24(%rbx)
je 0x2bc53
cmpl $0x0, 0xdc(%rbx)
je 0x2b8c4
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %r15d
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r12d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc39e6(%rip), %rdi     # 0xef28f
movsd 0x8(%rsp), %xmm0
movl %r15d, %esi
movl %r12d, %edx
movb $0x2, %al
callq 0x15080
movq %r14, %rdi
callq 0x19eb4
leaq 0x18(%rbx), %rax
movq %rax, 0x20(%rsp)
xorl %r15d, %r15d
leaq 0x10(%rsp), %r12
cmpl 0xb8(%rbx), %r15d
jge 0x2bacb
movq %rbx, %rdi
callq 0x2ac8e
cmpl $0x0, 0xdc(%rbx)
je 0x2b940
movq %r12, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %r14d
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r13d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc3985(%rip), %rdi     # 0xef2a7
movl %r15d, %esi
movsd 0x8(%rsp), %xmm0
movl %r14d, %edx
movl %r13d, %ecx
movb $0x2, %al
callq 0x15080
movq %r12, %rdi
callq 0x19eb4
cmpl $0x0, 0xd4(%rbx)
jne 0x2b953
movq %rbx, %rdi
movl %ebp, %esi
callq 0x2aaf4
cmpl $0x0, 0xdc(%rbx)
je 0x2b9a9
movq %r12, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %r14d
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r13d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc3939(%rip), %rdi     # 0xef2c4
movl %r15d, %esi
movsd 0x8(%rsp), %xmm0
movl %r14d, %edx
movl %r13d, %ecx
movb $0x2, %al
callq 0x15080
movq %r12, %rdi
callq 0x19eb4
movq %rbx, %rdi
callq 0x2af8c
cmpl $0x0, 0xdc(%rbx)
je 0x2ba07
movq %r12, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %r14d
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r13d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc38f8(%rip), %rdi     # 0xef2e1
movl %r15d, %esi
movsd 0x8(%rsp), %xmm0
movl %r14d, %edx
movl %r13d, %ecx
movb $0x2, %al
callq 0x15080
movq %r12, %rdi
callq 0x19eb4
movq %rbx, %rdi
callq 0x2b6c0
cmpl $0x0, 0xdc(%rbx)
je 0x2ba65
movq %r12, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %r14d
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r13d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc38b7(%rip), %rdi     # 0xef2fe
movl %r15d, %esi
movsd 0x8(%rsp), %xmm0
movl %r14d, %edx
movl %r13d, %ecx
movb $0x2, %al
callq 0x15080
movq %r12, %rdi
callq 0x19eb4
movq %rbx, %rdi
callq 0x2adee
cmpl $0x0, 0xdc(%rbx)
je 0x2bac3
movq %r12, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %r14d
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r13d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc3876(%rip), %rdi     # 0xef31b
movl %r15d, %esi
movsd 0x8(%rsp), %xmm0
movl %r14d, %edx
movl %r13d, %ecx
movb $0x2, %al
callq 0x15080
movq %r12, %rdi
callq 0x19eb4
incl %r15d
jmp 0x2b8d5
cmpl $0x0, 0xdc(%rbx)
je 0x2bb21
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %ebp
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r15d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc3831(%rip), %rdi     # 0xef338
movsd 0x8(%rsp), %xmm0
movl %ebp, %esi
movl %r15d, %edx
movb $0x2, %al
callq 0x15080
movq %r14, %rdi
callq 0x19eb4
movq %rbx, %rdi
callq 0x2b6c0
cmpl $0x0, 0xdc(%rbx)
je 0x2bb7f
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %ebp
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r15d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc37eb(%rip), %rdi     # 0xef350
movsd 0x8(%rsp), %xmm0
movl %ebp, %esi
movl %r15d, %edx
movb $0x2, %al
callq 0x15080
movq %r14, %rdi
callq 0x19eb4
movq %rbx, %rdi
callq 0x2adee
cmpl $0x0, 0xdc(%rbx)
je 0x2bbdd
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %ebp
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r15d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc37a5(%rip), %rdi     # 0xef368
movsd 0x8(%rsp), %xmm0
movl %ebp, %esi
movl %r15d, %edx
movb $0x2, %al
callq 0x15080
movq %r14, %rdi
callq 0x19eb4
leaq -0xe23(%rip), %rsi      # 0x2adc1
movq 0x20(%rsp), %rdi
callq 0xb21de
movq %rbx, %rdi
callq 0x2b570
pushq $0x1
popq %rbp
cmpl $0x0, 0xdc(%rbx)
je 0x2bc55
leaq 0x10(%rsp), %r14
movq %r14, %rdi
callq 0xb19cc
movsd %xmm0, 0x8(%rsp)
movl 0x24(%rbx), %r15d
movq %rbx, %rdi
callq 0x2a11a
movl %eax, %r12d
movq %rbx, %rdi
callq 0x2a0f0
movaps %xmm0, %xmm1
leaq 0xc374a(%rip), %rdi     # 0xef380
movsd 0x8(%rsp), %xmm0
movl %r15d, %esi
movl %r12d, %edx
movb $0x2, %al
callq 0x15080
movq %r14, %rdi
callq 0x19eb4
jmp 0x2bc55
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq