// File: /workspace/github_cpp_makefile_star2_O2/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 0x567b
movq 0x30(%rbx), %r14
movq 0x38(%rbx), %r13
leaq 0x8(%rsp), %r15
leaq 0x8738(%rip), %r12      # 0xdd79
cmpq %r13, %r14
je 0x567b
cmpb $0x0, 0x74(%r14)
jne 0x5672
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4208
movq 0x8(%rsp), %rsi
movq %r12, %rdi
xorl %eax, %eax
callq 0x2040
movq %r15, %rdi
callq 0x20b0
addq $0x90, %r14
jmp 0x5641
movq 0x30(%rbx), %rcx
movq 0x38(%rbx), %rsi
movq %rsi, %rax
subq %rcx, %rax
movl $0x90, %edi
cqto
idivq %rdi
movq %rax, %r14
xorl %r12d, %r12d
cmpq %rsi, %rcx
je 0x56ae
movzbl 0x74(%rcx), %eax
addl %eax, %r12d
addq $0x90, %rcx
jmp 0x5699
movq (%rbx), %rax
movq 0x8(%rbx), %rsi
movl 0x60(%rbx), %ecx
cmpq %rsi, %rax
je 0x575f
testl %ecx, %ecx
je 0x56da
subq %rax, %rsi
shrq $0x6, %rsi
leaq 0x86d2(%rip), %rdi      # 0xdda5
xorl %eax, %eax
callq 0x2040
pushq $0x3
popq %rsi
movq %rbx, %rdi
callq 0x6f08
xorl %ebp, %ebp
movq %rbx, %rdi
xorl %esi, %esi
callq 0x6f08
pushq $0x2
popq %rsi
movq %rbx, %rdi
callq 0x6f08
pushq $0x1
popq %rsi
movq %rbx, %rdi
callq 0x6f08
movq 0x30(%rbx), %rax
movq 0x38(%rbx), %rcx
xorl %r15d, %r15d
cmpq %rcx, %rax
je 0x572b
movzbl 0x74(%rax), %edx
addl %edx, %r15d
xorl $0x1, %edx
addl %edx, %ebp
addq $0x90, %rax
jmp 0x5712
cmpl $0x0, 0x60(%rbx)
je 0x5747
leaq 0x8685(%rip), %rdi      # 0xddbd
movl %ebp, %esi
movl %r14d, %edx
movl %r15d, %ecx
xorl %eax, %eax
callq 0x2040
testl %ebp, %ebp
je 0x577a
cmpl %r12d, %r15d
jne 0x5790
cmpl $0x0, 0x60(%rbx)
je 0x578c
leaq 0x886a(%rip), %rdi      # 0xdfc7
jmp 0x5787
testl %ecx, %ecx
je 0x578c
leaq 0x8618(%rip), %rdi      # 0xdd82
xorl %ebx, %ebx
xorl %esi, %esi
xorl %eax, %eax
callq 0x2040
jmp 0x5806
cmpl $0x0, 0x60(%rbx)
je 0x578c
leaq 0x8823(%rip), %rdi      # 0xdfaa
callq 0x21c0
xorl %ebx, %ebx
jmp 0x5806
movq %rbx, %rdi
callq 0x8d76
incl 0x148(%rbx)
movq 0x30(%rbx), %r14
movq 0x38(%rbx), %r13
leaq 0x8(%rsp), %r15
leaq 0x862e(%rip), %r12      # 0xdde0
cmpq %r13, %r14
je 0x5804
cmpb $0x0, 0x74(%r14)
jne 0x57cb
movl 0x148(%rbx), %eax
movl %eax, 0x88(%r14)
cmpl $0x3, 0x78(%r14)
jb 0x57fb
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x4208
movq 0x8(%rsp), %rsi
movl 0x78(%r14), %edx
movq %r12, %rdi
xorl %eax, %eax
callq 0x2040
movq %r15, %rdi
callq 0x20b0
addq $0x90, %r14
jmp 0x57b2
movb $0x1, %bl
movl %ebx, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop