|
| 1 | +export type Intersection = import('./intersect.d.ts').Intersection & { |
| 2 | + id1: number; |
| 3 | + id2: number; |
| 4 | +}; |
| 5 | +type Path = import('./intersect.d.ts').Path; |
| 6 | +type PathComponent = import('./intersect.d.ts').PathComponent; |
| 7 | + |
| 8 | +export type BBox = { x0: number; y0: number; x1: number; y1: number }; |
| 9 | + |
| 10 | +export type IndexEntry = { |
| 11 | + pathId: number; |
| 12 | + curveIndex: number; |
| 13 | + curve: PathComponent; |
| 14 | +}; |
| 15 | + |
| 16 | +export type IndexIntersection = [IndexEntry, IndexEntry]; |
| 17 | + |
| 18 | +export interface SpatialIndex { |
| 19 | + add( |
| 20 | + pathId: number, |
| 21 | + curveIndex: number, |
| 22 | + curve: PathComponent, |
| 23 | + bbox: BBox |
| 24 | + ): void; |
| 25 | + |
| 26 | + remove(pathId: number): void; |
| 27 | + |
| 28 | + intersect(pathIds: number[]): IndexIntersection[]; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Index {@link path} into {@link spatialIndex} |
| 33 | + * Must be called before {@link findPathIntersections} |
| 34 | + * @returns index key to pass to {@link findPathIntersections} |
| 35 | + */ |
| 36 | +export function indexPath(path: Path, spatialIndex: SpatialIndex): number; |
| 37 | + |
| 38 | +/** |
| 39 | + * Find or counts the intersections between two SVG paths. |
| 40 | + * |
| 41 | + * Returns a number in counting mode and a list of intersections otherwise. |
| 42 | + * |
| 43 | + * A single intersection entry contains the intersection coordinates (x, y) |
| 44 | + * as well as additional information regarding the intersecting segments |
| 45 | + * on each path (segment1, segment2) and the relative location of the |
| 46 | + * intersection on these segments (t1, t2). |
| 47 | + * |
| 48 | + * The path may be an SVG path string or a list of path components |
| 49 | + * such as `[ [ 'M', 0, 10 ], [ 'L', 20, 0 ] ]`. |
| 50 | + * |
| 51 | + * Uses spatial indexing to boost performance. |
| 52 | + * If a path is not indexed the method will return no intersections. |
| 53 | + * @see {@link indexPath} |
| 54 | + * |
| 55 | + * @example |
| 56 | + * |
| 57 | + * const spatialIndex = new SpatialIndex(); |
| 58 | + * const id1 = indexPath('M0,0L100,100', spatialIndex); |
| 59 | + * const id2 = indexPath([ [ 'M', 0, 100 ], [ 'L', 100, 0 ] ], spatialIndex); |
| 60 | + * const id3 = indexPath([ [ 'M', 0, 50 ], [ 'L', 100, 50 ] ], spatialIndex); |
| 61 | + * |
| 62 | + * const intersections = findPathIntersections(id1, id2, spatialIndex, false); |
| 63 | + * const intersections2 = findPathIntersections(id1, id3, spatialIndex, false); |
| 64 | + * |
| 65 | + * // intersections = [ |
| 66 | + * // { x: 50, y: 50, segment1: 1, segment2: 1, t1: 0.5, t2: 0.5 } |
| 67 | + * // ]; |
| 68 | + * // intersections2 = [ |
| 69 | + * // { x: 50, y: 50, segment1: 1, segment2: 1, t1: 0.5, t2: 0.5 } |
| 70 | + * // ]; |
| 71 | + */ |
| 72 | +declare function findPathIntersections( |
| 73 | + pathIds: number[], |
| 74 | + index: SpatialIndex, |
| 75 | + justCount: true |
| 76 | +): number; |
| 77 | +declare function findPathIntersections( |
| 78 | + pathIds: number[], |
| 79 | + index: SpatialIndex, |
| 80 | + justCount: false |
| 81 | +): Intersection[]; |
| 82 | +declare function findPathIntersections( |
| 83 | + pathIds: number[], |
| 84 | + index: SpatialIndex |
| 85 | +): Intersection[]; |
| 86 | +declare function findPathIntersections( |
| 87 | + pathIds: number[], |
| 88 | + index: SpatialIndex, |
| 89 | + justCount?: boolean |
| 90 | +): Intersection[] | number; |
| 91 | + |
| 92 | +export default findPathIntersections; |
0 commit comments