// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::RasterizeGridPoint(double, double, double, double, double, int)
void R3Grid::
RasterizeGridPoint(RNScalar x, RNScalar y, RNScalar z, RNScalar value, RNLength sigma, int operation)
{
  // Check if within bounds
  if ((x < 0) || (x > grid_resolution[0]-1)) return;
  if ((y < 0) || (y > grid_resolution[1]-1)) return;
  if ((z < 0) || (z > grid_resolution[2]-1)) return;

  // Determine 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
  double a = sqrt(2.0 * RN_PI) * sigma;
  double fac = 1.0 / (a * a * a);
  double denom = 2.0 * sigma * sigma;

  // Compute total weight of Gaussian filter
  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 = sqrt(dx*dx + dy*dy + dz*dz);
        RNScalar w = fac * exp(-d * d / denom);
        value += w * GridValue(i, j, k);
        weight += w;
      }
    }
  }

  // Splat values into grid (scaled by total weight of Gaussian filter)
  if (RNIsPositive(weight)) {
    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 = sqrt(dx*dx + dy*dy + dz*dz);
          RNScalar w = fac * exp(-d * d / denom);
          RasterizeGridValue(i, j, k, w * value / weight, operation);
        }
      }
    }
  }
}

// Assembly:
subq $0x118, %rsp            # imm = 0x118
movq %rdi, 0x110(%rsp)
movsd %xmm0, 0x108(%rsp)
movsd %xmm1, 0x100(%rsp)
movsd %xmm2, 0xf8(%rsp)
movsd %xmm3, 0xf0(%rsp)
movsd %xmm4, 0xe8(%rsp)
movl %esi, 0xe4(%rsp)
movq 0x110(%rsp), %rax
movq %rax, 0x30(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x108(%rsp), %xmm0
ja 0x5fd7f
movq 0x30(%rsp), %rax
movsd 0x108(%rsp), %xmm0
movl 0x238(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5fd84
jmp 0x60462
xorps %xmm0, %xmm0
ucomisd 0x100(%rsp), %xmm0
ja 0x5fdb3
movq 0x30(%rsp), %rax
movsd 0x100(%rsp), %xmm0
movl 0x23c(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5fdb8
jmp 0x60462
xorps %xmm0, %xmm0
ucomisd 0xf8(%rsp), %xmm0
ja 0x5fde7
movq 0x30(%rsp), %rax
movsd 0xf8(%rsp), %xmm0
movl 0x240(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5fdec
jmp 0x60462
movsd 0xe8(%rsp), %xmm0
movsd 0xd33e3(%rip), %xmm1    # 0x1331e0
mulsd %xmm1, %xmm0
movsd 0xd3207(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xe0(%rsp)
movsd 0x108(%rsp), %xmm0
cvtsi2sdl 0xe0(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0xd31da(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xdc(%rsp)
movsd 0x108(%rsp), %xmm0
cvtsi2sdl 0xe0(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd 0xd31ad(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xd8(%rsp)
movsd 0x100(%rsp), %xmm0
cvtsi2sdl 0xe0(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0xd3180(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xd4(%rsp)
movsd 0x100(%rsp), %xmm0
cvtsi2sdl 0xe0(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd 0xd3153(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xd0(%rsp)
movsd 0xf8(%rsp), %xmm0
cvtsi2sdl 0xe0(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd 0xd3126(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xcc(%rsp)
movsd 0xf8(%rsp), %xmm0
cvtsi2sdl 0xe0(%rsp), %xmm1
addsd %xmm1, %xmm0
movsd 0xd30f9(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xc8(%rsp)
cmpl $0x0, 0xdc(%rsp)
jge 0x5ff3b
movl $0x0, 0xdc(%rsp)
movq 0x30(%rsp), %rdi
movl 0xd8(%rsp), %eax
movl %eax, 0x2c(%rsp)
callq 0x138c0
movl %eax, %ecx
movl 0x2c(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x5ff71
movq 0x30(%rsp), %rdi
callq 0x138c0
subl $0x1, %eax
movl %eax, 0xd8(%rsp)
cmpl $0x0, 0xd4(%rsp)
jge 0x5ff86
movl $0x0, 0xd4(%rsp)
movq 0x30(%rsp), %rdi
movl 0xd0(%rsp), %eax
movl %eax, 0x28(%rsp)
callq 0x138e0
movl %eax, %ecx
movl 0x28(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x5ffbc
movq 0x30(%rsp), %rdi
callq 0x138e0
subl $0x1, %eax
movl %eax, 0xd0(%rsp)
cmpl $0x0, 0xcc(%rsp)
jge 0x5ffd1
movl $0x0, 0xcc(%rsp)
movq 0x30(%rsp), %rdi
movl 0xc8(%rsp), %eax
movl %eax, 0x24(%rsp)
callq 0x13900
movl %eax, %ecx
movl 0x24(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x60007
movq 0x30(%rsp), %rdi
callq 0x13900
subl $0x1, %eax
movl %eax, 0xc8(%rsp)
movsd 0xd31f1(%rip), %xmm0    # 0x133200
leaq 0xd68c2(%rip), %rax     # 0x1368d8
mulsd (%rax), %xmm0
callq 0xf390
mulsd 0xe8(%rsp), %xmm0
movsd %xmm0, 0xc0(%rsp)
movsd 0xc0(%rsp), %xmm1
mulsd 0xc0(%rsp), %xmm1
mulsd 0xc0(%rsp), %xmm1
movsd 0xd2fb4(%rip), %xmm0    # 0x133008
divsd %xmm1, %xmm0
movsd %xmm0, 0xb8(%rsp)
movsd 0xd3197(%rip), %xmm0    # 0x133200
mulsd 0xe8(%rsp), %xmm0
mulsd 0xe8(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
xorps %xmm0, %xmm0
movsd %xmm0, 0xa8(%rsp)
movl 0xcc(%rsp), %eax
movl %eax, 0xa4(%rsp)
movl 0xa4(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jg 0x602a3
movl 0xd4(%rsp), %eax
movl %eax, 0xa0(%rsp)
movl 0xa0(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jg 0x6028b
movl 0xdc(%rsp), %eax
movl %eax, 0x9c(%rsp)
movl 0x9c(%rsp), %eax
cmpl 0xd8(%rsp), %eax
jg 0x60273
movsd 0x108(%rsp), %xmm0
cvtsi2sdl 0x9c(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x90(%rsp)
movsd 0x100(%rsp), %xmm0
cvtsi2sdl 0xa0(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x88(%rsp)
movsd 0xf8(%rsp), %xmm0
cvtsi2sdl 0xa4(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
movsd 0x90(%rsp), %xmm1
movsd 0x90(%rsp), %xmm2
movsd 0x88(%rsp), %xmm0
mulsd 0x88(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x80(%rsp), %xmm0
movsd 0x80(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0xf390
movsd %xmm0, 0x78(%rsp)
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x78(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd 0x78(%rsp), %xmm0
divsd 0xb0(%rsp), %xmm0
callq 0xf8c0
movq 0x30(%rsp), %rdi
movaps %xmm0, %xmm1
movsd 0x10(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movl 0x9c(%rsp), %esi
movl 0xa0(%rsp), %edx
movl 0xa4(%rsp), %ecx
callq 0x6bad0
movaps %xmm0, %xmm2
movsd 0x18(%rsp), %xmm0
movsd 0xf0(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0xf0(%rsp)
movsd 0x70(%rsp), %xmm0
addsd 0xa8(%rsp), %xmm0
movsd %xmm0, 0xa8(%rsp)
movl 0x9c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x9c(%rsp)
jmp 0x600e2
jmp 0x60275
movl 0xa0(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa0(%rsp)
jmp 0x600c0
jmp 0x6028d
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x6009e
movsd 0xa8(%rsp), %xmm0
leaq 0x11822d(%rip), %rax    # 0x1784e0
movsd (%rax), %xmm1
callq 0x36d50
cmpl $0x0, %eax
je 0x60462
movl 0xcc(%rsp), %eax
movl %eax, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
cmpl 0xc8(%rsp), %eax
jg 0x60460
movl 0xd4(%rsp), %eax
movl %eax, 0x68(%rsp)
movl 0x68(%rsp), %eax
cmpl 0xd0(%rsp), %eax
jg 0x6044e
movl 0xdc(%rsp), %eax
movl %eax, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0xd8(%rsp), %eax
jg 0x6043c
movsd 0x108(%rsp), %xmm0
cvtsi2sdl 0x64(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
movsd 0x100(%rsp), %xmm0
cvtsi2sdl 0x68(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x50(%rsp)
movsd 0xf8(%rsp), %xmm0
cvtsi2sdl 0x6c(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x58(%rsp), %xmm1
movsd 0x58(%rsp), %xmm2
movsd 0x50(%rsp), %xmm0
mulsd 0x50(%rsp), %xmm0
mulsd %xmm2, %xmm1
addsd %xmm0, %xmm1
movsd 0x48(%rsp), %xmm0
movsd 0x48(%rsp), %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
callq 0xf390
movsd %xmm0, 0x40(%rsp)
movsd 0xb8(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movsd 0x40(%rsp), %xmm0
movq %xmm0, %rax
movabsq $-0x8000000000000000, %rcx # imm = 0x8000000000000000
xorq %rcx, %rax
movq %rax, %xmm0
mulsd 0x40(%rsp), %xmm0
divsd 0xb0(%rsp), %xmm0
callq 0xf8c0
movq 0x30(%rsp), %rdi
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
mulsd %xmm1, %xmm0
movsd %xmm0, 0x38(%rsp)
movl 0x64(%rsp), %esi
movl 0x68(%rsp), %edx
movl 0x6c(%rsp), %ecx
movsd 0x38(%rsp), %xmm0
mulsd 0xf0(%rsp), %xmm0
divsd 0xa8(%rsp), %xmm0
movl 0xe4(%rsp), %r8d
callq 0x5f820
movl 0x64(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x64(%rsp)
jmp 0x60308
jmp 0x6043e
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0x602ec
jmp 0x60450
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0x602d0
jmp 0x60462
addq $0x118, %rsp            # imm = 0x118
retq
nopw (%rax,%rax)