// File: 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:
subq $0x108, %rsp            # imm = 0x108
movq %rdi, 0xf8(%rsp)
movq 0xf8(%rsp), %rax
movq %rax, 0x10(%rsp)
cmpl $0x3, 0x60(%rax)
jbe 0x9549
movq 0x10(%rsp), %rax
addq $0x30, %rax
movq %rax, 0xf0(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x12a70
movq %rax, 0xe8(%rsp)
movq 0xf0(%rsp), %rdi
callq 0x12aa0
movq %rax, 0xe0(%rsp)
leaq 0xe8(%rsp), %rdi
leaq 0xe0(%rsp), %rsi
callq 0x12ad0
testb $0x1, %al
jne 0x949f
jmp 0x9547
leaq 0xe8(%rsp), %rdi
callq 0x12b10
movq %rax, 0xd8(%rsp)
movq 0xd8(%rsp), %rax
testb $0x1, 0x74(%rax)
jne 0x9533
movq 0x10(%rsp), %rdx
movq 0xd8(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x65a0
movq 0x8(%rsp), %rdi
callq 0x20b0
movq %rax, %rsi
leaq 0x1d8e9(%rip), %rdi     # 0x26dde
xorl %eax, %eax
callq 0x2050
jmp 0x94fe
leaq 0xb8(%rsp), %rdi
callq 0x2100
jmp 0x9533
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x2100
jmp 0x98ea
jmp 0x9535
leaq 0xe8(%rsp), %rdi
callq 0x12b20
jmp 0x9481
jmp 0x9549
movq 0x10(%rsp), %rdi
addq $0x30, %rdi
callq 0x12b40
movq %rax, %rcx
movq 0x10(%rsp), %rax
movl %ecx, 0xa8(%rsp)
movl $0x0, 0xa4(%rsp)
addq $0x30, %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rdi
callq 0x12a70
movq %rax, 0x90(%rsp)
movq 0x98(%rsp), %rdi
callq 0x12aa0
movq %rax, 0x88(%rsp)
leaq 0x90(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x12ad0
testb $0x1, %al
jne 0x95c2
jmp 0x9607
leaq 0x90(%rsp), %rdi
callq 0x12b10
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rax
testb $0x1, 0x74(%rax)
je 0x95f6
movl 0xa4(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xa4(%rsp)
jmp 0x95f8
leaq 0x90(%rsp), %rdi
callq 0x12b20
jmp 0x95a7
movq 0x10(%rsp), %rdi
callq 0x14e70
testb $0x1, %al
jne 0x9617
jmp 0x9649
movq 0x10(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0x963c
movq 0x10(%rsp), %rdi
callq 0x14ec0
movl %eax, %esi
leaq 0x1d7b2(%rip), %rdi     # 0x26de7
movb $0x0, %al
callq 0x2050
movb $0x0, 0x107(%rsp)
jmp 0x98d9
movq 0x10(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0x966e
movq 0x10(%rsp), %rdi
callq 0x14ec0
movl %eax, %esi
leaq 0x1d7a3(%rip), %rdi     # 0x26e0a
movb $0x0, %al
callq 0x2050
movq 0x10(%rsp), %rdi
movl $0x3, %esi
callq 0xcb20
movq 0x10(%rsp), %rdi
xorl %esi, %esi
callq 0xcb20
movq 0x10(%rsp), %rdi
movl $0x2, %esi
callq 0xcb20
movq 0x10(%rsp), %rdi
movl $0x1, %esi
callq 0xcb20
movq 0x10(%rsp), %rax
movl $0x0, 0x7c(%rsp)
movl $0x0, 0x78(%rsp)
addq $0x30, %rax
movq %rax, 0x70(%rsp)
movq 0x70(%rsp), %rdi
callq 0x12a70
movq %rax, 0x68(%rsp)
movq 0x70(%rsp), %rdi
callq 0x12aa0
movq %rax, 0x60(%rsp)
leaq 0x68(%rsp), %rdi
leaq 0x60(%rsp), %rsi
callq 0x12ad0
testb $0x1, %al
jne 0x96f8
jmp 0x9738
leaq 0x68(%rsp), %rdi
callq 0x12b10
movq %rax, 0x58(%rsp)
movq 0x58(%rsp), %rax
testb $0x1, 0x74(%rax)
jne 0x971f
movl 0x78(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x78(%rsp)
jmp 0x972a
movl 0x7c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x7c(%rsp)
jmp 0x972c
leaq 0x68(%rsp), %rdi
callq 0x12b20
jmp 0x96e3
movq 0x10(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0x9760
movl 0x78(%rsp), %esi
movl 0xa8(%rsp), %edx
movl 0x7c(%rsp), %ecx
leaq 0x1d6c9(%rip), %rdi     # 0x26e22
movb $0x0, %al
callq 0x2050
cmpl $0x0, 0x78(%rsp)
jne 0x978d
movq 0x10(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0x9780
leaq 0x1d6cc(%rip), %rdi     # 0x26e45
movb $0x0, %al
callq 0x2050
movb $0x0, 0x107(%rsp)
jmp 0x98d9
movl 0x7c(%rsp), %eax
cmpl 0xa4(%rsp), %eax
jne 0x97c0
movq 0x10(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0x97b3
leaq 0x1d6b7(%rip), %rdi     # 0x26e63
movb $0x0, %al
callq 0x2050
movb $0x0, 0x107(%rsp)
jmp 0x98d9
movq 0x10(%rsp), %rdi
callq 0x14ee0
movq 0x10(%rsp), %rax
movl 0x148(%rax), %ecx
addl $0x1, %ecx
movl %ecx, 0x148(%rax)
addq $0x30, %rax
movq %rax, 0x50(%rsp)
movq 0x50(%rsp), %rdi
callq 0x12a70
movq %rax, 0x48(%rsp)
movq 0x50(%rsp), %rdi
callq 0x12aa0
movq %rax, 0x40(%rsp)
leaq 0x48(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x12ad0
testb $0x1, %al
jne 0x981d
jmp 0x98d1
leaq 0x48(%rsp), %rdi
callq 0x12b10
movq %rax, 0x38(%rsp)
movq 0x38(%rsp), %rax
testb $0x1, 0x74(%rax)
jne 0x984d
movq 0x10(%rsp), %rax
movl 0x148(%rax), %ecx
movq 0x38(%rsp), %rax
movl %ecx, 0x88(%rax)
movq 0x38(%rsp), %rax
cmpl $0x2, 0x78(%rax)
jbe 0x98c0
movq 0x10(%rsp), %rdx
movq 0x38(%rsp), %rsi
leaq 0x18(%rsp), %rdi
movq %rdi, (%rsp)
callq 0x65a0
movq (%rsp), %rdi
callq 0x20b0
movq %rax, %rsi
movq 0x38(%rsp), %rax
movl 0x78(%rax), %edx
leaq 0x1d5f8(%rip), %rdi     # 0x26e83
xorl %eax, %eax
callq 0x2050
jmp 0x9894
leaq 0x18(%rsp), %rdi
callq 0x2100
jmp 0x98c0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xb0(%rsp)
movl %eax, 0xac(%rsp)
leaq 0x18(%rsp), %rdi
callq 0x2100
jmp 0x98ea
jmp 0x98c2
leaq 0x48(%rsp), %rdi
callq 0x12b20
jmp 0x9805
movb $0x1, 0x107(%rsp)
movb 0x107(%rsp), %al
andb $0x1, %al
addq $0x108, %rsp            # imm = 0x108
retq
movq 0xb0(%rsp), %rdi
callq 0x2280
nopw (%rax,%rax)