// File: 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:
subq $0x1e8, %rsp            # imm = 0x1E8
movq %rdi, 0x1e0(%rsp)
movq %rsi, 0x1d8(%rsp)
movsd %xmm0, 0x1d0(%rsp)
movl %edx, 0x1cc(%rsp)
movq 0x1e0(%rsp), %rax
movq %rax, 0xc8(%rsp)
movq 0x1d8(%rsp), %rsi
leaq 0x180(%rsp), %rdi
callq 0xb9ab0
movq 0xc8(%rsp), %rsi
leaq 0x158(%rsp), %rdi
callq 0xb96a0
jmp 0xb1b43
leaq 0x180(%rsp), %rdi
leaq 0x158(%rsp), %rsi
callq 0xbcae0
jmp 0xb1b5a
leaq 0x158(%rsp), %rdi
callq 0x6bc50
leaq 0x180(%rsp), %rdi
callq 0xa3810
movl %eax, 0xc4(%rsp)
jmp 0xb1b7d
movl 0xc4(%rsp), %eax
cmpl $0x0, %eax
jne 0xb1bd8
movl $0x1, 0x148(%rsp)
jmp 0xb234b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
jmp 0xb2360
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x150(%rsp)
movl %eax, 0x14c(%rsp)
leaq 0x158(%rsp), %rdi
callq 0x6bc50
jmp 0xb2360
leaq 0x180(%rsp), %rdi
callq 0xa3810
movl %eax, 0xc0(%rsp)
jmp 0xb1bee
movl 0xc0(%rsp), %eax
cmpl $0x1, %eax
jne 0xb1c4a
leaq 0x180(%rsp), %rdi
xorl %esi, %esi
callq 0xa3820
movq %rax, 0xb8(%rsp)
jmp 0xb1c13
movq 0xb8(%rsp), %rsi
movq 0xc8(%rsp), %rdi
movsd 0x1d0(%rsp), %xmm0
movl 0x1cc(%rsp), %edx
callq 0xb9630
jmp 0xb1c3a
movl $0x1, 0x148(%rsp)
jmp 0xb234b
leaq 0x180(%rsp), %rdi
callq 0xa3810
movl %eax, 0xb4(%rsp)
jmp 0xb1c60
movl 0xb4(%rsp), %eax
cmpl $0x2, %eax
jne 0xb1ce0
leaq 0x180(%rsp), %rdi
xorl %esi, %esi
callq 0xa3820
movq %rax, 0xa8(%rsp)
jmp 0xb1c85
leaq 0x180(%rsp), %rdi
movl $0x1, %esi
callq 0xa3820
movq %rax, 0xa0(%rsp)
jmp 0xb1ca1
movq 0xa0(%rsp), %rdx
movq 0xa8(%rsp), %rsi
movq 0xc8(%rsp), %rdi
movsd 0x1d0(%rsp), %xmm0
movl 0x1cc(%rsp), %ecx
callq 0xb96f0
jmp 0xb1cd0
movl $0x1, 0x148(%rsp)
jmp 0xb234b
movq 0xc8(%rsp), %rdi
callq 0xb9280
movl %eax, 0x9c(%rsp)
jmp 0xb1cf6
movl 0x9c(%rsp), %eax
addl %eax, %eax
movl %eax, 0x94(%rsp)
leaq 0x180(%rsp), %rdi
callq 0xa3810
movl %eax, 0x98(%rsp)
jmp 0xb1d1c
movl 0x98(%rsp), %ecx
movl 0x94(%rsp), %eax
imull %ecx, %eax
movl %eax, 0x144(%rsp)
movslq 0x144(%rsp), %rax
movl $0x10, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0xf030
movq %rax, 0x88(%rsp)
jmp 0xb1d61
movq 0x88(%rsp), %rax
movq %rax, 0x138(%rsp)
movl $0x0, 0x134(%rsp)
movl $0x0, 0x130(%rsp)
movq 0xc8(%rsp), %rdi
movl 0x130(%rsp), %eax
movl %eax, 0x80(%rsp)
callq 0xb9280
movl %eax, 0x84(%rsp)
jmp 0xb1dab
movl 0x80(%rsp), %eax
movl 0x84(%rsp), %ecx
cmpl %ecx, %eax
jge 0xb2325
movl $0x0, 0x134(%rsp)
leaq 0x180(%rsp), %rdi
callq 0xa3810
movl %eax, 0x7c(%rsp)
jmp 0xb1ddf
movl 0x7c(%rsp), %esi
decl %esi
leaq 0x180(%rsp), %rdi
callq 0xa3820
movq %rax, 0x70(%rsp)
jmp 0xb1df9
movq 0x70(%rsp), %rax
movq %rax, 0x128(%rsp)
movq 0x128(%rsp), %rdi
callq 0x38c60
movsd %xmm0, 0x68(%rsp)
jmp 0xb1e1b
movsd 0x68(%rsp), %xmm0
cvtsi2sdl 0x130(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x120(%rsp)
movl $0x0, 0x11c(%rsp)
movl 0x11c(%rsp), %eax
movl %eax, 0x60(%rsp)
leaq 0x180(%rsp), %rdi
callq 0xa3810
movl %eax, 0x64(%rsp)
jmp 0xb1e60
movl 0x60(%rsp), %eax
movl 0x64(%rsp), %ecx
cmpl %ecx, %eax
jge 0xb2131
movl 0x134(%rsp), %eax
cmpl 0x144(%rsp), %eax
jl 0xb1e85
jmp 0xb2131
movl 0x11c(%rsp), %esi
leaq 0x180(%rsp), %rdi
callq 0xa3820
movq %rax, 0x58(%rsp)
jmp 0xb1ea0
movq 0x58(%rsp), %rax
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rdi
callq 0x38c60
movsd %xmm0, 0x50(%rsp)
jmp 0xb1ec2
movsd 0x50(%rsp), %xmm0
cvtsi2sdl 0x130(%rsp), %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x108(%rsp)
xorps %xmm0, %xmm0
ucomisd 0x120(%rsp), %xmm0
jbe 0xb1fed
movsd 0x108(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xb1fed
movsd 0x108(%rsp), %xmm0
movsd 0x120(%rsp), %xmm2
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0x100(%rsp)
movq 0x110(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x48(%rsp)
jmp 0xb1f41
movsd 0x100(%rsp), %xmm0
movsd %xmm0, 0x38(%rsp)
movq 0x128(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x40(%rsp)
jmp 0xb1f65
movq 0x110(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x30(%rsp)
jmp 0xb1f7a
movsd 0x48(%rsp), %xmm1
movsd 0x38(%rsp), %xmm0
movsd 0x30(%rsp), %xmm3
movsd 0x40(%rsp), %xmm2
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x138(%rsp), %rax
movslq 0x134(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd %xmm0, (%rax)
movq 0x138(%rsp), %rax
movslq 0x134(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0xffffffff, 0x8(%rax)  # imm = 0xFFFFFFFF
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0xb20f9
xorps %xmm0, %xmm0
ucomisd 0x108(%rsp), %xmm0
jbe 0xb20f7
movsd 0x120(%rsp), %xmm0
xorps %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jb 0xb20f7
movsd 0x120(%rsp), %xmm0
movsd 0x108(%rsp), %xmm2
movaps %xmm0, %xmm1
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, 0xf8(%rsp)
movq 0x128(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x28(%rsp)
jmp 0xb2050
movsd 0xf8(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x110(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x20(%rsp)
jmp 0xb2074
movq 0x128(%rsp), %rdi
callq 0x38c50
movsd %xmm0, 0x10(%rsp)
jmp 0xb2089
movsd 0x28(%rsp), %xmm1
movsd 0x18(%rsp), %xmm0
movsd 0x10(%rsp), %xmm3
movsd 0x20(%rsp), %xmm2
subsd %xmm3, %xmm2
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movq 0x138(%rsp), %rax
movslq 0x134(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd %xmm0, (%rax)
movq 0x138(%rsp), %rax
movslq 0x134(%rsp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movl $0x1, 0x8(%rax)
movl 0x134(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x134(%rsp)
jmp 0xb20f9
movq 0x110(%rsp), %rax
movq %rax, 0x128(%rsp)
movsd 0x108(%rsp), %xmm0
movsd %xmm0, 0x120(%rsp)
movl 0x11c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x11c(%rsp)
jmp 0xb1e42
cmpl $0x2, 0x134(%rsp)
jge 0xb2140
jmp 0xb230f
movq 0x138(%rsp), %rdi
movslq 0x134(%rsp), %rsi
leaq 0x229(%rip), %rcx       # 0xb2380
movl $0x10, %edx
callq 0xf950
jmp 0xb2163
movl $0x0, 0xf4(%rsp)
movl 0xf4(%rsp), %eax
movl 0x134(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xb230d
movq 0x138(%rsp), %rax
movl 0xf4(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0xf4(%rsp)
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xe8(%rsp)
movq 0x138(%rsp), %rax
movl 0xf4(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0xf4(%rsp)
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xe0(%rsp)
movl 0xf4(%rsp), %ecx
xorl %eax, %eax
cmpl 0x134(%rsp), %ecx
movb %al, 0xf(%rsp)
jge 0xb2214
movq 0xe0(%rsp), %rax
movl 0x8(%rax), %eax
movq 0xe8(%rsp), %rcx
cmpl 0x8(%rcx), %eax
sete %al
movb %al, 0xf(%rsp)
movb 0xf(%rsp), %al
testb $0x1, %al
jne 0xb221e
jmp 0xb224d
movq 0x138(%rsp), %rax
movl 0xf4(%rsp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, 0xf4(%rsp)
movslq %ecx, %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, 0xe0(%rsp)
jmp 0xb21e1
movq 0xe8(%rsp), %rax
movl 0x8(%rax), %eax
movq 0xe0(%rsp), %rcx
cmpl 0x8(%rcx), %eax
jne 0xb226a
jmp 0xb230d
movq 0xe8(%rsp), %rax
movsd 0x80d96(%rip), %xmm0    # 0x133010
addsd (%rax), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xdc(%rsp)
movq 0xe0(%rsp), %rax
movsd 0x80d77(%rip), %xmm0    # 0x133010
addsd (%rax), %xmm0
cvttsd2si %xmm0, %eax
movl %eax, 0xd8(%rsp)
movl 0xdc(%rsp), %eax
movl %eax, 0xd4(%rsp)
movl 0xd4(%rsp), %eax
cmpl 0xd8(%rsp), %eax
jg 0xb2308
movq 0xc8(%rsp), %rdi
movl 0xd4(%rsp), %esi
movl 0x130(%rsp), %edx
movsd 0x1d0(%rsp), %xmm0
movl 0x1cc(%rsp), %ecx
callq 0xb0550
jmp 0xb22f3
jmp 0xb22f5
movl 0xd4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xd4(%rsp)
jmp 0xb22b6
jmp 0xb216e
jmp 0xb230f
movl 0x130(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x130(%rsp)
jmp 0xb1d87
movq 0x138(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0xb2340
movq (%rsp), %rdi
callq 0xf670
movl $0x0, 0x148(%rsp)
leaq 0x180(%rsp), %rdi
callq 0xba090
addq $0x1e8, %rsp            # imm = 0x1E8
retq
leaq 0x180(%rsp), %rdi
callq 0xba090
movq 0x150(%rsp), %rdi
callq 0xf890
nopw (%rax,%rax)