// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::BilateralFilter(double, double)
void R3Grid::
BilateralFilter(RNLength grid_sigma, RNLength value_sigma)
{
  // Make copy of grid
  R3Grid copy(*this);

  // Determine reasonable value sigma
  if (value_sigma == -1) {
    RNInterval range = Range();
    value_sigma = 0.01 * (range.Max() - range.Min());
  }

  // Get convenient variables
  double grid_denom = -2.0 * grid_sigma * grid_sigma;
  double value_denom = -2.0 * value_sigma * value_sigma;
  RNScalar grid_radius = 3 * grid_sigma;
  int r = (int) (grid_radius + 1);
  int r_squared = r * r;

  // Set every sample to be median 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++) {
        // Get current value
        RNScalar value = copy.GridValue(cx, cy, cz);

        // Compute new value
        RNScalar sum = 0;
        RNScalar weight = 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 ymin = cy - r;
          int ymax = cy + r;
          if (ymin < 0) ymin = 0;
          if (ymax >= YResolution()) ymax = YResolution() - 1;
          int dz = z - cz;
          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 grid_distance_squared = dx*dx + dy*dy + dz*dz;
              if (grid_distance_squared > r_squared) continue;
              RNScalar sample = copy.GridValue(x, y, z);
              RNScalar value_distance_squared = value - sample;
              value_distance_squared *= value_distance_squared;
              RNScalar w = exp(grid_distance_squared/grid_denom) * exp(value_distance_squared/value_denom);
              sum += w * sample;
              weight += w;
            }
          }
        }

        // Set new value
        if (weight > 0) SetGridValue(cx, cy, cz, sum / weight);
      }
    }
  }
}

// Assembly:
subq $0x398, %rsp            # imm = 0x398
movq %rdi, 0x390(%rsp)
movsd %xmm0, 0x388(%rsp)
movsd %xmm1, 0x380(%rsp)
movq 0x390(%rsp), %rsi
movq %rsi, 0x70(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x568c0
movsd 0x380(%rsp), %xmm0
movsd 0xd88fc(%rip), %xmm1    # 0x133018
ucomisd %xmm1, %xmm0
jne 0x5a7b7
jp 0x5a7b7
movq 0x70(%rsp), %rsi
leaq 0x120(%rsp), %rdi
callq 0x56bc0
jmp 0x5a740
leaq 0x120(%rsp), %rdi
callq 0x13970
movsd %xmm0, 0x68(%rsp)
jmp 0x5a755
leaq 0x120(%rsp), %rdi
callq 0x13960
movsd %xmm0, 0x60(%rsp)
jmp 0x5a76a
movsd 0x60(%rsp), %xmm0
movsd 0x68(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0xd970e(%rip), %xmm0    # 0x133e90
mulsd %xmm1, %xmm0
movsd %xmm0, 0x380(%rsp)
jmp 0x5a7b7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x118(%rsp)
movl %eax, 0x114(%rsp)
leaq 0x130(%rsp), %rdi
callq 0x56960
jmp 0x5ad9f
movsd 0xd8a49(%rip), %xmm0    # 0x133208
mulsd 0x388(%rsp), %xmm0
mulsd 0x388(%rsp), %xmm0
movsd %xmm0, 0x108(%rsp)
movsd 0xd8a26(%rip), %xmm0    # 0x133208
mulsd 0x380(%rsp), %xmm0
mulsd 0x380(%rsp), %xmm0
movsd %xmm0, 0x100(%rsp)
movsd 0xd89db(%rip), %xmm0    # 0x1331e0
mulsd 0x388(%rsp), %xmm0
movsd %xmm0, 0xf8(%rsp)
movsd 0xd87e9(%rip), %xmm0    # 0x133008
addsd 0xf8(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
imull 0xf4(%rsp), %eax
movl %eax, 0xf0(%rsp)
movl $0x0, 0xec(%rsp)
movq 0x70(%rsp), %rdi
movl 0xec(%rsp), %eax
movl %eax, 0x58(%rsp)
callq 0x13900
movl %eax, 0x5c(%rsp)
jmp 0x5a86f
movl 0x58(%rsp), %eax
movl 0x5c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5ad8a
movl $0x0, 0xe8(%rsp)
movq 0x70(%rsp), %rdi
movl 0xe8(%rsp), %eax
movl %eax, 0x50(%rsp)
callq 0x138e0
movl %eax, 0x54(%rsp)
jmp 0x5a8a5
movl 0x50(%rsp), %eax
movl 0x54(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5ad72
movl $0x0, 0xe4(%rsp)
movq 0x70(%rsp), %rdi
movl 0xe4(%rsp), %eax
movl %eax, 0x48(%rsp)
callq 0x138c0
movl %eax, 0x4c(%rsp)
jmp 0x5a8db
movl 0x48(%rsp), %eax
movl 0x4c(%rsp), %ecx
cmpl %ecx, %eax
jge 0x5ad5a
movl 0xe4(%rsp), %esi
movl 0xe8(%rsp), %edx
movl 0xec(%rsp), %ecx
leaq 0x130(%rsp), %rdi
callq 0x6bad0
movsd %xmm0, 0x40(%rsp)
jmp 0x5a915
movsd 0x40(%rsp), %xmm0
movsd %xmm0, 0xd8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xd0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xc8(%rsp)
movl 0xec(%rsp), %eax
subl 0xf4(%rsp), %eax
movl %eax, 0xc4(%rsp)
movl 0xec(%rsp), %eax
addl 0xf4(%rsp), %eax
movl %eax, 0xc0(%rsp)
cmpl $0x0, 0xc4(%rsp)
jge 0x5a97b
movl $0x0, 0xc4(%rsp)
movq 0x70(%rsp), %rdi
movl 0xc0(%rsp), %eax
movl %eax, 0x38(%rsp)
callq 0x13900
movl %eax, 0x3c(%rsp)
jmp 0x5a996
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jl 0x5a9c0
movq 0x70(%rsp), %rdi
callq 0x13900
movl %eax, 0x34(%rsp)
jmp 0x5a9b2
movl 0x34(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xc0(%rsp)
movl 0xc4(%rsp), %eax
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
cmpl 0xc0(%rsp), %eax
jg 0x5acf7
movl 0xe8(%rsp), %eax
subl 0xf4(%rsp), %eax
movl %eax, 0xb8(%rsp)
movl 0xe8(%rsp), %eax
addl 0xf4(%rsp), %eax
movl %eax, 0xb4(%rsp)
cmpl $0x0, 0xb8(%rsp)
jge 0x5aa21
movl $0x0, 0xb8(%rsp)
movq 0x70(%rsp), %rdi
movl 0xb4(%rsp), %eax
movl %eax, 0x2c(%rsp)
callq 0x138e0
movl %eax, 0x30(%rsp)
jmp 0x5aa3c
movl 0x2c(%rsp), %eax
movl 0x30(%rsp), %ecx
cmpl %ecx, %eax
jl 0x5aa66
movq 0x70(%rsp), %rdi
callq 0x138e0
movl %eax, 0x28(%rsp)
jmp 0x5aa58
movl 0x28(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xb4(%rsp)
movl 0xbc(%rsp), %eax
subl 0xec(%rsp), %eax
movl %eax, 0xb0(%rsp)
movl 0xb8(%rsp), %eax
movl %eax, 0xac(%rsp)
movl 0xac(%rsp), %eax
cmpl 0xb4(%rsp), %eax
jg 0x5acdf
movl 0xe4(%rsp), %eax
subl 0xf4(%rsp), %eax
movl %eax, 0xa8(%rsp)
movl 0xe4(%rsp), %eax
addl 0xf4(%rsp), %eax
movl %eax, 0xa4(%rsp)
cmpl $0x0, 0xa8(%rsp)
jge 0x5aadc
movl $0x0, 0xa8(%rsp)
movq 0x70(%rsp), %rdi
movl 0xa4(%rsp), %eax
movl %eax, 0x20(%rsp)
callq 0x138c0
movl %eax, 0x24(%rsp)
jmp 0x5aaf7
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jl 0x5ab21
movq 0x70(%rsp), %rdi
callq 0x138c0
movl %eax, 0x1c(%rsp)
jmp 0x5ab13
movl 0x1c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0xa4(%rsp)
movl 0xac(%rsp), %eax
subl 0xe8(%rsp), %eax
movl %eax, 0xa0(%rsp)
movl 0xa8(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jg 0x5acc7
movl 0x9c(%rsp), %eax
subl 0xe4(%rsp), %eax
movl %eax, 0x98(%rsp)
movl 0x98(%rsp), %eax
imull 0x98(%rsp), %eax
movl 0xa0(%rsp), %ecx
imull 0xa0(%rsp), %ecx
addl %ecx, %eax
movl 0xb0(%rsp), %ecx
imull 0xb0(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0xf0(%rsp), %eax
jle 0x5abba
jmp 0x5acb1
movl 0x9c(%rsp), %esi
movl 0xac(%rsp), %edx
movl 0xbc(%rsp), %ecx
leaq 0x130(%rsp), %rdi
callq 0x6bad0
movsd %xmm0, 0x10(%rsp)
jmp 0x5abe4
movsd 0x10(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0xd8(%rsp), %xmm0
subsd 0x88(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x80(%rsp), %xmm0
mulsd 0x80(%rsp), %xmm0
movsd %xmm0, 0x80(%rsp)
cvtsi2sdl 0x94(%rsp), %xmm0
divsd 0x108(%rsp), %xmm0
callq 0xf8c0
movsd %xmm0, 0x8(%rsp)
movsd 0x80(%rsp), %xmm0
divsd 0x100(%rsp), %xmm0
callq 0xf8c0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
movsd 0x88(%rsp), %xmm2
movsd 0xd0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x78(%rsp), %xmm0
addsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x5ab44
jmp 0x5acc9
movl 0xac(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xac(%rsp)
jmp 0x5aa89
jmp 0x5ace1
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x5a9ce
movsd 0xc8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5ad42
movq 0x70(%rsp), %rdi
movl 0xe4(%rsp), %esi
movl 0xe8(%rsp), %edx
movl 0xec(%rsp), %ecx
movsd 0xd0(%rsp), %xmm0
movsd 0xc8(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0x6bc60
jmp 0x5ad40
jmp 0x5ad42
jmp 0x5ad44
movl 0xe4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe4(%rsp)
jmp 0x5a8c0
jmp 0x5ad5c
movl 0xe8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xe8(%rsp)
jmp 0x5a88a
jmp 0x5ad74
movl 0xec(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xec(%rsp)
jmp 0x5a854
leaq 0x130(%rsp), %rdi
callq 0x56960
addq $0x398, %rsp            # imm = 0x398
retq
movq 0x118(%rsp), %rdi
callq 0xf890
nopl (%rax)