-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_reader.cc
141 lines (118 loc) · 3.5 KB
/
test_reader.cc
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <vector>
#include <string>
bool isFloat(std::string myString) {
std::istringstream iss(myString);
double f;
// iss >> std::noskipws >> f; // noskipws considers leading whitespace invalid
// Check the entire string was consumed and if either failbit or badbit is set
iss >> f;
return iss.eof() && !iss.fail();
}
template <typename real_t>
void read_points_from_file(std::string fname, float vfac_,
std::vector<real_t> &p) {
int num_columns;
std::ifstream ifs(fname.c_str());
if (!ifs) {
fprintf(stderr,
"region_ellipsoid_plugin::read_points_from_file : Could not open "
"file \'%s\'",
fname.c_str());
throw std::runtime_error("region_ellipsoid_plugin::read_points_from_file : "
"cannot open point file.");
}
int colcount = 0, colcount1 = 0, row = 0;
p.clear();
while (ifs) {
std::string s;
if (!getline(ifs, s))
break;
//std::cerr << s << std::endl;
std::replace(s.begin(), s.end(), '\t', ' ');
std::replace(s.begin(), s.end(), '\r', ' ');
std::stringstream ss(s);
colcount1 = 0;
while (ss) {
if (!getline(ss, s, ' '))
break;
if (!isFloat(s))
continue;
p.push_back(strtod(s.c_str(), NULL));
if (row == 0)
colcount++;
else
colcount1++;
}
++row;
if (row > 1 && colcount != colcount1)
fprintf(stderr, "error on line %d of input file", row);
// std::cout << std::endl;
}
printf("region point file appears to contain %d columns", colcount);
if (p.size() % 3 != 0 && p.size() % 6 != 0) {
for (auto x : p)
std::cerr << x << "\n";
fprintf(stderr,
"Region point file \'%s\' does not contain triplets (%d elems)",
fname.c_str(), p.size());
throw std::runtime_error("region_ellipsoid_plugin::read_points_from_file : "
"file does not contain triplets.");
}
double x0[3] = {p[0], p[1], p[2]}, dx;
if (colcount == 3) {
// only positions are given
for (size_t i = 3; i < p.size(); i += 3) {
for (size_t j = 0; j < 3; ++j) {
dx = p[i + j] - x0[j];
if (dx < -0.5)
dx += 1.0;
else if (dx > 0.5)
dx -= 1.0;
p[i + j] = x0[j] + dx;
}
}
} else if (colcount == 6) {
// positions and velocities are given
//... include the velocties to unapply Zeldovich approx.
for (size_t j = 3; j < 6; ++j) {
dx = (p[j - 3] - p[j] / vfac_) - x0[j - 3];
if (dx < -0.5)
dx += 1.0;
else if (dx > 0.5)
dx -= 1.0;
p[j] = x0[j - 3] + dx;
}
for (size_t i = 6; i < p.size(); i += 6) {
for (size_t j = 0; j < 3; ++j) {
dx = p[i + j] - x0[j];
if (dx < -0.5)
dx += 1.0;
else if (dx > 0.5)
dx -= 1.0;
p[i + j] = x0[j] + dx;
}
for (size_t j = 3; j < 6; ++j) {
dx = (p[i + j - 3] - p[i + j] / vfac_) - x0[j - 3];
if (dx < -0.5)
dx += 1.0;
else if (dx > 0.5)
dx -= 1.0;
p[i + j] = x0[j - 3] + dx;
}
}
} else
fprintf(stderr, "Problem interpreting the region point file \'%s\'",
fname.c_str());
num_columns = colcount;
fprintf(stderr,"num_columns = %d\n\n",num_columns);
}
int main( int argc, char**argv )
{
std::vector<float> pts;
read_points_from_file<float>( argv[1], 1.0, pts );
return 1;
}