Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tscircuit/routing

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tscircuit/routing - npm Package Compare versions

Comparing version 1.3.0 to 1.3.1

src/lib/logging/log-context.ts

24

dist/index.d.ts

@@ -0,1 +1,13 @@

interface LogContextTree {
name: string;
start_time: number;
end_time?: number;
duration?: number;
children: LogContextTree[];
info: any;
start: (info: any) => void;
child: (name: string) => LogContextTree;
end: () => void;
}
type Obstacle = {

@@ -46,2 +58,3 @@ center: {

allowDiagonal?: boolean;
log?: LogContextTree;
};

@@ -66,3 +79,3 @@ type FoundPath = Path & {

declare const findTwoPointGranularRoute: ({ pointsToConnect, obstacles, grid, allowDiagonal, }: PathFindingParameters) => PathFindingResult;
declare const findTwoPointGranularRoute: ({ pointsToConnect, obstacles, grid, allowDiagonal, log, }: PathFindingParameters) => PathFindingResult;

@@ -89,2 +102,3 @@ type Parameters$1 = {

allowDiagonal?: boolean;
log?: LogContextTree;
};

@@ -98,5 +112,5 @@ /**

*/
declare const findSchematicRoute: ({ pointsToConnect, obstacles, grid, }: Parameters) => PathFindingResult;
declare const findSchematicRoute: ({ pointsToConnect, obstacles, grid, log, }: Parameters) => PathFindingResult;
declare const findTwoPointMixedGranularityRoute: ({ pointsToConnect, obstacles, grid, allowDiagonal, }: PathFindingParameters) => PathFindingResult;
declare const findTwoPointMixedGranularityRoute: ({ pointsToConnect, obstacles, grid, allowDiagonal, log, }: PathFindingParameters) => PathFindingResult;

@@ -107,3 +121,3 @@ /**

*/
declare const findTwoPointNearBiasRoute: ({ pointsToConnect, obstacles, grid, allowDiagonal, depth, }: PathFindingParameters & {
declare const findTwoPointNearBiasRoute: ({ pointsToConnect, obstacles, grid, allowDiagonal, depth, log, }: PathFindingParameters & {
depth?: number;

@@ -123,3 +137,3 @@ }) => PathFindingResult;

*/
declare const findTwoPointSchematicRoute: ({ grid, obstacles, pointsToConnect, }: PathFindingParameters) => PathFindingResult;
declare const findTwoPointSchematicRoute: ({ grid, obstacles, pointsToConnect, log, }: PathFindingParameters) => PathFindingResult;

@@ -126,0 +140,0 @@ declare const movePointsOutsideObstacles: (params: PathFindingParameters, opts?: {

@@ -70,2 +70,40 @@ var __create = Object.create;

var PF = __toESM(require("pathfinding"));
// src/lib/logging/log-context.ts
var getStringifiedTreeLines = (tree) => {
var _a;
return [
`${`${((_a = tree == null ? void 0 : tree.duration) == null ? void 0 : _a.toString()) ?? "???"}ms`.padEnd(6, " ")} ${tree.name} ${tree.info ? JSON.stringify(tree.info) : ""}`,
...tree.children.map((child) => getStringifiedTreeLines(child)).flat().map((line) => ` ${line}`)
];
};
var createLogContextTree = ({
loudEnd
} = {}) => {
const tree = {
name: "root",
start_time: Date.now(),
info: {},
children: []
};
tree.start = (info) => {
tree.info = info;
};
tree.end = () => {
tree.end_time = Date.now();
tree.duration = tree.end_time - tree.start_time;
if (loudEnd && tree.name === "root") {
console.log(getStringifiedTreeLines(tree).join("\n"));
}
};
tree.child = (name) => {
const child = createLogContextTree();
child.name = name;
tree.children.push(child);
return child;
};
return tree;
};
// src/lib/find-two-point-granular-route.ts
var findTwoPointGranularRoute = ({

@@ -75,4 +113,6 @@ pointsToConnect,

grid,
allowDiagonal
allowDiagonal,
log
}) => {
log ??= createLogContextTree();
if (pointsToConnect.length !== 2)

@@ -95,6 +135,6 @@ throw new Error("Must supply exactly 2 pointsToConnect");

});
const gridWidthSegments = Math.round(
const gridWidthSegments = Math.ceil(
(gridBottomRight.x - gridTopLeft.x) / grid.segmentSize
);
const gridHeightSegments = Math.round(
const gridHeightSegments = Math.ceil(
(gridBottomRight.y - gridTopLeft.y) / grid.segmentSize

@@ -151,2 +191,3 @@ );

}, 0);
log.end();
return {

@@ -202,4 +243,6 @@ pathFound: true,

distanceToBorder,
allowDiagonal
allowDiagonal,
log
}) => {
log ??= createLogContextTree();
const { roundToNearestGridPoint } = computeGridTransform({ grid });

@@ -273,5 +316,72 @@ const borderPoints = [];

}
log.end();
return resultPaths;
};
// src/lib/find-two-point-mixed-granularity-route.ts
var MULTIPLES = [1, 2, 5, 10, 20];
var findTwoPointMixedGranularityRoute = ({
pointsToConnect,
obstacles,
grid,
allowDiagonal,
log
}) => {
log ??= createLogContextTree();
if (pointsToConnect.length !== 2)
throw new Error("Must supply exactly 2 pointsToConnect");
const [start, end] = pointsToConnect;
const maxDist = Math.max(Math.abs(start.x - end.x), Math.abs(start.y - end.y));
for (const multiple of MULTIPLES) {
if (grid.segmentSize * multiple > maxDist / 4)
continue;
const remainingSegDist = Math.max(Math.abs(start.x - end.x), Math.abs(start.y - end.y)) / (grid.segmentSize * multiple);
if (remainingSegDist > grid.maxGranularSearchSegments)
continue;
const { transformedGrid } = computeGridTransform({ grid, multiple });
const result = findTwoPointGranularRoute({
pointsToConnect: [start, end],
obstacles,
grid: transformedGrid
});
if (result.pathFound && multiple !== 1) {
const startMiddle = result.points[1];
const startPath = findTwoPointGranularRoute({
pointsToConnect: [start, startMiddle],
obstacles,
grid,
allowDiagonal
});
const middleEnd = result.points[result.points.length - 2];
const endPath = findTwoPointGranularRoute({
pointsToConnect: [middleEnd, end],
obstacles,
grid,
allowDiagonal
});
if (startPath.pathFound && endPath.pathFound) {
log.end();
return {
pathFound: true,
points: [
...startPath.points,
...result.points.slice(1, -1),
...endPath.points
],
// TODO result.length needs to be computed for slice
length: startPath.length + result.length - getPointDistance(result.points[0], result.points[1]) - getPointDistance(...result.points.slice(-2)) + endPath.length,
width: startPath.width
};
}
}
if (multiple === 1) {
log.end();
return result;
}
}
log.info.not_found = true;
log.end();
return { pathFound: false };
};
// src/lib/find-two-point-near-bias-route.ts

@@ -283,4 +393,6 @@ var findTwoPointNearBiasRoute = ({

allowDiagonal,
depth
depth,
log
}) => {
log ??= createLogContextTree();
if (pointsToConnect.length !== 2)

@@ -292,11 +404,28 @@ throw new Error("Must supply exactly 2 pointsToConnect");

if (remainingSegDist <= grid.maxGranularSearchSegments) {
return findTwoPointGranularRoute({
const granRoute = findTwoPointGranularRoute({
pointsToConnect,
obstacles,
grid,
allowDiagonal
allowDiagonal,
log: log.child("2P Granular Route")
});
log.end();
return granRoute;
}
if (depth > 3)
const mixedGranRoute = findTwoPointMixedGranularityRoute({
pointsToConnect,
obstacles,
grid,
allowDiagonal,
log: log.child(`2P Mixed Granularity Route`)
});
if (mixedGranRoute.pathFound) {
log.end();
return mixedGranRoute;
}
if (depth > 3) {
log.info.max_depth = true;
log.end();
return { pathFound: false, message: "Max depth reached" };
}
const distanceToBorder = Math.min(

@@ -312,3 +441,4 @@ grid.segmentSize * grid.maxGranularSearchSegments,

distantTarget: B,
allowDiagonal
allowDiagonal,
log: log.child("Border Target Paths A")
});

@@ -321,7 +451,13 @@ const BBP = findBestBorderTargetPaths({

distantTarget: A,
allowDiagonal
allowDiagonal,
log: log.child("Border Target Paths B")
});
const routes = [];
for (const abp of ABP) {
for (const bbp of BBP) {
const bp_log = log.child(
`Border Target Matrix Search ${ABP.length * BBP.length}`
);
for (let abpi = 0; abpi < ABP.length; abpi++) {
const abp = ABP[abpi];
for (let bbpi = 0; bbpi < BBP.length; bbpi++) {
const bbp = BBP[bbpi];
const middleRoute = findTwoPointNearBiasRoute({

@@ -332,3 +468,6 @@ pointsToConnect: [abp.borderTarget, bbp.borderTarget],

depth: depth + 1,
allowDiagonal
allowDiagonal,
log: bp_log.child(
`Middle Route ${abpi + 1}x${bbpi + 1}/${ABP.length}x${BBP.length}`
)
});

@@ -351,4 +490,9 @@ if (middleRoute.pathFound === false)

}
if (routes.length === 0)
bp_log.end();
if (routes.length === 0) {
log.info.no_routes = true;
log.end();
return { pathFound: false };
}
log.end();
return routes.reduce((a, b) => a.length < b.length ? a : b);

@@ -414,3 +558,3 @@ };

};
if (points.length > 2) {
if ((points == null ? void 0 : points.length) > 2) {
return isIntersectingObstacle({

@@ -468,6 +612,10 @@ points: points.slice(0, 2),

points,
obstacles
obstacles,
log
}) => {
if (points.length <= 3)
log ??= createLogContextTree();
if (points.length <= 3) {
log.end();
return points;
}
const newPoints = [points[0], points[1]];

@@ -505,2 +653,3 @@ for (let i = 1; i < points.length - 3; i++) {

newPoints.push(...points.slice(-2));
log.end();
return newPoints;

@@ -511,4 +660,6 @@ };

obstacles,
pointsToConnect
pointsToConnect,
log
}) => {
log ??= createLogContextTree();
const route = findTwoPointNearBiasRoute({

@@ -518,3 +669,4 @@ grid,

pointsToConnect,
allowDiagonal: false
allowDiagonal: false,
log
});

@@ -524,4 +676,11 @@ if (!route.pathFound)

let currentBest = route.points;
const tr_log = log.child("Remove Unnecessary Turns");
let iters = 0;
while (true) {
let newPoints = removeUnnecessaryTurns({ points: currentBest, obstacles });
iters++;
let newPoints = removeUnnecessaryTurns({
points: currentBest,
obstacles
// log: tr_log.child(`Remove Unnecessary Turns Iteration ${iters}`),
});
newPoints = removeUnnecessaryPoints(newPoints);

@@ -534,2 +693,3 @@ if (newPoints.length === currentBest.length) {

}
tr_log.end();
return {

@@ -545,4 +705,6 @@ ...route,

obstacles,
grid
grid,
log
}) => {
log ??= createLogContextTree();
pointsToConnect.sort((a, b) => {

@@ -556,2 +718,3 @@ const distA = Math.sqrt(a.x ** 2 + a.y ** 2);

let totalLength = 0;
const ps_log = log.child(`${pointsToConnect.length} A->B Path Search`);
for (let i = 0; i < pointsToConnect.length - 1; i++) {

@@ -564,3 +727,4 @@ const pointA = pointsToConnect[i];

grid,
allowDiagonal: false
allowDiagonal: false,
log: ps_log
});

@@ -575,61 +739,7 @@ if ("pathFound" in pathResult && pathResult.pathFound) {

}
ps_log.end();
log.end();
return pathFound ? { points, length: totalLength, width: 1, pathFound: true } : { pathFound: false };
};
// src/lib/find-two-point-mixed-granularity-route.ts
var MULTIPLES = [16, 8, 4, 3, 2, 1];
var findTwoPointMixedGranularityRoute = ({
pointsToConnect,
obstacles,
grid,
allowDiagonal
}) => {
if (pointsToConnect.length !== 2)
throw new Error("Must supply exactly 2 pointsToConnect");
const [start, end] = pointsToConnect;
const maxDist = Math.max(Math.abs(start.x - end.x), Math.abs(start.y - end.y));
for (const multiple of MULTIPLES) {
if (grid.segmentSize * multiple > maxDist / 4)
continue;
const { transformedGrid } = computeGridTransform({ grid, multiple });
const result = findTwoPointGranularRoute({
pointsToConnect: [start, end],
obstacles,
grid: transformedGrid
});
if (result.pathFound && multiple !== 1) {
const startMiddle = result.points[1];
const startPath = findTwoPointGranularRoute({
pointsToConnect: [start, startMiddle],
obstacles,
grid,
allowDiagonal
});
const middleEnd = result.points[result.points.length - 2];
const endPath = findTwoPointGranularRoute({
pointsToConnect: [middleEnd, end],
obstacles,
grid,
allowDiagonal
});
if (startPath.pathFound && endPath.pathFound) {
return {
pathFound: true,
points: [
...startPath.points,
...result.points.slice(1, -1),
...endPath.points
],
// TODO result.length needs to be computed for slice
length: startPath.length + result.length - getPointDistance(result.points[0], result.points[1]) - getPointDistance(...result.points.slice(-2)) + endPath.length,
width: startPath.width
};
}
}
if (multiple === 1)
return result;
}
throw new Error("unreachable");
};
// src/lib/move-points-outside-obstacles.ts

@@ -636,0 +746,0 @@ var movePointsOutsideObstacles = (params, opts) => {

{
"name": "@tscircuit/routing",
"version": "1.3.0",
"version": "1.3.1",
"description": "",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

import { computeGridTransform } from "./compute-grid-transform"
import { findTwoPointGranularRoute } from "./find-two-point-granular-route"
import { isInObstacle } from "./is-in-obstacle"
import { LogContextTree, createLogContextTree } from "./logging/log-context"
import type {

@@ -37,2 +38,3 @@ FoundPath,

allowDiagonal,
log,
}: {

@@ -45,3 +47,5 @@ grid: Grid

allowDiagonal?: boolean
log?: LogContextTree
}): Array<FoundPath & { borderTarget: Point }> => {
log ??= createLogContextTree()
const { roundToNearestGridPoint } = computeGridTransform({ grid })

@@ -151,3 +155,5 @@

}
log.end()
return resultPaths
}
import * as PF from "pathfinding"
import type { Point, Obstacle, Path, Grid, PathFindingResult } from "./types"
import { findTwoPointSchematicRoute } from "./find-two-point-schematic-route"
import { LogContextTree, createLogContextTree } from "./logging/log-context"

@@ -10,2 +11,3 @@ type Parameters = {

allowDiagonal?: boolean
log?: LogContextTree
}

@@ -24,3 +26,6 @@

grid,
log,
}: Parameters): PathFindingResult => {
log ??= createLogContextTree()
// Sort the points to connect in ascending order of their distances

@@ -38,2 +43,3 @@ pointsToConnect.sort((a, b) => {

// Iterate over each point
const ps_log = log.child(`${pointsToConnect.length} A->B Path Search`)
for (let i = 0; i < pointsToConnect.length - 1; i++) {

@@ -49,2 +55,3 @@ const pointA = pointsToConnect[i]

allowDiagonal: false,
log: ps_log,
})

@@ -60,2 +67,4 @@

}
ps_log.end()
log.end()

@@ -62,0 +71,0 @@ return pathFound

@@ -10,2 +10,3 @@ import * as PF from "pathfinding"

} from "./types"
import { createLogContextTree } from "./logging/log-context"

@@ -17,3 +18,5 @@ export const findTwoPointGranularRoute = ({

allowDiagonal,
log,
}: PathFindingParameters): PathFindingResult => {
log ??= createLogContextTree()
if (pointsToConnect.length !== 2)

@@ -39,6 +42,6 @@ throw new Error("Must supply exactly 2 pointsToConnect")

})
const gridWidthSegments = Math.round(
const gridWidthSegments = Math.ceil(
(gridBottomRight.x - gridTopLeft.x) / grid.segmentSize
)
const gridHeightSegments = Math.round(
const gridHeightSegments = Math.ceil(
(gridBottomRight.y - gridTopLeft.y) / grid.segmentSize

@@ -114,2 +117,3 @@ )

log.end()
return {

@@ -116,0 +120,0 @@ pathFound: true,

import { computeGridTransform } from "./compute-grid-transform"
import { findTwoPointGranularRoute } from "./find-two-point-granular-route"
import { getPointDistance } from "./get-point-distance"
import { createLogContextTree } from "./logging/log-context"
import type {

@@ -11,3 +12,4 @@ Point,

const MULTIPLES = [16, 8, 4, 3, 2, 1]
// const MULTIPLES = [20, 10, 5, 2, 1]
const MULTIPLES = [1, 2, 5, 10, 20]

@@ -19,3 +21,5 @@ export const findTwoPointMixedGranularityRoute = ({

allowDiagonal,
log,
}: PathFindingParameters): PathFindingResult => {
log ??= createLogContextTree()
if (pointsToConnect.length !== 2)

@@ -31,2 +35,9 @@ throw new Error("Must supply exactly 2 pointsToConnect")

// We should never exceed the maxGranularSearchSegments
const remainingSegDist =
Math.max(Math.abs(start.x - end.x), Math.abs(start.y - end.y)) /
(grid.segmentSize * multiple)
if (remainingSegDist > grid.maxGranularSearchSegments) continue
const { transformedGrid } = computeGridTransform({ grid, multiple })

@@ -54,2 +65,3 @@ const result = findTwoPointGranularRoute({

if (startPath.pathFound && endPath.pathFound) {
log.end()
return {

@@ -74,5 +86,11 @@ pathFound: true,

if (multiple === 1) return result
if (multiple === 1) {
log.end()
return result
}
}
throw new Error("unreachable")
log.info.not_found = true
log.end()
return { pathFound: false }
}
import { findBestBorderTargetPaths } from "./find-best-border-target-paths"
import { findTwoPointGranularRoute } from "./find-two-point-granular-route"
import { findTwoPointMixedGranularityRoute } from "./find-two-point-mixed-granularity-route"
import { createLogContextTree } from "./logging/log-context"
import type {

@@ -19,3 +21,5 @@ FoundPath,

depth,
log,
}: PathFindingParameters & { depth?: number }): PathFindingResult => {
log ??= createLogContextTree()
if (pointsToConnect.length !== 2)

@@ -30,3 +34,3 @@ throw new Error("Must supply exactly 2 pointsToConnect")

if (remainingSegDist <= grid.maxGranularSearchSegments) {
return findTwoPointGranularRoute({
const granRoute = findTwoPointGranularRoute({
pointsToConnect,

@@ -36,7 +40,28 @@ obstacles,

allowDiagonal,
log: log.child("2P Granular Route"),
})
log.end()
return granRoute
}
if (depth > 3) return { pathFound: false, message: "Max depth reached" }
// Attempt to find route w/ higher granualarity if it's a middle route (where
// precision isn't as important, since it doesn't connect to the end point)
const mixedGranRoute = findTwoPointMixedGranularityRoute({
pointsToConnect,
obstacles,
grid,
allowDiagonal,
log: log.child(`2P Mixed Granularity Route`),
})
if (mixedGranRoute.pathFound) {
log.end()
return mixedGranRoute
}
if (depth > 3) {
log.info.max_depth = true
log.end()
return { pathFound: false, message: "Max depth reached" }
}
const distanceToBorder = Math.min(

@@ -54,2 +79,3 @@ grid.segmentSize * grid.maxGranularSearchSegments,

allowDiagonal,
log: log.child("Border Target Paths A"),
})

@@ -64,2 +90,3 @@

allowDiagonal,
log: log.child("Border Target Paths B"),
})

@@ -70,4 +97,9 @@

const routes: FoundPath[] = []
for (const abp of ABP) {
for (const bbp of BBP) {
const bp_log = log.child(
`Border Target Matrix Search ${ABP.length * BBP.length}`
)
for (let abpi = 0; abpi < ABP.length; abpi++) {
const abp = ABP[abpi]
for (let bbpi = 0; bbpi < BBP.length; bbpi++) {
const bbp = BBP[bbpi]
const middleRoute = findTwoPointNearBiasRoute({

@@ -79,2 +111,5 @@ pointsToConnect: [abp.borderTarget, bbp.borderTarget],

allowDiagonal,
log: bp_log.child(
`Middle Route ${abpi + 1}x${bbpi + 1}/${ABP.length}x${BBP.length}`
),
})

@@ -96,6 +131,12 @@ if (middleRoute.pathFound === false) continue

}
if (routes.length === 0) return { pathFound: false }
bp_log.end()
if (routes.length === 0) {
log.info.no_routes = true
log.end()
return { pathFound: false }
}
log.end()
// Return shortest route
return routes.reduce((a, b) => (a.length < b.length ? a : b))
}
import { findTwoPointNearBiasRoute } from "./find-two-point-near-bias-route"
import { isIntersectingObstacle } from "./is-intersecting-obstacle"
import { LogContextTree, createLogContextTree } from "./logging/log-context"
import { removeUnnecessaryPoints } from "./remove-unnecessary-points"

@@ -14,7 +15,13 @@ import {

obstacles,
log,
}: {
points: Point[]
obstacles: Obstacle[]
log?: LogContextTree
}): Point[] => {
if (points.length <= 3) return points
log ??= createLogContextTree()
if (points.length <= 3) {
log.end()
return points
}

@@ -71,2 +78,3 @@ const newPoints = [points[0], points[1]]

newPoints.push(...points.slice(-2))
log.end()
return newPoints

@@ -90,3 +98,5 @@ }

pointsToConnect,
log,
}: PathFindingParameters): PathFindingResult => {
log ??= createLogContextTree()
// TODO Omit grid- shouldn't be needed for schematic routes

@@ -98,2 +108,3 @@ const route = findTwoPointNearBiasRoute({

allowDiagonal: false,
log,
})

@@ -104,4 +115,11 @@

let currentBest = route.points
const tr_log = log.child("Remove Unnecessary Turns")
let iters = 0
while (true) {
let newPoints = removeUnnecessaryTurns({ points: currentBest, obstacles })
iters++
let newPoints = removeUnnecessaryTurns({
points: currentBest,
obstacles,
// log: tr_log.child(`Remove Unnecessary Turns Iteration ${iters}`),
})
newPoints = removeUnnecessaryPoints(newPoints)

@@ -115,2 +133,3 @@

}
tr_log.end()

@@ -117,0 +136,0 @@ return {

@@ -57,3 +57,3 @@ import type { Obstacle, Point } from "./types"

if (points.length > 2) {
if (points?.length > 2) {
return (

@@ -60,0 +60,0 @@ isIntersectingObstacle({

@@ -0,1 +1,3 @@

import { LogContextTree } from "./logging/log-context"
export type Obstacle = {

@@ -42,2 +44,3 @@ center: { x: number; y: number }

allowDiagonal?: boolean
log?: LogContextTree
}

@@ -44,0 +47,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc