/
datagen.cpp
444 lines (426 loc) · 15.1 KB
/
datagen.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <cmath>
#include "datagen.h"
#include "helpers.cpp"
#include "Simulation.h"
#include "Object.h"
#include "Color.h"
using namespace std;
//FUNCTIONS NOT BELONGING TO ANY CLASS
//Function to take in .csv data
Simulation* inCsv(string filename, bool debug) {
ifstream inputFile(filename.c_str());
int xRes;
int yRes;
int numObjects;
double pixelSize;
string row;
string col;
double timeInterval;
int numIterations;
//File input for x and y dimensions of window as well as number of objects
try {
//Ensures row is present
if (!(inputFile >> row)) {
string e = "Row appears to be missing: 0";
throw e;
}
else {
istringstream ss(row);
//Checks xRes
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: xRes (0, 0)";
throw e;
}
else {
istringstream stoi(col);
if (!(stoi >> xRes)) {
string e = "xRes value not properly formatted: (0, 0)";
throw e;
}
}
//Checks yRes
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: yRes (0, 1)";
throw e;
}
else {
istringstream stoi(col);
if (!(stoi >> yRes)) {
string e = "yRes value not properly formatted: (0, 1)";
throw e;
}
}
//Checks numObjects
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: numObjects (0, 2)";
throw e;
}
else {
istringstream stoi(col);
if (!(stoi >> numObjects)) {
string e = "numObjects value not properly formatted: (0, 2)";
throw e;
}
}
//Checks pixelSize
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: pixelSize (0, 3)";
throw e;
}
else {
istringstream stoi(col);
if (!(stoi >> pixelSize)) {
string e = "pixelSize value not properly formatted: (0, 3)";
throw e;
}
}
//Checks timeInterval
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: timeInterval (0, 4)";
throw e;
}
else {
istringstream stoi(col);
if (!(stoi >> timeInterval)) {
string e = "timeInterval value not properly formatted: (0, 4)";
throw e;
}
}
//Checks numIterations
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: numIterations (0, 5)";
throw e;
}
else {
istringstream stoi(col);
if (!(stoi >> numIterations)) {
string e = "numIterations value not properly formatted: (0, 5)";
throw e;
}
}
}
}
catch (string exception) {
cout << exception << endl;
exit(1);
}
if (debug) {
cout << "xRes: " << xRes << endl;
cout << "yRes: " << yRes << endl;
cout << "numObjects: " << numObjects << endl;
cout << "pixelSize: " << pixelSize << endl;
cout << "timeInterval: " << timeInterval << endl;
cout << "debug: " << debug << endl;
cout << "================================" << endl;
}
Color** tempColors = new Color*[numObjects];
Object** tempObjects = new Object*[numObjects];
//File input for widths, heights, colors, and positions of objects
for (int i = 0; i < numObjects; ++i) {
int width;
int height;
int r;
int g;
int b;
int x;
int y;
double density;
try {
//Ensures row is present
ostringstream estream;
if (!(inputFile >> row)) {
string e = "Row appears to be missing: ";
estream << e << i;
throw estream.str();
}
else {
istringstream ss(row);
//Checks x
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: x (";
estream << e << i;
e += ", 0)";
throw estream.str();
}
else{
istringstream stoi(col);
if (!(stoi >> x)) {
cout << col << endl;
string e = "x value not properly formatted: (";
estream << e << i;
e += ", 0)";
throw estream.str();
}
}
//Checks y
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: y (";
estream << e << i;
e += ", 1)";
throw estream.str();
}
else{
istringstream stoi(col);
if (!(stoi >> y)) {
cout << col << endl;
string e = "y value not properly formatted: (";
estream << e << i;
e += ", 1)";
throw estream.str();
}
}
//Checks r
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: r (";
estream << e << i;
e += ", 2)";
throw estream.str();
}
else{
istringstream stoi(col);
if (!(stoi >> r)) {
cout << col << endl;
string e = "r value not properly formatted: (";
estream << e << i;
e += ", 2)";
throw estream.str();
}
}
//Checks g
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: g (";
estream << e << i;
e += ", 3)";
throw estream.str();
}
else{
istringstream stoi(col);
if (!(stoi >> g)) {
cout << col << endl;
string e = "g value not properly formatted: (";
estream << e << i;
e += ", 3)";
throw estream.str();
}
}
//Checks b
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: b (";
estream << e << i;
e += ", 4)";
throw estream.str();
}
else{
istringstream stoi(col);
if (!(stoi >> b)) {
cout << col << endl;
string e = "b value not properly formatted: (";
estream << e << i;
e += ", 4)";
throw estream.str();
}
}
//Checks width
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: width (";
estream << e << i;
e += ", 5)";
throw estream.str();
}
else{
istringstream stoi(col);
if (!(stoi >> width)) {
cout << col << endl;
string e = "width value not properly formatted: (";
estream << e << i;
e += ", 5)";
throw estream.str();
}
}
//Checks height
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: height (";
estream << e << i;
e += ", 6)";
throw estream.str();
}
else{
istringstream stoi(col);
if (!(stoi >> height)) {
cout << col << endl;
string e = "height value not properly formatted: (";
estream << e << i;
e += ", 6)";
throw estream.str();
}
}
//Checks density
if (!(getline(ss, col, ','))) {
string e = "Input file not properly formatted: density (";
estream << e << i;
e += ", 7)";
throw estream.str();
}
else{
istringstream stoi(col);
if (!(stoi >> density)) {
cout << col << endl;
string e = "density value not properly formatted: (";
estream << e << i;
e += ", 7)";
throw estream.str();
}
}
}
}
catch (string exception) {
cout << exception << endl;
exit(1);
}
tempColors[i] = new Color(r, g, b);
tempObjects[i] = new Object(x, y, tempColors[i], width, height, density, i);
tempObjects[i]->buildData();
//Sets object values for constants, such as density. If there is
//density input, this should take in arguments
//tempObjects[i]->setDefaultConstants();
//Sets initial values for mass, velocity, and position
tempObjects[i]->setMass(pixelSize);
tempObjects[i]->xiSet(x * pixelSize);
tempObjects[i]->yiSet(y * pixelSize);
tempObjects[i]->vxiSet(0);
tempObjects[i]->vyiSet(0);
if (debug) {
cout << "Built data for object " << i << endl;
cout << "OBJECT: " << i << endl;
cout << "witdh: " << width << endl;
cout << "height: " << height << endl;
cout << "mass: " << tempObjects[i]->getMass() << endl;
cout << "r: " << r << endl;
cout << "g: " << g << endl;
cout << "b: " << b << endl;
cout << "x: " << x << endl;
cout << "y: " << y << endl;
cout << "================================" << endl;
}
}
inputFile.close();
Simulation* simulation = new Simulation(xRes,
yRes,
numObjects,
tempColors,
tempObjects,
pixelSize,
timeInterval,
numIterations,
debug);
simulation->setInFilename(filename);
//Attempting to fix memory leaks for this function
delete[] tempColors;
delete[] tempObjects;
return simulation;
}
//Function to write csv output data
void outCsv(Simulation* theSim) {
//File output for objects
ofstream outputFile(theSim->getOutFilename().c_str());
try {
if (outputFile.is_open()) {
outputFile << theSim->getXRes() << ",";
outputFile << theSim->getYRes() << ",";
outputFile << theSim->getNumObjects() << endl;
for (int i = 0; i < theSim->getNumObjects(); ++i) {
outputFile << theSim->getObject(i)->getElt(0) << ",";
outputFile << theSim->getObject(i)->getElt(1) << ",";
outputFile << theSim->getObject(i)->getElt(2) << ",";
outputFile << theSim->getObject(i)->getElt(3) << endl;
for (int j = 4;
j < theSim->getObject(i)->getNumElts();
j += 3) {
for (int k = 0; k < 3; ++k) {
outputFile << theSim->getObject(i)->getElt(j + k);
if (k != 2) {
outputFile << ",";
}
else {
outputFile << endl;
}
}
}
}
}
else {
string e = "Unable to open output file: ";
e += theSim->getOutFilename();
throw e;
}
outputFile.close();
}
catch (string exception) {
cout << exception << endl;
exit(1);
}
}
int main(int argc, char* argv[]) {
bool debugIt;
string debugString;
string outFilename;
string inFilename;
//If there are 3 inputs, we assume the extra 2 are filenames
if (argc == 3) {
inFilename = argv[1];
outFilename = argv[2];
debugIt = false;
}
//With another, we assume this is specifying debug mode.
else if (argc == 4) {
inFilename = argv[1];
outFilename = argv[2];
try {
if (lowercase(debugString) == "true" ||
lowercase(debugString) == "debug" ||
lowercase(debugString) == "y") {
debugIt = true;
}
else if (lowercase(debugString) == "false" ||
lowercase(debugString) == "nodebug" ||
lowercase(debugString) == "n") {
debugIt = false;
}
else {
string e = "debugString value not recognized: ";
e += debugString;
throw e;
}
}
catch (string exception) {
cout << exception << endl;
exit(1);
}
}
else {
inFilename = "datagen.csv";
outFilename = "input.csv";
debugIt = false;
}
//Gets input data and stores it as a pointer to simulation
Simulation* sim = inCsv(inFilename, debugIt);
sim->setOutFilename(outFilename);
sim->setConstants();
//Sets initial position and velocity values for all objects
//Populates initial output data
outCsv(sim);
//Runs numFrames iterations of the simulation
for (int i = 0; i < sim->getNumIterations(); ++i) {
sim->deltaPos();
}
//Deletes the simulation
delete sim;
sim = 0;
return 0;
}