// File: /workspace/github_cpp_makefile_star2_O2/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 $0x28, %rsp
movsd %xmm0, (%rsp)
movq %rdi, %r12
movslq 0xcc(%rdi), %rbx
leaq (,%rbx,4), %r14
testq %rbx, %rbx
pushq $-0x1
popq %r13
cmovsq %r13, %r14
movq %r14, %rdi
callq 0xe030
movq %rax, %r15
movq %r14, %rdi
callq 0xe030
movq %r12, %rdi
movsd (%rsp), %xmm0
movl %ebx, %esi
xorl %edx, %edx
movq %r15, %rcx
movq %rax, 0x10(%rsp)
movq %rax, %r8
callq 0x584b8
testl %eax, %eax
je 0x58d94
movq %r12, 0x18(%rsp)
movq %r15, 0x20(%rsp)
movslq %eax, %r12
leaq (,%r12,4), %rdi
movq %r12, %r15
shlq $0x4, %r15
movl %eax, 0xc(%rsp)
testl %eax, %eax
cmovsq %r13, %rdi
movq %r15, %rbp
cmovsq %r13, %rbp
callq 0xe030
movq %rax, %r13
movq %rbp, %rdi
callq 0xe030
movq %rax, %r14
xorl %ebx, %ebx
leaq (%r14,%rbx), %rdi
callq 0x5f58a
addq $0x10, %rbx
cmpq %rbx, %r15
jne 0x58b1d
shlq $0x3, %r12
xorl %ebx, %ebx
movl 0xc(%rsp), %eax
testl %eax, %eax
pushq $-0x1
popq %rcx
cmovsq %rcx, %r12
movl $0x0, %ebp
cmovgl %eax, %ebp
movq %r12, %rdi
callq 0xe030
movq %rax, (%rsp)
leaq 0x484733(%rip), %r15    # 0x4dd290
movq %r14, %r12
cmpq %rbx, %rbp
je 0x58b88
andl $0x0, (%r13,%rbx,4)
movq %r12, %rdi
movq %r15, %rsi
callq 0x5f6c4
movq (%rsp), %rax
andq $0x0, (%rax,%rbx,8)
incq %rbx
addq $0x10, %r12
jmp 0x58b60
movq 0x18(%rsp), %r12
movl 0xcc(%r12), %esi
movq 0xb8(%r12), %rdi
movl 0xc8(%r12), %r8d
xorl %ecx, %ecx
testl %esi, %esi
cmovlel %ecx, %esi
movsd 0x3b3b4(%rip), %xmm0    # 0x93f68
movq 0x10(%rsp), %r10
movl 0xc(%rsp), %r11d
cmpq %rcx, %rsi
je 0x58c14
movsd (%rdi,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x58bd0
jnp 0x58c0f
movl (%r10,%rcx,4), %r9d
testl %r9d, %r9d
sets %al
cmpl %r11d, %r9d
setge %dl
orb %al, %dl
jne 0x58c0f
movl %ecx, %eax
cltd
idivl %r8d
incl (%r13,%r9,4)
cvtsi2sd %edx, %xmm1
shlq $0x4, %r9
cvtsi2sd %eax, %xmm2
unpcklpd %xmm2, %xmm1            # xmm1 = xmm1[0],xmm2[0]
movupd (%r14,%r9), %xmm2
addpd %xmm1, %xmm2
movupd %xmm2, (%r14,%r9)
incq %rcx
jmp 0x58bbe
xorl %r15d, %r15d
movq %r14, %rbx
cmpq %r15, %rbp
je 0x58c3d
movl (%r13,%r15,4), %eax
testl %eax, %eax
je 0x58c34
cvtsi2sd %eax, %xmm0
movq %rbx, %rdi
callq 0x5f772
incq %r15
addq $0x10, %rbx
jmp 0x58c1a
movl 0xc0(%r12), %eax
movl 0xc4(%r12), %ecx
movq 0xb8(%r12), %rdx
movl 0xc8(%r12), %esi
addq %rbp, %rbp
xorl %edi, %edi
movsd 0x3a3a6(%rip), %xmm1    # 0x93010
movq 0x20(%rsp), %rbx
movq 0x10(%rsp), %r15
cmpq %rdi, %rbp
je 0x58cd4
cmpl $0x0, (%r13,%rdi,2)
je 0x58cce
movsd (%r14,%rdi,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r8d
testl %r8d, %r8d
js 0x58cce
cmpl %r8d, %eax
jle 0x58cce
movsd 0x8(%r14,%rdi,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r9d
testl %r9d, %r9d
js 0x58cce
cmpl %r9d, %ecx
jle 0x58cce
imull %esi, %r9d
addl %r8d, %r9d
movslq %r9d, %r8
movsd (%rdx,%r8,8), %xmm0
movq (%rsp), %r8
movsd %xmm0, (%r8,%rdi,4)
addq $0x2, %rdi
jmp 0x58c74
xorpd %xmm0, %xmm0
movq %r12, %rdi
callq 0x53578
movsd 0x3a328(%rip), %xmm1    # 0x93010
movq 0xb8(%r12), %rax
movl 0xc8(%r12), %ecx
xorl %edx, %edx
movq (%rsp), %r8
cmpq %rdx, %rbp
je 0x58d5d
cmpl $0x0, (%r13,%rdx,2)
je 0x58d57
movsd (%r14,%rdx,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %esi
testl %esi, %esi
js 0x58d57
cmpl %esi, 0xc0(%r12)
jle 0x58d57
movsd 0x8(%r14,%rdx,8), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edi
testl %edi, %edi
js 0x58d57
cmpl %edi, 0xc4(%r12)
jle 0x58d57
movsd (%r8,%rdx,4), %xmm0
imull %ecx, %edi
addl %esi, %edi
movslq %edi, %rsi
movsd %xmm0, (%rax,%rsi,8)
addq $0x2, %rdx
jmp 0x58cfe
movq %r13, %rdi
callq 0xe5d0
movq %r14, %rdi
callq 0xe5d0
movq (%rsp), %rdi
callq 0xe5d0
movq %r15, %rdi
callq 0xe5d0
movq %rbx, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5d0
xorps %xmm0, %xmm0
movq %r12, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x53578
movq %rax, %rbx
movq %r14, %rdi
movq %rbp, %rsi
callq 0xe8f0
movq %rbx, %rdi
callq 0xe810
nop