@@ -41,8 +41,6 @@ Development and Distribution License("CDDL") (collectively, the
41
41
*/
42
42
package org .gephi .layout .plugin .force .yifanHu ;
43
43
44
- import java .util .ArrayList ;
45
- import java .util .List ;
46
44
import org .gephi .graph .api .Edge ;
47
45
import org .gephi .graph .api .Graph ;
48
46
import org .gephi .graph .api .Node ;
@@ -58,6 +56,9 @@ Development and Distribution License("CDDL") (collectively, the
58
56
import org .gephi .layout .spi .LayoutProperty ;
59
57
import org .openide .util .NbBundle ;
60
58
59
+ import java .util .ArrayList ;
60
+ import java .util .List ;
61
+
61
62
/**
62
63
* Hu's basic algorithm
63
64
*
@@ -229,88 +230,111 @@ public void initAlgo() {
229
230
return ;
230
231
}
231
232
graph = graphModel .getGraphVisible ();
232
- energy = Float .POSITIVE_INFINITY ;
233
- for (Node n : graph .getNodes ()) {
234
- n .setLayoutData (new ForceVector ());
233
+ graph .readLock ();
234
+ try
235
+ {
236
+ energy = Float .POSITIVE_INFINITY ;
237
+ for (Node n : graph .getNodes ())
238
+ {
239
+ n .setLayoutData (new ForceVector ());
240
+ }
241
+ progress = 0 ;
242
+ setConverged (false );
243
+ setStep (initialStep );
244
+ } finally {
245
+ graph .readUnlock ();
235
246
}
236
- progress = 0 ;
237
- setConverged (false );
238
- setStep (initialStep );
239
247
}
240
248
241
249
@ Override
242
250
public void endAlgo () {
243
- for (Node n : graph .getNodes ()) {
244
- n .setLayoutData (null );
251
+ graph .readLock ();
252
+ try
253
+ {
254
+ for (Node n : graph .getNodes ())
255
+ {
256
+ n .setLayoutData (null );
257
+ }
258
+ } finally {
259
+ graph .readUnlock ();
245
260
}
246
261
}
247
262
248
263
@ Override
249
264
public void goAlgo () {
250
265
graph = graphModel .getGraphVisible ();
251
266
graph .readLock ();
252
- Node [] nodes = graph .getNodes ().toArray ();
253
- for (Node n : nodes ) {
254
- if (n .getLayoutData () == null || !(n .getLayoutData () instanceof ForceVector )) {
255
- n .setLayoutData (new ForceVector ());
267
+ try
268
+ {
269
+ Node [] nodes = graph .getNodes ().toArray ();
270
+ for (Node n : nodes )
271
+ {
272
+ if (n .getLayoutData () == null || !(n .getLayoutData () instanceof ForceVector ))
273
+ {
274
+ n .setLayoutData (new ForceVector ());
275
+ }
256
276
}
257
- }
258
277
259
- // Evaluates n^2 inter node forces using BarnesHut.
260
- QuadTree tree = QuadTree .buildTree (graph , getQuadTreeMaxLevel ());
278
+ // Evaluates n^2 inter node forces using BarnesHut.
279
+ QuadTree tree = QuadTree .buildTree (graph , getQuadTreeMaxLevel ());
261
280
262
- // double electricEnergy = 0; ///////////////////////
263
- // double springEnergy = 0; ///////////////////////
264
- BarnesHut barnes = new BarnesHut (getNodeForce ());
265
- barnes .setTheta (getBarnesHutTheta ());
266
- for (Node node : nodes ) {
267
- ForceVector layoutData = node .getLayoutData ();
281
+ // double electricEnergy = 0; ///////////////////////
282
+ // double springEnergy = 0; ///////////////////////
283
+ BarnesHut barnes = new BarnesHut (getNodeForce ());
284
+ barnes .setTheta (getBarnesHutTheta ());
285
+ for (Node node : nodes )
286
+ {
287
+ ForceVector layoutData = node .getLayoutData ();
268
288
269
- ForceVector f = barnes .calculateForce (node , tree );
270
- layoutData .add (f );
271
- // electricEnergy += f.getEnergy();
272
- }
289
+ ForceVector f = barnes .calculateForce (node , tree );
290
+ layoutData .add (f );
291
+ // electricEnergy += f.getEnergy();
292
+ }
273
293
274
- // Apply edge forces.
294
+ // Apply edge forces.
275
295
276
- for (Edge e : graph .getEdges ()) {
277
- if (!e .getSource ().equals (e .getTarget ())) {
278
- Node n1 = e .getSource ();
279
- Node n2 = e .getTarget ();
280
- ForceVector f1 = n1 .getLayoutData ();
281
- ForceVector f2 = n2 .getLayoutData ();
282
-
283
- ForceVector f = getEdgeForce ().calculateForce (n1 , n2 );
284
- f1 .add (f );
285
- f2 .subtract (f );
296
+ for (Edge e : graph .getEdges ())
297
+ {
298
+ if (!e .getSource ().equals (e .getTarget ()))
299
+ {
300
+ Node n1 = e .getSource ();
301
+ Node n2 = e .getTarget ();
302
+ ForceVector f1 = n1 .getLayoutData ();
303
+ ForceVector f2 = n2 .getLayoutData ();
304
+
305
+ ForceVector f = getEdgeForce ().calculateForce (n1 , n2 );
306
+ f1 .add (f );
307
+ f2 .subtract (f );
308
+ }
286
309
}
287
- }
288
310
289
- // Calculate energy and max force.
290
- energy0 = energy ;
291
- energy = 0 ;
292
- double maxForce = 1 ;
293
- for (Node n : nodes ) {
294
- ForceVector force = n .getLayoutData ();
311
+ // Calculate energy and max force.
312
+ energy0 = energy ;
313
+ energy = 0 ;
314
+ double maxForce = 1 ;
315
+ for (Node n : nodes )
316
+ {
317
+ ForceVector force = n .getLayoutData ();
295
318
296
- energy += force .getNorm ();
297
- maxForce = Math .max (maxForce , force .getNorm ());
298
- }
319
+ energy += force .getNorm ();
320
+ maxForce = Math .max (maxForce , force .getNorm ());
321
+ }
299
322
300
- // Apply displacements on nodes.
301
- for (Node n : nodes ) {
302
- if (!n .isFixed ()) {
303
- ForceVector force = n .getLayoutData ();
323
+ // Apply displacements on nodes.
324
+ for (Node n : nodes )
325
+ {
326
+ if (!n .isFixed ())
327
+ {
328
+ ForceVector force = n .getLayoutData ();
304
329
305
- force .multiply ((float ) (1.0 / maxForce ));
306
- getDisplacement ().moveNode (n , force );
330
+ force .multiply ((float ) (1.0 / maxForce ));
331
+ getDisplacement ().moveNode (n , force );
332
+ }
307
333
}
334
+ postAlgo ();
335
+ } finally {
336
+ graph .readUnlock ();
308
337
}
309
- postAlgo ();
310
- // springEnergy = energy - electricEnergy;
311
- // System.out.println("electric: " + electricEnergy + " spring: " + springEnergy);
312
- // System.out.println("energy0 = " + energy0 + " energy = " + energy);
313
- graph .readUnlock ();
314
338
}
315
339
316
340
0 commit comments