Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[KED-2365] Simplify layout algorithm, improve layout quality and performance #398

Merged
merged 5 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 35 additions & 90 deletions src/utils/graph/constraints.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,13 @@ import { Constraint, Operator, Strength } from 'kiwi.js';
export const rowConstraint = {
property: 'y',

solve: (constraint, constants) => {
const { a, b } = constraint;
const difference = a.y - b.y;
const target = constants.spaceY;

if (difference >= target) {
return;
}

const resolve = difference - target;
a.y -= 0.5 * resolve;
b.y += 0.5 * resolve;
},

strict: (constraint, constants, variableA, variableB) => {
return new Constraint(
strict: (constraint, constants, variableA, variableB) =>
new Constraint(
variableA.minus(variableB),
Operator.Ge,
constants.spaceY,
Strength.required
);
},
),
};

/**
Expand All @@ -42,28 +27,13 @@ export const rowConstraint = {
export const layerConstraint = {
property: 'y',

solve: (constraint, constants) => {
const { a, b } = constraint;
const difference = a.y - b.y;
const target = constants.layerSpace;

if (difference >= target) {
return;
}

const resolve = difference - target;
a.y -= 0.5 * resolve;
b.y += 0.5 * resolve;
},

strict: (constraint, constants, variableA, variableB) => {
return new Constraint(
strict: (constraint, constants, variableA, variableB) =>
new Constraint(
variableA.minus(variableB),
Operator.Ge,
constants.layerSpace,
Strength.required
);
},
),
};

/**
Expand All @@ -73,54 +43,44 @@ export const parallelConstraint = {
property: 'x',

solve: (constraint) => {
const { a, b } = constraint;
const difference = a.x - b.x;

if (difference === 0) {
return;
}

const strength =
1 / Math.max(1, 0.5 * (a.targets.length + b.sources.length));

const resolve = strength * difference;
a.x -= 0.5 * resolve;
b.x += 0.5 * resolve;
const { a, b, strength } = constraint;
const resolve = strength * (a.x - b.x);
a.x -= resolve;
b.x += resolve;
},

strict: (constraint, constants, variableA, variableB) => {
return new Constraint(
strict: (constraint, constants, variableA, variableB) =>
new Constraint(
variableA.minus(variableB),
Operator.Eq,
0,
Strength.strong
);
},
Strength.create(1, 0, 0, constraint.strength)
),
};

/**
* Layout constraint in X for minimising edge crossings
* Crossing constraint in X for minimising edge crossings
*/
export const crossingConstraint = {
property: 'x',

solve: (constraint, constants) => {
const { a, b, edgeA, edgeB } = constraint;
const difference = a.x - b.x;
const sourceDelta = edgeA.sourceNode.x - edgeB.sourceNode.x;
const targetDelta = edgeA.targetNode.x - edgeB.targetNode.x;
const target =
sourceDelta + targetDelta < 0 ? -constants.basisX : constants.basisX;

if (target >= 0 ? difference >= target : difference <= target) {
return;
}

const strength = 1 / constants.basisX;

const resolve = strength * (difference - target);
a.x -= 0.5 * resolve;
b.x += 0.5 * resolve;
solve: (constraint) => {
const { edgeA, edgeB, separationA, separationB, strength } = constraint;

// Amount to move each node towards required separation
const resolveSource =
strength *
((edgeA.sourceNode.x - edgeB.sourceNode.x - separationA) / separationA);
const resolveTarget =
strength *
((edgeA.targetNode.x - edgeB.targetNode.x - separationB) / separationB);

// Apply the resolve each node
edgeA.sourceNode.x -= resolveSource;
edgeB.sourceNode.x += resolveSource;
edgeA.targetNode.x -= resolveTarget;
edgeB.targetNode.x += resolveTarget;
},
};

Expand All @@ -130,26 +90,11 @@ export const crossingConstraint = {
export const separationConstraint = {
property: 'x',

solve: (constraint) => {
const { a, b } = constraint;
const difference = b.x - a.x;
const target = constraint.separation;

if (difference >= target) {
return;
}

const resolve = difference - target;
a.x += 0.5 * resolve;
b.x -= 0.5 * resolve;
},

strict: (constraint, constants, variableA, variableB) => {
return new Constraint(
strict: (constraint, constants, variableA, variableB) =>
new Constraint(
variableB.minus(variableA),
Operator.Ge,
constraint.separation,
Strength.required
);
},
),
};
6 changes: 3 additions & 3 deletions src/utils/graph/graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { routing } from './routing';

const defaultOptions = {
layout: {
spaceX: 16,
spaceX: 14,
spaceY: 110,
layerSpaceY: 55,
basisX: 1500,
spreadX: 2.2,
padding: 100,
iterations: 20,
iterations: 25,
},
routing: {
spaceX: 26,
Expand Down
Loading