// File: /workspace/github_cpp_makefile_star2_O3/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 0x2cbd0
movl 0x34(%r14), %ecx
testl %ecx, %ecx
jle 0x2cbd7
movl 0xc(%r14), %eax
shll $0x2, %ecx
cltd
idivl %ecx
cmpl $0x2, %eax
movl $0x1, %edx
cmovgel %eax, %edx
jmp 0x2cbdc
movl $0x1, %ecx
jmp 0x2cc03
movl $0x1, %edx
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 0x2cc03
movl 0xc(%r14), %eax
cltd
idivl %esi
cmpl %eax, %ecx
cmovgel %eax, %ecx
movl 0xc(%r14), %edx
testl %edx, %edx
jle 0x2cd31
leaq 0x18(%r14), %rax
movq %rax, 0x8(%rsp)
movslq %ecx, %r12
xorl %ebx, %ebx
leaq 0x10(%rsp), %r13
movq (%r14), %rax
movslq %ebx, %rbx
movslq %edx, %rcx
movq (%rax,%rbx,8), %r15
cmpq $0x0, 0xc0(%r15)
je 0x2cc3c
xorl %r15d, %r15d
addq %r12, %rbx
cmpq %rcx, %rbx
jge 0x2cc49
testq %r15, %r15
je 0x2cc2b
testq %r15, %r15
je 0x2cd31
movq %r13, %rdi
movl 0x4(%rsp), %esi
callq 0x28edc
movq %r13, %rdi
movq %r15, %rsi
xorl %edx, %edx
callq 0x29040
movl $0x140, %edi            # imm = 0x140
callq 0x15610
movq %rax, %rbp
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x29c80
movq 0x10(%r14), %rsi
movq %rbp, %rdi
callq 0x2b1ae
testl %eax, %eax
je 0x2ccd2
movq %rbp, %rdi
callq 0x2b50e
movq $0x0, 0x108(%rbp)
movl 0x14(%rbp), %eax
testq %rax, %rax
je 0x2ccfd
testl %eax, %eax
jle 0x2cce9
movq 0x8(%rbp), %rcx
xorpd %xmm0, %xmm0
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
addsd 0x68(%rsi), %xmm0
incq %rdx
cmpq %rdx, %rax
jne 0x2ccbf
jmp 0x2cced
movq %rbp, %rdi
callq 0x2a8cc
movl $0x140, %esi            # imm = 0x140
movq %rbp, %rdi
callq 0x15620
jmp 0x2cd1b
xorpd %xmm0, %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x108(%rbp)
movq %r14, 0x130(%rbp)
movl 0x24(%r14), %edx
movl %edx, 0x138(%rbp)
movq 0x8(%rsp), %rdi
movq %rbp, %rsi
callq 0xc35c2
leaq 0x18(%rsp), %rdi
callq 0x88520
movl 0xc(%r14), %edx
cmpl %ebx, %edx
jg 0x2cc22
movl $0x1, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2cd5c
movq %rax, %rbx
movl $0x140, %esi            # imm = 0x140
movq %rbp, %rdi
callq 0x15620
jmp 0x2cd5f
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x88520
movq %rbx, %rdi
callq 0x15ad0
nop