// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::RasterizeGridPolygon(gaps::R2Polygon const&, double, int)
void R2Grid::
RasterizeGridPolygon(const R2Polygon& polygon, RNScalar value, int operation) 
{
  // Clip polygon to grid bounding box
  R2Polygon clipped_polygon(polygon);
  clipped_polygon.Clip(GridBox());
  if (clipped_polygon.NPoints() == 0) return; 

  // Check if only one point
  if (clipped_polygon.NPoints() == 1) { 
    RasterizeGridPoint(clipped_polygon.Point(0), value, operation); 
    return; 
  }

  // Check if only two points
  if (clipped_polygon.NPoints() == 2) { 
    RasterizeGridSpan(clipped_polygon.Point(0), clipped_polygon.Point(1), value, operation); 
    return; 
  }

  // Allocate list of scan line crossings
  int max_crossings = 2 * YResolution() * clipped_polygon.NPoints();
  ScanLineCrossing *crossings = new ScanLineCrossing [max_crossings];
  int ncrossings = 0;

  // Scan convert polygon
  for (int iy = 0; iy < YResolution(); iy++) {
    // Build list of scan line crossings
    ncrossings = 0;    
    const R2Point *p1 = &clipped_polygon.Point(clipped_polygon.NPoints()-1);
    RNScalar d1 = p1->Y() - iy;
    for (int i = 0; i < clipped_polygon.NPoints(); i++) {
      if (ncrossings >= max_crossings) break;
      const R2Point *p2 = &clipped_polygon.Point(i);
      RNScalar d2 = p2->Y() - iy;
      if ((d1 < 0) && (d2 >= 0)) {
        RNScalar t = d2 / (d2 - d1);
        crossings[ncrossings].x = p2->X() + t * (p1->X() - p2->X());
        crossings[ncrossings].side = -1;
        ncrossings++;
      }
      else if ((d2 < 0) && (d1 >= 0)) {
        RNScalar t = d1 / (d1 - d2);
        crossings[ncrossings].x = p1->X() + t * (p2->X() - p1->X());
        crossings[ncrossings].side = 1;
        ncrossings++;
      }
      p1 = p2;
      d1 = d2;
    }

    // Check number of crossings
    if (ncrossings < 2) continue;

    // Sort scan line crossings by x coordinate
    qsort(crossings, ncrossings, sizeof(ScanLineCrossing), CompareScanLineCrossings);

    // Rasterize scan line
    int index = 0;
    while (index < ncrossings-1) {
      // Find two crossings
      ScanLineCrossing *crossing1 = &crossings[index++];
      ScanLineCrossing *crossing2 = &crossings[index++];
      while ((index < ncrossings) && (crossing2->side == crossing1->side)) crossing2 = &crossings[index++];
      if (crossing1->side == crossing2->side) break;

      // Fill pixels between crossings
      int ix1 = (int) (crossing1->x + 0.5);
      int ix2 = (int) (crossing2->x + 0.5);
      for (int ix = ix1; ix <= ix2; ix++) {
        RasterizeGridValue(ix, iy, value, operation);
      }
    }
  }

  // Delete crossings
  delete [] crossings;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %edx, 0x4(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rdi, %rbx
leaq 0x38(%rsp), %rdi
callq 0x64230
movl 0xc0(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm2
movq %rbx, 0x8(%rsp)
movl 0xc4(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm3
leaq 0x80(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
callq 0x6611c
leaq 0x38(%rsp), %rdi
leaq 0x80(%rsp), %rsi
callq 0x65b6a
leaq 0x80(%rsp), %rdi
callq 0x6699c
movslq 0x48(%rsp), %rax
testq %rax, %rax
je 0x605de
cmpl $0x1, %eax
je 0x60352
cmpl $0x2, %eax
movq 0x8(%rsp), %r8
jne 0x60379
movq 0x40(%rsp), %rsi
leaq 0x10(%rsi), %rdx
movq %r8, %rdi
movsd 0x10(%rsp), %xmm0
movapd %xmm0, %xmm1
callq 0x5fb08
jmp 0x605de
movq 0x40(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
movq 0x8(%rsp), %rdi
movsd 0x10(%rsp), %xmm2
movl 0x4(%rsp), %esi
callq 0x5f9b6
jmp 0x605de
movslq 0xc4(%r8), %rcx
imulq %rax, %rcx
leal (%rcx,%rcx), %r14d
shlq $0x5, %rcx
testl %r14d, %r14d
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
movq %r8, %rbx
callq 0xe030
movq %rax, %r15
cmpl $0x0, 0xc4(%rbx)
jle 0x605d6
movq %rbx, %rdi
xorl %r12d, %r12d
movl %r14d, 0x1c(%rsp)
movq %r15, 0x30(%rsp)
movslq 0x48(%rsp), %rax
movl $0x0, %ebp
testq %rax, %rax
jle 0x604b3
movl $0x0, %ebp
testl %r14d, %r14d
jle 0x604b3
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
movq 0x40(%rsp), %r8
movq %rax, %rcx
shlq $0x4, %rcx
leaq (%r8,%rcx), %rsi
addq $-0x10, %rsi
movsd 0x8(%rsi), %xmm2
subsd %xmm0, %xmm2
xorl %ebp, %ebp
movl $0x1, %ecx
movq %r8, %rdx
movsd 0x8(%r8), %xmm1
subsd %xmm0, %xmm1
xorpd %xmm3, %xmm3
ucomisd %xmm2, %xmm3
jbe 0x60447
ucomisd %xmm3, %xmm1
jb 0x60447
movapd %xmm1, %xmm3
subsd %xmm2, %xmm3
movapd %xmm1, %xmm2
divsd %xmm3, %xmm2
movl $0xffffffff, %r9d       # imm = 0xFFFFFFFF
movq %rdx, %r8
jmp 0x6046b
ucomisd %xmm1, %xmm3
jbe 0x60497
ucomisd %xmm3, %xmm2
jb 0x60497
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
divsd %xmm3, %xmm2
movl $0x1, %r9d
movq %rsi, %r8
movq %rdx, %rsi
movsd (%r8), %xmm3
movsd (%rsi), %xmm4
subsd %xmm3, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
movslq %ebp, %rbp
movq %rbp, %rsi
shlq $0x4, %rsi
movsd %xmm4, (%r15,%rsi)
movl %r9d, 0x8(%r15,%rsi)
incl %ebp
cmpq %rax, %rcx
jae 0x604b3
leaq 0x10(%rdx), %r8
incq %rcx
movq %rdx, %rsi
movapd %xmm1, %xmm2
cmpl %r14d, %ebp
jl 0x6040f
cmpl $0x2, %ebp
jl 0x605c1
movl %ebp, %ebx
movl $0x10, %edx
movq %r15, %rdi
movq %rbx, %rsi
leaq 0x159(%rip), %rcx       # 0x60629
callq 0xe880
leal -0x1(%rbp), %eax
movl %eax, 0x20(%rsp)
negq %rbx
movq %rbx, 0x28(%rsp)
xorl %r11d, %r11d
movq 0x8(%rsp), %rdi
cmpl 0x20(%rsp), %r11d
jge 0x605c1
movslq %r11d, %rcx
shlq $0x4, %rcx
leaq (%r15,%rcx), %rax
addl $0x2, %r11d
addq %r15, %rcx
addq $0x10, %rcx
cmpl %ebp, %r11d
jge 0x60550
movl 0x8(%rax), %edx
movslq %r11d, %r10
movq 0x28(%rsp), %rsi
addq %r10, %rsi
shlq $0x4, %r10
addq %r15, %r10
xorl %r8d, %r8d
cmpl %edx, 0x8(%rcx)
jne 0x6054d
movq %r10, %r9
decq %r8
addq $0x10, %r10
movq %r9, %rcx
cmpq %r8, %rsi
jne 0x6052a
addq $-0x10, %r10
movl %ebp, %r11d
movq %r10, %rcx
jmp 0x60550
subl %r8d, %r11d
movl 0x8(%rax), %ebx
movl 0x8(%rcx), %r14d
cmpl %r14d, %ebx
je 0x605c1
movl %r11d, 0x24(%rsp)
movsd (%rax), %xmm0
movhpd (%rcx), %xmm0           # xmm0 = xmm0[0],mem[0]
addpd 0x41a9f(%rip), %xmm0    # 0xa2010
cvttpd2dq %xmm0, %xmm0
movd %xmm0, %r13d
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %r15d
cmpl %r15d, %r13d
jg 0x605ae
incl %r15d
movl %r13d, %esi
movl %r12d, %edx
movq 0x10(%rsp), %xmm0
movl 0x4(%rsp), %ecx
callq 0x5f8c6
incl %r13d
cmpl %r13d, %r15d
movq 0x8(%rsp), %rdi
jne 0x6058c
cmpl %r14d, %ebx
movq 0x30(%rsp), %r15
movl 0x24(%rsp), %r11d
jne 0x604ec
incl %r12d
cmpl 0xc4(%rdi), %r12d
movl 0x1c(%rsp), %r14d
jl 0x603c2
movq %r15, %rdi
callq 0xe5b0
leaq 0x38(%rsp), %rdi
callq 0x6451e
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x60614
jmp 0x60614
movq %rax, %rbx
leaq 0x80(%rsp), %rdi
callq 0x6699c
jmp 0x60617
jmp 0x60614
jmp 0x60614
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x6451e
movq %rbx, %rdi
callq 0xe7e0