// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movl %ecx, %ebx
movl %edx, 0x30(%rsp)
movsd %xmm1, 0x18(%rsp)
movsd %xmm0, 0x8(%rsp)
movq %rsi, %r14
movq %rdi, %r15
leaq 0x238(%rdi), %rax
xorl %ecx, %ecx
xorl %edx, %edx
cmpq $0xc, %rdx
je 0x30e2c
movsd (%r14,%rdx,2), %xmm0
movapd %xmm0, %xmm1
addsd 0x8(%rsp), %xmm1
cvttsd2si %xmm1, %edi
movl %edi, 0x3c(%rsp,%rdx)
testl %edi, %edi
js 0x3100b
movl (%rax,%rdx), %esi
cmpl %edi, %esi
jg 0x30e0b
leal -0x1(%rsi), %edi
movl %edi, 0x3c(%rsp,%rdx)
subsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %edi
cmpl %edi, %esi
jle 0x3100b
testl %edi, %edi
cmovlel %ecx, %edi
movl %edi, 0x54(%rsp,%rdx)
addq $0x4, %rdx
jmp 0x30dd7
movsd 0x8(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x5c(%rsp), %r12d
movl 0x44(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x54(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x58(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x3c(%rsp), %eax
movl %eax, 0x34(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x20(%rsp)
movq %r14, 0x60(%rsp)
cmpl 0x28(%rsp), %r12d
jg 0x3100b
cvtsi2sd %r12d, %xmm0
subsd 0x10(%r14), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm0
mulsd %xmm0, %xmm0
movsd 0x8(%rsp), %xmm4
subsd %xmm0, %xmm4
ucomisd 0x62346(%rip), %xmm4    # 0x931e8
movsd %xmm4, 0x48(%rsp)
jb 0x30eb0
sqrtsd %xmm4, %xmm0
jmp 0x30ebf
movapd %xmm4, %xmm0
callq 0xe350
movsd 0x48(%rsp), %xmm4
movsd 0x8(%r14), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd 0x6213b(%rip), %xmm3    # 0x93010
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %r13d
addsd %xmm1, %xmm0
addsd %xmm3, %xmm0
cvttsd2si %xmm0, %ecx
movl 0x24(%rsp), %eax
cmpl %r13d, %eax
cmovgl %eax, %r13d
movl 0x20(%rsp), %eax
cmpl %ecx, %eax
cmovll %eax, %ecx
movl %ecx, 0x14(%rsp)
movl %r13d, 0x2c(%rsp)
cmpl 0x14(%rsp), %r13d
jg 0x31003
cvtsi2sd %r13d, %xmm0
subsd 0x8(%r14), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm1
mulsd %xmm1, %xmm1
subsd %xmm1, %xmm4
ucomisd 0x622b3(%rip), %xmm4    # 0x931e8
jb 0x30f3d
sqrtsd %xmm4, %xmm0
jmp 0x30f46
movapd %xmm4, %xmm0
callq 0xe350
movsd (%r14), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd 0x620b5(%rip), %xmm3    # 0x93010
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %r14d
addsd %xmm1, %xmm0
addsd %xmm3, %xmm0
cvttsd2si %xmm0, %ebp
movl 0x38(%rsp), %eax
cmpl %r14d, %eax
cmovgl %eax, %r14d
movl 0x34(%rsp), %eax
cmpl %ebp, %eax
cmovll %eax, %ebp
cmpl 0x14(%rsp), %r13d
je 0x30feb
cmpl $0x0, 0x30(%rsp)
jne 0x30feb
cmpl 0x2c(%rsp), %r13d
je 0x30feb
movq %r15, %rdi
movl %r14d, %esi
movl %r13d, %edx
movl %r12d, %ecx
movsd 0x18(%rsp), %xmm0
movl %ebx, %r8d
callq 0x301e4
movq %r15, %rdi
movl %ebp, %esi
movl %r13d, %edx
movl %r12d, %ecx
movsd 0x18(%rsp), %xmm0
movl %ebx, %r8d
callq 0x301e4
jmp 0x30ff0
movq %r15, %rdi
movl %r14d, %esi
movl %r13d, %edx
movl %r12d, %ecx
movsd 0x18(%rsp), %xmm0
movl %ebx, %r8d
callq 0x301e4
incl %r14d
cmpl %ebp, %r14d
jle 0x30fce
incl %r13d
movq 0x60(%rsp), %r14
movsd 0x48(%rsp), %xmm4
jmp 0x30f07
incl %r12d
jmp 0x30e6e
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq