// File: 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:
subq $0xe8, %rsp
movq %rdi, 0xe0(%rsp)
movl %esi, 0xdc(%rsp)
movsd %xmm0, 0xd0(%rsp)
movq 0xe0(%rsp), %rax
movq %rax, 0x30(%rsp)
movsd 0xd0(%rsp), %xmm0
movsd %xmm0, 0xc8(%rsp)
movsd 0xc8(%rsp), %xmm0
movsd 0x889e1(%rip), %xmm1    # 0x1331e0
mulsd %xmm1, %xmm0
movsd 0x88805(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xc4(%rsp)
movl 0xc4(%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, 0xb8(%rsp)
movl 0xdc(%rsp), %esi
callq 0xb9480
movl %eax, 0xb4(%rsp)
movslq 0xb4(%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, 0xa8(%rsp)
leaq 0x8c059(%rip), %rax     # 0x1368f8
movsd (%rax), %xmm0
callq 0xf390
movsd %xmm0, 0xa0(%rsp)
movsd 0xa0(%rsp), %xmm0
mulsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0x98(%rsp)
movsd 0x98(%rsp), %xmm1
mulsd 0x98(%rsp), %xmm1
mulsd 0x98(%rsp), %xmm1
movsd 0x88719(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x888fc(%rip), %xmm0    # 0x133200
mulsd 0xc8(%rsp), %xmm0
mulsd 0xc8(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jg 0xaa9a1
movsd 0x90(%rsp), %xmm0
movsd %xmm0, 0x28(%rsp)
xorl %eax, %eax
subl 0x84(%rsp), %eax
imull 0x84(%rsp), %eax
cvtsi2sd %eax, %xmm0
divsd 0x88(%rsp), %xmm0
callq 0xf8c0
movaps %xmm0, %xmm1
movsd 0x28(%rsp), %xmm0
mulsd %xmm1, %xmm0
movq 0xb8(%rsp), %rax
movslq 0x84(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0xaa92a
movl $0x0, 0x80(%rsp)
movq 0x30(%rsp), %rdi
movl 0x80(%rsp), %eax
movl %eax, 0x24(%rsp)
movl $0x1, %esi
subl 0xdc(%rsp), %esi
callq 0xb9480
movl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %ecx, %eax
jge 0xaad72
movl $0x0, 0x7c(%rsp)
movq 0x30(%rsp), %rdi
movl 0x7c(%rsp), %eax
movl %eax, 0x20(%rsp)
movl 0xdc(%rsp), %esi
callq 0xb9480
movl %eax, %ecx
movl 0x20(%rsp), %eax
cmpl %ecx, %eax
jge 0xaaa70
cmpl $0x0, 0xdc(%rsp)
jne 0xaaa2d
movq 0x30(%rsp), %rdi
movl 0x7c(%rsp), %esi
movl 0x80(%rsp), %edx
callq 0xb92a0
movsd %xmm0, 0x18(%rsp)
jmp 0xaaa48
movq 0x30(%rsp), %rdi
movl 0x80(%rsp), %esi
movl 0x7c(%rsp), %edx
callq 0xb92a0
movsd %xmm0, 0x18(%rsp)
movsd 0x18(%rsp), %xmm0
movq 0xa8(%rsp), %rax
movslq 0x7c(%rsp), %rcx
movsd %xmm0, (%rax,%rcx,8)
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0xaa9e3
movl $0x0, 0x78(%rsp)
movq 0x30(%rsp), %rdi
movl 0x78(%rsp), %eax
movl %eax, 0x14(%rsp)
movl 0xdc(%rsp), %esi
callq 0xb9480
movl %eax, %ecx
movl 0x14(%rsp), %eax
cmpl %ecx, %eax
jge 0xaad5a
movq 0xa8(%rsp), %rax
movslq 0x78(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x893f7(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xaaac6
jp 0xaaac6
jmp 0xaad4a
xorps %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0x68(%rsp)
movq 0xa8(%rsp), %rax
movslq 0x78(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movsd 0x893b2(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xaab08
jp 0xaab08
jmp 0xaab46
movq 0xb8(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x60(%rsp), %xmm2
movsd 0x70(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movq 0xb8(%rsp), %rax
movsd (%rax), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movl 0x78(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jle 0xaab66
movl 0xc4(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl $0x1, 0x58(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jg 0xaac0e
movq 0xa8(%rsp), %rax
movl 0x78(%rsp), %ecx
subl 0x58(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0x50(%rsp), %xmm0
movsd 0x89308(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xaabb2
jp 0xaabb2
jmp 0xaabfc
movq 0xb8(%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 0xb8(%rsp), %rax
movslq 0x58(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0xaabfe
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0xaab6e
movq 0x30(%rsp), %rdi
callq 0xb9260
subl $0x1, %eax
subl 0x78(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0xc4(%rsp), %eax
jle 0xaac3b
movl 0xc4(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl $0x1, 0x4c(%rsp)
movl 0x4c(%rsp), %eax
cmpl 0x5c(%rsp), %eax
jg 0xaace3
movq 0xa8(%rsp), %rax
movl 0x78(%rsp), %ecx
addl 0x4c(%rsp), %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x40(%rsp), %xmm0
movsd 0x89233(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xaac87
jp 0xaac87
jmp 0xaacd1
movq 0xb8(%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 0xb8(%rsp), %rax
movslq 0x4c(%rsp), %rcx
movsd (%rax,%rcx,8), %xmm0
addsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
jmp 0xaacd3
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xaac43
movsd 0x68(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0xaad48
movsd 0x70(%rsp), %xmm0
divsd 0x68(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
cmpl $0x0, 0xdc(%rsp)
jne 0xaad2b
movq 0x30(%rsp), %rdi
movl 0x78(%rsp), %esi
movl 0x80(%rsp), %edx
movsd 0x38(%rsp), %xmm0
callq 0x6bc00
jmp 0xaad46
movq 0x30(%rsp), %rdi
movl 0x80(%rsp), %esi
movl 0x78(%rsp), %edx
movsd 0x38(%rsp), %xmm0
callq 0x6bc00
jmp 0xaad48
jmp 0xaad4a
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0xaaa78
jmp 0xaad5c
movl 0x80(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x80(%rsp)
jmp 0xaa9ac
movq 0xb8(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0xaad8f
movq 0x8(%rsp), %rdi
callq 0xf670
movq 0xa8(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xaadaa
movq (%rsp), %rdi
callq 0xf670
addq $0xe8, %rsp
retq
nopw %cs:(%rax,%rax)