index 112a942a7ecd238ebad67190ca10b6bd5eafbed7..60cdab8146c6490e07e810a1ca915e8385a0a243 100644 (file)
*/
/*
* Authors:
-* Tim Dwyer <tgdwyer@gmail.com>
+* Tim Dwyer <Tim.Dwyer@infotech.monash.edu.au>
*
* Copyright (C) 2005 Authors
*
* Released under GNU GPL. Read the file 'COPYING' for more information.
*/
-#include "graphlayout/graphlayout.h"
#include <iostream>
#include <config.h>
+#include <map>
+#include <vector>
+#include <algorithm>
+#include <cstring>
+#include <cstdlib>
+#include <float.h>
-#ifdef HAVE_BOOST_GRAPH_LIB
+#include "desktop.h"
+#include "inkscape.h"
+#include "sp-namedview.h"
+#include "util/glib-list-iterators.h"
+#include "graphlayout/graphlayout.h"
#include "sp-path.h"
#include "sp-item.h"
#include "sp-item-transform.h"
#include "sp-conn-end-pair.h"
+#include "style.h"
#include "conn-avoid-ref.h"
#include "libavoid/connector.h"
#include "libavoid/geomtypes.h"
-#include <boost/graph/kamada_kawai_spring_layout.hpp>
-#include <boost/graph/circle_layout.hpp>
-#include <boost/graph/adjacency_list.hpp>
-#include <boost/graph/graphviz.hpp>
-#include <map>
-#include <vector>
-#include <algorithm>
-#include <float.h>
+#include "libcola/cola.h"
+#include "libvpsc/generate-constraints.h"
+#include "prefs-utils.h"
-using namespace boost;
-
-// create a typedef for the Graph type
-typedef adjacency_list<vecS, vecS, undirectedS, no_property,
- property<edge_weight_t, double> > Graph;
-typedef property_map<Graph, edge_weight_t>::type WeightMap;
-typedef graph_traits<Graph>::vertex_descriptor Vertex;
-typedef std::vector<Avoid::Point> PositionVec;
-typedef iterator_property_map<PositionVec::iterator, property_map<Graph, vertex_index_t>::type> PositionMap;
-
-bool isConnector(SPItem *i) {
+using namespace std;
+using namespace cola;
+using namespace vpsc;
+/**
+ * Returns true if item is a connector
+ */
+bool isConnector(SPItem const *const i) {
SPPath *path = NULL;
if(SP_IS_PATH(i)) {
path = SP_PATH(i);
}
return path && path->connEndPair.isAutoRoutingConn();
}
-#endif // HAVE_BOOST_GRAPH_LIB
+
+struct CheckProgress : TestConvergence {
+ CheckProgress(double d,unsigned i,list<SPItem *>&
+ selected,vector<Rectangle*>& rs,map<string,unsigned>& nodelookup) :
+ TestConvergence(d,i), selected(selected), rs(rs), nodelookup(nodelookup) {}
+ bool operator()(double new_stress, double* X, double* Y) {
+ /* This is where, if we wanted to animate the layout, we would need to update
+ * the positions of all objects and redraw the canvas and maybe sleep a bit
+ cout << "stress="<<new_stress<<endl;
+ cout << "x[0]="<<rs[0]->getMinX()<<endl;
+ for (list<SPItem *>::iterator it(selected.begin());
+ it != selected.end();
+ ++it)
+ {
+ SPItem *u=*it;
+ if(!isConnector(u)) {
+ Rectangle* r=rs[nodelookup[u->id]];
+ NR::Rect const item_box(sp_item_bbox_desktop(u));
+ NR::Point const curr(item_box.midpoint());
+ NR::Point const dest(r->getCentreX(),r->getCentreY());
+ sp_item_move_rel(u, NR::translate(dest - curr));
+ }
+ }
+ */
+ return TestConvergence::operator()(new_stress,X,Y);
+ }
+ list<SPItem *>& selected;
+ vector<Rectangle*>& rs;
+ map<string,unsigned>& nodelookup;
+};
+
+/**
+ * Scans the items list and places those items that are
+ * not connectors in filtered
+ */
+void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
+ for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
+ SPItem *item=SP_ITEM(i->data);
+ if(!isConnector(item)) {
+ filtered.push_back(item);
+ }
+ }
+}
/**
* Takes a list of inkscape items, extracts the graph defined by
* connectors between them, and uses graph layout techniques to find
if(!items) {
return;
}
-#ifdef HAVE_BOOST_GRAPH_LIB
-
using Inkscape::Util::GSListConstIterator;
- std::list<SPItem *> selected;
- selected.insert<GSListConstIterator<SPItem *> >(selected.end(), items, NULL);
+ list<SPItem *> selected;
+ filterConnectors(items,selected);
if (selected.empty()) return;
- Graph g;
+ const unsigned n=selected.size();
+ //Check 2 or more selected objects
+ if (n < 2) return;
- double minX=DBL_MAX, minY=DBL_MAX, maxX=-DBL_MAX, maxY=-DBL_MAX;
+ // add the connector spacing to the size of node bounding boxes
+ // so that connectors can always be routed between shapes
+ SPDesktop* desktop = inkscape_active_desktop();
+ double spacing = 0;
+ if(desktop) spacing = desktop->namedview->connector_spacing+0.1;
- std::map<std::string,Vertex> nodelookup;
- std::vector<std::string> labels;
- for (std::list<SPItem *>::iterator it(selected.begin());
- it != selected.end();
- ++it)
+ map<string,unsigned> nodelookup;
+ vector<Rectangle*> rs;
+ vector<Edge> es;
+ for (list<SPItem *>::iterator i(selected.begin());
+ i != selected.end();
+ ++i)
{
- SPItem *u=*it;
- if(!isConnector(u)) {
- std::cout<<"Creating node for id: "<<u->id<<std::endl;
- nodelookup[u->id]=add_vertex(g);
- labels.push_back(u->id);
- }
+ SPItem *u=*i;
+ boost::optional<NR::Rect> const item_box(sp_item_bbox_desktop(u));
+ if(item_box) {
+ NR::Point ll(item_box->min());
+ NR::Point ur(item_box->max());
+ nodelookup[u->id]=rs.size();
+ rs.push_back(new Rectangle(ll[0]-spacing,ur[0]+spacing,
+ ll[1]-spacing,ur[1]+spacing));
+ } else {
+ // I'm not actually sure if it's possible for something with a
+ // NULL item-box to be attached to a connector in which case we
+ // should never get to here... but if such a null box can occur it's
+ // probably pretty safe to simply ignore
+ //fprintf(stderr,"NULL item_box found in graphlayout, ignoring!\n");
+ }
}
- int n=labels.size();
- //Check 2 or more selected objects
- if (n < 2) return;
+ SimpleConstraints scx,scy;
+ double ideal_connector_length = prefs_get_double_attribute("tools.connector","length",100);
+ double directed_edge_height_modifier = 1.0;
+ gchar const *directed_str = NULL, *overlaps_str = NULL;
+ directed_str = prefs_get_string_attribute("tools.connector",
+ "directedlayout");
+ overlaps_str = prefs_get_string_attribute("tools.connector",
+ "avoidoverlaplayout");
+ bool avoid_overlaps = false;
+ bool directed = false;
+ if (directed_str && !strcmp(directed_str, "true")) {
+ directed = true;
+ }
+ if (overlaps_str && !strcmp(overlaps_str, "true")) {
+ avoid_overlaps = true;
+ }
- WeightMap weightmap=get(edge_weight, g);
- int i=0;
- for (std::list<SPItem *>::iterator it(selected.begin());
- it != selected.end();
- ++it)
+ for (list<SPItem *>::iterator i(selected.begin());
+ i != selected.end();
+ ++i)
{
- using NR::X; using NR::Y;
- SPItem *itu=*it;
- Vertex u=nodelookup[itu->id];
- GSList *nlist=itu->avoidRef->getAttachedShapes(Avoid::ConnRef::runningFrom);
- std::list<SPItem *> neighbours;
- neighbours.insert<GSListConstIterator<SPItem *> >(neighbours.end(),nlist,NULL);
- for (std::list<SPItem *>::iterator ne(neighbours.begin());
- ne != neighbours.end();
- ++ne) {
-
- SPItem *itv=*ne;
- Vertex v=nodelookup[itv->id];
- Graph::edge_descriptor e; bool inserted;
- tie(e, inserted)=add_edge(u,v,g);
- weightmap[e]=1.0;
- }
+ SPItem *iu=*i;
+ map<string,unsigned>::iterator i=nodelookup.find(iu->id);
+ if(i==nodelookup.end()) {
+ continue;
+ }
+ unsigned u=i->second;
+ GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
+ list<SPItem *> connectors;
+
+ connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
+ for (list<SPItem *>::iterator j(connectors.begin());
+ j != connectors.end();
+ ++j) {
+ SPItem *conn=*j;
+ SPItem *iv;
+ SPItem *items[2];
+ assert(isConnector(conn));
+ SP_PATH(conn)->connEndPair.getAttachedItems(items);
+ if(items[0]==iu) {
+ iv=items[1];
+ } else {
+ iv=items[0];
+ }
+
+ if (iv == NULL) {
+ // The connector is not attached to anything at the
+ // other end so we should just ignore it.
+ continue;
+ }
+
+ // If iv not in nodelookup we again treat the connector
+ // as disconnected and continue
+ map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
+ if(v_pair!=nodelookup.end()) {
+ unsigned v=v_pair->second;
+ //cout << "Edge: (" << u <<","<<v<<")"<<endl;
+ es.push_back(make_pair(u,v));
+ if(conn->style->marker[SP_MARKER_LOC_END].set) {
+ if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
+ scy.push_back(new SimpleConstraint(v, u,
+ (ideal_connector_length * directed_edge_height_modifier)));
+ }
+ }
+ }
+ }
if(nlist) {
g_slist_free(nlist);
}
- NR::Rect const item_box(sp_item_bbox_desktop(*it));
-
- NR::Point ll(item_box.min());
- minX=std::min(ll[0],minX);
- minY=std::min(ll[1],minY);
- NR::Point ur(item_box.max());
- maxX=std::max(ur[0],maxX);
- maxY=std::max(ur[1],maxY);
}
- double width=maxX-minX;
- double height=maxY-minY;
- std::cout<<"Graph has |V|="<<num_vertices(g)<<" Width="<<width<<" Height="<<height<<std::endl;
- PositionVec position_vec(num_vertices(g));
- PositionMap position(position_vec.begin(), get(vertex_index, g));
- //write_graphviz(std::cout, g, make_label_writer<std::vector<std::string>>(labels));
- circle_graph_layout(g, position, width/2.0);
- kamada_kawai_spring_layout(g, position, weightmap, side_length(width));
-
- graph_traits<Graph>::vertex_iterator vi, vi_end;
- i=0;
- for (std::list<SPItem *>::iterator it(selected.begin());
+ const unsigned E = es.size();
+ double eweights[E];
+ fill(eweights,eweights+E,1);
+ vector<Component*> cs;
+ connectedComponents(rs,es,scx,scy,cs);
+ for(unsigned i=0;i<cs.size();i++) {
+ Component* c=cs[i];
+ if(c->edges.size()<2) continue;
+ CheckProgress test(0.0001,100,selected,rs,nodelookup);
+ ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length,test);
+ alg.setupConstraints(NULL,NULL,avoid_overlaps,
+ NULL,NULL,&c->scx,&c->scy,NULL,NULL);
+ alg.run();
+ }
+ separateComponents(cs);
+
+ for (list<SPItem *>::iterator it(selected.begin());
it != selected.end();
++it)
{
SPItem *u=*it;
if(!isConnector(u)) {
- NR::Rect const item_box(sp_item_bbox_desktop(u));
- NR::Point const curr(item_box.midpoint());
- NR::Point const dest(minX+width/2.0+position[nodelookup[u->id]].x,
- minY+height/2.0+position[nodelookup[u->id]].y);
- sp_item_move_rel(u, NR::translate(dest - curr));
+ map<string,unsigned>::iterator i=nodelookup.find(u->id);
+ if(i!=nodelookup.end()) {
+ Rectangle* r=rs[i->second];
+ boost::optional<NR::Rect> item_box(sp_item_bbox_desktop(u));
+ if(item_box) {
+ NR::Point const curr(item_box->midpoint());
+ NR::Point const dest(r->getCentreX(),r->getCentreY());
+ sp_item_move_rel(u, NR::translate(dest - curr));
+ }
+ }
}
}
-#else
- std::cout<<"Connector network layout not available! Install boost graph library and recompile to enable."<<std::endl;
-#endif // HAVE_BOOST_GRAPH_LIB
+ for(unsigned i=0;i<scx.size();i++) {
+ delete scx[i];
+ }
+ for(unsigned i=0;i<scy.size();i++) {
+ delete scy[i];
+ }
+ for(unsigned i=0;i<rs.size();i++) {
+ delete rs[i];
+ }
}
+// vim: set cindent
+// vim: ts=4 sw=4 et tw=0 wm=0