// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::RasterizeGridPoint(double, double, double, double, int)
void R3Grid::
RasterizeGridPoint(RNScalar x, RNScalar y, RNScalar z, RNScalar value, 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;

  // Trilinear interpolation
  int ix1 = (int) x;
  int iy1 = (int) y;
  int iz1 = (int) z;
  int ix2 = ix1 + 1;
  int iy2 = iy1 + 1;
  int iz2 = iz1 + 1;
  if (ix2 >= grid_resolution[0]) ix2 = ix1;
  if (iy2 >= grid_resolution[1]) iy2 = iy1;
  if (iz2 >= grid_resolution[2]) iz2 = iz1;
  RNScalar dx = x - ix1;
  RNScalar dy = y - iy1;
  RNScalar dz = z - iz1;
  RasterizeGridValue(ix1, iy1, iz1, value * (1.0-dx) * (1.0-dy) * (1.0-dz), operation);
  RasterizeGridValue(ix1, iy1, iz2, value * (1.0-dx) * (1.0-dy) * dz, operation);
  RasterizeGridValue(ix1, iy2, iz1, value * (1.0-dx) * dy * (1.0-dz), operation);
  RasterizeGridValue(ix1, iy2, iz2, value * (1.0-dx) * dy * dz, operation);
  RasterizeGridValue(ix2, iy1, iz1, value * dx * (1.0-dy) * (1.0-dz), operation);
  RasterizeGridValue(ix2, iy1, iz2, value * dx * (1.0-dy) * dz, operation);
  RasterizeGridValue(ix2, iy2, iz1, value * dx * dy * (1.0-dz), operation);
  RasterizeGridValue(ix2, iy2, iz2, value * dx * dy * dz, operation);
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x60(%rsp)
movsd %xmm0, 0x58(%rsp)
movsd %xmm1, 0x50(%rsp)
movsd %xmm2, 0x48(%rsp)
movsd %xmm3, 0x40(%rsp)
movl %esi, 0x3c(%rsp)
movq 0x60(%rsp), %rax
movq %rax, (%rsp)
xorps %xmm0, %xmm0
ucomisd 0x58(%rsp), %xmm0
ja 0x5f9a6
movq (%rsp), %rax
movsd 0x58(%rsp), %xmm0
movl 0x238(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5f9ab
jmp 0x5fcee
xorps %xmm0, %xmm0
ucomisd 0x50(%rsp), %xmm0
ja 0x5f9d3
movq (%rsp), %rax
movsd 0x50(%rsp), %xmm0
movl 0x23c(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5f9d8
jmp 0x5fcee
xorps %xmm0, %xmm0
ucomisd 0x48(%rsp), %xmm0
ja 0x5fa00
movq (%rsp), %rax
movsd 0x48(%rsp), %xmm0
movl 0x240(%rax), %eax
subl $0x1, %eax
cvtsi2sd %eax, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x5fa05
jmp 0x5fcee
movq (%rsp), %rcx
cvttsd2si 0x58(%rsp), %eax
movl %eax, 0x38(%rsp)
cvttsd2si 0x50(%rsp), %eax
movl %eax, 0x34(%rsp)
cvttsd2si 0x48(%rsp), %eax
movl %eax, 0x30(%rsp)
movl 0x38(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2c(%rsp)
movl 0x34(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x28(%rsp)
movl 0x30(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0x238(%rcx), %eax
jl 0x5fa5c
movl 0x38(%rsp), %eax
movl %eax, 0x2c(%rsp)
movq (%rsp), %rcx
movl 0x28(%rsp), %eax
cmpl 0x23c(%rcx), %eax
jl 0x5fa74
movl 0x34(%rsp), %eax
movl %eax, 0x28(%rsp)
movq (%rsp), %rcx
movl 0x24(%rsp), %eax
cmpl 0x240(%rcx), %eax
jl 0x5fa8c
movl 0x30(%rsp), %eax
movl %eax, 0x24(%rsp)
movq (%rsp), %rdi
movsd 0x58(%rsp), %xmm0
cvtsi2sdl 0x38(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x18(%rsp)
movsd 0x50(%rsp), %xmm0
cvtsi2sdl 0x34(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x10(%rsp)
movsd 0x48(%rsp), %xmm0
cvtsi2sdl 0x30(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x38(%rsp), %esi
movl 0x34(%rsp), %edx
movl 0x30(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
movsd 0xd351c(%rip), %xmm1    # 0x133008
subsd 0x18(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xd350a(%rip), %xmm1    # 0x133008
subsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xd34f8(%rip), %xmm1    # 0x133008
subsd 0x8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movl 0x3c(%rsp), %r8d
callq 0x5f820
movq (%rsp), %rdi
movl 0x38(%rsp), %esi
movl 0x34(%rsp), %edx
movl 0x24(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
movsd 0xd34c6(%rip), %xmm1    # 0x133008
subsd 0x18(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xd34b4(%rip), %xmm1    # 0x133008
subsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm0
mulsd 0x8(%rsp), %xmm0
movl 0x3c(%rsp), %r8d
callq 0x5f820
movq (%rsp), %rdi
movl 0x38(%rsp), %esi
movl 0x28(%rsp), %edx
movl 0x30(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
movsd 0xd347c(%rip), %xmm1    # 0x133008
subsd 0x18(%rsp), %xmm1
mulsd %xmm1, %xmm0
mulsd 0x10(%rsp), %xmm0
movsd 0xd3464(%rip), %xmm1    # 0x133008
subsd 0x8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movl 0x3c(%rsp), %r8d
callq 0x5f820
movq (%rsp), %rdi
movl 0x38(%rsp), %esi
movl 0x28(%rsp), %edx
movl 0x24(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
movsd 0xd3432(%rip), %xmm1    # 0x133008
subsd 0x18(%rsp), %xmm1
mulsd %xmm1, %xmm0
mulsd 0x10(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
movl 0x3c(%rsp), %r8d
callq 0x5f820
movq (%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x34(%rsp), %edx
movl 0x30(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
movsd 0xd33ee(%rip), %xmm1    # 0x133008
subsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm0
movsd 0xd33dc(%rip), %xmm1    # 0x133008
subsd 0x8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movl 0x3c(%rsp), %r8d
callq 0x5f820
movq (%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x34(%rsp), %edx
movl 0x24(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
movsd 0xd33a4(%rip), %xmm1    # 0x133008
subsd 0x10(%rsp), %xmm1
mulsd %xmm1, %xmm0
mulsd 0x8(%rsp), %xmm0
movl 0x3c(%rsp), %r8d
callq 0x5f820
movq (%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
movl 0x30(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
movsd 0xd3360(%rip), %xmm1    # 0x133008
subsd 0x8(%rsp), %xmm1
mulsd %xmm1, %xmm0
movl 0x3c(%rsp), %r8d
callq 0x5f820
movq (%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x28(%rsp), %edx
movl 0x24(%rsp), %ecx
movsd 0x40(%rsp), %xmm0
mulsd 0x18(%rsp), %xmm0
mulsd 0x10(%rsp), %xmm0
mulsd 0x8(%rsp), %xmm0
movl 0x3c(%rsp), %r8d
callq 0x5f820
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)