// File: realuptime[P]PolyDetector/PolyDetector.cpp
// Function: PolyDetector::DetectPolygons()
bool PolyDetector::DetectPolygons()
{
    polys.clear();
    
    logoutf("verbose:%u", verbose);
    
    if (verbose)
        logoutf("origLines: %u", uint32_t(origLines.size()));

    if (!CreateLines())
    {
        logoutf1("Error: Could not successfully create lines!");
        return false;
    }
    if (verbose)
        logoutf("lines %u", uint32_t(lines.size()));
    
    while (true)
    {
        bool stop = false;
        
        if (verbose)
            logoutf("----------- BEGIN %u {", dissolveCount);
        
        SortLines();
        
        if (!FindPolys())
        {
            logoutf1("Error constructing the polygon set.");
            return false;
        }

        if (verbose)
            logoutf("Polygon set contains %d polygons.", GetPolyCount());
        
        SimplifyPolys(0.0);
        //logoutf("Polygon set contains %d polygons after simplification!.", GetPolyCount());
        
        auto beforeCount = dissolveCount;
        
        if (verbose)
            logoutf("----------- DISSOLVE %u", dissolveCount);
        
        if (!dissolve())
            stop = true;
        
        if (verbose)
            logoutf("} ----------- END %u\n", beforeCount);
        
        if (stop)
            break;
    }
    
#if 1
    //std::sort(polys.begin(), polys.end(), [](const PolyPol &a, const PolyPol &b) {
    //    return a.dissolveStep < b.dissolveStep;
    //});
    
    std::string str;
    
    for (auto &p : polys)
    {
        //if (verbose)
        //    p.cycle.print(("[FINI_ALL STEP:" + std::to_string(p.dissolveStep) + "] ").c_str());
        
        /*
        str = "[";
        for (auto &lid : p.cycle.idx)
        {
            auto l = findLine(lid);
            if (!l) continue;
            
            str += "{";
            for (auto &pid : {l->aIdx, l->bIdx})
                str += std::to_string(pid) + " ";
            str.pop_back();
            str += "} ";
        }
        if (!str.empty())
            str.pop_back();
        str += "]";
        
        logoutf("[%u]: pts:%s", p.dissolveStep, str.c_str());
        */
    }
#endif

    return true;
}

// Assembly:
subq $0x68, %rsp
movq %rdi, 0x58(%rsp)
movq 0x58(%rsp), %rdi
movq %rdi, 0x8(%rsp)
addq $0x48, %rdi
callq 0x13b50
movq 0x8(%rsp), %rax
movl 0x60(%rax), %esi
leaq 0x1eeac(%rip), %rdi     # 0x267d7
movb $0x0, %al
callq 0x2050
movq 0x8(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0x795b
movq 0x8(%rsp), %rdi
addq $0x18, %rdi
callq 0x12b40
movl %eax, %esi
leaq 0x1ee8f(%rip), %rdi     # 0x267e3
movb $0x0, %al
callq 0x2050
movq 0x8(%rsp), %rdi
callq 0x3010
testb $0x1, %al
jne 0x7981
leaq 0x1ee82(%rip), %rdi     # 0x267f2
movb $0x0, %al
callq 0x2050
movb $0x0, 0x67(%rsp)
jmp 0x7b18
movq 0x8(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0x79aa
movq 0x8(%rsp), %rdi
addq $0x30, %rdi
callq 0x12b40
movl %eax, %esi
leaq 0x1ee7c(%rip), %rdi     # 0x2681f
movb $0x0, %al
callq 0x2050
jmp 0x79ac
movq 0x8(%rsp), %rax
movb $0x0, 0x57(%rsp)
cmpl $0x0, 0x60(%rax)
je 0x79d5
movq 0x8(%rsp), %rax
movl 0x148(%rax), %esi
leaq 0x1ee5b(%rip), %rdi     # 0x26829
movb $0x0, %al
callq 0x2050
movq 0x8(%rsp), %rdi
callq 0x6e80
movq 0x8(%rsp), %rdi
callq 0x7b30
testb $0x1, %al
jne 0x7a05
leaq 0x1ee4d(%rip), %rdi     # 0x26841
movb $0x0, %al
callq 0x2050
movb $0x0, 0x67(%rsp)
jmp 0x7b18
movq 0x8(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0x7a2a
movq 0x8(%rsp), %rdi
callq 0x13b70
movl %eax, %esi
leaq 0x1ee43(%rip), %rdi     # 0x26866
movb $0x0, %al
callq 0x2050
movq 0x8(%rsp), %rdi
xorps %xmm0, %xmm0
callq 0x9330
movq 0x8(%rsp), %rax
movl 0x148(%rax), %ecx
movl %ecx, 0x50(%rsp)
cmpl $0x0, 0x60(%rax)
je 0x7a65
movq 0x8(%rsp), %rax
movl 0x148(%rax), %esi
leaq 0x1ee2b(%rip), %rdi     # 0x26889
movb $0x0, %al
callq 0x2050
movq 0x8(%rsp), %rdi
callq 0x9420
testb $0x1, %al
jne 0x7a78
movb $0x1, 0x57(%rsp)
movq 0x8(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0x7a95
movl 0x50(%rsp), %esi
leaq 0x1ee14(%rip), %rdi     # 0x268a2
movb $0x0, %al
callq 0x2050
testb $0x1, 0x57(%rsp)
je 0x7a9e
jmp 0x7aa3
jmp 0x79ac
leaq 0x30(%rsp), %rdi
callq 0x2220
movq 0x8(%rsp), %rax
addq $0x48, %rax
movq %rax, 0x28(%rsp)
movq 0x28(%rsp), %rdi
callq 0x13b90
movq %rax, 0x20(%rsp)
movq 0x28(%rsp), %rdi
callq 0x13bc0
movq %rax, 0x18(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x18(%rsp), %rsi
callq 0x13bf0
testb $0x1, %al
jne 0x7aee
jmp 0x7b09
leaq 0x20(%rsp), %rdi
callq 0x13c30
movq %rax, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x13c40
jmp 0x7ad9
movb $0x1, 0x67(%rsp)
leaq 0x30(%rsp), %rdi
callq 0x2100
movb 0x67(%rsp), %al
andb $0x1, %al
addq $0x68, %rsp
retq
nopw %cs:(%rax,%rax)