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