alpar@174: /* -*- C++ -*- alpar@174: * alpar@174: * This file is a part of LEMON, a generic C++ optimization library alpar@174: * alpar@174: * Copyright (C) 2003-2006 alpar@174: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@174: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@174: * alpar@174: * Permission to use, modify and distribute this software is granted alpar@174: * provided that this copyright notice appears in all copies. For alpar@174: * precise terms see the accompanying LICENSE file. alpar@174: * alpar@174: * This software is provided "AS IS" with no warranty of any kind, alpar@174: * express or implied, and with no claim as to its suitability for any alpar@174: * purpose. alpar@174: * alpar@174: */ alpar@174: ladanyi@53: #include "mapstorage.h" ladanyi@184: #include "nbtab.h" ladanyi@98: #include "gui_writer.h" ladanyi@98: #include "gui_reader.h" alpar@128: #include alpar@128: #include ladanyi@63: #include deba@77: hegyi@177: const double i_d=20; hegyi@177: const double a_d=0.05; hegyi@177: const double p_d=40000; hegyi@177: ladanyi@184: MapStorage::MapStorage(NoteBookTab& tab) : mytab(tab), modified(false), file_name(""), arrow_pos_read_ok(false), iterations(i_d), attraction(a_d), propulsation(p_d), background_set(false) ladanyi@6: { ladanyi@53: nodemap_storage["coordinates_x"] = new Graph::NodeMap(graph); ladanyi@53: coords.setXMap(*nodemap_storage["coordinates_x"]); ladanyi@53: nodemap_storage["coordinates_y"] = new Graph::NodeMap(graph); ladanyi@53: coords.setYMap(*nodemap_storage["coordinates_y"]); ladanyi@53: ladanyi@98: edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap(graph); ladanyi@98: arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]); ladanyi@98: edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap(graph); ladanyi@98: arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]); ladanyi@98: ladanyi@134: nodemap_storage["label"] = new Graph::NodeMap(graph); ladanyi@134: edgemap_storage["label"] = new Graph::EdgeMap(graph); ladanyi@64: ladanyi@134: nodemap_default["label"] = 1.0; ladanyi@134: edgemap_default["label"] = 1.0; hegyi@94: hegyi@94: active_nodemaps.resize(NODE_PROPERTY_NUM); hegyi@94: for(int i=0;i*>::const_iterator it = ladanyi@53: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@53: { ladanyi@53: delete it->second; ladanyi@53: } ladanyi@53: for (std::map*>::const_iterator it = ladanyi@53: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@53: { ladanyi@53: delete it->second; ladanyi@53: } ladanyi@53: } ladanyi@6: hegyi@118: int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap *nodemap, double default_value) ladanyi@6: { hegyi@46: if( nodemap_storage.find(name) == nodemap_storage.end() ) hegyi@46: { hegyi@46: nodemap_storage[name]=nodemap; ladanyi@63: // set the maps default value ladanyi@63: nodemap_default[name] = default_value; hegyi@108: hegyi@108: //announce changement in maps hegyi@108: signal_node_map.emit(name); hegyi@46: return 0; hegyi@46: } hegyi@46: return 1; ladanyi@6: } ladanyi@6: hegyi@94: void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname) hegyi@94: { hegyi@94: if(itisedge) hegyi@94: { hegyi@94: active_edgemaps[prop]=mapname; hegyi@94: } hegyi@94: else hegyi@94: { hegyi@94: active_nodemaps[prop]=mapname; hegyi@94: } hegyi@94: signal_prop.emit(itisedge, prop); hegyi@94: } hegyi@94: hegyi@172: void MapStorage::broadcastActiveMaps() hegyi@172: { hegyi@172: for(int i=0;i MapStorage::getEdgeMapList() hegyi@94: { hegyi@94: std::vector eml; hegyi@94: eml.resize(edgemap_storage.size()); hegyi@94: int i=0; hegyi@94: std::map< std::string,Graph::EdgeMap * >::iterator emsi=beginOfEdgeMaps(); hegyi@94: for(;emsi!=endOfEdgeMaps();emsi++) hegyi@94: { hegyi@94: eml[i]=(emsi->first); hegyi@94: i++; hegyi@94: } hegyi@94: return eml; hegyi@94: } hegyi@94: hegyi@94: std::vector MapStorage::getNodeMapList() hegyi@94: { hegyi@94: std::vector nml; hegyi@94: nml.resize(nodemap_storage.size()); hegyi@94: int i=0; hegyi@94: std::map< std::string,Graph::NodeMap * >::iterator nmsi=beginOfNodeMaps(); hegyi@94: for(;nmsi!=endOfNodeMaps();nmsi++) hegyi@94: { hegyi@94: nml[i]=(nmsi->first); hegyi@94: i++; hegyi@94: } hegyi@94: return nml; hegyi@94: } hegyi@94: hegyi@177: sigc::signal MapStorage::signal_prop_ch() hegyi@94: { hegyi@94: return signal_prop; hegyi@94: } hegyi@94: hegyi@118: int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap *edgemap, double default_value) ladanyi@6: { hegyi@46: if( edgemap_storage.find(name) == edgemap_storage.end() ) hegyi@46: { hegyi@46: edgemap_storage[name]=edgemap; ladanyi@63: // set the maps default value ladanyi@63: edgemap_default[name] = default_value; hegyi@108: hegyi@108: //announce changement in maps hegyi@108: signal_edge_map.emit(name); hegyi@46: return 0; hegyi@46: } hegyi@46: return 1; ladanyi@6: } ladanyi@6: ladanyi@6: double MapStorage::maxOfNodeMap(const std::string & name) ladanyi@6: { ladanyi@6: double max=0; ladanyi@53: for (NodeIt j(graph); j!=INVALID; ++j) ladanyi@6: { ladanyi@6: if( (*nodemap_storage[name])[j]>max ) ladanyi@6: { ladanyi@6: max=(*nodemap_storage[name])[j]; ladanyi@6: } ladanyi@6: } ladanyi@6: return max; ladanyi@6: } ladanyi@6: ladanyi@6: double MapStorage::maxOfEdgeMap(const std::string & name) ladanyi@6: { ladanyi@6: double max=0; ladanyi@53: for (EdgeIt j(graph); j!=INVALID; ++j) ladanyi@6: { ladanyi@6: if( (*edgemap_storage[name])[j]>max ) ladanyi@6: { ladanyi@6: max=(*edgemap_storage[name])[j]; ladanyi@6: } ladanyi@6: } ladanyi@6: return max; ladanyi@6: } ladanyi@6: ladanyi@6: double MapStorage::minOfNodeMap(const std::string & name) ladanyi@6: { ladanyi@53: NodeIt j(graph); hegyi@58: double min; hegyi@58: if(j!=INVALID) hegyi@58: { hegyi@58: min=(*nodemap_storage[name])[j]; hegyi@58: } hegyi@58: else hegyi@58: { hegyi@58: min=0; hegyi@58: } ladanyi@6: for (; j!=INVALID; ++j) ladanyi@6: { ladanyi@6: if( (*nodemap_storage[name])[j]& nodeMapNames = content.nodeSetMaps(0); ladanyi@53: const std::vector& edgeMapNames = content.edgeSetMaps(0); ladanyi@53: ladanyi@53: GraphReader greader(filename, graph); ladanyi@53: for (std::vector::const_iterator it = nodeMapNames.begin(); ladanyi@53: it != nodeMapNames.end(); ++it) ladanyi@53: { ladanyi@53: if (*it == "coordinates_x") ladanyi@53: { ladanyi@53: read_x = true; ladanyi@53: //std::cout << "read X nodemap" << std::endl; ladanyi@53: } ladanyi@53: else if (*it == "coordinates_y") ladanyi@53: { ladanyi@53: read_y = true; ladanyi@53: //std::cout << "read Y nodemap" << std::endl; ladanyi@53: } ladanyi@134: else if (*it == "label") ladanyi@53: { ladanyi@53: //std::cout << "read id nodemap" << std::endl; ladanyi@53: } ladanyi@53: else ladanyi@53: { ladanyi@53: nodemap_storage[*it] = new Graph::NodeMap(graph); ladanyi@53: //std::cout << "read " << *it << " nodemap" << std::endl; ladanyi@53: } ladanyi@53: greader.readNodeMap(*it, *nodemap_storage[*it]); ladanyi@53: } ladanyi@53: for (std::vector::const_iterator it = edgeMapNames.begin(); ladanyi@53: it != edgeMapNames.end(); ++it) ladanyi@53: { ladanyi@134: if (*it == "label") ladanyi@53: { ladanyi@53: //std::cout << "read id edgemap" << std::endl; ladanyi@135: read_edge_id = true; ladanyi@53: } ladanyi@53: else ladanyi@53: { ladanyi@53: edgemap_storage[*it] = new Graph::EdgeMap(graph); ladanyi@53: //std::cout << "read " << *it << " edgemap" << std::endl; ladanyi@53: } ladanyi@53: greader.readEdgeMap(*it, *edgemap_storage[*it]); ladanyi@53: } ladanyi@98: GuiReader gui_reader(greader, this); ladanyi@53: greader.run(); ladanyi@101: } catch (Exception& error) { ladanyi@79: Gtk::MessageDialog mdialog(error.what()); ladanyi@53: mdialog.run(); ladanyi@63: clear(); ladanyi@63: return 1; ladanyi@53: } ladanyi@53: ladanyi@64: if (!read_edge_id) ladanyi@64: { ladanyi@134: edgemap_storage["label"] = new Graph::EdgeMap(graph); ladanyi@64: int i = 1; ladanyi@64: for (EdgeIt e(graph); e != INVALID; ++e) ladanyi@64: { ladanyi@134: (*edgemap_storage["label"])[e] = i++; ladanyi@64: } ladanyi@64: } ladanyi@64: ladanyi@53: if (!read_x || !read_y) ladanyi@53: { ladanyi@53: int node_num = 0; ladanyi@53: for (NodeIt n(graph); n != INVALID; ++n) ladanyi@53: { ladanyi@53: node_num++; ladanyi@53: } ladanyi@53: const double pi = 3.142; ladanyi@53: double step = 2 * pi / (double) node_num; ladanyi@53: int i = 0; ladanyi@53: for (NodeIt n(graph); n != INVALID; ++n) ladanyi@53: { deba@77: nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step)); deba@77: nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step)); ladanyi@53: i++; ladanyi@53: } ladanyi@53: } ladanyi@63: ladanyi@98: if (!arrow_pos_read_ok) ladanyi@98: { ladanyi@98: arrow_pos_read_ok = false; ladanyi@98: for (EdgeIt e(graph); e != INVALID; ++e) ladanyi@98: { ladanyi@151: if (graph.source(e) == graph.target(e)) ladanyi@151: { ladanyi@151: arrow_pos.set(e, coords[graph.source(e)] + XY(0.0, 80.0)); ladanyi@151: } ladanyi@151: else ladanyi@151: { ladanyi@151: arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0); ladanyi@151: } ladanyi@98: } ladanyi@98: } ladanyi@98: ladanyi@63: // fill in the default values for the maps ladanyi@63: for (std::map*>::const_iterator it = ladanyi@63: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@63: { ladanyi@134: if ((it->first != "label") && ladanyi@63: (it->first != "coordiantes_x") && ladanyi@63: (it->first != "coordinates_y")) ladanyi@63: { ladanyi@63: nodemap_default[it->first] = 0.0; ladanyi@63: } ladanyi@134: else if (it->first == "label") ladanyi@64: { ladanyi@64: NodeIt n(graph); ladanyi@134: double max = (*nodemap_storage["label"])[n]; ladanyi@64: for (; n != INVALID; ++n) ladanyi@64: { ladanyi@134: if ((*nodemap_storage["label"])[n] > max) ladanyi@134: max = (*nodemap_storage["label"])[n]; ladanyi@64: } ladanyi@134: nodemap_default["label"] = max + 1.0; ladanyi@64: } ladanyi@63: } ladanyi@63: for (std::map*>::const_iterator it = ladanyi@63: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@63: { ladanyi@134: if (it->first != "label") ladanyi@63: { ladanyi@63: edgemap_default[it->first] = 0.0; ladanyi@63: } ladanyi@64: else ladanyi@64: { ladanyi@67: double max = std::numeric_limits::min(); ladanyi@67: for (EdgeIt e(graph); e != INVALID; ++e) ladanyi@64: { ladanyi@134: if ((*edgemap_storage["label"])[e] > max) ladanyi@134: max = (*edgemap_storage["label"])[e]; ladanyi@64: } ladanyi@67: if (max > std::numeric_limits::min()) ladanyi@134: edgemap_default["label"] = max + 1.0; ladanyi@67: else ladanyi@134: edgemap_default["label"] = 1.0; ladanyi@64: } ladanyi@63: } ladanyi@63: ladanyi@63: return 0; ladanyi@53: } ladanyi@53: ladanyi@53: void MapStorage::writeToFile(const std::string &filename) ladanyi@53: { ladanyi@53: GraphWriter gwriter(filename, graph); ladanyi@53: ladanyi@53: for (std::map*>::const_iterator it = ladanyi@53: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@53: { ladanyi@53: gwriter.writeNodeMap(it->first, *(it->second)); ladanyi@53: } ladanyi@53: for (std::map*>::const_iterator it = ladanyi@53: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@53: { ladanyi@98: if ((it->first != "arrow_pos_x") && ladanyi@98: (it->first != "arrow_pos_y")) ladanyi@98: { ladanyi@98: gwriter.writeEdgeMap(it->first, *(it->second)); ladanyi@98: } ladanyi@53: } ladanyi@98: ladanyi@98: GuiWriter gui_writer(gwriter, this); ladanyi@98: ladanyi@53: gwriter.run(); ladanyi@53: } ladanyi@53: ladanyi@53: void MapStorage::clear() ladanyi@53: { ladanyi@53: for (std::map*>::iterator it = ladanyi@53: nodemap_storage.begin(); it != nodemap_storage.end(); ++it) ladanyi@53: { ladanyi@53: if ((it->first != "coordinates_x") && ladanyi@53: (it->first != "coordinates_y") && ladanyi@134: (it->first != "label")) ladanyi@53: { ladanyi@53: delete it->second; ladanyi@53: nodemap_storage.erase(it); ladanyi@53: } ladanyi@53: } ladanyi@53: for (std::map*>::iterator it = ladanyi@53: edgemap_storage.begin(); it != edgemap_storage.end(); ++it) ladanyi@53: { ladanyi@134: if ((it->first != "label") && ladanyi@98: (it->first != "arrow_pos_x") && ladanyi@98: (it->first != "arrow_pos_y")) ladanyi@53: { ladanyi@53: delete it->second; ladanyi@53: edgemap_storage.erase(it); ladanyi@53: } ladanyi@53: } ladanyi@63: for (std::map::iterator it = ladanyi@63: nodemap_default.begin(); it != nodemap_default.end(); ++it) ladanyi@63: { ladanyi@134: if (it->first != "label") ladanyi@64: nodemap_default.erase(it); ladanyi@63: } ladanyi@63: for (std::map::iterator it = ladanyi@63: edgemap_default.begin(); it != edgemap_default.end(); ++it) ladanyi@63: { ladanyi@134: if (it->first != "label") ladanyi@64: edgemap_default.erase(it); ladanyi@63: } ladanyi@53: graph.clear(); ladanyi@53: file_name = ""; ladanyi@53: modified = false; hegyi@172: hegyi@172: arrow_pos_read_ok = false; hegyi@172: hegyi@172: for(int i=0;isetBackground(); ladanyi@184: } ladanyi@184: ladanyi@184: const std::string& MapStorage::getBackgroundFilename() ladanyi@184: { ladanyi@184: return background_file_name; ladanyi@184: } ladanyi@184: ladanyi@184: bool MapStorage::isBackgroundSet() ladanyi@184: { ladanyi@184: return background_set; ladanyi@184: } ladanyi@184: ladanyi@184: double MapStorage::getBackgroundScaling() ladanyi@184: { ladanyi@184: return background_scaling; ladanyi@184: } ladanyi@184: ladanyi@184: void MapStorage::setBackgroundScaling(double scaling) ladanyi@184: { ladanyi@184: background_scaling = scaling; ladanyi@184: }