Ejemplo n.º 1
0
TStrParam    StringUtil::split(const std::string& str, char ch)
{
    TStrParam vec;

    if( str.empty() )
        return vec;

    std::string::size_type startIdx(0);

    while(true)
    {
        std::string::size_type startIdy = str.find_first_of(ch, startIdx);

        if( startIdy == std::string::npos )
        {
	    if( str.substr( startIdx ).length() )
            	vec.push_back( str.substr( startIdx ) );
            break;
        }
        else
        {
            vec.push_back( str.substr( startIdx, startIdy -  startIdx) );
            startIdx = startIdy+1;
        }
    }

    return vec;
}
Ejemplo n.º 2
0
void GLWidget::TraceOneStep()
{
    if(_traceList.size() == 0)
    {
        _isTracingDone = false;

        AnIndex startIdx(0, 0);

        _traceList.push_back(startIdx); // put in list
        _cells[startIdx.x][startIdx.y]._isVisited = true; // mark
        _cells[startIdx.x][startIdx.y]._directionType = DirectionType::DIR_UPRIGHT; // (0,0) always upright or downleft

        _tilePainter->SetTiles(_cells, _traceList, _gridSpacing, _isTracingDone);
        this->repaint();
    }
    else if(!_isTracingDone)
    {
        AnIndex curIdx = _traceList[_traceList.size() - 1];
        _cells[curIdx.x][curIdx.y]._isVisited = true;

        DirectionType curDir =_cells[curIdx.x][curIdx.y]._directionType;

        AnIndex urIdx(curIdx.x + 1, curIdx.y - 1);    // up right
        AnIndex drIdx(curIdx.x + 1, curIdx.y + 1);    // down right
        AnIndex dlIdx(curIdx.x - 1, curIdx.y + 1);    // down left
        AnIndex ulIdx(curIdx.x - 1, curIdx.y - 1);    // up left

        AnIndex rIdx(curIdx.x + 1, curIdx.y    );     // right
        AnIndex dIdx(curIdx.x    , curIdx.y + 1);     // down
        AnIndex lIdx(curIdx.x - 1, curIdx.y    );     // left
        AnIndex uIdx(curIdx.x    , curIdx.y - 1);     // up

        // point-to-line intersection
        AVector endVec;
        if(curDir == DirectionType::DIR_UPRIGHT)
            { endVec = AVector(rIdx.x  * _gridSpacing, rIdx.y * _gridSpacing); }
        else if(curDir == DirectionType::DIR_DOWNRIGHT)
            { endVec = AVector(drIdx.x  * _gridSpacing, drIdx.y * _gridSpacing); }
        else if(curDir == DirectionType::DIR_DOWNLEFT)
            { endVec = AVector(dIdx.x  * _gridSpacing, dIdx.y * _gridSpacing); }
        else if(curDir == DirectionType::DIR_UPLEFT)
            { endVec = AVector(curIdx.x  * _gridSpacing, curIdx.y * _gridSpacing); }
        LineType hitType = GetLineIntersection(endVec);

        // enter
        if(curDir == DirectionType::DIR_RIGHT &&
           IsValid(rIdx) &&
           _cells[rIdx.x][rIdx.y]._straightness == Straightness::ST_HORIZONTAL)
        {
            //std::cout << "[a] " << "right --> right" << " - " << rIdx.x << " " << rIdx.y << "\n";
            _traceList.push_back(rIdx);
            _cells[rIdx.x][rIdx.y]._directionType = DirectionType::DIR_RIGHT;
            _cells[rIdx.x][rIdx.y]._tempDirection = _cells[curIdx.x][curIdx.y]._tempDirection;
        }
        else if(curDir == DirectionType::DIR_LEFT &&
                IsValid(lIdx) &&
                _cells[lIdx.x][lIdx.y]._straightness == Straightness::ST_HORIZONTAL)
        {
            //std::cout << "[b] " << "left --> left" << " - " << lIdx.x << " " << lIdx.y << "\n";
            _traceList.push_back(lIdx);
            _cells[lIdx.x][lIdx.y]._directionType = DirectionType::DIR_LEFT;
            _cells[lIdx.x][lIdx.y]._tempDirection = _cells[curIdx.x][curIdx.y]._tempDirection;
        }
        else if(curDir == DirectionType::DIR_UP &&
                IsValid(uIdx) &&
                _cells[uIdx.x][uIdx.y]._straightness == Straightness::ST_VERTICAL)
        {
            //std::cout << "[c] " << "up --> up" << " - " << uIdx.x << " " << uIdx.y << "\n";
            _traceList.push_back(uIdx);
            _cells[uIdx.x][uIdx.y]._directionType = DirectionType::DIR_UP;
            _cells[uIdx.x][uIdx.y]._tempDirection = _cells[curIdx.x][curIdx.y]._tempDirection;
        }
        else if(curDir == DirectionType::DIR_DOWN &&
                IsValid(dIdx) &&
                _cells[dIdx.x][dIdx.y]._straightness == Straightness::ST_VERTICAL)
        {
            //std::cout << "[d] " << "down --> down" << " - " << dIdx.x << " " << dIdx.y << "\n";
            _traceList.push_back(dIdx);
            _cells[dIdx.x][dIdx.y]._directionType = DirectionType::DIR_DOWN;
            _cells[dIdx.x][dIdx.y]._tempDirection = _cells[curIdx.x][curIdx.y]._tempDirection;
        }

        // out
        else if(curDir == DirectionType::DIR_RIGHT &&
           IsValid(rIdx) &&
           _cells[rIdx.x][rIdx.y]._straightness == Straightness::ST_DIAGONAL)
        {
            //std::cout << "[e] " << "right --> out" << " - " << rIdx.x << " " << rIdx.y << "\n";
            _traceList.push_back(rIdx);
            _cells[rIdx.x][rIdx.y]._directionType = _cells[curIdx.x][curIdx.y]._tempDirection;
        }
        else if(curDir == DirectionType::DIR_LEFT &&
                IsValid(lIdx) &&
                _cells[lIdx.x][lIdx.y]._straightness == Straightness::ST_DIAGONAL)
        {
            //std::cout << "[f] " << "left --> out" << " - " << lIdx.x << " " << lIdx.y << "\n";
            _traceList.push_back(lIdx);
            _cells[lIdx.x][lIdx.y]._directionType = _cells[curIdx.x][curIdx.y]._tempDirection;
        }
        else if(curDir == DirectionType::DIR_UP &&
                IsValid(uIdx) &&
                _cells[uIdx.x][uIdx.y]._straightness == Straightness::ST_DIAGONAL)
        {
            //std::cout << "[g] " << "up --> out" << " - " << uIdx.x << " " << uIdx.y << "\n";
            _traceList.push_back(uIdx);
            _cells[uIdx.x][uIdx.y]._directionType = _cells[curIdx.x][curIdx.y]._tempDirection;
        }
        else if(curDir == DirectionType::DIR_DOWN &&
                IsValid(dIdx) &&
                _cells[dIdx.x][dIdx.y]._straightness == Straightness::ST_DIAGONAL)
        {
            //std::cout << "[h] " << "down --> out" << " - " << dIdx.x << " " << dIdx.y << "\n";
            _traceList.push_back(dIdx);
            _cells[dIdx.x][dIdx.y]._directionType = _cells[curIdx.x][curIdx.y]._tempDirection;
        }

        else if(curDir == DirectionType::DIR_UPRIGHT &&
           hitType == LineType::LINE_HORIZONTAL &&
           IsValid(rIdx) &&
           _cells[rIdx.x][rIdx.y]._straightness == Straightness::ST_HORIZONTAL) // upright --> right
        {

            //std::cout << "[1] " << "upright --> right" << " - " << rIdx.x << " " << rIdx.y << "\n";
            // rIdx
            _traceList.push_back(rIdx);
            _cells[rIdx.x][rIdx.y]._directionType = DirectionType::DIR_RIGHT;
            _cells[rIdx.x][rIdx.y]._tempDirection = DirectionType::DIR_DOWNRIGHT;
        }
        else if(curDir == DirectionType::DIR_DOWNRIGHT  &&
                hitType == LineType::LINE_HORIZONTAL &&
                IsValid(rIdx) &&
                _cells[rIdx.x][rIdx.y]._straightness == Straightness::ST_HORIZONTAL) // downright --> right
        {
            //std::cout << "[2] " << "downright --> right" << " - " << rIdx.x << " " << rIdx.y << "\n";
            //std::cout << curDir << " " << hitType << " " << IsValid(rIdx) << " " << _cells[rIdx.x][rIdx.y]._straightness << "\n";
            // rIdx
            _traceList.push_back(rIdx);
            _cells[rIdx.x][rIdx.y]._directionType = DirectionType::DIR_RIGHT;
            _cells[rIdx.x][rIdx.y]._tempDirection = DirectionType::DIR_UPRIGHT;
        }
        else if(curDir == DirectionType::DIR_DOWNLEFT &&
                hitType == LineType::LINE_HORIZONTAL &&
                IsValid(lIdx) &&
                _cells[lIdx.x][lIdx.y]._straightness == Straightness::ST_HORIZONTAL) // downleft --> left
        {
            //std::cout << "[3] " << "downleft --> left" << " - " << lIdx.x << " " << lIdx.y << "\n";
            // lIdx
            _traceList.push_back(lIdx);
            _cells[lIdx.x][lIdx.y]._directionType = DirectionType::DIR_LEFT;
            _cells[lIdx.x][lIdx.y]._tempDirection = DirectionType::DIR_UPLEFT;
        }
        else if(curDir == DirectionType::DIR_UPLEFT &&
                hitType == LineType::LINE_HORIZONTAL &&
                IsValid(lIdx) &&
                _cells[lIdx.x][lIdx.y]._straightness == Straightness::ST_HORIZONTAL)    // upleft --> left
        {
            //std::cout << "[4] " << "// upleft --> left" << " - " << lIdx.x << " " << lIdx.y << "\n";
            // lIdx
            _traceList.push_back(lIdx);
            _cells[lIdx.x][lIdx.y]._directionType = DirectionType::DIR_LEFT;
            _cells[lIdx.x][lIdx.y]._tempDirection = DirectionType::DIR_DOWNLEFT;
        }

        else if(curDir == DirectionType::DIR_UPRIGHT &&
                hitType == LineType::LINE_VERTICAL &&
                IsValid(uIdx) &&
                _cells[uIdx.x][uIdx.y]._straightness == Straightness::ST_VERTICAL) // upright --> up
        {
            //std::cout << "[5] " << "upright --> up" << " - " << uIdx.x << " " << uIdx.y << "\n";
            // uIdx
            _traceList.push_back(uIdx);
            _cells[uIdx.x][uIdx.y]._directionType = DirectionType::DIR_UP;
            _cells[uIdx.x][uIdx.y]._tempDirection = DirectionType::DIR_UPLEFT;
        }
        else if(curDir == DirectionType::DIR_DOWNRIGHT  &&
                hitType == LineType::LINE_VERTICAL &&
                IsValid(dIdx) &&
                _cells[dIdx.x][dIdx.y]._straightness == Straightness::ST_VERTICAL) // downright --> down
        {
            //std::cout << "[6] " << "downright --> down" << " - " << dIdx.x << " " << dIdx.y << "\n";
            // dIdx
            _traceList.push_back(dIdx);
            _cells[dIdx.x][dIdx.y]._directionType = DirectionType::DIR_DOWN;
            _cells[dIdx.x][dIdx.y]._tempDirection = DirectionType::DIR_DOWNLEFT;
        }
        else if(curDir == DirectionType::DIR_DOWNLEFT &&
                hitType == LineType::LINE_VERTICAL &&
                IsValid(dIdx) &&
                _cells[dIdx.x][dIdx.y]._straightness == Straightness::ST_VERTICAL) // downleft --> down
        {
            //std::cout << "[7] " << "downleft --> down" << " - " << dIdx.x << " " << dIdx.y << "\n";
            // dIdx
            _traceList.push_back(dIdx);
            _cells[dIdx.x][dIdx.y]._directionType = DirectionType::DIR_DOWN;
            _cells[dIdx.x][dIdx.y]._tempDirection = DirectionType::DIR_DOWNRIGHT;
        }
        else if(curDir == DirectionType::DIR_UPLEFT &&
                hitType == LineType::LINE_VERTICAL &&
                IsValid(uIdx) &&
                _cells[uIdx.x][uIdx.y]._straightness == Straightness::ST_VERTICAL)    // upleft --> up
        {
            //std::cout << "[8] " << "upleft --> up" << " - " << uIdx.x << " " << uIdx.y << "\n";
            // uIdx
            _traceList.push_back(uIdx);
            _cells[uIdx.x][uIdx.y]._directionType = DirectionType::DIR_UP;
            _cells[uIdx.x][uIdx.y]._tempDirection = DirectionType::DIR_UPRIGHT;
        }

        // old code
        else if(curDir == DirectionType::DIR_UPRIGHT && IsValid(urIdx) && hitType == LineType::LINE_NONE)
        {
            //std::cout << "[9] " << hitType << " - " << urIdx.x << " " << urIdx.y << "\n";
            _traceList.push_back(urIdx);
            _cells[urIdx.x][urIdx.y]._directionType = DirectionType::DIR_UPRIGHT;
        }
        else if(curDir == DirectionType::DIR_DOWNRIGHT && IsValid(drIdx) && hitType == LineType::LINE_NONE)
        {
            //std::cout << "[10] " << hitType << " - " << drIdx.x << " " << drIdx.y << "\n";
            _traceList.push_back(drIdx);
            _cells[drIdx.x][drIdx.y]._directionType = DirectionType::DIR_DOWNRIGHT;
        }
        else if(curDir == DirectionType::DIR_DOWNLEFT && IsValid(dlIdx) && hitType == LineType::LINE_NONE)
        {
            //std::cout << "[11] " << hitType << " - " << dlIdx.x << " " << dlIdx.y << "\n";
            _traceList.push_back(dlIdx);
            _cells[dlIdx.x][dlIdx.y]._directionType = DirectionType::DIR_DOWNLEFT;
        }
        else if(curDir == DirectionType::DIR_UPLEFT && IsValid(ulIdx) && hitType == LineType::LINE_NONE)
        {
            //std::cout << "[12] " << hitType << " - " << ulIdx.x << " " << ulIdx.y << "\n";
            _traceList.push_back(ulIdx);
            _cells[ulIdx.x][ulIdx.y]._directionType = DirectionType::DIR_UPLEFT;
        }
        else if(curDir == DirectionType::DIR_UPRIGHT && ( IsValid(rIdx) || IsValid(uIdx) )  )
        {
            if(hitType == LineType::LINE_HORIZONTAL && IsValid(rIdx))
            {
                //std::cout << "[13] " << hitType << " - " << rIdx.x << " " << rIdx.y << "\n";
                _traceList.push_back(rIdx);
                _cells[rIdx.x][rIdx.y]._directionType = DirectionType::DIR_DOWNRIGHT;
            }
            else
            {
                //std::cout << "[14] " << hitType << " - " << uIdx.x << " " << uIdx.y << "\n";
                _traceList.push_back(uIdx);
                _cells[uIdx.x][uIdx.y]._directionType = DirectionType::DIR_UPLEFT;
            }
        }
        else if(curDir == DirectionType::DIR_DOWNRIGHT && ( IsValid(rIdx) || IsValid(dIdx) ))
        {
            if(hitType == LineType::LINE_HORIZONTAL && IsValid(rIdx))
            {
                //std::cout << "[15] " << hitType << " - " << rIdx.x << " " << rIdx.y << "\n";
                _traceList.push_back(rIdx);
                _cells[rIdx.x][rIdx.y]._directionType = DirectionType::DIR_UPRIGHT;
            }
            else
            {
                //std::cout << "[16] " << hitType << " - " << dIdx.x << " " << dIdx.y << "\n";
                //std::cout << curDir << " " << hitType << " " << IsValid(rIdx) << " " << _cells[rIdx.x][rIdx.y]._straightness << "\n";
                _traceList.push_back(dIdx);
                _cells[dIdx.x][dIdx.y]._directionType = DirectionType::DIR_DOWNLEFT;
            }
        }
        else if(curDir == DirectionType::DIR_DOWNLEFT && ( IsValid(lIdx) || IsValid(dIdx) ))
        {
            if(hitType == LineType::LINE_HORIZONTAL && IsValid(lIdx))   // up left
            {
                //std::cout << "[17] " << hitType << " - " << lIdx.x << " " << lIdx.y << "\n";
                _traceList.push_back(lIdx);
                _cells[lIdx.x][lIdx.y]._directionType = DirectionType::DIR_UPLEFT;
            }
            else        // down right
            {
                //std::cout << "[18] " << hitType << " - " << dIdx.x << " " << dIdx.y << "\n";
                _traceList.push_back(dIdx);
                _cells[dIdx.x][dIdx.y]._directionType = DirectionType::DIR_DOWNRIGHT;
            }
        }
        else if(curDir == DirectionType::DIR_UPLEFT && ( IsValid(lIdx) || IsValid(uIdx) ))
        {
            if(hitType == LineType::LINE_HORIZONTAL && IsValid(lIdx))
            {
                //std::cout << "[19] " << hitType << " - " << lIdx.x << " " << lIdx.y << "\n";
                _traceList.push_back(lIdx);
                _cells[lIdx.x][lIdx.y]._directionType = DirectionType::DIR_DOWNLEFT;
            }
            else
            {
                //std::cout << "[20] " << hitType << " - " << uIdx.x << " " << uIdx.y << "\n";
                _traceList.push_back(uIdx);
                _cells[uIdx.x][uIdx.y]._directionType = DirectionType::DIR_UPRIGHT;
            }
        }

        // check if we revisit a cell which means done
        AnIndex nextIdx = _traceList[_traceList.size() - 1];
        if(_cells[nextIdx.x][nextIdx.y]._isVisited)
        {
            //std::cout << "end here " << nextIdx.x << " " << nextIdx.y << "\n";
            _isTracingDone = true;
        }

        _tilePainter->SetTiles(_cells, _traceList, _gridSpacing, _isTracingDone);
        this->repaint();
    }
}