// File: realuptime[P]PolyDetector/PolyDetector.cpp
// Function: PolyDetector::RemoveOverlappings()
void PolyDetector::RemoveOverlappings()
{
    uint32_t i, j, count = uint32_t(origLines.size());
    
    //logoutf("RemoveOverlappings");
    
    for (auto &l : origLines)
    {
        l.calcCenter();
    }
    
    PolyLine line;

    // lets find overlapping lines
    uint32_t countBefore = count;
    for (i = 0; i < count; i++)
    {
        auto &line_i = origLines[i];

        for(j = i + 1; j < count; j++)
        {
            auto &line_j = origLines[j];
            if (overlap(line_i, line_j))
            {
                int ret = simplifiedLine(line_i, line_j, line);
                
                if (verbose > 3)
                    logoutf("origLine #%u {%f %f %f %f} overlaps with origLine #%u {%f %f %f %f} ret:%d",
                            line_i.id, line_i.a.x, line_i.a.y, line_i.b.x, line_i.b.y,
                            line_j.id, line_j.a.x, line_j.a.y, line_j.b.x, line_j.b.y,
                            ret);

                if (ret == 1)
                {
                    // must remove line_j
                    if (verbose > 1)
                        logoutf("rm origLine %u", line_j.id);
                    origLines.erase(origLines.begin() + j);
                    j--;
                    count--;
                    
                    //i = 0; break;
                }
                else
                {
                    if (ret != 2)
                    {
                        //line.id = uint32_t(origLines.size());
                        line.id = line_i.id;
                        
                        if (verbose)
                            logoutf("new origLine %u by merging %u and %u", line.id, line_i.id, line_j.id);
                        
                        // must remove both line_i and line_j and add a new one
                        origLines.erase(origLines.begin() + j);
                        
                        line.calcCenter();
                        origLines.push_back(std::move(line));
                    }

                    // must remove line_i
                    origLines.erase(origLines.begin() + i);

                    // update counters
                    i--;
                    count--;

                    // skip inner loop an go to next step of outer loop
                    break;
                }
            }
        }
    }
    
    if (countBefore != count)
        logoutf("orig: countBefore:%u count:%u", countBefore, count);
}

// Assembly:
subq $0x198, %rsp            # imm = 0x198
movq %rdi, 0x190(%rsp)
movq 0x190(%rsp), %rdi
movq %rdi, 0x40(%rsp)
addq $0x18, %rdi
callq 0x12b40
movq %rax, %rcx
movq 0x40(%rsp), %rax
movl %ecx, 0x184(%rsp)
addq $0x18, %rax
movq %rax, 0x178(%rsp)
movq 0x178(%rsp), %rdi
callq 0x12a70
movq %rax, 0x170(%rsp)
movq 0x178(%rsp), %rdi
callq 0x12aa0
movq %rax, 0x168(%rsp)
leaq 0x170(%rsp), %rdi
leaq 0x168(%rsp), %rsi
callq 0x12ad0
testb $0x1, %al
jne 0x3e35
jmp 0x3e66
leaq 0x170(%rsp), %rdi
callq 0x12b10
movq %rax, 0x160(%rsp)
movq 0x160(%rsp), %rdi
callq 0x2fb0
leaq 0x170(%rsp), %rdi
callq 0x12b20
jmp 0x3e1a
leaq 0xd0(%rsp), %rdi
callq 0x12910
movl 0x184(%rsp), %eax
movl %eax, 0xcc(%rsp)
movl $0x0, 0x18c(%rsp)
movl 0x18c(%rsp), %eax
cmpl 0x184(%rsp), %eax
jae 0x4271
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
movl 0x18c(%rsp), %eax
movl %eax, %esi
callq 0x12cf0
movq %rax, 0xc0(%rsp)
movl 0x18c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x188(%rsp)
movl 0x188(%rsp), %eax
cmpl 0x184(%rsp), %eax
jae 0x4259
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
movl 0x188(%rsp), %eax
movl %eax, %esi
callq 0x12cf0
movq %rax, 0xb8(%rsp)
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
callq 0x64b0
movb %al, 0x3f(%rsp)
jmp 0x3f1e
movb 0x3f(%rsp), %al
testb $0x1, %al
jne 0x3f2b
jmp 0x4241
movq 0xc0(%rsp), %rdi
movq 0xb8(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
callq 0x6c00
movl %eax, 0x38(%rsp)
jmp 0x3f4e
movq 0x40(%rsp), %rax
movl 0x38(%rsp), %ecx
movl %ecx, 0xa8(%rsp)
cmpl $0x3, 0x60(%rax)
jbe 0x4003
movq 0xc0(%rsp), %rax
movl 0x60(%rax), %esi
movss (%rax), %xmm0
movss 0x4(%rax), %xmm1
cvtss2sd %xmm0, %xmm0
cvtss2sd %xmm1, %xmm1
movss 0x8(%rax), %xmm2
cvtss2sd %xmm2, %xmm2
movss 0xc(%rax), %xmm3
cvtss2sd %xmm3, %xmm3
movq 0xb8(%rsp), %rax
movl 0x60(%rax), %edx
movss (%rax), %xmm4
movss 0x4(%rax), %xmm5
cvtss2sd %xmm4, %xmm4
cvtss2sd %xmm5, %xmm5
movss 0x8(%rax), %xmm6
cvtss2sd %xmm6, %xmm6
movss 0xc(%rax), %xmm7
cvtss2sd %xmm7, %xmm7
movl 0xa8(%rsp), %ecx
leaq 0x2234d(%rip), %rdi     # 0x2631f
movb $0x8, %al
callq 0x2050
jmp 0x3fdb
jmp 0x4003
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x12a40
jmp 0x42b6
cmpl $0x1, 0xa8(%rsp)
jne 0x40d4
movq 0x40(%rsp), %rax
cmpl $0x1, 0x60(%rax)
jbe 0x4039
movq 0xb8(%rsp), %rax
movl 0x60(%rax), %esi
leaq 0x2233d(%rip), %rdi     # 0x2636b
xorl %eax, %eax
callq 0x2050
jmp 0x4037
jmp 0x4039
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
movq %rdi, 0x28(%rsp)
callq 0x12a70
movq %rax, 0x90(%rsp)
movl 0x188(%rsp), %eax
movl %eax, %esi
leaq 0x90(%rsp), %rdi
callq 0x12fd0
movq %rax, 0x98(%rsp)
leaq 0xa0(%rsp), %rdi
leaq 0x98(%rsp), %rsi
callq 0x12d90
movq 0x28(%rsp), %rdi
movq 0xa0(%rsp), %rsi
callq 0x12d20
movq %rax, 0x30(%rsp)
jmp 0x40a0
movq 0x30(%rsp), %rax
movq %rax, 0x88(%rsp)
movl 0x188(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x188(%rsp)
movl 0x184(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x184(%rsp)
jmp 0x423f
cmpl $0x2, 0xa8(%rsp)
je 0x41bc
movq 0x40(%rsp), %rax
movq 0xc0(%rsp), %rcx
movl 0x60(%rcx), %ecx
movl %ecx, 0x130(%rsp)
cmpl $0x0, 0x60(%rax)
je 0x412e
movl 0x130(%rsp), %esi
movq 0xc0(%rsp), %rax
movl 0x60(%rax), %edx
movq 0xb8(%rsp), %rax
movl 0x60(%rax), %ecx
leaq 0x22258(%rip), %rdi     # 0x2637b
xorl %eax, %eax
callq 0x2050
jmp 0x412c
jmp 0x412e
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
movq %rdi, 0x18(%rsp)
callq 0x12a70
movq %rax, 0x70(%rsp)
movl 0x188(%rsp), %eax
movl %eax, %esi
leaq 0x70(%rsp), %rdi
callq 0x12fd0
movq %rax, 0x78(%rsp)
leaq 0x80(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x12d90
movq 0x18(%rsp), %rdi
movq 0x80(%rsp), %rsi
callq 0x12d20
movq %rax, 0x20(%rsp)
jmp 0x4189
movq 0x20(%rsp), %rax
movq %rax, 0x68(%rsp)
leaq 0xd0(%rsp), %rdi
callq 0x2fb0
jmp 0x41a2
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
leaq 0xd0(%rsp), %rsi
callq 0x12cc0
jmp 0x41ba
jmp 0x41bc
movq 0x40(%rsp), %rdi
addq $0x18, %rdi
movq %rdi, 0x8(%rsp)
callq 0x12a70
movq %rax, 0x50(%rsp)
movl 0x18c(%rsp), %eax
movl %eax, %esi
leaq 0x50(%rsp), %rdi
callq 0x12fd0
movq %rax, 0x58(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x12d90
movq 0x8(%rsp), %rdi
movq 0x60(%rsp), %rsi
callq 0x12d20
movq %rax, 0x10(%rsp)
jmp 0x4211
movq 0x10(%rsp), %rax
movq %rax, 0x48(%rsp)
movl 0x18c(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x18c(%rsp)
movl 0x184(%rsp), %eax
addl $-0x1, %eax
movl %eax, 0x184(%rsp)
jmp 0x4259
jmp 0x4241
jmp 0x4243
movl 0x188(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x188(%rsp)
jmp 0x3ed0
jmp 0x425b
movl 0x18c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x18c(%rsp)
jmp 0x3e8c
movl 0xcc(%rsp), %eax
cmpl 0x184(%rsp), %eax
je 0x42a1
movl 0xcc(%rsp), %esi
movl 0x184(%rsp), %edx
leaq 0x2210b(%rip), %rdi     # 0x263a1
xorl %eax, %eax
callq 0x2050
jmp 0x429f
jmp 0x42a1
leaq 0xd0(%rsp), %rdi
callq 0x12a40
addq $0x198, %rsp            # imm = 0x198
retq
movq 0xb0(%rsp), %rdi
callq 0x2280
nopw %cs:(%rax,%rax)