// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::MaskNonMaxima(double)
void R2Grid::
MaskNonMaxima(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 = nptrs-1; i >= 0; i--){
    int ix, iy;
    RNScalar *ptr = ptrs[i];
    int grid_index = ptr - mask.grid_values;
    assert(grid_index < mask.NEntries());
    mask.IndexToIndices(grid_index, ix, iy);

    // Check if it has been masked
    if (*ptr >= range.Min()) {
      // Set this grid value if it is a local maximum
      if (IsLocalExtremum(copy, ix, iy, TRUE)) {
        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 0xacd65
jmp 0xad0ff
movq 0x48(%rsp), %rdi
callq 0xb94a0
cmpl $0x0, %eax
jne 0xacd79
jmp 0xad0ff
movq 0x48(%rsp), %rsi
leaq 0x248(%rsp), %rdi
movq %rdi, 0x40(%rsp)
callq 0xa76f0
movq 0x40(%rsp), %rdi
callq 0x6bf70
movq 0x48(%rsp), %rsi
movsd 0x86461(%rip), %xmm1    # 0x133208
mulsd %xmm1, %xmm0
movsd %xmm0, 0x240(%rsp)
leaq 0x170(%rsp), %rdi
callq 0xa73a0
movq 0x48(%rsp), %rsi
leaq 0xa0(%rsp), %rdi
callq 0xa73a0
jmp 0xacdd5
movq 0x48(%rsp), %rdi
movsd 0x870ce(%rip), %xmm0    # 0x133eb0
callq 0xa9d20
movl $0x0, 0x90(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xb94a0
movl %eax, 0x3c(%rsp)
jmp 0xace05
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 0xace2d
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 0xace63
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0xacf2b
movq 0x158(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x87020(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xaced9
jp 0xaced9
jmp 0xacf15
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
jmp 0xad107
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x98(%rsp)
movl %eax, 0x94(%rsp)
leaq 0xa0(%rsp), %rdi
callq 0xa7690
jmp 0xad107
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 0xace45
cmpl $0x0, 0x90(%rsp)
jne 0xacf62
movq 0x88(%rsp), %rax
movq %rax, 0x20(%rsp)
cmpq $0x0, %rax
je 0xacf52
movq 0x20(%rsp), %rdi
callq 0xf670
movl $0x1, 0x80(%rsp)
jmp 0xad0e5
movq 0x88(%rsp), %rdi
movslq 0x90(%rsp), %rsi
leaq -0x769(%rip), %rcx      # 0xac810
movl $0x8, %edx
callq 0xf950
jmp 0xacf85
movl 0x90(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x7c(%rsp)
cmpl $0x0, 0x7c(%rsp)
jl 0xad0bf
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 0xacfe9
movq 0x68(%rsp), %rax
movsd (%rax), %xmm0
movsd %xmm0, 0x10(%rsp)
leaq 0x248(%rsp), %rdi
callq 0x13960
movsd %xmm0, 0x18(%rsp)
jmp 0xad00d
movsd 0x10(%rsp), %xmm0
movsd 0x18(%rsp), %xmm1
ucomisd %xmm1, %xmm0
jb 0xad0ad
movl 0x78(%rsp), %esi
movl 0x74(%rsp), %edx
leaq 0x170(%rsp), %rdi
movl $0x1, %ecx
callq 0xac8a0
movl %eax, 0xc(%rsp)
jmp 0xad043
movl 0xc(%rsp), %eax
cmpl $0x0, %eax
je 0xad06b
movq 0x48(%rsp), %rdi
movl 0x78(%rsp), %esi
movl 0x74(%rsp), %edx
movq 0x68(%rsp), %rax
movsd (%rax), %xmm0
callq 0x6bc00
jmp 0xad069
jmp 0xad06b
cvtsi2sdl 0x78(%rsp), %xmm0
cvtsi2sdl 0x74(%rsp), %xmm1
leaq 0x50(%rsp), %rdi
callq 0xc3140
jmp 0xad083
movsd 0x258(%rsp), %xmm0
movsd 0x240(%rsp), %xmm1
leaq 0xa0(%rsp), %rdi
leaq 0x50(%rsp), %rsi
xorl %edx, %edx
callq 0xaca00
jmp 0xad0ab
jmp 0xad0ad
jmp 0xad0af
movl 0x7c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0xacf93
movq 0x88(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xad0da
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)