~masgk3team/masgk3/roads

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "LSystemGenerator.hpp"
#include "RoadJunctionPoint.hpp"
#include "Road.hpp"
//-----//
#include <cmath>
#include <QImage>
//-----//
#include <QDebug>
//-----//
Utils::Junctions LSystemGenerator::GenerateRoads(const ConfigRoads &c, const QImage *inputImage)
{
    const ConfigLSystemRoads conf = *((ConfigLSystemRoads*)&c);
    const QSize mapSize = inputImage->size();

    //QVector2D tmpVec((float)rand()/RAND_MAX, (float)rand()/RAND_MAX);
    QVector2D tmpVec(1, 0);
    QList<LSystemState> aStack;

    int drawLength = 8,
        drawLength2 = 6,
        rotationAngle = 90,
        rotationAngle2 = 5; // rand(+-x)

    LSystemState currentState(QVector2D(mapSize.width() / 2, mapSize.height() / 2),
                              tmpVec.normalized(),
                              drawLength);


    QString currentWord = conf.system.sInitialWord;
    RoadJunctionPoint *rjp = new RoadJunctionPoint(currentState.position);
    junctions.push_back(rjp);

    for (int i = 0; i < conf.system.iIterations; ++i)
    {
        QString newWord = "";
        for (int j = 0; j < currentWord.length(); ++j)
        {
            if (conf.system.aRules.contains(currentWord.at(j)))
            {
                newWord += conf.system.aRules[currentWord.at(j)];
            }
            else
                newWord += currentWord.at(j);
        }
        currentWord = newWord;
    }

    for (int j = 0; j < currentWord.length(); ++j)
    {
        switch (currentWord.at(j).toAscii())
        {
        case 'X':
            currentState.drawLength = drawLength2;
            CreateRoad(rjp, currentState, inputImage);
            break;
        case 'F':
            currentState.drawLength = drawLength;
            CreateRoad(rjp, currentState, inputImage);
            break;
        case '+':
            currentState.direction = RotateVectorByAngle(currentState.direction, -rotationAngle);
            break;
        case '-':
            currentState.direction = RotateVectorByAngle(currentState.direction,  rotationAngle);
            break;
        case 'R':
            currentState.direction = RotateVectorByAngle(currentState.direction, (rand() % (rotationAngle2 * 2 + 1) - rotationAngle2) );
            break;
        case '[':
            aStack.push_back(currentState);
            break;
        case ']':
            currentState = aStack.last();
            aStack.pop_back();
            break;
        }
    }

    return junctions;
}
//-----//
void LSystemGenerator::CreateRoad(RoadJunctionPoint *rjp, LSystemState &state, const QImage *map)
{
    int heightAngles[] = { -30, -22, -15, -8, 0, 8, 15, 22, 30 };
    //int heightAngles[] = { 0 };
    int num = 9;

    Road *r;

    Road *lowestSlopeRoad = NULL;
    int slope = INT_MAX;
    for (int i = 0; i < num; ++i)
    {
        QVector2D rotation = RotateVectorByAngle(state.direction, heightAngles[i]);
        QVector2D newPosition =
            Clamp(QVector2D(state.position.x() + state.drawLength * rotation.x(),
                            state.position.y() + state.drawLength * rotation.y()),
                  map->size());
        r = new Road(state.position, newPosition);
        if (ValidPoint(map, newPosition) && r->Valid(map))
        {
            int heightDiff = abs(qRed(map->pixel(state.position.toPoint())) - qRed(map->pixel(newPosition.toPoint())));
            if (heightDiff < slope)
            {
                slope = heightDiff;
                if (lowestSlopeRoad)
                    delete lowestSlopeRoad;
                lowestSlopeRoad = r;
            }
            else
            {
                delete r;
            }
        }
        else
            delete r;

    }

    if (lowestSlopeRoad)
    {
        rjp->AddRoad(lowestSlopeRoad);
        state.position = lowestSlopeRoad->End();
    }
}
//-----//
bool LSystemGenerator::JunctionExists(const QVector2D &v) const
{
    unsigned num = 0;
    float epsilon = 2.0f;
    RoadJunctionPoint *rjp = junctions.first();
    //for (Utils::Junctions::const_iterator iter = junctions.begin(); iter != junctions.end(); ++iter)
    {
        for (Utils::Roads::const_iterator iter = rjp->StartingRoads().begin(); iter != rjp->StartingRoads().end(); ++iter)
        {
            QVector2D p1 = (*iter)->Start();
            QVector2D p2 = (*iter)->End();
            if ((fabs(p1.x() - v.x()) < epsilon && fabs(p1.y() - v.y()) < epsilon) ||
                (fabs(p2.x() - v.x()) < epsilon && fabs(p2.y() - v.y()) < epsilon))
            {
                ++num;
            }
        }
    }
    return num > 5;
}
//-----//
void LSystemGenerator::FilterRoads()
{

}
//-----//
QVector2D LSystemGenerator::RotateVectorByAngle(const QVector2D &vec, int angle)
{
    float tmpSin = sin((float)angle * M_PI / 180);
    float tmpCos = cos((float)angle * M_PI / 180);
    float tmpPosX = vec.x() * tmpCos - vec.y() * tmpSin;
    float tmpPosY = vec.x() * tmpSin + vec.y() * tmpCos;
    return QVector2D(tmpPosX, tmpPosY).normalized();
}
//-----//
QVector2D LSystemGenerator::Clamp(const QVector2D pt, const QSize &bounds) const
{
	return QVector2D(qBound(0.0, pt.x(), (qreal)bounds.width()),
					 qBound(0.0, pt.y(), (qreal)bounds.height()));
}
//-----//
bool LSystemGenerator::ValidPoint(const QImage *i, const QVector2D &p) const
{
    return i->rect().contains(p.toPoint()) &&
           qGray(i->pixel(p.toPoint())) > Utils::LEVEL_WATER
          ;//&& !JunctionExists(p);

}