// File: /workspace/github_cpp_makefile_star2_O1/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, 0x10(%rsp)
movsd %xmm1, 0x60(%rsp)
movsd %xmm0, 0x50(%rsp)
movq %rdi, %rbx
leaq 0xb8(%rsp), %rdi
movq %rbx, %rsi
callq 0x5a63c
movsd 0x60(%rsp), %xmm3
ucomisd 0x453c6(%rip), %xmm3    # 0xa2048
jne 0x5ccb0
jp 0x5ccb0
leaq 0xa8(%rsp), %rdi
movq %rbx, %rsi
callq 0x5a83c
movsd 0xb0(%rsp), %xmm3
subsd 0xa8(%rsp), %xmm3
mulsd 0x46300(%rip), %xmm3    # 0xa2fb0
cmpl $0x0, 0xc4(%rbx)
jle 0x5cfc9
movsd 0x4553b(%rip), %xmm0    # 0xa2200
movsd 0x50(%rsp), %xmm2
movapd %xmm2, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm2, %xmm1
movsd %xmm1, 0x88(%rsp)
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm0
mulsd 0x454e8(%rip), %xmm2    # 0xa21d8
addsd 0x45340(%rip), %xmm2    # 0xa2038
cvttsd2si %xmm2, %eax
movl %eax, %ebp
imull %eax, %ebp
movq %rax, 0x28(%rsp)
negl %eax
movl %eax, 0xc(%rsp)
movsd 0x462a4(%rip), %xmm2    # 0xa2fb8
movsd %xmm0, 0x70(%rsp)
movapd %xmm0, 0x40(%rsp)
movq $0x0, (%rsp)
movq %rbx, 0x80(%rsp)
cmpl $0x0, 0xc0(%rbx)
jle 0x5cfb3
movq (%rsp), %rcx
movl %ecx, %edx
movq 0x28(%rsp), %rax
subl %eax, %edx
addl %ecx, %eax
movl %eax, 0x14(%rsp)
xorl %r15d, %r15d
testl %edx, %edx
cmovlel %r15d, %edx
movl %edx, 0x8(%rsp)
movl 0xc(%rsp), %edx
xorl %esi, %esi
testl %edx, %edx
movl $0x0, %eax
cmovgl %edx, %eax
movq %rax, 0x78(%rsp)
movq 0x170(%rsp), %rax
movl 0x180(%rsp), %ecx
imull (%rsp), %ecx
addl %esi, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x38(%rsp)
ucomisd %xmm2, %xmm0
jne 0x5cda1
jnp 0x5cfa0
xorpd %xmm4, %xmm4
cmpl $0x0, 0x10(%rsp)
movq %rsi, 0x30(%rsp)
movl %edx, 0x18(%rsp)
je 0x5cde6
movsd 0x70(%rsp), %xmm0
movsd 0x38(%rsp), %xmm3
mulsd %xmm3, %xmm0
mulsd %xmm3, %xmm0
xorpd %xmm1, %xmm1
cmpltsd %xmm3, %xmm1
andpd %xmm1, %xmm0
andnpd 0x40(%rsp), %xmm1
orpd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
movl 0xc4(%rbx), %eax
leal -0x1(%rax), %edx
movl 0x14(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %edx
xorpd %xmm3, %xmm3
movl %edx, 0x24(%rsp)
cmpl %edx, 0x8(%rsp)
jg 0x5cf57
movq 0x30(%rsp), %rdx
movl %edx, %esi
movq 0x28(%rsp), %rax
subl %eax, %esi
testl %esi, %esi
movl $0x0, %ecx
cmovlel %ecx, %esi
movl %esi, 0x20(%rsp)
addl %edx, %eax
movl %eax, 0x1c(%rsp)
movl 0x8(%rsp), %eax
movl %eax, %r12d
movl 0xc0(%rbx), %eax
leal -0x1(%rax), %r13d
movl 0x1c(%rsp), %ecx
cmpl %eax, %ecx
cmovll %ecx, %r13d
cmpl %r13d, 0x20(%rsp)
jg 0x5cf3c
movl %r12d, %ebx
subl (%rsp), %ebx
imull %ebx, %ebx
movq 0x78(%rsp), %r14
leal (%r15,%r14), %eax
imull %eax, %eax
addl %ebx, %eax
cmpl %ebp, %eax
ja 0x5cf2c
movq 0x170(%rsp), %rcx
movslq 0x180(%rsp), %rdx
movslq %r12d, %rsi
imulq %rdx, %rsi
addq %r14, %rsi
movsd (%rcx,%rsi,8), %xmm0
ucomisd %xmm2, %xmm0
jne 0x5ce9c
jnp 0x5cf2c
movsd 0x38(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd %xmm1, 0x98(%rsp)
movsd %xmm0, 0xa0(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd 0x88(%rsp), %xmm0
movapd %xmm4, 0x60(%rsp)
movapd %xmm3, 0x50(%rsp)
callq 0xe7f0
movsd %xmm0, 0x90(%rsp)
movsd 0x98(%rsp), %xmm0
divsd 0x40(%rsp), %xmm0
callq 0xe7f0
movapd 0x50(%rsp), %xmm3
movapd 0x60(%rsp), %xmm4
movsd 0x460aa(%rip), %xmm2    # 0xa2fb8
mulsd 0x90(%rsp), %xmm0
movsd 0xa0(%rsp), %xmm1
mulsd %xmm0, %xmm1
addsd %xmm1, %xmm4
addsd %xmm0, %xmm3
incq %r14
leal -0x1(%r14), %eax
cmpl %r13d, %eax
jl 0x5ce60
leal 0x1(%r12), %eax
cmpl 0x24(%rsp), %r12d
movl %eax, %r12d
movq 0x80(%rsp), %rbx
jl 0x5ce33
divsd %xmm3, %xmm4
cmpeqsd 0x45284(%rip), %xmm3    # 0xa21e8
movapd %xmm3, %xmm0
andnpd %xmm4, %xmm0
movsd 0x46044(%rip), %xmm1    # 0xa2fb8
andpd %xmm1, %xmm3
orpd %xmm0, %xmm3
movq 0xb8(%rbx), %rax
movl 0xc8(%rbx), %ecx
imull (%rsp), %ecx
movq 0x30(%rsp), %rsi
addl %esi, %ecx
movslq %ecx, %rcx
movlpd %xmm3, (%rax,%rcx,8)
movl 0x18(%rsp), %edx
incl %esi
incl %edx
decq %r15
cmpl 0xc0(%rbx), %esi
jl 0x5cd63
movq (%rsp), %rax
incl %eax
movq %rax, (%rsp)
cmpl 0xc4(%rbx), %eax
jl 0x5cd30
leaq 0xb8(%rsp), %rdi
callq 0x5a814
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 0x5a814
movq %rbx, %rdi
callq 0xe7e0