// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::BilateralFilter(double, double, int)
void R2Grid::
BilateralFilter(RNLength grid_sigma, RNLength value_sigma, RNBoolean value_sigma_is_fraction)
{
  // Make copy of grid
  R2Grid 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 filter of surrounding region in input grid
  for (int cy = 0; cy < YResolution(); cy++) {
    for (int cx = 0; cx < XResolution(); cx++) {
      // Check if current value is unknown - if so, don't update
      RNScalar value = copy.GridValue(cx, cy);
      if (value != R2_GRID_UNKNOWN_VALUE) {
        if (value_sigma_is_fraction && (value > 0)) {
          value_denom = -2.0 * value_sigma * value_sigma * value * value;
        }
        RNScalar sum = 0;
        RNScalar weight = 0;
        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 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;
            if (grid_distance_squared > r_squared) continue;
            RNScalar sample = copy.GridValue(x, y);
            if (sample == R2_GRID_UNKNOWN_VALUE) continue;
            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 grid value
        if (weight == 0) SetGridValue(cx, cy, R2_GRID_UNKNOWN_VALUE);
        else SetGridValue(cx, cy, sum / weight);
      }
    }
  }
}

// Assembly:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x1e0(%rsp)
movsd %xmm0, 0x1d8(%rsp)
movsd %xmm1, 0x1d0(%rsp)
movl %esi, 0x1cc(%rsp)
movq 0x1e0(%rsp), %rsi
movq %rsi, 0x50(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0xa73a0
movsd 0x1d0(%rsp), %xmm0
movsd 0x876d5(%rip), %xmm1    # 0x133018
ucomisd %xmm1, %xmm0
jne 0xab9de
jp 0xab9de
movq 0x50(%rsp), %rsi
leaq 0xe8(%rsp), %rdi
callq 0xa76f0
jmp 0xab967
leaq 0xe8(%rsp), %rdi
callq 0x13970
movsd %xmm0, 0x48(%rsp)
jmp 0xab97c
leaq 0xe8(%rsp), %rdi
callq 0x13960
movsd %xmm0, 0x40(%rsp)
jmp 0xab991
movsd 0x40(%rsp), %xmm0
movsd 0x48(%rsp), %xmm1
subsd %xmm0, %xmm1
movsd 0x884e7(%rip), %xmm0    # 0x133e90
mulsd %xmm1, %xmm0
movsd %xmm0, 0x1d0(%rsp)
jmp 0xab9de
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xe0(%rsp)
movl %eax, 0xdc(%rsp)
leaq 0xf8(%rsp), %rdi
callq 0xa7690
jmp 0xabef3
movsd 0x87822(%rip), %xmm0    # 0x133208
mulsd 0x1d8(%rsp), %xmm0
mulsd 0x1d8(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movsd 0x877ff(%rip), %xmm0    # 0x133208
mulsd 0x1d0(%rsp), %xmm0
mulsd 0x1d0(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0x877b4(%rip), %xmm0    # 0x1331e0
mulsd 0x1d8(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0x875c2(%rip), %xmm0    # 0x133008
addsd 0xc0(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
imull 0xbc(%rsp), %eax
movl %eax, 0xb8(%rsp)
movl $0x0, 0xb4(%rsp)
movq 0x50(%rsp), %rdi
movl 0xb4(%rsp), %eax
movl %eax, 0x38(%rsp)
callq 0xb9280
movl %eax, 0x3c(%rsp)
jmp 0xaba96
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jge 0xabede
movl $0x0, 0xb0(%rsp)
movq 0x50(%rsp), %rdi
movl 0xb0(%rsp), %eax
movl %eax, 0x30(%rsp)
callq 0xb9260
movl %eax, 0x34(%rsp)
jmp 0xabacc
movl 0x30(%rsp), %eax
movl 0x34(%rsp), %ecx
cmpl %ecx, %eax
jge 0xabec6
movl 0xb0(%rsp), %esi
movl 0xb4(%rsp), %edx
leaq 0xf8(%rsp), %rdi
callq 0xb92a0
movsd %xmm0, 0x28(%rsp)
jmp 0xabaff
movsd 0x28(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
movsd 0x88391(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xabb2c
jp 0xabb2c
jmp 0xabeae
cmpl $0x0, 0x1cc(%rsp)
je 0xabb7d
movsd 0xa8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xabb7d
movsd 0x876b8(%rip), %xmm0    # 0x133208
mulsd 0x1d0(%rsp), %xmm0
mulsd 0x1d0(%rsp), %xmm0
mulsd 0xa8(%rsp), %xmm0
mulsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xa0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0xb4(%rsp), %eax
subl 0xbc(%rsp), %eax
movl %eax, 0x94(%rsp)
movl 0xb4(%rsp), %eax
addl 0xbc(%rsp), %eax
movl %eax, 0x90(%rsp)
cmpl $0x0, 0x94(%rsp)
jge 0xabbd4
movl $0x0, 0x94(%rsp)
movq 0x50(%rsp), %rdi
movl 0x90(%rsp), %eax
movl %eax, 0x20(%rsp)
callq 0xb9280
movl %eax, 0x24(%rsp)
jmp 0xabbef
movl 0x20(%rsp), %eax
movl 0x24(%rsp), %ecx
cmpl %ecx, %eax
jl 0xabc19
movq 0x50(%rsp), %rdi
callq 0xb9280
movl %eax, 0x1c(%rsp)
jmp 0xabc0b
movl 0x1c(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x90(%rsp)
movl 0x94(%rsp), %eax
movl %eax, 0x8c(%rsp)
movl 0x8c(%rsp), %eax
cmpl 0x90(%rsp), %eax
jg 0xabe42
movl 0xb0(%rsp), %eax
subl 0xbc(%rsp), %eax
movl %eax, 0x88(%rsp)
movl 0xb0(%rsp), %eax
addl 0xbc(%rsp), %eax
movl %eax, 0x84(%rsp)
cmpl $0x0, 0x88(%rsp)
jge 0xabc7a
movl $0x0, 0x88(%rsp)
movq 0x50(%rsp), %rdi
movl 0x84(%rsp), %eax
movl %eax, 0x14(%rsp)
callq 0xb9260
movl %eax, 0x18(%rsp)
jmp 0xabc95
movl 0x14(%rsp), %eax
movl 0x18(%rsp), %ecx
cmpl %ecx, %eax
jl 0xabcbf
movq 0x50(%rsp), %rdi
callq 0xb9260
movl %eax, 0x10(%rsp)
jmp 0xabcb1
movl 0x10(%rsp), %eax
subl $0x1, %eax
movl %eax, 0x84(%rsp)
movl 0x8c(%rsp), %eax
subl 0xb4(%rsp), %eax
movl %eax, 0x80(%rsp)
movl 0x88(%rsp), %eax
movl %eax, 0x7c(%rsp)
movl 0x7c(%rsp), %eax
cmpl 0x84(%rsp), %eax
jg 0xabe2a
movl 0x7c(%rsp), %eax
subl 0xb0(%rsp), %eax
movl %eax, 0x78(%rsp)
movl 0x78(%rsp), %eax
imull 0x78(%rsp), %eax
movl 0x80(%rsp), %ecx
imull 0x80(%rsp), %ecx
addl %ecx, %eax
movl %eax, 0x74(%rsp)
movl 0x74(%rsp), %eax
cmpl 0xb8(%rsp), %eax
jle 0xabd2f
jmp 0xabe1a
movl 0x7c(%rsp), %esi
movl 0x8c(%rsp), %edx
leaq 0xf8(%rsp), %rdi
callq 0xb92a0
movsd %xmm0, 0x8(%rsp)
jmp 0xabd4f
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x68(%rsp), %xmm0
movsd 0x88147(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xabd76
jp 0xabd76
jmp 0xabe1a
movsd 0xa8(%rsp), %xmm0
subsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
mulsd 0x60(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
cvtsi2sdl 0x74(%rsp), %xmm0
divsd 0xd0(%rsp), %xmm0
callq 0xf8c0
movsd %xmm0, (%rsp)
movsd 0x60(%rsp), %xmm0
divsd 0xc8(%rsp), %xmm0
callq 0xf8c0
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x58(%rsp), %xmm0
movsd 0x68(%rsp), %xmm2
movsd 0xa0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xa0(%rsp)
movsd 0x58(%rsp), %xmm0
addsd 0x98(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0xabcdf
jmp 0xabe2c
movl 0x8c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x8c(%rsp)
jmp 0xabc27
movsd 0x98(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0xabe7a
jp 0xabe7a
movq 0x50(%rsp), %rdi
movl 0xb0(%rsp), %esi
movl 0xb4(%rsp), %edx
movsd 0x8803f(%rip), %xmm0    # 0x133eb0
callq 0x6bc00
jmp 0xabe78
jmp 0xabeac
movq 0x50(%rsp), %rdi
movl 0xb0(%rsp), %esi
movl 0xb4(%rsp), %edx
movsd 0xa0(%rsp), %xmm0
movsd 0x98(%rsp), %xmm1
divsd %xmm1, %xmm0
callq 0x6bc00
jmp 0xabeaa
jmp 0xabeac
jmp 0xabeae
jmp 0xabeb0
movl 0xb0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb0(%rsp)
jmp 0xabab1
jmp 0xabec8
movl 0xb4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb4(%rsp)
jmp 0xaba7b
leaq 0xf8(%rsp), %rdi
callq 0xa7690
addq $0x1e8, %rsp            # imm = 0x1E8
retq
movq 0xe0(%rsp), %rdi
callq 0xf890