ladanyi@1606: #include "mapstorage.h" ladanyi@1645: #include ladanyi@1634: #include ladanyi@1442: deba@1686: #include deba@1686: ladanyi@1606: MapStorage::MapStorage() : modified(false), file_name("") ladanyi@1442: { ladanyi@1606: nodemap_storage["coordinates_x"] = new Graph::NodeMap(graph); ladanyi@1606: coords.setXMap(*nodemap_storage["coordinates_x"]); ladanyi@1606: nodemap_storage["coordinates_y"] = new Graph::NodeMap(graph); ladanyi@1606: coords.setYMap(*nodemap_storage["coordinates_y"]); ladanyi@1606: ladanyi@1606: nodemap_storage["id"] = new Graph::NodeMap(graph); ladanyi@1606: edgemap_storage["id"] = new Graph::EdgeMap(graph); ladanyi@1646: ladanyi@1646: nodemap_default["id"] = 1.0; ladanyi@1646: edgemap_default["id"] = 1.0; hegyi@1837: hegyi@1837: active_nodemaps.resize(NODE_PROPERTY_NUM); hegyi@1837: for(int i=0;i*>::const_iterator it = ladanyi@1606: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: delete it->second; ladanyi@1606: } ladanyi@1606: for (std::map*>::const_iterator it = ladanyi@1606: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: delete it->second; ladanyi@1606: } ladanyi@1606: } ladanyi@1442: ladanyi@1645: int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap *nodemap, double default_value = 0.0) ladanyi@1442: { hegyi@1597: if( nodemap_storage.find(name) == nodemap_storage.end() ) hegyi@1597: { hegyi@1597: nodemap_storage[name]=nodemap; ladanyi@1645: // set the maps default value ladanyi@1645: nodemap_default[name] = default_value; hegyi@1597: return 0; hegyi@1597: } hegyi@1597: return 1; ladanyi@1442: } ladanyi@1442: hegyi@1837: void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname) hegyi@1837: { hegyi@1837: if(itisedge) hegyi@1837: { hegyi@1837: active_edgemaps[prop]=mapname; hegyi@1837: } hegyi@1837: else hegyi@1837: { hegyi@1837: active_nodemaps[prop]=mapname; hegyi@1837: } hegyi@1837: signal_prop.emit(itisedge, prop); hegyi@1837: } hegyi@1837: hegyi@1837: std::string MapStorage::getActiveEdgeMap(int prop) hegyi@1837: { hegyi@1837: return active_edgemaps[prop]; hegyi@1837: } hegyi@1837: hegyi@1837: std::string MapStorage::getActiveNodeMap(int prop) hegyi@1837: { hegyi@1837: return active_nodemaps[prop]; hegyi@1837: } hegyi@1837: hegyi@1837: std::vector MapStorage::getEdgeMapList() hegyi@1837: { hegyi@1837: std::vector eml; hegyi@1837: eml.resize(edgemap_storage.size()); hegyi@1837: int i=0; hegyi@1837: std::map< std::string,Graph::EdgeMap * >::iterator emsi=beginOfEdgeMaps(); hegyi@1837: for(;emsi!=endOfEdgeMaps();emsi++) hegyi@1837: { hegyi@1837: eml[i]=(emsi->first); hegyi@1837: i++; hegyi@1837: } hegyi@1837: return eml; hegyi@1837: } hegyi@1837: hegyi@1837: std::vector MapStorage::getNodeMapList() hegyi@1837: { hegyi@1837: std::vector nml; hegyi@1837: nml.resize(nodemap_storage.size()); hegyi@1837: int i=0; hegyi@1837: std::map< std::string,Graph::NodeMap * >::iterator nmsi=beginOfNodeMaps(); hegyi@1837: for(;nmsi!=endOfNodeMaps();nmsi++) hegyi@1837: { hegyi@1837: nml[i]=(nmsi->first); hegyi@1837: i++; hegyi@1837: } hegyi@1837: return nml; hegyi@1837: } hegyi@1837: hegyi@1837: MapStorage::Signal_Prop MapStorage::signal_prop_ch() hegyi@1837: { hegyi@1837: return signal_prop; hegyi@1837: } hegyi@1837: ladanyi@1645: int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap *edgemap, double default_value = 0.0) ladanyi@1442: { hegyi@1597: if( edgemap_storage.find(name) == edgemap_storage.end() ) hegyi@1597: { hegyi@1597: edgemap_storage[name]=edgemap; ladanyi@1645: // set the maps default value ladanyi@1645: edgemap_default[name] = default_value; hegyi@1597: return 0; hegyi@1597: } hegyi@1597: return 1; ladanyi@1442: } ladanyi@1442: ladanyi@1442: double MapStorage::maxOfNodeMap(const std::string & name) ladanyi@1442: { ladanyi@1442: double max=0; ladanyi@1606: for (NodeIt j(graph); j!=INVALID; ++j) ladanyi@1442: { ladanyi@1442: if( (*nodemap_storage[name])[j]>max ) ladanyi@1442: { ladanyi@1442: max=(*nodemap_storage[name])[j]; ladanyi@1442: } ladanyi@1442: } ladanyi@1442: return max; ladanyi@1442: } ladanyi@1442: ladanyi@1442: double MapStorage::maxOfEdgeMap(const std::string & name) ladanyi@1442: { ladanyi@1442: double max=0; ladanyi@1606: for (EdgeIt j(graph); j!=INVALID; ++j) ladanyi@1442: { ladanyi@1442: if( (*edgemap_storage[name])[j]>max ) ladanyi@1442: { ladanyi@1442: max=(*edgemap_storage[name])[j]; ladanyi@1442: } ladanyi@1442: } ladanyi@1442: return max; ladanyi@1442: } ladanyi@1442: ladanyi@1442: double MapStorage::minOfNodeMap(const std::string & name) ladanyi@1442: { ladanyi@1606: NodeIt j(graph); hegyi@1617: double min; hegyi@1617: if(j!=INVALID) hegyi@1617: { hegyi@1617: min=(*nodemap_storage[name])[j]; hegyi@1617: } hegyi@1617: else hegyi@1617: { hegyi@1617: min=0; hegyi@1617: } ladanyi@1442: for (; j!=INVALID; ++j) ladanyi@1442: { ladanyi@1442: if( (*nodemap_storage[name])[j]& nodeMapNames = content.nodeSetMaps(0); ladanyi@1606: const std::vector& edgeMapNames = content.edgeSetMaps(0); ladanyi@1606: ladanyi@1606: GraphReader greader(filename, graph); ladanyi@1606: for (std::vector::const_iterator it = nodeMapNames.begin(); ladanyi@1606: it != nodeMapNames.end(); ++it) ladanyi@1606: { ladanyi@1606: if (*it == "coordinates_x") ladanyi@1606: { ladanyi@1606: read_x = true; ladanyi@1606: //std::cout << "read X nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: else if (*it == "coordinates_y") ladanyi@1606: { ladanyi@1606: read_y = true; ladanyi@1606: //std::cout << "read Y nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: else if (*it == "id") ladanyi@1606: { ladanyi@1606: //std::cout << "read id nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: else ladanyi@1606: { ladanyi@1606: nodemap_storage[*it] = new Graph::NodeMap(graph); ladanyi@1606: //std::cout << "read " << *it << " nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: greader.readNodeMap(*it, *nodemap_storage[*it]); ladanyi@1606: } ladanyi@1606: for (std::vector::const_iterator it = edgeMapNames.begin(); ladanyi@1606: it != edgeMapNames.end(); ++it) ladanyi@1606: { ladanyi@1606: if (*it == "id") ladanyi@1606: { ladanyi@1606: //std::cout << "read id edgemap" << std::endl; ladanyi@1606: } ladanyi@1606: else ladanyi@1606: { ladanyi@1606: edgemap_storage[*it] = new Graph::EdgeMap(graph); ladanyi@1606: //std::cout << "read " << *it << " edgemap" << std::endl; ladanyi@1606: } ladanyi@1606: greader.readEdgeMap(*it, *edgemap_storage[*it]); ladanyi@1606: } ladanyi@1606: greader.run(); ladanyi@1606: } catch (DataFormatError& error) { ladanyi@1708: Gtk::MessageDialog mdialog(error.what()); ladanyi@1606: mdialog.run(); ladanyi@1645: clear(); ladanyi@1645: return 1; ladanyi@1606: } ladanyi@1606: ladanyi@1646: if (!read_edge_id) ladanyi@1646: { ladanyi@1646: edgemap_storage["id"] = new Graph::EdgeMap(graph); ladanyi@1646: int i = 1; ladanyi@1646: for (EdgeIt e(graph); e != INVALID; ++e) ladanyi@1646: { ladanyi@1646: (*edgemap_storage["id"])[e] = i++; ladanyi@1646: } ladanyi@1646: } ladanyi@1646: ladanyi@1606: if (!read_x || !read_y) ladanyi@1606: { ladanyi@1606: int node_num = 0; ladanyi@1606: for (NodeIt n(graph); n != INVALID; ++n) ladanyi@1606: { ladanyi@1606: node_num++; ladanyi@1606: } ladanyi@1606: const double pi = 3.142; ladanyi@1606: double step = 2 * pi / (double) node_num; ladanyi@1606: int i = 0; ladanyi@1606: for (NodeIt n(graph); n != INVALID; ++n) ladanyi@1606: { deba@1686: nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step)); deba@1686: nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step)); ladanyi@1606: i++; ladanyi@1606: } ladanyi@1606: } ladanyi@1645: ladanyi@1645: // fill in the default values for the maps ladanyi@1645: for (std::map*>::const_iterator it = ladanyi@1645: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@1645: { ladanyi@1645: if ((it->first != "id") && ladanyi@1645: (it->first != "coordiantes_x") && ladanyi@1645: (it->first != "coordinates_y")) ladanyi@1645: { ladanyi@1645: nodemap_default[it->first] = 0.0; ladanyi@1645: } ladanyi@1646: else if (it->first == "id") ladanyi@1646: { ladanyi@1646: NodeIt n(graph); ladanyi@1646: double max = (*nodemap_storage["id"])[n]; ladanyi@1646: for (; n != INVALID; ++n) ladanyi@1646: { ladanyi@1646: if ((*nodemap_storage["id"])[n] > max) ladanyi@1646: max = (*nodemap_storage["id"])[n]; ladanyi@1646: } ladanyi@1646: nodemap_default["id"] = max + 1.0; ladanyi@1646: } ladanyi@1645: } ladanyi@1645: for (std::map*>::const_iterator it = ladanyi@1645: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@1645: { ladanyi@1645: if (it->first != "id") ladanyi@1645: { ladanyi@1645: edgemap_default[it->first] = 0.0; ladanyi@1645: } ladanyi@1646: else ladanyi@1646: { ladanyi@1649: double max = std::numeric_limits::min(); ladanyi@1649: for (EdgeIt e(graph); e != INVALID; ++e) ladanyi@1646: { ladanyi@1646: if ((*edgemap_storage["id"])[e] > max) ladanyi@1646: max = (*edgemap_storage["id"])[e]; ladanyi@1646: } ladanyi@1649: if (max > std::numeric_limits::min()) ladanyi@1649: edgemap_default["id"] = max + 1.0; ladanyi@1649: else ladanyi@1649: edgemap_default["id"] = 1.0; ladanyi@1646: } ladanyi@1645: } ladanyi@1645: ladanyi@1645: return 0; ladanyi@1606: } ladanyi@1606: ladanyi@1606: void MapStorage::writeToFile(const std::string &filename) ladanyi@1606: { ladanyi@1606: GraphWriter gwriter(filename, graph); ladanyi@1606: ladanyi@1606: for (std::map*>::const_iterator it = ladanyi@1606: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: gwriter.writeNodeMap(it->first, *(it->second)); ladanyi@1606: //std::cout << "wrote " << it->first << " nodemap" << std::endl; ladanyi@1606: } ladanyi@1606: for (std::map*>::const_iterator it = ladanyi@1606: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: gwriter.writeEdgeMap(it->first, *(it->second)); ladanyi@1606: //std::cout << "wrote " << it->first << " edgemap" << std::endl; ladanyi@1606: } ladanyi@1606: gwriter.run(); ladanyi@1606: } ladanyi@1606: ladanyi@1606: void MapStorage::clear() ladanyi@1606: { ladanyi@1606: for (std::map*>::iterator it = ladanyi@1606: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: if ((it->first != "coordinates_x") && ladanyi@1606: (it->first != "coordinates_y") && ladanyi@1606: (it->first != "id")) ladanyi@1606: { ladanyi@1606: delete it->second; ladanyi@1606: nodemap_storage.erase(it); ladanyi@1606: } ladanyi@1606: } ladanyi@1606: for (std::map*>::iterator it = ladanyi@1606: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@1606: { ladanyi@1606: if (it->first != "id") ladanyi@1606: { ladanyi@1606: delete it->second; ladanyi@1606: edgemap_storage.erase(it); ladanyi@1606: } ladanyi@1606: } ladanyi@1645: for (std::map::iterator it = ladanyi@1645: nodemap_default.begin(); it != nodemap_default.end(); ++it) ladanyi@1645: { ladanyi@1646: if (it->first != "id") ladanyi@1646: nodemap_default.erase(it); ladanyi@1645: } ladanyi@1645: for (std::map::iterator it = ladanyi@1645: edgemap_default.begin(); it != edgemap_default.end(); ++it) ladanyi@1645: { ladanyi@1646: if (it->first != "id") ladanyi@1646: edgemap_default.erase(it); ladanyi@1645: } ladanyi@1606: graph.clear(); ladanyi@1606: file_name = ""; ladanyi@1606: modified = false; ladanyi@1606: }