// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::PercentileFilter(double, double)
void R3Grid::
PercentileFilter(RNLength grid_radius, RNScalar percentile)
{
  // Make copy of grid
  R3Grid 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) * (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 cz = 0; cz < ZResolution(); cz++) {
    for (int cy = 0; cy < YResolution(); cy++) {
      for (int cx = 0; cx < XResolution(); cx++) {
        // Build list of grid values in neighborhood
        int nsamples = 0;
        int zmin = cz - r;
        int zmax = cz + r;
        if (zmin < 0) zmin = 0;
        if (zmax >= ZResolution()) zmax = ZResolution() - 1;
        for (int z = zmin; z <= zmax; z++) {
          int dz = z - cz;
          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 dy = y - cy;
            int xmin = cx - r;
            int xmax = cx + r;
            if (xmin < 0) xmin = 0;
            if (xmax >= XResolution()) xmax = XResolution() - 1;
            for (int x = xmin; x <= xmax; x++) {
              int dx = x - cx;
              int d_squared = dx*dx + dy*dy + dz*dz;
              if (d_squared > grid_radius_squared) continue;
              RNScalar sample = copy.GridValue(x, y, z);
              samples[nsamples++] = sample;
            }
          }
        }

        // Check number of grid values in neighborhood
        if (nsamples == 0) {
          SetGridValue(cx, cy, cz, 0);
        }
        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, cz, samples[index]);
        }
      }
    }
  }

  // Delete temporary memory
  delete [] samples;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2e8, %rsp            # imm = 0x2E8
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x20(%rsp)
movq %rdi, %rbx
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0x2ee42
cvttsd2si 0x20(%rsp), %eax
movq %rax, 0x28(%rsp)
leal 0x1(,%rax,2), %eax
movl %eax, %ecx
imull %eax, %ecx
imull %eax, %ecx
movslq %ecx, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r14
movl 0x240(%rbx), %ecx
testl %ecx, %ecx
movsd 0x20(%rsp), %xmm1
jle 0x31d42
mulsd %xmm1, %xmm1
movq 0x28(%rsp), %rax
movl %eax, %edx
negl %edx
cltq
movq %rax, 0x68(%rsp)
movl 0x23c(%rbx), %eax
movl %edx, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movsd %xmm1, 0x20(%rsp)
movq %rbx, 0x70(%rsp)
testl %edx, %edx
movl $0x0, %esi
cmovgl %edx, %esi
movl %esi, 0x40(%rsp)
testl %eax, %eax
jle 0x31d2c
movq 0x8(%rsp), %rsi
movl %esi, %ebp
movq 0x28(%rsp), %rcx
subl %ecx, %ebp
leal (%rsi,%rcx), %r13d
xorl %esi, %esi
testl %ebp, %ebp
movl $0x0, %ecx
movq %rcx, 0x10(%rsp)
cmovlel %esi, %ebp
movl 0x238(%rbx), %r9d
movl 0x18(%rsp), %esi
movl %edx, 0x30(%rsp)
movl %ebp, 0x3c(%rsp)
movl %r13d, 0x38(%rsp)
testl %esi, %esi
movl $0x0, %ecx
cmovgl %esi, %ecx
movl %ecx, 0x48(%rsp)
testl %r9d, %r9d
jle 0x31d10
movl %esi, 0x34(%rsp)
movq 0x10(%rsp), %rdx
movl %edx, %esi
movq 0x28(%rsp), %rax
subl %eax, %esi
testl %esi, %esi
movl $0x0, %ecx
cmovlel %ecx, %esi
movl %esi, 0x50(%rsp)
addl %edx, %eax
movl %eax, 0x44(%rsp)
xorl %r12d, %r12d
xorl %r8d, %r8d
movl 0x18(%rsp), %esi
testl %esi, %esi
movl $0x0, %eax
cmovgl %esi, %eax
movq %rax, 0x90(%rsp)
movl 0x240(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl %eax, %r13d
cmovll %r13d, %ecx
movl %ecx, 0x54(%rsp)
cmpl %ecx, %ebp
jg 0x31cc1
movl %esi, 0x1c(%rsp)
movl 0x23c(%rbx), %eax
leal -0x1(%rax), %esi
movl 0x44(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %esi
movl %r8d, %edi
subl 0x28(%rsp), %edi
xorl %r15d, %r15d
testl %edi, %edi
cmovlel %r15d, %edi
movq 0x68(%rsp), %rax
movq %r8, 0x58(%rsp)
addq %rax, %r8
movl %r9d, 0x4c(%rsp)
movslq %r9d, %rax
leal -0x1(%rax), %ecx
cmpq %rax, %r8
cmovgel %ecx, %r8d
movq 0x2c8(%rsp), %r9
movl 0x2e0(%rsp), %edx
movl 0x2dc(%rsp), %r11d
movslq %r8d, %rax
movl 0x40(%rsp), %ecx
movq %rdx, 0x78(%rsp)
imull %edx, %ecx
movl 0x48(%rsp), %edx
movl %edx, %ebx
imull %r11d, %ebx
addl %ecx, %ebx
movl %ebp, %edx
movl 0x50(%rsp), %ecx
cmpl %esi, %ecx
movq %rbx, 0x88(%rsp)
movq %rdx, 0x80(%rsp)
jg 0x31bf0
subl 0x8(%rsp), %edx
imull %edx, %edx
movl %ecx, %ebp
cmpl %r8d, %edi
jg 0x31be4
movl %ebp, %r13d
subl 0x10(%rsp), %r13d
imull %r13d, %r13d
addl %edx, %r13d
movq 0x90(%rsp), %rcx
leal (%r12,%rcx), %r10d
imull %r10d, %r10d
addl %r13d, %r10d
xorps %xmm0, %xmm0
cvtsi2sd %r10d, %xmm0
ucomisd %xmm1, %xmm0
ja 0x31bd8
leal (%rbx,%rcx), %r10d
movslq %r10d, %r10
movsd (%r9,%r10,8), %xmm0
movslq %r15d, %r10
incl %r15d
movsd %xmm0, (%r14,%r10,8)
leaq 0x1(%rcx), %r10
cmpq %rax, %rcx
movq %r10, %rcx
jl 0x31ba6
leal 0x1(%rbp), %ecx
addq %r11, %rbx
cmpl %esi, %ebp
movl %ecx, %ebp
jl 0x31b8a
movq 0x80(%rsp), %rdx
leal 0x1(%rdx), %ecx
movq 0x88(%rsp), %rbx
addq 0x78(%rsp), %rbx
cmpl 0x54(%rsp), %edx
movl %ecx, %edx
jl 0x31b69
testl %r15d, %r15d
movq 0x70(%rsp), %rbx
movl 0x3c(%rsp), %ebp
movl 0x38(%rsp), %r13d
movl 0x4c(%rsp), %r9d
movq 0x58(%rsp), %r8
movl 0x1c(%rsp), %esi
je 0x31cc1
movslq %r15d, %rsi
movl $0x8, %edx
movq %r14, %rdi
leaq 0x3f9d6(%rip), %rcx     # 0x71621
callq 0xe880
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0x60(%rsp), %xmm0
cvttsd2si %xmm0, %eax
leal -0x1(%r15), %ecx
cmpl %eax, %r15d
cmovgl %eax, %ecx
testl %eax, %eax
movl $0x0, %eax
cmovsl %eax, %ecx
movslq %ecx, %rax
movsd (%r14,%rax,8), %xmm0
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull 0x8(%rsp), %ecx
movl 0x244(%rbx), %edx
imull 0x10(%rsp), %edx
movq 0x58(%rsp), %r8
addl %r8d, %ecx
addl %ecx, %edx
movslq %edx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x238(%rbx), %r9d
movsd 0x20(%rsp), %xmm1
movl 0x1c(%rsp), %esi
jmp 0x31cee
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull 0x8(%rsp), %ecx
movl 0x244(%rbx), %edx
imull 0x10(%rsp), %edx
addl %r8d, %ecx
addl %ecx, %edx
movslq %edx, %rcx
movq $0x0, (%rax,%rcx,8)
incq %r8
movslq %r9d, %rax
incl %esi
decq %r12
cmpq %rax, %r8
jl 0x31ac2
movl 0x23c(%rbx), %eax
movl 0x30(%rsp), %edx
movl 0x34(%rsp), %esi
movq 0x10(%rsp), %rcx
incl %ecx
incl %esi
movq %rcx, 0x10(%rsp)
cmpl %eax, %ecx
jl 0x31a7b
movl 0x240(%rbx), %ecx
movq 0x8(%rsp), %rsi
incl %esi
incl %edx
movq %rsi, 0x8(%rsp)
cmpl %ecx, %esi
jl 0x31a2a
movq %r14, %rdi
callq 0xe5b0
leaq 0x98(%rsp), %rdi
callq 0x2ee9e
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x31d6b
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x2ee9e
movq %rbx, %rdi
callq 0xe7e0
nop