Skip to content

Commit

Permalink
improved routing algorithm (Issue #381)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoika committed Feb 18, 2025
1 parent 8b9f5d9 commit 4cacdc6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 46 deletions.
10 changes: 10 additions & 0 deletions open-bpmn.glsp-client/open-bpmn-glsp/src/bpmn-manhattan-router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {
Action,
Bounds,
findParentByFeature,
GLSPManhattanEdgeRouter,
GModelElement,
Expand Down Expand Up @@ -145,6 +146,15 @@ export class BPMNManhattanRouter extends GLSPManhattanEdgeRouter {
x: isHorizontalSegment ? completeRoute[routeIndex].x : completeRoute[routeIndex].x + horizontalOffset,
y: isHorizontalSegment ? completeRoute[routeIndex].y + verticalOffset : completeRoute[routeIndex].y,
};

// Collision-Check
// if the first routingPoint is inside the element, reset WayPointData!
if (Bounds.includes(element.bounds, completeRoute[routeIndex])) {
this.debug('Collision with nearest routing point detected - reset waypoint data!');
this.resetWayPointData();
edge.routingPoints = completeRoute;
return completeRoute;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
********************************************************************************/
import {
ActionDispatcher,
GModelElement,
GModelRoot,
Grid,
ISelectionListener,
ISnapper,
Point,
SelectAction,
TYPES,
filter,
Expand All @@ -34,33 +30,13 @@ import {
isPoolNode,
isTaskNode
} from '@open-bpmn/open-bpmn-model';
import { inject, injectable, optional } from 'inversify';
import { inject, injectable } from 'inversify';

/****************************************************************************
* This module provides BPMN select listeners for custom behavior.
*
****************************************************************************/

/**
* A extension that snaps BPMN elements onto a fixed gride size.
* This snapper calculates the grid size based on the selected element to align tasks, gateways and events.
*
* See also discussion here: https://github.com/eclipse-glsp/glsp/discussions/1255
*/
@injectable()
export class BPMNElementSnapper implements ISnapper {

constructor(@optional() @inject(TYPES.Grid) public readonly grid: Grid = { x: 1, y: 1 }) {}
snap(position: Point, element: GModelElement): Point {
// default move 1x1...
return {
// maybe the better snap behavior instead of round()...
x: Math.floor(position.x),
y: Math.floor(position.y)
};
}
}

/**
* This selectionListener selects additional associated BoundaryEvents and BPMNLabels.
* This allows to move both independent Nodes (TaskNode and BoundaryEvent, Event|Gateway and BPMNLabel)
Expand Down
5 changes: 2 additions & 3 deletions open-bpmn.glsp-client/open-bpmn-glsp/src/di.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ import {
import { BPMNRouterModule } from './bpmn-router-module';
import { BPMNEdgeView } from './bpmn-routing-views';
import {
BPMNElementSnapper,
BPMNMultiNodeSelectionListener,
BPMNSelectionHelper
} from './bpmn-select-listeners';
Expand All @@ -91,13 +90,13 @@ const bpmnDiagramModule = new ContainerModule((bind, unbind, isBound, rebind) =>
rebind(TYPES.LogLevel).toConstantValue(LogLevel.warn);

// beaks launch
bind(TYPES.ISnapper).to(BPMNElementSnapper);
bind<IHelperLineOptions>(TYPES.IHelperLineOptions).toConstantValue({
elementLines: [HelperLineType.Center, HelperLineType.Middle], // only show center and middle lines
viewportLines: [], // do not show alignment lines for viewport
alignmentElementFilter: element =>
isBPMNNode(element) && !isBoundaryEvent(element),
minimumMoveDelta: { x: 10, y: 10 }
minimumMoveDelta: { x: 10, y: 10 },
alignmentEpsilon: 0.5
});
// bind new SelectionListener for BPMNLabels and BoundaryEvents
bind(TYPES.ISelectionListener).to(BPMNSelectionHelper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ private void executeOperation(final ChangeRoutingPointsOperation operation) {
String id = routingPoint.getElementId();
List<GPoint> newGLSPRoutingPoints = routingPoint.getNewRoutingPoints();

// snap the new routing points to the grid 5x5
BPMNGridSnapper.snapPointsToGrid(newGLSPRoutingPoints);
// round routing points ...
BPMNGridSnapper.round(newGLSPRoutingPoints);

// update the GModel.
GEdge edge = (GEdge) modelState.getIndex().get(id).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,6 @@ public static GPoint snap(final BPMNElementNode elementNode, final GPoint point)
return GraphUtil.point(x, y);
}

/**
* Helper method to snap routing points to the grid
*/
public static void snapPointsToGrid(List<GPoint> points) {
int gridSize = 5;
for (GPoint point : points) {
// Calculate the nearest grid coordinates
long snappedX = Math.round(point.getX() / (float) gridSize) * gridSize;
long snappedY = Math.round(point.getY() / (float) gridSize) * gridSize;

// Update the existing point's coordinates
point.setX(snappedX);
point.setY(snappedY);
}
}

/**
* Helper method that rounds the x/y coordinates of a GPoint
*
Expand All @@ -75,6 +59,17 @@ public static GPoint round(final GPoint point) {
return GraphUtil.point(x, y);
}

/**
* Helper method to snap routing points to the grid
*/
public static void round(List<GPoint> points) {
for (GPoint point : points) {
// Update the existing point's coordinates
point.setX(Math.round(point.getX()));
point.setY(Math.round(point.getY()));
}
}

/**
* Helper method that rounds the width/height of a GDimension
*
Expand Down

0 comments on commit 4cacdc6

Please sign in to comment.