-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scenario.cpp
161 lines (139 loc) · 7.44 KB
/
Scenario.cpp
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
#include <Scenario.h>
#include <QDataStream>
#include <QFileDialog>
// checks to see if the scenario is valid - IE all of the appropriate points have been written to
bool scenarioIsValid(scenario scen) {
bool returnMe = true;
returnMe = returnMe && (scen.name != QString());
returnMe = returnMe && (scen.timeUnit != QString());
returnMe = returnMe && (scen.lengthUnit != QString());
returnMe = returnMe && (scen.massUnit != QString());
returnMe = returnMe && (scen.bigG > 0);
returnMe = returnMe && (scen.dt > 0);
returnMe = returnMe && ((scen.endTime > scen.dt) || scen.endTime == -1);
returnMe = returnMe && (scen.planets.length() != 0);
returnMe = returnMe && (scen.iterationsPerDataPoint > 0);
return returnMe;
}
// resets a scenario to defaults
scenario defaultScenario() {
scenario newScenario;
return newScenario;
}
Point3D stringToPoint3D(QString string) {
Point3D point(-1234, -1234, -1234);
QStringList individualStrings = string.split(",");
if (individualStrings.length() == 3) {
point.setX(individualStrings.at(0).toDouble());
point.setY(individualStrings.at(1).toDouble());
point.setZ(individualStrings.at(2).toDouble());
}
return point;
}
bool writeScenariosV1(QVector<scenario> scens, QString filename) {
QFile file(filename);
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream outputToFile;
outputToFile.setRealNumberPrecision(16);
outputToFile.setDevice(&file);
outputToFile << "Scenario v0.1\n";
for (int i = 0; i < scens.length(); i++) {
outputToFile << "name " << scens.at(i).name << "\n";
outputToFile << "bigG " << static_cast<double>(scens.at(i).bigG) << "\n";
outputToFile << "dt " << static_cast<double>(log2(scens.at(i).dt)) << "\n";
outputToFile << "iterationsPerDataPoint " << static_cast<double>(log2(scens.at(i).iterationsPerDataPoint)) << "\n";
outputToFile << "endTime " << static_cast<double>(scens.at(i).endTime) << "\n";
outputToFile << "lengthUnit " << scens.at(i).lengthUnit << "\n";
outputToFile << "massUnit " << scens.at(i).massUnit << "\n";
outputToFile << "timeUnit " << scens.at(i).timeUnit << "\n";
for (int j = 0; j < scens.at(i).planets.length(); j++) {
outputToFile << "planet\n";
outputToFile << " position " << scens.at(i).planets.at(j).getPosition().toString() << "\n";
outputToFile << " velocity " << scens.at(i).planets.at(j).getVelocity().toString() << "\n";
outputToFile << " mass " << static_cast<double>(scens.at(i).planets.at(j).getMass()) << "\n";
outputToFile << " radius " << static_cast<double>(scens.at(i).planets.at(j).getRadius()) << "\n";
outputToFile << " name " << scens.at(i).planets.at(j).getName() << "\n";
outputToFile << " color " << Point3D(scens.at(i).planets.at(j).getColor().red(), scens.at(i).planets.at(j).getColor().green(), scens.at(i).planets.at(j).getColor().blue()).toString() << "\n";
}
outputToFile << "\n";
}
file.close();
return true;
}
return false;
}
bool readScenariosV1(QVector<scenario> * scenarios, QString filename) {
QFile file(filename);
QString errorMessage;
QString errorLine;
if (file.open(QFile::ReadOnly)) {
QString temp;
QTextStream inputFromFile;
inputFromFile.setRealNumberPrecision(16);
inputFromFile.setDevice(&file);
temp = inputFromFile.readLine();
if (temp != QString("Scenario v0.1")) {errorMessage = "Wrong Scenario Version!";}
scenario newScen;
Object newPlanet;
while (!inputFromFile.atEnd() && errorMessage.isNull()) {
newScen = defaultScenario();
temp = inputFromFile.readLine();
if (temp.indexOf("name ") != 0) {errorMessage = "Name incorrect!"; errorLine = temp;}
else newScen.name = temp.mid(5);
temp = inputFromFile.readLine();
if (temp.indexOf("bigG ") != 0) {errorMessage = "bigG incorrect!"; errorLine = temp;}
else newScen.bigG = temp.midRef(5).toDouble();
temp = inputFromFile.readLine();
if (temp.indexOf("dt ") != 0) {errorMessage = "dt incorrect!"; errorLine = temp;}
else newScen.dt = powl(2.0, temp.midRef(3).toDouble());
temp = inputFromFile.readLine();
if (temp.indexOf("iterationsPerDataPoint ") != 0) {errorMessage = "iterationsPerDataPoint incorrect!"; errorLine = temp;}
else newScen.iterationsPerDataPoint = powl(2.0, temp.midRef(23).toDouble());
temp = inputFromFile.readLine();
if (temp.indexOf("endTime ") != 0) {errorMessage = "endTime incorrect!"; errorLine = temp;}
else newScen.endTime = temp.midRef(8).toDouble();
temp = inputFromFile.readLine();
if (temp.indexOf("lengthUnit ") != 0) {errorMessage = "lengthUnit incorrect!"; errorLine = temp;}
else newScen.lengthUnit = temp.mid(11);
temp = inputFromFile.readLine();
if (temp.indexOf("massUnit ") != 0) {errorMessage = "massUnit incorrect!"; errorLine = temp;}
else newScen.massUnit = temp.mid(9);
temp = inputFromFile.readLine();
if (temp.indexOf("timeUnit ") != 0) {errorMessage = "timeUnit incorrect!"; errorLine = temp;}
else newScen.timeUnit = temp.mid(9);
while (inputFromFile.readLine() == "planet" && errorMessage.isNull()) {
newPlanet = Object();
temp = inputFromFile.readLine();
if (temp.indexOf(" position ") != 0) {errorMessage = "position incorrect!"; errorLine = temp;}
else newPlanet.setPosition(stringToPoint3D(temp.mid(13)));
temp = inputFromFile.readLine();
if (temp.indexOf(" velocity ") != 0) {errorMessage = "velocity incorrect!"; errorLine = temp;}
else newPlanet.setVelocity(stringToPoint3D(temp.mid(13)));
temp = inputFromFile.readLine();
if (temp.indexOf(" mass ") != 0) {errorMessage = "mass incorrect!"; errorLine = temp;}
else newPlanet.setMass(temp.midRef(9).toDouble());
temp = inputFromFile.readLine();
if (temp.indexOf(" radius ") != 0) {errorMessage = "radius incorrect!"; errorLine = temp;}
else newPlanet.setRadius(temp.midRef(11).toDouble());
temp = inputFromFile.readLine();
if (temp.indexOf(" name ") != 0) {errorMessage = "name incorrect!"; errorLine = temp;}
else newPlanet.setName(temp.mid(9));
temp = inputFromFile.readLine();
if (temp.indexOf(" color ") != 0) {errorMessage = "color incorrect!"; errorLine = temp;}
else {
Point3D colorPoint = stringToPoint3D(temp.mid(10));
newPlanet.setColor(QColor(colorPoint.x(), colorPoint.y(), colorPoint.z(), 255));
}
newScen.planets.append(newPlanet);
}
scenarios->append(newScen);
}
file.close();
} else errorMessage = "Failed to open the file";
if (errorMessage.isNull()) return true;
else {
qDebug() << "message:" << errorMessage;
qDebug() << "line:" << errorLine;
return false;
}
}