// File: thierry7100[P]CutOptim/src/Geometry.cpp
// Function: Polygon::Simplify(double)
void Polygon::Simplify(double max_error)
{
Point A = _Vertices[nVertices-1];           //  Start at end of polygon, to keep indexes OK
Point B;
Point C;
int hasSimplify = 1;

    while ( hasSimplify )
    {
        hasSimplify = 0;
        if ( nVertices == 3 ) break;    //  Not able to remove one more vertex
        //  Now walk through all path elements to delete vertices if they are aligned
        for ( int i = nVertices-2; i > 0; i--)
        {
            B = _Vertices[i];
            C = _Vertices[i-1];
            Segment AC = Segment(A, C);
            if ( AC.sqrDistancePoint(B) < max_error)
            {
                //  Point B is not necessary, remove
                delVertex(i);
                hasSimplify = 1;
                break;              //  Will start a new loop
            }
            A = B;
        }
        //  Now the case of first vertex (and last one)
        A = _Vertices[1];
        B = _Vertices[0];
        C = _Vertices[nVertices-2];
        Segment AC = Segment(A, C);
        if ( AC.sqrDistancePoint(B) < max_error)
        {
            //  Point B, with index 0 is not neceassry, remove it
            delVertex(0);               // Beware, this is also index nVertices - 1
            _Vertices[nVertices-1] = _Vertices[0];  //  New index 0, was 1 before removing point
            hasSimplify = 1;
        }
    }
    ReCalcArea();
    ReCalcBoundingBox();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x8(%rbp)
movsd %xmm0, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x100(%rbp)
movq %rax, %rdi
addq $0x10, %rdi
movl 0x8(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movq (%rax), %rcx
movq %rcx, -0x20(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
leaq -0x30(%rbp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x5b520
leaq -0x40(%rbp), %rdi
xorps %xmm1, %xmm1
movaps %xmm1, %xmm0
callq 0x5b520
movl $0x1, -0x44(%rbp)
cmpl $0x0, -0x44(%rbp)
je 0x62f08
movq -0x100(%rbp), %rax
movl $0x0, -0x44(%rbp)
cmpl $0x3, 0x8(%rax)
jne 0x62d2b
jmp 0x62f08
movq -0x100(%rbp), %rax
movl 0x8(%rax), %eax
subl $0x2, %eax
movl %eax, -0x48(%rbp)
cmpl $0x0, -0x48(%rbp)
jle 0x62df8
movq -0x100(%rbp), %rdi
addq $0x10, %rdi
movslq -0x48(%rbp), %rsi
callq 0x63990
movq -0x100(%rbp), %rdi
movq (%rax), %rcx
movq %rcx, -0x30(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x28(%rbp)
addq $0x10, %rdi
movl -0x48(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movq (%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
leaq -0xa0(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x639b0
leaq -0xa0(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x64b10
movaps %xmm0, %xmm1
movsd -0x10(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x62dda
movq -0x100(%rbp), %rdi
movl -0x48(%rbp), %esi
callq 0x61250
movl $0x1, -0x44(%rbp)
jmp 0x62df8
movq -0x30(%rbp), %rax
movq %rax, -0x20(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x18(%rbp)
movl -0x48(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x48(%rbp)
jmp 0x62d3b
movq -0x100(%rbp), %rdi
addq $0x10, %rdi
movl $0x1, %esi
callq 0x63990
movq -0x100(%rbp), %rdi
movq (%rax), %rcx
movq %rcx, -0x20(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x18(%rbp)
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x63990
movq %rax, %rcx
movq -0x100(%rbp), %rax
movq (%rcx), %rdx
movq %rdx, -0x30(%rbp)
movq 0x8(%rcx), %rcx
movq %rcx, -0x28(%rbp)
movq %rax, %rdi
addq $0x10, %rdi
movl 0x8(%rax), %eax
subl $0x2, %eax
movslq %eax, %rsi
callq 0x63990
movq (%rax), %rcx
movq %rcx, -0x40(%rbp)
movq 0x8(%rax), %rax
movq %rax, -0x38(%rbp)
leaq -0xf8(%rbp), %rdi
leaq -0x20(%rbp), %rsi
leaq -0x40(%rbp), %rdx
callq 0x639b0
leaq -0xf8(%rbp), %rdi
leaq -0x30(%rbp), %rsi
callq 0x64b10
movaps %xmm0, %xmm1
movsd -0x10(%rbp), %xmm0
ucomisd %xmm1, %xmm0
jbe 0x62f03
movq -0x100(%rbp), %rdi
xorl %esi, %esi
callq 0x61250
movq -0x100(%rbp), %rdi
addq $0x10, %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x63990
movq %rax, %rcx
movq -0x100(%rbp), %rax
movq %rcx, -0x108(%rbp)
movq %rax, %rdi
addq $0x10, %rdi
movl 0x8(%rax), %eax
subl $0x1, %eax
movslq %eax, %rsi
callq 0x63990
movq -0x108(%rbp), %rcx
movq (%rcx), %rdx
movq %rdx, (%rax)
movq 0x8(%rcx), %rcx
movq %rcx, 0x8(%rax)
movl $0x1, -0x44(%rbp)
jmp 0x62d08
movq -0x100(%rbp), %rdi
callq 0x612f0
movq -0x100(%rbp), %rdi
callq 0x60860
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
nopl (%rax)