Search in sources :

Example 46 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class ElkLayered method resizeGraph.

// //////////////////////////////////////////////////////////////////////////////
// Graph Postprocessing (Size and External Ports)
/**
 * Sets the size of the given graph such that size constraints are adhered to.
 * Furthermore, the border spacing is added to the graph size and the graph offset.
 * Afterwards, the border spacing property is reset to 0.
 *
 * <p>Major parts of this method are adapted from
 * {@link ElkUtil#resizeNode(org.eclipse.elk.graph.ElkNode, double, double, boolean, boolean)}.</p>
 *
 * <p>Note: This method doesn't care about labels of compound nodes since those labels are not
 * attached to the graph.</p>
 *
 * @param lgraph the graph to resize.
 */
private void resizeGraph(final LGraph lgraph) {
    Set<SizeConstraint> sizeConstraint = lgraph.getProperty(LayeredOptions.NODE_SIZE_CONSTRAINTS);
    Set<SizeOptions> sizeOptions = lgraph.getProperty(LayeredOptions.NODE_SIZE_OPTIONS);
    KVector calculatedSize = lgraph.getActualSize();
    KVector adjustedSize = new KVector(calculatedSize);
    // calculate the new size
    if (sizeConstraint.contains(SizeConstraint.MINIMUM_SIZE)) {
        KVector minSize = lgraph.getProperty(LayeredOptions.NODE_SIZE_MINIMUM);
        // if minimum width or height are not set, maybe default to default values
        if (sizeOptions.contains(SizeOptions.DEFAULT_MINIMUM_SIZE)) {
            if (minSize.x <= 0) {
                minSize.x = ElkUtil.DEFAULT_MIN_WIDTH;
            }
            if (minSize.y <= 0) {
                minSize.y = ElkUtil.DEFAULT_MIN_HEIGHT;
            }
        }
        // apply new size including border spacing
        adjustedSize.x = Math.max(calculatedSize.x, minSize.x);
        adjustedSize.y = Math.max(calculatedSize.y, minSize.y);
    }
    resizeGraphNoReallyIMeanIt(lgraph, calculatedSize, adjustedSize);
}
Also used : SizeOptions(org.eclipse.elk.core.options.SizeOptions) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) KVector(org.eclipse.elk.core.math.KVector)

Example 47 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class InteractiveLayeredGraphVisitor method setCoordinatesOrthogonalToLayoutDirection.

/**
 * Sets the positions of the nodes in their layer.
 *
 * @param nodesOfLayer
 *            The list containing nodes that are in the same layer.
 * @param direction
 *            The layout direction.
 */
private void setCoordinatesOrthogonalToLayoutDirection(final List<ElkNode> nodesOfLayer, final int layerId, final Direction direction) {
    // Separate nodes with and without position constraints.
    List<ElkNode> nodesWithPositionConstraint = new ArrayList<ElkNode>();
    List<ElkNode> nodes = new ArrayList<ElkNode>();
    for (ElkNode node : nodesOfLayer) {
        if (node.getProperty(LayeredOptions.CROSSING_MINIMIZATION_POSITION_CHOICE_CONSTRAINT) != -1) {
            nodesWithPositionConstraint.add(node);
        } else {
            nodes.add(node);
        }
    }
    // Determine the order of the nodes.
    sortNodesInLayer(nodesWithPositionConstraint, nodes, direction);
    // Add the nodes with position constraint at the desired position in their layer.
    for (ElkNode node : nodesWithPositionConstraint) {
        int pos = node.getProperty(LayeredOptions.CROSSING_MINIMIZATION_POSITION_CHOICE_CONSTRAINT);
        if (pos < nodes.size()) {
            nodes.add(pos, node);
        } else {
            nodes.add(node);
        }
    }
    // Set the y/x positions according to the order of the nodes.
    switch(direction) {
        case UNDEFINED:
        case RIGHT:
        case LEFT:
            double yPos = nodes.get(0).getY();
            for (ElkNode node : nodes) {
                node.setProperty(LayeredOptions.POSITION, new KVector(node.getX(), yPos));
                yPos += node.getHeight() + PSEUDO_POSITION_SPACING;
            }
            break;
        case DOWN:
        case UP:
            double xPos = nodes.get(0).getX() + 2 * layerId * PSEUDO_POSITION_SPACING;
            for (ElkNode node : nodes) {
                node.setProperty(LayeredOptions.POSITION, new KVector(xPos, node.getY()));
                xPos += node.getWidth() + PSEUDO_POSITION_SPACING;
            }
            break;
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ArrayList(java.util.ArrayList) KVector(org.eclipse.elk.core.math.KVector) LayerConstraint(org.eclipse.elk.alg.layered.options.LayerConstraint)

Example 48 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class JsonDebugUtil method calculateGraphSize.

/**
 * Calculate the overall size of the given graph. Returns a {@link KVectorChain} with two
 * elements where the first one represents the minimal coordinates used by any node, edge or
 * port contained in the graph and the second one represents the maximal coordinates.
 *
 * @param lgraph
 *            the graph to calculate the size for.
 * @return a {@link KVectorChain} with two elements, the minimal and the maximal coordinates.
 */
private static KVectorChain calculateGraphSize(final LGraph lgraph) {
    KVector min = new KVector();
    KVector max = new KVector();
    for (LNode node : lgraph.getLayerlessNodes()) {
        calculateMinMaxPositions(min, max, node);
    }
    for (Layer layer : lgraph) {
        for (LNode node : layer) {
            calculateMinMaxPositions(min, max, node);
        }
    }
    max.x -= min.x;
    max.y -= min.y;
    return new KVectorChain(min, max);
}
Also used : KVectorChain(org.eclipse.elk.core.math.KVectorChain) LNode(org.eclipse.elk.alg.layered.graph.LNode) KVector(org.eclipse.elk.core.math.KVector) Layer(org.eclipse.elk.alg.layered.graph.Layer)

Example 49 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class JsonDebugUtil method calculateMinMaxPositions.

/**
 * Inspects the given node, its outgoing edges and its ports for minimal and maximal coordinates
 * and adjusts the given vectors {@code min} and {@code max} if necessary.
 *
 * @param min
 *            the minimal coordinates used by the graph.
 * @param max
 *            the maximal coordinates used by the graph.
 * @param node
 *            the current node to inspect.
 */
private static void calculateMinMaxPositions(final KVector min, final KVector max, final LNode node) {
    min.x = Math.min(min.x, node.getPosition().x - node.getMargin().left);
    max.x = Math.max(max.x, node.getPosition().x + node.getSize().x + node.getMargin().right);
    min.y = Math.min(min.y, node.getPosition().y - node.getMargin().top);
    max.y = Math.max(max.y, node.getPosition().y + node.getSize().y + node.getMargin().bottom);
    for (LPort port : node.getPorts()) {
        min.x = Math.min(min.x, node.getPosition().x + port.getPosition().x - port.getMargin().left);
        max.x = Math.max(max.x, node.getPosition().x + port.getPosition().x + port.getSize().x + port.getMargin().right);
        min.y = Math.min(min.y, node.getPosition().y + port.getPosition().y - port.getMargin().top);
        max.y = Math.max(max.y, node.getPosition().y + port.getPosition().y + port.getSize().y + port.getMargin().bottom);
    }
    for (LEdge edge : node.getOutgoingEdges()) {
        for (KVector bendpoint : edge.getBendPoints()) {
            min.x = Math.min(min.x, bendpoint.x);
            max.x = Math.max(max.x, bendpoint.x);
            min.y = Math.min(min.y, bendpoint.y);
            max.y = Math.max(max.y, bendpoint.y);
        }
    }
}
Also used : LEdge(org.eclipse.elk.alg.layered.graph.LEdge) LPort(org.eclipse.elk.alg.layered.graph.LPort) KVector(org.eclipse.elk.core.math.KVector)

Example 50 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class JsonDebugUtil method writeBendPoints.

/**
 * Writes the given bendpoints as JSON formatted string to the given writer.
 *
 * @param writer
 *            the writer to write to.
 * @param bendPoints
 *            the bendpoints to write.
 * @param indentation
 *            the indentation level to use.
 * @param offset
 *            the combined offset of the containing LGraph.
 */
private static void writeBendPoints(final StringWriter writer, final KVectorChain bendPoints, final int indentation, final KVector offset) {
    String indent0 = Strings.repeat(INDENT, indentation);
    String indent1 = Strings.repeat(INDENT, indentation + 1);
    writer.write(indent0 + "\"bendPoints\": [");
    Iterator<KVector> pointsIterator = bendPoints.iterator();
    while (pointsIterator.hasNext()) {
        KVector point = new KVector(pointsIterator.next()).add(offset);
        writer.write("\n" + indent1 + "{ \"x\": " + point.x + ", \"y\": " + point.y + "}");
        if (pointsIterator.hasNext()) {
            writer.write(",");
        }
    }
    writer.write("\n" + indent0 + "]");
}
Also used : KVector(org.eclipse.elk.core.math.KVector)

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