// File: /workspace/github_cpp_makefile_star2_O1/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, 0x34(%rsp)
movsd %xmm0, 0x8(%rsp)
movq %rsi, %r14
movq %rdi, %r15
leaq 0x238(%rdi), %rax
xorl %ecx, %ecx
xorl %edx, %edx
movsd (%r14,%rdx,2), %xmm0
movapd %xmm0, %xmm2
addsd 0x8(%rsp), %xmm2
cvttsd2si %xmm2, %edi
movl %edi, 0x40(%rsp,%rdx)
testl %edi, %edi
js 0x351c7
movl (%rax,%rdx), %esi
cmpl %edi, %esi
jg 0x34fab
leal -0x1(%rsi), %edi
movl %edi, 0x40(%rsp,%rdx)
subsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %edi
movl %edi, 0x4c(%rsp,%rdx)
cmpl %edi, %esi
jle 0x351c7
testl %edi, %edi
cmovlel %ecx, %edi
movl %edi, 0x4c(%rsp,%rdx)
addq $0x4, %rdx
cmpq $0xc, %rdx
jne 0x34f7d
movl 0x54(%rsp), %r12d
movl 0x48(%rsp), %eax
movl %eax, 0x2c(%rsp)
cmpl %eax, %r12d
jg 0x351c7
movsd 0x8(%rsp), %xmm0
mulsd %xmm0, %xmm0
movsd %xmm0, 0x8(%rsp)
movl 0x4c(%rsp), %eax
movl %eax, 0x3c(%rsp)
movl 0x50(%rsp), %eax
movl %eax, 0x28(%rsp)
movl 0x40(%rsp), %eax
movl %eax, 0x38(%rsp)
movl 0x44(%rsp), %eax
movl %eax, 0x24(%rsp)
movsd %xmm1, 0x18(%rsp)
movq %r14, 0x58(%rsp)
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
subsd 0x10(%r14), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd 0x8(%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd 0x6d197(%rip), %xmm0    # 0xa21e8
movsd %xmm0, 0x60(%rsp)
jb 0x3505f
sqrtsd %xmm0, %xmm0
jmp 0x35064
callq 0xe340
movsd 0x8(%r14), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd 0x6cfb6(%rip), %xmm3    # 0xa2030
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %ecx
addsd %xmm1, %xmm0
addsd %xmm3, %xmm0
cvttsd2si %xmm0, %edx
movl 0x28(%rsp), %eax
cmpl %ecx, %eax
cmovgl %eax, %ecx
movl 0x24(%rsp), %eax
cmpl %edx, %eax
cmovll %eax, %edx
movl %ecx, %r13d
movl %ecx, 0x30(%rsp)
movl %edx, 0x14(%rsp)
cmpl %edx, %ecx
jg 0x351b4
xorps %xmm0, %xmm0
cvtsi2sd %r13d, %xmm0
subsd 0x8(%r14), %xmm0
cvttpd2dq %xmm0, %xmm0
cvtdq2pd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd 0x60(%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd 0x6d109(%rip), %xmm0    # 0xa21e8
jb 0x350e7
sqrtsd %xmm0, %xmm0
jmp 0x350ec
callq 0xe340
movsd (%r14), %xmm1
movapd %xmm1, %xmm2
subsd %xmm0, %xmm2
movsd 0x6cf2f(%rip), %xmm3    # 0xa2030
addsd %xmm3, %xmm2
cvttsd2si %xmm2, %r14d
addsd %xmm1, %xmm0
addsd %xmm3, %xmm0
cvttsd2si %xmm0, %ebp
movl 0x3c(%rsp), %eax
cmpl %r14d, %eax
cmovgl %eax, %r14d
movl 0x38(%rsp), %eax
cmpl %ebp, %eax
cmovll %eax, %ebp
cmpl 0x14(%rsp), %r13d
je 0x35174
cmpl $0x0, 0x34(%rsp)
jne 0x35174
cmpl 0x30(%rsp), %r13d
je 0x35174
movq %r15, %rdi
movl %r14d, %esi
movl %r13d, %edx
movl %r12d, %ecx
movsd 0x18(%rsp), %xmm0
movl %ebx, %r8d
callq 0x341de
movq %r15, %rdi
movl %ebp, %esi
movl %r13d, %edx
movl %r12d, %ecx
movsd 0x18(%rsp), %xmm0
movl %ebx, %r8d
callq 0x341de
jmp 0x3519d
cmpl %ebp, %r14d
jg 0x3519d
incl %ebp
movq %r15, %rdi
movl %r14d, %esi
movl %r13d, %edx
movl %r12d, %ecx
movsd 0x18(%rsp), %xmm0
movl %ebx, %r8d
callq 0x341de
incl %r14d
cmpl %r14d, %ebp
jne 0x3517b
leal 0x1(%r13), %eax
cmpl 0x14(%rsp), %r13d
movl %eax, %r13d
movq 0x58(%rsp), %r14
jne 0x350b3
leal 0x1(%r12), %eax
cmpl 0x2c(%rsp), %r12d
movl %eax, %r12d
jne 0x35025
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq