// File: /workspace/github_cpp_makefile_star2_O2/thierry7100[P]CutOptim/src/Geometry.cpp
// Function: Polygon::BreakLongerEdges(double, double)
void Polygon::BreakLongerEdges(double max_length, double Diff)
{
std::vector<Point>::iterator it;
double Sign = 1;
int Modified = 0;

    do {
        Modified = 0;
        //  Do it reverse to avoid problem with indexes when inserting new points
        for ( int i = nVertices - 1; i > 0; i-- )
        {
            double l = _Vertices[i].Distance(_Vertices[i-1]);       //  Segment length
            if ( l > max_length )
            {
                //  Too long, break it
                int nSegment = (int)(l / max_length) + 1;       //  Number of segments, will add nSegment - 1 vertices
                //  Compute Delta off line
                //  For each segment create a segment which is parallel to the original segment, but translated at distance Diff
                //  Original segment
                double a = _Vertices[i-1].y - _Vertices[i].y;
                double b = _Vertices[i].x - _Vertices[i-1].x;
                double c = _Vertices[i-1].x * _Vertices[i].y - _Vertices[i].x * _Vertices[i-1].y;
                //  If equation of first segment is ax + by + c = 0, the translated segment has an equation like ax + by + C = 0 where C = c + Diff*sqrt(a*a + b*b)
                //  Here we will first build a segment which begins in A' and end in B' where A' is the point on the line perpendicular to the segment and which starts in A
                double C = c + Diff*sqrt(a*a+b*b);
                Line A1B1 = Line(a, b, C);
                //  Now the lines which are perpendicular in A or B to the segment
                Line AB_perpendicular_A = Line(-b, a, b*_Vertices[i-1].x - a*_Vertices[i-1].y);
                // Compute intersection of AB_perpendicular_A and A1B1 (point A1)
                Point A1 = A1B1.Intersect(AB_perpendicular_A);
                Point Delta = Point(A1.x - _Vertices[i-1].x, A1.y - _Vertices[i-1].y );
                //  Insert only one point at a time to avoid indices mismatch
                Point As = Point(_Vertices[i-1].x + (_Vertices[i].x-_Vertices[i-1].x) / double(nSegment), _Vertices[i-1].y + (_Vertices[i].y-_Vertices[i-1].y) / double(nSegment));
                As.x += Sign * Delta.x;
                As.y += Sign * Delta.y;
                As.x = round(As.x/precision)*precision;
                As.y = round(As.y/precision)*precision;
                Sign *= -1;
                it = _Vertices.begin();
                _Vertices.insert(it + i, As);
                BoundingBox.A.x = fmin(BoundingBox.A.x, As.x);
                BoundingBox.A.y = fmin(BoundingBox.A.y, As.y);
                BoundingBox.B.x = fmax(BoundingBox.B.x, As.x);
                BoundingBox.B.y = fmax(BoundingBox.B.y, As.y);
                Modified = 1;
                poly_changed = 1;
                nVertices = _Vertices.size();
                break;
            }
        }
    } while ( Modified );
    ReCalcArea();
    ReCalcBoundingBox();
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movsd %xmm1, 0x28(%rsp)
movsd %xmm0, 0x8(%rsp)
movq %rdi, %rbx
leaq 0x10(%rdi), %r14
movsd 0xff12(%rip), %xmm0     # 0x34008
movapd %xmm0, 0x10(%rsp)
leaq 0x90(%rsp), %r12
leaq 0x30(%rsp), %r13
movl 0x8(%rbx), %eax
leal 0x1(%rax), %ebp
shlq $0x4, %rax
decl %ebp
cmpl $0x2, %ebp
jl 0x2431e
leaq -0x10(%rax), %r15
movq (%r14), %rcx
leaq (%rcx,%rax), %rdi
addq $-0x10, %rdi
leaq (%rcx,%rax), %rsi
addq $-0x20, %rsi
callq 0x244c6
ucomisd 0x8(%rsp), %xmm0
movq %r15, %rax
jbe 0x24113
divsd 0x8(%rsp), %xmm0
cvttsd2si %xmm0, %ebp
incl %ebp
movq 0x10(%rbx), %rax
movsd 0x8(%rax,%r15), %xmm3
movsd -0x10(%rax,%r15), %xmm0
movupd -0x8(%rax,%r15), %xmm1
movapd %xmm0, %xmm2
mulsd %xmm3, %xmm2
unpcklpd %xmm0, %xmm3            # xmm3 = xmm3[0],xmm0[0]
movapd %xmm1, %xmm4
subpd %xmm3, %xmm4
movapd %xmm1, %xmm3
unpckhpd %xmm1, %xmm3            # xmm3 = xmm3[1],xmm1[1]
mulsd %xmm1, %xmm3
subsd %xmm3, %xmm2
movapd %xmm4, %xmm3
unpckhpd %xmm4, %xmm3            # xmm3 = xmm3[1],xmm4[1]
movapd %xmm3, %xmm5
mulsd %xmm3, %xmm5
movapd %xmm4, 0x70(%rsp)
movapd %xmm3, %xmm6
movapd 0x11f10(%rip), %xmm7    # 0x360c0
xorpd %xmm7, %xmm6
mulsd %xmm4, %xmm1
unpcklpd %xmm4, %xmm6            # xmm6 = xmm6[0],xmm4[0]
mulsd %xmm4, %xmm4
addsd %xmm5, %xmm4
sqrtsd %xmm4, %xmm4
mulsd 0x28(%rsp), %xmm4
addsd %xmm2, %xmm4
movsd %xmm4, 0x80(%rsp)
mulsd %xmm0, %xmm3
subsd %xmm1, %xmm3
movupd %xmm6, 0x90(%rsp)
movsd %xmm3, 0xa0(%rsp)
leaq 0x70(%rsp), %rdi
movq %r12, %rsi
callq 0x24434
movq 0x10(%rbx), %rax
cvtsi2sd %ebp, %xmm3
leaq (%rax,%r15), %rbp
movupd -0x10(%rax,%r15), %xmm2
unpcklpd %xmm1, %xmm0            # xmm0 = xmm0[0],xmm1[0]
subpd %xmm2, %xmm0
movupd (%rax,%r15), %xmm1
subpd %xmm2, %xmm1
unpcklpd %xmm3, %xmm3            # xmm3 = xmm3[0,0]
divpd %xmm3, %xmm1
addpd %xmm2, %xmm1
movapd 0x10(%rsp), %xmm2
unpcklpd %xmm2, %xmm2            # xmm2 = xmm2[0,0]
mulpd %xmm2, %xmm0
addpd %xmm1, %xmm0
movupd 0xa0(%rbx), %xmm1
unpcklpd %xmm1, %xmm1            # xmm1 = xmm1[0,0]
movapd %xmm1, 0x60(%rsp)
divpd %xmm1, %xmm0
movapd %xmm0, 0x40(%rsp)
callq 0x81a0
movapd %xmm0, 0x50(%rsp)
movapd 0x40(%rsp), %xmm0
unpckhpd %xmm0, %xmm0            # xmm0 = xmm0[1,1]
callq 0x81a0
movapd 0x50(%rsp), %xmm1
unpcklpd %xmm0, %xmm1            # xmm1 = xmm1[0],xmm0[0]
mulpd 0x60(%rsp), %xmm1
movapd %xmm1, 0x30(%rsp)
movaps 0x10(%rsp), %xmm0
xorps 0x11e23(%rip), %xmm0    # 0x360c0
movaps %xmm0, 0x10(%rsp)
movq %r14, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x24b1a
movapd 0x30(%rsp), %xmm0
movupd 0x58(%rbx), %xmm1
movapd %xmm0, %xmm2
minpd %xmm1, %xmm2
cmpunordpd %xmm1, %xmm1
movapd %xmm0, %xmm3
andpd %xmm1, %xmm3
andnpd %xmm2, %xmm1
orpd %xmm3, %xmm1
movupd %xmm1, 0x58(%rbx)
movupd 0x68(%rbx), %xmm1
movapd %xmm0, %xmm2
maxpd %xmm1, %xmm2
cmpunordpd %xmm1, %xmm1
andpd %xmm1, %xmm0
andnpd %xmm2, %xmm1
orpd %xmm0, %xmm1
movupd %xmm1, 0x68(%rbx)
movl $0x1, 0x9c(%rbx)
movq 0x18(%rbx), %rax
subq 0x10(%rbx), %rax
shrq $0x4, %rax
movl %eax, 0x8(%rbx)
jmp 0x24109
movq %rbx, %rdi
callq 0x2328a
movq %rbx, %rdi
callq 0x22da8
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq