// File: /workspace/github_cpp_makefile_star2_O2/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 $0x2d8, %rsp            # imm = 0x2D8
movsd %xmm1, 0x58(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rdi, %rbx
leaq 0x88(%rsp), %rdi
movq %rbx, %rsi
callq 0x2bbaa
cvttsd2si 0x10(%rsp), %r15d
leal 0x1(,%r15,2), %eax
movl %eax, %ecx
imull %eax, %ecx
imull %eax, %ecx
movslq %ecx, %rax
leaq (,%rax,8), %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r14
movsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm1
movl %r15d, %eax
negl %eax
movslq %r15d, %rcx
movq %rcx, 0x68(%rsp)
movl %eax, 0xc(%rsp)
xorl %edi, %edi
movsd %xmm1, 0x10(%rsp)
movq %rbx, 0x60(%rsp)
movq %r15, 0x50(%rsp)
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movl %ecx, 0x2c(%rsp)
cmpl 0x240(%rbx), %edi
jge 0x2e3b7
movl %eax, 0x1c(%rsp)
movl %edi, %eax
subl %r15d, %eax
leal (%rdi,%r15), %ecx
movl %ecx, 0x24(%rsp)
xorl %r12d, %r12d
testl %eax, %eax
cmovlel %r12d, %eax
movl %eax, 0x28(%rsp)
movl 0xc(%rsp), %eax
movq %rdi, 0x48(%rsp)
testl %eax, %eax
movl $0x0, %ecx
cmovgl %eax, %ecx
movl %ecx, 0x34(%rsp)
cmpl 0x23c(%rbx), %r12d
jge 0x2e3aa
movl %eax, 0x20(%rsp)
movl %r12d, %ecx
subl %r15d, %ecx
testl %ecx, %ecx
movl $0x0, %eax
cmovlel %eax, %ecx
movl %ecx, 0x3c(%rsp)
leal (%r12,%r15), %eax
movl %eax, 0x30(%rsp)
xorl %ebp, %ebp
xorl %r8d, %r8d
movl 0xc(%rsp), %ecx
testl %ecx, %ecx
movl $0x0, %r13d
cmovgl %ecx, %r13d
movslq 0x238(%rbx), %rax
cmpq %rax, %r8
jge 0x2e397
movl %ecx, 0x38(%rsp)
movl 0x23c(%rbx), %ecx
movl 0x240(%rbx), %edx
leal -0x1(%rdx), %r9d
movl 0x24(%rsp), %esi
cmpl %edx, %esi
cmovll %esi, %r9d
movl %r9d, 0x40(%rsp)
leal -0x1(%rcx), %edx
movl 0x30(%rsp), %esi
cmpl %ecx, %esi
cmovll %esi, %edx
movq 0x68(%rsp), %rcx
movq %r8, 0x70(%rsp)
addq %r8, %rcx
leal -0x1(%rax), %esi
cmpq %rax, %rcx
cmovgel %esi, %ecx
movq 0x2b8(%rsp), %rsi
movl 0x2d0(%rsp), %r10d
movl 0x2cc(%rsp), %r8d
movslq %ecx, %r9
movl 0x2c(%rsp), %eax
movq %r10, 0x78(%rsp)
imull %r10d, %eax
movl 0x34(%rsp), %ecx
movl %ecx, %r10d
imull %r8d, %r10d
addl %eax, %r10d
xorl %ebx, %ebx
movl 0x28(%rsp), %r11d
cmpl 0x40(%rsp), %r11d
jg 0x2e302
movl %r11d, 0x44(%rsp)
subl %edi, %r11d
imull %r11d, %r11d
movq %r10, 0x80(%rsp)
movl 0x3c(%rsp), %edi
cmpl %edx, %edi
jg 0x2e2e3
movl %edi, %ecx
subl %r12d, %ecx
imull %ecx, %ecx
addl %r11d, %ecx
movq %r13, %rax
cmpq %r9, %rax
jg 0x2e2dc
leal (%rax,%rbp), %r15d
imull %r15d, %r15d
addl %ecx, %r15d
cvtsi2sd %r15d, %xmm0
ucomisd %xmm1, %xmm0
ja 0x2e2d7
leal (%r10,%rax), %r15d
movslq %r15d, %r15
movsd (%rsi,%r15,8), %xmm0
movslq %ebx, %r15
incl %ebx
movsd %xmm0, (%r14,%r15,8)
incq %rax
jmp 0x2e2a4
incl %edi
addq %r8, %r10
jmp 0x2e292
movl 0x44(%rsp), %r11d
incl %r11d
movq 0x80(%rsp), %r10
addq 0x78(%rsp), %r10
movq 0x48(%rsp), %rdi
jmp 0x2e26f
testl %ebx, %ebx
je 0x2e351
movslq %ebx, %rsi
movq %r14, %rdi
pushq $0x8
popq %rdx
leaq 0x39b1f(%rip), %rcx     # 0x67e35
callq 0xe8b0
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
movsd 0x10(%rsp), %xmm1
movq 0x48(%rsp), %rdi
jmp 0x2e355
xorpd %xmm0, %xmm0
movq 0x60(%rsp), %rbx
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull %edi, %ecx
movl 0x244(%rbx), %edx
imull %r12d, %edx
movq 0x70(%rsp), %r8
addl %r8d, %ecx
addl %ecx, %edx
movslq %edx, %rcx
movsd %xmm0, (%rax,%rcx,8)
incq %r8
movl 0x38(%rsp), %ecx
incl %ecx
decq %rbp
jmp 0x2e1d1
incl %r12d
movl 0x20(%rsp), %eax
incl %eax
movq 0x50(%rsp), %r15
jmp 0x2e18d
incl %edi
movl 0x1c(%rsp), %eax
incl %eax
jmp 0x2e14c
movq %r14, %rdi
callq 0xe5d0
leaq 0x88(%rsp), %rdi
callq 0x2bc02
addq $0x2d8, %rsp            # imm = 0x2D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2e3e0
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x2bc02
movq %rbx, %rdi
callq 0xe810