// File: /workspace/github_cpp_makefile_star2_O2/tomfunkhouser[P]gaps/pkgs/R3Utils/R3Segmentation.cpp
// Function: gaps::R3Segmentation::CreateRegionGrowingClusters(int)
int R3Segmentation::
CreateRegionGrowingClusters(int primitive_type)
{
  // Determine how many seed points to skip each iteration
  int skip = 1;
  if (allow_outlier_points) {
    if ((max_clusters > 0) && (points.NEntries()/(4*max_clusters) > skip))
      skip = points.NEntries()/(4*max_clusters);
    if ((min_cluster_points > 0) && ((min_cluster_points/4) > skip))
      skip = min_cluster_points/4;
    if ((min_clusters > 0) && (skip > points.NEntries()/min_clusters))
      skip = points.NEntries()/min_clusters;
  }

  // Search seed points
  int seed_index = 0;
  while (seed_index < points.NEntries()) {
    // Find next seed point
    R3SegmentationPoint *seed_point = NULL;
    while ((seed_index < points.NEntries()) && !seed_point) {
      R3SegmentationPoint *point = points.Kth(seed_index);
      if (!point->cluster) seed_point = point;
      seed_index += skip; 
    }

    // Check seed point
    if (!seed_point) break;

    // Create cluster
    R3SegmentationPrimitive primitive(primitive_type);
    primitive.Update(seed_point);
    R3SegmentationCluster *cluster = new R3SegmentationCluster(seed_point, primitive);
    if (!cluster->UpdatePoints(kdtree)) { delete cluster; continue; }
    if (!cluster->UpdateColor()) { delete cluster; continue; }
    if (!cluster->UpdateTimestamp()) { delete cluster; continue; }

    // Insert cluster
    cluster->segmentation = this;
    cluster->segmentation_index = clusters.NEntries();    
    clusters.Insert(cluster);
  } 

  // Return success
  return 1;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movl %esi, 0x4(%rsp)
movq %rdi, %r14
cmpl $0x0, 0xd4(%rdi)
je 0x2ab32
movl 0x34(%r14), %ecx
testl %ecx, %ecx
jle 0x2ab37
movl 0xc(%r14), %eax
shll $0x2, %ecx
cltd
idivl %ecx
cmpl $0x2, %eax
pushq $0x1
popq %rdx
cmovgel %eax, %edx
jmp 0x2ab3a
pushq $0x1
popq %rcx
jmp 0x2ab61
pushq $0x1
popq %rdx
movl 0x30(%r14), %esi
movl 0x38(%r14), %eax
movl %eax, %ecx
shrl $0x2, %ecx
cmpl %edx, %ecx
cmovbel %edx, %ecx
testl %eax, %eax
cmovlel %edx, %ecx
testl %esi, %esi
jle 0x2ab61
movl 0xc(%r14), %eax
cltd
idivl %esi
cmpl %eax, %ecx
cmovgel %eax, %ecx
leaq 0x18(%r14), %rax
movq %rax, 0x8(%rsp)
movslq %ecx, %r12
xorl %ebx, %ebx
xorl %r13d, %r13d
movslq 0xc(%r14), %rax
cmpl %eax, %r13d
jge 0x2ac4f
movq (%r14), %rcx
movslq %r13d, %r13
xorl %r15d, %r15d
cmpq %rax, %r13
jge 0x2aba7
testq %r15, %r15
jne 0x2aba7
movq (%rcx,%r13,8), %r15
cmpq $0x0, 0xc0(%r15)
cmovneq %rbx, %r15
addq %r12, %r13
jmp 0x2ab88
testq %r15, %r15
je 0x2ac4f
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
movl 0x4(%rsp), %esi
callq 0x272bc
movq %rbp, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x2741c
movl $0x140, %edi            # imm = 0x140
callq 0x15620
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
leaq 0x10(%rsp), %rdx
callq 0x27f0a
movq 0x10(%r14), %rsi
movq %rbp, %rdi
callq 0x291fe
testl %eax, %eax
je 0x2ac2b
movq %rbp, %rdi
callq 0x29514
movq %rbp, %rdi
callq 0x29578
movq %r14, 0x130(%rbp)
movl 0x24(%r14), %eax
movl %eax, 0x138(%rbp)
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0x1a04c
jmp 0x2ac40
movq %rbp, %rdi
callq 0x28a24
movl $0x140, %esi            # imm = 0x140
movq %rbp, %rdi
callq 0x15630
leaq 0x18(%rsp), %rdi
callq 0x7cbba
jmp 0x2ab72
pushq $0x1
popq %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movl $0x140, %esi            # imm = 0x140
movq %rbp, %rdi
callq 0x15630
jmp 0x2ac7b
jmp 0x2ac78
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x7cbba
movq %rbx, %rdi
callq 0x15af0
nop