use of org.eclipse.elk.core.math.KVector in project elk by eclipse.
the class EdgeLengthOptimization method evaluate.
/**
* This evaluation method calculates the sum of the length of each outgoing edge from the root node.
*/
@Override
public double evaluate(final ElkNode root) {
double edgeLength = 0;
for (ElkEdge edge : ElkGraphUtil.allOutgoingEdges(root)) {
ElkNode target = ElkGraphUtil.connectableShapeToNode(edge.getTargets().get(0));
double targetX = target.getX() + target.getWidth() / 2;
double targetY = target.getY() + target.getHeight() / 2;
double rootX = root.getX() + root.getWidth() / 2;
double rootY = root.getY() + root.getHeight() / 2;
// Clipping
KVector vector = new KVector();
vector.x = targetX - rootX;
vector.y = targetY - rootY;
KVector sourceClip = new KVector(vector.x, vector.y);
ElkMath.clipVector(sourceClip, root.getWidth(), root.getHeight());
vector.x -= sourceClip.x;
vector.y -= sourceClip.y;
rootX = targetX - vector.x;
rootY = targetY - vector.y;
KVector targetClip = new KVector(vector.x, vector.y);
ElkMath.clipVector(targetClip, target.getWidth(), target.getHeight());
vector.x -= targetClip.x;
vector.y -= targetClip.y;
targetX = rootX + vector.x;
targetY = rootY + vector.y;
double vectorX = targetX - rootX;
double vectorY = targetY - rootY;
edgeLength += Math.sqrt(vectorX * vectorX + vectorY * vectorY);
}
return edgeLength;
}
use of org.eclipse.elk.core.math.KVector in project elk by eclipse.
the class MaxSTPhase method process.
@Override
public void process(final Graph graph, final IElkProgressMonitor progressMonitor) {
progressMonitor.begin("Maximum spanning tree construction", 1);
// inverted cost function
ICostFunction invertedCF = e -> {
return -graph.costFunction.cost(e);
};
KVector root;
if (graph.preferredRoot != null) {
root = graph.preferredRoot.vertex;
} else {
root = graph.vertices.get(0).vertex;
}
Tree<KVector> tree;
if (graph.getProperty(InternalProperties.DEBUG_SVG)) {
tree = NaiveMinST.createSpanningTree(graph.tEdges, root, invertedCF, ElkUtil.debugFolderPath("spore") + "20minst");
} else {
tree = NaiveMinST.createSpanningTree(graph.tEdges, root, invertedCF);
}
// convert result to a Tree that can be used in the execution phase
convert(tree, graph);
progressMonitor.done();
}
use of org.eclipse.elk.core.math.KVector in project elk by eclipse.
the class CalculateGraphSize method process.
/**
* Shift the nodes such that each nodes has x and y coordinates bigger 0.
*/
public void process(final ElkNode graph, final IElkProgressMonitor progressMonitor) {
progressMonitor.begin("Calculate Graph Size", 1);
progressMonitor.logGraph(graph, "Before");
// calculate the offset from border spacing and node distribution
double minXPos = Double.MAX_VALUE;
double minYPos = Double.MAX_VALUE;
double maxXPos = Double.MIN_VALUE;
double maxYPos = Double.MIN_VALUE;
for (ElkNode node : graph.getChildren()) {
double posX = node.getX();
double posY = node.getY();
double width = node.getWidth();
double height = node.getHeight();
ElkMargin margins = node.getProperty(CoreOptions.MARGINS);
minXPos = Math.min(minXPos, posX - margins.left);
minYPos = Math.min(minYPos, posY - margins.top);
maxXPos = Math.max(maxXPos, posX + width + margins.right);
maxYPos = Math.max(maxYPos, posY + height + margins.bottom);
}
ElkPadding padding = graph.getProperty(CoreOptions.PADDING);
KVector offset = new KVector(minXPos - padding.getLeft(), minYPos - padding.getTop());
// process the nodes
for (ElkNode node : graph.getChildren()) {
// set the node position
node.setX(node.getX() - offset.x);
node.setY(node.getY() - offset.y);
}
// set up the graph
double width = maxXPos - minXPos + padding.getHorizontal();
double height = maxYPos - minYPos + padding.getVertical();
graph.setWidth(width);
graph.setHeight(height);
progressMonitor.logGraph(graph, "After");
}
use of org.eclipse.elk.core.math.KVector in project elk by eclipse.
the class GraphRenderer method renderNodeChildren.
/**
* Paints all children of the given parent node that fall into the given dirty area.
*
* @param parent the node whose children to paint
* @param graphics the graphics context used to paint
* @param area dirty area that needs painting
* @param offset offset to be added to relative coordinates
* @param nodeAlpha alpha value for nodes
*/
private void renderNodeChildren(final ElkNode parent, final GC graphics, final Rectangle area, final KVector offset, final int nodeAlpha) {
for (ElkNode childNode : parent.getChildren()) {
PaintRectangle rect = boundsMap.get(childNode);
if (rect == null) {
rect = new PaintRectangle(childNode, offset, scale);
boundsMap.put(childNode, rect);
}
KVector childOffset = new KVector(rect.x, rect.y);
// render the child node and its content
if (!rect.painted && rect.intersects(area)) {
// paint this node
graphics.setAlpha(nodeAlpha);
if (configurator.getNodeFillColor() != null) {
graphics.setBackground(configurator.getNodeFillColor());
graphics.fillRectangle(rect.x, rect.y, rect.width, rect.height);
}
if (configurator.getNodeBorderColor() != null) {
graphics.setForeground(configurator.getNodeBorderColor());
graphics.drawRectangle(rect.x, rect.y, rect.width, rect.height);
}
rect.painted = true;
renderNodeChildren(childNode, graphics, area, childOffset, nodeAlpha);
}
// render node labels
if (configurator.getNodeLabelFont() != null) {
graphics.setFont(configurator.getNodeLabelFont());
for (ElkLabel label : childNode.getLabels()) {
renderLabel(label, graphics, area, childOffset, nodeAlpha);
}
}
// render ports
for (ElkPort port : childNode.getPorts()) {
renderPort(port, graphics, area, childOffset, nodeAlpha);
}
}
// Paint the edges contained in this node
for (ElkEdge childEdge : parent.getContainedEdges()) {
renderEdge(childEdge, graphics, area, offset, nodeAlpha);
}
}
use of org.eclipse.elk.core.math.KVector in project elk by eclipse.
the class GraphRenderer method calculateBaseOffsetFromTopLevelGraph.
/**
* Calculate the base offset so all graph elements can fit onto the canvas.
*
* @param parent the graph to be drawn
*/
public void calculateBaseOffsetFromTopLevelGraph(final ElkNode parent) {
double minX = Double.POSITIVE_INFINITY;
double minY = Double.POSITIVE_INFINITY;
for (ElkNode childNode : parent.getChildren()) {
minX = Math.min(minX, childNode.getX());
minY = Math.min(minY, childNode.getY());
for (ElkLabel childNodeLabel : childNode.getLabels()) {
minX = Math.min(minX, childNode.getX() + childNodeLabel.getX());
minY = Math.min(minY, childNode.getY() + childNodeLabel.getY());
}
}
for (ElkEdge edge : parent.getContainedEdges()) {
PaintRectangle edgeRect = new PaintRectangle(edge, new KVector(), scale);
minX = Math.min(minX, edgeRect.x);
minY = Math.min(minY, edgeRect.y);
for (ElkLabel edgeLabel : edge.getLabels()) {
minX = Math.min(minX, edgeLabel.getX());
minY = Math.min(minY, edgeLabel.getY());
}
}
baseOffset.x = -Math.min(0, minX);
baseOffset.y = -Math.min(0, minY);
}
Aggregations