// File: /workspace/github_cpp_makefile_star2_O1/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 0xb4efc(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm2
movapd 0x71378(%rip), %xmm1    # 0xa2020
xorpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x30cc7
ucomisd %xmm0, %xmm2
jb 0x30cc7
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x71509(%rip), %xmm1    # 0xa21d8
movsd %xmm0, (%rsp)
mulsd %xmm0, %xmm1
addsd 0x71350(%rip), %xmm1    # 0xa2030
cvttsd2si %xmm1, %r12d
movslq %r12d, %rax
leaq 0x8(,%rax,8), %rdi
cmpl $-0x1, %eax
movq $-0x1, %rbx
cmovlq %rbx, %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 %rbx, %rdi
callq 0xe030
movq %rax, %r15
leaq 0x79281(%rip), %rax     # 0xa9fc8
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x30d5b
sqrtsd %xmm0, %xmm0
jmp 0x30d60
callq 0xe340
testl %r12d, %r12d
movsd (%rsp), %xmm2
js 0x30dd3
mulsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x712b6(%rip), %xmm0    # 0xa2038
divsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, (%rsp)
leal 0x1(%r12), %ebx
xorl %r13d, %r13d
movl %r13d, %eax
imull %r13d, %eax
negl %eax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
divsd (%rsp), %xmm0
callq 0xe7f0
mulsd 0x8(%rsp), %xmm0
movsd %xmm0, (%r14,%r13,8)
incq %r13
cmpq %r13, %rbx
jne 0x30da5
movq 0x10(%rsp), %rax
movl 0x240(%rax), %eax
testl %eax, %eax
jle 0x30f65
movq 0x10(%rsp), %rdx
movl 0x238(%rdx), %ecx
movl 0x23c(%rdx), %edx
movl %edx, 0x28(%rsp)
leaq -0x8(%r15), %rdx
movq %rdx, 0x30(%rsp)
leaq 0x8(%r15), %rdx
movq %rdx, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
cmpl $0x0, 0x28(%rsp)
jle 0x30f51
movq 0x10(%rsp), %rdx
movq 0x230(%rdx), %r9
movl 0x244(%rdx), %esi
movq %rsi, (%rsp)
movl 0x248(%rdx), %r11d
imull 0x18(%rsp), %r11d
movl $0x0, 0x8(%rsp)
movq %r11, %r13
testl %ecx, %ecx
jle 0x30e70
xorl %edx, %edx
leal (%rdx,%r13), %esi
movslq %esi, %rsi
movsd (%r9,%rsi,8), %xmm0
movsd %xmm0, (%r15,%rdx,8)
incq %rdx
cmpq %rdx, %rcx
jne 0x30e55
testl %ecx, %ecx
jle 0x30f39
movq (%rsp), %rdx
movl %edx, %ebx
imull 0x8(%rsp), %ebx
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rbp
xorl %edx, %edx
cmpl %edx, %r12d
movl %edx, %r8d
cmovll %r12d, %r8d
movsd (%r14), %xmm1
movsd (%r15,%rdx,8), %xmm0
mulsd %xmm1, %xmm0
testl %r8d, %r8d
jle 0x30edb
incl %r8d
movl $0x1, %r10d
movq %rbp, %rsi
movsd (%r14,%r10,8), %xmm2
movsd (%rsi), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %r10
addq $-0x8, %rsi
cmpq %r10, %r8
jne 0x30eb9
movl %edx, %esi
notl %esi
addl %ecx, %esi
cmpl %r12d, %esi
cmovgel %r12d, %esi
testl %esi, %esi
jle 0x30f12
movl %esi, %esi
xorl %r8d, %r8d
movsd 0x8(%r14,%r8,8), %xmm2
movsd (%rdi,%r8,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %r8
cmpq %rsi, %r8
jb 0x30ef1
divsd %xmm1, %xmm0
leal (%r11,%rdx), %esi
addl %ebx, %esi
movslq %esi, %rsi
movsd %xmm0, (%r9,%rsi,8)
incq %rdx
addq $0x8, %rbp
addq $0x8, %rdi
cmpq %rcx, %rdx
jne 0x30e8f
movl 0x8(%rsp), %edx
incl %edx
addq (%rsp), %r13
movl %edx, 0x8(%rsp)
cmpl 0x28(%rsp), %edx
jne 0x30e4f
movq 0x18(%rsp), %rdx
incl %edx
movq %rdx, 0x18(%rsp)
cmpl %eax, %edx
jne 0x30e16
testl %eax, %eax
jle 0x310e4
movq 0x10(%rsp), %rcx
movl 0x238(%rcx), %edx
movl %edx, (%rsp)
movl 0x23c(%rcx), %edx
leaq -0x8(%r15), %rcx
movq %rcx, 0x30(%rsp)
leaq 0x8(%r15), %rcx
movq %rcx, 0x20(%rsp)
movq $0x0, 0x18(%rsp)
cmpl $0x0, (%rsp)
jle 0x310d0
movq 0x10(%rsp), %rcx
movq 0x230(%rcx), %r9
movl 0x244(%rcx), %r10d
movl 0x248(%rcx), %ecx
imull 0x18(%rsp), %ecx
xorl %r13d, %r13d
movq %rcx, 0x28(%rsp)
movl %ecx, %edi
testl %edx, %edx
jle 0x30ff0
movl %edi, %ecx
xorl %esi, %esi
movslq %ecx, %rcx
movsd (%r9,%rcx,8), %xmm0
movsd %xmm0, (%r15,%rsi,8)
incq %rsi
addl %r10d, %ecx
cmpq %rsi, %rdx
jne 0x30fd6
movl %edi, 0x8(%rsp)
testl %edx, %edx
jle 0x310bd
movq 0x28(%rsp), %rcx
leal (%rcx,%r13), %ebx
movq 0x20(%rsp), %rdi
movq 0x30(%rsp), %rbp
xorl %r11d, %r11d
cmpl %r11d, %r12d
movl %r11d, %r8d
cmovll %r12d, %r8d
movsd (%r14), %xmm1
movsd (%r15,%r11,8), %xmm0
mulsd %xmm1, %xmm0
testl %r8d, %r8d
jle 0x3105d
incl %r8d
movl $0x1, %ecx
movq %rbp, %rsi
movsd (%r14,%rcx,8), %xmm2
movsd (%rsi), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %rcx
addq $-0x8, %rsi
cmpq %rcx, %r8
jne 0x3103b
movl %r11d, %ecx
notl %ecx
addl %edx, %ecx
cmpl %r12d, %ecx
cmovgel %r12d, %ecx
testl %ecx, %ecx
jle 0x31093
movl %ecx, %ecx
xorl %esi, %esi
movsd 0x8(%r14,%rsi,8), %xmm2
movsd (%rdi,%rsi,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %rsi
cmpq %rcx, %rsi
jb 0x31073
divsd %xmm1, %xmm0
movl %r10d, %ecx
imull %r11d, %ecx
addl %ebx, %ecx
movslq %ecx, %rcx
movsd %xmm0, (%r9,%rcx,8)
incq %r11
addq $0x8, %rbp
addq $0x8, %rdi
cmpq %rdx, %r11
jne 0x31012
incl %r13d
movl 0x8(%rsp), %edi
incl %edi
cmpl (%rsp), %r13d
jne 0x30fce
movq 0x18(%rsp), %rcx
incl %ecx
movq %rcx, 0x18(%rsp)
cmpl %eax, %ecx
jne 0x30f9c
movq 0x10(%rsp), %rcx
movl 0x23c(%rcx), %ecx
movl %ecx, 0x18(%rsp)
testl %ecx, %ecx
jle 0x3126a
movq 0x10(%rsp), %rcx
movl 0x238(%rcx), %ecx
movl %ecx, 0x8(%rsp)
leaq -0x8(%r15), %rcx
movq %rcx, 0x28(%rsp)
movq %r15, %rcx
addq $0x8, %rcx
movq %rcx, 0x30(%rsp)
movq $0x0, 0x20(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x31254
movq 0x10(%rsp), %rcx
movq 0x230(%rcx), %r9
movl 0x248(%rcx), %r10d
movl 0x244(%rcx), %edx
imull 0x20(%rsp), %edx
xorl %r13d, %r13d
movq %rdx, (%rsp)
movl %edx, %ebp
testl %eax, %eax
jle 0x3117c
movl %ebp, %ecx
xorl %edx, %edx
movslq %ecx, %rcx
movsd (%r9,%rcx,8), %xmm0
movsd %xmm0, (%r15,%rdx,8)
incq %rdx
addl %r10d, %ecx
cmpq %rdx, %rax
jne 0x31162
testl %eax, %eax
jle 0x31244
movq (%rsp), %rcx
leal (%rcx,%r13), %ebx
movq 0x30(%rsp), %rdi
movq 0x28(%rsp), %rdx
xorl %ecx, %ecx
cmpl %ecx, %r12d
movl %ecx, %r8d
cmovll %r12d, %r8d
movsd (%r14), %xmm1
movsd (%r15,%rcx,8), %xmm0
mulsd %xmm1, %xmm0
testl %r8d, %r8d
jle 0x311e4
incl %r8d
movl $0x1, %r11d
movq %rdx, %rsi
movsd (%r14,%r11,8), %xmm2
movsd (%rsi), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %r11
addq $-0x8, %rsi
cmpq %r11, %r8
jne 0x311c2
movl %ecx, %esi
notl %esi
addl %eax, %esi
cmpl %r12d, %esi
cmovgel %r12d, %esi
testl %esi, %esi
jle 0x3121b
movl %esi, %esi
xorl %r8d, %r8d
movsd 0x8(%r14,%r8,8), %xmm2
movsd (%rdi,%r8,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %r8
cmpq %rsi, %r8
jb 0x311fa
divsd %xmm1, %xmm0
movl %r10d, %esi
imull %ecx, %esi
addl %ebx, %esi
movslq %esi, %rsi
movsd %xmm0, (%r9,%rsi,8)
incq %rcx
addq $0x8, %rdx
addq $0x8, %rdi
cmpq %rax, %rcx
jne 0x31198
incl %r13d
incl %ebp
cmpl 0x8(%rsp), %r13d
jne 0x3115a
movq 0x20(%rsp), %rcx
incl %ecx
movq %rcx, 0x20(%rsp)
cmpl 0x18(%rsp), %ecx
jne 0x31128
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