From 19a528cb8e524239aaeea16df87558ca4be4cced Mon Sep 17 00:00:00 2001 From: Tim Dwyer Date: Mon, 13 Feb 2006 02:00:14 +0000 Subject: Added connector graph layout functionality (bzr r122) --- src/graphlayout/graphlayout.cpp | 147 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/graphlayout/graphlayout.cpp (limited to 'src/graphlayout/graphlayout.cpp') diff --git a/src/graphlayout/graphlayout.cpp b/src/graphlayout/graphlayout.cpp new file mode 100644 index 000000000..9828e22fe --- /dev/null +++ b/src/graphlayout/graphlayout.cpp @@ -0,0 +1,147 @@ +/** \file + * Interface between Inkscape code (SPItem) and graphlayout functions. + */ +/* +* Authors: +* Tim Dwyer +* +* Copyright (C) 2005 Authors +* +* Released under GNU GPL. Read the file 'COPYING' for more information. +*/ +#include "graphlayout/graphlayout.h" +#include +#include + +#ifdef HAVE_BOOST_GRAPH_LIB +#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 +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace boost; +// create a typedef for the Graph type +typedef adjacency_list > Graph; +typedef property_map::type WeightMap; +typedef graph_traits::vertex_descriptor Vertex; +typedef std::vector > PositionVec; +typedef iterator_property_map::type> PositionMap; +#endif // HAVE_BOOST_GRAPH_LIB + +/** +* 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; + } +#ifdef HAVE_BOOST_GRAPH_LIB + + + using Inkscape::Util::GSListConstIterator; + std::list selected; + selected.insert >(selected.end(), items, NULL); + if (selected.empty()) return; + int n=selected.size(); + + //Check 2 or more selected objects + if (n < 2) return; + + Graph g; + + std::cout<<"Building graph with "< nodelookup; + for (std::list::iterator it(selected.begin()); + it != selected.end(); + ++it) + { + SPItem *u=*it; + std::cout<<"id:"<id<id,"path",4)) { + nodelookup[u->id]=add_vertex(g); + } + } + WeightMap weightmap=get(edge_weight, g); + std::cout<<"Added vertices: Graph has |V|="<::iterator it(selected.begin()); + it != selected.end(); + ++it) + { + using NR::X; using NR::Y; + SPItem *itu=*it; + Vertex u=nodelookup[itu->id]; + GSList *nlist=itu->avoidRef->getAttachedConnectors(Avoid::ConnRef::runningFrom); + std::list neighbours; + neighbours.insert >(neighbours.end(),nlist,NULL); + std::cout<<" Node "<id<<" has "<::iterator ne(neighbours.begin()); + ne != neighbours.end(); + ++ne) { + + SPItem *itv=*ne; + std::cout<<"neighbour: "<id<id]; + std::cout<<" Neighbour: "<id]].x, + minY+height/2.0+position[nodelookup[u->id]].y); + sp_item_move_rel(u, NR::translate(dest - curr)); + } + } +#else + std::cout<<"Connector network layout not available! Install boost graph library and recompile to enable."<