use of org.eclipse.elk.graph.ElkEdgeSection in project elk by eclipse.
the class GmfLayoutEditPolicy method getBendPoints.
/**
* Transform the bend points of the given edge layout into a point list, reusing existing ones
* if possible. The source and target points of the edge layout are included in the point list.
*
* @param edge
* the edge for which to fetch bend points
* @param isSplineEdge
* indicates whether the connection supports splines
* @return point list with the bend points of the edge layout
* @param scale
* scale factor for coordinates
*/
private PointList getBendPoints(final ElkEdge edge, final IFigure edgeFigure, final double scale) {
// This assumes that the edge has at least one edge section, which by this point it should
ElkEdgeSection edgeSection = edge.getSections().get(0);
PointList pointList = pointListMap.get(edgeSection);
if (pointList == null) {
KVectorChain bendPoints = ElkUtil.createVectorChain(edgeSection);
// for connections that support splines the control points are passed without change
boolean approx = handleSplineConnection(edgeFigure, edge.getProperty(CoreOptions.EDGE_ROUTING));
// in other cases an approximation is used
if (approx && bendPoints.size() >= 1) {
bendPoints = ElkMath.approximateBezierSpline(bendPoints);
}
bendPoints.scale(scale);
pointList = new PointList(bendPoints.size() + 2);
for (KVector bendPoint : bendPoints) {
pointList.addPoint((int) bendPoint.x, (int) bendPoint.y);
}
pointListMap.put(edgeSection, pointList);
}
return pointList;
}
use of org.eclipse.elk.graph.ElkEdgeSection in project elk by eclipse.
the class GmfLayoutEditPolicy method getRelativeSourcePoint.
/**
* Create a vector that contains the relative position of the source point to the corresponding source node or
* port.
*
* @param edge
* an edge
* @return the relative source point
*/
private KVector getRelativeSourcePoint(final ElkEdge edge) {
// The edge should have exactly one source shape
ElkConnectableShape sourceShape = edge.getSources().get(0);
// The edge should have one edge section after layout
ElkEdgeSection edgeSection = edge.getSections().get(0);
KVector sourcePoint = new KVector(edgeSection.getStartX(), edgeSection.getStartY());
// We will now make the source point absolute, and then relative to the source node
ElkUtil.toAbsolute(sourcePoint, edge.getContainingNode());
ElkUtil.toRelative(sourcePoint, ElkGraphUtil.connectableShapeToNode(sourceShape));
// 1 being at the right / bottom
if (sourceShape instanceof ElkPort) {
ElkPort sourcePort = (ElkPort) sourceShape;
// calculate the relative position to the port size
if (sourcePort.getWidth() <= 0) {
sourcePoint.x = 0;
} else {
sourcePoint.x = (sourcePoint.x - sourcePort.getX()) / sourcePort.getWidth();
}
if (sourcePort.getHeight() <= 0) {
sourcePoint.y = 0;
} else {
sourcePoint.y = (sourcePoint.y - sourcePort.getY()) / sourcePort.getHeight();
}
} else {
// calculate the relative position to the node size
if (sourceShape.getWidth() <= 0) {
sourcePoint.x = 0;
} else {
sourcePoint.x /= sourceShape.getWidth();
}
if (sourceShape.getHeight() <= 0) {
sourcePoint.y = 0;
} else {
sourcePoint.y /= sourceShape.getHeight();
}
}
// check the bound of the relative position
return sourcePoint.bound(0, 0, 1, 1);
}
use of org.eclipse.elk.graph.ElkEdgeSection in project elk by eclipse.
the class GmfLayoutEditPolicy method getRelativeTargetPoint.
/**
* Create a vector that contains the relative position of the target point to the corresponding
* target node or port.
*
* @param edge
* an edge
* @return the relative target point
*/
private KVector getRelativeTargetPoint(final ElkEdge edge) {
// The edge should have exactly one source shape
ElkConnectableShape targetShape = edge.getTargets().get(0);
// The edge should have one edge section after layout
ElkEdgeSection edgeSection = edge.getSections().get(0);
KVector targetPoint = new KVector(edgeSection.getEndX(), edgeSection.getEndY());
// We will now make the source point absolute, and then relative to the source node
ElkUtil.toAbsolute(targetPoint, edge.getContainingNode());
ElkUtil.toRelative(targetPoint, ElkGraphUtil.connectableShapeToNode(targetShape));
// 1 being at the right / bottom
if (targetShape instanceof ElkPort) {
ElkPort targetPort = (ElkPort) targetShape;
// calculate the relative position to the port size
if (targetPort.getWidth() <= 0) {
targetPoint.x = 0;
} else {
targetPoint.x = (targetPoint.x - targetPort.getX()) / targetPort.getWidth();
}
if (targetPort.getHeight() <= 0) {
targetPoint.y = 0;
} else {
targetPoint.y = (targetPoint.y - targetPort.getY()) / targetPort.getHeight();
}
} else {
// calculate the relative position to the node size
if (targetShape.getWidth() <= 0) {
targetPoint.x = 0;
} else {
targetPoint.x /= targetShape.getWidth();
}
if (targetShape.getHeight() <= 0) {
targetPoint.y = 0;
} else {
targetPoint.y /= targetShape.getHeight();
}
}
// check the bound of the relative position
return targetPoint.bound(0, 0, 1, 1);
}
use of org.eclipse.elk.graph.ElkEdgeSection in project elk by eclipse.
the class Draw2DLayoutProvider method applyLayout.
/**
* Applies the layout determined by Draw2D to the original graph.
*
* @param parentNode the parent layout node
* @param graph the Draw2D graph
*/
private void applyLayout(final ElkNode parentNode, final DirectedGraph graph) {
// MIGRATE The original code does not apply parent node insets to the child node coordinates, but it should?
for (int i = 0; i < graph.nodes.size(); i++) {
Node node = graph.nodes.getNode(i);
if (node.data instanceof ElkNode) {
ElkNode knode = (ElkNode) node.data;
knode.setLocation(node.x, node.y);
}
}
// apply edge layouts
for (int i = 0; i < graph.edges.size(); i++) {
Edge edge = graph.edges.getEdge(i);
if (edge.data instanceof ElkEdge) {
ElkEdge kedge = (ElkEdge) edge.data;
ElkEdgeSection edgeSection = ElkGraphUtil.firstEdgeSection(kedge, true, true);
PointList pointList = edge.getPoints();
// Start point
Point startPoint = pointList.getFirstPoint();
edgeSection.setStartLocation(startPoint.x, startPoint.y);
// Bend points
for (int j = 1; j < pointList.size() - 1; j++) {
Point point = pointList.getPoint(j);
ElkGraphUtil.createBendPoint(edgeSection, point.x, point.y);
}
// End point
Point endPoint = pointList.getFirstPoint();
edgeSection.setStartLocation(endPoint.x, endPoint.y);
}
}
// apply parent node layout
ElkPadding insets = parentNode.getProperty(Draw2DOptions.PADDING);
Dimension layoutSize = graph.getLayoutSize();
double width = insets.getLeft() + layoutSize.width + insets.getRight();
double height = insets.getTop() + layoutSize.height + insets.getBottom();
ElkUtil.resizeNode(parentNode, width, height, false, true);
}
use of org.eclipse.elk.graph.ElkEdgeSection in project elk by eclipse.
the class ElkGraphImporter method applyPositions.
@Override
public void applyPositions(final Graph g) {
// set new node positions
double minX = Double.POSITIVE_INFINITY;
double minY = Double.POSITIVE_INFINITY;
double maxX = Double.NEGATIVE_INFINITY;
double maxY = Double.NEGATIVE_INFINITY;
for (Node node : g.vertices) {
ElkNode elkNode = nodeMap.get(node.originalVertex).getSecond();
elkNode.setLocation(node.rect.x, node.rect.y);
minX = Math.min(minX, elkNode.getX());
minY = Math.min(minY, elkNode.getY());
maxX = Math.max(maxX, elkNode.getX() + elkNode.getWidth());
maxY = Math.max(maxY, elkNode.getY() + elkNode.getHeight());
}
// set new dimensions of parent node
ElkPadding padding = elkGraph.getProperty(SporeCompactionOptions.PADDING);
ElkUtil.resizeNode(elkGraph, maxX - minX + padding.getHorizontal(), maxY - minY + padding.getVertical(), true, true);
ElkUtil.translate(elkGraph, -minX + padding.left, -minY + padding.top);
// update edges and route them as straight lines
for (ElkEdge e : elkGraph.getContainedEdges()) {
ElkEdgeSection kedgeSection = ElkGraphUtil.firstEdgeSection(e, true, true);
ElkNode source = ElkGraphUtil.getSourceNode(e);
ElkNode target = ElkGraphUtil.getTargetNode(e);
KVector startLocation = new KVector(source.getX() + source.getWidth() / 2, source.getY() + source.getHeight() / 2);
KVector endLocation = new KVector(target.getX() + target.getWidth() / 2, target.getY() + target.getHeight() / 2);
KVector uv = endLocation.clone().sub(startLocation);
ElkMath.clipVector(uv, source.getWidth(), source.getHeight());
startLocation.add(uv);
KVector vu = startLocation.clone().sub(endLocation);
ElkMath.clipVector(vu, target.getWidth(), target.getHeight());
endLocation.add(vu);
kedgeSection.setStartLocation(startLocation.x, startLocation.y);
kedgeSection.setEndLocation(endLocation.x, endLocation.y);
}
}
Aggregations