Code

Avoid crash by uninitialized perspectives.
[inkscape.git] / src / graphlayout / graphlayout.cpp
1 /** @file
2  * @brief 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  */
13 #include <iostream>
14 #include <config.h>
15 #include <map>
16 #include <vector>
17 #include <algorithm>
18 #include <cstring>
19 #include <cstdlib>
20 #include <float.h>
22 #include "desktop.h"
23 #include "inkscape.h"
24 #include "sp-namedview.h"
25 #include "util/glib-list-iterators.h"
26 #include "graphlayout/graphlayout.h"
27 #include "sp-path.h"
28 #include "sp-item.h"
29 #include "sp-item-transform.h"
30 #include "sp-conn-end-pair.h"
31 #include "style.h"
32 #include "conn-avoid-ref.h"
33 #include "libavoid/connector.h"
34 #include "libavoid/router.h"
35 #include "libavoid/geomtypes.h"
36 #include "libcola/cola.h"
37 #include "libvpsc/generate-constraints.h"
38 #include "preferences.h"
40 using namespace std;
41 using namespace cola;
42 using namespace vpsc;
43 /**
44  * Returns true if item is a connector
45  */
46 bool isConnector(SPItem const *const i) {
47         SPPath *path = NULL;
48         if(SP_IS_PATH(i)) {
49                 path = SP_PATH(i);
50         }
51         return path && path->connEndPair.isAutoRoutingConn();
52 }
54 struct CheckProgress : TestConvergence {
55     CheckProgress(double d,unsigned i,list<SPItem *>&
56             selected,vector<Rectangle*>& rs,map<string,unsigned>& nodelookup) :
57         TestConvergence(d,i), selected(selected), rs(rs), nodelookup(nodelookup) {}
58         bool operator()(double new_stress, double* X, double* Y) {
59         /* This is where, if we wanted to animate the layout, we would need to update
60          * the positions of all objects and redraw the canvas and maybe sleep a bit
61                 cout << "stress="<<new_stress<<endl;
62         cout << "x[0]="<<rs[0]->getMinX()<<endl;
63         for (list<SPItem *>::iterator it(selected.begin());
64             it != selected.end();
65             ++it)
66         {
67             SPItem *u=*it;
68             if(!isConnector(u)) {
69                 Rectangle* r=rs[nodelookup[u->id]];
70                 Geom::Rect const item_box(sp_item_bbox_desktop(u));
71                 Geom::Point const curr(item_box.midpoint());
72                 Geom::Point const dest(r->getCentreX(),r->getCentreY());
73                 sp_item_move_rel(u, Geom::Translate(dest - curr));
74             }
75         }
76         */
77                 return TestConvergence::operator()(new_stress,X,Y);
78         }
79     list<SPItem *>& selected;
80     vector<Rectangle*>& rs;
81     map<string,unsigned>& nodelookup;
82 };
84 /**
85  * Scans the items list and places those items that are 
86  * not connectors in filtered
87  */
88 void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
89         for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
90                 SPItem *item=SP_ITEM(i->data);
91                 if(!isConnector(item)) {
92                         filtered.push_back(item);
93                 }
94         }
95 }
96 /**
97 * Takes a list of inkscape items, extracts the graph defined by 
98 * connectors between them, and uses graph layout techniques to find
99 * a nice layout
100 */
101 void graphlayout(GSList const *const items) {
102         if(!items) {
103                 return;
104         }
106         using Inkscape::Util::GSListConstIterator;
107         list<SPItem *> selected;
108         filterConnectors(items,selected);
109         if (selected.empty()) return;
111         const unsigned n=selected.size();
112         //Check 2 or more selected objects
113         if (n < 2) return;
115     // add the connector spacing to the size of node bounding boxes
116     // so that connectors can always be routed between shapes
117     SPDesktop* desktop = inkscape_active_desktop();
118     double spacing = 0;
119     if(desktop) spacing = desktop->namedview->connector_spacing+0.1;
121         map<string,unsigned> nodelookup;
122         vector<Rectangle*> rs;
123         vector<Edge> es;
124         for (list<SPItem *>::iterator i(selected.begin());
125                 i != selected.end();
126                 ++i)
127         {
128                 SPItem *u=*i;
129                 Geom::OptRect const item_box(sp_item_bbox_desktop(u));
130         if(item_box) {
131             Geom::Point ll(item_box->min());
132             Geom::Point ur(item_box->max());
133             nodelookup[u->id]=rs.size();
134             rs.push_back(new Rectangle(ll[0]-spacing,ur[0]+spacing,
135                         ll[1]-spacing,ur[1]+spacing));
136         } else {
137             // I'm not actually sure if it's possible for something with a
138             // NULL item-box to be attached to a connector in which case we
139             // should never get to here... but if such a null box can occur it's
140             // probably pretty safe to simply ignore
141             //fprintf(stderr,"NULL item_box found in graphlayout, ignoring!\n");
142         }
143         }
145     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
146         SimpleConstraints scx,scy;
147         double ideal_connector_length = prefs->getDouble("/tools/connector/length", 100.0);
148         double directed_edge_height_modifier = 1.0;
149         
150         bool directed =       prefs->getBool("/tools/connector/directedlayout");
151         bool avoid_overlaps = prefs->getBool("/tools/connector/avoidoverlaplayout");
153         for (list<SPItem *>::iterator i(selected.begin());
154                 i != selected.end();
155                 ++i)
156         {
157                 SPItem *iu=*i;
158         map<string,unsigned>::iterator i=nodelookup.find(iu->id);
159         if(i==nodelookup.end()) {
160             continue;
161         }
162         unsigned u=i->second;
163         GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
164         list<SPItem *> connectors;
165         
166         connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
167         for (list<SPItem *>::iterator j(connectors.begin());
168                 j != connectors.end();
169                 ++j) {
170             SPItem *conn=*j;
171             SPItem *iv;
172             SPItem *items[2];
173             assert(isConnector(conn));
174             SP_PATH(conn)->connEndPair.getAttachedItems(items);
175             if(items[0]==iu) {
176                 iv=items[1];
177             } else {
178                 iv=items[0];
179             }
180     
181             if (iv == NULL) {
182                 // The connector is not attached to anything at the 
183                 // other end so we should just ignore it.
184                 continue;
185             }
187             // If iv not in nodelookup we again treat the connector
188             // as disconnected and continue
189             map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
190             if(v_pair!=nodelookup.end()) {
191                 unsigned v=v_pair->second;
192                 //cout << "Edge: (" << u <<","<<v<<")"<<endl;
193                 es.push_back(make_pair(u,v));
194                 if(conn->style->marker[SP_MARKER_LOC_END].set) {
195                     if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
196                         scy.push_back(new SimpleConstraint(v, u, 
197                                     (ideal_connector_length * directed_edge_height_modifier)));
198                     }
199                 }
200             }
201         }
202                 if(nlist) {
203                         g_slist_free(nlist);
204                 }
205         }
206         const unsigned E = es.size();
207         double eweights[E];
208         fill(eweights,eweights+E,1);
209     vector<Component*> cs;
210     connectedComponents(rs,es,scx,scy,cs);
211     for(unsigned i=0;i<cs.size();i++) {
212         Component* c=cs[i];
213         if(c->edges.size()<2) continue;
214         CheckProgress test(0.0001,100,selected,rs,nodelookup);
215         ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length,test);
216         alg.setupConstraints(NULL,NULL,avoid_overlaps,
217                 NULL,NULL,&c->scx,&c->scy,NULL,NULL);
218         alg.run();
219     }
220     separateComponents(cs);
221         
222         for (list<SPItem *>::iterator it(selected.begin());
223                 it != selected.end();
224                 ++it)
225         {
226                 SPItem *u=*it;
227                 if(!isConnector(u)) {
228                         map<string,unsigned>::iterator i=nodelookup.find(u->id);
229                         if(i!=nodelookup.end()) {
230                 Rectangle* r=rs[i->second];
231                 Geom::OptRect item_box(sp_item_bbox_desktop(u));
232                 if(item_box) {
233                     Geom::Point const curr(item_box->midpoint());
234                     Geom::Point const dest(r->getCentreX(),r->getCentreY());
235                     sp_item_move_rel(u, Geom::Translate(dest - curr));
236                 }
237             }
238                 }
239         }
240     for(unsigned i=0;i<scx.size();i++) {
241         delete scx[i];
242     }
243     for(unsigned i=0;i<scy.size();i++) {
244         delete scy[i];
245     }
246     for(unsigned i=0;i<rs.size();i++) {
247         delete rs[i];
248     }
250 // vim: set cindent 
251 // vim: ts=4 sw=4 et tw=0 wm=0
253 /*
254   Local Variables:
255   mode:c++
256   c-file-style:"stroustrup"
257   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
258   indent-tabs-mode:nil
259   fill-column:99
260   End:
261 */
262 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :