-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryLoader.cpp
More file actions
96 lines (79 loc) · 3.2 KB
/
binaryLoader.cpp
File metadata and controls
96 lines (79 loc) · 3.2 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
#include <iostream>
#include "main.h"
#include "loader/city_loader.h"
CSR loadToBinCSR(const string& filename) {
JSONData data = loadNodesAndWays("../data/" + filename + ".json");
unordered_map<long long, size_t> id_to_index = data.id_to_index;
int n = data.nodes.size();
vector<int> edge_count(n, 0);
int prev_index = -1;
for(const Ways& way: data.ways){
if(way.nodes.size() < 2) continue;
prev_index = -1;
for(long long wn: way.nodes){
auto it = id_to_index.find(wn);
if (it != id_to_index.end()) {
if(prev_index != -1 && prev_index != it->second){
count_road_edges(edge_count, prev_index, it->second, way.oneway);
}
prev_index = it->second;
}
}
}
CSR csr;
csr.row_ptr.resize(n + 1);
csr.row_ptr[0] = 0;
for(int i = 0; i < n; i++) {
csr.row_ptr[i + 1] = csr.row_ptr[i] + edge_count[i];
}
csr.edges.resize(csr.row_ptr[n]);
vector<size_t> current_pos = csr.row_ptr;
prev_index = -1;
for(const Ways& way: data.ways){
if(way.nodes.size() < 2) continue;
prev_index = -1;
for(long long wn: way.nodes){
auto it = id_to_index.find(wn);
if (it != id_to_index.end()) {
if(prev_index != -1 && prev_index != it->second){
add_road(csr, current_pos, data.nodes, prev_index, it->second, way.oneway);
}
prev_index = it->second;
}
}
}
ofstream foutCSR("../data/binRoads/CSR_" + filename + ".bin", ios::out | ios::binary);
if(foutCSR){
size_t row_size = csr.row_ptr.size();
size_t edge_size = csr.edges.size();
foutCSR.write(reinterpret_cast<char*>(&row_size), sizeof(row_size));
foutCSR.write(reinterpret_cast<char*>(&edge_size), sizeof(edge_size));
foutCSR.write(reinterpret_cast<char*>(csr.row_ptr.data()), row_size * sizeof(size_t));
foutCSR.write(reinterpret_cast<char*>(csr.edges.data()), edge_size * sizeof(Edge));
foutCSR.close();
}
ofstream foutNodes("../data/binRoads/nodes_" + filename + ".bin", ios::out | ios::binary);
if(foutNodes){
size_t node_size = data.nodes.size();
foutNodes.write(reinterpret_cast<char*>(&node_size), sizeof(node_size));
foutNodes.write(reinterpret_cast<char*>(data.nodes.data()), node_size * sizeof(Node));
foutNodes.close();
}
return csr;
}
CSR loadFromBinCSR(const string& filename) {
CSR csr;
ifstream fin("../data/binRoads/CSR_" + filename + ".bin", ios::in | ios::binary);
if(fin){
size_t row_ptr_size = 0;
size_t edge_size = 0;
fin.read(reinterpret_cast<char*>(&row_ptr_size), sizeof(row_ptr_size));
fin.read(reinterpret_cast<char*>(&edge_size), sizeof(edge_size));
csr.row_ptr.resize(row_ptr_size);
csr.edges.resize(edge_size);
fin.read(reinterpret_cast<char*>(csr.row_ptr.data()), csr.row_ptr.size() * sizeof(size_t));
fin.read(reinterpret_cast<char*>(csr.edges.data()), csr.edges.size() * sizeof(Edge));
fin.close();
}
return csr;
}