// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::Blur(double)
void R2Grid::
Blur(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 = XResolution();
  if (res < YResolution()) res = YResolution();
  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 in X direction
  for (int j = 0; j < YResolution(); j++) { 
    for (int i = 0; i < XResolution(); i++) 
      buffer[i] = GridValue(i, j); 
    for (int i = 0; i < XResolution(); 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) SetGridValue(i, j, sum / weight);
    }
  }

  // Convolve grid with filter in Y direction
  for (int j = 0; j < XResolution(); j++) { 
    for (int i = 0; i < YResolution(); i++) 
      buffer[i] = GridValue(j, i); 
    for (int i = 0; i < YResolution(); 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 = YResolution() - 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) SetGridValue(j, i, sum / weight);
    }
  }

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

// Assembly:
subq $0x128, %rsp            # imm = 0x128
movq %rdi, 0x120(%rsp)
movsd %xmm0, 0x118(%rsp)
movq 0x120(%rsp), %rax
movq %rax, 0x30(%rsp)
movsd 0x118(%rsp), %xmm0
movsd %xmm0, 0x110(%rsp)
movsd 0x110(%rsp), %xmm0
movsd 0x883d8(%rip), %xmm1    # 0x1331e0
mulsd %xmm1, %xmm0
movsd 0x881fc(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x10c(%rsp)
movl 0x10c(%rsp), %eax
addl $0x1, %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq 0x30(%rsp), %rdi
movq %rax, 0x100(%rsp)
callq 0xb9260
movq 0x30(%rsp), %rdi
movl %eax, 0xfc(%rsp)
movl 0xfc(%rsp), %eax
movl %eax, 0x3c(%rsp)
callq 0xb9280
movl %eax, %ecx
movl 0x3c(%rsp), %eax
cmpl %ecx, %eax
jge 0xaae99
movq 0x30(%rsp), %rdi
callq 0xb9280
movl %eax, 0xfc(%rsp)
movslq 0xfc(%rsp), %rax
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0xf030
movq %rax, 0xf0(%rsp)
leaq 0x8ba27(%rip), %rax     # 0x1368f8
movsd (%rax), %xmm0
callq 0xf390
movsd %xmm0, 0xe8(%rsp)
movsd 0xe8(%rsp), %xmm0
mulsd 0x110(%rsp), %xmm0
movsd %xmm0, 0xe0(%rsp)
movsd 0xe0(%rsp), %xmm1
mulsd 0xe0(%rsp), %xmm1
mulsd 0xe0(%rsp), %xmm1
movsd 0x880e7(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
movsd %xmm0, 0xd8(%rsp)
movsd 0x882ca(%rip), %xmm0    # 0x133200
mulsd 0x110(%rsp), %xmm0
mulsd 0x110(%rsp), %xmm0
movsd %xmm0, 0xd0(%rsp)
movl $0x0, 0xcc(%rsp)
movl 0xcc(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jg 0xaafd3
movsd 0xd8(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %eax, %eax
subl 0xcc(%rsp), %eax
imull 0xcc(%rsp), %eax
cvtsi2sd %eax, %xmm0
divsd 0xd0(%rsp), %xmm0
callq 0xf8c0
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0x100(%rsp), %rax
movslq 0xcc(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0xcc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xcc(%rsp)
jmp 0xaaf5c
movl $0x0, 0xc8(%rsp)
movq 0x30(%rsp), %rdi
movl 0xc8(%rsp), %eax
movl %eax, 0x24(%rsp)
callq 0xb9280
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0xab3e2
movl $0x0, 0xc4(%rsp)
movq 0x30(%rsp), %rdi
movl 0xc4(%rsp), %eax
movl %eax, 0x20(%rsp)
callq 0xb9260
movl %eax, %ecx
movl 0x20(%rsp), %eax
cmpl %ecx, %eax
jge 0xab06b
movq 0x30(%rsp), %rdi
movl 0xc4(%rsp), %esi
movl 0xc8(%rsp), %edx
callq 0xb92a0
movq 0xf0(%rsp), %rax
movslq 0xc4(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0xc4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc4(%rsp)
jmp 0xab00c
movl $0x0, 0xc0(%rsp)
movq 0x30(%rsp), %rdi
movl 0xc0(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0xb9260
movl %eax, %ecx
movl 0x1c(%rsp), %eax
cmpl %ecx, %eax
jge 0xab3ca
movq 0xf0(%rsp), %rax
movslq 0xc0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x88dfa(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xab0c3
jp 0xab0c3
jmp 0xab3b4
xorps %xmm0, %xmm0
movsd %xmm0, 0xb8(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xb0(%rsp)
movq 0xf0(%rsp), %rax
movslq 0xc0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0xa8(%rsp)
movsd 0xa8(%rsp), %xmm0
movsd 0x88da6(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xab114
jp 0xab114
jmp 0xab161
movq 0x100(%rsp), %rax
movsd (%rax), %xmm0
movsd 0xa8(%rsp), %xmm2
movsd 0xb8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x100(%rsp), %rax
movsd (%rax), %xmm0
addsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
movl 0xc0(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jle 0xab18d
movl 0x10c(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl $0x1, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jg 0xab265
movq 0xf0(%rsp), %rax
movl 0xc0(%rsp), %ecx
subl 0xa0(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm0
movsd 0x88ccc(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xab1ee
jp 0xab1ee
jmp 0xab24d
movq 0x100(%rsp), %rax
movslq 0xa0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x98(%rsp), %xmm2
movsd 0xb8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x100(%rsp), %rax
movslq 0xa0(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
jmp 0xab24f
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0xab198
movq 0x30(%rsp), %rdi
callq 0xb9260
subl $0x1, %eax
subl 0xc0(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jle 0xab29e
movl 0x10c(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl $0x1, 0x94(%rsp)
movl 0x94(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jg 0xab376
movq 0xf0(%rsp), %rax
movl 0xc0(%rsp), %ecx
addl 0x94(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0x88(%rsp), %xmm0
movsd 0x88bbb(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xab2ff
jp 0xab2ff
jmp 0xab35e
movq 0x100(%rsp), %rax
movslq 0x94(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x88(%rsp), %xmm2
movsd 0xb8(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movq 0x100(%rsp), %rax
movslq 0x94(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0xb0(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
jmp 0xab360
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0xab2a9
movsd 0xb0(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xab3b2
movq 0x30(%rsp), %rdi
movl 0xc0(%rsp), %esi
movl 0xc8(%rsp), %edx
movsd 0xb8(%rsp), %xmm0
divsd 0xb0(%rsp), %xmm0
callq 0x6bc00
jmp 0xab3b4
movl 0xc0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc0(%rsp)
jmp 0xab076
jmp 0xab3cc
movl 0xc8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xc8(%rsp)
jmp 0xaafde
movl $0x0, 0x84(%rsp)
movq 0x30(%rsp), %rdi
movl 0x84(%rsp), %eax
movl %eax, 0x18(%rsp)
callq 0xb9260
movl %eax, %ecx
movl 0x18(%rsp), %eax
cmpl %ecx, %eax
jge 0xab740
movl $0x0, 0x80(%rsp)
movq 0x30(%rsp), %rdi
movl 0x80(%rsp), %eax
movl %eax, 0x14(%rsp)
callq 0xb9280
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0xab47a
movq 0x30(%rsp), %rdi
movl 0x84(%rsp), %esi
movl 0x80(%rsp), %edx
callq 0xb92a0
movq 0xf0(%rsp), %rax
movslq 0x80(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0xab41b
movl $0x0, 0x7c(%rsp)
movq 0x30(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, 0x10(%rsp)
callq 0xb9280
movl %eax, %ecx
movl 0x10(%rsp), %eax
cmpl %ecx, %eax
jge 0xab728
movq 0xf0(%rsp), %rax
movslq 0x7c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x889f4(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xab4c9
jp 0xab4c9
jmp 0xab718
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0xf0(%rsp), %rax
movslq 0x7c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movsd 0x889af(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xab50b
jp 0xab50b
jmp 0xab549
movq 0x100(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x60(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x100(%rsp), %rax
movsd (%rax), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x7c(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jle 0xab569
movl 0x10c(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl $0x1, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jg 0xab611
movq 0xf0(%rsp), %rax
movl 0x7c(%rsp), %ecx
subl 0x58(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x88905(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xab5b5
jp 0xab5b5
jmp 0xab5ff
movq 0x100(%rsp), %rax
movslq 0x58(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x50(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x100(%rsp), %rax
movslq 0x58(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0xab601
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0xab571
movq 0x30(%rsp), %rdi
callq 0xb9280
subl $0x1, %eax
subl 0x7c(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0x10c(%rsp), %eax
jle 0xab63e
movl 0x10c(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl $0x1, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jg 0xab6e6
movq 0xf0(%rsp), %rax
movl 0x7c(%rsp), %ecx
addl 0x4c(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
movsd 0x88830(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xab68a
jp 0xab68a
jmp 0xab6d4
movq 0x100(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x40(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0x100(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0xab6d6
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xab646
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xab716
movq 0x30(%rsp), %rdi
movl 0x84(%rsp), %esi
movl 0x7c(%rsp), %edx
movsd 0x70(%rsp), %xmm0
divsd 0x68(%rsp), %xmm0
callq 0x6bc00
jmp 0xab718
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0xab482
jmp 0xab72a
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0xab3ed
movq 0x100(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xab75d
movq 0x8(%rsp), %rdi
callq 0xf670
movq 0xf0(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xab778
movq (%rsp), %rdi
callq 0xf670
addq $0x128, %rsp            # imm = 0x128
retq