// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movl %esi, 0x14(%rsp)
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %rdi, %rsi
leaq 0xc8(%rsp), %rdi
movq %rsi, 0x8(%rsp)
callq 0x51fb4
movsd 0x60(%rsp), %xmm3
ucomisd 0x3ed24(%rip), %xmm3    # 0x93020
jne 0x5432c
jp 0x5432c
leaq 0xb8(%rsp), %rdi
movq 0x8(%rsp), %rsi
callq 0x52194
movsd 0xc0(%rsp), %xmm3
subsd 0xb8(%rsp), %xmm3
mulsd 0x3fc34(%rip), %xmm3    # 0x93f60
movsd 0x3eecc(%rip), %xmm0    # 0x93200
movsd 0x50(%rsp), %xmm2
movapd %xmm2, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, 0x98(%rsp)
mulsd %xmm3, %xmm0
mulsd 0x3ee7d(%rip), %xmm2    # 0x931d8
addsd 0x3eca5(%rip), %xmm2    # 0x93008
mulsd %xmm3, %xmm0
cvttsd2si %xmm2, %ecx
movl %ecx, %ebp
imull %ecx, %ebp
movq %rcx, %rax
movq %rcx, 0x30(%rsp)
negl %eax
movl %eax, 0x10(%rsp)
xorl %edx, %edx
movsd 0x3fbe0(%rip), %xmm2    # 0x93f68
movsd %xmm0, 0x78(%rsp)
movapd %xmm0, 0x40(%rsp)
movq 0x8(%rsp), %rax
cmpl 0xc4(%rax), %edx
jge 0x5461e
movl %edx, %ecx
movq 0x30(%rsp), %rax
subl %eax, %ecx
addl %edx, %eax
movl %eax, 0x18(%rsp)
xorl %r15d, %r15d
testl %ecx, %ecx
cmovlel %r15d, %ecx
movl %ecx, 0x1c(%rsp)
movl 0x10(%rsp), %ecx
xorl %esi, %esi
movq %rdx, 0x80(%rsp)
testl %ecx, %ecx
movl $0x0, %eax
cmovgl %ecx, %eax
movq %rax, 0x90(%rsp)
movq 0x8(%rsp), %rax
cmpl 0xc0(%rax), %esi
jge 0x54617
movl %ecx, 0x20(%rsp)
movq 0x180(%rsp), %rax
movl 0x190(%rsp), %ecx
imull %edx, %ecx
addl %esi, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x38(%rsp)
ucomisd %xmm2, %xmm0
jne 0x54424
jnp 0x54607
xorpd %xmm3, %xmm3
cmpl $0x0, 0x14(%rsp)
je 0x54460
movsd 0x78(%rsp), %xmm0
movsd 0x38(%rsp), %xmm4
mulsd %xmm4, %xmm0
mulsd %xmm4, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm4, %xmm1
andpd %xmm1, %xmm0
andnpd 0x40(%rsp), %xmm1
orpd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movq 0x8(%rsp), %rax
movl 0xc4(%rax), %eax
leal -0x1(%rax), %edi
movl 0x18(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %edi
movl %edi, 0x28(%rsp)
movq 0x30(%rsp), %rax
addl %esi, %eax
movl %eax, 0x24(%rsp)
xorpd %xmm4, %xmm4
movl 0x1c(%rsp), %edi
movq %rsi, 0x88(%rsp)
cmpl 0x28(%rsp), %edi
jg 0x545c3
movq 0x8(%rsp), %rax
movl 0xc0(%rax), %eax
leal -0x1(%rax), %ebx
movl 0x24(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %ebx
movl %edi, %r13d
subl %edx, %r13d
imull %r13d, %r13d
movl %edi, 0x2c(%rsp)
movslq %edi, %r14
movq 0x90(%rsp), %r12
cmpl %ebx, %r12d
jg 0x545a8
leal (%r15,%r12), %eax
imull %eax, %eax
addl %r13d, %eax
cmpl %ebp, %eax
ja 0x545a0
movq 0x180(%rsp), %rcx
movslq 0x190(%rsp), %rdx
imulq %r14, %rdx
addq %r12, %rdx
movsd (%rcx,%rdx,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x54513
jnp 0x545a0
movsd 0x38(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd %xmm1, 0xa8(%rsp)
movsd %xmm0, 0xb0(%rsp)
cvtsi2sd %eax, %xmm0
divsd 0x98(%rsp), %xmm0
movapd %xmm3, 0x60(%rsp)
movapd %xmm4, 0x50(%rsp)
callq 0xe820
movsd %xmm0, 0xa0(%rsp)
movsd 0xa8(%rsp), %xmm0
divsd 0x40(%rsp), %xmm0
callq 0xe820
movapd 0x50(%rsp), %xmm4
movapd 0x60(%rsp), %xmm3
movsd 0x3f9e6(%rip), %xmm2    # 0x93f68
mulsd 0xa0(%rsp), %xmm0
movsd 0xb0(%rsp), %xmm1
mulsd %xmm0, %xmm1
addsd %xmm1, %xmm3
addsd %xmm0, %xmm4
incq %r12
jmp 0x544d0
movl 0x2c(%rsp), %edi
incl %edi
movq 0x80(%rsp), %rdx
movq 0x88(%rsp), %rsi
jmp 0x54496
divsd %xmm4, %xmm3
cmpeqsd 0x3ec18(%rip), %xmm4    # 0x931e8
movapd %xmm4, %xmm0
andnpd %xmm3, %xmm0
movsd 0x3f988(%rip), %xmm1    # 0x93f68
andpd %xmm1, %xmm4
orpd %xmm0, %xmm4
movq 0x8(%rsp), %rcx
movq 0xb8(%rcx), %rax
movl 0xc8(%rcx), %ecx
imull %edx, %ecx
addl %esi, %ecx
movslq %ecx, %rcx
movlpd %xmm4, (%rax,%rcx,8)
incl %esi
movl 0x20(%rsp), %ecx
incl %ecx
decq %r15
jmp 0x543cf
incl %edx
jmp 0x54394
leaq 0xc8(%rsp), %rdi
callq 0x5216c
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0x5216c
movq %rbx, %rdi
callq 0xe810
nop