// File: 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:
subq $0x268, %rsp            # imm = 0x268
movq %rdi, 0x260(%rsp)
movsd %xmm0, 0x258(%rsp)
movq 0x260(%rsp), %rdi
movq %rdi, 0x48(%rsp)
callq 0xb9420
cmpq $0x0, %rax
jne 0xac455
jmp 0xac7e4
movq 0x48(%rsp), %rdi
callq 0xb94a0
cmpl $0x0, %eax
jne 0xac469
jmp 0xac7e4
movq 0x48(%rsp), %rsi
leaq 0x248(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0xa76f0
movq 0x40(%rsp), %rdi
callq 0x6bf70
movq 0x48(%rsp), %rsi
addsd %xmm0, %xmm0
movsd %xmm0, 0x240(%rsp)
leaq 0x170(%rsp), %rdi
callq 0xa73a0
movq 0x48(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0xa73a0
jmp 0xac4bd
movq 0x48(%rsp), %rdi
movsd 0x879e6(%rip), %xmm0    # 0x133eb0
callq 0xa9d20
movl $0x0, 0x90(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xb94a0
movl %eax, 0x3c(%rsp)
jmp 0xac4ed
movl 0x3c(%rsp), %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xf030
movq %rax, 0x30(%rsp)
jmp 0xac515
movq 0x30(%rsp), %rax
movq %rax, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
movl %eax, 0x28(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xb94a0
movl %eax, 0x2c(%rsp)
jmp 0xac54b
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0xac613
movq 0x158(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x87938(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xac5c1
jp 0xac5c1
jmp 0xac5fd
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
jmp 0xac7ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xa7690
jmp 0xac7ec
movq 0x158(%rsp), %rdx
movslq 0x84(%rsp), %rax
shlq $0x3, %rax
addq %rax, %rdx
movq 0x88(%rsp), %rax
movslq 0x90(%rsp), %rcx
movq %rdx, (%rax,%rcx,8)
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0xac52d
cmpl $0x0, 0x90(%rsp)
jne 0xac64a
movq 0x88(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0xac63a
movq 0x20(%rsp), %rdi
callq 0xf670
movl $0x1, 0x80(%rsp)
jmp 0xac7ca
movq 0x88(%rsp), %rdi
movslq 0x90(%rsp), %rsi
leaq 0x1af(%rip), %rcx       # 0xac810
movl $0x8, %edx
callq 0xf950
jmp 0xac66d
movl $0x0, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x90(%rsp), %eax
jge 0xac7a4
movq 0x88(%rsp), %rax
movslq 0x7c(%rsp), %rcx
movq (%rax,%rcx,8), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rax
movq 0x158(%rsp), %rcx
subq %rcx, %rax
shrq $0x3, %rax
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %esi
leaq 0xa0(%rsp), %rdi
leaq 0x78(%rsp), %rdx
leaq 0x74(%rsp), %rcx
callq 0xb9440
jmp 0xac6d1
movq 0x68(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x248(%rsp), %rdi
callq 0x13970
movsd %xmm0, 0x18(%rsp)
jmp 0xac6f5
movsd 0x18(%rsp), %xmm0
movsd 0x10(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jb 0xac792
movl 0x78(%rsp), %esi
movl 0x74(%rsp), %edx
leaq 0x170(%rsp), %rdi
xorl %ecx, %ecx
callq 0xac8a0
movl %eax, 0xc(%rsp)
jmp 0xac728
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
je 0xac750
movq 0x48(%rsp), %rdi
movl 0x78(%rsp), %esi
movl 0x74(%rsp), %edx
movq 0x68(%rsp), %rax
movsd (%rax), %xmm0
callq 0x6bc00
jmp 0xac74e
jmp 0xac750
cvtsi2sdl 0x78(%rsp), %xmm0
cvtsi2sdl 0x74(%rsp), %xmm1
leaq 0x50(%rsp), %rdi
callq 0xc3140
jmp 0xac768
movsd 0x258(%rsp), %xmm0
movsd 0x240(%rsp), %xmm1
leaq 0xa0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0xaca00
jmp 0xac790
jmp 0xac792
jmp 0xac794
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0xac675
movq 0x88(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xac7bf
movq (%rsp), %rdi
callq 0xf670
movl $0x0, 0x80(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xa7690
leaq 0x170(%rsp), %rdi
callq 0xa7690
addq $0x268, %rsp            # imm = 0x268
retq
leaq 0x170(%rsp), %rdi
callq 0xa7690
movq 0x98(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)