// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::Blur(double)
void R3Grid::
Blur(RNLength grid_sigma) 
{
  // Check sigma
  if (RNIsZero(grid_sigma)) return;

  // 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();
  if (res < ZResolution()) res = ZResolution();
  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 k = 0; k < ZResolution(); k++) {
    for (int j = 0; j < YResolution(); j++) { 
      for (int i = 0; i < XResolution(); i++) 
        buffer[i] = GridValue(i, j, k); 
      for (int i = 0; i < XResolution(); i++) { 
        RNScalar sum = filter[0] * buffer[i];
        RNScalar weight = filter[0];
        int nsamples = i;
        if (nsamples > filter_radius) nsamples = filter_radius;
        for (int m = 1; m <= nsamples; m++) {
          sum += filter[m] * buffer[i - m];
          weight += filter[m];
        }
        nsamples = XResolution() - 1 - i;
        if (nsamples > filter_radius) nsamples = filter_radius;
        for (int m = 1; m <= nsamples; m++) {
          sum += filter[m] * buffer[i + m];
          weight += filter[m];
        }
        SetGridValue(i, j, k, sum / weight);
      }
    }
  }

  // Convolve grid with filter in Y direction
  for (int k = 0; k < ZResolution(); k++) {
    for (int j = 0; j < XResolution(); j++) { 
      for (int i = 0; i < YResolution(); i++) 
        buffer[i] = GridValue(j, i, k); 
      for (int i = 0; i < YResolution(); i++) { 
        RNScalar sum = filter[0] * buffer[i];
        RNScalar weight = filter[0];
        int nsamples = i;
        if (nsamples > filter_radius) nsamples = filter_radius;
        for (int m = 1; m <= nsamples; m++) {
          sum += filter[m] * buffer[i - m];
          weight += filter[m];
        }
        nsamples = YResolution() - 1 - i;
        if (nsamples > filter_radius) nsamples = filter_radius;
        for (int m = 1; m <= nsamples; m++) {
          sum += filter[m] * buffer[i + m];
          weight += filter[m];
        }
        SetGridValue(j, i, k, sum / weight);
      }
    }
  }

  // Convolve grid with filter in Z direction
  for (int k = 0; k < YResolution(); k++) {
    for (int j = 0; j < XResolution(); j++) { 
      for (int i = 0; i < ZResolution(); i++) 
        buffer[i] = GridValue(j, k, i); 
      for (int i = 0; i < ZResolution(); i++) { 
        RNScalar sum = filter[0] * buffer[i];
        RNScalar weight = filter[0];
        int nsamples = i;
        if (nsamples > filter_radius) nsamples = filter_radius;
        for (int m = 1; m <= nsamples; m++) {
          sum += filter[m] * buffer[i - m];
          weight += filter[m];
        }
        nsamples = ZResolution() - 1 - i;
        if (nsamples > filter_radius) nsamples = filter_radius;
        for (int m = 1; m <= nsamples; m++) {
          sum += filter[m] * buffer[i + m];
          weight += filter[m];
        }
        SetGridValue(j, k, i, sum / weight);
      }
    }
  }

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

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %rdi, 0x10(%rsp)
leaq 0xb50f4(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm2
movapd 0x71570(%rip), %xmm1    # 0xa2020
xorpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x30acf
ucomisd %xmm0, %xmm2
jb 0x30acf
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x71701(%rip), %xmm1    # 0xa21d8
movsd %xmm0, (%rsp)
mulsd %xmm0, %xmm1
addsd 0x71548(%rip), %xmm1    # 0xa2030
cvttsd2si %xmm1, %r12d
leal 0x1(%r12), %ebx
movslq %ebx, %rdi
shlq $0x3, %rdi
cmpl $-0x1, %r12d
movq $-0x1, %r15
cmovlq %r15, %rdi
callq 0xe030
movq %rax, %r14
movq 0x10(%rsp), %rdx
movl 0x238(%rdx), %eax
movl 0x23c(%rdx), %ecx
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl 0x240(%rdx), %eax
cmpl %eax, %ecx
cmovlel %eax, %ecx
movslq %ecx, %rax
leaq (,%rax,8), %rdi
testl %eax, %eax
cmovsq %r15, %rdi
callq 0xe030
movq %rax, %r15
leaq 0x79497(%rip), %rax     # 0xa9fe8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x30b65
sqrtsd %xmm0, %xmm0
jmp 0x30b6a
callq 0xe340
testl %r12d, %r12d
movsd (%rsp), %xmm2
js 0x30bda
mulsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x714ac(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
movl %ebx, %ebx
xorl %r13d, %r13d
movl %r13d, %eax
imull %r13d, %eax
negl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0xe7f0
mulsd 0x18(%rsp), %xmm0
movsd %xmm0, (%r14,%r13,8)
incq %r13
cmpq %r13, %rbx
jne 0x30bac
movq 0x10(%rsp), %rax
movl 0x23c(%rax), %esi
movl 0x240(%rax), %ecx
testl %ecx, %ecx
movq %rsi, 0x18(%rsp)
jle 0x30f06
movq 0x10(%rsp), %rax
movl 0x238(%rax), %edx
leaq -0x8(%r15), %rax
movq %rax, 0x30(%rsp)
leaq 0x8(%r15), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x8(%rsp)
testl %esi, %esi
jle 0x30d67
movq 0x10(%rsp), %rax
movq 0x230(%rax), %r9
movl 0x248(%rax), %r10d
imull 0x8(%rsp), %r10d
movl 0x244(%rax), %eax
movq %rax, 0x20(%rsp)
movl $0x0, (%rsp)
movq %r10, %r13
testl %edx, %edx
jle 0x30d4d
xorl %eax, %eax
leal (%rax,%r13), %esi
movslq %esi, %rsi
movsd (%r9,%rsi,8), %xmm0
movsd %xmm0, (%r15,%rax,8)
incq %rax
cmpq %rax, %rdx
jne 0x30c5e
testl %edx, %edx
jle 0x30d4d
movq 0x20(%rsp), %rax
movl %eax, %ebx
imull (%rsp), %ebx
movsd (%r14), %xmm0
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rax
xorl %r11d, %r11d
cmpl %r11d, %r12d
movl %r11d, %r8d
cmovll %r12d, %r8d
movsd (%r15,%r11,8), %xmm1
mulsd %xmm0, %xmm1
testl %r8d, %r8d
jle 0x30cea
incl %r8d
movl $0x1, %ebp
movq %rax, %rsi
movapd %xmm0, %xmm2
movsd (%r14,%rbp,8), %xmm3
movsd (%rsi), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm1
addsd %xmm3, %xmm2
incq %rbp
addq $-0x8, %rsi
cmpq %rbp, %r8
jne 0x30cc6
jmp 0x30cee
movapd %xmm0, %xmm2
movl %r11d, %esi
notl %esi
addl %edx, %esi
cmpl %r12d, %esi
cmovgel %r12d, %esi
testl %esi, %esi
jle 0x30d26
movl %esi, %esi
xorl %r8d, %r8d
movsd 0x8(%r14,%r8,8), %xmm3
movsd (%rdi,%r8,8), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm1
addsd %xmm3, %xmm2
incq %r8
cmpq %rsi, %r8
jb 0x30d05
divsd %xmm2, %xmm1
leal (%r10,%r11), %esi
addl %ebx, %esi
movslq %esi, %rsi
movsd %xmm1, (%r9,%rsi,8)
incq %r11
addq $0x8, %rax
addq $0x8, %rdi
cmpq %rdx, %r11
jne 0x30c9e
movl (%rsp), %eax
incl %eax
addq 0x20(%rsp), %r13
movq 0x18(%rsp), %rsi
movl %eax, (%rsp)
cmpl %esi, %eax
jne 0x30c54
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
cmpl %ecx, %eax
jne 0x30c1e
testl %ecx, %ecx
jle 0x30f06
movq 0x10(%rsp), %rax
movl 0x238(%rax), %eax
movl %eax, (%rsp)
leaq -0x8(%r15), %rax
movq %rax, 0x30(%rsp)
leaq 0x8(%r15), %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x8(%rsp)
cmpl $0x0, (%rsp)
jle 0x30ef2
movq 0x10(%rsp), %rax
movq 0x230(%rax), %r9
movl 0x248(%rax), %edx
imull 0x8(%rsp), %edx
movl 0x244(%rax), %r11d
xorl %r13d, %r13d
movq %rdx, 0x20(%rsp)
movl %edx, %ebp
testl %esi, %esi
jle 0x30ede
movl %ebp, %eax
xorl %edx, %edx
movq 0x18(%rsp), %rsi
cltq
movsd (%r9,%rax,8), %xmm0
movsd %xmm0, (%r15,%rdx,8)
incq %rdx
addl %r11d, %eax
cmpq %rdx, %rsi
jne 0x30def
testl %esi, %esi
jle 0x30ede
movq 0x20(%rsp), %rax
leal (%rax,%r13), %ebx
movsd (%r14), %xmm0
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
xorl %r10d, %r10d
cmpl %r10d, %r12d
movl %r10d, %r8d
cmovll %r12d, %r8d
movsd (%r15,%r10,8), %xmm1
mulsd %xmm0, %xmm1
testl %r8d, %r8d
jle 0x30e77
incl %r8d
movl $0x1, %edx
movq %rsi, %rax
movapd %xmm0, %xmm2
movsd (%r14,%rdx,8), %xmm3
movsd (%rax), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm1
addsd %xmm3, %xmm2
incq %rdx
addq $-0x8, %rax
cmpq %rdx, %r8
jne 0x30e53
jmp 0x30e7b
movapd %xmm0, %xmm2
movl %r10d, %eax
notl %eax
addl 0x18(%rsp), %eax
cmpl %r12d, %eax
cmovgel %r12d, %eax
testl %eax, %eax
jle 0x30eb3
movl %eax, %eax
xorl %edx, %edx
movsd 0x8(%r14,%rdx,8), %xmm3
movsd (%rdi,%rdx,8), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm1
addsd %xmm3, %xmm2
incq %rdx
cmpq %rax, %rdx
jb 0x30e93
divsd %xmm2, %xmm1
movl %r11d, %eax
imull %r10d, %eax
addl %ebx, %eax
cltq
movsd %xmm1, (%r9,%rax,8)
incq %r10
addq $0x8, %rsi
addq $0x8, %rdi
cmpq 0x18(%rsp), %r10
jne 0x30e2b
incl %r13d
incl %ebp
cmpl (%rsp), %r13d
movq 0x18(%rsp), %rsi
jne 0x30dde
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
cmpl %ecx, %eax
jne 0x30dac
testl %esi, %esi
jle 0x31088
movq 0x10(%rsp), %rax
movl 0x238(%rax), %eax
movl %eax, (%rsp)
leaq -0x8(%r15), %rax
movq %rax, 0x30(%rsp)
movq %r15, %rax
addq $0x8, %rax
movq %rax, 0x28(%rsp)
movq $0x0, 0x8(%rsp)
cmpl $0x0, (%rsp)
jle 0x31072
movq 0x10(%rsp), %rax
movq 0x230(%rax), %r9
movl 0x248(%rax), %r10d
movl 0x244(%rax), %edx
imull 0x8(%rsp), %edx
xorl %r13d, %r13d
movq %rdx, 0x20(%rsp)
movl %edx, %ebp
testl %ecx, %ecx
jle 0x31063
movl %ebp, %eax
xorl %edx, %edx
cltq
movsd (%r9,%rax,8), %xmm0
movsd %xmm0, (%r15,%rdx,8)
incq %rdx
addl %r10d, %eax
cmpq %rdx, %rcx
jne 0x30f78
testl %ecx, %ecx
jle 0x31063
movsd (%r14), %xmm0
movq 0x20(%rsp), %rax
leal (%rax,%r13), %ebx
movq 0x28(%rsp), %rdi
movq 0x30(%rsp), %rsi
xorl %r11d, %r11d
cmpl %r11d, %r12d
movl %r11d, %r8d
cmovll %r12d, %r8d
movsd (%r15,%r11,8), %xmm1
mulsd %xmm0, %xmm1
testl %r8d, %r8d
jle 0x31000
incl %r8d
movl $0x1, %edx
movq %rsi, %rax
movapd %xmm0, %xmm2
movsd (%r14,%rdx,8), %xmm3
movsd (%rax), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm1
addsd %xmm3, %xmm2
incq %rdx
addq $-0x8, %rax
cmpq %rdx, %r8
jne 0x30fdc
jmp 0x31004
movapd %xmm0, %xmm2
movl %r11d, %eax
notl %eax
addl %ecx, %eax
cmpl %r12d, %eax
cmovgel %r12d, %eax
testl %eax, %eax
jle 0x3103a
movl %eax, %eax
xorl %edx, %edx
movsd 0x8(%r14,%rdx,8), %xmm3
movsd (%rdi,%rdx,8), %xmm4
mulsd %xmm3, %xmm4
addsd %xmm4, %xmm1
addsd %xmm3, %xmm2
incq %rdx
cmpq %rax, %rdx
jb 0x3101a
divsd %xmm2, %xmm1
movl %r10d, %eax
imull %r11d, %eax
addl %ebx, %eax
cltq
movsd %xmm1, (%r9,%rax,8)
incq %r11
addq $0x8, %rsi
addq $0x8, %rdi
cmpq %rcx, %r11
jne 0x30fb4
incl %r13d
incl %ebp
cmpl (%rsp), %r13d
jne 0x30f6c
movq 0x8(%rsp), %rax
incl %eax
movq %rax, 0x8(%rsp)
cmpl 0x18(%rsp), %eax
jne 0x30f3a
movq %r14, %rdi
callq 0xe5b0
movq %r15, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5b0