$darkmode
DENOPTIM
DNPSpringLayout.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2022 Marco Foscato <marco.foscato@uib.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.gui;
20
21import java.awt.Dimension;
22import java.awt.geom.Point2D;
23import java.util.Map;
24import java.util.concurrent.ExecutionException;
25
26import com.google.common.base.Function;
27import com.google.common.base.Functions;
28
29import denoptim.gui.GraphViewerPanel.JVertex;
30import edu.uci.ics.jung.algorithms.layout.SpringLayout2;
31import edu.uci.ics.jung.algorithms.layout.util.RandomLocationTransformer;
32import edu.uci.ics.jung.graph.Graph;
33
45public class DNPSpringLayout<V, E> extends SpringLayout2<V, E>
46{
47 private Map<String, Point2D> oldVertexPosition;
48 private Dimension oldRange = new Dimension();
49
50 private int iteration = 0;
51 private int maxIterations = 500;
52 private boolean lockInitialPositions = false;
53
54//------------------------------------------------------------------------------
55
56 @SuppressWarnings("unchecked")
57 public DNPSpringLayout(Graph<V, E> g)
58 {
59 super(g, (Function<E,Integer>)Functions.<Integer>constant(60));
60 setForceMultiplier(1);
61 setRepulsionRange(200);
62 }
63
64//------------------------------------------------------------------------------
65
66 public void setInitialLocations(Map<String, Point2D> vertexPosition,
67 boolean lock)
68 {
69 this.lockInitialPositions = lock;
70 this.oldVertexPosition = vertexPosition;
71 double maxX = -1.0;
72 double maxY = -1.0;
73 for (Point2D p : vertexPosition.values())
74 {
75 if (p.getX() > maxX)
76 maxX = p.getX();
77
78 if (p.getY() > maxY)
79 maxY = p.getY();
80 }
81 this.oldRange.width = (int) maxX;
82 this.oldRange.height = (int) maxY;
83 }
84
85//------------------------------------------------------------------------------
86
87 @Override
88 public void initialize() {
89 super.initialize();
90 if (oldVertexPosition != null)
91 {
92 // Defines the initial position of the vertexes according to the
93 // Point2D stored in the local field, which comes from a previous
94 // visualisation of a previous version of this graph.
95 setInitializer(new RecreateKnownPositions(oldVertexPosition));
96
98 {
99 // Freeze/lock the position of those nodes with known position
100 for (V v : this.graph.getVertices())
101 {
102 if (v instanceof JVertex)
103 {
104 if (oldVertexPosition.containsKey(((JVertex) v).idStr))
105 lock(v,true);
106 }
107 }
108 }
109 }
110 }
111
112//------------------------------------------------------------------------------
113
114 private class RecreateKnownPositions implements Function<V, Point2D>
115 {
116 private Map<String, Point2D> vertexPosition;
117 private RandomLocationTransformer<V> randomPosition =
118 new RandomLocationTransformer<>(new Dimension(oldRange));
119
120 public RecreateKnownPositions(Map<String, Point2D> vertexPosition)
121 {
122 this.vertexPosition = vertexPosition;
123 }
124
125 @Override
126 public Point2D apply(V v)
127 {
128 if (v instanceof JVertex)
129 {
130 Point2D p = vertexPosition.get(((JVertex)v).idStr);
131 if (p != null)
132 return p;
133 }
134 return randomPosition.apply(v);
135 }
136 }
137
138//------------------------------------------------------------------------------
139
146 public Point2D getVertexPosition(V vertex) throws ExecutionException
147 {
148 return locations.get(vertex);
149 }
150
151//------------------------------------------------------------------------------
152
156 @Override
157 public void step() {
158 super.step();
159 iteration++;
161 }
162
163//------------------------------------------------------------------------------
164
165 private void testTermination()
166 {
168 {
169 done = true;
170 }
171 }
172
173//------------------------------------------------------------------------------
174
175}
RecreateKnownPositions(Map< String, Point2D > vertexPosition)
This layout extends the SpringLayout to change its behaviour.
void step()
Relaxation step.
Point2D getVertexPosition(V vertex)
Returns the current value of the position of the given vertex.
void setInitialLocations(Map< String, Point2D > vertexPosition, boolean lock)
Map< String, Point2D > oldVertexPosition