summaryrefslogtreecommitdiffstats
path: root/src/graphlayout/graphlayout.cpp
blob: ac2d5429f4ae1518e50d73706d01567b37f12a88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/** \file
 * Interface between Inkscape code (SPItem) and graphlayout functions.
 */
/*
* Authors:
*   Tim Dwyer <tgdwyer@gmail.com>
*
* Copyright (C) 2005 Authors
*
* Released under GNU GPL.  Read the file 'COPYING' for more information.
*/
#include <iostream>
#include <config.h>
#include <map>
#include <vector>
#include <algorithm>
#include <float.h>

#include "util/glib-list-iterators.h"
#include "graphlayout/graphlayout.h"
#include "sp-path.h"
#include "sp-item.h"
#include "sp-item-transform.h"
#include "sp-conn-end-pair.h"
#include "conn-avoid-ref.h"
#include "libavoid/connector.h"
#include "libavoid/geomtypes.h"
#include "libcola/cola.h"
#include "libvpsc/generate-constraints.h"
#include "prefs-utils.h"

using namespace std;
using namespace cola;

/**
 * Returns true if item is a connector
 */
bool isConnector(SPItem const *const i) {
	SPPath *path = NULL;
	if(SP_IS_PATH(i)) {
		path = SP_PATH(i);
	}
	return path && path->connEndPair.isAutoRoutingConn();
}

/**
 * Scans the items list and places those items that are 
 * not connectors in filtered
 */
void filterConnectors(GSList const *const items, list<SPItem *> &filtered) {
	for(GSList *i=(GSList *)items; i!=NULL; i=i->next) {
		SPItem *item=SP_ITEM(i->data);
		if(!isConnector(item)) {
			filtered.push_back(item);
		}
	}
}
/**
* Takes a list of inkscape items, extracts the graph defined by 
* connectors between them, and uses graph layout techniques to find
* a nice layout
*/
void graphlayout(GSList const *const items) {
	if(!items) {
		return;
	}

	using Inkscape::Util::GSListConstIterator;
	list<SPItem *> selected;
	filterConnectors(items,selected);
	if (selected.empty()) return;

	const unsigned n=selected.size();
	//Check 2 or more selected objects
	if (n < 2) return;

	double minX=DBL_MAX, minY=DBL_MAX, maxX=-DBL_MAX, maxY=-DBL_MAX;

	map<string,unsigned> nodelookup;
	vector<Rectangle*> rs;
	vector<Edge> es;
	for (list<SPItem *>::iterator i(selected.begin());
		i != selected.end();
		++i)
	{
		SPItem *u=*i;
		NR::Rect const item_box(sp_item_bbox_desktop(u));
		NR::Point ll(item_box.min());
		NR::Point ur(item_box.max());
		minX=min(ll[0],minX); minY=min(ll[1],minY);
	        maxX=max(ur[0],maxX); maxY=max(ur[1],maxY);
		nodelookup[u->id]=rs.size();
		rs.push_back(new Rectangle(ll[0],ur[0],ll[1],ur[1]));
	}


	for (list<SPItem *>::iterator i(selected.begin());
		i != selected.end();
		++i)
	{
		SPItem *iu=*i;
		unsigned u=nodelookup[iu->id];
		GSList *nlist=iu->avoidRef->getAttachedShapes(Avoid::runningFrom);
		list<SPItem *> neighbours;
		neighbours.insert<GSListConstIterator<SPItem *> >(neighbours.end(),nlist,NULL);
		for (list<SPItem *>::iterator j(neighbours.begin());
				j != neighbours.end();
				++j) {
			
			SPItem *iv=*j;
			// What do we do if iv not in nodelookup?!?!
			unsigned v=nodelookup[iv->id];
			es.push_back(make_pair(u,v));
		}
		if(nlist) {
			g_slist_free(nlist);
		}
	}
	double width=maxX-minX;
	double height=maxY-minY;
	const unsigned E = es.size();
	double eweights[E];
	fill(eweights,eweights+E,1);

	ConstrainedMajorizationLayout alg(rs,es,eweights,
	       	prefs_get_double_attribute("tools.connector","length",100));
	gchar const *directed = NULL, *overlaps = NULL;
	directed = prefs_get_string_attribute("tools.connector",
		       	"directedlayout");
	overlaps = prefs_get_string_attribute("tools.connector",
		       	"avoidoverlaplayout");
	bool avoid_overlaps = false;
        if (directed && !strcmp(directed, "true")) {
            cout << "Directed layout requested, but not yet implemented\n";
            cout << "  because we haven't coded cyclic removal alg...\n";
	}
        if (overlaps && !strcmp(overlaps, "true")) {
            cout << "Avoid overlaps requested.\n";
	    avoid_overlaps = true;
	}
	alg.setupConstraints(NULL,NULL,avoid_overlaps,
			NULL,NULL,NULL,NULL,NULL,NULL);
	alg.run();
	
	for (list<SPItem *>::iterator it(selected.begin());
		it != selected.end();
		++it)
	{
		SPItem *u=*it;
		if(!isConnector(u)) {
			Rectangle* r=rs[nodelookup[u->id]];
			NR::Rect const item_box(sp_item_bbox_desktop(u));
			NR::Point const curr(item_box.midpoint());
			NR::Point const dest(r->getCentreX(),r->getCentreY());
			sp_item_move_rel(u, NR::translate(dest - curr));
		}
	}
}