// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movsd %xmm1, 0x40(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rdi, %rbx
leaq 0x58(%rsp), %rdi
movq %rbx, %rsi
callq 0x51fb4
cvttsd2si 0x10(%rsp), %r15d
leal 0x1(,%r15,2), %edi
imull %edi, %edi
shlq $0x3, %rdi
callq 0xe030
movq %rax, %r14
movsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm1
movslq %r15d, %rax
movq %rax, 0x30(%rsp)
negl %r15d
movsd 0x3f89c(%rip), %xmm2    # 0x93f68
xorl %r13d, %r13d
movl %r15d, %edx
movq %r15, %rcx
movsd %xmm1, 0x10(%rsp)
movq %rbx, 0x20(%rsp)
movq %r15, 0x38(%rsp)
testl %edx, %edx
movl $0x0, %r9d
cmovgl %edx, %r9d
movslq 0xc4(%rbx), %rax
cmpq %rax, %r13
jge 0x548c0
movl %edx, 0x1c(%rsp)
movq 0x30(%rsp), %rax
addq %r13, %rax
movq %rax, 0x50(%rsp)
leaq (,%r9,8), %rax
movq %rax, 0x48(%rsp)
xorl %ebp, %ebp
xorl %r15d, %r15d
movl %ecx, %esi
movq %r9, 0x28(%rsp)
testl %esi, %esi
movl $0x0, %eax
cmovgl %esi, %eax
movslq 0xc0(%rbx), %rdx
cmpq %rdx, %r15
jge 0x548ad
movslq 0xc8(%rbx), %rcx
imulq %r13, %rcx
shlq $0x3, %rcx
addq 0xb8(%rbx), %rcx
movsd (%rcx,%r15,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5476d
jnp 0x548a0
movl %esi, 0xc(%rsp)
movslq 0xc4(%rbx), %rsi
leal -0x1(%rsi), %r8d
movq 0x50(%rsp), %rdi
cmpq %rsi, %rdi
cmovll %edi, %r8d
movq 0x30(%rsp), %rsi
addq %r15, %rsi
leal -0x1(%rdx), %edi
cmpq %rdx, %rsi
cmovgel %edi, %esi
movslq 0x120(%rsp), %rdx
movslq %esi, %rsi
movq 0x48(%rsp), %rdi
imulq %rdx, %rdi
addq 0x110(%rsp), %rdi
movslq %r8d, %r8
shlq $0x3, %rdx
xorl %ebx, %ebx
cmpq %r8, %r9
jg 0x54811
movl %r9d, %r10d
subl %r13d, %r10d
imull %r10d, %r10d
movq %rax, %r11
cmpq %rsi, %r11
jg 0x54809
leal (%r11,%rbp), %r12d
imull %r12d, %r12d
addl %r10d, %r12d
cvtsi2sd %r12d, %xmm0
ucomisd %xmm1, %xmm0
ja 0x54804
movsd (%rdi,%r11,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x547f9
jnp 0x54804
movslq %ebx, %r12
incl %ebx
movsd %xmm0, (%r14,%r12,8)
incq %r11
jmp 0x547d0
incq %r9
addq %rdx, %rdi
jmp 0x547be
testl %ebx, %ebx
je 0x54884
movslq %ebx, %rsi
movq %r14, %rdi
pushq $0x8
popq %rdx
leaq 0x13610(%rip), %rcx     # 0x67e35
callq 0xe8b0
cvtsi2sd %ebx, %xmm0
mulsd 0x40(%rsp), %xmm0
cvttsd2si %xmm0, %eax
leal -0x1(%rbx), %ecx
cmpl %eax, %ebx
cmovgl %eax, %ecx
testl %eax, %eax
movl $0x0, %eax
cmovsl %eax, %ecx
movslq %ecx, %rax
movsd (%r14,%rax,8), %xmm0
movq 0x20(%rsp), %rbx
movslq 0xc8(%rbx), %rax
imulq %r13, %rax
shlq $0x3, %rax
addq 0xb8(%rbx), %rax
movsd %xmm0, (%rax,%r15,8)
movsd 0x10(%rsp), %xmm1
movsd 0x3f6e6(%rip), %xmm2    # 0x93f68
jmp 0x54897
movabsq $-0x3f8bf00000000000, %rax # imm = 0xC074100000000000
movq %rax, (%rcx,%r15,8)
movq 0x20(%rsp), %rbx
movq 0x28(%rsp), %r9
movl 0xc(%rsp), %esi
incq %r15
incl %esi
decq %rbp
jmp 0x5472b
incq %r13
movl 0x1c(%rsp), %edx
incl %edx
movq 0x38(%rsp), %rcx
jmp 0x546e5
movq %r14, %rdi
callq 0xe5d0
leaq 0x58(%rsp), %rdi
callq 0x5216c
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x548e6
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x5216c
movq %rbx, %rdi
callq 0xe810
nop