use of org.eclipse.elk.graph.ElkBendPoint in project elk by eclipse.
the class RandomLayoutProvider method randomize.
/**
* Randomize the given edge by adding bend points in the area between the source and target node.
*
* @param edge an edge
* @param source the source node
* @param target the target node
* @param random the random number generator
* @param drawWidth the total width of the drawing
* @param drawHeight the total Height of the drawing
*/
private void randomize(final ElkEdge edge, final Random random, final double drawWidth, final double drawHeight) {
// Determine source coordinate
ElkConnectableShape sourceShape = edge.getSources().get(0);
double sourceX = sourceShape.getX();
double sourceY = sourceShape.getY();
double sourceWidth = sourceShape.getWidth() / 2;
double sourceHeight = sourceShape.getHeight() / 2;
if (sourceShape instanceof ElkPort) {
ElkPort sourcePort = (ElkPort) sourceShape;
sourceX += sourcePort.getParent().getX();
sourceX += sourcePort.getParent().getX();
}
sourceX += sourceWidth;
sourceY += sourceHeight;
// Determine target coordinate
ElkConnectableShape targetShape = edge.getSources().get(0);
double targetX = targetShape.getX();
double targetY = targetShape.getY();
double targetWidth = targetShape.getWidth() / 2;
double targetHeight = targetShape.getHeight() / 2;
if (targetShape instanceof ElkPort) {
ElkPort targetPort = (ElkPort) targetShape;
targetX += targetPort.getParent().getX();
targetX += targetPort.getParent().getX();
}
targetX += targetWidth;
targetY += targetHeight;
// Ensure that the edge has a single edge section
if (edge.getSections().isEmpty()) {
// We need an edge section to apply the bend points to
ElkEdgeSection edgeSection = ElkGraphFactory.eINSTANCE.createElkEdgeSection();
edge.getSections().add(edgeSection);
} else if (edge.getSections().size() > 1) {
// We can only apply bend points to a single edge section, so throw away all except for the last one
ListIterator<ElkEdgeSection> sections = edge.getSections().listIterator();
while (sections.hasNext()) {
sections.remove();
}
}
ElkEdgeSection edgeSection = edge.getSections().get(0);
// set the source point onto the border of the source element
double sourcePX = targetX;
if (targetX > sourceX + sourceWidth) {
sourcePX = sourceX + sourceWidth;
} else if (targetX < sourceX - sourceWidth) {
sourcePX = sourceX - sourceWidth;
}
double sourcePY = targetY;
if (targetY > sourceY + sourceHeight) {
sourcePY = sourceY + sourceHeight;
} else if (targetY < sourceY - sourceHeight) {
sourcePY = sourceY - sourceHeight;
}
if (sourcePX > sourceX - sourceWidth && sourcePX < sourceX + sourceWidth && sourcePY > sourceY - sourceHeight && sourcePY < sourceY + sourceHeight) {
sourcePX = sourceX + sourceWidth;
}
edgeSection.setStartLocation(sourcePX, sourcePY);
// set the target point onto the border of the target element
double targetPX = sourceX;
if (sourceX > targetX + targetWidth) {
targetPX = targetX + targetWidth;
} else if (sourceX < targetX - targetWidth) {
targetPX = targetX - targetWidth;
}
double targetPY = sourceY;
if (sourceY > targetY + targetHeight) {
targetPY = targetY + targetHeight;
} else if (sourceY < targetY - targetHeight) {
targetPY = targetY - targetHeight;
}
if (targetPX > targetX - targetWidth && targetPX < targetX + targetWidth && targetPY > targetY - targetHeight && targetPY < targetY + targetHeight) {
targetPY = targetY + targetHeight;
}
edgeSection.setEndLocation(targetPX, targetPY);
// add a random number of bend points
edgeSection.getBendPoints().clear();
int bendsNum = random.nextInt(MAX_BENDS);
if (sourceShape == targetShape) {
bendsNum++;
}
double xdiff = targetPX - sourcePX;
double ydiff = targetPY - sourcePY;
double totalDist = (double) Math.sqrt(xdiff * xdiff + ydiff * ydiff);
double maxRand = totalDist * RAND_FACT;
double xincr = xdiff / (bendsNum + 1);
double yincr = ydiff / (bendsNum + 1);
double x = sourcePX, y = sourcePY;
for (int i = 0; i < bendsNum; i++) {
// determine coordinates that deviate from the straight connection by a random amount
x += xincr;
y += yincr;
double randx = x + random.nextFloat() * maxRand - maxRand / 2;
if (randx < 0) {
randx = 1;
} else if (randx > drawWidth) {
randx = drawWidth - 1;
}
double randy = y + random.nextFloat() * maxRand - maxRand / 2;
if (randy < 0) {
randy = 1;
} else if (randy > drawHeight) {
randy = drawHeight - 1;
}
ElkBendPoint bendPoint = ElkGraphFactory.eINSTANCE.createElkBendPoint();
bendPoint.set(randx, randy);
edgeSection.getBendPoints().add(bendPoint);
}
}
use of org.eclipse.elk.graph.ElkBendPoint in project elk by eclipse.
the class AbstractElkGraphSemanticSequencer method sequence.
@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
EPackage epackage = semanticObject.eClass().getEPackage();
ParserRule rule = context.getParserRule();
Action action = context.getAssignedAction();
Set<Parameter> parameters = context.getEnabledBooleanParameters();
if (epackage == ElkGraphPackage.eINSTANCE)
switch(semanticObject.eClass().getClassifierID()) {
case ElkGraphPackage.ELK_BEND_POINT:
sequence_ElkBendPoint(context, (ElkBendPoint) semanticObject);
return;
case ElkGraphPackage.ELK_EDGE:
sequence_EdgeLayout_ElkEdge(context, (ElkEdge) semanticObject);
return;
case ElkGraphPackage.ELK_EDGE_SECTION:
if (rule == grammarAccess.getElkEdgeSectionRule()) {
sequence_ElkEdgeSection(context, (ElkEdgeSection) semanticObject);
return;
} else if (rule == grammarAccess.getElkSingleEdgeSectionRule()) {
sequence_ElkSingleEdgeSection(context, (ElkEdgeSection) semanticObject);
return;
} else
break;
case ElkGraphPackage.ELK_LABEL:
sequence_ElkLabel_ShapeLayout(context, (ElkLabel) semanticObject);
return;
case ElkGraphPackage.ELK_NODE:
if (rule == grammarAccess.getElkNodeRule()) {
sequence_ElkNode_ShapeLayout(context, (ElkNode) semanticObject);
return;
} else if (rule == grammarAccess.getRootNodeRule()) {
sequence_RootNode_ShapeLayout(context, (ElkNode) semanticObject);
return;
} else
break;
case ElkGraphPackage.ELK_PORT:
sequence_ElkPort_ShapeLayout(context, (ElkPort) semanticObject);
return;
case ElkGraphPackage.ELK_PROPERTY_TO_VALUE_MAP_ENTRY:
sequence_Property(context, (Map.Entry) semanticObject);
return;
}
if (errorAcceptor != null)
errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
use of org.eclipse.elk.graph.ElkBendPoint in project elk by eclipse.
the class ElkGraphUtil method createBendPoint.
/**
* Creates a new bend point with the given coordinates and adds it to the given edge section.
*
* @param edgeSection the edge section to add the bend point to. May be {@code null}, in which case the new
* bend point is not added to anything.
* @param x the bend point's x coordinate.
* @param y the bend point's y coordinate.
* @return the new bend point.
*/
public static ElkBendPoint createBendPoint(final ElkEdgeSection edgeSection, final double x, final double y) {
ElkBendPoint bendPoint = ElkGraphFactory.eINSTANCE.createElkBendPoint();
bendPoint.set(x, y);
if (edgeSection != null) {
edgeSection.getBendPoints().add(bendPoint);
}
return bendPoint;
}
use of org.eclipse.elk.graph.ElkBendPoint in project elk by eclipse.
the class ElkUtil method translate.
/**
* Translates the given edge section by an offset.
*
* @param section edge section that shall be translated
* @param xoffset x coordinate offset
* @param yoffset y coordinate offset
*/
public static void translate(final ElkEdgeSection section, final double xoffset, final double yoffset) {
// Translate source point
section.setStartLocation(section.getStartX() + xoffset, section.getStartY() + yoffset);
// Translate bend points
for (ElkBendPoint bendPoint : section.getBendPoints()) {
bendPoint.set(bendPoint.getX() + xoffset, bendPoint.getY() + yoffset);
}
// Translate target point
section.setEndLocation(section.getEndX() + xoffset, section.getEndY() + yoffset);
}
use of org.eclipse.elk.graph.ElkBendPoint in project elk by eclipse.
the class ElkUtil method createVectorChain.
/**
* Creates a vector chain containing the start point, bend points, and end point of the given edge section. Note
* that modifying the vector chain will be of no consequence to the edge section.
*
* @param edgeSection
* the edge section to initialize the vector chain with.
* @return the vector chain.
*/
public static KVectorChain createVectorChain(final ElkEdgeSection edgeSection) {
KVectorChain chain = new KVectorChain();
chain.add(new KVector(edgeSection.getStartX(), edgeSection.getStartY()));
for (ElkBendPoint bendPoint : edgeSection.getBendPoints()) {
chain.add(new KVector(bendPoint.getX(), bendPoint.getY()));
}
chain.add(new KVector(edgeSection.getEndX(), edgeSection.getEndY()));
return chain;
}
Aggregations