// File: /workspace/github_cpp_makefile_star2_O1/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 $0x138, %rsp            # imm = 0x138
movsd %xmm1, 0x58(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rdi, %rbx
leaq 0x68(%rsp), %rdi
movq %rbx, %rsi
callq 0x5a63c
cvttsd2si 0x10(%rsp), %eax
movq %rax, 0x18(%rsp)
leal 0x1(,%rax,2), %edi
imull %edi, %edi
shlq $0x3, %rdi
callq 0xe030
movq %rax, %r14
cmpl $0x0, 0xc4(%rbx)
movsd 0x10(%rsp), %xmm1
jle 0x5d2e1
mulsd %xmm1, %xmm1
movq 0x18(%rsp), %rax
movl %eax, %ecx
negl %ecx
cltq
movq %rax, 0x38(%rsp)
movsd 0x45f2d(%rip), %xmm2    # 0xa2fb8
movq $0x0, (%rsp)
movl %ecx, 0x2c(%rsp)
movl %ecx, %esi
movsd %xmm1, 0x10(%rsp)
movq %rbx, 0x48(%rsp)
testl %esi, %esi
movl $0x0, %eax
movl %esi, 0x30(%rsp)
cmovgl %esi, %eax
movq %rax, 0x40(%rsp)
movl 0xc0(%rbx), %edi
testl %edi, %edi
jle 0x5d2c0
movq 0x38(%rsp), %rax
movq (%rsp), %rcx
addq %rcx, %rax
movq %rax, 0x60(%rsp)
subl 0x18(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
movl %ecx, 0x34(%rsp)
movq 0x40(%rsp), %rax
leaq (,%rax,8), %rax
movq %rax, 0x50(%rsp)
xorl %r12d, %r12d
xorl %ecx, %ecx
movl 0x2c(%rsp), %edx
testl %edx, %edx
movl $0x0, %r13d
cmovgl %edx, %r13d
movslq 0xc8(%rbx), %rax
imulq (%rsp), %rax
shlq $0x3, %rax
addq 0xb8(%rbx), %rax
movsd (%rax,%rcx,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5d137
jnp 0x5d2a8
movl %edx, 0xc(%rsp)
movq %rcx, 0x20(%rsp)
movslq 0xc4(%rbx), %rdx
leal -0x1(%rdx), %ecx
movq 0x60(%rsp), %rsi
cmpq %rdx, %rsi
cmovll %esi, %ecx
xorl %ebx, %ebx
cmpl %ecx, 0x34(%rsp)
jg 0x5d20c
movq 0x38(%rsp), %rdx
movq 0x20(%rsp), %rsi
addq %rsi, %rdx
subl 0x18(%rsp), %esi
xorl %ebx, %ebx
testl %esi, %esi
cmovlel %ebx, %esi
movslq %edi, %rdi
leal -0x1(%rdi), %r8d
cmpq %rdi, %rdx
cmovgel %r8d, %edx
movslq 0x130(%rsp), %rdi
movslq %edx, %r8
movslq %ecx, %r9
movq 0x50(%rsp), %r10
imulq %rdi, %r10
addq 0x120(%rsp), %r10
shlq $0x3, %rdi
movq 0x40(%rsp), %r11
cmpl %edx, %esi
jg 0x5d1fd
movl %r11d, %r15d
subl (%rsp), %r15d
imull %r15d, %r15d
movq %r13, %rcx
leal (%r12,%rcx), %ebp
imull %ebp, %ebp
addl %r15d, %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
ucomisd %xmm1, %xmm0
ja 0x5d1f1
movsd (%r10,%rcx,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5d1e6
jnp 0x5d1f1
movslq %ebx, %rbp
incl %ebx
movsd %xmm0, (%r14,%rbp,8)
leaq 0x1(%rcx), %rbp
cmpq %r8, %rcx
movq %rbp, %rcx
jl 0x5d1c1
leaq 0x1(%r11), %rcx
addq %rdi, %r10
cmpq %r9, %r11
movq %rcx, %r11
jl 0x5d1af
testl %ebx, %ebx
je 0x5d289
movslq %ebx, %rsi
movl $0x8, %edx
movq %r14, %rdi
leaq 0x15447(%rip), %rcx     # 0x72669
callq 0xe880
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
mulsd 0x58(%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 0x48(%rsp), %rbx
movslq 0xc8(%rbx), %rax
imulq (%rsp), %rax
shlq $0x3, %rax
addq 0xb8(%rbx), %rax
movq 0x20(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movsd 0x10(%rsp), %xmm1
movsd 0x45d31(%rip), %xmm2    # 0xa2fb8
jmp 0x5d2a4
movabsq $-0x3f8bf00000000000, %rcx # imm = 0xC074100000000000
movq 0x20(%rsp), %rdx
movq %rcx, (%rax,%rdx,8)
movq %rdx, %rcx
movq 0x48(%rsp), %rbx
movl 0xc(%rsp), %edx
incq %rcx
movslq 0xc0(%rbx), %rdi
incl %edx
decq %r12
cmpq %rdi, %rcx
jl 0x5d103
movq (%rsp), %rdx
incq %rdx
movslq 0xc4(%rbx), %rax
movl 0x30(%rsp), %esi
incl %esi
movq %rdx, (%rsp)
cmpq %rax, %rdx
jl 0x5d0a4
movq %r14, %rdi
callq 0xe5b0
leaq 0x68(%rsp), %rdi
callq 0x5a814
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5d307
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x5a814
movq %rbx, %rdi
callq 0xe7e0