// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::ConnectedComponentFilter(double, double, double, double, double, double)
void R2Grid::
ConnectedComponentFilter(RNScalar isolevel, RNArea min_grid_area, RNArea max_grid_area, 
  RNScalar under_isolevel_value, RNScalar too_small_value, RNScalar too_large_value)
{
  // Compute connected components
  int *sizes = new int [ grid_size ];
  int *components = new int [ grid_size ];
  int ncomponents = ConnectedComponents(isolevel, grid_size, NULL, sizes, components);

  // Mask values under isolevel
  if (under_isolevel_value != R2_GRID_KEEP_VALUE) {
    for (int i = 0; i < grid_size; i++) {
      if (grid_values[i] == R2_GRID_UNKNOWN_VALUE) continue;
      if (grid_values[i] < isolevel) grid_values[i] = under_isolevel_value;
    }
  }

  // Mask components with areas too small 
  if (too_small_value != R2_GRID_KEEP_VALUE) {
    for (int component = 0; component < ncomponents; component++) {
      int area = sizes[component];
      if (area < min_grid_area) {
        for (int i = 0; i < grid_size; i++) {
          if (components[i] == component) grid_values[i] = too_small_value;
        }
      }
    }
  }

  // Mask components with areas too large 
  if (too_large_value != R2_GRID_KEEP_VALUE) {
    for (int component = 0; component < ncomponents; component++) {
      int area = sizes[component];
      if (area > max_grid_area) {
        for (int i = 0; i < grid_size; i++) {
          if (components[i] == component) grid_values[i] = too_large_value;
        }
      }
    }
  }

  // Delete temporary memory
  delete [] components;
  delete [] sizes;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x38, %rsp
movsd %xmm5, 0x30(%rsp)
movsd %xmm4, 0x20(%rsp)
movsd %xmm3, 0x10(%rsp)
movsd %xmm2, 0x28(%rsp)
movsd %xmm1, 0x18(%rsp)
movsd %xmm0, 0x8(%rsp)
movq %rdi, %rbx
movslq 0xcc(%rdi), %r12
leaq (,%r12,4), %rax
testq %r12, %r12
movq $-0x1, %r15
cmovnsq %rax, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r14
movq %r15, %rdi
callq 0xe030
movq %rax, %r15
movq %rbx, %rdi
movsd 0x8(%rsp), %xmm0
movl %r12d, %esi
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
callq 0x6158e
movsd 0x10(%rsp), %xmm2
ucomisd 0x40fe8(%rip), %xmm2    # 0xa2fa8
jne 0x61fc4
jnp 0x62007
movslq 0xcc(%rbx), %rcx
testq %rcx, %rcx
jle 0x62007
movq 0xb8(%rbx), %rdx
xorl %esi, %esi
movsd 0x40fd7(%rip), %xmm0    # 0xa2fb8
movsd 0x8(%rsp), %xmm3
movsd (%rdx,%rsi,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x61ff4
jnp 0x61fff
ucomisd %xmm1, %xmm3
jbe 0x61fff
movsd %xmm2, (%rdx,%rsi,8)
incq %rsi
cmpq %rsi, %rcx
jne 0x61fe7
movsd 0x20(%rsp), %xmm3
ucomisd 0x40f93(%rip), %xmm3    # 0xa2fa8
movsd 0x30(%rsp), %xmm1
movsd 0x28(%rsp), %xmm2
movsd 0x18(%rsp), %xmm4
jne 0x6202b
jnp 0x6207e
testl %eax, %eax
jle 0x6207e
movl 0xcc(%rbx), %ecx
movl %eax, %edx
xorl %esi, %esi
testl %ecx, %ecx
setle %dil
xorps %xmm0, %xmm0
cvtsi2sdl (%r14,%rsi,4), %xmm0
ucomisd %xmm0, %xmm4
setbe %r8b
orb %dil, %r8b
jne 0x62076
movq 0xb8(%rbx), %rdi
xorl %r8d, %r8d
movl (%r15,%r8,4), %r9d
cmpq %r9, %rsi
jne 0x6206e
movsd %xmm3, (%rdi,%r8,8)
incq %r8
cmpq %r8, %rcx
jne 0x6205f
incq %rsi
cmpq %rdx, %rsi
jne 0x62039
ucomisd 0x40f22(%rip), %xmm1    # 0xa2fa8
jne 0x6208a
jnp 0x620db
testl %eax, %eax
jle 0x620db
movl 0xcc(%rbx), %ecx
movl %eax, %eax
xorl %edx, %edx
testl %ecx, %ecx
setle %sil
xorps %xmm0, %xmm0
cvtsi2sdl (%r14,%rdx,4), %xmm0
ucomisd %xmm2, %xmm0
setbe %dil
orb %sil, %dil
jne 0x620d3
movq 0xb8(%rbx), %rsi
xorl %edi, %edi
movl (%r15,%rdi,4), %r8d
cmpq %r8, %rdx
jne 0x620cb
movsd %xmm1, (%rsi,%rdi,8)
incq %rdi
cmpq %rdi, %rcx
jne 0x620bd
incq %rdx
cmpq %rax, %rdx
jne 0x62098
movq %r15, %rdi
callq 0xe5b0
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0xe5b0