pipeNeighborhood.js 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /** Local metric corridor clipping. Proximity describes display context, never connectivity. */
  2. export const NEIGHBORHOOD_RADII = Object.freeze([25, 50, 100])
  3. export const neighborhoodRadius = (value) => NEIGHBORHOOD_RADII.includes(Number(value)) ? Number(value) : 50
  4. const EPS = 1e-9
  5. const dot = (a, b) => a[0] * b[0] + a[1] * b[1]
  6. const sub = (a, b) => [a[0] - b[0], a[1] - b[1]]
  7. const lerp = (a, b, t) => a.map((v, i) => v + (b[i] - v) * t)
  8. function circleInterval(p, d, center, radius) {
  9. const v = sub(p, center)
  10. const a = dot(d, d)
  11. const c = dot(v, v) - radius * radius
  12. if (a < EPS) return c <= EPS ? [[0, 1]] : []
  13. const b = 2 * dot(v, d)
  14. const disc = b * b - 4 * a * c
  15. if (disc < 0) return []
  16. const lo = Math.max(0, (-b - Math.sqrt(disc)) / (2 * a))
  17. const hi = Math.min(1, (-b + Math.sqrt(disc)) / (2 * a))
  18. return hi - lo > EPS ? [[lo, hi]] : []
  19. }
  20. function slab(value, delta, min, max, range) {
  21. if (Math.abs(delta) < EPS) return value >= min - EPS && value <= max + EPS ? range : null
  22. const a = (min - value) / delta
  23. const b = (max - value) / delta
  24. const lo = Math.max(range[0], Math.min(a, b))
  25. const hi = Math.min(range[1], Math.max(a, b))
  26. return hi - lo > EPS ? [lo, hi] : null
  27. }
  28. function corridorIntervals(p, q, a, b, radius) {
  29. const d = sub(q, p)
  30. const ab = sub(b, a)
  31. const length = Math.hypot(...ab)
  32. const intervals = [...circleInterval(p, d, a, radius), ...circleInterval(p, d, b, radius)]
  33. if (length < EPS) return intervals
  34. const along = ab.map((v) => v / length)
  35. const normal = [-along[1], along[0]]
  36. const local = sub(p, a)
  37. let range = slab(dot(local, along), dot(d, along), 0, length, [0, 1])
  38. if (range) range = slab(dot(local, normal), dot(d, normal), -radius, radius, range)
  39. if (range) intervals.push(range)
  40. return intervals
  41. }
  42. function mergeIntervals(intervals) {
  43. const merged = []
  44. for (const range of intervals.sort((a, b) => a[0] - b[0])) {
  45. const last = merged.at(-1)
  46. if (last && range[0] <= last[1] + EPS) last[1] = Math.max(last[1], range[1])
  47. else merged.push([...range])
  48. }
  49. return merged
  50. }
  51. export function createNeighborhood(path, radiusM) {
  52. const origin = path[0]
  53. const xFactor = 111320 * Math.cos(origin[1] * Math.PI / 180)
  54. const project = (point) => [(point[0] - origin[0]) * xFactor, (point[1] - origin[1]) * 111320]
  55. const points = path.map(project)
  56. const segments = points.length > 1 ? points.slice(1).map((p, i) => [points[i], p]) : [[points[0], points[0]]]
  57. const bounds = points.reduce((box, p) => [Math.min(box[0], p[0]), Math.min(box[1], p[1]), Math.max(box[2], p[0]), Math.max(box[3], p[1])], [Infinity, Infinity, -Infinity, -Infinity])
  58. const overlapsBounds = (p, q) => !(Math.max(p[0], q[0]) < bounds[0] - radiusM || Math.min(p[0], q[0]) > bounds[2] + radiusM || Math.max(p[1], q[1]) < bounds[1] - radiusM || Math.min(p[1], q[1]) > bounds[3] + radiusM)
  59. const intervals = (p, q) => overlapsBounds(p, q)
  60. ? mergeIntervals(segments.flatMap(([a, b]) => corridorIntervals(p, q, a, b, radiusM))) : []
  61. return {
  62. contains(point) { const p = project(point); return intervals(p, p).length > 0 },
  63. clip(pathToClip) {
  64. const pieces = []
  65. let current = null
  66. for (let i = 1; i < pathToClip.length; i += 1) {
  67. const start = pathToClip[i - 1]
  68. const end = pathToClip[i]
  69. const ranges = intervals(project(start), project(end))
  70. if (!ranges.length) { current = null; continue }
  71. for (const [lo, hi] of ranges) {
  72. const a = lerp(start, end, lo)
  73. const b = lerp(start, end, hi)
  74. const joined = current && current.at(-1).every((v, axis) => Math.abs(v - a[axis]) < 1e-10)
  75. if (joined) current.push(b)
  76. else { current = [a, b]; pieces.push(current) }
  77. if (hi < 1 - EPS) current = null
  78. }
  79. }
  80. return pieces
  81. },
  82. }
  83. }