Code

Merge further bbox work
[inkscape.git] / src / graphlayout / graphlayout.cpp
1 /** \file
2  * Interface between Inkscape code (SPItem) and graphlayout functions.
3  */
4 /*
5 * Authors:
6 *   Tim Dwyer <Tim.Dwyer@infotech.monash.edu.au>
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 "desktop.h"
20 #include "inkscape.h"
21 #include "sp-namedview.h"
22 #include "util/glib-list-iterators.h"
23 #include "graphlayout/graphlayout.h"
24 #include "sp-path.h"
25 #include "sp-item.h"
26 #include "sp-item-transform.h"
27 #include "sp-conn-end-pair.h"
28 #include "style.h"
29 #include "conn-avoid-ref.h"
30 #include "libavoid/connector.h"
31 #include "libavoid/geomtypes.h"
32 #include "libcola/cola.h"
33 #include "libvpsc/generate-constraints.h"
34 #include "prefs-utils.h"
36 using namespace std;
37 using namespace cola;
38 using namespace vpsc;
40 /**
41  * Returns true if item is a connector
42  */
43 bool isConnector(SPItem const *const i) {
44         SPPath *path = NULL;
45         if(SP_IS_PATH(i)) {
46                 path = SP_PATH(i);
47         }
48         return path && path->connEndPair.isAutoRoutingConn();
49 }
51 /**
52  * Scans the items list and places those items that are 
53  * not connectors in filtered
54  */
55 void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
56         for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
57                 SPItem *item=SP_ITEM(i->data);
58                 if(!isConnector(item)) {
59                         filtered.push_back(item);
60                 }
61         }
62 }
63 /**
64 * Takes a list of inkscape items, extracts the graph defined by 
65 * connectors between them, and uses graph layout techniques to find
66 * a nice layout
67 */
68 void graphlayout(GSList const *const items) {
69         if(!items) {
70                 return;
71         }
73         using Inkscape::Util::GSListConstIterator;
74         list<SPItem *> selected;
75         filterConnectors(items,selected);
76         if (selected.empty()) return;
78         const unsigned n=selected.size();
79         //Check 2 or more selected objects
80         if (n < 2) return;
82     // add the connector spacing to the size of node bounding boxes
83     // so that connectors can always be routed between shapes
84     SPDesktop* desktop = inkscape_active_desktop();
85     double spacing = 0;
86     if(desktop) spacing = desktop->namedview->connector_spacing+0.1;
88         map<string,unsigned> nodelookup;
89         vector<Rectangle*> rs;
90         vector<Edge> es;
91         for (list<SPItem *>::iterator i(selected.begin());
92                 i != selected.end();
93                 ++i)
94         {
95                 SPItem *u=*i;
96                 NR::Maybe<NR::Rect> const item_box(sp_item_bbox_desktop(u));
97         g_assert(item_box);
98                 NR::Point ll(item_box->min());
99                 NR::Point ur(item_box->max());
100                 nodelookup[u->id]=rs.size();
101                 rs.push_back(new Rectangle(ll[0]-spacing,ur[0]+spacing,
102                     ll[1]-spacing,ur[1]+spacing));
103         }
105         SimpleConstraints scx,scy;
106         double ideal_connector_length = prefs_get_double_attribute("tools.connector","length",100);
107         double directed_edge_height_modifier = 1.0;
108         gchar const *directed_str = NULL, *overlaps_str = NULL;
109         directed_str = prefs_get_string_attribute("tools.connector",
110                         "directedlayout");
111         overlaps_str = prefs_get_string_attribute("tools.connector",
112                         "avoidoverlaplayout");
113         bool avoid_overlaps = false;
114         bool directed = false;
115     if (directed_str && !strcmp(directed_str, "true")) {
116             directed = true;
117         }
118     if (overlaps_str && !strcmp(overlaps_str, "true")) {
119             avoid_overlaps = true;
120         }
122         for (list<SPItem *>::iterator i(selected.begin());
123                 i != selected.end();
124                 ++i)
125         {
126                 SPItem *iu=*i;
127                 unsigned u=nodelookup[iu->id];
128                 GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
129                 list<SPItem *> connectors;
130                 
131                 connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
132                 for (list<SPItem *>::iterator j(connectors.begin());
133                                 j != connectors.end();
134                                 ++j) {
135                         SPItem *conn=*j;
136                         SPItem *iv;
137                         SPItem *items[2];
138                         assert(isConnector(conn));
139                         SP_PATH(conn)->connEndPair.getAttachedItems(items);
140                         if(items[0]==iu) {
141                                 iv=items[1];
142                         } else {
143                                 iv=items[0];
144                         }
145         
146             if (iv == NULL) {
147                 // The connector is not attached to anything at the 
148                 // other end so we should just ignore it.
149                 continue;
150             }
152                         // What do we do if iv not in nodelookup?!?!
153                         map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
154                         if(v_pair!=nodelookup.end()) {
155                                 unsigned v=v_pair->second;
156                                 //cout << "Edge: (" << u <<","<<v<<")"<<endl;
157                                 es.push_back(make_pair(u,v));
158                                 if(conn->style->marker[SP_MARKER_LOC_END].set) {
159                                         if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
160                                                 scy.push_back(new SimpleConstraint(v, u, 
161                                     (ideal_connector_length * directed_edge_height_modifier)));
162                                         }
163                                 }
164                         }
165                 }
166                 if(nlist) {
167                         g_slist_free(nlist);
168                 }
169         }
170         const unsigned E = es.size();
171         double eweights[E];
172         fill(eweights,eweights+E,1);
173     vector<Component*> cs;
174     connectedComponents(rs,es,scx,scy,cs);
175     for(unsigned i=0;i<cs.size();i++) {
176         Component* c=cs[i];
177         if(c->edges.size()<2) continue;
178         ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length);
179         alg.setupConstraints(NULL,NULL,avoid_overlaps,
180                 NULL,NULL,&c->scx,&c->scy,NULL,NULL);
181         alg.run();
182     }
183     separateComponents(cs);
184         
185         for (list<SPItem *>::iterator it(selected.begin());
186                 it != selected.end();
187                 ++it)
188         {
189                 SPItem *u=*it;
190                 if(!isConnector(u)) {
191                         Rectangle* r=rs[nodelookup[u->id]];
192                         NR::Maybe<NR::Rect> item_box(sp_item_bbox_desktop(u));
193             g_assert(item_box);
194                         NR::Point const curr(item_box->midpoint());
195                         NR::Point const dest(r->getCentreX(),r->getCentreY());
196                         sp_item_move_rel(u, NR::translate(dest - curr));
197                 }
198         }
199     for(unsigned i=0;i<scx.size();i++) {
200         delete scx[i];
201     }
202     for(unsigned i=0;i<scy.size();i++) {
203         delete scy[i];
204     }
205     for(unsigned i=0;i<rs.size();i++) {
206         delete rs[i];
207     }
209 // vim: set cindent 
210 // vim: ts=4 sw=4 et tw=0 wm=0