• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

rocs/RocsCore

  • sources
  • kde-4.12
  • kdeedu
  • rocs
  • RocsCore
  • Modifiers
Topology.cpp
Go to the documentation of this file.
1 /*
2  This file is part of Rocs.
3  Copyright (C) 2011 Andreas Cord-Landwehr <cola@uni-paderborn.de>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 
21 #include "Topology.h"
22 
23 #include "DataStructure.h"
24 #include "Data.h"
25 #include "Pointer.h"
26 
27 #include <QList>
28 #include <QPair>
29 #include <QVector>
30 
31 #include <KDebug>
32 
33 #include <boost/graph/fruchterman_reingold.hpp>
34 #include <boost/graph/circle_layout.hpp>
35 #include <boost/graph/random_layout.hpp>
36 #include <boost/graph/adjacency_list.hpp>
37 #include <boost/graph/topology.hpp>
38 #include <boost/lexical_cast.hpp>
39 #include <boost/random/linear_congruential.hpp>
40 
41 #include "CoreTypes.h"
42 
43 typedef boost::adjacency_list < boost::listS, boost::vecS, boost::undirectedS,
44  boost::property<boost::vertex_name_t, std::string> >
45  Graph;
46 typedef boost::rectangle_topology<> topology_type;
47 typedef topology_type::point_type point_type;
48 typedef QVector<point_type> PositionVec;
49 typedef boost::iterator_property_map < PositionVec::iterator,
50  boost::property_map<Graph, boost::vertex_index_t>::type >
51  PositionMap;
52 typedef boost::graph_traits<Graph>::vertex_descriptor Vertex;
53 typedef QPair<int, int> Edge;
54 
55 
56 Topology::Topology()
57 {
58 
59 }
60 
61 Topology::~Topology()
62 {
63 
64 }
65 
66 void Topology::applyMinCutTreeAlignment(DataList dataList)
67 {
68  // dataList must be at least of length 2, and two nodes cannot have crossing edges
69  if (dataList.count() < 3) {
70  return;
71  }
72 
73  PositionVec position_vec(dataList.count());
74 
75  // set box inside which we may reposition
76  QList<qreal> xList;
77  QList<qreal> yList;
78  foreach(DataPtr data, dataList) {
79  xList << data->x();
80  yList << data->y();
81  }
82  qSort(xList.begin(), xList.end());
83  qSort(yList.begin(), yList.end());
84 
85  // do not perform algorithm if graph is very dense:
86  // this prevents very long algorithm computations and possible threading issues
87  if (xList.last() - xList.first() < 10 && yList.last() - yList.first() < 10 ) {
88  qDebug() << "Aborting min cut alignment: nodes are already close to each other.";
89  return;
90  }
91 
92  topology_type topology(xList.first(), yList.first(), xList.last(), yList.last());
93 
94  // create IDs for all nodes
95  QMap<Data*, int> node_mapping;
96  QMap<QPair<int, int>, PointerPtr > edge_mapping; // to map all edges back afterwards
97  int counter = 0;
98  foreach(DataPtr data, dataList) {
99  node_mapping[data.get()] = counter++;
100  }
101 
102  DataStructurePtr ds = dataList.first()->dataStructure();
103  //FIXME only default data type considered
104  QVector<Edge> edges(ds->pointers(0).count());
105 
106  counter = 0;
107  //FIXME only default pointer type considered
108  foreach(PointerPtr p, ds->pointers(0)) {
109  edges[counter++] = Edge(node_mapping[p->from().get()], node_mapping[p->to().get()]);
110  }
111 
112  // setup the graph
113  Graph graph(
114  edges.begin(),
115  edges.end(),
116  dataList.count()
117  );
118 
119  PositionMap positionMap(position_vec.begin(), get(boost::vertex_index, graph));
120  counter = 0;
121  foreach(DataPtr data, dataList) {
122  positionMap[counter][0] = data->x();
123  positionMap[counter][1] = data->y();
124  counter++;
125  }
126 
127  // minimize cuts by Fruchtman-Reingold layout algorithm
128  boost::fruchterman_reingold_force_directed_layout< topology_type, Graph, PositionMap >
129  (graph,
130  positionMap,
131  topology,
132  boost::cooling(boost::linear_cooling<double>(100))
133  );
134 
135  // put nodes at whiteboard as generated
136  foreach(DataPtr data, dataList) {
137  Vertex v = boost::vertex(node_mapping[data.get()], graph);
138  data->setX(positionMap[v][0]);
139  data->setY(positionMap[v][1]);
140  }
141 }
142 
143 void Topology::applyCircleAlignment(DataList dataList, qreal radius)
144 {
145  if (dataList.length() == 0) {
146  return;
147  }
148 
149  PositionVec position_vec(dataList.count());
150 
151  if(radius == 0) {
152  // set box inside which we may reposition
153  QList<qreal> xList;
154  QList<qreal> yList;
155  foreach(DataPtr data, dataList) {
156  xList << data->x();
157  yList << data->y();
158  }
159  qSort(xList.begin(), xList.end());
160  qSort(yList.begin(), yList.end());
161 
162  radius = fmax(abs(xList.first() - xList.last()), abs(yList.first() - yList.last())) / 2;
163  }
164 
165  // create IDs for all nodes
166  QMap<Data*, int> node_mapping;
167  QMap<QPair<int, int>, PointerPtr > edge_mapping; // to map all edges back afterwards
168  int counter = 0;
169  foreach(DataPtr data, dataList) {
170  node_mapping[data.get()] = counter++;
171  }
172 
173  DataStructurePtr ds = dataList.first()->dataStructure();
174  //FIXME only default pointer type considered
175  QVector<Edge> edges(ds->pointers(0).count());
176 
177  counter = 0;
178  //FIXME only default pointer type considered
179  foreach(PointerPtr p, ds->pointers(0)) {
180  edges[counter++] = Edge(node_mapping[p->from().get()], node_mapping[p->to().get()]);
181  }
182 
183  // setup the graph
184  Graph graph(
185  edges.begin(),
186  edges.end(),
187  dataList.count()
188  );
189 
190  PositionMap positionMap(position_vec.begin(), get(boost::vertex_index, graph));
191  counter = 0;
192  foreach(DataPtr data, dataList) {
193  positionMap[counter][0] = data->x();
194  positionMap[counter][1] = data->y();
195  counter++;
196  }
197 
198  // layout to circle
199  boost::circle_graph_layout< Graph, PositionMap >
200  (graph,
201  positionMap,
202  radius
203  );
204 
205  // put nodes at whiteboard as generated
206  foreach(DataPtr data, dataList) {
207  Vertex v = boost::vertex(node_mapping[data.get()], graph);
208  data->setX(positionMap[v][0]);
209  data->setY(positionMap[v][1]);
210  }
211 }
212 
213 
214 void Topology::directedGraphDefaultTopology(DataStructurePtr dataStructure)
215 {
216  //TODO: port to graphviz layout functions
217  kDebug() << "Temporary implementation, should be replaced soon.";
218 
219  QList<DataPtr> allDataList;
220  foreach(int type, dataStructure->document()->dataTypeList()) {
221  allDataList << dataStructure->dataList(type);
222  }
223  applyCircleAlignment(allDataList, 300);
224  applyMinCutTreeAlignment(allDataList);
225 }
226 
227 
228 void Topology::undirectedGraphDefaultTopology(DataStructurePtr dataStructure)
229 {
230  //TODO: port to graphviz layout functions
231  kDebug() << "Temporary implementation, should be replaced soon.";
232 
233  QList<DataPtr> allDataList;
234  foreach(int type, dataStructure->document()->dataTypeList()) {
235  allDataList << dataStructure->dataList(type);
236  }
237  applyCircleAlignment(allDataList, 300);
238  applyMinCutTreeAlignment(allDataList);
239 }
240 
Graph
boost::adjacency_list< boost::listS, boost::vecS, boost::undirectedS, boost::property< boost::vertex_name_t, std::string > > Graph
Definition: Topology.cpp:45
Topology::undirectedGraphDefaultTopology
void undirectedGraphDefaultTopology(DataStructurePtr dataStructure)
applies a default topology for undirected graphs
Definition: Topology.cpp:228
PositionVec
QVector< point_type > PositionVec
Definition: Topology.cpp:48
Topology.h
DataStructurePtr
boost::shared_ptr< DataStructure > DataStructurePtr
Definition: CoreTypes.h:38
Topology::applyCircleAlignment
void applyCircleAlignment(DataList dataList, qreal radius=0)
applies Circle topology to data set
Definition: Topology.cpp:143
Topology::directedGraphDefaultTopology
void directedGraphDefaultTopology(DataStructurePtr dataStructure)
applies a default topology for undirected graphs
Definition: Topology.cpp:214
DataList
QList< boost::shared_ptr< Data > > DataList
Definition: CoreTypes.h:30
CoreTypes.h
Edge
QPair< int, int > Edge
Definition: Topology.cpp:53
topology_type
boost::rectangle_topology topology_type
Definition: Topology.cpp:46
Data.h
PointerPtr
boost::shared_ptr< Pointer > PointerPtr
Definition: CoreTypes.h:35
DataStructure.h
Vertex
boost::graph_traits< Graph >::vertex_descriptor Vertex
Definition: Topology.cpp:52
point_type
topology_type::point_type point_type
Definition: Topology.cpp:47
Topology::~Topology
virtual ~Topology()
Definition: Topology.cpp:61
Pointer.h
Topology::Topology
Topology()
Definition: Topology.cpp:56
DataPtr
boost::shared_ptr< Data > DataPtr
Definition: CoreTypes.h:34
Topology::applyMinCutTreeAlignment
void applyMinCutTreeAlignment(DataList dataList)
applies Fruchterman-Reingold cut minimization
Definition: Topology.cpp:66
PositionMap
boost::iterator_property_map< PositionVec::iterator, boost::property_map< Graph, boost::vertex_index_t >::type > PositionMap
Definition: Topology.cpp:51
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:26 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

rocs/RocsCore

Skip menu "rocs/RocsCore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal