Search in sources :

Example 26 with Direction

use of org.eclipse.elk.core.options.Direction in project elk by eclipse.

the class OneDimensionalCompactor method changeDirection.

/**
 * Changes the direction for compaction by transforming the hitboxes.
 *
 * @param dir
 *          the new direction
 * @return
 *          this instance of {@link OneDimensionalCompactor}
 */
public OneDimensionalCompactor changeDirection(final Direction dir) {
    if (finished) {
        throw new IllegalStateException("The " + getClass().getSimpleName() + " instance has been finished already.");
    }
    // segments would interchange
    if (!cGraph.supports(dir)) {
        throw new RuntimeException("The direction " + dir + " is not supported by the CGraph instance.");
    }
    // short-circuit if nothing todo
    if (dir == direction) {
        return this;
    }
    Direction oldDirection = direction;
    direction = dir;
    // being recalculated and CNodes are locked
    switch(oldDirection) {
        case UNDEFINED:
            switch(dir) {
                case LEFT:
                    calculateConstraints();
                    break;
                case RIGHT:
                    mirrorHitboxes();
                    calculateConstraints();
                    break;
                case UP:
                    transposeHitboxes();
                    calculateConstraints();
                    break;
                case DOWN:
                    transposeHitboxes();
                    mirrorHitboxes();
                    calculateConstraints();
                    break;
                default:
                    break;
            }
            break;
        case LEFT:
            switch(dir) {
                case RIGHT:
                    mirrorHitboxes();
                    reverseConstraints();
                    break;
                case UP:
                    transposeHitboxes();
                    calculateConstraints();
                    break;
                case DOWN:
                    transposeHitboxes();
                    mirrorHitboxes();
                    calculateConstraints();
                    break;
                default:
                    break;
            }
            break;
        case RIGHT:
            switch(dir) {
                case LEFT:
                    mirrorHitboxes();
                    reverseConstraints();
                    break;
                case UP:
                    mirrorHitboxes();
                    transposeHitboxes();
                    calculateConstraints();
                    break;
                case DOWN:
                    mirrorHitboxes();
                    transposeHitboxes();
                    mirrorHitboxes();
                    calculateConstraints();
                    break;
                default:
                    break;
            }
            break;
        case UP:
            switch(dir) {
                case LEFT:
                    transposeHitboxes();
                    calculateConstraints();
                    break;
                case RIGHT:
                    transposeHitboxes();
                    mirrorHitboxes();
                    calculateConstraints();
                    break;
                case DOWN:
                    mirrorHitboxes();
                    reverseConstraints();
                    break;
                default:
                    break;
            }
            break;
        case DOWN:
            switch(dir) {
                case LEFT:
                    mirrorHitboxes();
                    transposeHitboxes();
                    calculateConstraints();
                    break;
                case RIGHT:
                    mirrorHitboxes();
                    transposeHitboxes();
                    mirrorHitboxes();
                    calculateConstraints();
                    break;
                case UP:
                    mirrorHitboxes();
                    reverseConstraints();
                    break;
                default:
                    break;
            }
            break;
        default:
            break;
    }
    return this;
}
Also used : Direction(org.eclipse.elk.core.options.Direction)

Example 27 with Direction

use of org.eclipse.elk.core.options.Direction in project elk by eclipse.

the class OneDimensionalCompactorTest method testSubsequentDirectionsCompaction.

/* --------------------------------------------------
     * Testing subsequent calls with different directions
     * -------------------------------------------------- */
@Test
public void testSubsequentDirectionsCompaction() {
    CGraph graph = new CGraph(EnumSet.allOf(Direction.class));
    CNode one = CNode.of().hitbox(new ElkRectangle(0, 0, 20, 20)).create(graph);
    CNode two = CNode.of().hitbox(new ElkRectangle(25, 0, 20, 20)).create(graph);
    CNode three = CNode.of().hitbox(new ElkRectangle(0, 25, 20, 20)).create(graph);
    CNode four = CNode.of().hitbox(new ElkRectangle(25, 25, 20, 20)).create(graph);
    Set<Direction> directions = EnumSet.of(Direction.LEFT, Direction.RIGHT, Direction.UP, Direction.DOWN);
    // subsequently apply all combinations of four subsequent compaction steps
    for (Direction d1 : directions) {
        for (Direction d2 : directions) {
            for (Direction d3 : directions) {
                for (Direction d4 : directions) {
                    compacter(graph).setSpacingsHandler(TEST_SPACING_HANDLER).changeDirection(d1).compact().changeDirection(d2).compact().changeDirection(d3).compact().changeDirection(d4).compact().finish();
                    // the way we modeled the graph, every node should stay where it is
                    String currentDirections = d1 + " " + d2 + " " + d3 + " " + d4;
                    assertEquals(currentDirections, 0, one.hitbox.x, EPSILON);
                    assertEquals(currentDirections, 0, one.hitbox.y, EPSILON);
                    assertEquals(currentDirections, 25, two.hitbox.x, EPSILON);
                    assertEquals(currentDirections, 0, two.hitbox.y, EPSILON);
                    assertEquals(currentDirections, 0, three.hitbox.x, EPSILON);
                    assertEquals(currentDirections, 25, three.hitbox.y, EPSILON);
                    assertEquals(currentDirections, 25, four.hitbox.x, EPSILON);
                    assertEquals(currentDirections, 25, four.hitbox.y, EPSILON);
                }
            }
        }
    }
}
Also used : ElkRectangle(org.eclipse.elk.core.math.ElkRectangle) Direction(org.eclipse.elk.core.options.Direction) Test(org.junit.Test)

Example 28 with Direction

use of org.eclipse.elk.core.options.Direction in project elk by eclipse.

the class LGraphToCGraphTransformer method init.

private void init() {
    // checking if the graph has edges and possibly prohibiting vertical compaction
    boolean hasEdges = false;
    int index = 0;
    for (Layer l : layeredGraph) {
        l.id = index++;
        for (LNode n : l) {
            // avoid calling Iterables.isEmpty unnecessarily
            if (!hasEdges && !Iterables.isEmpty(n.getConnectedEdges())) {
                hasEdges = true;
            }
        }
    }
    EnumSet<Direction> supportedDirections = EnumSet.of(Direction.UNDEFINED, Direction.LEFT, Direction.RIGHT);
    if (!hasEdges) {
        supportedDirections.add(Direction.UP);
        supportedDirections.add(Direction.DOWN);
    }
    // initializing members
    cGraph = new CGraph(supportedDirections);
    nodesMap.clear();
    commentOffsets.clear();
    lockMap.clear();
    verticalSegmentsMap.clear();
}
Also used : CGraph(org.eclipse.elk.alg.common.compaction.oned.CGraph) LNode(org.eclipse.elk.alg.layered.graph.LNode) Layer(org.eclipse.elk.alg.layered.graph.Layer) Direction(org.eclipse.elk.core.options.Direction)

Example 29 with Direction

use of org.eclipse.elk.core.options.Direction in project elk by eclipse.

the class ElkUtil method resizeNode.

/**
 * Sets the size of a given node, depending on the minimal size, the number of ports
 * on each side and the label.
 *
 * @param node the node that shall be resized
 * @return a vector holding the width and height resizing ratio, or {@code null} if the size
 *     constraint is set to {@code FIXED}
 */
public static KVector resizeNode(final ElkNode node) {
    Set<SizeConstraint> sizeConstraint = node.getProperty(CoreOptions.NODE_SIZE_CONSTRAINTS);
    if (sizeConstraint.isEmpty()) {
        return null;
    }
    double newWidth = 0, newHeight = 0;
    if (sizeConstraint.contains(SizeConstraint.PORTS)) {
        PortConstraints portConstraints = node.getProperty(CoreOptions.PORT_CONSTRAINTS);
        double minNorth = 2, minEast = 2, minSouth = 2, minWest = 2;
        Direction direction = node.getParent() == null ? node.getProperty(CoreOptions.DIRECTION) : node.getParent().getProperty(CoreOptions.DIRECTION);
        for (ElkPort port : node.getPorts()) {
            PortSide portSide = port.getProperty(CoreOptions.PORT_SIDE);
            if (portSide == PortSide.UNDEFINED) {
                portSide = calcPortSide(port, direction);
                port.setProperty(CoreOptions.PORT_SIDE, portSide);
            }
            if (portConstraints == PortConstraints.FIXED_POS) {
                switch(portSide) {
                    case NORTH:
                        minNorth = Math.max(minNorth, port.getX() + port.getWidth());
                        break;
                    case EAST:
                        minEast = Math.max(minEast, port.getY() + port.getHeight());
                        break;
                    case SOUTH:
                        minSouth = Math.max(minSouth, port.getX() + port.getWidth());
                        break;
                    case WEST:
                        minWest = Math.max(minWest, port.getY() + port.getHeight());
                        break;
                }
            } else {
                switch(portSide) {
                    case NORTH:
                        minNorth += port.getWidth() + 2;
                        break;
                    case EAST:
                        minEast += port.getHeight() + 2;
                        break;
                    case SOUTH:
                        minSouth += port.getWidth() + 2;
                        break;
                    case WEST:
                        minWest += port.getHeight() + 2;
                        break;
                }
            }
        }
        newWidth = Math.max(minNorth, minSouth);
        newHeight = Math.max(minEast, minWest);
    }
    return resizeNode(node, newWidth, newHeight, true, true);
}
Also used : ElkPort(org.eclipse.elk.graph.ElkPort) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) PortSide(org.eclipse.elk.core.options.PortSide) PortConstraints(org.eclipse.elk.core.options.PortConstraints) Direction(org.eclipse.elk.core.options.Direction)

Example 30 with Direction

use of org.eclipse.elk.core.options.Direction in project elk by eclipse.

the class DotExporter method transformEdges.

/**
 * Transform the edges of the given parent node.
 *
 * @param parent a parent node
 * @param statements the list to which new statements are added
 * @param transData transformation data
 */
private void transformEdges(final ElkNode parent, final List<Statement> statements, final IDotTransformationData<ElkNode, GraphvizModel> transData) {
    boolean hierarchy = transData.getProperty(HIERARCHY);
    boolean transformEdgeLayout = transData.getProperty(TRANSFORM_EDGE_LAYOUT);
    Direction direction = parent.getProperty(CoreOptions.DIRECTION);
    boolean vertical = direction == Direction.DOWN || direction == Direction.UP || direction == Direction.UNDEFINED;
    LinkedList<ElkNode> nodes = new LinkedList<>(parent.getChildren());
    BiMap<ElkGraphElement, String> nodeIds = transData.getProperty(GRAPH_ELEMS).inverse();
    while (!nodes.isEmpty()) {
        ElkNode source = nodes.removeFirst();
        for (ElkEdge edge : ElkGraphUtil.allOutgoingEdges(source)) {
            // We don't support hyperedges
            if (edge.isHyperedge()) {
                throw new UnsupportedGraphException("Hyperedges are not supported.");
            }
            ElkNode target = ElkGraphUtil.connectableShapeToNode(edge.getTargets().get(0));
            // cross-hierarchy edges are considered only if hierarchy mode is active
            if (source.getParent() == target.getParent() || hierarchy && isInsideGraph(target, transData.getSourceGraph())) {
                EdgeStatement edgeStatement = DotFactory.eINSTANCE.createEdgeStatement();
                List<Attribute> attributes = edgeStatement.getAttributes();
                // set source node or cluster
                Node sourceNode = DotFactory.eINSTANCE.createNode();
                if (hierarchy && !source.getChildren().isEmpty()) {
                    sourceNode.setName(source.getProperty(CLUSTER_DUMMY));
                    attributes.add(createAttribute(Attributes.LTAIL, nodeIds.get(source)));
                } else {
                    sourceNode.setName(nodeIds.get(source));
                }
                edgeStatement.setSourceNode(sourceNode);
                // set target node or cluster
                EdgeTarget edgeTarget = DotFactory.eINSTANCE.createEdgeTarget();
                Node targetNode = DotFactory.eINSTANCE.createNode();
                if (hierarchy && !target.getChildren().isEmpty()) {
                    targetNode.setName(target.getProperty(CLUSTER_DUMMY));
                    attributes.add(createAttribute(Attributes.LHEAD, nodeIds.get(target)));
                } else {
                    targetNode.setName(nodeIds.get(target));
                }
                edgeTarget.setTargetnode(targetNode);
                edgeStatement.getEdgeTargets().add(edgeTarget);
                // add edge labels at head, tail, and middle position
                setEdgeLabels(edge, attributes, vertical);
                if (transData.getProperty(USE_EDGE_IDS)) {
                    // add comment with edge identifier
                    String edgeID = getEdgeID(edge, transData);
                    attributes.add(createAttribute(Attributes.COMMENT, "\"" + edgeID + "\""));
                }
                // include edge routing for full export, if there is one
                if (!edge.getSections().isEmpty()) {
                    ElkEdgeSection edgeSection = edge.getSections().get(0);
                    if (transformEdgeLayout && (edgeSection.getBendPoints().size() > 0 || edgeSection.getStartX() != 0 || edgeSection.getStartY() != 0 || edgeSection.getEndX() != 0 || edgeSection.getEndY() != 0)) {
                        StringBuilder pos = new StringBuilder();
                        Iterator<KVector> pointIter = ElkUtil.createVectorChain(edgeSection).iterator();
                        while (pointIter.hasNext()) {
                            KVector point = pointIter.next();
                            ElkUtil.toAbsolute(point, edge.getContainingNode());
                            pos.append(point.x);
                            pos.append(",");
                            pos.append(point.y);
                            if (pointIter.hasNext()) {
                                pos.append(" ");
                            }
                        }
                        attributes.add(createAttribute(Attributes.POS, "\"" + pos + "\""));
                    }
                }
                statements.add(edgeStatement);
            }
        }
        if (hierarchy) {
            nodes.addAll(source.getChildren());
        }
    }
}
Also used : UnsupportedGraphException(org.eclipse.elk.core.UnsupportedGraphException) ElkNode(org.eclipse.elk.graph.ElkNode) Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute) ElkNode(org.eclipse.elk.graph.ElkNode) Node(org.eclipse.elk.alg.graphviz.dot.dot.Node) Direction(org.eclipse.elk.core.options.Direction) LinkedList(java.util.LinkedList) EdgeStatement(org.eclipse.elk.alg.graphviz.dot.dot.EdgeStatement) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) EdgeTarget(org.eclipse.elk.alg.graphviz.dot.dot.EdgeTarget) KVector(org.eclipse.elk.core.math.KVector) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Aggregations

Direction (org.eclipse.elk.core.options.Direction)30 LNode (org.eclipse.elk.alg.layered.graph.LNode)9 KVector (org.eclipse.elk.core.math.KVector)7 PortSide (org.eclipse.elk.core.options.PortSide)7 LEdge (org.eclipse.elk.alg.layered.graph.LEdge)6 ElkRectangle (org.eclipse.elk.core.math.ElkRectangle)5 PortConstraints (org.eclipse.elk.core.options.PortConstraints)5 ElkNode (org.eclipse.elk.graph.ElkNode)5 CGroup (org.eclipse.elk.alg.layered.compaction.oned.CGroup)4 LGraph (org.eclipse.elk.alg.layered.graph.LGraph)4 GraphProperties (org.eclipse.elk.alg.layered.options.GraphProperties)4 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)4 ElkEdge (org.eclipse.elk.graph.ElkEdge)4 ElkPort (org.eclipse.elk.graph.ElkPort)4 Test (org.junit.Test)4 CGraph (org.eclipse.elk.alg.layered.compaction.oned.CGraph)3 CNode (org.eclipse.elk.alg.layered.compaction.oned.CNode)3 LPort (org.eclipse.elk.alg.layered.graph.LPort)3 ElkLabel (org.eclipse.elk.graph.ElkLabel)3 LLabel (org.eclipse.elk.alg.layered.graph.LLabel)2