Code

Added check for null bounding box of shapes involved in graph layout.
[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         if(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         } else {
104             // I'm not actually sure if it's possible for something with a
105             // NULL item-box to be attached to a connector in which case we
106             // should never get to here... but if such a null box can occur it's
107             // probably pretty safe to simply ignore
108             //fprintf(stderr,"NULL item_box found in graphlayout, ignoring!\n");
109         }
110         }
112         SimpleConstraints scx,scy;
113         double ideal_connector_length = prefs_get_double_attribute("tools.connector","length",100);
114         double directed_edge_height_modifier = 1.0;
115         gchar const *directed_str = NULL, *overlaps_str = NULL;
116         directed_str = prefs_get_string_attribute("tools.connector",
117                         "directedlayout");
118         overlaps_str = prefs_get_string_attribute("tools.connector",
119                         "avoidoverlaplayout");
120         bool avoid_overlaps = false;
121         bool directed = false;
122     if (directed_str && !strcmp(directed_str, "true")) {
123             directed = true;
124         }
125     if (overlaps_str && !strcmp(overlaps_str, "true")) {
126             avoid_overlaps = true;
127         }
129         for (list<SPItem *>::iterator i(selected.begin());
130                 i != selected.end();
131                 ++i)
132         {
133                 SPItem *iu=*i;
134                 unsigned u=nodelookup[iu->id];
135                 GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
136                 list<SPItem *> connectors;
137                 
138                 connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
139                 for (list<SPItem *>::iterator j(connectors.begin());
140                                 j != connectors.end();
141                                 ++j) {
142                         SPItem *conn=*j;
143                         SPItem *iv;
144                         SPItem *items[2];
145                         assert(isConnector(conn));
146                         SP_PATH(conn)->connEndPair.getAttachedItems(items);
147                         if(items[0]==iu) {
148                                 iv=items[1];
149                         } else {
150                                 iv=items[0];
151                         }
152         
153             if (iv == NULL) {
154                 // The connector is not attached to anything at the 
155                 // other end so we should just ignore it.
156                 continue;
157             }
159                         // What do we do if iv not in nodelookup?!?!
160                         map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
161                         if(v_pair!=nodelookup.end()) {
162                                 unsigned v=v_pair->second;
163                                 //cout << "Edge: (" << u <<","<<v<<")"<<endl;
164                                 es.push_back(make_pair(u,v));
165                                 if(conn->style->marker[SP_MARKER_LOC_END].set) {
166                                         if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
167                                                 scy.push_back(new SimpleConstraint(v, u, 
168                                     (ideal_connector_length * directed_edge_height_modifier)));
169                                         }
170                                 }
171                         }
172                 }
173                 if(nlist) {
174                         g_slist_free(nlist);
175                 }
176         }
177         const unsigned E = es.size();
178         double eweights[E];
179         fill(eweights,eweights+E,1);
180     vector<Component*> cs;
181     connectedComponents(rs,es,scx,scy,cs);
182     for(unsigned i=0;i<cs.size();i++) {
183         Component* c=cs[i];
184         if(c->edges.size()<2) continue;
185         ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length);
186         alg.setupConstraints(NULL,NULL,avoid_overlaps,
187                 NULL,NULL,&c->scx,&c->scy,NULL,NULL);
188         alg.run();
189     }
190     separateComponents(cs);
191         
192         for (list<SPItem *>::iterator it(selected.begin());
193                 it != selected.end();
194                 ++it)
195         {
196                 SPItem *u=*it;
197                 if(!isConnector(u)) {
198                         Rectangle* r=rs[nodelookup[u->id]];
199                         NR::Maybe<NR::Rect> item_box(sp_item_bbox_desktop(u));
200             if(item_box) {
201                 NR::Point const curr(item_box->midpoint());
202                 NR::Point const dest(r->getCentreX(),r->getCentreY());
203                 sp_item_move_rel(u, NR::translate(dest - curr));
204             }
205                 }
206         }
207     for(unsigned i=0;i<scx.size();i++) {
208         delete scx[i];
209     }
210     for(unsigned i=0;i<scy.size();i++) {
211         delete scy[i];
212     }
213     for(unsigned i=0;i<rs.size();i++) {
214         delete rs[i];
215     }
217 // vim: set cindent 
218 // vim: ts=4 sw=4 et tw=0 wm=0