UNPKG

@deck.gl-community/layers

Version:

Add-on layers for deck.gl

131 lines 5.05 kB
// deck.gl-community // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors import { createIterable } from '@deck.gl/core'; import { Vector2 } from '@math.gl/core'; /** Directions in which dependency markers may be rendered. */ export var PathDirection; (function (PathDirection) { /** Do not render dependency markers. */ PathDirection[PathDirection["NONE"] = 0] = "NONE"; /** Render markers from source to target. */ PathDirection[PathDirection["FORWARD"] = 1] = "FORWARD"; /** Render markers from target to source. */ PathDirection[PathDirection["BACKWARD"] = 2] = "BACKWARD"; /** Render markers in both directions. */ PathDirection[PathDirection["BOTH"] = 3] = "BOTH"; })(PathDirection || (PathDirection = {})); /** * Resolves directional marker placements for dependency path data. * @param options - Marker resolution options. * @param options.data - Dependency path data to inspect. * @param options.getPath - Accessor returning nested or flat path coordinates. * @param options.positionSize - Number of coordinate values in one flat path position. * @param options.getDirection - Accessor returning marker direction flags. * @param options.getMarkerPlacements - Accessor returning path ratios for marker placement. * @param options.mode - Routing mode used by the dependency line. * @returns Marker placements resolved against each dependency path. */ export function createPathMarkers({ data, getPath, positionSize, getDirection, getMarkerPlacements, mode }) { const markers = []; if (!data || typeof data === 'string' || !(Symbol.iterator in Object(data))) { return markers; } const { iterable, objectInfo } = createIterable(data); for (const object of iterable) { objectInfo.index++; const path = normalizePath(getPath(object, objectInfo), positionSize); if (path.length < 2) { continue; } if (mode !== 'path') { path.splice(1, path.length - 2); } const direction = typeof getDirection === 'function' ? getDirection(object, objectInfo) : getDirection; // calculate total length const lineLength = getLineLength(path); if (!Number.isFinite(lineLength) || lineLength <= 0) { continue; } objectInfo.lineLength = lineLength; const placements = typeof getMarkerPlacements === 'function' ? getMarkerPlacements(object, objectInfo) : getMarkerPlacements; const sourceObject = { object, index: objectInfo.index }; // Create the markers for (const dir of [PathDirection.FORWARD, PathDirection.BACKWARD]) { if (!(direction & dir)) continue; if (dir === PathDirection.BACKWARD) { path.reverse(); } for (const percentage of placements) { const marker = mode === 'arc' ? { source: path[0], target: path[1], percentage, __source: sourceObject } : createMarkerAlongPath({ path, percentage, lineLength, sourceObject }); markers.push(marker); } } } return markers; } function normalizePath(path, size) { if (!path) { return []; } if (Array.isArray(path[0])) { return path.map(([x, y]) => new Vector2(x, y)); } const flatPath = path; const length = flatPath.length / size; const points = new Array(length); for (let i = 0; i < length; i++) { points[i] = new Vector2(flatPath[i * size], flatPath[i * size + 1]); } return points; } function getLineLength(vPoints) { // calculate total length let lineLength = 0; for (let i = 0; i < vPoints.length - 1; i++) { lineLength += vPoints[i].distance(vPoints[i + 1]); } return lineLength; } function createMarkerAlongPath({ path, percentage, lineLength, sourceObject }) { const distanceAlong = lineLength * percentage; let currentDistance = 0; let previousDistance = 0; let i = 0; for (i = 0; i < path.length - 1; i++) { currentDistance += path[i].distance(path[i + 1]); if (currentDistance >= distanceAlong) { break; } previousDistance = currentDistance; } // If reached the end of the loop without exiting early, // undo the final increment to avoid a null-pointer exception if (i === path.length - 1) { i -= 1; } const along = distanceAlong - previousDistance; const segmentLength = path[i + 1].distance(path[i]); return { source: path[i], target: path[i + 1], percentage: segmentLength > 0 ? along / segmentLength : 0, __source: sourceObject }; } //# sourceMappingURL=create-path-markers.js.map