Search in sources :

Example 36 with KVector

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;
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) KVector(org.eclipse.elk.core.math.KVector) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 37 with KVector

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();
}
Also used : ICostFunction(org.eclipse.elk.alg.common.ICostFunction) ElkUtil(org.eclipse.elk.core.util.ElkUtil) NaiveMinST(org.eclipse.elk.alg.common.NaiveMinST) KVector(org.eclipse.elk.core.math.KVector) IElkProgressMonitor(org.eclipse.elk.core.util.IElkProgressMonitor) ICostFunction(org.eclipse.elk.alg.common.ICostFunction) Tree(org.eclipse.elk.alg.common.Tree) Graph(org.eclipse.elk.alg.spore.graph.Graph) InternalProperties(org.eclipse.elk.alg.common.spore.InternalProperties) KVector(org.eclipse.elk.core.math.KVector)

Example 38 with KVector

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");
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) KVector(org.eclipse.elk.core.math.KVector) ElkPadding(org.eclipse.elk.core.math.ElkPadding) ElkMargin(org.eclipse.elk.core.math.ElkMargin)

Example 39 with KVector

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);
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkPort(org.eclipse.elk.graph.ElkPort) KVector(org.eclipse.elk.core.math.KVector) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 40 with KVector

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);
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkLabel(org.eclipse.elk.graph.ElkLabel) KVector(org.eclipse.elk.core.math.KVector) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Aggregations

KVector (org.eclipse.elk.core.math.KVector)292 KVectorChain (org.eclipse.elk.core.math.KVectorChain)52 ElkNode (org.eclipse.elk.graph.ElkNode)49 LNode (org.eclipse.elk.alg.layered.graph.LNode)39 LPort (org.eclipse.elk.alg.layered.graph.LPort)37 ElkRectangle (org.eclipse.elk.core.math.ElkRectangle)36 LEdge (org.eclipse.elk.alg.layered.graph.LEdge)35 Test (org.junit.Test)30 ElkEdge (org.eclipse.elk.graph.ElkEdge)28 ElkLabel (org.eclipse.elk.graph.ElkLabel)27 ElkEdgeSection (org.eclipse.elk.graph.ElkEdgeSection)26 ElkPadding (org.eclipse.elk.core.math.ElkPadding)25 LLabel (org.eclipse.elk.alg.layered.graph.LLabel)20 PortSide (org.eclipse.elk.core.options.PortSide)19 ElkPort (org.eclipse.elk.graph.ElkPort)18 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)17 LGraph (org.eclipse.elk.alg.layered.graph.LGraph)15 ArrayList (java.util.ArrayList)13 Layer (org.eclipse.elk.alg.layered.graph.Layer)12 LMargin (org.eclipse.elk.alg.layered.graph.LMargin)11