// File: /workspace/github_cpp_makefile_star2_O3/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, 0xc(%rsp)
movsd %xmm0, 0x10(%rsp)
movq %rdi, %rbx
leaq 0x28(%rsp), %rdi
callq 0x633f4
movl 0xc0(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm2
movq %rbx, (%rsp)
movl 0xc4(%rbx), %eax
decl %eax
cvtsi2sd %eax, %xmm3
leaq 0x70(%rsp), %rdi
xorpd %xmm0, %xmm0
xorpd %xmm1, %xmm1
callq 0x6539c
leaq 0x28(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x64de6
leaq 0x70(%rsp), %rdi
callq 0x65c14
movl 0x38(%rsp), %r14d
testl %r14d, %r14d
je 0x5f7fa
cmpl $0x1, %r14d
je 0x5f5ba
cmpl $0x2, %r14d
jne 0x5f5e0
movq 0x30(%rsp), %rsi
leaq 0x10(%rsi), %rdx
movq (%rsp), %rdi
movsd 0x10(%rsp), %xmm0
movapd %xmm0, %xmm1
callq 0x5ed56
jmp 0x5f7fa
movq 0x30(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x8(%rax), %xmm1
movq (%rsp), %rdi
movsd 0x10(%rsp), %xmm2
movl 0xc(%rsp), %esi
callq 0x5ec04
jmp 0x5f7fa
movq (%rsp), %rax
movl 0xc4(%rax), %ebx
imull %ebx, %r14d
addl %r14d, %r14d
movslq %r14d, %rax
movq %rax, %rcx
shlq $0x4, %rcx
testl %eax, %eax
movq $-0x1, %rdi
cmovnsq %rcx, %rdi
callq 0xe030
movq %rax, %r12
testl %ebx, %ebx
jle 0x5f7f2
xorl %ebp, %ebp
movl %r14d, 0x8(%rsp)
movq %r12, 0x20(%rsp)
testl %r14d, %r14d
setle %cl
movslq 0x38(%rsp), %rax
testq %rax, %rax
setle %dl
orb %cl, %dl
jne 0x5f7db
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
movq 0x30(%rsp), %rdi
movq %rax, %rcx
shlq $0x4, %rcx
leaq (%rdi,%rcx), %rsi
addq $-0x10, %rsi
movsd 0x8(%rsi), %xmm2
subsd %xmm0, %xmm2
xorl %r15d, %r15d
movl $0x1, %ecx
movl 0x8(%rsp), %r9d
movq %rdi, %rdx
movsd 0x8(%rdi), %xmm1
subsd %xmm0, %xmm1
xorpd %xmm3, %xmm3
ucomisd %xmm2, %xmm3
jbe 0x5f6a4
ucomisd %xmm3, %xmm1
jb 0x5f6a4
movapd %xmm1, %xmm3
subsd %xmm2, %xmm3
movapd %xmm1, %xmm2
divsd %xmm3, %xmm2
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
movq %rdx, %r8
jmp 0x5f6c7
ucomisd %xmm1, %xmm3
jbe 0x5f6f4
ucomisd %xmm3, %xmm2
jb 0x5f6f4
movapd %xmm2, %xmm3
subsd %xmm1, %xmm3
divsd %xmm3, %xmm2
movl $0x1, %edi
movq %rsi, %r8
movq %rdx, %rsi
movsd (%r8), %xmm3
movsd (%rsi), %xmm4
subsd %xmm3, %xmm4
mulsd %xmm2, %xmm4
addsd %xmm3, %xmm4
movslq %r15d, %r15
movq %r15, %rsi
shlq $0x4, %rsi
movsd %xmm4, (%r12,%rsi)
movl %edi, 0x8(%r12,%rsi)
incl %r15d
cmpq %rax, %rcx
jge 0x5f710
leaq 0x10(%rdx), %rdi
incq %rcx
movq %rdx, %rsi
movapd %xmm1, %xmm2
cmpl %r9d, %r15d
jl 0x5f66e
cmpl $0x2, %r15d
jl 0x5f7db
movl %r15d, %r13d
movl $0x10, %edx
movq %r12, %rdi
movq %r13, %rsi
leaq 0x113(%rip), %rcx       # 0x5f842
callq 0xe880
leal -0x1(%r15), %eax
movl %eax, 0x1c(%rsp)
xorl %ebx, %ebx
movslq %ebx, %rsi
movq %rsi, %rax
shlq $0x4, %rax
leal 0x2(%rsi), %ebx
leaq (%r12,%rax), %rcx
addq $0x10, %rcx
movl 0x8(%r12,%rax), %edx
cmpl %r15d, %ebx
jge 0x5f779
addq $0x2, %rsi
cmpl %edx, 0x8(%rcx)
jne 0x5f777
incq %rsi
addq $0x10, %rcx
cmpq %rsi, %r13
jne 0x5f761
movl %r13d, %ebx
jmp 0x5f779
movl %esi, %ebx
cmpl 0x8(%rcx), %edx
je 0x5f7db
addq %r12, %rax
movsd (%rax), %xmm0
movhpd (%rcx), %xmm0           # xmm0 = xmm0[0],mem[0]
addpd 0x4287f(%rip), %xmm0    # 0xa2010
cvttpd2dq %xmm0, %xmm0
movd %xmm0, %r12d
pshufd $0x55, %xmm0, %xmm0     # xmm0 = xmm0[1,1,1,1]
movd %xmm0, %r14d
cmpl %r14d, %r12d
jg 0x5f7cc
incl %r14d
movq (%rsp), %rdi
movl %r12d, %esi
movl %ebp, %edx
movq 0x10(%rsp), %xmm0
movl 0xc(%rsp), %ecx
callq 0x5eb14
incl %r12d
cmpl %r12d, %r14d
jne 0x5f7ac
cmpl 0x1c(%rsp), %ebx
movq 0x20(%rsp), %r12
jl 0x5f73e
incl %ebp
movq (%rsp), %rax
cmpl 0xc4(%rax), %ebp
movl 0x8(%rsp), %r14d
jl 0x5f624
movq %r12, %rdi
callq 0xe5b0
leaq 0x28(%rsp), %rdi
callq 0x636e8
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x5f82d
jmp 0x5f82d
movq %rax, %rbx
leaq 0x70(%rsp), %rdi
callq 0x65c14
jmp 0x5f830
jmp 0x5f82d
jmp 0x5f82d
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x636e8
movq %rbx, %rdi
callq 0xe7e0