// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::PercentileFilter(double, double)
void R2Grid::
PercentileFilter(RNLength grid_radius, RNScalar percentile)
{
  // Make copy of grid
  R2Grid copy(*this);

  // Get convenient variables
  RNScalar grid_radius_squared = grid_radius * grid_radius;
  int r = (int) grid_radius;
  assert(r >= 0);
  int max_samples = (2*r+1) * (2*r+1);
  RNScalar *samples = new RNScalar [ max_samples ];
  assert(samples);

  // Set every sample to be Kth percentile of surrounding region in input grid
  for (int cy = 0; cy < YResolution(); cy++) {
    for (int cx = 0; cx < XResolution(); cx++) {
      // Check if current value is unknown - if so, don't update
      if (GridValue(cx, cy) != R2_GRID_UNKNOWN_VALUE) {
        // Build list of grid values in neighborhood
        int nsamples = 0;
        int ymin = cy - r;
        int ymax = cy + r;
        if (ymin < 0) ymin = 0;
        if (ymax >= YResolution()) ymax = YResolution() - 1;
        for (int y = ymin; y <= ymax; y++) {
          int xmin = cx - r;
          int xmax = cx + r;
          if (xmin < 0) xmin = 0;
          if (xmax >= XResolution()) xmax = XResolution() - 1;
          int dy = y - cy;
          for (int x = xmin; x <= xmax; x++) {
            int dx = x - cx;
            int d_squared = dx*dx + dy*dy;
            if (d_squared > grid_radius_squared) continue;
            RNScalar sample = copy.GridValue(x, y);
            if (sample == R2_GRID_UNKNOWN_VALUE) continue;
            samples[nsamples++] = sample;
          }
        }

        // Check number of grid values in neighborhood
        if (nsamples == 0) {
          SetGridValue(cx, cy, R2_GRID_UNKNOWN_VALUE);
        }
        else {
          // Sort samples found in neighborhood
          qsort(samples, nsamples, sizeof(RNScalar), RNCompareScalars);

          // Set grid value to percentile of neighborhood
          int index = (int) (percentile * nsamples);
          if (index < 0) index = 0;
          else if (index >= nsamples) index = nsamples-1;
          SetGridValue(cx, cy, samples[index]);
        }
      }
    }
  }

  // Delete temporary memory
  delete [] samples;
}

// Assembly:
subq $0x1a8, %rsp            # imm = 0x1A8
movq %rdi, 0x1a0(%rsp)
movsd %xmm0, 0x198(%rsp)
movsd %xmm1, 0x190(%rsp)
movq 0x1a0(%rsp), %rsi
movq %rsi, 0x48(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xa73a0
movsd 0x198(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0xb8(%rsp)
cvttsd2si 0x198(%rsp), %eax
movl %eax, 0xb4(%rsp)
movl 0xb4(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %eax
imull %eax, %eax
movl %eax, 0xb0(%rsp)
movslq 0xb0(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xf030
movq %rax, 0x50(%rsp)
jmp 0xabfd2
movq 0x50(%rsp), %rax
movq %rax, 0xa8(%rsp)
movl $0x0, 0x98(%rsp)
movq 0x48(%rsp), %rdi
movl 0x98(%rsp), %eax
movl %eax, 0x40(%rsp)
callq 0xb9280
movl %eax, 0x44(%rsp)
jmp 0xac005
movl 0x40(%rsp), %eax
movl 0x44(%rsp), %ecx
cmpl %ecx, %eax
jge 0xac3d4
movl $0x0, 0x94(%rsp)
movq 0x48(%rsp), %rdi
movl 0x94(%rsp), %eax
movl %eax, 0x38(%rsp)
callq 0xb9260
movl %eax, 0x3c(%rsp)
jmp 0xac03b
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jge 0xac3bc
movq 0x48(%rsp), %rdi
movl 0x94(%rsp), %esi
movl 0x98(%rsp), %edx
callq 0xb92a0
movsd %xmm0, 0x30(%rsp)
jmp 0xac06b
movsd 0x30(%rsp), %xmm0
movsd 0x87e37(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xac086
jp 0xac086
jmp 0xac3a4
movl $0x0, 0x90(%rsp)
movl 0x98(%rsp), %eax
subl 0xb4(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x98(%rsp), %eax
addl 0xb4(%rsp), %eax
movl %eax, 0x88(%rsp)
cmpl $0x0, 0x8c(%rsp)
jge 0xac0f8
movl $0x0, 0x8c(%rsp)
jmp 0xac0f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0xa7690
jmp 0xac406
movq 0x48(%rsp), %rdi
movl 0x88(%rsp), %eax
movl %eax, 0x28(%rsp)
callq 0xb9280
movl %eax, 0x2c(%rsp)
jmp 0xac113
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jl 0xac13d
movq 0x48(%rsp), %rdi
callq 0xb9280
movl %eax, 0x24(%rsp)
jmp 0xac12f
movl 0x24(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x88(%rsp)
movl 0x8c(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0x88(%rsp), %eax
jg 0xac2d4
movl 0x94(%rsp), %eax
subl 0xb4(%rsp), %eax
movl %eax, 0x80(%rsp)
movl 0x94(%rsp), %eax
addl 0xb4(%rsp), %eax
movl %eax, 0x7c(%rsp)
cmpl $0x0, 0x80(%rsp)
jge 0xac19b
movl $0x0, 0x80(%rsp)
movq 0x48(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0xb9260
movl %eax, 0x20(%rsp)
jmp 0xac1b3
movl 0x1c(%rsp), %eax
movl 0x20(%rsp), %ecx
cmpl %ecx, %eax
jl 0xac1da
movq 0x48(%rsp), %rdi
callq 0xb9260
movl %eax, 0x18(%rsp)
jmp 0xac1cf
movl 0x18(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x7c(%rsp)
movl 0x84(%rsp), %eax
subl 0x98(%rsp), %eax
movl %eax, 0x78(%rsp)
movl 0x80(%rsp), %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
cmpl 0x7c(%rsp), %eax
jg 0xac2bc
movl 0x74(%rsp), %eax
subl 0x94(%rsp), %eax
movl %eax, 0x70(%rsp)
movl 0x70(%rsp), %eax
imull 0x70(%rsp), %eax
movl 0x78(%rsp), %ecx
imull 0x78(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x6c(%rsp)
cvtsi2sdl 0x6c(%rsp), %xmm0
ucomisd 0xb8(%rsp), %xmm0
jbe 0xac23f
jmp 0xac2ac
movl 0x74(%rsp), %esi
movl 0x84(%rsp), %edx
leaq 0xc0(%rsp), %rdi
callq 0xb92a0
movsd %xmm0, 0x10(%rsp)
jmp 0xac25f
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movsd 0x87c37(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xac283
jp 0xac283
jmp 0xac2ac
movsd 0x60(%rsp), %xmm0
movq 0xa8(%rsp), %rax
movl 0x90(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0x90(%rsp)
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
jmp 0xac1f7
jmp 0xac2be
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0xac14b
cmpl $0x0, 0x90(%rsp)
jne 0xac305
movq 0x48(%rsp), %rdi
movl 0x94(%rsp), %esi
movl 0x98(%rsp), %edx
movsd 0x87bb7(%rip), %xmm0    # 0x133eb0
callq 0x6bc00
jmp 0xac300
jmp 0xac3a2
movq 0xa8(%rsp), %rdi
movslq 0x90(%rsp), %rsi
leaq 0x28414(%rip), %rcx     # 0xd4730
movl $0x8, %edx
callq 0xf950
jmp 0xac328
movsd 0x190(%rsp), %xmm0
cvtsi2sdl 0x90(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x5c(%rsp)
cmpl $0x0, 0x5c(%rsp)
jge 0xac357
movl $0x0, 0x5c(%rsp)
jmp 0xac374
movl 0x5c(%rsp), %eax
cmpl 0x90(%rsp), %eax
jl 0xac372
movl 0x90(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0xac374
movq 0x48(%rsp), %rdi
movl 0x94(%rsp), %esi
movl 0x98(%rsp), %edx
movq 0xa8(%rsp), %rax
movslq 0x5c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
callq 0x6bc00
jmp 0xac3a0
jmp 0xac3a2
jmp 0xac3a4
jmp 0xac3a6
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0xac020
jmp 0xac3be
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0xabfea
movq 0xa8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xac3f1
movq 0x8(%rsp), %rdi
callq 0xf670
leaq 0xc0(%rsp), %rdi
callq 0xa7690
addq $0x1a8, %rsp            # imm = 0x1A8
retq
movq 0xa0(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)