1
2
3
4
5
6
7
8
9
10
11 package edu.uci.ics.jung.algorithms.layout3d;
12
13 import java.util.Collection;
14 import java.util.ConcurrentModificationException;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Map;
18 import java.util.Set;
19
20 import javax.media.j3d.BoundingSphere;
21 import javax.vecmath.Point3f;
22
23 import org.apache.commons.collections15.Transformer;
24 import org.apache.commons.collections15.map.LazyMap;
25
26 import edu.uci.ics.jung.graph.Graph;
27
28
29
30
31
32
33
34
35
36
37 public abstract class AbstractLayout<V, E> implements Layout<V,E> {
38
39
40
41
42
43 private Set<V> dontmove = new HashSet<V>();
44
45 private BoundingSphere size;
46 private Graph<V, E> graph;
47
48 protected Map<V, Point3f> locations =
49 LazyMap.decorate(new HashMap<V, Point3f>(),
50 new Transformer<V,Point3f>() {
51 public Point3f transform(V arg0) {
52 return new Point3f();
53 }});
54
55
56
57
58
59
60
61
62
63 protected AbstractLayout(Graph<V, E> graph) {
64 this.graph = graph;
65 }
66
67 protected AbstractLayout(Graph<V,E> graph, Transformer<V,Point3f> initializer) {
68 this.graph = graph;
69 this.locations = LazyMap.decorate(new HashMap<V,Point3f>(), initializer);
70 }
71
72 protected AbstractLayout(Graph<V,E> graph, BoundingSphere size) {
73 this.graph = graph;
74 this.size = size;
75 }
76
77 protected AbstractLayout(Graph<V,E> graph, Transformer<V,Point3f> initializer, BoundingSphere size) {
78 this.graph = graph;
79 this.locations = LazyMap.decorate(new HashMap<V,Point3f>(), initializer);
80 this.size = size;
81 }
82
83 public void setGraph(Graph<V,E> graph) {
84 this.graph = graph;
85 if(size != null && graph != null) {
86 initialize();
87 }
88 }
89
90
91
92
93
94
95
96
97
98 public void setSize(BoundingSphere size) {
99
100 if(size != null && graph != null) {
101
102 BoundingSphere oldSize = this.size;
103 this.size = size;
104 initialize();
105
106 if(oldSize != null) {
107 adjustLocations(oldSize, size);
108 }
109 }
110 }
111
112 private void adjustLocations(BoundingSphere oldSize, BoundingSphere size) {
113
114 float oldWidth = 0;
115 float oldHeight = 0;
116 float oldDepth = 0;
117 float width = 0;
118 float height = 0;
119 float depth = 0;
120
121 oldWidth = oldHeight = oldDepth = (float) (2*oldSize.getRadius());
122 width = height = depth = (float) (2*size.getRadius());
123
124 float xOffset = (oldWidth - width) / 2;
125 float yOffset = (oldHeight - height) / 2;
126 float zOffset = (oldDepth - depth) / 2;
127
128
129 while(true) {
130 try {
131 for(V v : getGraph().getVertices()) {
132 offsetVertex(v, xOffset, yOffset, zOffset);
133 }
134 break;
135 } catch(ConcurrentModificationException cme) {
136 }
137 }
138 }
139
140 public boolean isLocked(V v) {
141 return dontmove.contains(v);
142 }
143
144 public Collection<V> getVertices() {
145 return getGraph().getVertices();
146 }
147
148
149
150
151
152 public abstract void initialize();
153
154 public void setInitializer(Transformer<V,Point3f> initializer) {
155 this.locations = LazyMap.decorate(new HashMap<V,Point3f>(locations), initializer);
156 }
157
158
159
160
161
162
163
164 public BoundingSphere getSize() {
165 return size;
166 }
167
168
169
170
171
172
173
174
175 private Point3f getCoordinates(V v) {
176 return locations.get(v);
177 }
178
179 public Point3f transform(V v) {
180 return getCoordinates(v);
181 }
182
183
184
185
186
187
188 public double getX(V v) {
189 assert getCoordinates(v) != null : "Cannot getX for an unmapped vertex "+v;
190 return getCoordinates(v).getX();
191 }
192
193
194
195
196
197
198 public double getY(V v) {
199 assert getCoordinates(v) != null : "Cannot getY for an unmapped vertex "+v;
200 return getCoordinates(v).getY();
201 }
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 protected void offsetVertex(V v, float xOffset, float yOffset, float zOffset) {
217 Point3f c = getCoordinates(v);
218 c.set(c.getX()+xOffset, c.getY()+yOffset, c.getZ()+zOffset);
219 setLocation(v, c);
220 }
221
222
223
224
225
226
227 public Graph<V, E> getGraph() {
228 return graph;
229 }
230
231
232
233
234
235
236
237 public void setLocation(V picked, float x, float y, float z) {
238 Point3f coord = getCoordinates(picked);
239 coord.set(x, y, z);
240 }
241
242 public void setLocation(V picked, Point3f p) {
243 Point3f coord = getCoordinates(picked);
244 coord.set(p);
245 }
246
247
248
249
250 public void lock(V v, boolean state) {
251 if(state == true) dontmove.add(v);
252 else dontmove.remove(v);
253 }
254
255 public void lock(boolean lock) {
256 for(V v : graph.getVertices()) {
257 lock(v, lock);
258 }
259 }
260 }