// File: /workspace/github_cpp_makefile_star2_O2/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 %r13
pushq %r12
pushq %rbx
subq $0x30, %rsp
movsd %xmm5, 0x28(%rsp)
movsd %xmm4, 0x18(%rsp)
movsd %xmm3, 0x8(%rsp)
movsd %xmm2, 0x20(%rsp)
movsd %xmm1, 0x10(%rsp)
movsd %xmm0, (%rsp)
movq %rdi, %rbx
movslq 0xcc(%rdi), %r12
leaq (,%r12,4), %rax
testq %r12, %r12
pushq $-0x1
popq %r15
cmovnsq %rax, %r15
movq %r15, %rdi
callq 0xe030
movq %rax, %r14
movq %r15, %rdi
callq 0xe030
movq %rax, %r15
xorl %r13d, %r13d
movq %rbx, %rdi
movsd (%rsp), %xmm0
movl %r12d, %esi
xorl %edx, %edx
movq %r14, %rcx
movq %rax, %r8
callq 0x584b8
movsd (%rsp), %xmm3
movsd 0x8(%rsp), %xmm2
ucomisd 0x3b103(%rip), %xmm2    # 0x93f58
jne 0x58e59
jnp 0x58e98
movl 0xcc(%rbx), %ecx
movq 0xb8(%rbx), %rdx
testl %ecx, %ecx
cmovlel %r13d, %ecx
movsd 0x3b0f4(%rip), %xmm0    # 0x93f68
cmpq %r13, %rcx
je 0x58e98
movsd (%rdx,%r13,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x58e87
jnp 0x58e93
ucomisd %xmm1, %xmm3
jbe 0x58e93
movsd %xmm2, (%rdx,%r13,8)
incq %r13
jmp 0x58e74
movsd 0x18(%rsp), %xmm3
ucomisd 0x3b0b2(%rip), %xmm3    # 0x93f58
movsd 0x28(%rsp), %xmm1
movsd 0x20(%rsp), %xmm2
movsd 0x10(%rsp), %xmm4
jne 0x58ebc
jnp 0x58f0c
movl 0xcc(%rbx), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
testl %eax, %eax
movl $0x0, %esi
cmovgl %eax, %esi
cmpq %rsi, %rdx
je 0x58f0c
cvtsi2sdl (%r14,%rdx,4), %xmm0
ucomisd %xmm0, %xmm4
jbe 0x58f07
movq 0xb8(%rbx), %rdi
xorl %r8d, %r8d
cmpq %r8, %rcx
je 0x58f07
movl (%r15,%r8,4), %r9d
cmpq %r9, %rdx
jne 0x58f02
movsd %xmm3, (%rdi,%r8,8)
incq %r8
jmp 0x58eee
incq %rdx
jmp 0x58ed3
ucomisd 0x3b044(%rip), %xmm1    # 0x93f58
jne 0x58f18
jnp 0x58f61
movl 0xcc(%rbx), %ecx
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
testl %eax, %eax
cmovlel %edx, %eax
cmpq %rax, %rdx
je 0x58f61
cvtsi2sdl (%r14,%rdx,4), %xmm0
ucomisd %xmm2, %xmm0
jbe 0x58f5c
movq 0xb8(%rbx), %rsi
xorl %edi, %edi
cmpq %rdi, %rcx
je 0x58f5c
movl (%r15,%rdi,4), %r8d
cmpq %r8, %rdx
jne 0x58f57
movsd %xmm1, (%rsi,%rdi,8)
incq %rdi
jmp 0x58f44
incq %rdx
jmp 0x58f2a
movq %r15, %rdi
callq 0xe5d0
movq %r14, %rdi
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
jmp 0xe5d0