// File: /workspace/github_cpp_makefile_star2_O2/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 $0x78, %rsp
movsd %xmm2, 0x8(%rsp)
xorpd %xmm2, %xmm2
movsd %xmm0, 0x10(%rsp)
ucomisd %xmm0, %xmm2
xorpd %xmm0, %xmm0
jbe 0x2c037
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
ucomisd %xmm1, %xmm0
ja 0x2c028
movq %rdi, %rbx
movl 0x238(%rdi), %eax
leal -0x1(%rax), %ebp
cvtsi2sd %ebp, %xmm0
movsd 0x10(%rsp), %xmm2
ucomisd %xmm0, %xmm2
xorpd %xmm0, %xmm0
ja 0x2c028
movsd %xmm1, 0x38(%rsp)
ucomisd 0x8(%rsp), %xmm0
ja 0x2c028
movapd %xmm3, %xmm5
movl 0x23c(%rbx), %ecx
leal -0x1(%rcx), %edx
movl %edx, 0x4(%rsp)
cvtsi2sd %edx, %xmm0
movsd 0x38(%rsp), %xmm3
ucomisd %xmm0, %xmm3
ja 0x2c19a
movl 0x240(%rbx), %edx
leal -0x1(%rdx), %r12d
cvtsi2sd %r12d, %xmm0
movsd 0x8(%rsp), %xmm1
ucomisd %xmm0, %xmm1
xorpd %xmm0, %xmm0
ja 0x2c028
movsd 0x6711d(%rip), %xmm1    # 0x931d8
mulsd %xmm5, %xmm1
movsd 0x66f49(%rip), %xmm0    # 0x93010
addsd %xmm0, %xmm1
cvttpd2dq %xmm1, %xmm1
cvtdq2pd %xmm1, %xmm1
movsd 0x10(%rsp), %xmm4
movapd %xmm4, %xmm2
subsd %xmm1, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %r14d
movapd %xmm1, %xmm2
addsd %xmm4, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %esi
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %r10d
movapd %xmm1, %xmm2
addsd %xmm3, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %edi
movsd 0x8(%rsp), %xmm3
movapd %xmm3, %xmm2
subsd %xmm1, %xmm2
addsd %xmm0, %xmm2
cvttsd2si %xmm2, %r8d
addsd %xmm3, %xmm1
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %r9d
xorl %r15d, %r15d
testl %r14d, %r14d
cmovlel %r15d, %r14d
cmpl %esi, %eax
cmovgl %esi, %ebp
testl %r10d, %r10d
cmovlel %r15d, %r10d
movl %r10d, 0x18(%rsp)
cmpl %edi, %ecx
movl 0x4(%rsp), %eax
cmovgl %edi, %eax
movl %eax, 0x4(%rsp)
testl %r8d, %r8d
cmovgl %r8d, %r15d
cmpl %r9d, %edx
cmovgl %r9d, %r12d
leaq 0x6edec(%rip), %rax     # 0x9af68
movsd (%rax), %xmm0
xorpd %xmm2, %xmm2
xorpd %xmm1, %xmm1
movapd %xmm1, 0x40(%rsp)
ucomisd %xmm2, %xmm0
jb 0x2c1a3
sqrtsd %xmm0, %xmm0
jmp 0x2c1b4
xorpd %xmm0, %xmm0
jmp 0x2c028
movsd %xmm5, 0x20(%rsp)
callq 0xe350
movsd 0x20(%rsp), %xmm5
mulsd %xmm5, %xmm0
movapd %xmm0, %xmm1
mulsd %xmm0, %xmm1
mulsd %xmm0, %xmm1
movsd 0x66e3c(%rip), %xmm0    # 0x93008
divsd %xmm1, %xmm0
movsd %xmm0, 0x60(%rsp)
movapd %xmm5, %xmm0
addsd %xmm5, %xmm0
mulsd %xmm5, %xmm0
movsd %xmm0, 0x58(%rsp)
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movl %r12d, 0x1c(%rsp)
cmpl %r12d, %r15d
jg 0x2c2eb
cvtsi2sd %r15d, %xmm0
movsd 0x8(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd %xmm1, 0x68(%rsp)
movl 0x18(%rsp), %r13d
cmpl 0x4(%rsp), %r13d
jg 0x2c2de
cvtsi2sd %r13d, %xmm0
movsd 0x38(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd %xmm1, 0x70(%rsp)
movl %r14d, %r12d
cmpl %ebp, %r12d
jg 0x2c2d6
cvtsi2sd %r12d, %xmm1
movsd 0x10(%rsp), %xmm0
subsd %xmm1, %xmm0
mulsd %xmm0, %xmm0
addsd 0x70(%rsp), %xmm0
addsd 0x68(%rsp), %xmm0
xorpd 0x66dbb(%rip), %xmm0    # 0x93030
divsd 0x58(%rsp), %xmm0
callq 0xe820
mulsd 0x60(%rsp), %xmm0
movq 0x230(%rbx), %rax
movl 0x248(%rbx), %ecx
imull %r15d, %ecx
movl 0x244(%rbx), %edx
imull %r13d, %edx
addl %ecx, %edx
addl %r12d, %edx
movslq %edx, %rcx
movapd 0x20(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x20(%rsp)
mulsd (%rax,%rcx,8), %xmm0
movapd 0x40(%rsp), %xmm1
addsd %xmm0, %xmm1
movapd %xmm1, 0x40(%rsp)
incl %r12d
jmp 0x2c245
incl %r13d
jmp 0x2c21e
incl %r15d
movl 0x1c(%rsp), %r12d
jmp 0x2c1f7
leaq 0xae8b6(%rip), %rax     # 0xdaba8
movsd (%rax), %xmm0
movapd 0x66d32(%rip), %xmm1    # 0x93030
xorpd %xmm0, %xmm1
movapd 0x40(%rsp), %xmm4
movapd %xmm4, %xmm2
movapd 0x20(%rsp), %xmm3
divsd %xmm3, %xmm2
cmpnltsd %xmm3, %xmm0
cmpnltsd %xmm1, %xmm3
movapd %xmm3, %xmm1
andnpd %xmm2, %xmm1
andpd %xmm4, %xmm3
orpd %xmm1, %xmm3
andpd %xmm0, %xmm3
andnpd %xmm2, %xmm0
orpd %xmm0, %xmm3
movapd %xmm3, %xmm0
jmp 0x2c028
nop