|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * JavaFXSmartGraph | Copyright 2024 brunomnsilva@gmail.com |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.brunomnsilva.smartgraph.graphview; |
| 26 | + |
| 27 | +import javafx.geometry.Point2D; |
| 28 | + |
| 29 | +/** |
| 30 | + * An implementation of a spring system layout strategy. This strategy allows to freely move the graph along |
| 31 | + * the panel, but if you have a bipartite graph, the sub-graphs will repel each other to the edges of the panel. |
| 32 | + * <br/> |
| 33 | + * Parameters: |
| 34 | + * <br/> |
| 35 | + * Repulsive force (> 0): Recommended [1, 50]. Default 25. The strength of the repulsive force between nodes. |
| 36 | + * Higher values result in greater repulsion. |
| 37 | + * <br/> |
| 38 | + * Attraction force (> 0): Recommended [1, 5]. Default 3. The strength of the attractive force between connected nodes. |
| 39 | + * Higher values result in stronger attraction. Careful, because larger values may not produce stable states. |
| 40 | + * <br/> |
| 41 | + * Attraction scale (> 0): Recommended [1, ?]. Default 10. The scale factor for attraction. |
| 42 | + * It determines the effectiveness of the attraction force based on the distance between connected nodes. |
| 43 | + * <br/> |
| 44 | + * Acceleration: Mandatory ]0, 1]. Default 0.8. The acceleration factor applied to node movements. |
| 45 | + * Higher values result in faster movements. |
| 46 | + * |
| 47 | + * @param <V> The generic type of {@link SmartGraphVertexNode}, i.e., the nodes of a {@link SmartGraphPanel}. |
| 48 | + */ |
| 49 | +public class ForceDirectedSpringSystemLayoutStrategy<V> extends ForceDirectedLayoutStrategy<V> { |
| 50 | + |
| 51 | + private final double repulsiveForce; |
| 52 | + private final double attractionForce; |
| 53 | + private final double attractionScale; |
| 54 | + private final double acceleration; |
| 55 | + |
| 56 | + /* just a scaling factor so all parameters are, at most, two-digit numbers. */ |
| 57 | + private static final double A_THOUSAND = 1000; |
| 58 | + |
| 59 | + /** |
| 60 | + * Constructs a new instance of ForceDirectedSpringGravityLayoutStrategy with default parameters, namely: |
| 61 | + * <br/> |
| 62 | + * repulsiveForce = 25, attractionForce = 3, attractionScale = 10 and acceleration = 0.8. |
| 63 | + */ |
| 64 | + public ForceDirectedSpringSystemLayoutStrategy() { |
| 65 | + this.repulsiveForce = 25; |
| 66 | + this.attractionForce = 3; |
| 67 | + this.attractionScale = 10; |
| 68 | + this.acceleration = 0.8; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Constructs a new instance of ForceDirectedSpringGravityLayoutStrategy with the specified parameters. |
| 73 | + * |
| 74 | + * @param repulsiveForce The strength of the repulsive force between nodes. Higher values result in greater repulsion. |
| 75 | + * @param attractionForce The strength of the attractive force between connected nodes. Higher values result in stronger attraction. |
| 76 | + * @param attractionScale The scale factor for attraction. It determines the effectiveness of the attraction force based on the distance between connected nodes. |
| 77 | + * @param acceleration The acceleration factor applied to node movements. Higher values result in faster movements. |
| 78 | + */ |
| 79 | + public ForceDirectedSpringSystemLayoutStrategy(double repulsiveForce, double attractionForce, double attractionScale, double acceleration) { |
| 80 | + Args.requireGreaterThan(repulsiveForce, "repulsiveForce", 0); |
| 81 | + Args.requireGreaterThan(attractionForce, "attractionForce", 0); |
| 82 | + Args.requireGreaterThan(attractionScale, "attractionScale", 0); |
| 83 | + Args.requireGreaterThan(acceleration, "acceleration", 0); |
| 84 | + Args.requireInRange(acceleration, "acceleration", 0, 1); |
| 85 | + |
| 86 | + this.repulsiveForce = repulsiveForce; |
| 87 | + this.attractionForce = attractionForce; |
| 88 | + this.attractionScale = attractionScale; |
| 89 | + this.acceleration = acceleration; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + protected Point2D computeForceBetween(SmartGraphVertexNode<V> v, SmartGraphVertexNode<V> w, double panelWidth, double panelHeight) { |
| 94 | + // The panel's width and height are not used in this strategy |
| 95 | + // This allows to freely move the graph to a particular region in the panel; |
| 96 | + // On the other hand, e.g., in a bipartite graph the two sub-graphs will repel each other to the edges of the panel |
| 97 | + |
| 98 | + Point2D vPosition = v.getUpdatedPosition(); |
| 99 | + Point2D wPosition = w.getUpdatedPosition(); |
| 100 | + double distance = vPosition.distance(wPosition); |
| 101 | + Point2D forceDirection = wPosition.subtract(vPosition).normalize(); |
| 102 | + |
| 103 | + if (distance < 1) { |
| 104 | + distance = 1; |
| 105 | + } |
| 106 | + |
| 107 | + // attractive force |
| 108 | + Point2D attraction; |
| 109 | + if(v.isAdjacentTo(w)) { |
| 110 | + double attraction_factor = attractionForce * Math.log(distance / attractionScale); |
| 111 | + attraction = forceDirection.multiply(attraction_factor); |
| 112 | + } else { |
| 113 | + attraction = new Point2D(0,0); |
| 114 | + } |
| 115 | + |
| 116 | + // repelling force |
| 117 | + double repulsive_factor = repulsiveForce * A_THOUSAND / (distance * distance); |
| 118 | + Point2D repulsion = forceDirection.multiply(-repulsive_factor); |
| 119 | + |
| 120 | + // combine forces |
| 121 | + Point2D totalForce = new Point2D(attraction.getX() + repulsion.getX(), |
| 122 | + attraction.getY() + repulsion.getY()); |
| 123 | + |
| 124 | + return totalForce.multiply(acceleration); |
| 125 | + } |
| 126 | +} |
0 commit comments