// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::ConnectedComponentCentroidFilter(double)
void R2Grid::
ConnectedComponentCentroidFilter(RNScalar isolevel)
{
  // Compute connected components
  int *sizes = new int [ grid_size ];
  int *components = new int [ grid_size ];
  int ncomponents = ConnectedComponents(isolevel, grid_size, NULL, sizes, components);
  if (ncomponents == 0) { Clear(0); return; }

  // Allocate and initalize centroids
  int *centroid_counts = new int [ ncomponents ];
  R2Point *centroid_positions = new R2Point [ ncomponents ];
  RNScalar *centroid_values = new RNScalar [ ncomponents ];
  for (int i = 0; i < ncomponents; i++) {
    centroid_counts[i] = 0;
    centroid_positions[i] = R2zero_point;
    centroid_values[i] = 0;
  }

  // Sum positions in every connected component
  for (int i = 0; i < grid_size; i++) {
    if (grid_values[i] == R2_GRID_UNKNOWN_VALUE) continue;
    int component = components[i];
    if ((component < 0) || (component >= ncomponents)) continue;
    int ix, iy;
    IndexToIndices(i, ix, iy);
    centroid_counts[component]++;
    centroid_positions[component][0] += ix;
    centroid_positions[component][1] += iy;
  }

  // Divide by counts
  for (int i = 0; i < ncomponents; i++) {
    if (centroid_counts[i] == 0) continue;
    centroid_positions[i] /= centroid_counts[i];
  }

  // Remember original values at centroids in grid
  for (int i = 0; i < ncomponents; i++) {
    if (centroid_counts[i] == 0) continue;
    int ix = (int) (centroid_positions[i][0] + 0.5);
    int iy = (int) (centroid_positions[i][1] + 0.5);
    if ((ix < 0) || (ix >= grid_resolution[0])) continue;
    if ((iy < 0) || (iy >= grid_resolution[1])) continue;
    centroid_values[i] = GridValue(ix, iy);
  }

  // Clear out entire grid
  Clear(0);

  // Set values at centroids of connected components
  for (int i = 0; i < ncomponents; i++) {
    if (centroid_counts[i] == 0) continue;
    int ix = (int) (centroid_positions[i][0] + 0.5);
    int iy = (int) (centroid_positions[i][1] + 0.5);
    if ((ix < 0) || (ix >= grid_resolution[0])) continue;
    if ((iy < 0) || (iy >= grid_resolution[1])) continue;
    SetGridValue(ix, iy, centroid_values[i]);
  }

  // Delete temporary memory
  delete [] centroid_counts;
  delete [] centroid_positions;
  delete [] centroid_values;
  delete [] components;
  delete [] sizes;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm0, 0x8(%rsp)
movq %rdi, %rbx
movslq 0xcc(%rdi), %r14
leaq (,%r14,4), %r15
testq %r14, %r14
movq $-0x1, %rbp
cmovsq %rbp, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r12
movq %r15, %rdi
callq 0xe030
movq %rax, %r15
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
movl %r14d, %esi
xorl %edx, %edx
movq %r12, %rcx
movq %rax, %r8
callq 0x6079e
testl %eax, %eax
je 0x610d5
movq %r15, 0x18(%rsp)
movq %r12, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
movslq %eax, %rbx
leaq (,%rbx,4), %rdi
movq %rbx, %r15
shlq $0x4, %r15
movl %eax, 0x8(%rsp)
testl %eax, %eax
cmovsq %rbp, %rdi
cmovnsq %r15, %rbp
callq 0xe030
movq %rax, %r13
movq %rbp, %rdi
callq 0xe030
movq %rax, %r14
xorl %r12d, %r12d
leaq (%r14,%r12), %rdi
callq 0x680a4
addq $0x10, %r12
cmpq %r12, %r15
jne 0x60e0c
movq %r14, 0x10(%rsp)
shlq $0x3, %rbx
movl 0x8(%rsp), %ebp
testl %ebp, %ebp
movq $-0x1, %rdi
cmovnsq %rbx, %rdi
callq 0xe030
movq %rax, %r12
movl %ebp, %ebx
testl %ebp, %ebp
jle 0x60e7c
xorl %ebp, %ebp
leaq 0x487431(%rip), %r14    # 0x4e8280
movq 0x10(%rsp), %r15
movl $0x0, (%r13,%rbp,4)
movq %r15, %rdi
movq %r14, %rsi
callq 0x681de
movq $0x0, (%r12,%rbp,8)
incq %rbp
addq $0x10, %r15
cmpq %rbp, %rbx
jne 0x60e54
movq 0x30(%rsp), %rbp
movslq 0xcc(%rbp), %rsi
testq %rsi, %rsi
movq 0x18(%rsp), %r15
movl 0x8(%rsp), %r11d
movq 0x10(%rsp), %r14
jle 0x60f0e
movq 0xb8(%rbp), %rdi
movl 0xc8(%rbp), %r8d
xorl %ecx, %ecx
movsd 0x42104(%rip), %xmm0    # 0xa2fb8
movsd (%rdi,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x60ec1
jnp 0x60f06
movl (%r15,%rcx,4), %r9d
testl %r9d, %r9d
sets %al
cmpl %r11d, %r9d
setge %dl
orb %al, %dl
jne 0x60f06
movl %ecx, %eax
cltd
idivl %r8d
incl (%r13,%r9,4)
xorps %xmm1, %xmm1
cvtsi2sd %edx, %xmm1
shlq $0x4, %r9
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
movupd (%r14,%r9), %xmm2
addpd %xmm1, %xmm2
movupd %xmm2, (%r14,%r9)
incq %rcx
cmpq %rcx, %rsi
jne 0x60eb4
testl %r11d, %r11d
jle 0x60fea
movq %r12, 0x20(%rsp)
xorl %r15d, %r15d
movq %r14, %r12
movq %rbx, %rcx
movl (%r13,%r15,4), %eax
testl %eax, %eax
je 0x60f40
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq %r12, %rdi
callq 0x6828c
movq %rbx, %rcx
incq %r15
addq $0x10, %r12
cmpq %r15, %rcx
jne 0x60f25
movl 0x8(%rsp), %r11d
testl %r11d, %r11d
movq 0x18(%rsp), %r15
movq 0x20(%rsp), %r12
jle 0x60fea
movl 0xc0(%rbp), %eax
movl 0xc4(%rbp), %ecx
movq 0xb8(%rbp), %rdx
movl 0xc8(%rbp), %esi
leaq (,%rbx,4), %rdi
xorl %r8d, %r8d
movsd 0x410a0(%rip), %xmm0    # 0xa2030
cmpl $0x0, (%r13,%r8)
je 0x60fe1
movsd (%r14,%r8,4), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
testl %r9d, %r9d
js 0x60fe1
cmpl %r9d, %eax
jle 0x60fe1
movsd 0x8(%r14,%r8,4), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r10d
testl %r10d, %r10d
js 0x60fe1
cmpl %r10d, %ecx
jle 0x60fe1
imull %esi, %r10d
addl %r9d, %r10d
movslq %r10d, %r9
movsd (%rdx,%r9,8), %xmm1
movsd %xmm1, (%r12,%r8,2)
addq $0x4, %r8
cmpq %r8, %rdi
jne 0x60f90
movslq 0xcc(%rbp), %rax
testq %rax, %rax
jle 0x61024
movq 0xb8(%rbp), %rcx
xorl %edx, %edx
movsd 0x41fb1(%rip), %xmm0    # 0xa2fb8
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x61014
jnp 0x6101c
movq $0x0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x61007
testl %r11d, %r11d
jle 0x6109d
movq 0xb8(%rbp), %rax
movl 0xc8(%rbp), %ecx
shlq $0x2, %rbx
xorl %edx, %edx
movsd 0x40fec(%rip), %xmm0    # 0xa2030
cmpl $0x0, (%r13,%rdx)
je 0x61094
movsd (%r14,%rdx,4), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %esi
testl %esi, %esi
js 0x61094
cmpl %esi, 0xc0(%rbp)
jle 0x61094
movsd 0x8(%r14,%rdx,4), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
testl %edi, %edi
js 0x61094
cmpl %edi, 0xc4(%rbp)
jle 0x61094
movsd (%r12,%rdx,2), %xmm1
imull %ecx, %edi
addl %esi, %edi
movslq %edi, %rsi
movsd %xmm1, (%rax,%rsi,8)
addq $0x4, %rdx
cmpq %rdx, %rbx
jne 0x61044
movq %r13, %rdi
callq 0xe5b0
movq %r14, %rdi
callq 0xe5b0
movq %r12, %rdi
callq 0xe5b0
movq %r15, %rdi
callq 0xe5b0
movq 0x28(%rsp), %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0
movslq 0xcc(%rbx), %rax
testq %rax, %rax
jle 0x6110f
movq 0xb8(%rbx), %rcx
xorl %edx, %edx
movsd 0x41ec6(%rip), %xmm0    # 0xa2fb8
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x610ff
jnp 0x61107
movq $0x0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x610f2
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xe8c0
movq %rbx, %rdi
callq 0xe7e0