// File: 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:
subq $0x348, %rsp            # imm = 0x348
movq %rdi, 0x340(%rsp)
movsd %xmm0, 0x338(%rsp)
movsd %xmm1, 0x330(%rsp)
movq 0x340(%rsp), %rsi
movq %rsi, 0x58(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x568c0
movsd 0x338(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0xd8(%rsp)
cvttsd2si 0x338(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl 0xd4(%rsp), %ecx
movl %ecx, %eax
leal 0x1(%rax,%rax), %ecx
movl %ecx, %eax
imull %eax, %eax
imull %ecx, %eax
movl %eax, 0xd0(%rsp)
movslq 0xd0(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xf030
movq %rax, 0x60(%rsp)
jmp 0x5b7a7
movq 0x60(%rsp), %rax
movq %rax, 0xc8(%rsp)
movl $0x0, 0xb8(%rsp)
movq 0x58(%rsp), %rdi
movl 0xb8(%rsp), %eax
movl %eax, 0x50(%rsp)
callq 0x13900
movl %eax, 0x54(%rsp)
jmp 0x5b7da
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5bcbe
movl $0x0, 0xb4(%rsp)
movq 0x58(%rsp), %rdi
movl 0xb4(%rsp), %eax
movl %eax, 0x48(%rsp)
callq 0x138e0
movl %eax, 0x4c(%rsp)
jmp 0x5b810
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5bca6
movl $0x0, 0xb0(%rsp)
movq 0x58(%rsp), %rdi
movl 0xb0(%rsp), %eax
movl %eax, 0x40(%rsp)
callq 0x138c0
movl %eax, 0x44(%rsp)
jmp 0x5b846
movl 0x40(%rsp), %eax
movl 0x44(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5bc8e
movl $0x0, 0xac(%rsp)
movl 0xb8(%rsp), %eax
subl 0xd4(%rsp), %eax
movl %eax, 0xa8(%rsp)
movl 0xb8(%rsp), %eax
addl 0xd4(%rsp), %eax
movl %eax, 0xa4(%rsp)
cmpl $0x0, 0xa8(%rsp)
jge 0x5b8c8
movl $0x0, 0xa8(%rsp)
jmp 0x5b8c8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xc0(%rsp)
movl %eax, 0xbc(%rsp)
leaq 0xe0(%rsp), %rdi
callq 0x56960
jmp 0x5bcf0
movq 0x58(%rsp), %rdi
movl 0xa4(%rsp), %eax
movl %eax, 0x38(%rsp)
callq 0x13900
movl %eax, 0x3c(%rsp)
jmp 0x5b8e3
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jl 0x5b90d
movq 0x58(%rsp), %rdi
callq 0x13900
movl %eax, 0x34(%rsp)
jmp 0x5b8ff
movl 0x34(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xa4(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jg 0x5bb9e
movl 0xa0(%rsp), %eax
subl 0xb8(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0xb4(%rsp), %eax
subl 0xd4(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0xb4(%rsp), %eax
addl 0xd4(%rsp), %eax
movl %eax, 0x94(%rsp)
cmpl $0x0, 0x98(%rsp)
jge 0x5b983
movl $0x0, 0x98(%rsp)
movq 0x58(%rsp), %rdi
movl 0x94(%rsp), %eax
movl %eax, 0x2c(%rsp)
callq 0x138e0
movl %eax, 0x30(%rsp)
jmp 0x5b99e
movl 0x2c(%rsp), %eax
movl 0x30(%rsp), %ecx
cmpl %ecx, %eax
jl 0x5b9c8
movq 0x58(%rsp), %rdi
callq 0x138e0
movl %eax, 0x28(%rsp)
jmp 0x5b9ba
movl 0x28(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x94(%rsp)
movl 0x98(%rsp), %eax
movl %eax, 0x90(%rsp)
movl 0x90(%rsp), %eax
cmpl 0x94(%rsp), %eax
jg 0x5bb86
movl 0x90(%rsp), %eax
subl 0xb4(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0xb0(%rsp), %eax
subl 0xd4(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0xb0(%rsp), %eax
addl 0xd4(%rsp), %eax
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x88(%rsp)
jge 0x5ba3e
movl $0x0, 0x88(%rsp)
movq 0x58(%rsp), %rdi
movl 0x84(%rsp), %eax
movl %eax, 0x20(%rsp)
callq 0x138c0
movl %eax, 0x24(%rsp)
jmp 0x5ba59
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jl 0x5ba83
movq 0x58(%rsp), %rdi
callq 0x138c0
movl %eax, 0x1c(%rsp)
jmp 0x5ba75
movl 0x1c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x84(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0x80(%rsp)
movl 0x80(%rsp), %eax
cmpl 0x84(%rsp), %eax
jg 0x5bb6e
movl 0x80(%rsp), %eax
subl 0xb0(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
imull 0x7c(%rsp), %eax
movl 0x8c(%rsp), %ecx
imull 0x8c(%rsp), %ecx
addl %ecx, %eax
movl 0x9c(%rsp), %ecx
imull 0x9c(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x78(%rsp)
cvtsi2sdl 0x78(%rsp), %xmm0
ucomisd 0xd8(%rsp), %xmm0
jbe 0x5baf9
jmp 0x5bb58
movl 0x80(%rsp), %esi
movl 0x90(%rsp), %edx
movl 0xa0(%rsp), %ecx
leaq 0xe0(%rsp), %rdi
callq 0x6bad0
movsd %xmm0, 0x10(%rsp)
jmp 0x5bb23
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x70(%rsp), %xmm0
movq 0xc8(%rsp), %rax
movl 0xac(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0xac(%rsp)
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0x5ba91
jmp 0x5bb70
movl 0x90(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x90(%rsp)
jmp 0x5b9d6
jmp 0x5bb88
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0x5b91b
cmpl $0x0, 0xac(%rsp)
jne 0x5bbd1
movq 0x58(%rsp), %rdi
movl 0xb0(%rsp), %esi
movl 0xb4(%rsp), %edx
movl 0xb8(%rsp), %ecx
xorps %xmm0, %xmm0
callq 0x6bc60
jmp 0x5bbcc
jmp 0x5bc76
movq 0xc8(%rsp), %rdi
movslq 0xac(%rsp), %rsi
leaq 0x78b48(%rip), %rcx     # 0xd4730
movl $0x8, %edx
callq 0xf950
jmp 0x5bbf4
movsd 0x330(%rsp), %xmm0
cvtsi2sdl 0xac(%rsp), %xmm1
mulsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x6c(%rsp)
cmpl $0x0, 0x6c(%rsp)
jge 0x5bc23
movl $0x0, 0x6c(%rsp)
jmp 0x5bc40
movl 0x6c(%rsp), %eax
cmpl 0xac(%rsp), %eax
jl 0x5bc3e
movl 0xac(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x5bc40
movq 0x58(%rsp), %rdi
movl 0xb0(%rsp), %esi
movl 0xb4(%rsp), %edx
movl 0xb8(%rsp), %ecx
movq 0xc8(%rsp), %rax
movslq 0x6c(%rsp), %r8
movsd (%rax,%r8,8), %xmm0
callq 0x6bc60
jmp 0x5bc74
jmp 0x5bc76
jmp 0x5bc78
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0x5b82b
jmp 0x5bc90
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0x5b7f5
jmp 0x5bca8
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x5b7bf
movq 0xc8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x5bcdb
movq 0x8(%rsp), %rdi
callq 0xf670
leaq 0xe0(%rsp), %rdi
callq 0x56960
addq $0x348, %rsp            # imm = 0x348
retq
movq 0xc0(%rsp), %rdi
callq 0xf890
nopl (%rax)