// File: /workspace/github_cpp_makefile_star2_O2/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 0xad8aa(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm2
movapd 0x65d26(%rip), %xmm1    # 0x93030
xorpd %xmm2, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2d329
ucomisd %xmm0, %xmm2
jb 0x2d329
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movsd 0x65ea7(%rip), %xmm1    # 0x931d8
movsd %xmm0, 0x8(%rsp)
mulsd %xmm0, %xmm1
addsd 0x65ccd(%rip), %xmm1    # 0x93010
cvttsd2si %xmm1, %ebp
movslq %ebp, %r15
leaq 0x8(,%r15,8), %rdi
cmpl $-0x1, %r15d
pushq $-0x1
popq %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, %rbx
leaq 0x6dbc2(%rip), %rax     # 0x9af68
movsd (%rax), %xmm0
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0x2d3ba
sqrtsd %xmm0, %xmm0
jmp 0x2d3bf
callq 0xe350
movsd 0x8(%rsp), %xmm2
mulsd %xmm2, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x65c2b(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movsd %xmm0, 0x30(%rsp)
movapd %xmm2, %xmm0
addsd %xmm2, %xmm0
mulsd %xmm2, %xmm0
movsd %xmm0, 0x8(%rsp)
xorl %r12d, %r12d
cmpq %r15, %r12
jg 0x2d42a
movl %r12d, %eax
imull %r12d, %eax
negl %eax
cvtsi2sd %eax, %xmm0
divsd 0x8(%rsp), %xmm0
callq 0xe820
mulsd 0x30(%rsp), %xmm0
movsd %xmm0, (%r14,%r12,8)
incq %r12
jmp 0x2d3fc
movq 0x10(%rsp), %rax
movl 0x23c(%rax), %ecx
movl 0x240(%rax), %eax
xorl %edx, %edx
testl %ecx, %ecx
cmovlel %edx, %ecx
movl %ecx, 0x4(%rsp)
testl %eax, %eax
movl $0x0, %ecx
movl %eax, 0x30(%rsp)
cmovgl %eax, %ecx
movl %ecx, 0x1c(%rsp)
leaq 0x8(%r14), %rsi
leaq -0x8(%rbx), %rax
movq %rax, 0x20(%rsp)
xorl %r8d, %r8d
movq 0x10(%rsp), %rax
movl 0x238(%rax), %r12d
testl %r12d, %r12d
movl $0x0, %ecx
cmovgl %r12d, %ecx
cmpl 0x1c(%rsp), %r8d
je 0x2d5c7
movl %ecx, %edi
xorl %r11d, %r11d
movl %r8d, (%rsp)
cmpl 0x4(%rsp), %r11d
je 0x2d5bf
movq 0x10(%rsp), %rax
movq 0x230(%rax), %rcx
movl 0x248(%rax), %edx
imull %r8d, %edx
movl 0x244(%rax), %r9d
imull %r11d, %r9d
leal (%rdx,%r9), %eax
xorl %r8d, %r8d
cmpq %r8, %rdi
je 0x2d4e4
leal (%rax,%r8), %r10d
movslq %r10d, %r10
movsd (%rcx,%r10,8), %xmm0
movsd %xmm0, (%rbx,%r8,8)
incq %r8
jmp 0x2d4c7
movl %r11d, 0x8(%rsp)
movq %rbx, %r15
movq 0x20(%rsp), %rax
xorl %r13d, %r13d
cmpl %r13d, %ebp
movl %r13d, %r11d
cmovll %ebp, %r11d
testl %r11d, %r11d
movl $0x0, %r8d
cmovlel %r8d, %r11d
cmpq %rdi, %r13
je 0x2d5ae
negq %r11
movsd (%r14), %xmm1
movsd (%rbx,%r13,8), %xmm0
mulsd %xmm1, %xmm0
movq %rsi, %r8
xorl %r10d, %r10d
cmpq %r10, %r11
je 0x2d551
movsd (%r8), %xmm2
movsd (%rax,%r10,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
decq %r10
addq $0x8, %r8
jmp 0x2d52c
movl %r13d, %r8d
notl %r8d
addl %r12d, %r8d
cmpl %ebp, %r8d
cmovgel %ebp, %r8d
movslq %r8d, %r8
pushq $0x1
popq %r10
cmpq %r8, %r10
jg 0x2d58a
movsd (%r14,%r10,8), %xmm2
movsd (%r15,%r10,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %r10
jmp 0x2d568
divsd %xmm1, %xmm0
leal (%rdx,%r13), %r8d
addl %r9d, %r8d
movslq %r8d, %r8
movsd %xmm0, (%rcx,%r8,8)
incq %r13
addq $0x8, %rax
addq $0x8, %r15
jmp 0x2d4f4
movl 0x8(%rsp), %r11d
incl %r11d
movl (%rsp), %r8d
jmp 0x2d494
incl %r8d
jmp 0x2d468
leaq 0x8(%r14), %rax
movq %rax, 0x8(%rsp)
leaq -0x8(%rbx), %rax
movq %rax, 0x20(%rsp)
xorl %edx, %edx
movl %ecx, 0x28(%rsp)
movq 0x10(%rsp), %rax
movl 0x23c(%rax), %r9d
cmpl 0x1c(%rsp), %edx
je 0x2d731
xorl %esi, %esi
testl %r9d, %r9d
movl $0x0, %r13d
cmovgl %r9d, %r13d
movl %edx, (%rsp)
cmpl %ecx, %esi
je 0x2d72a
movq 0x10(%rsp), %rax
movq 0x230(%rax), %rcx
movl 0x248(%rax), %r8d
imull %edx, %r8d
movl 0x244(%rax), %r11d
addl %esi, %r8d
movl %r8d, %eax
xorl %edx, %edx
cmpq %rdx, %r13
je 0x2d64e
cltq
movsd (%rcx,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rdx,8)
incq %rdx
addl %r11d, %eax
jmp 0x2d635
movl %esi, 0x4(%rsp)
movq %rbx, %r15
movq 0x20(%rsp), %rax
xorl %r12d, %r12d
cmpl %r12d, %ebp
movl %r12d, %edi
cmovll %ebp, %edi
testl %edi, %edi
movl $0x0, %edx
cmovlel %edx, %edi
cmpq %r13, %r12
je 0x2d718
movl %r9d, %r10d
negq %rdi
movsd (%r14), %xmm1
movsd (%rbx,%r12,8), %xmm0
mulsd %xmm1, %xmm0
movq 0x8(%rsp), %rsi
xorl %r9d, %r9d
cmpq %r9, %rdi
je 0x2d6ba
movsd (%rsi), %xmm2
movsd (%rax,%r9,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
decq %r9
addq $0x8, %rsi
jmp 0x2d696
movl %r12d, %edx
notl %edx
movl %r10d, %r9d
addl %r10d, %edx
cmpl %ebp, %edx
cmovgel %ebp, %edx
movslq %edx, %rdx
pushq $0x1
popq %rsi
cmpq %rdx, %rsi
jg 0x2d6f2
movsd (%r14,%rsi,8), %xmm2
movsd (%r15,%rsi,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %rsi
jmp 0x2d6d0
divsd %xmm1, %xmm0
movl %r11d, %edx
imull %r12d, %edx
addl %r8d, %edx
movslq %edx, %rdx
movsd %xmm0, (%rcx,%rdx,8)
incq %r12
addq $0x8, %rax
addq $0x8, %r15
jmp 0x2d65d
movl 0x4(%rsp), %esi
incl %esi
movl 0x28(%rsp), %ecx
movl (%rsp), %edx
jmp 0x2d607
incl %edx
jmp 0x2d5df
movq 0x10(%rsp), %rax
movl 0x238(%rax), %eax
xorl %ecx, %ecx
testl %eax, %eax
cmovlel %ecx, %eax
movl %eax, 0x8(%rsp)
testl %r9d, %r9d
cmovlel %ecx, %r9d
movl 0x1c(%rsp), %ecx
movq %r14, %r10
addq $0x8, %r10
movq %rbx, %rax
addq $-0x8, %rax
movq %rax, 0x28(%rsp)
xorl %edx, %edx
movl %r9d, (%rsp)
cmpl %r9d, %edx
je 0x2d895
xorl %r12d, %r12d
movl %edx, 0x4(%rsp)
cmpl 0x8(%rsp), %r12d
je 0x2d88e
movq 0x10(%rsp), %rax
movq 0x230(%rax), %r13
movl 0x248(%rax), %esi
movl 0x244(%rax), %r9d
imull %edx, %r9d
leal (%r12,%r9), %eax
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x2d7c3
cltq
movsd (%r13,%rax,8), %xmm0
movsd %xmm0, (%rbx,%rdx,8)
incq %rdx
addl %esi, %eax
jmp 0x2d7a9
movq %rbx, %r15
movq 0x28(%rsp), %r11
xorl %edi, %edi
cmpl %edi, %ebp
movl %edi, %eax
cmovll %ebp, %eax
testl %eax, %eax
movl $0x0, %edx
cmovlel %edx, %eax
cmpq %rcx, %rdi
je 0x2d87e
negq %rax
movsd (%r14), %xmm1
movsd (%rbx,%rdi,8), %xmm0
mulsd %xmm1, %xmm0
movq %r10, %r8
xorl %edx, %edx
cmpq %rdx, %rax
je 0x2d822
movsd (%r8), %xmm2
movsd (%r11,%rdx,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
decq %rdx
addq $0x8, %r8
jmp 0x2d7fd
movl %edi, %eax
notl %eax
addl 0x30(%rsp), %eax
cmpl %ebp, %eax
cmovgel %ebp, %eax
cltq
pushq $0x1
popq %rdx
cmpq %rax, %rdx
jg 0x2d856
movsd (%r14,%rdx,8), %xmm2
movsd (%r15,%rdx,8), %xmm3
mulsd %xmm2, %xmm3
addsd %xmm3, %xmm0
addsd %xmm2, %xmm1
incq %rdx
jmp 0x2d834
divsd %xmm1, %xmm0
movl %esi, %eax
imull %edi, %eax
addl %r12d, %eax
addl %r9d, %eax
cltq
movsd %xmm0, (%r13,%rax,8)
incq %rdi
addq $0x8, %r11
addq $0x8, %r15
jmp 0x2d7cd
incl %r12d
movl (%rsp), %r9d
movl 0x4(%rsp), %edx
jmp 0x2d77b
incl %edx
jmp 0x2d76b
movq %r14, %rdi
callq 0xe5d0
movq %rbx, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xe5d0
nop