// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::GridValue(double, double, double, double) const
RNScalar R3Grid::
GridValue(RNScalar x, RNScalar y, RNScalar z, RNLength sigma) const
{
  // Check if within bounds
  if ((x < 0) || (x > grid_resolution[0]-1)) return 0.0;
  if ((y < 0) || (y > grid_resolution[1]-1)) return 0.0;
  if ((z < 0) || (z > grid_resolution[2]-1)) return 0.0;

  // Determine Gaussian filter extent
  int f = (int) (3 * sigma + 0.5);
  int xmin = (int) (x - f + 0.5);
  int xmax = (int) (x + f + 0.5);
  int ymin = (int) (y - f + 0.5);
  int ymax = (int) (y + f + 0.5);
  int zmin = (int) (z - f + 0.5);
  int zmax = (int) (z + f + 0.5);
  if (xmin < 0) xmin = 0;
  if (xmax > XResolution()-1) xmax = XResolution()-1;
  if (ymin < 0) ymin = 0;
  if (ymax > YResolution()-1) ymax = YResolution()-1;
  if (zmin < 0) zmin = 0;
  if (zmax > ZResolution()-1) zmax = ZResolution()-1;

  // Compute Gaussian weighting variables
  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;

  // Filter sample with Gaussian
  RNScalar value = 0.0;
  RNScalar weight = 0.0;
  for (int k = zmin; k <= zmax; k++) {
    for (int j = ymin; j <= ymax; j++) {
      for (int i = xmin; i <= xmax; i++) {
        RNScalar dx = x - i;
        RNScalar dy = y - j;
        RNScalar dz = z - k;
        RNScalar d = dx*dx + dy*dy + dz*dz;
        RNScalar w = fac * exp(-d / denom);
        value += w * GridValue(i, j, k);
        weight += w;
      }
    }
  }

  // Normalize value based on total weight of Gaussian samples
  if (RNIsNotZero(weight)) value /= weight;

  // Return value
  return value;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movapd %xmm1, 0x20(%rsp)
xorpd %xmm8, %xmm8
ucomisd %xmm0, %xmm8
jbe 0x2f2f8
movapd %xmm8, %xmm0
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
ucomisd 0x20(%rsp), %xmm8
ja 0x2f2e1
movapd %xmm0, %xmm5
movq %rdi, %rbx
movl 0x238(%rdi), %r15d
leal -0x1(%r15), %ebp
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
ucomisd %xmm0, %xmm5
ja 0x2f2e1
ucomisd %xmm2, %xmm8
ja 0x2f2e1
movl 0x23c(%rbx), %r13d
leal -0x1(%r13), %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movapd 0x20(%rsp), %xmm1
ucomisd %xmm0, %xmm1
ja 0x2f2e1
movl 0x240(%rbx), %eax
leal -0x1(%rax), %esi
xorps %xmm0, %xmm0
cvtsi2sd %esi, %xmm0
ucomisd %xmm0, %xmm2
ja 0x2f2e1
movsd 0x72e74(%rip), %xmm0    # 0xa21d8
mulsd %xmm3, %xmm0
movsd 0x72cc0(%rip), %xmm1    # 0xa2030
addsd %xmm1, %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm9
movapd %xmm2, %xmm0
subsd %xmm9, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movapd %xmm9, %xmm0
addsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %edx
xorl %r14d, %r14d
testl %ecx, %ecx
cmovgl %ecx, %r14d
cmpl %edx, %eax
cmovgl %edx, %esi
leaq 0x7ac34(%rip), %rax     # 0xa9fe8
movsd (%rax), %xmm0
xorpd %xmm4, %xmm4
ucomisd %xmm4, %xmm0
movl %esi, 0x10(%rsp)
movsd %xmm2, 0x50(%rsp)
movapd %xmm5, 0x70(%rsp)
jb 0x2f3d8
sqrtsd %xmm0, %xmm0
jmp 0x2f405
movsd %xmm3, 0x40(%rsp)
movapd %xmm9, 0x30(%rsp)
callq 0xe340
movapd 0x30(%rsp), %xmm9
movsd 0x40(%rsp), %xmm3
xorpd %xmm4, %xmm4
movl 0x10(%rsp), %esi
movapd 0x70(%rsp), %xmm5
xorpd %xmm8, %xmm8
cmpl %esi, %r14d
jg 0x2f626
movapd %xmm5, %xmm2
unpcklpd 0x20(%rsp), %xmm2       # xmm2 = xmm2[0],mem[0]
unpcklpd %xmm9, %xmm9            # xmm9 = xmm9[0,0]
movapd %xmm2, %xmm1
subpd %xmm9, %xmm1
addpd 0x72bdd(%rip), %xmm1    # 0xa2010
cvttpd2dq %xmm1, %xmm6
xorpd %xmm4, %xmm4
movapd %xmm6, %xmm1
pcmpgtd %xmm4, %xmm1
pand %xmm6, %xmm1
mulsd %xmm3, %xmm0
movapd %xmm0, %xmm4
mulsd %xmm0, %xmm4
mulsd %xmm0, %xmm4
movsd 0x72bd9(%rip), %xmm6    # 0xa2038
divsd %xmm4, %xmm6
movapd %xmm3, %xmm7
addsd %xmm3, %xmm7
mulsd %xmm3, %xmm7
addpd %xmm2, %xmm9
addpd 0x72b93(%rip), %xmm9    # 0xa2010
cvttpd2dq %xmm9, %xmm2
movd %r13d, %xmm3
movd %r15d, %xmm0
punpckldq %xmm3, %xmm0    # xmm0 = xmm0[0],xmm3[0],xmm0[1],xmm3[1]
pcmpgtd %xmm2, %xmm0
movd %r12d, %xmm3
movd %ebp, %xmm4
punpckldq %xmm3, %xmm4    # xmm4 = xmm4[0],xmm3[0],xmm4[1],xmm3[1]
pand %xmm0, %xmm2
pandn %xmm4, %xmm0
por %xmm2, %xmm0
pshufd $0x55, %xmm1, %xmm2     # xmm2 = xmm1[1,1,1,1]
movd %xmm1, 0x18(%rsp)
pcmpgtd %xmm0, %xmm1
pshufd $0x50, %xmm1, %xmm1     # xmm1 = xmm1[0,0,1,1]
movmskpd %xmm1, %eax
movl %eax, 0x1c(%rsp)
shrb %al
movb %al, 0xf(%rsp)
movd %xmm2, 0x14(%rsp)
pshufd $0x55, %xmm0, %xmm1     # xmm1 = xmm0[1,1,1,1]
movd %xmm1, %ebp
movd %xmm0, %r13d
incl %r13d
movapd 0x72b32(%rip), %xmm2    # 0xa2020
pxor %xmm4, %xmm4
movsd %xmm6, 0x60(%rsp)
movsd %xmm7, 0x58(%rsp)
cmpb $0x0, 0xf(%rsp)
jne 0x2f614
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd 0x50(%rsp), %xmm3
subsd %xmm0, %xmm3
movl 0x14(%rsp), %eax
movl %eax, %r15d
movsd %xmm3, 0x68(%rsp)
testb $0x1, 0x1c(%rsp)
jne 0x2f604
xorps %xmm0, %xmm0
cvtsi2sd %r15d, %xmm0
movapd 0x20(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movapd %xmm1, 0x80(%rsp)
movl 0x18(%rsp), %r12d
movdqa %xmm4, 0x30(%rsp)
movapd %xmm8, 0x40(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
subsd %xmm0, %xmm5
mulsd %xmm5, %xmm5
addsd 0x80(%rsp), %xmm5
movapd %xmm3, %xmm0
mulsd %xmm3, %xmm0
addsd %xmm5, %xmm0
xorpd %xmm2, %xmm0
divsd %xmm7, %xmm0
callq 0xe7f0
movsd 0x68(%rsp), %xmm3
movapd 0x72a7c(%rip), %xmm2    # 0xa2020
movapd 0x30(%rsp), %xmm4
movsd 0x58(%rsp), %xmm7
movsd 0x60(%rsp), %xmm6
movapd 0x40(%rsp), %xmm8
movapd 0x70(%rsp), %xmm5
mulsd %xmm6, %xmm0
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull %r14d, %ecx
movl 0x244(%rbx), %edx
imull %r15d, %edx
addl %ecx, %edx
addl %r12d, %edx
movslq %edx, %rcx
addsd %xmm0, %xmm8
mulsd (%rax,%rcx,8), %xmm0
addsd %xmm0, %xmm4
incl %r12d
cmpl %r12d, %r13d
jne 0x2f557
leal 0x1(%r15), %eax
cmpl %ebp, %r15d
movl %eax, %r15d
jne 0x2f528
leal 0x1(%r14), %eax
cmpl 0x10(%rsp), %r14d
movl %eax, %r14d
jne 0x2f4fe
leaq 0xb656b(%rip), %rax     # 0xe5b98
movsd (%rax), %xmm0
movapd 0x729e7(%rip), %xmm1    # 0xa2020
xorpd %xmm0, %xmm1
movapd %xmm4, %xmm2
divsd %xmm8, %xmm2
cmpnltsd %xmm8, %xmm0
cmpnltsd %xmm1, %xmm8
movapd %xmm8, %xmm1
andnpd %xmm2, %xmm1
andpd %xmm4, %xmm8
orpd %xmm1, %xmm8
andpd %xmm0, %xmm8
andnpd %xmm2, %xmm0
orpd %xmm0, %xmm8
jmp 0x2f2e1