Code

fix compositing for premultiplication and non-alpha cases
[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;
39 /**
40  * Returns true if item is a connector
41  */
42 bool isConnector(SPItem const *const i) {
43         SPPath *path = NULL;
44         if(SP_IS_PATH(i)) {
45                 path = SP_PATH(i);
46         }
47         return path && path->connEndPair.isAutoRoutingConn();
48 }
50 struct CheckProgress : TestConvergence {
51     CheckProgress(double d,unsigned i,list<SPItem *>&
52             selected,vector<Rectangle*>& rs,map<string,unsigned>& nodelookup) :
53         TestConvergence(d,i), selected(selected), rs(rs), nodelookup(nodelookup) {}
54         bool operator()(double new_stress, double* X, double* Y) {
55         /* This is where, if we wanted to animate the layout, we would need to update
56          * the positions of all objects and redraw the canvas and maybe sleep a bit
57                 cout << "stress="<<new_stress<<endl;
58         cout << "x[0]="<<rs[0]->getMinX()<<endl;
59         for (list<SPItem *>::iterator it(selected.begin());
60             it != selected.end();
61             ++it)
62         {
63             SPItem *u=*it;
64             if(!isConnector(u)) {
65                 Rectangle* r=rs[nodelookup[u->id]];
66                 NR::Rect const item_box(sp_item_bbox_desktop(u));
67                 NR::Point const curr(item_box.midpoint());
68                 NR::Point const dest(r->getCentreX(),r->getCentreY());
69                 sp_item_move_rel(u, NR::translate(dest - curr));
70             }
71         }
72         */
73                 return TestConvergence::operator()(new_stress,X,Y);
74         }
75     list<SPItem *>& selected;
76     vector<Rectangle*>& rs;
77     map<string,unsigned>& nodelookup;
78 };
80 /**
81  * Scans the items list and places those items that are 
82  * not connectors in filtered
83  */
84 void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
85         for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
86                 SPItem *item=SP_ITEM(i->data);
87                 if(!isConnector(item)) {
88                         filtered.push_back(item);
89                 }
90         }
91 }
92 /**
93 * Takes a list of inkscape items, extracts the graph defined by 
94 * connectors between them, and uses graph layout techniques to find
95 * a nice layout
96 */
97 void graphlayout(GSList const *const items) {
98         if(!items) {
99                 return;
100         }
102         using Inkscape::Util::GSListConstIterator;
103         list<SPItem *> selected;
104         filterConnectors(items,selected);
105         if (selected.empty()) return;
107         const unsigned n=selected.size();
108         //Check 2 or more selected objects
109         if (n < 2) return;
111     // add the connector spacing to the size of node bounding boxes
112     // so that connectors can always be routed between shapes
113     SPDesktop* desktop = inkscape_active_desktop();
114     double spacing = 0;
115     if(desktop) spacing = desktop->namedview->connector_spacing+0.1;
117         map<string,unsigned> nodelookup;
118         vector<Rectangle*> rs;
119         vector<Edge> es;
120         for (list<SPItem *>::iterator i(selected.begin());
121                 i != selected.end();
122                 ++i)
123         {
124                 SPItem *u=*i;
125                 NR::Maybe<NR::Rect> const item_box(sp_item_bbox_desktop(u));
126         if(item_box) {
127             NR::Point ll(item_box->min());
128             NR::Point ur(item_box->max());
129             nodelookup[u->id]=rs.size();
130             rs.push_back(new Rectangle(ll[0]-spacing,ur[0]+spacing,
131                         ll[1]-spacing,ur[1]+spacing));
132         } else {
133             // I'm not actually sure if it's possible for something with a
134             // NULL item-box to be attached to a connector in which case we
135             // should never get to here... but if such a null box can occur it's
136             // probably pretty safe to simply ignore
137             //fprintf(stderr,"NULL item_box found in graphlayout, ignoring!\n");
138         }
139         }
141         SimpleConstraints scx,scy;
142         double ideal_connector_length = prefs_get_double_attribute("tools.connector","length",100);
143         double directed_edge_height_modifier = 1.0;
144         gchar const *directed_str = NULL, *overlaps_str = NULL;
145         directed_str = prefs_get_string_attribute("tools.connector",
146                         "directedlayout");
147         overlaps_str = prefs_get_string_attribute("tools.connector",
148                         "avoidoverlaplayout");
149         bool avoid_overlaps = false;
150         bool directed = false;
151     if (directed_str && !strcmp(directed_str, "true")) {
152             directed = true;
153         }
154     if (overlaps_str && !strcmp(overlaps_str, "true")) {
155             avoid_overlaps = true;
156         }
158         for (list<SPItem *>::iterator i(selected.begin());
159                 i != selected.end();
160                 ++i)
161         {
162                 SPItem *iu=*i;
163         map<string,unsigned>::iterator i=nodelookup.find(iu->id);
164         if(i==nodelookup.end()) {
165             continue;
166         }
167         unsigned u=i->second;
168         GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
169         list<SPItem *> connectors;
170         
171         connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
172         for (list<SPItem *>::iterator j(connectors.begin());
173                 j != connectors.end();
174                 ++j) {
175             SPItem *conn=*j;
176             SPItem *iv;
177             SPItem *items[2];
178             assert(isConnector(conn));
179             SP_PATH(conn)->connEndPair.getAttachedItems(items);
180             if(items[0]==iu) {
181                 iv=items[1];
182             } else {
183                 iv=items[0];
184             }
185     
186             if (iv == NULL) {
187                 // The connector is not attached to anything at the 
188                 // other end so we should just ignore it.
189                 continue;
190             }
192             // If iv not in nodelookup we again treat the connector
193             // as disconnected and continue
194             map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
195             if(v_pair!=nodelookup.end()) {
196                 unsigned v=v_pair->second;
197                 //cout << "Edge: (" << u <<","<<v<<")"<<endl;
198                 es.push_back(make_pair(u,v));
199                 if(conn->style->marker[SP_MARKER_LOC_END].set) {
200                     if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
201                         scy.push_back(new SimpleConstraint(v, u, 
202                                     (ideal_connector_length * directed_edge_height_modifier)));
203                     }
204                 }
205             }
206         }
207                 if(nlist) {
208                         g_slist_free(nlist);
209                 }
210         }
211         const unsigned E = es.size();
212         double eweights[E];
213         fill(eweights,eweights+E,1);
214     vector<Component*> cs;
215     connectedComponents(rs,es,scx,scy,cs);
216     for(unsigned i=0;i<cs.size();i++) {
217         Component* c=cs[i];
218         if(c->edges.size()<2) continue;
219         CheckProgress test(0.0001,100,selected,rs,nodelookup);
220         ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length,test);
221         alg.setupConstraints(NULL,NULL,avoid_overlaps,
222                 NULL,NULL,&c->scx,&c->scy,NULL,NULL);
223         alg.run();
224     }
225     separateComponents(cs);
226         
227         for (list<SPItem *>::iterator it(selected.begin());
228                 it != selected.end();
229                 ++it)
230         {
231                 SPItem *u=*it;
232                 if(!isConnector(u)) {
233                         map<string,unsigned>::iterator i=nodelookup.find(u->id);
234                         if(i!=nodelookup.end()) {
235                 Rectangle* r=rs[i->second];
236                 NR::Maybe<NR::Rect> item_box(sp_item_bbox_desktop(u));
237                 if(item_box) {
238                     NR::Point const curr(item_box->midpoint());
239                     NR::Point const dest(r->getCentreX(),r->getCentreY());
240                     sp_item_move_rel(u, NR::translate(dest - curr));
241                 }
242             }
243                 }
244         }
245     for(unsigned i=0;i<scx.size();i++) {
246         delete scx[i];
247     }
248     for(unsigned i=0;i<scy.size();i++) {
249         delete scy[i];
250     }
251     for(unsigned i=0;i<rs.size();i++) {
252         delete rs[i];
253     }
255 // vim: set cindent 
256 // vim: ts=4 sw=4 et tw=0 wm=0