// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::Blur(int, double)
void R2Grid::
Blur(RNDimension dim, RNLength grid_sigma) 
{
  // Build filter
  RNScalar sigma = grid_sigma;
  int filter_radius = (int) (3 * sigma + 0.5);
  RNScalar *filter = new RNScalar [ filter_radius + 1 ];
  assert(filter);

  // Make buffer for temporary copy of row
  int res = Resolution(dim);
  RNScalar *buffer = new RNScalar [ res ];
  assert(buffer);

  // Fill filter with Gaussian 
  const RNScalar sqrt_two_pi = sqrt(RN_TWO_PI);
  double a = sqrt_two_pi * sigma;
  double fac = 1.0 / (a * a * a);
  double denom = 2.0 * sigma * sigma;
  for (int i = 0; i <= filter_radius; i++) {
    filter[i] = fac * exp(-i * i / denom);
  }

  // Convolve grid with filter 
  for (int j = 0; j < Resolution(1-dim); j++) { 
    for (int i = 0; i < Resolution(dim); i++) 
      buffer[i] = (dim == RN_X) ? GridValue(i, j) : GridValue(j, i); 
    for (int i = 0; i < Resolution(dim); i++) { 
      if (buffer[i] == R2_GRID_UNKNOWN_VALUE) continue;
      RNScalar sum = 0;
      RNScalar weight = 0;
      RNScalar value = buffer[i];
      if (value != R2_GRID_UNKNOWN_VALUE) {
        sum += filter[0] * value;
        weight += filter[0];
      }
      int nsamples = i;
      if (nsamples > filter_radius) nsamples = filter_radius;
      for (int m = 1; m <= nsamples; m++) {
        RNScalar value = buffer[i - m];;
        if (value != R2_GRID_UNKNOWN_VALUE) {
          sum += filter[m] * value;
          weight += filter[m];
        }
      }
      nsamples = XResolution() - 1 - i;
      if (nsamples > filter_radius) nsamples = filter_radius;
      for (int m = 1; m <= nsamples; m++) {
        RNScalar value = buffer[i + m];
        if (value != R2_GRID_UNKNOWN_VALUE) {
          sum += filter[m] * value;
          weight += filter[m];
        }
      }
      if (weight > 0) {
        RNScalar value = sum / weight;
        if (dim == RN_X) SetGridValue(i, j, value);
        else SetGridValue(j, i, value);
      }
    }
  }

  // Deallocate memory
  delete [] filter;
  delete [] buffer;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %ebx
movq %rdi, %r12
movsd 0x45e01(%rip), %xmm1    # 0xa21d8
movsd %xmm0, (%rsp)
mulsd %xmm0, %xmm1
addsd 0x45c48(%rip), %xmm1    # 0xa2030
cvttsd2si %xmm1, %r13d
movslq %r13d, %rax
leaq 0x8(,%rax,8), %rdi
cmpl $-0x1, %eax
movq $-0x1, %r14
cmovlq %r14, %rdi
callq 0xe030
movq %rax, %r15
movslq %ebx, %rax
movq %r12, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movslq 0xc0(%r12,%rax,4), %rax
leaq (,%rax,8), %rdi
testq %rax, %rax
cmovsq %r14, %rdi
callq 0xe030
movq %rax, %r12
leaq 0x4db87(%rip), %rax     # 0xa9fc8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x5c455
sqrtsd %xmm0, %xmm0
jmp 0x5c45a
callq 0xe340
testl %r13d, %r13d
js 0x5c4c9
movsd (%rsp), %xmm2
mulsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x45bbc(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
leal 0x1(%r13), %r14d
xorl %ebp, %ebp
movl %ebp, %eax
imull %ebp, %eax
negl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0xe7f0
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, (%r15,%rbp,8)
incq %rbp
cmpq %rbp, %r14
jne 0x5c49d
movl $0x1, %eax
subl %ebx, %eax
cltq
movq 0x8(%rsp), %rcx
movslq 0xc0(%rcx,%rax,4), %rax
movq %rax, (%rsp)
testq %rax, %rax
jle 0x5c6ac
movq 0x8(%rsp), %rax
movq 0x10(%rsp), %rcx
movl 0xc0(%rax,%rcx,4), %ecx
leaq -0x8(%r12), %rax
movq %rax, 0x10(%rsp)
movq %r12, %rax
addq $0x8, %rax
movq %rax, 0x20(%rsp)
xorl %edi, %edi
movsd 0x46a9b(%rip), %xmm0    # 0xa2fb8
xorpd %xmm1, %xmm1
testl %ecx, %ecx
jle 0x5c566
movq 0x8(%rsp), %rdx
movq 0xb8(%rdx), %rax
movl 0xc8(%rdx), %edx
movl %edx, %esi
imull %edi, %esi
movl %edi, %r8d
xorl %r9d, %r9d
leal (%rsi,%r9), %r10d
testl %ebx, %ebx
cmovnel %r8d, %r10d
movslq %r10d, %r10
movsd (%rax,%r10,8), %xmm2
movsd %xmm2, (%r12,%r9,8)
incq %r9
addl %edx, %r8d
cmpq %r9, %rcx
jne 0x5c542
testl %ecx, %ecx
jle 0x5c69f
movq 0x8(%rsp), %rax
movl 0xc0(%rax), %r8d
movq 0xb8(%rax), %rdx
movq %rdx, 0x18(%rsp)
movslq 0xc8(%rax), %r10
movq %rdi, %r11
imulq %r10, %r11
movq 0x20(%rsp), %rbp
movq 0x10(%rsp), %r9
xorl %eax, %eax
cmpl %eax, %r13d
movl %eax, %esi
cmovll %r13d, %esi
movsd (%r12,%rax,8), %xmm2
ucomisd %xmm0, %xmm2
jne 0x5c5bb
jnp 0x5c68b
ucomisd %xmm0, %xmm2
jne 0x5c5c3
jnp 0x5c5d6
movsd (%r15), %xmm3
mulsd %xmm3, %xmm2
addsd %xmm1, %xmm2
addsd %xmm1, %xmm3
jmp 0x5c5de
xorpd %xmm2, %xmm2
xorpd %xmm3, %xmm3
cmpl %r13d, %eax
movl %r13d, %edx
cmovll %eax, %edx
testl %edx, %edx
jle 0x5c620
incl %esi
movl $0x1, %r14d
movq %r9, %rdx
movsd (%rdx), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5c602
jnp 0x5c614
movsd (%r15,%r14,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %r14
addq $-0x8, %rdx
cmpq %r14, %rsi
jne 0x5c5f6
movl %eax, %edx
notl %edx
addl %r8d, %edx
cmpl %r13d, %edx
cmovgel %r13d, %edx
testl %edx, %edx
jle 0x5c65f
movl %edx, %edx
xorl %esi, %esi
movsd (%rbp,%rsi,8), %xmm4
ucomisd %xmm0, %xmm4
jne 0x5c644
jnp 0x5c657
movsd 0x8(%r15,%rsi,8), %xmm5
mulsd %xmm5, %xmm4
addsd %xmm4, %xmm2
addsd %xmm5, %xmm3
incq %rsi
cmpq %rdx, %rsi
jb 0x5c636
ucomisd %xmm1, %xmm3
jbe 0x5c68b
movq %rax, %rdx
imulq %r10, %rdx
testl %ebx, %ebx
divsd %xmm3, %xmm2
cmoveq %r11, %rdx
movq %rdi, %rsi
cmoveq %rax, %rsi
movq 0x18(%rsp), %r14
leaq (%r14,%rdx,8), %rdx
movsd %xmm2, (%rdx,%rsi,8)
incq %rax
addq $0x8, %r9
addq $0x8, %rbp
cmpq %rcx, %rax
jne 0x5c5a0
incq %rdi
cmpq (%rsp), %rdi
jne 0x5c521
movq %r15, %rdi
callq 0xe5b0
movq %r12, %rdi
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0