1 | #include "mapstorage.h" |
---|
2 | #include <gtkmm.h> |
---|
3 | #include <cmath> |
---|
4 | |
---|
5 | MapStorage::MapStorage() : modified(false), file_name("") |
---|
6 | { |
---|
7 | nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph); |
---|
8 | coords.setXMap(*nodemap_storage["coordinates_x"]); |
---|
9 | nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph); |
---|
10 | coords.setYMap(*nodemap_storage["coordinates_y"]); |
---|
11 | |
---|
12 | nodemap_storage["id"] = new Graph::NodeMap<double>(graph); |
---|
13 | edgemap_storage["id"] = new Graph::EdgeMap<double>(graph); |
---|
14 | |
---|
15 | nodemap_default["id"] = 1.0; |
---|
16 | edgemap_default["id"] = 1.0; |
---|
17 | } |
---|
18 | |
---|
19 | MapStorage::~MapStorage() |
---|
20 | { |
---|
21 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
22 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
23 | { |
---|
24 | delete it->second; |
---|
25 | } |
---|
26 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
27 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
28 | { |
---|
29 | delete it->second; |
---|
30 | } |
---|
31 | } |
---|
32 | |
---|
33 | int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0) |
---|
34 | { |
---|
35 | if( nodemap_storage.find(name) == nodemap_storage.end() ) |
---|
36 | { |
---|
37 | nodemap_storage[name]=nodemap; |
---|
38 | // set the maps default value |
---|
39 | nodemap_default[name] = default_value; |
---|
40 | return 0; |
---|
41 | } |
---|
42 | return 1; |
---|
43 | } |
---|
44 | |
---|
45 | int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0) |
---|
46 | { |
---|
47 | if( edgemap_storage.find(name) == edgemap_storage.end() ) |
---|
48 | { |
---|
49 | edgemap_storage[name]=edgemap; |
---|
50 | // set the maps default value |
---|
51 | edgemap_default[name] = default_value; |
---|
52 | return 0; |
---|
53 | } |
---|
54 | return 1; |
---|
55 | } |
---|
56 | |
---|
57 | double MapStorage::maxOfNodeMap(const std::string & name) |
---|
58 | { |
---|
59 | double max=0; |
---|
60 | for (NodeIt j(graph); j!=INVALID; ++j) |
---|
61 | { |
---|
62 | if( (*nodemap_storage[name])[j]>max ) |
---|
63 | { |
---|
64 | max=(*nodemap_storage[name])[j]; |
---|
65 | } |
---|
66 | } |
---|
67 | return max; |
---|
68 | } |
---|
69 | |
---|
70 | double MapStorage::maxOfEdgeMap(const std::string & name) |
---|
71 | { |
---|
72 | double max=0; |
---|
73 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
74 | { |
---|
75 | if( (*edgemap_storage[name])[j]>max ) |
---|
76 | { |
---|
77 | max=(*edgemap_storage[name])[j]; |
---|
78 | } |
---|
79 | } |
---|
80 | return max; |
---|
81 | } |
---|
82 | |
---|
83 | double MapStorage::minOfNodeMap(const std::string & name) |
---|
84 | { |
---|
85 | NodeIt j(graph); |
---|
86 | double min; |
---|
87 | if(j!=INVALID) |
---|
88 | { |
---|
89 | min=(*nodemap_storage[name])[j]; |
---|
90 | } |
---|
91 | else |
---|
92 | { |
---|
93 | min=0; |
---|
94 | } |
---|
95 | for (; j!=INVALID; ++j) |
---|
96 | { |
---|
97 | if( (*nodemap_storage[name])[j]<min ) |
---|
98 | { |
---|
99 | min=(*nodemap_storage[name])[j]; |
---|
100 | } |
---|
101 | } |
---|
102 | return min; |
---|
103 | } |
---|
104 | |
---|
105 | double MapStorage::minOfEdgeMap(const std::string & name) |
---|
106 | { |
---|
107 | EdgeIt j(graph); |
---|
108 | double min; |
---|
109 | if(j!=INVALID) |
---|
110 | { |
---|
111 | min=(*edgemap_storage[name])[j]; |
---|
112 | } |
---|
113 | else |
---|
114 | { |
---|
115 | min=0; |
---|
116 | } |
---|
117 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
118 | { |
---|
119 | if( (*edgemap_storage[name])[j]<min ) |
---|
120 | { |
---|
121 | min=(*edgemap_storage[name])[j]; |
---|
122 | } |
---|
123 | } |
---|
124 | return min; |
---|
125 | } |
---|
126 | |
---|
127 | int MapStorage::readFromFile(const std::string &filename) |
---|
128 | { |
---|
129 | bool read_x = false; |
---|
130 | bool read_y = false; |
---|
131 | bool read_edge_id = false; |
---|
132 | |
---|
133 | try { |
---|
134 | LemonReader lreader(filename); |
---|
135 | ContentReader content(lreader); |
---|
136 | lreader.run(); |
---|
137 | |
---|
138 | const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0); |
---|
139 | const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0); |
---|
140 | |
---|
141 | GraphReader<Graph> greader(filename, graph); |
---|
142 | for (std::vector<std::string>::const_iterator it = nodeMapNames.begin(); |
---|
143 | it != nodeMapNames.end(); ++it) |
---|
144 | { |
---|
145 | if (*it == "coordinates_x") |
---|
146 | { |
---|
147 | read_x = true; |
---|
148 | //std::cout << "read X nodemap" << std::endl; |
---|
149 | } |
---|
150 | else if (*it == "coordinates_y") |
---|
151 | { |
---|
152 | read_y = true; |
---|
153 | //std::cout << "read Y nodemap" << std::endl; |
---|
154 | } |
---|
155 | else if (*it == "id") |
---|
156 | { |
---|
157 | //std::cout << "read id nodemap" << std::endl; |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | nodemap_storage[*it] = new Graph::NodeMap<double>(graph); |
---|
162 | //std::cout << "read " << *it << " nodemap" << std::endl; |
---|
163 | } |
---|
164 | greader.readNodeMap(*it, *nodemap_storage[*it]); |
---|
165 | } |
---|
166 | for (std::vector<std::string>::const_iterator it = edgeMapNames.begin(); |
---|
167 | it != edgeMapNames.end(); ++it) |
---|
168 | { |
---|
169 | if (*it == "id") |
---|
170 | { |
---|
171 | //std::cout << "read id edgemap" << std::endl; |
---|
172 | } |
---|
173 | else |
---|
174 | { |
---|
175 | edgemap_storage[*it] = new Graph::EdgeMap<double>(graph); |
---|
176 | //std::cout << "read " << *it << " edgemap" << std::endl; |
---|
177 | } |
---|
178 | greader.readEdgeMap(*it, *edgemap_storage[*it]); |
---|
179 | } |
---|
180 | greader.run(); |
---|
181 | } catch (DataFormatError& error) { |
---|
182 | Gtk::MessageDialog mdialog("Read Error"); |
---|
183 | mdialog.set_message(error.what()); |
---|
184 | mdialog.run(); |
---|
185 | clear(); |
---|
186 | return 1; |
---|
187 | } |
---|
188 | |
---|
189 | if (!read_edge_id) |
---|
190 | { |
---|
191 | edgemap_storage["id"] = new Graph::EdgeMap<double>(graph); |
---|
192 | int i = 1; |
---|
193 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
194 | { |
---|
195 | (*edgemap_storage["id"])[e] = i++; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | if (!read_x || !read_y) |
---|
200 | { |
---|
201 | int node_num = 0; |
---|
202 | for (NodeIt n(graph); n != INVALID; ++n) |
---|
203 | { |
---|
204 | node_num++; |
---|
205 | } |
---|
206 | const double pi = 3.142; |
---|
207 | double step = 2 * pi / (double) node_num; |
---|
208 | int i = 0; |
---|
209 | for (NodeIt n(graph); n != INVALID; ++n) |
---|
210 | { |
---|
211 | nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step)); |
---|
212 | nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step)); |
---|
213 | i++; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | // fill in the default values for the maps |
---|
218 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
219 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
220 | { |
---|
221 | if ((it->first != "id") && |
---|
222 | (it->first != "coordiantes_x") && |
---|
223 | (it->first != "coordinates_y")) |
---|
224 | { |
---|
225 | nodemap_default[it->first] = 0.0; |
---|
226 | } |
---|
227 | else if (it->first == "id") |
---|
228 | { |
---|
229 | NodeIt n(graph); |
---|
230 | double max = (*nodemap_storage["id"])[n]; |
---|
231 | for (; n != INVALID; ++n) |
---|
232 | { |
---|
233 | if ((*nodemap_storage["id"])[n] > max) |
---|
234 | max = (*nodemap_storage["id"])[n]; |
---|
235 | } |
---|
236 | nodemap_default["id"] = max + 1.0; |
---|
237 | } |
---|
238 | } |
---|
239 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
240 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
241 | { |
---|
242 | if (it->first != "id") |
---|
243 | { |
---|
244 | edgemap_default[it->first] = 0.0; |
---|
245 | } |
---|
246 | else |
---|
247 | { |
---|
248 | EdgeIt e(graph); |
---|
249 | double max = (*edgemap_storage["id"])[e]; |
---|
250 | for (; e != INVALID; ++e) |
---|
251 | { |
---|
252 | if ((*edgemap_storage["id"])[e] > max) |
---|
253 | max = (*edgemap_storage["id"])[e]; |
---|
254 | } |
---|
255 | edgemap_default["id"] = max + 1.0; |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | return 0; |
---|
260 | } |
---|
261 | |
---|
262 | void MapStorage::writeToFile(const std::string &filename) |
---|
263 | { |
---|
264 | GraphWriter<Graph> gwriter(filename, graph); |
---|
265 | |
---|
266 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
267 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
268 | { |
---|
269 | gwriter.writeNodeMap(it->first, *(it->second)); |
---|
270 | //std::cout << "wrote " << it->first << " nodemap" << std::endl; |
---|
271 | } |
---|
272 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
273 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
274 | { |
---|
275 | gwriter.writeEdgeMap(it->first, *(it->second)); |
---|
276 | //std::cout << "wrote " << it->first << " edgemap" << std::endl; |
---|
277 | } |
---|
278 | gwriter.run(); |
---|
279 | } |
---|
280 | |
---|
281 | void MapStorage::clear() |
---|
282 | { |
---|
283 | for (std::map<std::string, Graph::NodeMap<double>*>::iterator it = |
---|
284 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
285 | { |
---|
286 | if ((it->first != "coordinates_x") && |
---|
287 | (it->first != "coordinates_y") && |
---|
288 | (it->first != "id")) |
---|
289 | { |
---|
290 | delete it->second; |
---|
291 | nodemap_storage.erase(it); |
---|
292 | } |
---|
293 | } |
---|
294 | for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it = |
---|
295 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
296 | { |
---|
297 | if (it->first != "id") |
---|
298 | { |
---|
299 | delete it->second; |
---|
300 | edgemap_storage.erase(it); |
---|
301 | } |
---|
302 | } |
---|
303 | for (std::map<std::string, double>::iterator it = |
---|
304 | nodemap_default.begin(); it != nodemap_default.end(); ++it) |
---|
305 | { |
---|
306 | if (it->first != "id") |
---|
307 | nodemap_default.erase(it); |
---|
308 | } |
---|
309 | for (std::map<std::string, double>::iterator it = |
---|
310 | edgemap_default.begin(); it != edgemap_default.end(); ++it) |
---|
311 | { |
---|
312 | if (it->first != "id") |
---|
313 | edgemap_default.erase(it); |
---|
314 | } |
---|
315 | graph.clear(); |
---|
316 | file_name = ""; |
---|
317 | modified = false; |
---|
318 | } |
---|