// File: tomfunkhouser[P]gaps/pkgs/R3Shapes/R3Grid.cpp
// Function: gaps::R3Grid::RasterizeGridSphere(gaps::R3Point const&, double, double, int, int)
void R3Grid::
RasterizeGridSphere(const R3Point& center, RNLength radius, RNScalar value, RNBoolean solid, int operation)
{
  // Figure out the min and max in each dimension
  int mn[3], mx[3];
  for (int i = 0; i < 3; i++) {
    mx[i]= (int) (center[i]+radius);
    if (mx[i] < 0) return;
    if (mx[i] > Resolution(i)-1) mx[i] = Resolution(i)-1;
    mn[i]= (int) (center[i]-radius);
    if (mn[i] > Resolution(i)-1) return;
    if (mn[i] < 0) mn[i] = 0;
  }

  // Rasterize sphere
  RNScalar radius_squared = radius * radius;
  for (int k = mn[2]; k <= mx[2]; k++) {
    RNCoord z = (int) (k - center[2]);
    RNCoord z_squared = z*z;
    RNLength xy_radius_squared = radius_squared - z_squared;
    RNLength y = sqrt(xy_radius_squared);
    int y1 = (int) (center[1] - y + 0.5);
    int y2 = (int) (center[1] + y + 0.5);
    if (y1 < mn[1]) y1 = mn[1];
    if (y2 > mx[1]) y2 = mx[1];
    for (int j = y1; j <= y2; j++) {
      RNCoord y = (int) (j - center[1]);
      RNCoord y_squared = y*y;
      RNLength x_squared = xy_radius_squared - y_squared;
      RNLength x = sqrt(x_squared);
      int x1 = (int) (center[0] - x + 0.5);
      int x2 = (int) (center[0] + x + 0.5);
      if (x1 < mn[0]) x1 = mn[0];
      if (x2 > mx[0]) x2 = mx[0];
      if (solid || (j == y1) || (j == y2)) {
        for (int i = x1; i <= x2; i++) {
          RasterizeGridValue(i, j, k, value, operation);
        }
      }
      else {
        RasterizeGridValue(x1, j, k, value, operation);
        RasterizeGridValue(x2, j, k, value, operation);
      }
    }
  }
}

// Assembly:
subq $0xd8, %rsp
movq %rdi, 0xd0(%rsp)
movq %rsi, 0xc8(%rsp)
movsd %xmm0, 0xc0(%rsp)
movsd %xmm1, 0xb8(%rsp)
movl %edx, 0xb4(%rsp)
movl %ecx, 0xb0(%rsp)
movq 0xd0(%rsp), %rax
movq %rax, 0x18(%rsp)
movl $0x0, 0x94(%rsp)
cmpl $0x3, 0x94(%rsp)
jge 0x6148d
movq 0xc8(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x13af0
addsd 0xc0(%rsp), %xmm0
cvttsd2si %xmm0, %ecx
movslq 0x94(%rsp), %rax
movl %ecx, 0x98(%rsp,%rax,4)
movslq 0x94(%rsp), %rax
cmpl $0x0, 0x98(%rsp,%rax,4)
jge 0x61394
jmp 0x617b1
movq 0x18(%rsp), %rdi
movslq 0x94(%rsp), %rax
movl 0x98(%rsp,%rax,4), %eax
movl %eax, 0x14(%rsp)
movl 0x94(%rsp), %esi
callq 0x6bdc0
movl %eax, %ecx
movl 0x14(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x613ea
movq 0x18(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x6bdc0
movl %eax, %ecx
subl $0x1, %ecx
movslq 0x94(%rsp), %rax
movl %ecx, 0x98(%rsp,%rax,4)
movq 0xc8(%rsp), %rdi
movl 0x94(%rsp), %esi
callq 0x13af0
movq 0x18(%rsp), %rdi
subsd 0xc0(%rsp), %xmm0
cvttsd2si %xmm0, %ecx
movslq 0x94(%rsp), %rax
movl %ecx, 0xa4(%rsp,%rax,4)
movslq 0x94(%rsp), %rax
movl 0xa4(%rsp,%rax,4), %eax
movl %eax, 0x10(%rsp)
movl 0x94(%rsp), %esi
callq 0x6bdc0
movl %eax, %ecx
movl 0x10(%rsp), %eax
subl $0x1, %ecx
cmpl %ecx, %eax
jle 0x61450
jmp 0x617b1
movslq 0x94(%rsp), %rax
cmpl $0x0, 0xa4(%rsp,%rax,4)
jge 0x61475
movslq 0x94(%rsp), %rax
movl $0x0, 0xa4(%rsp,%rax,4)
jmp 0x61477
movl 0x94(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x94(%rsp)
jmp 0x6133f
movsd 0xc0(%rsp), %xmm0
mulsd 0xc0(%rsp), %xmm0
movsd %xmm0, 0x88(%rsp)
movl 0xac(%rsp), %eax
movl %eax, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0xa0(%rsp), %eax
jg 0x617b1
cvtsi2sdl 0x84(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
movq 0xc8(%rsp), %rdi
movl $0x2, %esi
callq 0x13af0
movaps %xmm0, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x78(%rsp)
movsd 0x78(%rsp), %xmm0
mulsd 0x78(%rsp), %xmm0
movsd %xmm0, 0x70(%rsp)
movsd 0x88(%rsp), %xmm0
subsd 0x70(%rsp), %xmm0
movsd %xmm0, 0x68(%rsp)
movsd 0x68(%rsp), %xmm0
callq 0xf390
movsd %xmm0, 0x60(%rsp)
movq 0xc8(%rsp), %rdi
movl $0x1, %esi
callq 0x13af0
subsd 0x60(%rsp), %xmm0
movsd 0xd1ab2(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x5c(%rsp)
movq 0xc8(%rsp), %rdi
movl $0x1, %esi
callq 0x13af0
addsd 0x60(%rsp), %xmm0
movsd 0xd1a86(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x58(%rsp)
movl 0x5c(%rsp), %eax
cmpl 0xa8(%rsp), %eax
jge 0x615ae
movl 0xa8(%rsp), %eax
movl %eax, 0x5c(%rsp)
movl 0x58(%rsp), %eax
cmpl 0x9c(%rsp), %eax
jle 0x615c6
movl 0x9c(%rsp), %eax
movl %eax, 0x58(%rsp)
movl 0x5c(%rsp), %eax
movl %eax, 0x54(%rsp)
movl 0x54(%rsp), %eax
cmpl 0x58(%rsp), %eax
jg 0x61799
cvtsi2sdl 0x54(%rsp), %xmm0
movsd %xmm0, (%rsp)
movq 0xc8(%rsp), %rdi
movl $0x1, %esi
callq 0x13af0
movaps %xmm0, %xmm1
movsd (%rsp), %xmm0
subsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
cvtsi2sd %eax, %xmm0
movsd %xmm0, 0x48(%rsp)
movsd 0x48(%rsp), %xmm0
mulsd 0x48(%rsp), %xmm0
movsd %xmm0, 0x40(%rsp)
movsd 0x68(%rsp), %xmm0
subsd 0x40(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movsd 0x38(%rsp), %xmm0
callq 0xf390
movsd %xmm0, 0x30(%rsp)
movq 0xc8(%rsp), %rdi
xorl %esi, %esi
callq 0x13af0
subsd 0x30(%rsp), %xmm0
movsd 0xd19ab(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x2c(%rsp)
movq 0xc8(%rsp), %rdi
xorl %esi, %esi
callq 0x13af0
addsd 0x30(%rsp), %xmm0
movsd 0xd1982(%rip), %xmm1    # 0x133010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0x28(%rsp)
movl 0x2c(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jge 0x616b2
movl 0xa4(%rsp), %eax
movl %eax, 0x2c(%rsp)
movl 0x28(%rsp), %eax
cmpl 0x98(%rsp), %eax
jle 0x616ca
movl 0x98(%rsp), %eax
movl %eax, 0x28(%rsp)
cmpl $0x0, 0xb4(%rsp)
jne 0x616e8
movl 0x54(%rsp), %eax
cmpl 0x5c(%rsp), %eax
je 0x616e8
movl 0x54(%rsp), %eax
cmpl 0x58(%rsp), %eax
jne 0x61733
movl 0x2c(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x24(%rsp), %eax
cmpl 0x28(%rsp), %eax
jg 0x61731
movq 0x18(%rsp), %rdi
movl 0x24(%rsp), %esi
movl 0x54(%rsp), %edx
movl 0x84(%rsp), %ecx
movsd 0xb8(%rsp), %xmm0
movl 0xb0(%rsp), %r8d
callq 0x5f820
movl 0x24(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x24(%rsp)
jmp 0x616f0
jmp 0x61787
movq 0x18(%rsp), %rdi
movl 0x2c(%rsp), %esi
movl 0x54(%rsp), %edx
movl 0x84(%rsp), %ecx
movsd 0xb8(%rsp), %xmm0
movl 0xb0(%rsp), %r8d
callq 0x5f820
movq 0x18(%rsp), %rdi
movl 0x28(%rsp), %esi
movl 0x54(%rsp), %edx
movl 0x84(%rsp), %ecx
movsd 0xb8(%rsp), %xmm0
movl 0xb0(%rsp), %r8d
callq 0x5f820
jmp 0x61789
movl 0x54(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x54(%rsp)
jmp 0x615ce
jmp 0x6179b
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0x614b6
addq $0xd8, %rsp
retq
nopl (%rax)