1 package org.simantics.debug.graphical.layout;
3 public class ExtensionLayoutAlgorithm {
4 final double OPTIMAL_DISTANCE = 150.0;
6 final double K = C / OPTIMAL_DISTANCE;
8 final double Q = C * OPTIMAL_DISTANCE * OPTIMAL_DISTANCE;
9 final double TOLERANCE = 1e-3;
10 final int MAX_ITERATIONS = 10000;
11 final long TIMEOUT = 1000000000L; // 1 sec
12 final double MAX_MOVE2 = 40000.0;
21 double[] fixedRepulsiveX;
22 double[] fixedRepulsiveY;
25 public ExtensionLayoutAlgorithm(double[][] neighbors,
26 double[] fixedRepulsiveX, double[] fixedRepulsiveY) {
27 this.size = neighbors.length;
28 this.neighbors = neighbors;
29 this.repulsiveSize = fixedRepulsiveX.length;
30 this.fixedRepulsiveX = fixedRepulsiveX;
31 this.fixedRepulsiveY = fixedRepulsiveY;
32 this.posX = new double[size];
33 this.posY = new double[size];
34 this.forceX = new double[size];
35 this.forceY = new double[size];
38 public double[] getPosX() {
42 public double[] getPosY() {
46 private void computeForces() {
47 for(int i=0;i<size;++i) {
53 for(int j=0;j<size;++j)
55 double dx = posX[j] - x;
56 double dy = posY[j] - y;
57 double l2 = dx*dx + dy*dy;
61 for(int j=0;j<repulsiveSize;++j)
63 double dx = fixedRepulsiveX[j] - x;
64 double dy = fixedRepulsiveY[j] - y;
65 double l2 = dx*dx + dy*dy;
70 double[] ns = neighbors[i];
71 for(int j=0;j<ns.length;j+=2) {
72 double dx = ns[j] - x;
73 double dy = ns[j+1] - y;
74 double l2 = dx*dx + dy*dy;
75 double l = Math.sqrt(l2);
85 private boolean converged() {
86 for(int i=0;i<size;++i) {
87 double fx = forceX[i];
88 double fy = forceY[i];
89 double f2 = fx*fx + fy*fy;
96 private void moveAll() {
97 for(int i=0;i<size;++i) {
98 double fx = forceX[i];
99 double fy = forceY[i];
100 double f2 = fx*fx + fy*fy;
102 double s = Math.sqrt(MAX_MOVE2 / f2);
112 public void optimize() {
115 long beginTime = System.nanoTime();
117 for(iter=0;iter<MAX_ITERATIONS;++iter) {
118 if(converged() || ((iter&0xf)==0 && System.nanoTime()-beginTime > TIMEOUT))
122 System.out.println("Elapsed: " + (System.nanoTime()-beginTime)*1e-6 + " ms");
123 System.out.println("Iterations: " + iter);