-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_parser.cpp
62 lines (52 loc) · 2.06 KB
/
data_parser.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
// Copyright (c) Conni Bilham & Lucy Coward 2022, All Rights Reserved.
#include "data_parser.h"
// Not really sure what this does, probably made by Conni
// Yes it was made by me, and it splits a string by a delimiter.
// Example:
// Input: Hello, world! ,
// Output: ["Hello", "world!"]
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
void data_parser::handle_csv(csv_data_template data) {
std::ifstream infile;
infile.open(data.file_path);
std::string line;
// Loop through each line in the file
while (std::getline(infile, line)) {
std::istringstream iss(line);
// Split the line by commas into a vector array
auto split_str = split(line, ',');
int StarID = std::stoi(split_str.at(data.star_id));
Vectorr position = !data.position_override ? Vectorr(
std::stof(split_str.at(data.position.x)),
std::stof(split_str.at(data.position.y)),
std::stof(split_str.at(data.position.z))
) : data.position;
Vectorr velocity = !data.velocity_override ? Vectorr(
std::stof(split_str.at(data.velocity.x)),
std::stof(split_str.at(data.velocity.y)),
std::stof(split_str.at(data.velocity.z))
) : data.velocity;
Vectorr acceleration = !data.acceleration_override ? Vectorr(
std::stof(split_str.at(data.acceleration.x)),
std::stof(split_str.at(data.acceleration.y)),
std::stof(split_str.at(data.acceleration.z))
) : data.acceleration;
star_list->emplace_back(new Star(
StarID, // ID
position, // Position
velocity, // Velocity
acceleration, // Acceleration
1.0f,
data.regionMatrix_Parent // Parent region matrix
)); // Mass
}
infile.close();
}