Code

Layout algorithm is now applied to each connected component in the
[inkscape.git] / src / graphlayout / graphlayout.cpp
1 /** \file
2  * Interface between Inkscape code (SPItem) and graphlayout functions.
3  */
4 /*
5 * Authors:
6 *   Tim Dwyer <tgdwyer@gmail.com>
7 *
8 * Copyright (C) 2005 Authors
9 *
10 * Released under GNU GPL.  Read the file 'COPYING' for more information.
11 */
12 #include <iostream>
13 #include <config.h>
14 #include <map>
15 #include <vector>
16 #include <algorithm>
17 #include <float.h>
19 #include "util/glib-list-iterators.h"
20 #include "graphlayout/graphlayout.h"
21 #include "sp-path.h"
22 #include "sp-item.h"
23 #include "sp-item-transform.h"
24 #include "sp-conn-end-pair.h"
25 #include "style.h"
26 #include "conn-avoid-ref.h"
27 #include "libavoid/connector.h"
28 #include "libavoid/geomtypes.h"
29 #include "libcola/cola.h"
30 #include "libvpsc/generate-constraints.h"
31 #include "prefs-utils.h"
33 using namespace std;
34 using namespace cola;
35 using namespace vpsc;
37 /**
38  * Returns true if item is a connector
39  */
40 bool isConnector(SPItem const *const i) {
41         SPPath *path = NULL;
42         if(SP_IS_PATH(i)) {
43                 path = SP_PATH(i);
44         }
45         return path && path->connEndPair.isAutoRoutingConn();
46 }
48 /**
49  * Scans the items list and places those items that are 
50  * not connectors in filtered
51  */
52 void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
53         for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
54                 SPItem *item=SP_ITEM(i->data);
55                 if(!isConnector(item)) {
56                         filtered.push_back(item);
57                 }
58         }
59 }
60 /**
61 * Takes a list of inkscape items, extracts the graph defined by 
62 * connectors between them, and uses graph layout techniques to find
63 * a nice layout
64 */
65 void graphlayout(GSList const *const items) {
66         if(!items) {
67                 return;
68         }
70         using Inkscape::Util::GSListConstIterator;
71         list<SPItem *> selected;
72         filterConnectors(items,selected);
73         if (selected.empty()) return;
75         const unsigned n=selected.size();
76         //Check 2 or more selected objects
77         if (n < 2) return;
79         map<string,unsigned> nodelookup;
80         vector<Rectangle*> rs;
81         vector<Edge> es;
82         for (list<SPItem *>::iterator i(selected.begin());
83                 i != selected.end();
84                 ++i)
85         {
86                 SPItem *u=*i;
87                 NR::Rect const item_box(sp_item_bbox_desktop(u));
88                 NR::Point ll(item_box.min());
89                 NR::Point ur(item_box.max());
90                 nodelookup[u->id]=rs.size();
91                 rs.push_back(new Rectangle(ll[0],ur[0],ll[1],ur[1]));
92         }
94         SimpleConstraints scy;
95         double ideal_connector_length = prefs_get_double_attribute("tools.connector","length",100);
96         double directed_edge_height_modifier = 1.0;
97         gchar const *directed_str = NULL, *overlaps_str = NULL;
98         directed_str = prefs_get_string_attribute("tools.connector",
99                         "directedlayout");
100         overlaps_str = prefs_get_string_attribute("tools.connector",
101                         "avoidoverlaplayout");
102         bool avoid_overlaps = false;
103         bool directed = false;
104     if (directed_str && !strcmp(directed_str, "true")) {
105             directed = true;
106         }
107     if (overlaps_str && !strcmp(overlaps_str, "true")) {
108             avoid_overlaps = true;
109         }
111         for (list<SPItem *>::iterator i(selected.begin());
112                 i != selected.end();
113                 ++i)
114         {
115                 SPItem *iu=*i;
116                 unsigned u=nodelookup[iu->id];
117                 GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
118                 list<SPItem *> connectors;
119                 
120                 connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
121                 for (list<SPItem *>::iterator j(connectors.begin());
122                                 j != connectors.end();
123                                 ++j) {
124                         SPItem *conn=*j;
125                         SPItem *iv;
126                         SPItem *items[2];
127                         assert(isConnector(conn));
128                         SP_PATH(conn)->connEndPair.getAttachedItems(items);
129                         if(items[0]==iu) {
130                                 iv=items[1];
131                         } else {
132                                 iv=items[0];
133                         }
134                 
135                         // What do we do if iv not in nodelookup?!?!
136                         map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
137                         if(v_pair!=nodelookup.end()) {
138                                 unsigned v=v_pair->second;
139                                 cout << "Edge: (" << u <<","<<v<<")"<<endl;
140                                 es.push_back(make_pair(u,v));
141                                 if(conn->style->marker[SP_MARKER_LOC_END].set) {
142                                         if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
143                                                 scy.push_back(new SimpleConstraint(v, u, 
144                                     (ideal_connector_length * directed_edge_height_modifier)));
145                                         }
146                                 }
147                         }
148                 }
149                 if(nlist) {
150                         g_slist_free(nlist);
151                 }
152         }
153         const unsigned E = es.size();
154         double eweights[E];
155         fill(eweights,eweights+E,1);
156     vector<Component*> cs;
157     connectedComponents(rs,es,cs);
158     for(unsigned i=0;i<cs.size();i++) {
159         Component* c=cs[i];
160                 printf("Component %d:\n",i);
161                 for(unsigned j=0;j<c->edges.size();j++) {
162                         Edge& e=c->edges[j];
163                         printf("(%d,%d) ",e.first,e.second);
164                 }
165         if(c->edges.size()<2) continue;
166                 cout << endl;
167         ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length);
168         alg.setupConstraints(NULL,NULL,avoid_overlaps,
169                 NULL,NULL,NULL,&scy,NULL,NULL);
170         alg.run();
171     }
172         
173         for (list<SPItem *>::iterator it(selected.begin());
174                 it != selected.end();
175                 ++it)
176         {
177                 SPItem *u=*it;
178                 if(!isConnector(u)) {
179                         Rectangle* r=rs[nodelookup[u->id]];
180                         NR::Rect const item_box(sp_item_bbox_desktop(u));
181                         NR::Point const curr(item_box.midpoint());
182                         NR::Point const dest(r->getCentreX(),r->getCentreY());
183                         sp_item_move_rel(u, NR::translate(dest - curr));
184                 }
185         }
187 // vim: set cindent 
188 // vim: ts=4 sw=4 et tw=0 wm=0