1 | #include "mapstorage.h" |
---|
2 | #include "gui_writer.h" |
---|
3 | #include "gui_reader.h" |
---|
4 | #include <gtkmm.h> |
---|
5 | #include <cmath> |
---|
6 | |
---|
7 | #include <cmath> |
---|
8 | |
---|
9 | MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false) |
---|
10 | { |
---|
11 | nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph); |
---|
12 | coords.setXMap(*nodemap_storage["coordinates_x"]); |
---|
13 | nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph); |
---|
14 | coords.setYMap(*nodemap_storage["coordinates_y"]); |
---|
15 | |
---|
16 | edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph); |
---|
17 | arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]); |
---|
18 | edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph); |
---|
19 | arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]); |
---|
20 | |
---|
21 | nodemap_storage["id"] = new Graph::NodeMap<double>(graph); |
---|
22 | edgemap_storage["id"] = new Graph::EdgeMap<double>(graph); |
---|
23 | |
---|
24 | nodemap_default["id"] = 1.0; |
---|
25 | edgemap_default["id"] = 1.0; |
---|
26 | |
---|
27 | active_nodemaps.resize(NODE_PROPERTY_NUM); |
---|
28 | for(int i=0;i<NODE_PROPERTY_NUM;i++) |
---|
29 | { |
---|
30 | active_nodemaps[i]=""; |
---|
31 | } |
---|
32 | |
---|
33 | active_edgemaps.resize(EDGE_PROPERTY_NUM); |
---|
34 | for(int i=0;i<EDGE_PROPERTY_NUM;i++) |
---|
35 | { |
---|
36 | active_edgemaps[i]=""; |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | MapStorage::~MapStorage() |
---|
41 | { |
---|
42 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
43 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
44 | { |
---|
45 | delete it->second; |
---|
46 | } |
---|
47 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
48 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
49 | { |
---|
50 | delete it->second; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value) |
---|
55 | { |
---|
56 | std::cout << default_value << std::endl; |
---|
57 | if( nodemap_storage.find(name) == nodemap_storage.end() ) |
---|
58 | { |
---|
59 | nodemap_storage[name]=nodemap; |
---|
60 | // set the maps default value |
---|
61 | nodemap_default[name] = default_value; |
---|
62 | |
---|
63 | //announce changement in maps |
---|
64 | signal_node_map.emit(name); |
---|
65 | return 0; |
---|
66 | } |
---|
67 | return 1; |
---|
68 | } |
---|
69 | |
---|
70 | void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname) |
---|
71 | { |
---|
72 | if(itisedge) |
---|
73 | { |
---|
74 | active_edgemaps[prop]=mapname; |
---|
75 | } |
---|
76 | else |
---|
77 | { |
---|
78 | active_nodemaps[prop]=mapname; |
---|
79 | } |
---|
80 | signal_prop.emit(itisedge, prop); |
---|
81 | } |
---|
82 | |
---|
83 | std::string MapStorage::getActiveEdgeMap(int prop) |
---|
84 | { |
---|
85 | return active_edgemaps[prop]; |
---|
86 | } |
---|
87 | |
---|
88 | std::string MapStorage::getActiveNodeMap(int prop) |
---|
89 | { |
---|
90 | return active_nodemaps[prop]; |
---|
91 | } |
---|
92 | |
---|
93 | std::vector<std::string> MapStorage::getEdgeMapList() |
---|
94 | { |
---|
95 | std::vector<std::string> eml; |
---|
96 | eml.resize(edgemap_storage.size()); |
---|
97 | int i=0; |
---|
98 | std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps(); |
---|
99 | for(;emsi!=endOfEdgeMaps();emsi++) |
---|
100 | { |
---|
101 | eml[i]=(emsi->first); |
---|
102 | i++; |
---|
103 | } |
---|
104 | return eml; |
---|
105 | } |
---|
106 | |
---|
107 | std::vector<std::string> MapStorage::getNodeMapList() |
---|
108 | { |
---|
109 | std::vector<std::string> nml; |
---|
110 | nml.resize(nodemap_storage.size()); |
---|
111 | int i=0; |
---|
112 | std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps(); |
---|
113 | for(;nmsi!=endOfNodeMaps();nmsi++) |
---|
114 | { |
---|
115 | nml[i]=(nmsi->first); |
---|
116 | i++; |
---|
117 | } |
---|
118 | return nml; |
---|
119 | } |
---|
120 | |
---|
121 | MapStorage::Signal_Prop MapStorage::signal_prop_ch() |
---|
122 | { |
---|
123 | return signal_prop; |
---|
124 | } |
---|
125 | |
---|
126 | int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value) |
---|
127 | { |
---|
128 | if( edgemap_storage.find(name) == edgemap_storage.end() ) |
---|
129 | { |
---|
130 | edgemap_storage[name]=edgemap; |
---|
131 | // set the maps default value |
---|
132 | edgemap_default[name] = default_value; |
---|
133 | |
---|
134 | //announce changement in maps |
---|
135 | signal_edge_map.emit(name); |
---|
136 | return 0; |
---|
137 | } |
---|
138 | return 1; |
---|
139 | } |
---|
140 | |
---|
141 | double MapStorage::maxOfNodeMap(const std::string & name) |
---|
142 | { |
---|
143 | double max=0; |
---|
144 | for (NodeIt j(graph); j!=INVALID; ++j) |
---|
145 | { |
---|
146 | if( (*nodemap_storage[name])[j]>max ) |
---|
147 | { |
---|
148 | max=(*nodemap_storage[name])[j]; |
---|
149 | } |
---|
150 | } |
---|
151 | return max; |
---|
152 | } |
---|
153 | |
---|
154 | double MapStorage::maxOfEdgeMap(const std::string & name) |
---|
155 | { |
---|
156 | double max=0; |
---|
157 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
158 | { |
---|
159 | if( (*edgemap_storage[name])[j]>max ) |
---|
160 | { |
---|
161 | max=(*edgemap_storage[name])[j]; |
---|
162 | } |
---|
163 | } |
---|
164 | return max; |
---|
165 | } |
---|
166 | |
---|
167 | double MapStorage::minOfNodeMap(const std::string & name) |
---|
168 | { |
---|
169 | NodeIt j(graph); |
---|
170 | double min; |
---|
171 | if(j!=INVALID) |
---|
172 | { |
---|
173 | min=(*nodemap_storage[name])[j]; |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | min=0; |
---|
178 | } |
---|
179 | for (; j!=INVALID; ++j) |
---|
180 | { |
---|
181 | if( (*nodemap_storage[name])[j]<min ) |
---|
182 | { |
---|
183 | min=(*nodemap_storage[name])[j]; |
---|
184 | } |
---|
185 | } |
---|
186 | return min; |
---|
187 | } |
---|
188 | |
---|
189 | double MapStorage::minOfEdgeMap(const std::string & name) |
---|
190 | { |
---|
191 | EdgeIt j(graph); |
---|
192 | double min; |
---|
193 | if(j!=INVALID) |
---|
194 | { |
---|
195 | min=(*edgemap_storage[name])[j]; |
---|
196 | } |
---|
197 | else |
---|
198 | { |
---|
199 | min=0; |
---|
200 | } |
---|
201 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
202 | { |
---|
203 | if( (*edgemap_storage[name])[j]<min ) |
---|
204 | { |
---|
205 | min=(*edgemap_storage[name])[j]; |
---|
206 | } |
---|
207 | } |
---|
208 | return min; |
---|
209 | } |
---|
210 | |
---|
211 | int MapStorage::readFromFile(const std::string &filename) |
---|
212 | { |
---|
213 | bool read_x = false; |
---|
214 | bool read_y = false; |
---|
215 | bool read_edge_id = false; |
---|
216 | |
---|
217 | try { |
---|
218 | LemonReader lreader(filename); |
---|
219 | ContentReader content(lreader); |
---|
220 | lreader.run(); |
---|
221 | |
---|
222 | if (content.nodeSetNum() < 1) |
---|
223 | { |
---|
224 | Gtk::MessageDialog mdialog("No nodeset found in file."); |
---|
225 | mdialog.run(); |
---|
226 | clear(); |
---|
227 | return 1; |
---|
228 | } |
---|
229 | |
---|
230 | if (content.edgeSetNum() < 1) |
---|
231 | { |
---|
232 | Gtk::MessageDialog mdialog("No edgeset found in file."); |
---|
233 | mdialog.run(); |
---|
234 | clear(); |
---|
235 | return 1; |
---|
236 | } |
---|
237 | |
---|
238 | const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0); |
---|
239 | const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0); |
---|
240 | |
---|
241 | GraphReader<Graph> greader(filename, graph); |
---|
242 | for (std::vector<std::string>::const_iterator it = nodeMapNames.begin(); |
---|
243 | it != nodeMapNames.end(); ++it) |
---|
244 | { |
---|
245 | if (*it == "coordinates_x") |
---|
246 | { |
---|
247 | read_x = true; |
---|
248 | //std::cout << "read X nodemap" << std::endl; |
---|
249 | } |
---|
250 | else if (*it == "coordinates_y") |
---|
251 | { |
---|
252 | read_y = true; |
---|
253 | //std::cout << "read Y nodemap" << std::endl; |
---|
254 | } |
---|
255 | else if (*it == "id") |
---|
256 | { |
---|
257 | //std::cout << "read id nodemap" << std::endl; |
---|
258 | } |
---|
259 | else |
---|
260 | { |
---|
261 | nodemap_storage[*it] = new Graph::NodeMap<double>(graph); |
---|
262 | //std::cout << "read " << *it << " nodemap" << std::endl; |
---|
263 | } |
---|
264 | greader.readNodeMap(*it, *nodemap_storage[*it]); |
---|
265 | } |
---|
266 | for (std::vector<std::string>::const_iterator it = edgeMapNames.begin(); |
---|
267 | it != edgeMapNames.end(); ++it) |
---|
268 | { |
---|
269 | if (*it == "id") |
---|
270 | { |
---|
271 | //std::cout << "read id edgemap" << std::endl; |
---|
272 | } |
---|
273 | else |
---|
274 | { |
---|
275 | edgemap_storage[*it] = new Graph::EdgeMap<double>(graph); |
---|
276 | //std::cout << "read " << *it << " edgemap" << std::endl; |
---|
277 | } |
---|
278 | greader.readEdgeMap(*it, *edgemap_storage[*it]); |
---|
279 | } |
---|
280 | GuiReader gui_reader(greader, this); |
---|
281 | greader.run(); |
---|
282 | } catch (Exception& error) { |
---|
283 | Gtk::MessageDialog mdialog(error.what()); |
---|
284 | mdialog.run(); |
---|
285 | clear(); |
---|
286 | return 1; |
---|
287 | } |
---|
288 | |
---|
289 | if (!read_edge_id) |
---|
290 | { |
---|
291 | edgemap_storage["id"] = new Graph::EdgeMap<double>(graph); |
---|
292 | int i = 1; |
---|
293 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
294 | { |
---|
295 | (*edgemap_storage["id"])[e] = i++; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | if (!read_x || !read_y) |
---|
300 | { |
---|
301 | int node_num = 0; |
---|
302 | for (NodeIt n(graph); n != INVALID; ++n) |
---|
303 | { |
---|
304 | node_num++; |
---|
305 | } |
---|
306 | const double pi = 3.142; |
---|
307 | double step = 2 * pi / (double) node_num; |
---|
308 | int i = 0; |
---|
309 | for (NodeIt n(graph); n != INVALID; ++n) |
---|
310 | { |
---|
311 | nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step)); |
---|
312 | nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step)); |
---|
313 | i++; |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | if (!arrow_pos_read_ok) |
---|
318 | { |
---|
319 | arrow_pos_read_ok = false; |
---|
320 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
321 | { |
---|
322 | arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0); |
---|
323 | } |
---|
324 | } |
---|
325 | |
---|
326 | // fill in the default values for the maps |
---|
327 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
328 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
329 | { |
---|
330 | if ((it->first != "id") && |
---|
331 | (it->first != "coordiantes_x") && |
---|
332 | (it->first != "coordinates_y")) |
---|
333 | { |
---|
334 | nodemap_default[it->first] = 0.0; |
---|
335 | } |
---|
336 | else if (it->first == "id") |
---|
337 | { |
---|
338 | NodeIt n(graph); |
---|
339 | double max = (*nodemap_storage["id"])[n]; |
---|
340 | for (; n != INVALID; ++n) |
---|
341 | { |
---|
342 | if ((*nodemap_storage["id"])[n] > max) |
---|
343 | max = (*nodemap_storage["id"])[n]; |
---|
344 | } |
---|
345 | nodemap_default["id"] = max + 1.0; |
---|
346 | } |
---|
347 | } |
---|
348 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
349 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
350 | { |
---|
351 | if (it->first != "id") |
---|
352 | { |
---|
353 | edgemap_default[it->first] = 0.0; |
---|
354 | } |
---|
355 | else |
---|
356 | { |
---|
357 | double max = std::numeric_limits<double>::min(); |
---|
358 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
359 | { |
---|
360 | if ((*edgemap_storage["id"])[e] > max) |
---|
361 | max = (*edgemap_storage["id"])[e]; |
---|
362 | } |
---|
363 | if (max > std::numeric_limits<double>::min()) |
---|
364 | edgemap_default["id"] = max + 1.0; |
---|
365 | else |
---|
366 | edgemap_default["id"] = 1.0; |
---|
367 | } |
---|
368 | } |
---|
369 | |
---|
370 | return 0; |
---|
371 | } |
---|
372 | |
---|
373 | void MapStorage::writeToFile(const std::string &filename) |
---|
374 | { |
---|
375 | GraphWriter<Graph> gwriter(filename, graph); |
---|
376 | |
---|
377 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
378 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
379 | { |
---|
380 | gwriter.writeNodeMap(it->first, *(it->second)); |
---|
381 | } |
---|
382 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
383 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
384 | { |
---|
385 | if ((it->first != "arrow_pos_x") && |
---|
386 | (it->first != "arrow_pos_y")) |
---|
387 | { |
---|
388 | gwriter.writeEdgeMap(it->first, *(it->second)); |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | GuiWriter gui_writer(gwriter, this); |
---|
393 | |
---|
394 | gwriter.run(); |
---|
395 | } |
---|
396 | |
---|
397 | void MapStorage::clear() |
---|
398 | { |
---|
399 | for (std::map<std::string, Graph::NodeMap<double>*>::iterator it = |
---|
400 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
401 | { |
---|
402 | if ((it->first != "coordinates_x") && |
---|
403 | (it->first != "coordinates_y") && |
---|
404 | (it->first != "id")) |
---|
405 | { |
---|
406 | delete it->second; |
---|
407 | nodemap_storage.erase(it); |
---|
408 | } |
---|
409 | } |
---|
410 | for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it = |
---|
411 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
412 | { |
---|
413 | if ((it->first != "id") && |
---|
414 | (it->first != "arrow_pos_x") && |
---|
415 | (it->first != "arrow_pos_y")) |
---|
416 | { |
---|
417 | delete it->second; |
---|
418 | edgemap_storage.erase(it); |
---|
419 | } |
---|
420 | } |
---|
421 | for (std::map<std::string, double>::iterator it = |
---|
422 | nodemap_default.begin(); it != nodemap_default.end(); ++it) |
---|
423 | { |
---|
424 | if (it->first != "id") |
---|
425 | nodemap_default.erase(it); |
---|
426 | } |
---|
427 | for (std::map<std::string, double>::iterator it = |
---|
428 | edgemap_default.begin(); it != edgemap_default.end(); ++it) |
---|
429 | { |
---|
430 | if (it->first != "id") |
---|
431 | edgemap_default.erase(it); |
---|
432 | } |
---|
433 | graph.clear(); |
---|
434 | file_name = ""; |
---|
435 | modified = false; |
---|
436 | } |
---|
437 | |
---|
438 | void MapStorage::ArrowPosReadOK() |
---|
439 | { |
---|
440 | arrow_pos_read_ok = true; |
---|
441 | } |
---|
442 | |
---|
443 | void MapStorage::mapChanged(bool itisedge, std::string mapname) |
---|
444 | { |
---|
445 | if(itisedge) |
---|
446 | { |
---|
447 | for(int i=0;i<EDGE_PROPERTY_NUM;i++) |
---|
448 | { |
---|
449 | if(active_edgemaps[i]==mapname) |
---|
450 | { |
---|
451 | signal_prop.emit(itisedge, i); |
---|
452 | } |
---|
453 | } |
---|
454 | } |
---|
455 | else |
---|
456 | { |
---|
457 | for(int i=0;i<NODE_PROPERTY_NUM;i++) |
---|
458 | { |
---|
459 | if(active_nodemaps[i]==mapname) |
---|
460 | { |
---|
461 | signal_prop.emit(itisedge, i); |
---|
462 | } |
---|
463 | } |
---|
464 | } |
---|
465 | } |
---|