// File: /workspace/github_cpp_makefile_star2_O1/realuptime[P]PolyDetector/PolyDetector.cpp
// Function: PolyDetector::dissolve()
bool PolyDetector::dissolve()
{
    //return false; // test
    //if (dissolveCount == 1) return false;
    //if (dissolveCount == 1) return false;
    
    if (verbose > 3)
    {
        for (auto &l : lines)
        {
            if (!l.ignore)
            {
                logoutf("DIS: %s", l.toString(*this).c_str());
            }
        }
    }
    
    uint32_t nLinesBefore = uint32_t(lines.size());
    uint32_t nIgnoredBefore = 0;
    for (auto &l : lines)
        if (l.ignore)
            nIgnoredBefore++;
    
    if (_cycles.empty())
    {
        if (verbose)
            logoutf("No need to dissolve for %u cycles", uint32_t(_cycles.size()));
        return false;
    }
    
    if (verbose)
        logoutf("Dissolve for %u cycles", uint32_t(_cycles.size()));
    
    rmLines(RmLinesType::PointConsumed);
    rmLines(RmLinesType::TakenTwice); // points taken twice
    rmLines(RmLinesType::NoPointNeigh);
    rmLines(RmLinesType::Collinear);
    
#if 0
    return false; // test
#endif
    
    uint32_t nIgnored = 0;
    uint32_t nValid = 0;
    for (auto &l : lines)
    {
        if (!l.ignore)
        {
            nValid++;
        }
        else
        {
            nIgnored++;
        }
    }
    if (verbose)
        logoutf("Remaining lines:%u/%u nIgnored:%u", nValid, nLinesBefore, nIgnored);
    
    if (!nValid)
    {
        if (verbose)
            logoutf1("No valid lines. Skipping ...");
        return false;
    }
    
    if (nIgnored == nIgnoredBefore)
    {
        if (verbose)
            logoutf1("No lines ignored. Skipping ...");
        return false;
    }

#if 0
    // reassign new lines
    LineVector newLines;
    newLines.reserve(lines.size());
    for (auto it = lines.begin(); it != lines.end(); ++it)
    {
        if (!it->ignore)
        {
            newLines.push_back(std::move(*it));
        }
    }
    lines = std::move(newLines);
    
    logoutf("Remaining lines:%u/%u (after filter)", uint32_t(lines.size()), nLinesBefore);
#endif
    
    //logoutf("intersectionPoints:%u", uint32_t(intersectionPoints.size())); // should remain the same
    // also clear prev cycles
    _cycles.clear();
        
#if 0 // test
    polys.clear();
#endif
    
    dissolveCount++;
    
    for (auto &l : lines)
    {
        if (!l.ignore)
            l.lastDissolveStep = dissolveCount;
        //l.took = 0;
        if (l.took > 2)
        {
            logoutf("WARN: line %s tooked %u times!", l.toString(*this).c_str(), l.took);
            //assert(false);
            //l.took = 2;
        }
    }
    
    return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdi, %rbx
cmpl $0x4, 0x60(%rdi)
jb 0x6b46
movq 0x30(%rbx), %r14
movq 0x38(%rbx), %r13
cmpq %r13, %r14
je 0x6b46
leaq 0x18(%rsp), %rbp
leaq 0x8(%rsp), %r15
leaq 0xa28e(%rip), %r12      # 0x10d95
cmpb $0x0, 0x74(%r14)
jne 0x6b3a
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4d08
movq 0x8(%rsp), %rsi
movq %r12, %rdi
xorl %eax, %eax
callq 0x2030
movq 0x8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x6b3a
callq 0x2140
addq $0x90, %r14
cmpq %r13, %r14
jne 0x6b07
movq 0x30(%rbx), %rax
movq 0x38(%rbx), %rcx
movq %rcx, %r12
xorl %r15d, %r15d
subq %rax, %r12
je 0x6b6b
movzbl 0x74(%rax), %edx
addl %edx, %r15d
addq $0x90, %rax
cmpq %rcx, %rax
jne 0x6b59
movq (%rbx), %rax
movq 0x8(%rbx), %rsi
movl 0x60(%rbx), %ecx
cmpq %rsi, %rax
je 0x6c35
testl %ecx, %ecx
je 0x6b97
subq %rax, %rsi
shrq $0x6, %rsi
leaq 0xa231(%rip), %rdi      # 0x10dc1
xorl %eax, %eax
callq 0x2030
movq %rbx, %rdi
movl $0x3, %esi
callq 0x8ca8
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x8ca8
movq %rbx, %rdi
movl $0x2, %esi
callq 0x8ca8
movq %rbx, %rdi
movl $0x1, %esi
callq 0x8ca8
movq 0x30(%rbx), %rax
movq 0x38(%rbx), %rcx
movl $0x0, %r14d
cmpq %rcx, %rax
je 0x6bf9
xorl %r14d, %r14d
xorl %ebp, %ebp
movzbl 0x74(%rax), %edx
addl %edx, %r14d
xorl $0x1, %edx
addl %edx, %ebp
addq $0x90, %rax
cmpq %rcx, %rax
jne 0x6be2
cmpl $0x0, 0x60(%rbx)
je 0x6c1d
shrq $0x4, %r12
imull $0x38e38e39, %r12d, %edx # imm = 0x38E38E39
leaq 0xa1c8(%rip), %rdi      # 0x10dd9
movl %ebp, %esi
movl %r14d, %ecx
xorl %eax, %eax
callq 0x2030
testl %ebp, %ebp
je 0x6c52
cmpl %r15d, %r14d
jne 0x6c77
cmpl $0x0, 0x60(%rbx)
je 0x6c64
leaq 0xa3da(%rip), %rdi      # 0x1100d
jmp 0x6c5f
testl %ecx, %ecx
je 0x6c64
subq %rax, %rsi
shrq $0x6, %rsi
leaq 0xa157(%rip), %rdi      # 0x10d9e
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x2030
jmp 0x6c66
cmpl $0x0, 0x60(%rbx)
je 0x6c64
leaq 0xa391(%rip), %rdi      # 0x10ff0
callq 0x21d0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq (%rbx), %r15
movq 0x8(%rbx), %r12
cmpq %r15, %r12
je 0x6c9b
movq %r15, %r14
movq %r14, %rdi
callq 0xc16e
addq $0x40, %r14
cmpq %r12, %r14
jne 0x6c86
movq %r15, 0x8(%rbx)
incl 0x148(%rbx)
movq 0x30(%rbx), %r14
movq 0x38(%rbx), %r13
movb $0x1, %bpl
cmpq %r13, %r14
je 0x6c66
leaq 0x8(%rsp), %r15
leaq 0xa13f(%rip), %r12      # 0x10dfc
cmpb $0x0, 0x74(%r14)
jne 0x6cd1
movl 0x148(%rbx), %eax
movl %eax, 0x88(%r14)
cmpl $0x3, 0x78(%r14)
jb 0x6d0d
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4d08
movq 0x8(%rsp), %rsi
movl 0x78(%r14), %edx
movq %r12, %rdi
xorl %eax, %eax
callq 0x2030
movq 0x8(%rsp), %rdi
leaq 0x18(%rsp), %rax
cmpq %rax, %rdi
je 0x6d0d
callq 0x2140
addq $0x90, %r14
cmpq %r13, %r14
jne 0x6cbd
jmp 0x6c66