// File: /workspace/github_cpp_makefile_star2_O2/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 $0x1e8, %rsp            # imm = 0x1E8
movsd %xmm0, 0x18(%rsp)
cmpq $0x0, 0xb8(%rdi)
je 0x54aed
movq %rdi, %rbx
cmpl $0x0, 0xcc(%rdi)
je 0x54aed
leaq 0x28(%rsp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x52194
movsd 0x8(%r14), %xmm0
subsd (%r14), %xmm0
movsd %xmm0, 0x8(%rsp)
leaq 0x118(%rsp), %rdi
movq %rbx, %rsi
callq 0x51fb4
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
callq 0x51fb4
movsd 0x3f5f1(%rip), %xmm0    # 0x93f68
movq %rbx, 0x10(%rsp)
movq %rbx, %rdi
callq 0x53578
movslq 0x114(%rsp), %r15
leaq (,%r15,8), %rax
testq %r15, %r15
pushq $-0x1
popq %rdi
cmovnsq %rax, %rdi
callq 0xe030
movq %rax, %r14
movsd 0x8(%rsp), %xmm0
addsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0x100(%rsp), %rax
xorl %r13d, %r13d
testl %r15d, %r15d
cmovlel %r13d, %r15d
movsd 0x3f598(%rip), %xmm1    # 0x93f68
movslq %r13d, %rsi
subq $0x1, %r15
jb 0x549f4
movsd (%rax), %xmm0
ucomisd %xmm1, %xmm0
jne 0x549e5
jnp 0x549ee
movq %rax, (%r14,%rsi,8)
incl %esi
movl %esi, %r13d
addq $0x8, %rax
jmp 0x549d0
testl %r13d, %r13d
je 0x54ace
leaq 0x124(%rip), %rcx       # 0x54b28
pushq $0x8
popq %rdx
movq %r14, %rdi
callq 0xe8b0
xorl %r12d, %r12d
testl %r13d, %r13d
cmovlel %r12d, %r13d
leaq 0x38(%rsp), %rbx
cmpq %r12, %r13
je 0x54ace
movq (%r14,%r12,8), %rcx
movq %rcx, %rax
subq 0x100(%rsp), %rax
shrq $0x3, %rax
cltd
idivl 0x110(%rsp)
movsd (%rcx), %xmm1
movsd 0x30(%rsp), %xmm0
ucomisd %xmm1, %xmm0
jb 0x54ac6
movl %eax, %ebp
movl %edx, %r15d
movsd %xmm1, 0x20(%rsp)
leaq 0x118(%rsp), %rdi
movl %edx, %esi
movl %eax, %edx
xorl %ecx, %ecx
callq 0x54b4a
testl %eax, %eax
je 0x54a9a
movq 0x10(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %ebp, %ecx
addl %r15d, %ecx
movslq %ecx, %rcx
movsd 0x20(%rsp), %xmm0
movsd %xmm0, (%rax,%rcx,8)
cvtsi2sd %r15d, %xmm0
cvtsi2sd %ebp, %xmm1
movq %rbx, %rdi
callq 0x5f58c
leaq 0x48(%rsp), %rdi
movq %rbx, %rsi
movsd 0x18(%rsp), %xmm0
movsd 0x8(%rsp), %xmm1
xorl %edx, %edx
callq 0x54c32
incq %r12
jmp 0x54a1e
movq %r14, %rdi
callq 0xe5d0
leaq 0x48(%rsp), %rdi
callq 0x5216c
leaq 0x118(%rsp), %rdi
callq 0x5216c
addq $0x1e8, %rsp            # imm = 0x1E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
jmp 0x54b13
jmp 0x54b06
movq %rax, %rbx
leaq 0x48(%rsp), %rdi
callq 0x5216c
leaq 0x118(%rsp), %rdi
callq 0x5216c
movq %rbx, %rdi
callq 0xe810