Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | import d3 from 'd3' import { forEach } from 'lodash' import { distance } from '../util' import PointCluster from './pointcluster' import MultiPoint from './multipoint' /** * Utility class to cluster points into MultiPoint objects */ export default class PointClusterMap { constructor(transitive) { this.transitive = transitive this.clusters = [] this.clusterLookup = {} // maps Point object to its containing cluster const pointArr = [] forEach( Object.values(transitive.stops), (point) => { if (point.used) pointArr.push(point) }, this ) forEach( Object.values(transitive.turnPoints), (turnPoint) => { pointArr.push(turnPoint) }, this ) const links = d3.geom .voronoi() .x(function (d) { return d.worldX }) .y(function (d) { return d.worldY }) .links(pointArr) forEach( links, (link) => { const dist = distance( link.source.worldX, link.source.worldY, link.target.worldX, link.target.worldY ) if ( dist < 100 && (link.source.getType() !== 'TURN' || link.target.getType() !== 'TURN') ) { const sourceInCluster = link.source in this.clusterLookup const targetInCluster = link.target in this.clusterLookup if (sourceInCluster && !targetInCluster) { this.addPointToCluster(link.target, this.clusterLookup[link.source]) } else if (!sourceInCluster && targetInCluster) { this.addPointToCluster(link.source, this.clusterLookup[link.target]) } else if (!sourceInCluster && !targetInCluster) { const cluster = new PointCluster() this.clusters.push(cluster) this.addPointToCluster(link.source, cluster) this.addPointToCluster(link.target, cluster) } } }, this ) this.vertexPoints = [] forEach(this.clusters, (cluster) => { const multipoint = new MultiPoint(cluster.points) this.vertexPoints.push(multipoint) forEach(cluster.points, (point) => { point.multipoint = multipoint }) }) } addPointToCluster(point, cluster) { cluster.addPoint(point) this.clusterLookup[point] = cluster } clearMultiPoints() { forEach(this.clusters, (cluster) => { forEach(cluster.points, (point) => { point.multipoint = null }) }) } getVertexPoints(baseVertexPoints) { if (!baseVertexPoints) return this.vertexPoints const vertexPoints = this.vertexPoints.concat() forEach(baseVertexPoints, (point) => { if (!point.multipoint) vertexPoints.push(point) }) return vertexPoints } } |