// File: /workspace/github_cpp_makefile_star2_O1/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 0x2d05a
movl 0x34(%r14), %ecx
testl %ecx, %ecx
jle 0x2d061
movl 0xc(%r14), %eax
shll $0x2, %ecx
cltd
idivl %ecx
cmpl $0x2, %eax
movl $0x1, %edx
cmovgel %eax, %edx
jmp 0x2d066
movl $0x1, %ecx
jmp 0x2d08d
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 0x2d08d
movl 0xc(%r14), %eax
cltd
idivl %esi
cmpl %eax, %ecx
cmovgel %eax, %ecx
leaq 0x18(%r14), %rax
movq %rax, 0x8(%rsp)
movslq %ecx, %r15
xorl %ebx, %ebx
leaq 0x10(%rsp), %r13
movslq 0xc(%r14), %rax
cmpl %eax, %ebx
jge 0x2d179
movq (%r14), %rcx
movslq %ebx, %rbx
xorl %ebp, %ebp
movq (%rcx,%rbx,8), %rdx
cmpq $0x0, 0xc0(%rdx)
cmoveq %rdx, %rbp
addq %r15, %rbx
cmpq %rax, %rbx
jge 0x2d0d1
testq %rbp, %rbp
je 0x2d0b4
testq %rbp, %rbp
je 0x2d179
movq %r13, %rdi
movl 0x4(%rsp), %esi
callq 0x291d0
movq %r13, %rdi
movq %rbp, %rsi
xorl %edx, %edx
callq 0x29334
movl $0x140, %edi            # imm = 0x140
callq 0x15610
movq %rax, %r12
movq %rax, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x29fbe
movq 0x10(%r14), %rsi
movq %r12, %rdi
callq 0x2b4d8
testl %eax, %eax
je 0x2d151
movq %r12, %rdi
callq 0x2b842
movq %r12, %rdi
callq 0x2b8b2
movq %r14, 0x130(%r12)
movl 0x24(%r14), %edx
movl %edx, 0x138(%r12)
movq 0x8(%rsp), %rdi
movq %r12, %rsi
callq 0xc4be6
jmp 0x2d166
movq %r12, %rdi
callq 0x2ac1c
movl $0x140, %esi            # imm = 0x140
movq %r12, %rdi
callq 0x15620
leaq 0x18(%rsp), %rdi
callq 0x89530
testq %rbp, %rbp
jne 0x2d0a0
movl $0x1, %eax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2d1a4
movq %rax, %rbx
movl $0x140, %esi            # imm = 0x140
movq %r12, %rdi
callq 0x15620
jmp 0x2d1a7
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x89530
movq %rbx, %rdi
callq 0x15ae0
nop