// File: /workspace/github_cpp_makefile_star2_O1/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Polygon.cpp
// Function: gaps::R2Polygon::Clip(gaps::R2Line const&)
void R2Polygon::
Clip(const R2Line& line) 
{
  // Check number of points
  if (npoints == 0) {
    return;
  }
  else if (npoints == 1) {
    if (R2SignedDistance(line, points[0]) < 0) {
      bbox = R2null_box;
      delete [] points;
      points = NULL;
      npoints = 0;
    }
  }
  else {
    // Check bounding box
    R2Halfspace halfspace(line, 0);

    // Check if bounding box is entirely on positive side of line
    if (R2Contains(halfspace, bbox)) {
      return;
    }

    // Check if bounding box is entirely on negative side of line
    if (R2Contains(-halfspace, bbox)) {
      bbox = R2null_box;
      delete [] points;
      points = NULL;
      npoints = 0;
      return;
    }

    // Create new array for points
    int nbuffer = 0;
    R2Point *buffer = new R2Point [ 4 * npoints ];
    if (!buffer) RNAbort("Unable to allocate points during clip");

    // Build buffer with clipped points
    const R2Point *p1 = &points[npoints-1];
    RNScalar d1 = R2SignedDistance(line, *p1);
    for (int i = 0; i < npoints; i++) {
      const R2Point *p2 = &points[i];
      RNScalar d2 = R2SignedDistance(line, *p2);
      if (d2 >= 0) {
        // Insert crossing from negative to positive
        if (d1 < 0) {
          RNScalar t = d2 / (d2 - d1);
          buffer[nbuffer++] = *p2 + t * (*p1 - *p2);
        }

        // Insert point on positive side
        buffer[nbuffer++] = *p2;
      }
      else {
        // Insert crossing from positive to negative
        if (d1 >= 0) {
          RNScalar t = d1 / (d1 - d2);
          buffer[nbuffer++] = *p1 + t * (*p2 - *p1);
        }
      }

      // Remember previous point
      p1 = p2;
      d1 = d2;
    }

    // Copy points
    bbox = R2null_box;
    npoints = nbuffer;
    delete [] points;
    points = new R2Point [ npoints ];
    for (int i = 0; i < npoints; i++) {
      points[i] = buffer[i];
      bbox.Union(points[i]);
    }

    // Delete the buffer of points
    delete [] buffer;
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movl 0x10(%rdi), %eax
testl %eax, %eax
je 0x6588f
movq %rsi, %r14
movq %rdi, %rbx
cmpl $0x1, %eax
jne 0x657da
movq 0x8(%rbx), %rsi
movq %r14, %rdi
callq 0x6d501
xorpd %xmm1, %xmm1
ucomisd %xmm0, %xmm1
ja 0x6584e
jmp 0x6588f
leaq 0x70(%rsp), %r15
movq %r15, %rdi
movq %r14, %rsi
xorl %edx, %edx
callq 0x67cce
leaq 0x18(%rbx), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x582d6
testl %eax, %eax
jne 0x6588f
movapd 0x3c815(%rip), %xmm3    # 0xa2020
movsd 0x80(%rsp), %xmm0
movsd 0x88(%rsp), %xmm1
movsd 0x90(%rsp), %xmm2
xorpd %xmm3, %xmm0
xorpd %xmm3, %xmm1
xorpd %xmm3, %xmm2
leaq 0x48(%rsp), %r15
movq %r15, %rdi
callq 0x67c78
movq %r15, %rdi
movq %r12, %rsi
callq 0x582d6
testl %eax, %eax
je 0x658a1
leaq 0x20(%rbx), %rdi
leaq 0x48267f(%rip), %r14    # 0x4e7ed8
leaq 0x8(%r14), %rsi
callq 0x68f66
leaq 0x30(%rbx), %rdi
addq $0x18, %r14
movq %r14, %rsi
callq 0x68f66
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x65880
callq 0xe5b0
movq $0x0, 0x8(%rbx)
movl $0x0, 0x10(%rbx)
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r14, 0x18(%rsp)
movl 0x10(%rbx), %r15d
leal (,%r15,4), %eax
movslq %eax, %r14
shlq $0x4, %r14
testl %r15d, %r15d
movq $-0x1, %r13
cmovnsq %r14, %r13
movq %r13, %rdi
callq 0xe030
movq %rax, %rbp
testl %r15d, %r15d
je 0x658ec
xorl %r15d, %r15d
leaq (%r15,%rbp), %rdi
callq 0x68e2c
addq $0x10, %r15
cmpq %r15, %r14
jne 0x658da
movq %r12, 0x20(%rsp)
movq 0x8(%rbx), %rax
movslq 0x10(%rbx), %rcx
shlq $0x4, %rcx
leaq (%rax,%rcx), %r13
addq $-0x10, %r13
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x6d501
cmpl $0x0, 0x10(%rbx)
jle 0x65a80
xorl %r15d, %r15d
xorl %r12d, %r12d
xorl %r14d, %r14d
movq %rbp, 0x8(%rsp)
movsd %xmm0, (%rsp)
movq %r13, %rbp
movq 0x8(%rbx), %r13
addq %r15, %r13
movq 0x18(%rsp), %rdi
movq %r13, %rsi
callq 0x6d501
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jae 0x65969
movsd (%rsp), %xmm2
ucomisd %xmm1, %xmm2
jae 0x659e4
movq 0x8(%rsp), %rbp
jmp 0x65a6a
movsd (%rsp), %xmm2
ucomisd %xmm2, %xmm1
movsd %xmm0, 0x10(%rsp)
jbe 0x65a4a
movapd %xmm0, %xmm1
subsd %xmm2, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, (%rsp)
leaq 0x28(%rsp), %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x6906b
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
leaq 0x28(%rsp), %rsi
movsd (%rsp), %xmm0
callq 0x694f6
leaq 0x48(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x69057
movslq %r14d, %rdi
incl %r14d
shlq $0x4, %rdi
movq 0x8(%rsp), %rbp
addq %rbp, %rdi
leaq 0x48(%rsp), %rsi
callq 0x68f66
jmp 0x65a4f
movapd %xmm2, %xmm1
subsd %xmm0, %xmm1
divsd %xmm1, %xmm2
movsd %xmm2, (%rsp)
leaq 0x28(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
movsd %xmm0, 0x10(%rsp)
callq 0x6906b
leaq 0x38(%rsp), %rdi
leaq 0x28(%rsp), %rsi
movsd (%rsp), %xmm0
callq 0x694f6
leaq 0x48(%rsp), %rdi
movq %rbp, %rsi
leaq 0x38(%rsp), %rdx
callq 0x69057
movslq %r14d, %rdi
incl %r14d
shlq $0x4, %rdi
movq 0x8(%rsp), %rbp
addq %rbp, %rdi
leaq 0x48(%rsp), %rsi
jmp 0x65a5f
movq 0x8(%rsp), %rbp
movslq %r14d, %rdi
incl %r14d
shlq $0x4, %rdi
addq %rbp, %rdi
movq %r13, %rsi
callq 0x68f66
movsd 0x10(%rsp), %xmm0
incq %r12
movslq 0x10(%rbx), %rax
addq $0x10, %r15
cmpq %rax, %r12
jl 0x6592a
jmp 0x65a83
xorl %r14d, %r14d
leaq 0x20(%rbx), %rdi
leaq 0x48244a(%rip), %r12    # 0x4e7ed8
leaq 0x8(%r12), %rsi
callq 0x68f66
leaq 0x30(%rbx), %rdi
addq $0x18, %r12
movq %r12, %rsi
callq 0x68f66
movl %r14d, 0x10(%rbx)
movq 0x8(%rbx), %rdi
testq %rdi, %rdi
je 0x65aba
callq 0xe5b0
movslq 0x10(%rbx), %r15
movq %r15, %r14
shlq $0x4, %r14
testq %r15, %r15
movq $-0x1, %r12
cmovnsq %r14, %r12
movq %r12, %rdi
callq 0xe030
movq %rax, %r13
testq %r15, %r15
je 0x65af8
xorl %r15d, %r15d
leaq (%r15,%r13), %rdi
callq 0x68e2c
addq $0x10, %r15
cmpq %r15, %r14
jne 0x65ae6
movq %r13, 0x8(%rbx)
cmpl $0x0, 0x10(%rbx)
movq 0x20(%rsp), %r12
jle 0x65b3c
xorl %r14d, %r14d
xorl %r15d, %r15d
leaq (%r14,%rbp), %rsi
movq 0x8(%rbx), %rdi
addq %r14, %rdi
callq 0x68f66
movq 0x8(%rbx), %rsi
addq %r14, %rsi
movq %r12, %rdi
callq 0x66714
incq %r15
movslq 0x10(%rbx), %rax
addq $0x10, %r14
cmpq %rax, %r15
jl 0x65b0d
movq %rbp, %rdi
callq 0xe5b0
jmp 0x6588f
movq %rax, %rbx
movq %r13, %rdi
movq %r12, %rsi
jmp 0x65b5d
movq %rax, %rbx
movq %rbp, %rdi
movq %r13, %rsi
callq 0xe8c0
movq %rbx, %rdi
callq 0xe7e0