1 #include <mapstorage.h> |
1 #include "mapstorage.h" |
2 |
2 |
3 MapStorage::MapStorage(Graph & graph):g(graph) |
3 MapStorage::MapStorage() : modified(false), file_name("") |
4 { |
4 { |
5 }; |
5 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph); |
|
6 coords.setXMap(*nodemap_storage["coordinates_x"]); |
|
7 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph); |
|
8 coords.setYMap(*nodemap_storage["coordinates_y"]); |
|
9 |
|
10 nodemap_storage["id"] = new Graph::NodeMap<double>(graph); |
|
11 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph); |
|
12 } |
|
13 |
|
14 MapStorage::~MapStorage() |
|
15 { |
|
16 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
|
17 nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
|
18 { |
|
19 delete it->second; |
|
20 } |
|
21 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
|
22 edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
|
23 { |
|
24 delete it->second; |
|
25 } |
|
26 } |
6 |
27 |
7 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap) |
28 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap) |
8 { |
29 { |
9 if( nodemap_storage.find(name) == nodemap_storage.end() ) |
30 if( nodemap_storage.find(name) == nodemap_storage.end() ) |
10 { |
31 { |
84 for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++) |
105 for(ems_it=edgemap_storage.begin();ems_it!=edgemap_storage.end();ems_it++) |
85 { |
106 { |
86 (*((*ems_it).second))[e]=5; |
107 (*((*ems_it).second))[e]=5; |
87 } |
108 } |
88 } |
109 } |
|
110 |
|
111 void MapStorage::readFromFile(const std::string &filename) |
|
112 { |
|
113 bool read_x = false; |
|
114 bool read_y = false; |
|
115 |
|
116 try { |
|
117 LemonReader lreader(filename); |
|
118 ContentReader content(lreader); |
|
119 lreader.run(); |
|
120 |
|
121 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0); |
|
122 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0); |
|
123 |
|
124 GraphReader<Graph> greader(filename, graph); |
|
125 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin(); |
|
126 it != nodeMapNames.end(); ++it) |
|
127 { |
|
128 if (*it == "coordinates_x") |
|
129 { |
|
130 read_x = true; |
|
131 //std::cout << "read X nodemap" << std::endl; |
|
132 } |
|
133 else if (*it == "coordinates_y") |
|
134 { |
|
135 read_y = true; |
|
136 //std::cout << "read Y nodemap" << std::endl; |
|
137 } |
|
138 else if (*it == "id") |
|
139 { |
|
140 //std::cout << "read id nodemap" << std::endl; |
|
141 } |
|
142 else |
|
143 { |
|
144 nodemap_storage[*it] = new Graph::NodeMap<double>(graph); |
|
145 //std::cout << "read " << *it << " nodemap" << std::endl; |
|
146 } |
|
147 greader.readNodeMap(*it, *nodemap_storage[*it]); |
|
148 } |
|
149 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin(); |
|
150 it != edgeMapNames.end(); ++it) |
|
151 { |
|
152 if (*it == "id") |
|
153 { |
|
154 //std::cout << "read id edgemap" << std::endl; |
|
155 } |
|
156 else |
|
157 { |
|
158 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph); |
|
159 //std::cout << "read " << *it << " edgemap" << std::endl; |
|
160 } |
|
161 greader.readEdgeMap(*it, *edgemap_storage[*it]); |
|
162 } |
|
163 greader.run(); |
|
164 } catch (DataFormatError& error) { |
|
165 /* |
|
166 Gtk::MessageDialog mdialog("Read Error"); |
|
167 mdialog.set_message(error.what()); |
|
168 mdialog.run(); |
|
169 */ |
|
170 // reset graph and mapstorage ? |
|
171 return; |
|
172 } |
|
173 |
|
174 if (!read_x || !read_y) |
|
175 { |
|
176 int node_num = 0; |
|
177 for (NodeIt n(graph); n != INVALID; ++n) |
|
178 { |
|
179 node_num++; |
|
180 } |
|
181 const double pi = 3.142; |
|
182 double step = 2 * pi / (double) node_num; |
|
183 int i = 0; |
|
184 for (NodeIt n(graph); n != INVALID; ++n) |
|
185 { |
|
186 nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step)); |
|
187 nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step)); |
|
188 i++; |
|
189 } |
|
190 } |
|
191 } |
|
192 |
|
193 void MapStorage::writeToFile(const std::string &filename) |
|
194 { |
|
195 GraphWriter<Graph> gwriter(filename, graph); |
|
196 |
|
197 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it = |
|
198 nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
|
199 { |
|
200 gwriter.writeNodeMap(it->first, *(it->second)); |
|
201 //std::cout << "wrote " << it->first << " nodemap" << std::endl; |
|
202 } |
|
203 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
|
204 edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
|
205 { |
|
206 gwriter.writeEdgeMap(it->first, *(it->second)); |
|
207 //std::cout << "wrote " << it->first << " edgemap" << std::endl; |
|
208 } |
|
209 gwriter.run(); |
|
210 } |
|
211 |
|
212 void MapStorage::clear() |
|
213 { |
|
214 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it = |
|
215 nodemap_storage.begin(); it != nodemap_storage.end(); ++it) |
|
216 { |
|
217 if ((it->first != "coordinates_x") && |
|
218 (it->first != "coordinates_y") && |
|
219 (it->first != "id")) |
|
220 { |
|
221 delete it->second; |
|
222 nodemap_storage.erase(it); |
|
223 } |
|
224 } |
|
225 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it = |
|
226 edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
|
227 { |
|
228 if (it->first != "id") |
|
229 { |
|
230 delete it->second; |
|
231 edgemap_storage.erase(it); |
|
232 } |
|
233 } |
|
234 graph.clear(); |
|
235 file_name = ""; |
|
236 modified = false; |
|
237 } |