Code

remove remaining use of assume in nr-rect.cpp
[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::Rect const item_box(sp_item_bbox_desktop(u));
97                 NR::Point ll(item_box.min());
98                 NR::Point ur(item_box.max());
99                 nodelookup[u->id]=rs.size();
100                 rs.push_back(new Rectangle(ll[0]-spacing,ur[0]+spacing,
101                     ll[1]-spacing,ur[1]+spacing));
102         }
104         SimpleConstraints scx,scy;
105         double ideal_connector_length = prefs_get_double_attribute("tools.connector","length",100);
106         double directed_edge_height_modifier = 1.0;
107         gchar const *directed_str = NULL, *overlaps_str = NULL;
108         directed_str = prefs_get_string_attribute("tools.connector",
109                         "directedlayout");
110         overlaps_str = prefs_get_string_attribute("tools.connector",
111                         "avoidoverlaplayout");
112         bool avoid_overlaps = false;
113         bool directed = false;
114     if (directed_str && !strcmp(directed_str, "true")) {
115             directed = true;
116         }
117     if (overlaps_str && !strcmp(overlaps_str, "true")) {
118             avoid_overlaps = true;
119         }
121         for (list<SPItem *>::iterator i(selected.begin());
122                 i != selected.end();
123                 ++i)
124         {
125                 SPItem *iu=*i;
126                 unsigned u=nodelookup[iu->id];
127                 GSList *nlist=iu->avoidRef->getAttachedConnectors(Avoid::runningFrom);
128                 list<SPItem *> connectors;
129                 
130                 connectors.insert<GSListConstIterator<SPItem *> >(connectors.end(),nlist,NULL);
131                 for (list<SPItem *>::iterator j(connectors.begin());
132                                 j != connectors.end();
133                                 ++j) {
134                         SPItem *conn=*j;
135                         SPItem *iv;
136                         SPItem *items[2];
137                         assert(isConnector(conn));
138                         SP_PATH(conn)->connEndPair.getAttachedItems(items);
139                         if(items[0]==iu) {
140                                 iv=items[1];
141                         } else {
142                                 iv=items[0];
143                         }
144         
145             if (iv == NULL) {
146                 // The connector is not attached to anything at the 
147                 // other end so we should just ignore it.
148                 continue;
149             }
151                         // What do we do if iv not in nodelookup?!?!
152                         map<string,unsigned>::iterator v_pair=nodelookup.find(iv->id);
153                         if(v_pair!=nodelookup.end()) {
154                                 unsigned v=v_pair->second;
155                                 //cout << "Edge: (" << u <<","<<v<<")"<<endl;
156                                 es.push_back(make_pair(u,v));
157                                 if(conn->style->marker[SP_MARKER_LOC_END].set) {
158                                         if(directed && strcmp(conn->style->marker[SP_MARKER_LOC_END].value,"none")) {
159                                                 scy.push_back(new SimpleConstraint(v, u, 
160                                     (ideal_connector_length * directed_edge_height_modifier)));
161                                         }
162                                 }
163                         }
164                 }
165                 if(nlist) {
166                         g_slist_free(nlist);
167                 }
168         }
169         const unsigned E = es.size();
170         double eweights[E];
171         fill(eweights,eweights+E,1);
172     vector<Component*> cs;
173     connectedComponents(rs,es,scx,scy,cs);
174     for(unsigned i=0;i<cs.size();i++) {
175         Component* c=cs[i];
176         if(c->edges.size()<2) continue;
177         ConstrainedMajorizationLayout alg(c->rects,c->edges,eweights,ideal_connector_length);
178         alg.setupConstraints(NULL,NULL,avoid_overlaps,
179                 NULL,NULL,&c->scx,&c->scy,NULL,NULL);
180         alg.run();
181     }
182     separateComponents(cs);
183         
184         for (list<SPItem *>::iterator it(selected.begin());
185                 it != selected.end();
186                 ++it)
187         {
188                 SPItem *u=*it;
189                 if(!isConnector(u)) {
190                         Rectangle* r=rs[nodelookup[u->id]];
191                         NR::Rect const item_box(sp_item_bbox_desktop(u));
192                         NR::Point const curr(item_box.midpoint());
193                         NR::Point const dest(r->getCentreX(),r->getCentreY());
194                         sp_item_move_rel(u, NR::translate(dest - curr));
195                 }
196         }
197     for(unsigned i=0;i<scx.size();i++) {
198         delete scx[i];
199     }
200     for(unsigned i=0;i<scy.size();i++) {
201         delete scy[i];
202     }
203     for(unsigned i=0;i<rs.size();i++) {
204         delete rs[i];
205     }
207 // vim: set cindent 
208 // vim: ts=4 sw=4 et tw=0 wm=0