-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathio.cpp
More file actions
executable file
·248 lines (225 loc) · 6.64 KB
/
io.cpp
File metadata and controls
executable file
·248 lines (225 loc) · 6.64 KB
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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <random>
#include <fstream>
#include "tegbp.hpp"
#include <iomanip>
using namespace std;
vector<string> split(string& input, char delimiter)
{
istringstream stream(input);
string field;
vector<string> result;
while (getline(stream, field, delimiter)) {
result.push_back(field);
}
return result;
}
// void load_data_dummy(mem_pool pool){
// // printf("Generating dummy data");
// std::random_device rd;
// std::mt19937 gen(rd());
// std::uniform_int_distribution<int32> rand_idx(32, pool.W-32);
// std::uniform_int_distribution<int32> rand_time(1000, 1000000);
// std::uniform_real_distribution<> rand_val(0.0, 1.0);
// for(int32 i=0;i<pool.B;i++){
// pool.indices[2*i+0] = rand_idx(gen);
// pool.indices[2*i+1] = rand_idx(gen);
// pool.v_norms[2*i+0] = rand_val(gen);
// pool.v_norms[2*i+1] = rand_val(gen);
// pool.timestamps[i] = rand_time(gen);
// // printf("([%3d] %2d, %2d, %0.1f, %0.1f)\n",i, indices[2*i+0], indices[2*i+0], v_norms[2*i+0], v_norms[2*i+1]);
// }
// return;
// }
void load_txt(mem_pool pool){
std::string data_path = "data/" + pool.data_name + ".txt";
// printf("load_data_txt %s %d\n", data_path.c_str(), pool.B);
ifstream ifs(data_path);
string line;
getline(ifs, line);
// cout << line << '\n';
int32 n = 0;
while (getline(ifs, line) && n<pool.B) {
vector<string> strvec = split(line, ',');
pool.indices[2*n+0] = (int32)stoi(strvec.at(0)); // x
pool.indices[2*n+1] = (int32)stoi(strvec.at(1)); // y
pool.timestamps[n] = (int32)stoi(strvec.at(2)); // t
pool.polarities[n] = (int32)stoi(strvec.at(3)); // p
pool.norms[2*n+0] = stod(strvec.at(4)); // vx_perp
pool.norms[2*n+1] = stod(strvec.at(5)); // vy_perp
pool.timestamps[n] = (int32)stoi(strvec.at(2)); // t
// printf("timestamps %d %d\n", n, pool.timestamps[n] );
n++;
}
// printf("done..\n");
return;
}
data_cfg load_cfg(std::string data_name){
data_cfg cfg;
printf("load config %s\n", data_name.c_str());
if (data_name == "bricks"){
cfg.W = 272;
cfg.H = 208;
cfg.B = 15000;
}
if (data_name == "bricks_1slide"){
cfg.W = 272;
cfg.H = 208;
cfg.B = 15000;
}
if (data_name == "indoor_flying2"){
cfg.W = 368;
cfg.H = 288;
cfg.B = 2921002;
}
if (data_name == "zurich_city_11_a"){
cfg.W = 672;
cfg.H = 512;
cfg.B = 10696000;
}
if (data_name == "dummy"){
cfg.W = 272;
cfg.H = 208;
cfg.B = 15000;
}
if (data_name == "qr000"){ // substr(0, 2) == "qr"
cfg.W = 1312;
cfg.H = 752;
cfg.B = 1418870;
}
if (data_name == "qr001"){
cfg.W = 1312;
cfg.H = 752;
cfg.B = 684966;
}
if (data_name == "zurich_city_11_a"){
cfg.W = 672;
cfg.H = 512;
cfg.B = 10696000;
}
if (data_name == "hand_spinner"){
cfg.W = 688;
cfg.H = 528;
cfg.B = 434984;
}
cfg.data_name = data_name;
return cfg;
}
mem_pool load_data(mem_pool pool){
// printf("initialize memory pool %d %d %d %d\n", pool.B, pool.H, pool.W, pool.timestamps);
load_txt(pool);
printf("done..\n");
return pool;
}
void debug_output(mem_pool pool){
printf("Saving data");
for(int32 y=0;y<pool.H/6;y++){
printf("\n");
for(int32 x=0;x<pool.W/6;x++){
printf("%04.1f|", pool.node[sub2ind_nod(x, y, pool.H, pool.W)]);
}
}
printf("done..\n");
}
void save_flow(mem_pool pool, int32 seq_id, int32 index, int32 c_time){
double *fimg = (double *) malloc(2*pool.W*pool.H*sizeof(double));
memset(fimg, 0.0, 2*pool.W*pool.H*sizeof(double));
// for(int32 y=0; y<pool.H; y++){
// for(int32 x=0;x<pool.W;x++){
// fimg[2*(pool.W*y + x)+0] = 0;
// fimg[2*(pool.W*y + x)+1] = 0;
// }
// }
int32 time;
#pragma omp parallel for
for(int32 y=0; y<pool.H; y++){
for(int32 x=0;x<pool.W;x++){
time = pool.sae[(pool.W*y + x)];
if ((c_time-time)<DT_VIS){
switch (index){
case 0:
fimg[2*(pool.W*y + x)] = pool.node[sub2ind_nod(x, y, pool.H, pool.W)+0];
fimg[2*(pool.W*y + x)+1] = pool.node[sub2ind_nod(x, y, pool.H, pool.W)+1];
break;
case 1:
fimg[2*(pool.W*y + x)] = pool.node[sub2ind_nod(x, y, pool.H, pool.W)+0+STS_DIM];
fimg[2*(pool.W*y + x)+1] = pool.node[sub2ind_nod(x, y, pool.H, pool.W)+1+STS_DIM];
break;
default:
int32 ind = sub2ind_(x, y, pool.H, pool.W);
fimg[2*ind] = pool.flow_norm[2*ind];
fimg[2*ind+1] = pool.flow_norm[2*ind+1];
break;
}
}
}
}
stringstream filename;
filename << "result/" << pool.data_name << "/bin/flo_" << index << "_" << std::setw(5) << std::setfill('0') << seq_id << ".bin";
std::ofstream myFile (filename.str(), std::ios::out | std::ios::binary);
myFile.write ((char *)fimg, 2*pool.W*pool.H*sizeof(double));
printf("Saving data %s, %d_%04d\n",filename.str().c_str(), index, seq_id);
// printf("comleted\n");
}
void save_img(mem_pool pool, int32 seq_id, int32 index, int32 c_idx){
int32 *fimg = (int32 *) malloc(pool.W*pool.H*sizeof(int32));
for(int32 y=0; y<pool.H; y++){
for(int32 x=0;x<pool.W;x++){
fimg[pool.W*y + x] = 0;
}
}
// memset(fimg, 1.0, pool.W*pool.H*sizeof(double));
int32 c_time = pool.timestamps[c_idx];
int32 idx = 0;
for(int32 i=c_idx; i>0; i--){
if (c_time - pool.timestamps[i] > DT_VIS){
idx = i+1;
break;
}
}
// printf("%d, %d\n", idx, c_idx);
double scale;
double x_to;
double y_to;
for(int32 i=idx; i<=c_idx; i++){
int32 x = pool.indices[2*i+0];
int32 y = pool.indices[2*i+1];
int32 t = pool.timestamps[i];
int32 p = pool.polarities[i];
double vx = pool.fulls[2*i+0];
double vy = pool.fulls[2*i+1];
switch (index){
case 0:
fimg[pool.W*y + x] += p*2-1;
break;
case 1:
if ((vx==0.0) && (vy==0.0)) break;
scale = (double)(c_time - t) / (double)DT;
x_to = (double)x + scale * vx;
y_to = (double)y + scale * vy;
if (x_to >= pool.W) x_to = pool.W-1;
if (y_to >= pool.H) y_to = pool.H-1;
if (x_to < 0) x_to = 0;
if (y_to < 0) y_to = 0;
// printf("%.1f, %.1f, %d, %d, %f\n", vx, vy, t, c_time, scale);
// printf("%d, %d\n", (int32)x, (int32)y);
// printf("%d, %d\n", (int32)x_to, (int32)y_to);
fimg[pool.W*(int32)y_to + (int32)x_to] += p*2-1;
break;
default:
break;
}
}
stringstream filename;
filename << "result/" << pool.data_name << "/bin/img_" << index << "_" << std::setw(5) << std::setfill('0') << seq_id << ".bin";
std::ofstream myFile (filename.str(), std::ios::out | std::ios::binary);
myFile.write ((char *)fimg, pool.W*pool.H*sizeof(int32));
printf("Saving data %s, %d_%04d\n",filename.str().c_str(), index, seq_id);
// printf("comleted\n");
}