// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::MaskNonMinima(double)
void R2Grid::
MaskNonMinima(RNLength grid_radius)
{
  // Check everything
  if (!GridValues()) return;
  if (NEntries() == 0) return;

  // Determine ammount to add to grid value to mark as masked
  RNInterval range = Range();
  RNScalar mask_summand = 2.0 * range.Diameter();

  // Save copy of grid
  R2Grid copy(*this);
  R2Grid mask(*this);

  // Set all values in this grid to zero
  Clear(R2_GRID_UNKNOWN_VALUE);

  // Load mask grid value pointers into array
  int nptrs = 0;
  RNScalar **ptrs = new RNScalar * [ mask.NEntries() ];
  for (int i = 0; i < mask.NEntries(); i++) {
    if (mask.grid_values[i] == R2_GRID_UNKNOWN_VALUE) continue;
    ptrs[nptrs] = &mask.grid_values[i];
    nptrs++;
  }

  // Check number of non-unknown values
  if (nptrs == 0) { delete [] ptrs; return; }

  // Sort mask value pointers
  qsort(ptrs, nptrs, sizeof(RNScalar *), RNCompareScalarPtrs);

  // Select values in sorted order, masking neighborhoods
  for (int i = 0; i < nptrs; i++){
    int ix, iy;
    RNScalar *ptr = ptrs[i];
    int grid_index = ptr - mask.grid_values;
    mask.IndexToIndices(grid_index, ix, iy);

    // Check if it has been masked
    if (*ptr <= range.Max()) {
      // Set this grid value if it is a local minimum
      if (IsLocalExtremum(copy, ix, iy, FALSE)) {
        SetGridValue(ix, iy, *ptr);
      }

      // Mask neighborhood
      mask.RasterizeGridCircle(R2Point(ix, iy), grid_radius, mask_summand);
    }
  }

  // Delete temporary data
  delete [] ptrs;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1d8, %rsp            # imm = 0x1D8
movsd %xmm0, 0x10(%rsp)
cmpq $0x0, 0xb8(%rdi)
je 0x5c7ef
movq %rdi, %rbx
cmpl $0x0, 0xcc(%rdi)
je 0x5c7ef
leaq 0x18(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x59b34
movsd 0x8(%r14), %xmm0
subsd (%r14), %xmm0
movsd %xmm0, (%rsp)
leaq 0x108(%rsp), %rdi
movq %rbx, %rsi
callq 0x59934
leaq 0x38(%rsp), %rdi
movq %rbx, %rsi
callq 0x59934
movslq 0xcc(%rbx), %rax
testq %rax, %rax
jle 0x5c682
movq 0xb8(%rbx), %rcx
xorl %edx, %edx
movsd 0x46959(%rip), %xmm0    # 0xa2fb8
movabsq $-0x3f8bf00000000000, %rsi # imm = 0xC074100000000000
movsd (%rcx,%rdx,8), %xmm1
ucomisd %xmm0, %xmm1
jne 0x5c676
jnp 0x5c67a
movq %rsi, (%rcx,%rdx,8)
incq %rdx
cmpq %rdx, %rax
jne 0x5c669
movslq 0x104(%rsp), %r12
leaq (,%r12,8), %rax
testq %r12, %r12
movq $-0x1, %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r14
testl %r12d, %r12d
jle 0x5c7d0
movsd (%rsp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, (%rsp)
xorl %r15d, %r15d
movq 0xf0(%rsp), %rax
movsd 0x468e6(%rip), %xmm0    # 0xa2fb8
movsd (%rax), %xmm1
ucomisd %xmm0, %xmm1
jne 0x5c6de
jnp 0x5c6e8
movslq %r15d, %r15
movq %rax, (%r14,%r15,8)
incl %r15d
addq $0x8, %rax
decq %r12
jne 0x5c6d2
testl %r15d, %r15d
je 0x5c7d0
movslq %r15d, %rsi
leaq 0x126(%rip), %rcx       # 0x5c82a
movl $0x8, %edx
movq %r14, %rdi
callq 0xe880
testl %r15d, %r15d
jle 0x5c7d0
movl %r15d, %r13d
xorl %r12d, %r12d
movq (%r14,%r12,8), %rcx
movq %rcx, %rax
subq 0xf0(%rsp), %rax
shrq $0x3, %rax
cltd
idivl 0x100(%rsp)
movsd (%rcx), %xmm1
movsd 0x20(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x5c7c4
movl %eax, %ebp
movl %edx, %r15d
movsd %xmm1, 0x8(%rsp)
leaq 0x108(%rsp), %rdi
movl %edx, %esi
movl %eax, %edx
xorl %ecx, %ecx
callq 0x5c84e
testl %eax, %eax
je 0x5c78e
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull %ebp, %ecx
addl %r15d, %ecx
movslq %ecx, %rcx
movsd 0x8(%rsp), %xmm0
movsd %xmm0, (%rax,%rcx,8)
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ebp, %xmm1
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x680a6
leaq 0x38(%rsp), %rdi
movq %r15, %rsi
movsd 0x10(%rsp), %xmm0
movsd (%rsp), %xmm1
xorl %edx, %edx
callq 0x5c92a
incq %r12
cmpq %r12, %r13
jne 0x5c720
movq %r14, %rdi
callq 0xe5b0
leaq 0x38(%rsp), %rdi
callq 0x59b0c
leaq 0x108(%rsp), %rdi
callq 0x59b0c
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x5c815
jmp 0x5c808
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x59b0c
leaq 0x108(%rsp), %rdi
callq 0x59b0c
movq %rbx, %rdi
callq 0xe7e0