1 | #include "mapstorage.h" |
---|
2 | #include <cmath> |
---|
3 | |
---|
4 | MapStorage::MapStorage() : modified(false), file_name("") |
---|
5 | { |
---|
6 | nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph); |
---|
7 | coords.setXMap(*nodemap_storage["coordinates_x"]); |
---|
8 | nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph); |
---|
9 | coords.setYMap(*nodemap_storage["coordinates_y"]); |
---|
10 | |
---|
11 | nodemap_storage["id"] = new Graph::NodeMap<double>(graph); |
---|
12 | edgemap_storage["id"] = new Graph::EdgeMap<double>(graph); |
---|
13 | } |
---|
14 | |
---|
15 | MapStorage::~MapStorage() |
---|
16 | { |
---|
17 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
18 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
19 | { |
---|
20 | delete it->second; |
---|
21 | } |
---|
22 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
23 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
24 | { |
---|
25 | delete it->second; |
---|
26 | } |
---|
27 | } |
---|
28 | |
---|
29 | int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap) |
---|
30 | { |
---|
31 | if( nodemap_storage.find(name) == nodemap_storage.end() ) |
---|
32 | { |
---|
33 | nodemap_storage[name]=nodemap; |
---|
34 | return 0; |
---|
35 | } |
---|
36 | return 1; |
---|
37 | } |
---|
38 | |
---|
39 | int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap) |
---|
40 | { |
---|
41 | if( edgemap_storage.find(name) == edgemap_storage.end() ) |
---|
42 | { |
---|
43 | edgemap_storage[name]=edgemap; |
---|
44 | return 0; |
---|
45 | } |
---|
46 | return 1; |
---|
47 | } |
---|
48 | |
---|
49 | double MapStorage::maxOfNodeMap(const std::string & name) |
---|
50 | { |
---|
51 | double max=0; |
---|
52 | for (NodeIt j(graph); j!=INVALID; ++j) |
---|
53 | { |
---|
54 | if( (*nodemap_storage[name])[j]>max ) |
---|
55 | { |
---|
56 | max=(*nodemap_storage[name])[j]; |
---|
57 | } |
---|
58 | } |
---|
59 | return max; |
---|
60 | } |
---|
61 | |
---|
62 | double MapStorage::maxOfEdgeMap(const std::string & name) |
---|
63 | { |
---|
64 | double max=0; |
---|
65 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
66 | { |
---|
67 | if( (*edgemap_storage[name])[j]>max ) |
---|
68 | { |
---|
69 | max=(*edgemap_storage[name])[j]; |
---|
70 | } |
---|
71 | } |
---|
72 | return max; |
---|
73 | } |
---|
74 | |
---|
75 | double MapStorage::minOfNodeMap(const std::string & name) |
---|
76 | { |
---|
77 | NodeIt j(graph); |
---|
78 | double min; |
---|
79 | if(j!=INVALID) |
---|
80 | { |
---|
81 | min=(*nodemap_storage[name])[j]; |
---|
82 | } |
---|
83 | else |
---|
84 | { |
---|
85 | min=0; |
---|
86 | } |
---|
87 | for (; j!=INVALID; ++j) |
---|
88 | { |
---|
89 | if( (*nodemap_storage[name])[j]<min ) |
---|
90 | { |
---|
91 | min=(*nodemap_storage[name])[j]; |
---|
92 | } |
---|
93 | } |
---|
94 | return min; |
---|
95 | } |
---|
96 | |
---|
97 | double MapStorage::minOfEdgeMap(const std::string & name) |
---|
98 | { |
---|
99 | EdgeIt j(graph); |
---|
100 | double min; |
---|
101 | if(j!=INVALID) |
---|
102 | { |
---|
103 | min=(*edgemap_storage[name])[j]; |
---|
104 | } |
---|
105 | else |
---|
106 | { |
---|
107 | min=0; |
---|
108 | } |
---|
109 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
110 | { |
---|
111 | if( (*edgemap_storage[name])[j]<min ) |
---|
112 | { |
---|
113 | min=(*edgemap_storage[name])[j]; |
---|
114 | } |
---|
115 | } |
---|
116 | return min; |
---|
117 | } |
---|
118 | |
---|
119 | void MapStorage::initMapsForEdge(Graph::Edge e) |
---|
120 | { |
---|
121 | std::map< std::string,Graph::EdgeMap<double> * >::iterator ems_it; |
---|
122 | for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++) |
---|
123 | { |
---|
124 | (*((*ems_it).second))[e]=5; |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | void MapStorage::readFromFile(const std::string &filename) |
---|
129 | { |
---|
130 | bool read_x = false; |
---|
131 | bool read_y = 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 | /* |
---|
183 | Gtk::MessageDialog mdialog("Read Error"); |
---|
184 | mdialog.set_message(error.what()); |
---|
185 | mdialog.run(); |
---|
186 | */ |
---|
187 | // reset graph and mapstorage ? |
---|
188 | return; |
---|
189 | } |
---|
190 | |
---|
191 | if (!read_x || !read_y) |
---|
192 | { |
---|
193 | int node_num = 0; |
---|
194 | for (NodeIt n(graph); n != INVALID; ++n) |
---|
195 | { |
---|
196 | node_num++; |
---|
197 | } |
---|
198 | const double pi = 3.142; |
---|
199 | double step = 2 * pi / (double) node_num; |
---|
200 | int i = 0; |
---|
201 | for (NodeIt n(graph); n != INVALID; ++n) |
---|
202 | { |
---|
203 | nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step)); |
---|
204 | nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step)); |
---|
205 | i++; |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | void MapStorage::writeToFile(const std::string &filename) |
---|
211 | { |
---|
212 | GraphWriter<Graph> gwriter(filename, graph); |
---|
213 | |
---|
214 | for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
---|
215 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
216 | { |
---|
217 | gwriter.writeNodeMap(it->first, *(it->second)); |
---|
218 | //std::cout << "wrote " << it->first << " nodemap" << std::endl; |
---|
219 | } |
---|
220 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
221 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
222 | { |
---|
223 | gwriter.writeEdgeMap(it->first, *(it->second)); |
---|
224 | //std::cout << "wrote " << it->first << " edgemap" << std::endl; |
---|
225 | } |
---|
226 | gwriter.run(); |
---|
227 | } |
---|
228 | |
---|
229 | void MapStorage::clear() |
---|
230 | { |
---|
231 | for (std::map<std::string, Graph::NodeMap<double>*>::iterator it = |
---|
232 | nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
---|
233 | { |
---|
234 | if ((it->first != "coordinates_x") && |
---|
235 | (it->first != "coordinates_y") && |
---|
236 | (it->first != "id")) |
---|
237 | { |
---|
238 | delete it->second; |
---|
239 | nodemap_storage.erase(it); |
---|
240 | } |
---|
241 | } |
---|
242 | for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it = |
---|
243 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
244 | { |
---|
245 | if (it->first != "id") |
---|
246 | { |
---|
247 | delete it->second; |
---|
248 | edgemap_storage.erase(it); |
---|
249 | } |
---|
250 | } |
---|
251 | graph.clear(); |
---|
252 | file_name = ""; |
---|
253 | modified = false; |
---|
254 | } |
---|