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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 | import d3 from 'd3' import { forEach } from 'lodash' import MultiPoint from '../point/multipoint' import { ccw, isOutwardVector, sm } from '../util' import Edge from './edge' import EdgeGroup from './edgegroup' import Vertex from './vertex' const debug = require('debug')('transitive:graph') /** * A graph representing the underlying 'wireframe' network */ export default class NetworkGraph { constructor(network, vertices) { this.network = network this.edges = [] this.vertices = [] /** * Object mapping groups of edges that share the same two vertices. * - Key is string of format A_B, where A and B are vertex IDs and A < B * - Value is array of edges */ this.edgeGroups = {} // Add all base vertices for (const i in vertices) { this.addVertex(vertices[i], vertices[i].worldX, vertices[i].worldY) } } /** * Get the bounds of the graph in the graph's internal x/y coordinate space * * @return [[left, top], [right, bottom]] */ bounds() { let xmax = null let xmin = null let ymax = null let ymin = null for (const i in this.vertices) { const vertex = this.vertices[i] xmin = xmin ? Math.min(xmin, vertex.x) : vertex.x xmax = xmax ? Math.max(xmax, vertex.x) : vertex.x ymin = ymin ? Math.min(ymin, vertex.y) : vertex.y ymax = ymax ? Math.max(ymax, vertex.y) : vertex.y } const maxExtent = 20037508.34 return [ [xmin || -maxExtent, ymin || -maxExtent], [xmax || maxExtent, ymax || maxExtent] ] } /** * Add Vertex */ addVertex(point, x, y) { if (x === undefined || y === undefined) { const xy = sm.forward([point.getLon(), point.getLat()]) x = xy[0] y = xy[1] } const vertex = new Vertex(point, x, y) this.vertices.push(vertex) return vertex } /** * Add Edge */ addEdge(stops, from, to, segmentType) { if ( this.vertices.indexOf(from) === -1 || this.vertices.indexOf(to) === -1 ) { debug('Error: Cannot add edge. Graph does not contain vertices.') return } const edge = new Edge(stops, from, to) this.edges.push(edge) from.edges.push(edge) to.edges.push(edge) const groupKey = this.network.transitive.options.groupEdges ? this.getEdgeGroupKey(edge, segmentType) : edge.getId() if (!(groupKey in this.edgeGroups)) { this.edgeGroups[groupKey] = new EdgeGroup( edge.fromVertex, edge.toVertex, segmentType ) } this.edgeGroups[groupKey].addEdge(edge) return edge } removeEdge(edge) { // remove from the graph's edge collection const edgeIndex = this.edges.indexOf(edge) if (edgeIndex !== -1) this.edges.splice(edgeIndex, 1) // remove from any associated path segment edge lists forEach(edge.pathSegments, (segment) => { segment.removeEdge(edge) }) // remove from the endpoint vertex incidentEdge collections edge.fromVertex.removeEdge(edge) edge.toVertex.removeEdge(edge) } getEdgeGroup(edge) { return this.edgeGroups[this.getEdgeGroupKey(edge)] } getEdgeGroupKey(edge, segmentType) { return edge.fromVertex.getId() < edge.toVertex.getId() ? segmentType + '_' + edge.fromVertex.getId() + '_' + edge.toVertex.getId() : segmentType + '_' + edge.toVertex.getId() + '_' + edge.fromVertex.getId() } mergeVertices(vertexArray) { let xTotal = 0 let yTotal = 0 const vertexGroups = { MULTI: [], PLACE: [], STOP: [], TURN: [] } forEach(vertexArray, (vertex) => { if (vertex.point.getType() in vertexGroups) { vertexGroups[vertex.point.getType()].push(vertex) } }) // don't merge stops and places, or multiple places: if ( (vertexGroups.STOP.length > 0 && vertexGroups.PLACE.length > 0) || vertexGroups.PLACE.length > 1 || vertexGroups.MULTI.length > 0 ) { return } let mergePoint // if merging turns with a place, create a new merged vertex around the place if (vertexGroups.PLACE.length === 1 && vertexGroups.TURN.length > 0) { mergePoint = vertexGroups.PLACE[0].point // if merging turns with a single place, create a new merged vertex around the stop } else if (vertexGroups.STOP.length === 1 && vertexGroups.TURN.length > 0) { mergePoint = vertexGroups.STOP[0].point // if merging multiple stops, create a new MultiPoint vertex } else if (vertexGroups.STOP.length > 1) { mergePoint = new MultiPoint() forEach(vertexGroups.STOP, (stopVertex) => { mergePoint.addPoint(stopVertex.point) }) // if merging multiple turns } else if (vertexGroups.TURN.length > 1) { mergePoint = vertexGroups.TURN[0].point } if (!mergePoint) return const mergedVertex = new Vertex(mergePoint, 0, 0) forEach(vertexArray, (vertex) => { xTotal += vertex.x yTotal += vertex.y forEach(vertex.edges.slice(), (edge) => { if ( vertexArray.indexOf(edge.fromVertex) !== -1 && vertexArray.indexOf(edge.toVertex) !== -1 ) { this.removeEdge(edge) return } edge.replaceVertex(vertex, mergedVertex) mergedVertex.addEdge(edge) }) const index = this.vertices.indexOf(vertex) if (index !== -1) this.vertices.splice(index, 1) }) mergedVertex.x = xTotal / vertexArray.length mergedVertex.y = yTotal / vertexArray.length mergedVertex.oldVertices = vertexArray this.vertices.push(mergedVertex) } sortVertices() { this.vertices.sort((a, b) => { if (a.point && a.point.getType() === 'PLACE') return -1 if (b.point && b.point.getType() === 'PLACE') return 1 if (a.point && a.point.getType() === 'MULTI') return -1 if (b.point && b.point.getType() === 'MULTI') return 1 if (a.point && a.point.getType() === 'STOP') return -1 if (b.point && b.point.getType() === 'STOP') return 1 }) } /** * Get the equivalent edge */ getEquivalentEdge(pointArray, from, to) { for (let e = 0; e < this.edges.length; e++) { const edge = this.edges[e] if ( edge.fromVertex === from && edge.toVertex === to && pointArray.length === edge.pointArray.length && equal(pointArray, edge.pointArray) ) { return edge } if ( edge.fromVertex === to && edge.toVertex === from && pointArray.length === edge.pointArray.length && equal(pointArray.slice(0).reverse(), edge.pointArray) ) { return edge } } } /** * Split a specified graph edge around a set of specified split points, where * all split points are internal points of the edge to be split. A set of N * valid split points will result in N+1 new edges. The original edge is * removed from the graph. */ splitEdgeAtInternalPoints(edge, points) { let subEdgePoints = [] let newEdge const newEdgeInfoArr = [] let fromVertex = edge.fromVertex let geomCoords = [] // iterate through the parent edge points, creating new sub-edges as needed forEach(edge.pointArray, (point, i) => { if (edge.pointGeom && i < edge.pointGeom.length) { geomCoords = geomCoords.concat(edge.pointGeom[i]) } if (points.indexOf(point) !== -1) { // we've reached a split point const x = point.worldX const y = point.worldY const newVertex = point.graphVertex || this.addVertex(point, x, y) newVertex.isInternal = true newEdge = this.addEdge( subEdgePoints, fromVertex, newVertex, edge.edgeGroup.type ) newEdge.isInternal = true newEdge.copyPathSegments(edge) newEdgeInfoArr.push({ fromVertex: fromVertex, graphEdge: newEdge }) if (geomCoords.length > 0) newEdge.geomCoords = geomCoords subEdgePoints = [] fromVertex = newVertex geomCoords = [] } else { // otherwise, this point becomes an internal point of the new edge currently being created subEdgePoints.push(point) } }) // create the last sub-edge newEdge = this.addEdge( subEdgePoints, fromVertex, edge.toVertex, edge.edgeGroup.type ) newEdge.isInternal = true newEdge.copyPathSegments(edge) if (edge.pointGeom && edge.pointArray.length < edge.pointGeom.length) { geomCoords = geomCoords.concat(edge.pointGeom[edge.pointArray.length]) } if (geomCoords.length > 0) newEdge.geomCoords = geomCoords newEdgeInfoArr.push({ fromVertex: fromVertex, graphEdge: newEdge }) // insert the new edge sequence into the affected segments forEach(edge.pathSegments, (pathSegment) => { const indexInSegment = pathSegment.getEdgeIndex(edge) const forward = pathSegment.edges[indexInSegment].forward let index = pathSegment.getEdgeIndex(edge) forEach( forward ? newEdgeInfoArr : newEdgeInfoArr.reverse(), (edgeInfo) => { pathSegment.insertEdgeAt( index, edgeInfo.graphEdge, forward ? edgeInfo.fromVertex : edgeInfo.toVertex ) index++ } ) }) // remove the original edge from the graph this.removeEdge(edge) } /* collapseTransfers = function(threshold) { if(!threshold) return; this.edges.forEach(function(edge) { if (edge.getLength() > threshold || edge.fromVertex.point.containsFromPoint() || edge.fromVertex.point.containsToPoint() || edge.toVertex.point.containsFromPoint() || edge.toVertex.point.containsToPoint()) return; //if(edge.fromVertex.point.getType() === 'PLACE' || edge.toVertex.point.getType() === 'PLACE') return; var notTransit = true; edge.pathSegments.forEach(function(segment) { notTransit = notTransit && segment.type !== 'TRANSIT'; }); if (notTransit) { this.mergeVertices([edge.fromVertex, edge.toVertex]); } }, this); }; */ pruneVertices() { forEach(this.vertices, (vertex) => { if (vertex.point.containsSegmentEndPoint()) return const opposites = [] const pathSegmentBundles = {} // maps pathSegment id list (string) to collection of edges (array) forEach(vertex.edges, (edge) => { const pathSegmentIds = edge.getPathSegmentIds() if (!(pathSegmentIds in pathSegmentBundles)) { pathSegmentBundles[pathSegmentIds] = [] } pathSegmentBundles[pathSegmentIds].push(edge) const opp = edge.oppositeVertex(vertex) if (opposites.indexOf(opp) === -1) opposites.push(opp) }) if (opposites.length !== 2) return for (const key in pathSegmentBundles) { const edgeArr = pathSegmentBundles[key] if (edgeArr.length === 2) this.mergeEdges(edgeArr[0], edgeArr[1]) } }) } mergeEdges(edge1, edge2) { // check for infinite recursion loop case if ( edge1.fromVertex === edge2.toVertex && edge2.fromVertex === edge1.toVertex ) { return } // reverse edges if necessary if (edge1.fromVertex === edge2.toVertex) { this.mergeEdges(edge2, edge1) return } if (edge1.toVertex !== edge2.fromVertex) return // edges cannot be merged const internalPoints = edge1.pointArray.concat(edge2.pointArray) const newEdge = this.addEdge( internalPoints, edge1.fromVertex, edge2.toVertex, edge1.edgeGroup.type ) newEdge.pathSegments = edge1.pathSegments forEach(newEdge.pathSegments, (segment) => { const i = segment.getEdgeIndex(edge1) segment.insertEdgeAt(i, newEdge, newEdge.fromVertex) }) // if both input edges are have coordinate geometry, merge the coords arrays in the new edge if (edge1.geomCoords && edge2.geomCoords) { newEdge.geomCoords = edge1.geomCoords.concat( edge2.geomCoords.length > 0 ? edge2.geomCoords.slice(1) : [] ) } debug('merging:') debug(edge1) debug(edge2) this.removeEdge(edge1) this.removeEdge(edge2) } snapToGrid(cellSize) { const coincidenceMap = {} forEach(this.vertices, (vertex) => { const nx = Math.round(vertex.x / cellSize) * cellSize const ny = Math.round(vertex.y / cellSize) * cellSize vertex.x = nx vertex.y = ny const key = nx + '_' + ny if (!(key in coincidenceMap)) coincidenceMap[key] = [vertex] else coincidenceMap[key].push(vertex) }) forEach(coincidenceMap, (vertexArr) => { if (vertexArr.length > 1) { this.mergeVertices(vertexArr) } }) } calculateGeometry(cellSize, angleConstraint) { forEach(this.edges, (edge) => { edge.calculateGeometry(cellSize, angleConstraint) }) } resetCoordinates() { forEach(this.vertices, (vertex) => { vertex.x = vertex.origX vertex.y = vertex.origY }) } recenter() { const xCoords = [] const yCoords = [] forEach(this.vertices, (v) => { xCoords.push(v.x) yCoords.push(v.y) }) const mx = d3.median(xCoords) const my = d3.median(yCoords) forEach(this.vertices, (v) => { v.x = v.x - mx v.y = v.y - my }) } /** 2D line bundling & offsetting **/ apply2DOffsets() { this.initComparisons() const alignmentBundles = {} // maps alignment ID to array of range-bounded bundles on that alignment const addToBundle = (rEdge, alignmentId) => { let bundle // compute the alignment range of the edge being bundled const range = rEdge.graphEdge.getAlignmentRange(alignmentId) // check if bundles already exist for this alignment if (!(alignmentId in alignmentBundles)) { // if not, create new and add to collection bundle = new AlignmentBundle() bundle.addEdge(rEdge, range.min, range.max) alignmentBundles[alignmentId] = [bundle] // new AlignmentBundle(); } else { // 1 or more bundles currently exist for this alignmentId const bundleArr = alignmentBundles[alignmentId] // see if the segment range overlaps with that of an existing bundle for (let i = 0; i < bundleArr.length; i++) { if (bundleArr[i].rangeOverlaps(range.min, range.max)) { bundleArr[i].addEdge(rEdge, range.min, range.max) return } } // ..if not, create a new bundle bundle = new AlignmentBundle() bundle.addEdge(rEdge, range.min, range.max) bundleArr.push(bundle) } } forEach(this.edges, (edge) => { const fromAlignmentId = edge.getFromAlignmentId() const toAlignmentId = edge.getToAlignmentId() forEach(edge.renderedEdges, (rEdge) => { addToBundle(rEdge, fromAlignmentId) addToBundle(rEdge, toAlignmentId) }) }) const bundleSorter = (a, b) => { const aId = a.patternIds || a.pathSegmentIds const bId = b.patternIds || b.pathSegmentIds const aVector = a.getAlignmentVector(this.currentAlignmentId) const bVector = b.getAlignmentVector(this.currentAlignmentId) const isOutward = isOutwardVector(aVector) && isOutwardVector(bVector) ? 1 : -1 const abCompId = aId + '_' + bId if (abCompId in this.bundleComparisons) { return isOutward * this.bundleComparisons[abCompId] } const baCompId = bId + '_' + aId if (baCompId in this.bundleComparisons) { return isOutward * this.bundleComparisons[baCompId] } if (a.route && b.route && a.route.route_type !== b.route.route_type) { return a.route.route_type > b.route.route_type ? 1 : -1 } const isForward = a.forward && b.forward ? 1 : -1 return isForward * isOutward * (aId < bId ? -1 : 1) } forEach(Object.keys(alignmentBundles), (alignmentId) => { const bundleArr = alignmentBundles[alignmentId] forEach(bundleArr, (bundle) => { if (bundle.items.length <= 1) return const lw = 1.2 const bundleWidth = lw * (bundle.items.length - 1) this.currentAlignmentId = alignmentId bundle.items.sort(bundleSorter) forEach(bundle.items, (rEdge, i) => { const offset = -bundleWidth / 2 + i * lw if (rEdge.getType() === 'TRANSIT') { forEach(rEdge.patterns, (pattern) => { pattern.offsetAlignment(alignmentId, offset) }) } else rEdge.offsetAlignment(alignmentId, offset) }) }) }) } /** * Traverses the graph vertex-by-vertex, creating comparisons between all pairs of * edges for which a topological relationship can be established. */ initComparisons() { this.bundleComparisons = {} forEach(this.vertices, (vertex) => { const incidentGraphEdges = vertex.incidentEdges() const angleREdges = {} forEach(incidentGraphEdges, (incidentGraphEdge) => { const angle = incidentGraphEdge.fromVertex === vertex ? incidentGraphEdge.fromAngle : incidentGraphEdge.toAngle const angleDeg = (180 * angle) / Math.PI if (!(angleDeg in angleREdges)) angleREdges[angleDeg] = [] angleREdges[angleDeg] = angleREdges[angleDeg].concat( incidentGraphEdge.renderedEdges ) }) forEach(angleREdges, (rEdges) => { if (rEdges.length < 2) return for (let i = 0; i < rEdges.length - 1; i++) { for (let j = i + 1; j < rEdges.length; j++) { const re1 = rEdges[i] const re2 = rEdges[j] let opp1 = re1.graphEdge.oppositeVertex(vertex) let opp2 = re2.graphEdge.oppositeVertex(vertex) let isCcw = ccw(opp1.x, opp1.y, vertex.x, vertex.y, opp2.x, opp2.y) if (isCcw === 0) { const s1Ext = re1.findExtension(opp1) const s2Ext = re2.findExtension(opp2) if (s1Ext) opp1 = s1Ext.graphEdge.oppositeVertex(opp1) if (s2Ext) opp2 = s2Ext.graphEdge.oppositeVertex(opp2) isCcw = ccw(opp1.x, opp1.y, vertex.x, vertex.y, opp2.x, opp2.y) } isCcw = getInverse(re1, re2, vertex) * isCcw if (isCcw > 0) { // e1 patterns are 'less' than e2 patterns this.storeComparison(re1, re2) } if (isCcw < 0) { // e2 patterns are 'less' than e2 patterns this.storeComparison(re2, re1) } } } }) }) } storeComparison(s1, s2) { const s1Id = s1.patternIds || s1.pathSegmentIds const s2Id = s2.patternIds || s2.pathSegmentIds debug(`storing comparison: ${s1Id} < ${s2Id}`) this.bundleComparisons[`${s1Id}_${s2Id}`] = -1 this.bundleComparisons[`${s2Id}_${s1Id}`] = 1 } } /** * AlignmentBundle class */ class AlignmentBundle { constructor() { this.items = [] // RenderedEdges this.min = Number.MAX_VALUE this.max = -Number.MAX_VALUE } addEdge(rEdge, min, max) { if (this.items.indexOf(rEdge) === -1) { this.items.push(rEdge) } this.min = Math.min(this.min, min) this.max = Math.max(this.max, max) } rangeOverlaps(min, max) { return this.min < max && min < this.max } } /** Helper functions **/ function getInverse(s1, s2, vertex) { return (s1.graphEdge.toVertex === vertex && s2.graphEdge.toVertex === vertex) || (s1.graphEdge.toVertex === vertex && s2.graphEdge.fromVertex === vertex) ? -1 : 1 } /** * Check if arrays are equal */ function equal(a, b) { if (a.length !== b.length) { return false } for (const i in a) { if (a[i] !== b[i]) { return false } } return true } |