-
Notifications
You must be signed in to change notification settings - Fork 0
/
planetdata.cpp
157 lines (115 loc) · 4.41 KB
/
planetdata.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
/* Copyright (c) 2017 By the Authors
*
* This file is part of Orbit Simulator.
Orbit Simulator is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Orbit Simulator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*
* */
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include "planetdata.h"
PlanetData::PlanetData(const std::vector<std::string> &names){
m_planet_names = names;
for (unsigned int i = 0; i < m_planet_names.size(); ++i) {
m_positions.emplace_back(std::vector<Vector3> {});
m_velocities.emplace_back(std::vector<Vector3> {});
}
}
// Adapted from: https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
std::vector<std::string> split(std::string str, const std::string& delim) {
size_t pos = 0;
std::vector<std::string> result;
while ((pos = str.find(delim)) != std::string::npos) {
result.push_back(str.substr(0, pos));
str.erase(0, pos + delim.length());
}
return result;
}
std::vector<HorizonsFile> PlanetData::horizons_to_structs(const std::string planet) {
std::string path = "data/" + planet + ".csv";
boost::algorithm::to_lower(path);
boost::replace_all(path, " ", "_");
std::ifstream filereader;
HorizonsFile h = {};
std::vector<HorizonsFile> data;
std::string buffer;
typedef boost::tokenizer<boost::escaped_list_separator<char>> tokenizer;
filereader.open(path);
if (filereader.is_open()) {
while (!filereader.eof()) {
std::getline(filereader, buffer);
if (!buffer.empty()) {
tokenizer tok(buffer);
auto tok_it = tok.begin();
h.x = std::stod(*tok_it) * 1000.0;
++tok_it;
h.y = std::stod(*tok_it) * 1000.0;
++tok_it;
h.z = std::stod(*tok_it) * 1000.0;
++tok_it;
h.vx = std::stod(*tok_it) * 1000.0;
++tok_it;
h.vy = std::stod(*tok_it) * 1000.0;
++tok_it;
h.vz = std::stod(*tok_it) * 1000.0;
data.emplace_back(h);
}
}
filereader.close();
}
return data;
}
void PlanetData::structs_to_arrays() {
long n = m_planet_names.size();
std::string out;
for (int j = 0; j < n; ++j) {
std::vector<HorizonsFile> h_structs = horizons_to_structs(m_planet_names[j]);
out = "Initialized vector of structs with " + std::to_string(h_structs.size()) +
" elements, self-positons have size " + std::to_string(m_positions.size()) + "\n";
long n_of_structs = h_structs.size();
for (int i = 0; i < n_of_structs; ++i) {
HorizonsFile h = h_structs[i];
m_positions[j].emplace_back(Vector3 (h.x, h.y, h.z));
m_velocities[j].emplace_back(Vector3 (h.vx, h.vy, h.vz));
}
}
std::cout << "Imported position and velocity data for " << m_positions.size()
<< " bodies." << std::endl;
}
std::vector<Vector3> PlanetData::get_body_positions(int body){
return m_positions[body];
};
std::vector<Vector3> PlanetData::get_body_velocities(int body) {
return m_velocities[body];
}
std::vector<Vector3 > PlanetData::get_starting_positions(){
long n = m_planet_names.size();
std::vector<Vector3> starting_positions;
for (int i = 0; i < n; ++i) {
starting_positions.emplace_back(get_body_positions(i)[0]);
}
return starting_positions;
}
std::vector<Vector3 > PlanetData::get_starting_velocities() {
long n = m_planet_names.size();
std::vector<Vector3> starting_velocities;
for (int i = 0; i < n; ++i) {
starting_velocities.emplace_back(get_body_velocities(i)[0]);
}
return starting_velocities;
}
void PlanetData::read_data() {
structs_to_arrays();
}