// File: /workspace/github_cpp_makefile_star2_O2/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 $0x98, %rsp
movl %edx, %ebx
movsd %xmm0, 0x18(%rsp)
movq %rdi, %r14
leaq 0x28(%rsp), %rdi
callq 0x5ae24
leaq 0x70(%rsp), %rdi
movq %r14, %rsi
callq 0x5ad3c
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x5c5c0
leaq 0x70(%rsp), %rdi
callq 0x5d2ee
movl 0x38(%rsp), %eax
testl %eax, %eax
je 0x57620
cmpl $0x1, %eax
je 0x573da
cmpl $0x2, %eax
jne 0x573f4
movq 0x30(%rsp), %rsi
leaq 0x10(%rsi), %rdx
movq %r14, %rdi
movsd 0x18(%rsp), %xmm0
movl %ebx, %ecx
callq 0x5ad68
jmp 0x57620
movq 0x30(%rsp), %rsi
movq %r14, %rdi
movsd 0x18(%rsp), %xmm0
movl %ebx, %edx
callq 0x5ad28
jmp 0x57620
movl 0xc4(%r14), %r12d
imull %r12d, %eax
addl %eax, %eax
movl %eax, 0x24(%rsp)
cltq
movq %rax, %rcx
shlq $0x4, %rcx
testl %eax, %eax
pushq $-0x1
popq %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, 0x10(%rsp)
xorl %ebp, %ebp
cmpl %r12d, %ebp
jge 0x57616
movslq 0x38(%rsp), %rax
movq 0x30(%rsp), %rdi
movq %rax, %rcx
shlq $0x4, %rcx
leaq (%rdi,%rcx), %rsi
addq $-0x10, %rsi
cvtsi2sd %ebp, %xmm0
movsd 0x8(%rsi), %xmm1
subsd %xmm0, %xmm1
xorl %ecx, %ecx
movq $0x0, 0x8(%rsp)
cmpq %rax, %rcx
jge 0x57515
movq %rdi, %rdx
movq 0x8(%rsp), %rdi
cmpl 0x24(%rsp), %edi
jge 0x57515
movsd 0x8(%rdx), %xmm2
subsd %xmm0, %xmm2
xorpd %xmm3, %xmm3
ucomisd %xmm1, %xmm3
jbe 0x574a8
ucomisd %xmm3, %xmm2
jb 0x574a8
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
movapd %xmm2, %xmm1
divsd %xmm3, %xmm1
movq %rdx, %r8
pushq $-0x1
jmp 0x574c8
ucomisd %xmm2, %xmm3
jbe 0x57502
ucomisd %xmm3, %xmm1
jb 0x57502
movapd %xmm1, %xmm3
subsd %xmm2, %xmm3
divsd %xmm3, %xmm1
movq %rsi, %r8
movq %rdx, %rsi
pushq $0x1
popq %rdi
movsd (%r8), %xmm3
movsd (%rsi), %xmm4
subsd %xmm3, %xmm4
mulsd %xmm1, %xmm4
addsd %xmm3, %xmm4
movq 0x8(%rsp), %r9
movslq %r9d, %rsi
shlq $0x4, %rsi
movq 0x10(%rsp), %r8
movsd %xmm4, (%r8,%rsi)
movl %edi, 0x8(%r8,%rsi)
incl %r9d
movq %r9, 0x8(%rsp)
incq %rcx
leaq 0x10(%rdx), %rdi
movapd %xmm2, %xmm1
movq %rdx, %rsi
jmp 0x5745d
movq 0x8(%rsp), %r15
cmpl $0x2, %r15d
jl 0x5760f
movl %r15d, %r12d
movq 0x10(%rsp), %rdi
movq %r12, %rsi
pushq $0x10
popq %rdx
leaq 0x12f(%rip), %rcx       # 0x57668
callq 0xe8b0
leal -0x1(%r15), %eax
movl %eax, 0x20(%rsp)
movl $0x0, 0x4(%rsp)
movl 0x4(%rsp), %eax
cmpl 0x20(%rsp), %eax
jge 0x57608
movl 0x4(%rsp), %esi
movslq %esi, %rdx
shlq $0x4, %rdx
movq 0x10(%rsp), %r8
leaq (%r8,%rdx), %rax
addl $0x2, %esi
leaq (%r8,%rdx), %rcx
addq $0x10, %rcx
movslq %esi, %rsi
movq 0x8(%rsp), %rdi
cmpl %esi, %edi
movl %esi, %r9d
cmovgl %edi, %r9d
movl %r9d, 0x4(%rsp)
movl 0x8(%r8,%rdx), %edx
movq %rsi, %rdi
shlq $0x4, %rdi
addq %r8, %rdi
movq %rdi, %r8
movl 0x8(%rcx), %edi
cmpq %r12, %rsi
jge 0x575bf
cmpl %edx, %edi
jne 0x575bb
incq %rsi
leaq 0x10(%r8), %rdi
movq %r8, %rcx
jmp 0x575a0
movl %esi, 0x4(%rsp)
cmpl %edi, %edx
je 0x57608
movsd (%rax), %xmm0
movsd 0x3ba41(%rip), %xmm1    # 0x93010
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r13d
movsd (%rcx), %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %r15d
cmpl %r15d, %r13d
jg 0x5754e
movq %r14, %rdi
movl %r13d, %esi
movl %ebp, %edx
movsd 0x18(%rsp), %xmm0
movl %ebx, %ecx
callq 0x569e8
incl %r13d
jmp 0x575e5
movl 0xc4(%r14), %r12d
incl %ebp
jmp 0x57423
movq 0x10(%rsp), %rdi
callq 0xe5d0
leaq 0x28(%rsp), %rdi
callq 0x5b0fa
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x57653
jmp 0x57653
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x5d2ee
jmp 0x57656
jmp 0x57653
jmp 0x57653
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x5b0fa
movq %rbx, %rdi
callq 0xe810