// File: /workspace/github_cpp_makefile_star2_O1/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, %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 (%rsp), %xmm0
movl %r14d, %esi
xorl %edx, %edx
movq %r12, %rcx
movq %rax, %r8
callq 0x6158e
testl %eax, %eax
je 0x61ed4
movq %r15, 0x8(%rsp)
movq %r12, 0x18(%rsp)
movq %rbx, 0x20(%rsp)
movslq %eax, %rbx
leaq (,%rbx,4), %rdi
movq %rbx, %r15
shlq $0x4, %r15
movl %eax, (%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 0x68e2c
addq $0x10, %r12
cmpq %r12, %r15
jne 0x61c1f
shlq $0x3, %rbx
movl (%rsp), %ebp
testl %ebp, %ebp
movq $-0x1, %rdi
cmovnsq %rbx, %rdi
callq 0xe030
movq %rax, %rbx
movl %ebp, %r12d
testl %ebp, %ebp
jle 0x61c85
xorl %ebp, %ebp
movq %r14, %r15
movl $0x0, (%r13,%rbp,4)
movq %r15, %rdi
leaq 0x486614(%rip), %rsi    # 0x4e8280
callq 0x68f66
movq $0x0, (%rbx,%rbp,8)
incq %rbp
addq $0x10, %r15
cmpq %rbp, %r12
jne 0x61c59
movq %r12, %rbp
movq %rbx, 0x10(%rsp)
movq 0x20(%rsp), %rbx
movslq 0xcc(%rbx), %rsi
testq %rsi, %rsi
movq 0x8(%rsp), %r10
movl (%rsp), %r11d
jle 0x61d19
movq 0xb8(%rbx), %rdi
movl 0xc8(%rbx), %r8d
xorl %ecx, %ecx
movsd 0x412f9(%rip), %xmm0    # 0xa2fb8
movsd (%rdi,%rcx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x61ccc
jnp 0x61d11
movl (%r10,%rcx,4), %r9d
testl %r9d, %r9d
sets %al
cmpl %r11d, %r9d
setge %dl
orb %al, %dl
jne 0x61d11
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 0x61cbf
testl %r11d, %r11d
movq %rbp, %r11
jle 0x61d4e
xorl %r15d, %r15d
movq %r14, %r12
movl (%r13,%r15,4), %eax
testl %eax, %eax
je 0x61d42
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
movq %r12, %rdi
callq 0x69014
movq %rbp, %r11
incq %r15
addq $0x10, %r12
cmpq %r15, %r11
jne 0x61d27
cmpl $0x0, (%rsp)
movq 0x18(%rsp), %r15
movq 0x10(%rsp), %r12
jle 0x61de8
movl 0xc0(%rbx), %eax
movl 0xc4(%rbx), %ecx
movq 0xb8(%rbx), %rdx
movl 0xc8(%rbx), %esi
leaq (,%r11,4), %rdi
xorl %r8d, %r8d
movsd 0x402a2(%rip), %xmm0    # 0xa2030
cmpl $0x0, (%r13,%r8)
je 0x61ddf
movsd (%r14,%r8,4), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
testl %r9d, %r9d
js 0x61ddf
cmpl %r9d, %eax
jle 0x61ddf
movsd 0x8(%r14,%r8,4), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r10d
testl %r10d, %r10d
js 0x61ddf
cmpl %r10d, %ecx
jle 0x61ddf
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 0x61d8e
movslq 0xcc(%rbx), %rax
testq %rax, %rax
jle 0x61e22
movq 0xb8(%rbx), %rcx
xorl %edx, %edx
movsd 0x411b3(%rip), %xmm0    # 0xa2fb8
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x61e12
jnp 0x61e1a
movq $0x0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x61e05
cmpl $0x0, (%rsp)
jle 0x61e9c
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
shlq $0x2, %r11
xorl %edx, %edx
movsd 0x401ed(%rip), %xmm0    # 0xa2030
cmpl $0x0, (%r13,%rdx)
je 0x61e93
movsd (%r14,%rdx,4), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %esi
testl %esi, %esi
js 0x61e93
cmpl %esi, 0xc0(%rbx)
jle 0x61e93
movsd 0x8(%r14,%rdx,4), %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
testl %edi, %edi
js 0x61e93
cmpl %edi, 0xc4(%rbx)
jle 0x61e93
movsd (%r12,%rdx,2), %xmm1
imull %ecx, %edi
addl %esi, %edi
movslq %edi, %rsi
movsd %xmm1, (%rax,%rsi,8)
addq $0x4, %rdx
cmpq %rdx, %r11
jne 0x61e43
movq %r13, %rdi
callq 0xe5b0
movq %r14, %rdi
callq 0xe5b0
movq %r12, %rdi
callq 0xe5b0
movq 0x8(%rsp), %rdi
callq 0xe5b0
movq %r15, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0
movslq 0xcc(%rbx), %rax
testq %rax, %rax
jle 0x61f0e
movq 0xb8(%rbx), %rcx
xorl %edx, %edx
movsd 0x410c7(%rip), %xmm0    # 0xa2fb8
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x61efe
jnp 0x61f06
movq $0x0, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x61ef1
addq $0x28, %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
nop