// File: /workspace/github_cpp_makefile_star2_O3/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 $0x188, %rsp            # imm = 0x188
movl %esi, %ebx
movsd %xmm1, 0x18(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rdi, %r14
leaq 0xb8(%rsp), %rdi
movq %r14, %rsi
callq 0x59934
movsd 0x18(%rsp), %xmm3
ucomisd 0x4615c(%rip), %xmm3    # 0xa2048
jne 0x5bf1a
jp 0x5bf1a
leaq 0xa8(%rsp), %rdi
movq %r14, %rsi
callq 0x59b34
movsd 0xb0(%rsp), %xmm3
subsd 0xa8(%rsp), %xmm3
mulsd 0x47096(%rip), %xmm3    # 0xa2fb0
movl 0xc4(%r14), %eax
testl %eax, %eax
jle 0x5c246
movsd 0x462cf(%rip), %xmm0    # 0xa2200
movsd 0x10(%rsp), %xmm2
movapd %xmm2, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, 0x88(%rsp)
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm0
mulsd 0x4627c(%rip), %xmm2    # 0xa21d8
addsd 0x460d4(%rip), %xmm2    # 0xa2038
cvttsd2si %xmm2, %edx
movl %edx, %ebp
imull %edx, %ebp
movl 0xc0(%r14), %ecx
movq %rdx, 0x40(%rsp)
negl %edx
movl %edx, 0x20(%rsp)
movsd 0x47031(%rip), %xmm2    # 0xa2fb8
movsd %xmm0, 0x68(%rsp)
movapd %xmm0, 0x50(%rsp)
movq $0x0, 0x8(%rsp)
movl %ebx, 0x24(%rsp)
movq %r14, 0x80(%rsp)
testl %ecx, %ecx
jle 0x5c232
movq 0x8(%rsp), %rcx
movl %ecx, %edx
movq 0x40(%rsp), %rax
subl %eax, %edx
leal (%rcx,%rax), %esi
xorl %r15d, %r15d
testl %edx, %edx
cmovlel %r15d, %edx
movl 0x20(%rsp), %edi
xorl %r8d, %r8d
movl %edx, 0x2c(%rsp)
movl %esi, 0x28(%rsp)
testl %edi, %edi
movl $0x0, %eax
cmovgl %edi, %eax
movq %rax, 0x78(%rsp)
movq 0x170(%rsp), %rax
movl 0x180(%rsp), %ecx
imull 0x8(%rsp), %ecx
addl %r8d, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x48(%rsp)
ucomisd %xmm2, %xmm0
jne 0x5c019
jnp 0x5c213
testl %ebx, %ebx
je 0x5c04e
movsd 0x68(%rsp), %xmm0
movsd 0x48(%rsp), %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm0
andnpd 0x50(%rsp), %xmm1
orpd %xmm0, %xmm1
movapd %xmm1, 0x50(%rsp)
movl 0xc4(%r14), %eax
leal -0x1(%rax), %ecx
cmpl %eax, %esi
cmovll %esi, %ecx
movapd %xmm2, %xmm0
movl %ecx, 0x3c(%rsp)
cmpl %ecx, %edx
jg 0x5c1f5
movl %edi, 0x30(%rsp)
movl %r8d, %esi
movq 0x40(%rsp), %rax
subl %eax, %esi
testl %esi, %esi
movl $0x0, %ecx
cmovlel %ecx, %esi
movl %esi, 0x38(%rsp)
movq %r8, 0x70(%rsp)
addl %r8d, %eax
movl %eax, 0x34(%rsp)
xorpd %xmm1, %xmm1
movl %edx, %r12d
xorpd %xmm3, %xmm3
movl 0xc0(%r14), %eax
leal -0x1(%rax), %r13d
movl 0x34(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %r13d
cmpl %r13d, 0x38(%rsp)
jg 0x5c1ad
movl %r12d, %r14d
subl 0x8(%rsp), %r14d
imull %r14d, %r14d
movq 0x78(%rsp), %rbx
leal (%r15,%rbx), %eax
imull %eax, %eax
addl %r14d, %eax
cmpl %ebp, %eax
ja 0x5c19e
movq 0x170(%rsp), %rcx
movslq 0x180(%rsp), %rdx
movslq %r12d, %rsi
imulq %rdx, %rsi
addq %rbx, %rsi
movsd (%rcx,%rsi,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5c10e
jnp 0x5c19e
movsd 0x48(%rsp), %xmm2
subsd %xmm0, %xmm2
mulsd %xmm2, %xmm2
movsd %xmm2, 0x98(%rsp)
movsd %xmm0, 0xa0(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x88(%rsp), %xmm0
movsd %xmm1, 0x18(%rsp)
movsd %xmm3, 0x10(%rsp)
callq 0xe7f0
movsd %xmm0, 0x90(%rsp)
movsd 0x98(%rsp), %xmm0
divsd 0x50(%rsp), %xmm0
callq 0xe7f0
movsd 0x10(%rsp), %xmm3
movsd 0x18(%rsp), %xmm1
movsd 0x46e38(%rip), %xmm2    # 0xa2fb8
mulsd 0x90(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm4
mulsd %xmm0, %xmm4
addsd %xmm4, %xmm3
addsd %xmm0, %xmm1
incq %rbx
leal -0x1(%rbx), %eax
cmpl %r13d, %eax
jl 0x5c0d1
leal 0x1(%r12), %eax
cmpl 0x3c(%rsp), %r12d
movl %eax, %r12d
movq 0x80(%rsp), %r14
jl 0x5c0a0
ucomisd 0x46018(%rip), %xmm1    # 0xa21e8
movapd %xmm2, %xmm0
movl 0x24(%rsp), %ebx
movl 0x2c(%rsp), %edx
movl 0x28(%rsp), %esi
movl 0x30(%rsp), %edi
movq 0x70(%rsp), %r8
jne 0x5c1ed
jnp 0x5c1f5
divsd %xmm1, %xmm3
movapd %xmm3, %xmm0
movq 0xb8(%r14), %rax
movl 0xc8(%r14), %ecx
imull 0x8(%rsp), %ecx
addl %r8d, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%rax,%rcx,8)
incl %r8d
movl 0xc0(%r14), %ecx
incl %edi
decq %r15
cmpl %ecx, %r8d
jl 0x5bfd9
movl 0xc4(%r14), %eax
movq 0x8(%rsp), %rdx
incl %edx
movq %rdx, 0x8(%rsp)
cmpl %eax, %edx
jl 0x5bfa8
leaq 0xb8(%rsp), %rdi
callq 0x59b0c
addq $0x188, %rsp            # imm = 0x188
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x59b0c
movq %rbx, %rdi
callq 0xe7e0
nop