Inkscape
Vector Graphics Editor
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages Concepts
resize.cpp
Go to the documentation of this file.
1/*
2 * vim: ts=4 sw=4 et tw=0 wm=0
3 *
4 * libcola - A library providing force-directed network layout using the
5 * stress-majorization method subject to separation constraints.
6 *
7 * Copyright (C) 2006-2008 Monash University
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library in the file LICENSE; if not,
21 * write to the Free Software Foundation, Inc., 59 Temple Place,
22 * Suite 330, Boston, MA 02111-1307 USA
23 *
24*/
25
32/*
33* Authors:
34* Tim Dwyer <tgdwyer@gmail.com>
35*/
36#include <iostream>
37#include <iomanip>
38#include <fstream>
39
40#include <vector>
41#include <valarray>
42#include <algorithm>
43#include <float.h>
44#include <libcola/cola.h>
45#include <libtopology/topology_graph.h>
46#include <libproject/project.h>
47#include "graphlayouttest.h"
48using namespace std;
49using namespace cola;
50
51string outFName="resize";
52
53topology::Node* addNode(
54 topology::Nodes& vs, vpsc::Rectangle* r) {
55 topology::Node *v = new topology::Node(vs.size(), r);
56 vs.push_back(v);
57 return v;
58}
59void addToPath(topology::EdgePoints& ps, topology::Node *v, topology::EdgePoint::RectIntersect i) {
60 ps.push_back(new topology::EdgePoint(v,i));
61}
62struct Test : TestConvergence {
63 Test(const double d,const unsigned i,topology::Nodes& vs, topology::Edges& es) : TestConvergence(d,i), vs(vs), es(es), iter(1) {}
64 bool operator()(const double new_stress, valarray<double> & X, valarray<double> & Y) {
65 bool converged = TestConvergence::operator()(new_stress,X,Y);
66 if(converged) {
67 cout << "stress="<<new_stress<<" iteration="<<iter<<endl;
68 stringstream ss;
69 ss<<outFName<<"-"<< setfill('0') << setw(3) << iter++ << ".svg";
70 writeFile(vs,es,ss.str());
71 }
72 return converged;
73 }
74 double lastStress;
75 topology::Nodes& vs;
76 topology::Edges& es;
77 int iter;
78};
79
80void resize() {
81//printf(
82//"tests resizing of a shape. We have a number of disconnected"
83//"shapes. One shape is 'enlarged'. Overlaps should be"
84//"avoided."
85//"\n");
86 const unsigned V = 9;
87 Edge edge_array[] = { Edge(0, 1), Edge(1, 2), Edge(2, 0) };
88 const std::size_t E = sizeof(edge_array) / sizeof(Edge);
89 vector<Edge> es(edge_array,edge_array+E);
90 vector<vpsc::Rectangle*> rs;
91 const double w=54, h=34;
92 const double
93 x[]={406, 444, 474, 406, 441, 375, 408, 373, 339},
94 y[]={279, 224, 179, 92, 135, 135, 179, 226, 179};
95 const unsigned resizeID=6;
96 for(unsigned i=0;i<V;++i) {
97 rs.push_back(new vpsc::Rectangle(x[i],x[i]+w,y[i],y[i]+h));
98 }
99 double idealLength=60;
100 // set up topology graph
101 topology::Nodes vs;
102 for(vector<Rectangle*>::iterator i = rs.begin(); i!=rs.end();++i) {
103 addNode(vs,*i);
104 }
105 topology::Edges tes;
106 writeFile(vs,tes,outFName+"-000.svg");
107
109 vpsc::Rectangle* r=rs[resizeID];
110 resize.push_back(Resize(resizeID,r->getCentreX()-30, r->getCentreY()-30,
111 r->width()+60,r->height()+60));
112 PreIteration preIteration(resize);
113 Test test(0.00001,100,vs,tes);
114 ConstrainedFDLayout alg(rs,es,idealLength,nullptr,test,&preIteration);
115 alg.setTopology(&vs,&tes);
116
117 alg.run(true,true);
118
119 double finalStress=alg.computeStress();
120 printf("finalStress=%f\n",finalStress);
121
122 //assert(finalStress<1e-5);
123 for_each(rs.begin(),rs.end(),delete_object());
124 for_each(tes.begin(),tes.end(),delete_object());
125 for_each(vs.begin(),vs.end(),delete_object());
126}
127int main() {
128 resize();
129 return 0;
130}
131// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4:textwidth=80 :
132
int test()
Definition 2junctions.cpp:5
Implements a constrained force-directed layout algorithm.
Definition cola.h:622
void run(bool x=true, bool y=true)
Implements the main layout loop, taking descent steps until stress is no-longer significantly reduced...
Definition colafd.cpp:316
void setTopology(TopologyAddonInterface *topology)
Set an addon for doing topology preserving layout.
Definition colafd.cpp:944
double computeStress() const
Definition colafd.cpp:1314
A default functor that is called before each iteration in the main loop of the ConstrainedFDLayout::r...
Definition cola.h:168
A Resize specifies a new required bounding box for a node.
Definition cola.h:107
A default functor that is called after each iteration of the layout algorithm.
Definition cola.h:216
virtual bool operator()(const double new_stress, std::valarray< double > &X, std::valarray< double > &Y)
Definition cola.h:226
A rectangle represents a fixed-size shape in the diagram that may be moved to prevent overlaps and sa...
Definition rectangle.h:78
double getCentreY() const
Definition rectangle.h:131
double height() const
Definition rectangle.h:140
double getCentreX() const
Definition rectangle.h:130
double width() const
Definition rectangle.h:139
const double w
Definition conic-4.cpp:19
vector< Edge > es
vector< vpsc::Rectangle * > rs
void writeFile(const topology::Nodes &vs, const topology::Edges &es, const string &outputFileName)
libcola: Force-directed network layout subject to separation constraints library.
Definition box.cpp:25
std::vector< cola::Resize > Resizes
A vector of Resize objects.
Definition cola.h:135
std::pair< unsigned, unsigned > Edge
Edges are simply a pair of indices to entries in the Node vector.
Definition cola.h:68
STL namespace.
void resize()
Definition resize.cpp:80
topology::Node * addNode(topology::Nodes &vs, vpsc::Rectangle *r)
Definition resize.cpp:53
void addToPath(topology::EdgePoints &ps, topology::Node *v, topology::EdgePoint::RectIntersect i)
Definition resize.cpp:59
string outFName
Definition resize.cpp:51
int main()
Definition resize.cpp:127