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

#833 Parallel is not respect when it is auto-simple #931

Merged
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
8 changes: 4 additions & 4 deletions src/components/svgs/lines/paths/diagonal.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Button, Text } from '@chakra-ui/react';
import { Button } from '@chakra-ui/react';
import { RmgFields, RmgFieldsField } from '@railmapgen/rmg-components';
import { useTranslation } from 'react-i18next';
import { LineId, MiscNodeId, StnId } from '../../../../constants/constants';
import { LineId } from '../../../../constants/constants';
import {
LinePath,
LinePathAttributes,
LinePathAttrsProps,
LinePathType,
PathGenerator,
} from '../../../../constants/lines';
import { useRootDispatch, useRootSelector } from '../../../../redux';
import { useRootDispatch } from '../../../../redux';
import { setSelected } from '../../../../redux/runtime/runtime-slice';
import { getBaseParallelLineID, makeParallelIndex } from '../../../../util/parallel';
import { getBaseParallelLineID } from '../../../../util/parallel';
import { roundPathCorners } from '../../../../util/pathRounding';

const generateDiagonalPath: PathGenerator<DiagonalPathAttributes> = (
Expand Down
6 changes: 3 additions & 3 deletions src/components/svgs/lines/paths/perpendicular.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Button } from '@chakra-ui/react';
import { RmgFields, RmgFieldsField } from '@railmapgen/rmg-components';
import { useTranslation } from 'react-i18next';
import { LineId, MiscNodeId, StnId } from '../../../../constants/constants';
import { LineId } from '../../../../constants/constants';
import {
LinePath,
LinePathAttributes,
LinePathAttrsProps,
LinePathType,
PathGenerator,
} from '../../../../constants/lines';
import { useRootDispatch, useRootSelector } from '../../../../redux';
import { useRootDispatch } from '../../../../redux';
import { setSelected } from '../../../../redux/runtime/runtime-slice';
import { getBaseParallelLineID, makeParallelIndex } from '../../../../util/parallel';
import { getBaseParallelLineID } from '../../../../util/parallel';
import { roundPathCorners } from '../../../../util/pathRounding';

const generatePerpendicularPath: PathGenerator<PerpendicularPathAttributes> = (
Expand Down
49 changes: 28 additions & 21 deletions src/util/auto-simple.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { LineId } from '../constants/constants';
import { ExternalLinePathAttributes, LinePathType } from '../constants/lines';

/**
Expand All @@ -16,8 +15,7 @@ export const checkSimplePathAvailability = (
y1: number,
x2: number,
y2: number,
attrs: NonNullable<ExternalLinePathAttributes[keyof ExternalLinePathAttributes]>,
parallelIndex: number
attrs: NonNullable<ExternalLinePathAttributes[keyof ExternalLinePathAttributes]>
): { x1: number; y1: number; x2: number; y2: number; offset: number } | undefined => {
// Check if offsetFrom and offsetTo are defined and are numbers.
if (!('offsetFrom' in attrs) || !('offsetTo' in attrs)) return;
Expand All @@ -27,24 +25,7 @@ export const checkSimplePathAvailability = (
// It is just parallel to the line from (x1,y1) to (x2,y2).
if (attrs['offsetFrom'] === attrs['offsetTo']) {
if (checkKAndType(type, x1, y1, x2, y2)) {
if (parallelIndex < 0) {
return { x1, y1, x2, y2, offset: attrs['offsetFrom'] };
}

// auto parallel instead of manual offset tweaks when parallelIndex >= 0 (enabled)
if (x1 === x2) {
return { x1: x1 + 5 * parallelIndex, y1, x2: x2 + 5 * parallelIndex, y2, offset: attrs['offsetFrom'] };
}
if (y1 === y2) {
return { x1, y1: y1 + 5 * parallelIndex, x2, y2: y2 + 5 * parallelIndex, offset: attrs['offsetFrom'] };
}
return {
x1: x1 + 5 * Math.SQRT1_2 * parallelIndex,
y1: y1 + 5 * Math.SQRT1_2 * parallelIndex,
x2: x2 + 5 * Math.SQRT1_2 * parallelIndex,
y2: y2 + 5 * Math.SQRT1_2 * parallelIndex,
offset: attrs['offsetFrom'],
};
return { x1, y1, x2, y2, offset: attrs['offsetFrom'] };
}
return;
}
Expand Down Expand Up @@ -74,6 +55,32 @@ export const checkSimplePathAvailability = (
}
};

/**
* Auto parallel instead of manual offset tweaks when parallelIndex > 0 (non base parallel line).
*/
export const reconcileSimplePathWithParallel = (
x1: number,
y1: number,
x2: number,
y2: number,
offset: number,
parallelIndex: number
) => {
if (x1 === x2) {
return { x1: x1 + 5 * parallelIndex, y1, x2: x2 + 5 * parallelIndex, y2, offset };
}
if (y1 === y2) {
return { x1, y1: y1 + 5 * parallelIndex, x2, y2: y2 + 5 * parallelIndex, offset };
}
return {
x1: x1 + 5 * Math.SQRT1_2 * parallelIndex,
y1: y1 + 5 * Math.SQRT1_2 * parallelIndex,
x2: x2 + 5 * Math.SQRT1_2 * parallelIndex,
y2: y2 + 5 * Math.SQRT1_2 * parallelIndex,
offset,
};
};

/**
* Check k and type to see if this combination matches the simple path rule for this line path type.
* 1. k = 0 or ∞(parallel and vertical) for Diagonal and Perpendicular.
Expand Down
91 changes: 53 additions & 38 deletions src/util/parallel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,35 @@ export type NonSimpleLinePathAttributes = NonNullable<

const MIN_ROUND_CORNER_FACTOR = 1;

export const extractParallelLines = (
/**
* Classify all the lines between source and target of the provided line
* into lines that should be parallel to the provided line and lines that should not.
* Based on parallelIndex, type, and startFrom of the provided line.
* @param graph The graph.
* @param lineEntry The line entry.
* @returns An object containing normal and parallel lines.
*/
export const classifyParallelLines = (
graph: MultiDirectedGraph<NodeAttributes, EdgeAttributes, GraphAttributes>,
lineEntry: EdgeEntry<NodeAttributes, EdgeAttributes>
) => {
const lineID = lineEntry.edge as LineId;
const type = lineEntry.attributes.type;
const attr = lineEntry.attributes[type];
const parallelIndex = lineEntry.attributes.parallelIndex;

const { type, parallelIndex } = lineEntry.attributes;
// TODO: might be redundant to check this
if (type === LinePathType.Simple || parallelIndex < 0) {
return { normal: [lineEntry], parallel: [] };
}

const { source, target } = lineEntry;
const normal: EdgeEntry<NodeAttributes, EdgeAttributes>[] = [];
const [source, target] = graph.extremities(lineID);
const parallelLines: EdgeEntry<NodeAttributes, EdgeAttributes>[] = [];
for (const lineEntry of graph.edgeEntries(source, target)) {
if (lineEntry.attributes.parallelIndex < 0) {
normal.push(lineEntry);
continue;
}

// edgeEntries will also return edges from target to source
if (
lineEntry.attributes.type === type &&
source === lineEntry.source &&
(lineEntry.attributes[type] as NonSimpleLinePathAttributes).startFrom ===
(attr as NonSimpleLinePathAttributes).startFrom
) {
parallelLines.push(lineEntry);
} else if (
lineEntry.attributes.type === type &&
source === lineEntry.target &&
(lineEntry.attributes[type] as NonSimpleLinePathAttributes).startFrom !==
(attr as NonSimpleLinePathAttributes).startFrom
) {
const { startFrom } = lineEntry.attributes[type] as NonSimpleLinePathAttributes;
if (checkParallels(type, source as StnId | MiscNodeId, startFrom, lineEntry)) {
parallelLines.push(lineEntry);
}
}
Expand Down Expand Up @@ -135,6 +128,40 @@ export const makeParallelPaths = (parallelLines: EdgeEntry<NodeAttributes, EdgeA
return parallelPaths;
};

/**
* Check if the given lineEntry is in the same parallel group as the baseLineEntry.
* Which are either (source, target, from) or (target, source, to).
* Base line entry is determined by type, source, and startFrom.
* @param type The base line type.
* @param source The base line source.
* @param startFrom The base line startFrom.
* @param lineEntry The line entry to check.
* @returns Whether the lineEntry is in the same parallel group as the baseLineEntry.
*/
const checkParallels = (
type: LinePathType,
source: StnId | MiscNodeId,
startFrom: 'from' | 'to',
lineEntry: EdgeEntry<NodeAttributes, EdgeAttributes>
) => {
const lineEntryType = lineEntry.attributes.type;
if (
type === lineEntry.attributes.type &&
source === lineEntry.source &&
startFrom === (lineEntry.attributes[lineEntryType] as NonSimpleLinePathAttributes).startFrom
) {
return true;
} else if (
// edgeEntries will also return edges from target to source
type === lineEntry.attributes.type &&
source === lineEntry.target &&
startFrom !== (lineEntry.attributes[lineEntryType] as NonSimpleLinePathAttributes).startFrom
) {
return true;
}
return false;
};

export const makeParallelIndex = (
graph: MultiDirectedGraph<NodeAttributes, EdgeAttributes, GraphAttributes>,
type: LinePathType,
Expand All @@ -147,19 +174,7 @@ export const makeParallelIndex = (
// find all parallel lines that are either (source, target, from) or (target, source, to)
const existingParallelIndex: number[] = [];
for (const lineEntry of graph.edgeEntries(source, target)) {
const attr = lineEntry.attributes;
if (
type === attr.type &&
source === lineEntry.source &&
(attr[type] as NonSimpleLinePathAttributes).startFrom === startFrom
) {
existingParallelIndex.push(lineEntry.attributes.parallelIndex);
} else if (
type === attr.type &&
// edgeEntries will also return edges from target to source
source === lineEntry.target &&
(attr[type] as NonSimpleLinePathAttributes).startFrom !== startFrom
) {
if (checkParallels(type, source, startFrom, lineEntry)) {
existingParallelIndex.push(lineEntry.attributes.parallelIndex);
}
}
Expand Down Expand Up @@ -187,16 +202,16 @@ export const getBaseParallelLineID = (
graph: MultiDirectedGraph<NodeAttributes, EdgeAttributes, GraphAttributes>,
type: LinePathType,
lineID: LineId
) => {
): LineId => {
if (type === LinePathType.Simple) return lineID;

const parallelIndex = graph.getEdgeAttribute(lineID, 'parallelIndex');
if (parallelIndex < 0) return lineID;

const startFrom = graph.getEdgeAttribute(lineID, type)!['startFrom'];
const { startFrom } = graph.getEdgeAttribute(lineID, type)!;
const [source, target] = graph.extremities(lineID);

let minParallelIndex = Number.MAX_VALUE;
let minParallelIndex = parallelIndex;
let minLineID = lineID;
for (const lineEntry of graph.edgeEntries(source, target)) {
const attr = lineEntry.attributes;
Expand All @@ -222,7 +237,7 @@ export const getBaseParallelLineID = (
minLineID = lineEntry.edge as LineId;
}
}
return minParallelIndex == parallelIndex ? lineID : minLineID;
return minLineID;
};

export const MAX_PARALLEL_LINES_FREE = 5;
Expand Down
Loading
Loading