// File: realuptime[P]PolyDetector/PolyDetector.cpp
// Function: PolyCycle::pointConsumed(PolyDetector&, unsigned int) const
bool PolyCycle::pointConsumed(PolyDetector &pd, uint32_t pid) const
{
    auto &neigh = pd.pointToLines[pid];
    if (neigh.size() <= 1) // including self
    {
        if (pd.verbose)
            logoutf("pointConsumed(P%u) point with no neighbors! CAN be removed", pid);
        return true;
    }
    
    uint32_t lid1 = 0, lid2 = 0;
    uint32_t nValid = 0, nTaken = 0;
    for (auto &n : neigh)
    {
        auto l = pd.findLine(n);
        if (l)
        {
            nValid++;
            //if (l->took)
            if (l->took)
            {
#if 0
                //if (collinear)
                // if there is at least one collinear neighbor not taken, fail
                for (auto &nlid : _neighbors[l->id])
                {
                    auto ln = findLine(nlid);
                    //if (ln && ln->took < 2 && CollinearIdx(nlid, l->id))
                    if (ln && ln->took < 2)
                    {
                        return false;
                    }
                }
#endif
                if (nTaken == 0)
                    lid1 = l->id;
                else if (nTaken == 1)
                    lid2 = l->id;
                nTaken++;
            }
        }
    }
    
    //logoutf("P%u has nValid:%u taken:%u", pid, nValid, nTaken);
    
    if (nValid == 2 && nValid == nTaken)
    {
        //logoutf("l1:%u l2:%u", lid1, lid2);
        //if (collinear)
            //*collinear = CollinearIdx(lid1, lid2);
        if (pd.CollinearIdx(lid1, lid2))
            return false;
        
        // must be in the cycle!
        if (std::find(idx.begin(), idx.end(), lid1) == idx.end())
        {
            return false;
        }
        if (std::find(idx.begin(), idx.end(), lid2) == idx.end())
        {
            return false;
        }
        
        return true;
    }
    
    return false;
}

// Assembly:
subq $0xa8, %rsp
movq %rdi, 0x98(%rsp)
movq %rsi, 0x90(%rsp)
movl %edx, 0x8c(%rsp)
movq 0x98(%rsp), %rax
movq %rax, (%rsp)
movq 0x90(%rsp), %rdi
addq $0xe0, %rdi
leaq 0x8c(%rsp), %rsi
callq 0x12dd0
movq %rax, 0x80(%rsp)
movq 0x80(%rsp), %rdi
callq 0x12b80
cmpq $0x1, %rax
ja 0xd061
movq 0x90(%rsp), %rax
cmpl $0x0, 0x60(%rax)
je 0xd054
movl 0x8c(%rsp), %esi
leaq 0x19e56(%rip), %rdi     # 0x26ea3
movb $0x0, %al
callq 0x2050
movb $0x1, 0xa7(%rsp)
jmp 0xd26e
movl $0x0, 0x7c(%rsp)
movl $0x0, 0x78(%rsp)
movl $0x0, 0x74(%rsp)
movl $0x0, 0x70(%rsp)
movq 0x80(%rsp), %rax
movq %rax, 0x68(%rsp)
movq 0x68(%rsp), %rdi
callq 0x128b0
movq %rax, 0x60(%rsp)
movq 0x68(%rsp), %rdi
callq 0x128e0
movq %rax, 0x58(%rsp)
leaq 0x60(%rsp), %rdi
leaq 0x58(%rsp), %rsi
callq 0x134e0
testb $0x1, %al
jne 0xd0c4
jmp 0xd157
leaq 0x60(%rsp), %rdi
callq 0x13520
movq %rax, 0x50(%rsp)
movq 0x90(%rsp), %rdi
movq 0x50(%rsp), %rax
movl (%rax), %esi
movl $0x1, %edx
callq 0x7240
movq %rax, 0x48(%rsp)
cmpq $0x0, 0x48(%rsp)
je 0xd146
movl 0x74(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x74(%rsp)
movq 0x48(%rsp), %rax
cmpl $0x0, 0x78(%rax)
je 0xd144
cmpl $0x0, 0x70(%rsp)
jne 0xd124
movq 0x48(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x7c(%rsp)
jmp 0xd139
cmpl $0x1, 0x70(%rsp)
jne 0xd137
movq 0x48(%rsp), %rax
movl 0x60(%rax), %eax
movl %eax, 0x78(%rsp)
jmp 0xd139
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0xd146
jmp 0xd148
leaq 0x60(%rsp), %rdi
callq 0x13530
jmp 0xd0ac
cmpl $0x2, 0x74(%rsp)
jne 0xd266
movl 0x74(%rsp), %eax
cmpl 0x70(%rsp), %eax
jne 0xd266
movq 0x90(%rsp), %rdi
movl 0x7c(%rsp), %esi
movl 0x78(%rsp), %edx
callq 0xcee0
testb $0x1, %al
jne 0xd18b
jmp 0xd198
movb $0x0, 0xa7(%rsp)
jmp 0xd26e
movq (%rsp), %rdi
callq 0x13140
movq (%rsp), %rdi
movq %rax, 0x38(%rsp)
callq 0x13190
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rdi
movq 0x30(%rsp), %rsi
leaq 0x7c(%rsp), %rdx
callq 0x14f00
movq (%rsp), %rdi
movq %rax, 0x40(%rsp)
callq 0x13190
movq %rax, 0x28(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x142b0
testb $0x1, %al
jne 0xd1f0
jmp 0xd1fa
movb $0x0, 0xa7(%rsp)
jmp 0xd26e
movq (%rsp), %rdi
callq 0x13140
movq (%rsp), %rdi
movq %rax, 0x18(%rsp)
callq 0x13190
movq %rax, 0x10(%rsp)
movq 0x18(%rsp), %rdi
movq 0x10(%rsp), %rsi
leaq 0x78(%rsp), %rdx
callq 0x14f00
movq (%rsp), %rdi
movq %rax, 0x20(%rsp)
callq 0x13190
movq %rax, 0x8(%rsp)
leaq 0x20(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x142b0
testb $0x1, %al
jne 0xd252
jmp 0xd25c
movb $0x0, 0xa7(%rsp)
jmp 0xd26e
movb $0x1, 0xa7(%rsp)
jmp 0xd26e
movb $0x0, 0xa7(%rsp)
movb 0xa7(%rsp), %al
andb $0x1, %al
addq $0xa8, %rsp
retq
nop