// File: /workspace/github_cpp_makefile_star2_O1/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, 0x70(%rsp)
movsd %xmm0, 0x28(%rsp)
movq %rdi, %rbx
leaq 0x98(%rsp), %rdi
movq %rbx, %rsi
callq 0x2f120
cvttsd2si 0x28(%rsp), %eax
movq %rax, 0x20(%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
cmpl $0x0, 0x240(%rbx)
movsd 0x28(%rsp), %xmm1
jle 0x31ee5
mulsd %xmm1, %xmm1
movq 0x20(%rsp), %rax
movl %eax, %ecx
negl %ecx
cltq
movq %rax, 0x68(%rsp)
movl %ecx, 0x18(%rsp)
movq $0x0, 0x8(%rsp)
movsd %xmm1, 0x28(%rsp)
movq %rbx, 0x30(%rsp)
testl %ecx, %ecx
movl $0x0, %eax
movl %ecx, 0x44(%rsp)
cmovgl %ecx, %eax
movl %eax, 0x4c(%rsp)
cmpl $0x0, 0x23c(%rbx)
jle 0x31ec7
movq 0x8(%rsp), %rcx
movl %ecx, %edx
movq 0x20(%rsp), %rax
subl %eax, %edx
addl %ecx, %eax
movl %eax, 0x58(%rsp)
xorl %ecx, %ecx
testl %edx, %edx
movl $0x0, %eax
movq %rax, 0x10(%rsp)
cmovlel %ecx, %edx
movl %edx, 0x1c(%rsp)
movl 0x18(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %eax
movl %ecx, 0x48(%rsp)
cmovgl %ecx, %eax
movl %eax, 0x54(%rsp)
movl 0x238(%rbx), %ecx
testl %ecx, %ecx
jle 0x31ea9
movq 0x10(%rsp), %rsi
movl %esi, %ebp
movq 0x20(%rsp), %rax
subl %eax, %ebp
testl %ebp, %ebp
movl $0x0, %edx
cmovlel %edx, %ebp
addl %esi, %eax
movl %eax, 0x50(%rsp)
xorl %r12d, %r12d
movq $0x0, 0x38(%rsp)
movl 0x18(%rsp), %edx
movl %ebp, 0x60(%rsp)
xorl %r15d, %r15d
testl %edx, %edx
movl $0x0, %eax
movl %edx, 0x5c(%rsp)
cmovgl %edx, %eax
movq %rax, 0x90(%rsp)
movl 0x240(%rbx), %eax
leal -0x1(%rax), %esi
movl 0x58(%rsp), %edx
cmpl %eax, %edx
cmovll %edx, %esi
movl %esi, 0x64(%rsp)
cmpl %esi, 0x1c(%rsp)
jg 0x31df9
movq 0x30(%rsp), %rax
movl 0x23c(%rax), %eax
leal -0x1(%rax), %edx
movl 0x50(%rsp), %esi
cmpl %eax, %esi
cmovll %esi, %edx
movq 0x38(%rsp), %rdi
movl %edi, %esi
subl 0x20(%rsp), %esi
xorl %r15d, %r15d
testl %esi, %esi
cmovlel %r15d, %esi
movq 0x68(%rsp), %rax
addq %rax, %rdi
movslq %ecx, %rax
leal -0x1(%rax), %ecx
cmpq %rax, %rdi
cmovgel %ecx, %edi
movq 0x2c8(%rsp), %r8
movl 0x2e0(%rsp), %ecx
movl 0x2dc(%rsp), %r10d
movslq %edi, %r11
movl 0x4c(%rsp), %eax
movq %rcx, 0x78(%rsp)
imull %ecx, %eax
movl 0x54(%rsp), %ecx
movl %ecx, %ebx
imull %r10d, %ebx
addl %eax, %ebx
movl 0x1c(%rsp), %eax
cmpl %edx, %ebp
movq %rbx, 0x88(%rsp)
movq %rax, 0x80(%rsp)
jg 0x31dd3
movl %eax, %ecx
subl 0x8(%rsp), %ecx
imull %ecx, %ecx
cmpl %edi, %esi
jg 0x31dc7
movl %ebp, %r13d
subl 0x10(%rsp), %r13d
imull %r13d, %r13d
addl %ecx, %r13d
movq 0x90(%rsp), %rax
leal (%r12,%rax), %r9d
imull %r9d, %r9d
addl %r13d, %r9d
xorps %xmm0, %xmm0
cvtsi2sd %r9d, %xmm0
ucomisd %xmm1, %xmm0
ja 0x31dbb
leal (%rbx,%rax), %r9d
movslq %r9d, %r9
movsd (%r8,%r9,8), %xmm0
movslq %r15d, %r9
incl %r15d
movsd %xmm0, (%r14,%r9,8)
leaq 0x1(%rax), %r9
cmpq %r11, %rax
movq %r9, %rax
jl 0x31d89
leal 0x1(%rbp), %eax
addq %r10, %rbx
cmpl %edx, %ebp
movl %eax, %ebp
jl 0x31d6e
movq 0x80(%rsp), %rcx
leal 0x1(%rcx), %eax
movq 0x88(%rsp), %rbx
addq 0x78(%rsp), %rbx
cmpl 0x64(%rsp), %ecx
movl 0x60(%rsp), %ebp
jl 0x31d51
testl %r15d, %r15d
je 0x31e51
movslq %r15d, %rsi
movl $0x8, %edx
movq %r14, %rdi
leaq 0x40859(%rip), %rcx     # 0x72669
callq 0xe880
movq 0x30(%rsp), %rbx
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
mulsd 0x70(%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
movsd 0x28(%rsp), %xmm1
jmp 0x31e5a
xorpd %xmm0, %xmm0
movq 0x30(%rsp), %rbx
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull 0x8(%rsp), %ecx
movl 0x244(%rbx), %edx
imull 0x10(%rsp), %edx
movq 0x38(%rsp), %rsi
addl %esi, %ecx
addl %ecx, %edx
movslq %edx, %rcx
movsd %xmm0, (%rax,%rcx,8)
incq %rsi
movslq 0x238(%rbx), %rcx
movl 0x5c(%rsp), %edx
incl %edx
decq %r12
movq %rsi, 0x38(%rsp)
cmpq %rcx, %rsi
jl 0x31ca3
movq 0x10(%rsp), %rax
incl %eax
movl 0x48(%rsp), %ecx
incl %ecx
movq %rax, 0x10(%rsp)
cmpl 0x23c(%rbx), %eax
jl 0x31c51
movq 0x8(%rsp), %rax
incl %eax
movl 0x44(%rsp), %ecx
incl %ecx
movq %rax, 0x8(%rsp)
cmpl 0x240(%rbx), %eax
jl 0x31c05
movq %r14, %rdi
callq 0xe5b0
leaq 0x98(%rsp), %rdi
callq 0x2f17c
addq $0x2e8, %rsp            # imm = 0x2E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x31f0e
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x2f17c
movq %rbx, %rdi
callq 0xe7e0