// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::RasterizeGridPlane(gaps::R3Plane const&, double, int)
void R3Grid::
RasterizeGridPlane(const R3Plane& plane, RNScalar value, int operation)
{
  // Load info to pass to MarchingCubes
  R3Point corner_points[8];
  RNScalar corner_levels[8];
  R3Point triangle_points[3 * 8];
  R3Box grid_box = GridBox();
  for (int octant = 0; octant < 8; octant++) {
    corner_points[octant] = grid_box.Corner(octant);
    corner_levels[octant] = R3SignedDistance(plane, corner_points[octant]);
  }

  // Determine triangles 
  int npoints = MarchingCubes(corner_points, corner_levels, 0, triangle_points);

  // Rasterize triangles into temporary grid
  R3Grid tmp(grid_resolution[0], grid_resolution[1], grid_resolution[2]);
  for (int i = 0; i < npoints; i += 3) {
    tmp.RasterizeGridTriangle(triangle_points[i], triangle_points[i+1], triangle_points[i+2], 1);
  }
  
  // Add value to this grid where temporary grid is nonzero (avoids double counting at triangle boundaries)
  for (int iz = 0; iz < grid_resolution[2]; iz++) {
    for (int iy = 0; iy < grid_resolution[1]; iy++) {
      for (int ix = 0; ix < grid_resolution[0]; ix++) {
        if (tmp.GridValue(ix, iy, iz) > 0) {
          RasterizeGridValue(ix, iy, iz, value, operation);
        }
      }
    }
  }
}

// Assembly:
subq $0x688, %rsp            # imm = 0x688
movq %rdi, 0x680(%rsp)
movq %rsi, 0x678(%rsp)
movsd %xmm0, 0x670(%rsp)
movl %edx, 0x66c(%rsp)
movq 0x680(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0x5a0(%rsp), %rax
movq %rax, %rcx
addq $0xc0, %rcx
movq %rcx, 0x48(%rsp)
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x6eb40
movq 0x38(%rsp), %rax
movq 0x48(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x50(%rsp)
jne 0x61810
leaq 0x320(%rsp), %rax
movq %rax, %rcx
addq $0x240, %rcx            # imm = 0x240
movq %rcx, 0x28(%rsp)
movq %rax, 0x30(%rsp)
movq 0x30(%rsp), %rdi
movq %rdi, 0x20(%rsp)
callq 0x6eb40
movq 0x20(%rsp), %rax
movq 0x28(%rsp), %rcx
addq $0x18, %rax
cmpq %rcx, %rax
movq %rax, 0x30(%rsp)
jne 0x61853
movq 0x40(%rsp), %rsi
leaq 0x2e8(%rsp), %rdi
callq 0x6bde0
movl $0x0, 0x2e4(%rsp)
cmpl $0x8, 0x2e4(%rsp)
jge 0x61955
movl 0x2e4(%rsp), %edx
leaq 0x2c8(%rsp), %rdi
leaq 0x2e8(%rsp), %rsi
callq 0x42e40
jmp 0x618c3
movslq 0x2e4(%rsp), %rax
leaq (%rax,%rax,2), %rax
leaq 0x5a0(%rsp,%rax,8), %rdi
leaq 0x2c8(%rsp), %rsi
callq 0x6f1e0
jmp 0x618e6
movq 0x678(%rsp), %rdi
movslq 0x2e4(%rsp), %rax
leaq (%rax,%rax,2), %rax
leaq 0x5a0(%rsp,%rax,8), %rsi
callq 0x86f90
movsd %xmm0, 0x18(%rsp)
jmp 0x6190f
movsd 0x18(%rsp), %xmm0
movslq 0x2e4(%rsp), %rax
movsd %xmm0, 0x560(%rsp,%rax,8)
movl 0x2e4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x2e4(%rsp)
jmp 0x61897
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c0(%rsp)
movl %eax, 0x2bc(%rsp)
jmp 0x61b3f
leaq 0x5a0(%rsp), %rdi
leaq 0x560(%rsp), %rsi
xorps %xmm0, %xmm0
leaq 0x320(%rsp), %rdx
callq 0x61b60
movl %eax, 0x14(%rsp)
jmp 0x6197b
movq 0x40(%rsp), %rax
movl 0x14(%rsp), %ecx
movl %ecx, 0x2b8(%rsp)
movl 0x238(%rax), %esi
movl 0x23c(%rax), %edx
movl 0x240(%rax), %ecx
leaq 0x68(%rsp), %rdi
callq 0x55680
jmp 0x619a9
movl $0x0, 0x64(%rsp)
movl 0x64(%rsp), %eax
cmpl 0x2b8(%rsp), %eax
jge 0x61a42
movslq 0x64(%rsp), %rcx
movl %ecx, %eax
leaq (%rcx,%rcx,2), %rcx
leaq 0x320(%rsp,%rcx,8), %rsi
movl %eax, %ecx
incl %ecx
movslq %ecx, %rcx
leaq (%rcx,%rcx,2), %rcx
leaq 0x320(%rsp,%rcx,8), %rdx
addl $0x2, %eax
cltq
leaq (%rax,%rax,2), %rax
leaq 0x320(%rsp,%rax,8), %rcx
leaq 0x68(%rsp), %rdi
movsd 0xd1602(%rip), %xmm0    # 0x133008
xorl %r8d, %r8d
callq 0x13b30
jmp 0x61a10
jmp 0x61a12
movl 0x64(%rsp), %eax
addl $0x3, %eax
movl %eax, 0x64(%rsp)
jmp 0x619b1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x2c0(%rsp)
movl %eax, 0x2bc(%rsp)
leaq 0x68(%rsp), %rdi
callq 0x56960
jmp 0x61b3f
movl $0x0, 0x60(%rsp)
movq 0x40(%rsp), %rcx
movl 0x60(%rsp), %eax
cmpl 0x240(%rcx), %eax
jge 0x61b20
movl $0x0, 0x5c(%rsp)
movq 0x40(%rsp), %rcx
movl 0x5c(%rsp), %eax
cmpl 0x23c(%rcx), %eax
jge 0x61b0e
movl $0x0, 0x58(%rsp)
movq 0x40(%rsp), %rcx
movl 0x58(%rsp), %eax
cmpl 0x238(%rcx), %eax
jge 0x61afc
movl 0x58(%rsp), %esi
movl 0x5c(%rsp), %edx
movl 0x60(%rsp), %ecx
leaq 0x68(%rsp), %rdi
callq 0x6bad0
movsd %xmm0, 0x8(%rsp)
jmp 0x61ab3
movsd 0x8(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jbe 0x61aed
movq 0x40(%rsp), %rdi
movl 0x58(%rsp), %esi
movl 0x5c(%rsp), %edx
movl 0x60(%rsp), %ecx
movsd 0x670(%rsp), %xmm0
movl 0x66c(%rsp), %r8d
callq 0x5f820
jmp 0x61aeb
jmp 0x61aed
jmp 0x61aef
movl 0x58(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x58(%rsp)
jmp 0x61a84
jmp 0x61afe
movl 0x5c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x5c(%rsp)
jmp 0x61a67
jmp 0x61b10
movl 0x60(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x60(%rsp)
jmp 0x61a4a
leaq 0x68(%rsp), %rdi
callq 0x56960
leaq 0x2e8(%rsp), %rdi
callq 0x36a80
addq $0x688, %rsp            # imm = 0x688
retq
leaq 0x2e8(%rsp), %rdi
callq 0x36a80
movq 0x2c0(%rsp), %rdi
callq 0xf890
nopl (%rax)