Skip to content

Commit 9f39b77

Browse files
authored
Recompute graph bounds after packing components in MrTree (#1142)
* Recompute graph bounds after packing components in MrTree * add test for component packing in mrtree
1 parent db74e93 commit 9f39b77

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

plugins/org.eclipse.elk.alg.mrtree/src/org/eclipse/elk/alg/mrtree/ComponentsProcessor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@
2222
import org.eclipse.elk.alg.mrtree.graph.TEdge;
2323
import org.eclipse.elk.alg.mrtree.graph.TGraph;
2424
import org.eclipse.elk.alg.mrtree.graph.TNode;
25+
import org.eclipse.elk.alg.mrtree.intermediate.GraphBoundsProcessor;
2526
import org.eclipse.elk.alg.mrtree.options.InternalProperties;
2627
import org.eclipse.elk.alg.mrtree.options.MrTreeOptions;
2728
import org.eclipse.elk.core.math.ElkPadding;
2829
import org.eclipse.elk.core.math.KVector;
2930
import org.eclipse.elk.core.options.CoreOptions;
31+
import org.eclipse.elk.core.util.NullElkProgressMonitor;
3032
import org.eclipse.elk.graph.properties.IProperty;
3133

3234
import com.google.common.collect.Lists;
@@ -258,6 +260,10 @@ public int compare(final TGraph graph1, final TGraph graph2) {
258260
}
259261
}
260262

263+
// We need to recompute the graphs bounds, since each component only knows its own bounds and the prop merge
264+
// cannot catch this
265+
GraphBoundsProcessor boundsProcessor = new GraphBoundsProcessor();
266+
boundsProcessor.process(result, new NullElkProgressMonitor());
261267
// Move the resulting graph to 0,0 and apply padding
262268
applyPaddingAndNormalizePositions(result);
263269

test/org.eclipse.elk.alg.mrtree.test/src/org/eclipse/elk/alg/mrtree/test/MrTreeGraphSizeTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,38 @@ public void GraphSizeCalculationTest() {
101101
assertEquals(paddingLeft + nodeWidth + nodeNodeSpacing + nodeWidth + paddingRight, graph.getWidth(), DOUBLE_EQ_EPSILON);
102102
assertEquals(paddingTop + nodeHeight + nodeNodeSpacing + nodeHeight + paddingBottom, graph.getHeight(), DOUBLE_EQ_EPSILON);
103103
}
104+
105+
/**
106+
* Tests that the graph size is correct when the graph consists of disconnected components.
107+
*/
108+
@Test
109+
public void ComponentsGraphSizeCalculationTest() {
110+
PlainJavaInitialization.initializePlainJavaLayout();
111+
ElkNode graph = ElkGraphUtil.createGraph();
112+
graph.setProperty(CoreOptions.ALGORITHM, MrTreeOptions.ALGORITHM_ID);
113+
graph.setProperty(CoreOptions.PADDING, new ElkPadding(0));
114+
graph.setProperty(CoreOptions.SPACING_NODE_NODE, 0.0);
115+
// set aspect ratio to high value to force components to be laid out horizontally for the test
116+
graph.setProperty(CoreOptions.ASPECT_RATIO, 1000.0);
117+
118+
119+
ElkNode n1 = ElkGraphUtil.createNode(graph);
120+
n1.setDimensions(nodeWidth, nodeHeight);
121+
ElkNode n2 = ElkGraphUtil.createNode(graph);
122+
n2.setDimensions(nodeWidth, nodeHeight);
123+
124+
125+
// prepare layout engine
126+
LayoutConfigurator config = new LayoutConfigurator();
127+
ElkUtil.applyVisitors(graph, config, new LayoutAlgorithmResolver());
128+
// call layout with layout engine
129+
try {
130+
new RecursiveGraphLayoutEngine().layout(graph, new BasicProgressMonitor());
131+
} catch (UnsupportedGraphException exception) {
132+
fail(exception.toString());
133+
}
134+
135+
assertEquals(nodeWidth + nodeWidth, graph.getWidth(), DOUBLE_EQ_EPSILON);
136+
assertEquals(nodeHeight, graph.getHeight(), DOUBLE_EQ_EPSILON);
137+
}
104138
}

0 commit comments

Comments
 (0)