all files / diagram/interaction/ line-overlapping.js

93.38% Statements 423/453
85.26% Branches 266/312
94.23% Functions 49/52
93.29% Lines 417/447
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   564× 564× 564× 564× 354×   564× 564× 564× 564× 564× 564× 564×   837× 837× 837×     837×     837× 837× 837×   837× 837× 837×     837× 837×   837× 837×   1674× 1674× 1674× 1674× 1674×     1128× 1128× 1128×   3348×                 1401× 1401×   1401× 474×   1401× 1401× 1401× 1401×     837× 837×   837× 837× 837× 837× 837× 221×       1122× 1122×   1122× 1122× 1122× 1122× 1122× 4712× 1122× 4712× 4712× 4652× 1347×     3365× 760× 760× 760× 760× 656×         1122×   3360×       52×   145× 145×   175× 175× 120× 120× 120× 120×     175×                               210× 210× 210×           210×   210× 210× 210× 210×     210× 158×   210× 210× 210× 210× 137×     73× 73× 87× 87× 87× 138× 138× 138×     122×     210× 210× 564× 564×   210×   210× 210× 210× 564× 564× 564×       145× 145× 145× 595×         595×     145×   259× 259× 259× 259× 188× 188× 188× 188× 188× 975× 975× 188×   787× 787× 367× 367×   261× 70×   191×             188× 70× 70× 54× 54× 54× 54× 54×   54× 50× 50× 50× 50× 50× 50×           259× 259× 508× 508× 280× 280× 125×           259×               259× 259×     125× 125× 125× 125× 125× 125×     125× 125× 93× 93× 93× 14× 14×       32× 32×     125× 125× 125×   302× 302× 288× 288× 286× 286× 286× 286× 286× 176×           302×   140× 140× 140× 140× 27× 19× 19× 19×     140×   159× 159× 124×   159× 263× 165×   263× 263× 263×   159×   125× 125× 124× 165× 165× 165× 165× 165× 106×         238× 60× 60× 60× 60× 60× 60× 36×       24×   178× 178× 177× 177× 177×     177× 40×   177× 99×     78×   177×       178×   302× 302× 302× 302× 302× 302× 302× 302× 292× 292× 292× 292× 292× 250× 250× 250×   250× 250× 250× 110×     140×   22×     118×       292× 143× 140×             149× 148× 145×                   10×           302×   397× 397× 397× 397× 397× 397× 397× 397× 359× 359× 359× 359× 263×   359× 359× 359× 359× 15×                   15× 15×     344× 344× 220×     220×     38× 18× 18× 18× 18×     144×   451× 451× 451× 451× 451× 328× 328×     328×   451× 199× 199× 199×   199×     451× 428×     23×       49071×        
define(["require", "exports", "../objects/connector", "../utility/connector"], function (require, exports, connector_1, connector_2) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var LineSegment = (function () {
        function LineSegment(start, end, previousSegment) {
            this.startPoint = start;
            this.endPoint = end;
            this.previous = previousSegment;
            if (previousSegment) {
                previousSegment.next = this;
            }
            var isVertical = start.x === end.x;
            this.coordinate = isVertical ? start.x : start.y;
            this.sortedStart = isVertical ? Math.min(start.y, end.y) : Math.min(start.x, end.x);
            this.sortedEnd = isVertical ? Math.max(start.y, end.y) : Math.max(start.x, end.x);
            this.direction = isVertical ? (start.y < end.y ? 'Bottom' : 'Top') : (start.x < end.x ? 'Right' : 'Left');
            this.startEvent = new SweepEvent(this, true);
            this.endEvent = new SweepEvent(this, false);
        }
        LineSegment.prototype.updateCoordinate = function (delta) {
            this.coordinate += delta;
            var isHorizontal = this.direction === 'Left' || this.direction === 'Right';
            var newStartPoint = isHorizontal
                ? { x: this.startPoint.x, y: this.coordinate }
                : { x: this.coordinate, y: this.startPoint.y };
            var newEndPoint = isHorizontal
                ? { x: this.endPoint.x, y: this.coordinate }
                : { x: this.coordinate, y: this.endPoint.y };
            this.startPoint = newStartPoint;
            Eif (this.previous) {
                this.previous.adjustEnd(newStartPoint);
            }
            this.endPoint = newEndPoint;
            Eif (this.next) {
                this.next.adjustStart(newEndPoint);
            }
        };
        LineSegment.prototype.adjustStart = function (point) {
            this.startPoint = point;
            this.updateSortedValues();
        };
        LineSegment.prototype.adjustEnd = function (point) {
            this.endPoint = point;
            this.updateSortedValues();
        };
        LineSegment.prototype.updateSortedValues = function () {
            var isVertical = this.direction === 'Top' || this.direction === 'Bottom';
            this.sortedStart = isVertical ? Math.min(this.startPoint.y, this.endPoint.y) : Math.min(this.startPoint.x, this.endPoint.x);
            this.sortedEnd = isVertical ? Math.max(this.startPoint.y, this.endPoint.y) : Math.max(this.startPoint.x, this.endPoint.x);
            this.startEvent.refresh();
            this.endEvent.refresh();
        };
        return LineSegment;
    }());
    var SweepEvent = (function () {
        function SweepEvent(segment, isStart) {
            this.segment = segment;
            this.isStart = isStart;
            this.value = isStart ? segment.sortedStart : segment.sortedEnd;
        }
        SweepEvent.prototype.refresh = function () {
            this.value = this.isStart ? this.segment.sortedStart : this.segment.sortedEnd;
        };
        SweepEvent.prototype.compareTo = function (other) {
            if (this.value !== other.value) {
                return this.value < other.value ? -1 : 1;
            }
            return this.isStart ? -1 : other.isStart ? 1 : 0;
        };
        return SweepEvent;
    }());
    var SegmentTree = (function () {
        function SegmentTree() {
            this.horizontalBranches = new Map();
            this.verticalBranches = new Map();
        }
        SegmentTree.prototype.addSegment = function (segment) {
            var branches = this.getBranches(segment.direction);
            this.addSegmentToBranch(branches, segment);
        };
        SegmentTree.prototype.addSegmentToBranch = function (branches, segment) {
            if (!branches.has(segment.coordinate)) {
                branches.set(segment.coordinate, new Set());
            }
            var branch = branches.get(segment.coordinate);
            Eif (branch) {
                branch.add(segment.startEvent);
                branch.add(segment.endEvent);
            }
        };
        SegmentTree.prototype.removeSegment = function (segment) {
            var branches = this.getBranches(segment.direction);
            this.removeSegmentFromBranch(branches, segment);
        };
        SegmentTree.prototype.removeSegmentFromBranch = function (branches, segment) {
            var branch = branches.get(segment.coordinate);
            Eif (branch) {
                branch.delete(segment.startEvent);
                branch.delete(segment.endEvent);
                if (branch.size === 0) {
                    branches.delete(segment.coordinate);
                }
            }
        };
        SegmentTree.prototype.findOverlappingSegments = function (segment) {
            var branches = this.getBranches(segment.direction);
            return this.findOverlappingSegmentsInBranch(branches, segment);
        };
        SegmentTree.prototype.findOverlappingSegmentsInBranch = function (branches, segment) {
            var result = [];
            var seenSegments = new Set();
            var branch = branches.get(segment.coordinate);
            Eif (branch) {
                var branchArray_1 = [];
                branch.forEach(function (item) { return branchArray_1.push(item); });
                for (var i = 0; i < branchArray_1.length; i++) {
                    var sweepEvent = branchArray_1[parseInt(i.toString(), 10)];
                    if (!(sweepEvent.segment.sortedStart < segment.sortedStart && sweepEvent.segment.sortedEnd > segment.sortedEnd)) {
                        if (sweepEvent.value < segment.sortedStart || sweepEvent.value > segment.sortedEnd) {
                            continue;
                        }
                    }
                    if (sweepEvent.segment !== segment && !seenSegments.has(sweepEvent.segment)) {
                        seenSegments.add(sweepEvent.segment);
                        var maxStart = Math.max(segment.sortedStart, sweepEvent.segment.sortedStart);
                        var minEnd = Math.min(segment.sortedEnd, sweepEvent.segment.sortedEnd);
                        if (maxStart < minEnd) {
                            result.push(sweepEvent.segment);
                        }
                    }
                }
            }
            return result;
        };
        SegmentTree.prototype.getBranches = function (direction) {
            return direction === 'Left' || direction === 'Right' ? this.horizontalBranches : this.verticalBranches;
        };
        return SegmentTree;
    }());
    var AvoidLineOverlapping = (function () {
        function AvoidLineOverlapping(parent) {
            this.requireReroute = false;
            this.reRoutedCount = 0;
            this.considerNonWalkable = [];
            this.diagram = parent;
            this.segmentTree = new SegmentTree();
            this.segmentMappings = new Map();
            this.connectorMappings = new Map();
            this.modifiedConnector = new Set();
            this.modifiedSegments = new Map();
            this.currentSegments = new Map();
        }
        AvoidLineOverlapping.prototype.getModifiedConnector = function () {
            return this.modifiedConnector;
        };
        AvoidLineOverlapping.prototype.getModifiedConnectorSegments = function (connector) {
            var segments = this.segmentMappings.get(connector);
            return this.convertSegmentsToOrthogonal(segments);
        };
        AvoidLineOverlapping.prototype.refreshModifiedConnectors = function (diagram) {
            var _this = this;
            this.modifiedConnector.forEach(function (modifiedConnector) {
                var segments = _this.getModifiedConnectorSegments(modifiedConnector);
                Eif (segments.length) {
                    modifiedConnector.segments = segments;
                    diagram.connectorPropertyChange(modifiedConnector, {}, { type: 'Orthogonal', segments: segments });
                }
            });
            this.modifiedConnector.clear();
        };
        AvoidLineOverlapping.prototype.removeConnectors = function (connectors) {
            for (var i = 0; i < connectors.length; i++) {
                var obj = connectors[parseInt(i.toString(), 10)];
                var connector = void 0;
                if (typeof obj === 'string') {
                    connector = this.diagram.nameTable[obj];
                }
                else if (obj instanceof connector_1.Connector) {
                    connector = obj;
                }
                if (connector && connector.type === 'Orthogonal') {
                    this.removeConnector(connector);
                }
            }
        };
        AvoidLineOverlapping.prototype.removeConnector = function (connector) {
            var _this = this;
            var segments = this.segmentMappings.get(connector);
            Iif (segments) {
                segments.forEach(function (segment) {
                    _this.segmentTree.removeSegment(segment);
                    _this.connectorMappings.delete(segment);
                });
            }
            this.segmentMappings.delete(connector);
        };
        AvoidLineOverlapping.prototype.addConnector = function (connector, points, segments) {
            var _this = this;
            if (points === void 0) { points = []; }
            Eif (segments === void 0) { segments = []; }
            Iif (this.diagram.lineDistributionModule) {
                return;
            }
            if (points.length === 0) {
                points = connector.intermediatePoints;
            }
            this.removeConnector(connector);
            var lineSegments = this.createLineSegments(points);
            this.mapSegmentsToConnector(connector, lineSegments);
            if (points.length > 3) {
                this.adjustConnector(connector);
            }
            else {
                var overlappingConnectors = new Set();
                for (var _i = 0, lineSegments_1 = lineSegments; _i < lineSegments_1.length; _i++) {
                    var lineSegment = lineSegments_1[_i];
                    var overlappingsegments = this.segmentTree.findOverlappingSegments(lineSegment);
                    for (var _a = 0, overlappingsegments_1 = overlappingsegments; _a < overlappingsegments_1.length; _a++) {
                        var overlappingSegment = overlappingsegments_1[_a];
                        var overlappingConnector = this.connectorMappings.get(overlappingSegment);
                        overlappingConnectors.add(overlappingConnector);
                    }
                }
                overlappingConnectors.forEach(function (overlappingConnector) { return _this.adjustConnector(overlappingConnector); });
            }
        };
        AvoidLineOverlapping.prototype.createLineSegments = function (points) {
            var lineSegments = [];
            for (var i = 0; i < points.length - 1; i++) {
                var segment = new LineSegment(points[parseInt(i.toString(), 10)], points[parseInt((i + 1).toString(), 10)], i === 0 ? null : lineSegments[parseInt((i - 1).toString(), 10)]);
                lineSegments.push(segment);
            }
            return lineSegments;
        };
        AvoidLineOverlapping.prototype.mapSegmentsToConnector = function (connector, lineSegments) {
            var _this = this;
            this.segmentMappings.set(connector, lineSegments);
            lineSegments.forEach(function (lineSegment) {
                _this.segmentTree.addSegment(lineSegment);
                Eif (!_this.connectorMappings.has(lineSegment)) {
                    _this.connectorMappings.set(lineSegment, connector);
                }
            });
        };
        AvoidLineOverlapping.prototype.convertSegmentsToOrthogonal = function (lineSegments) {
            var modifiedSegments = [];
            Eif (lineSegments) {
                lineSegments.forEach(function (lineSegment) {
                    var orthogonalSegment = {
                        type: 'Orthogonal',
                        direction: lineSegment.direction,
                        length: Math.abs(lineSegment.sortedEnd - lineSegment.sortedStart)
                    };
                    modifiedSegments.push(orthogonalSegment);
                });
            }
            return modifiedSegments;
        };
        AvoidLineOverlapping.prototype.adjustConnector = function (connector) {
            var _this = this;
            this.requireReroute = false;
            var segments = this.segmentMappings.get(connector);
            if (segments.length > 2) {
                var firstSegment = segments[0];
                var sourceNode = this.diagram.nameTable[connector.sourceID];
                Eif (sourceNode && sourceNode.outEdges.length > 0) {
                    var refLineSegment = void 0;
                    for (var j = 0; j < sourceNode.outEdges.length; j++) {
                        var outConnector = this.diagram.nameTable[sourceNode.outEdges[parseInt(j.toString(), 10)]];
                        if (outConnector === connector) {
                            continue;
                        }
                        var refSegments = this.segmentMappings.get(outConnector);
                        if (refSegments && refSegments.length > 2) {
                            var refFirstSegment = refSegments[0];
                            if (refFirstSegment.direction === firstSegment.direction &&
                                refFirstSegment.next.direction === firstSegment.next.direction) {
                                if (!refLineSegment) {
                                    refLineSegment = refFirstSegment;
                                }
                                else Iif (Math.abs(firstSegment.coordinate - refFirstSegment.coordinate) <
                                    Math.abs(firstSegment.coordinate - refLineSegment.coordinate)) {
                                    refLineSegment = refFirstSegment;
                                }
                            }
                        }
                    }
                    if (refLineSegment) {
                        var shiftDelta = refLineSegment.next.coordinate - firstSegment.next.coordinate;
                        if (shiftDelta) {
                            var superNext = firstSegment.next.next;
                            var canShift = firstSegment.direction !== superNext.direction;
                            Eif (firstSegment.direction === superNext.direction) {
                                var maximumShift = this.calculateMaximumSegmentShift(firstSegment, superNext, superNext.direction);
                                canShift = Math.abs(shiftDelta) <= maximumShift;
                            }
                            if (canShift) {
                                this.modifiedSegments.clear();
                                this.segmentTree.removeSegment(firstSegment.next);
                                firstSegment.next.updateCoordinate(shiftDelta);
                                this.segmentTree.addSegment(firstSegment.next);
                                this.modifiedSegments.set(firstSegment.next, firstSegment.next.coordinate);
                                this.modifiedConnector.add(connector);
                            }
                        }
                    }
                }
            }
            var failedSegment = undefined;
            for (var i = 0; i < segments.length - 1; i++) {
                var segment = segments[parseInt(i.toString(), 10)];
                if (segment.previous && segment.next) {
                    var overlappingSegments = this.segmentTree.findOverlappingSegments(segment);
                    if (overlappingSegments.length > 0) {
                        if (!this.resolveOverlappingSegments(segment, overlappingSegments)) {
                            Eif (this.requireReroute) {
                                failedSegment = segment;
                                break;
                            }
                        }
                    }
                }
            }
            if (failedSegment && this.diagram.lineRoutingModule) {
                Eif (this.reRoutedCount <= AvoidLineOverlapping.maxReRouteLimit) {
                    while (this.considerNonWalkable.length > 0) {
                        var grid = this.considerNonWalkable.pop();
                        grid.walkable = true;
                    }
                    var grids = this.diagram.lineRoutingModule
                        .getGridsIntersect(failedSegment.startPoint, failedSegment.endPoint);
                    grids.forEach(function (grid) {
                        grid.walkable = false;
                        _this.considerNonWalkable.push(grid);
                    });
                    this.reRoutedCount++;
                    this.diagram.lineRoutingModule.refreshConnectorSegments(this.diagram, connector, false);
                }
            }
            this.reRoutedCount = 0;
            while (this.considerNonWalkable.length > 0) {
                var grid = this.considerNonWalkable.pop();
                grid.walkable = true;
            }
        };
        AvoidLineOverlapping.prototype.resolveOverlappingSegments = function (segment, overlappingSegments) {
            this.modifiedSegments.clear();
            this.currentSegments.clear();
            this.rootShiftingSegment = segment;
            var adjustSelfFirst = this.shouldAdjustSelfFirst(segment, overlappingSegments);
            var overlapSegment = overlappingSegments[0];
            var shiftDirection = adjustSelfFirst
                ? this.calculateShiftDirection(segment, overlappingSegments)
                : this.calculateShiftDirection(overlapSegment, this.segmentTree.findOverlappingSegments(overlapSegment));
            var shifted = false;
            if (adjustSelfFirst) {
                var secondarySegment = overlappingSegments.length === 1 ? overlapSegment : undefined;
                shifted = this.attemptAdjustment(segment, shiftDirection, secondarySegment);
                if (!shifted) {
                    shiftDirection = connector_2.getOppositeDirection(shiftDirection);
                    shifted = this.attemptAdjustment(segment, shiftDirection, secondarySegment, true);
                }
            }
            else {
                shifted = this.attemptAdjustment(overlapSegment, shiftDirection, segment);
                if (!shifted) {
                    shiftDirection = connector_2.getOppositeDirection(shiftDirection);
                    shifted = this.attemptAdjustment(overlapSegment, shiftDirection, segment, true);
                }
            }
            this.updateSegmentTreeWithModifiedSegments();
            this.rootShiftingSegment = undefined;
            return shifted;
        };
        AvoidLineOverlapping.prototype.shouldAdjustSelfFirst = function (segment, overlappingSegments) {
            var adjustSelfFirst = overlappingSegments.length > 1;
            if (overlappingSegments.length === 1) {
                var overlapSegment = overlappingSegments[0];
                if (overlapSegment.previous && overlapSegment.next) {
                    var maxStart = Math.max(segment.sortedStart, overlapSegment.sortedStart);
                    var minEnd = Math.min(segment.sortedEnd, overlapSegment.sortedEnd);
                    var currentRatio = (minEnd - maxStart) / (segment.sortedEnd - segment.sortedStart);
                    var overlapRatio = (minEnd - maxStart) / (overlapSegment.sortedEnd - overlapSegment.sortedStart);
                    if (currentRatio >= overlapRatio) {
                        adjustSelfFirst = true;
                    }
                }
                else {
                    adjustSelfFirst = true;
                }
            }
            return adjustSelfFirst;
        };
        AvoidLineOverlapping.prototype.attemptAdjustment = function (primarySegment, shiftDirection, secondarySegment, isOppositeShifting) {
            if (isOppositeShifting === void 0) { isOppositeShifting = false; }
            var isShifted = this.adjustSegment(primarySegment, shiftDirection, isOppositeShifting);
            this.updateModifiedSegments(isShifted);
            if (!isShifted) {
                if (secondarySegment && secondarySegment.previous && secondarySegment.next) {
                    shiftDirection = connector_2.getOppositeDirection(shiftDirection);
                    isShifted = this.adjustSegment(secondarySegment, shiftDirection, isOppositeShifting);
                    this.updateModifiedSegments(isShifted);
                }
            }
            return isShifted;
        };
        AvoidLineOverlapping.prototype.updateModifiedSegments = function (resetModifiedSegments) {
            var _this = this;
            if (resetModifiedSegments) {
                this.modifiedSegments.clear();
            }
            this.currentSegments.forEach(function (oldCoordinate, currentSegment) {
                if (resetModifiedSegments) {
                    _this.modifiedSegments.set(currentSegment, currentSegment.coordinate);
                }
                _this.segmentTree.removeSegment(currentSegment);
                currentSegment.updateCoordinate(oldCoordinate - currentSegment.coordinate);
                _this.segmentTree.addSegment(currentSegment);
            });
            this.currentSegments.clear();
        };
        AvoidLineOverlapping.prototype.updateSegmentTreeWithModifiedSegments = function () {
            var _this = this;
            if (this.modifiedSegments.size > 0) {
                this.modifiedSegments.forEach(function (newCoordinate, modifiedSegment) {
                    _this.segmentTree.removeSegment(modifiedSegment);
                    modifiedSegment.updateCoordinate(newCoordinate - modifiedSegment.coordinate);
                    _this.segmentTree.addSegment(modifiedSegment);
                    var connector = _this.connectorMappings.get(modifiedSegment);
                    if (!_this.modifiedConnector.has(connector)) {
                        _this.modifiedConnector.add(connector);
                    }
                });
            }
        };
        AvoidLineOverlapping.prototype.resolveOverlappingAtGivenDirection = function (segment, overlappingSegments, shiftDirection, isOppositeShifting, adjustOverlapsOnly) {
            if (adjustOverlapsOnly) {
                var isShifted = false;
                for (var _i = 0, overlappingSegments_1 = overlappingSegments; _i < overlappingSegments_1.length; _i++) {
                    var overlapSegment = overlappingSegments_1[_i];
                    Eif (overlapSegment.previous && overlapSegment.next) {
                        isShifted = this.adjustSegment(overlapSegment, shiftDirection, isOppositeShifting, adjustOverlapsOnly);
                        if (!isShifted) {
                            return false;
                        }
                    }
                }
                return isShifted;
            }
            var segmentToShift = segment;
            if (overlappingSegments.length === 1) {
                var adjustSelfFirst = this.shouldAdjustSelfFirst(segment, overlappingSegments);
                var overlapSegment = overlappingSegments[0];
                var directionToShift = adjustSelfFirst
                    ? this.calculateShiftDirection(segment, overlappingSegments)
                    : this.calculateShiftDirection(overlapSegment, this.segmentTree.findOverlappingSegments(overlapSegment));
                if (isOppositeShifting) {
                    directionToShift = connector_2.getOppositeDirection(directionToShift);
                }
                if (adjustSelfFirst) {
                    segmentToShift = directionToShift === shiftDirection ? segment : overlapSegment;
                }
                else {
                    segmentToShift = directionToShift === shiftDirection ? overlapSegment : segment;
                }
                Iif (segmentToShift === overlapSegment && (!overlapSegment.previous || !overlapSegment.next)) {
                    segmentToShift = segment;
                }
            }
            return this.adjustSegment(segmentToShift, shiftDirection, isOppositeShifting);
        };
        AvoidLineOverlapping.prototype.calculateShiftDirection = function (lineSegment, overlappingSegments) {
            var overlapSegment = overlappingSegments[0];
            var segment = lineSegment;
            var previousSegment = segment.previous;
            var nextSegment = segment.next;
            var nonSortedStart = segment.direction === 'Left' || segment.direction === 'Right' ? segment.startPoint.x : segment.startPoint.y;
            var nonSortedEnd = segment.direction === 'Left' || segment.direction === 'Right' ? segment.endPoint.x : segment.endPoint.y;
            var shiftDirection = segment.direction;
            if (previousSegment.direction === nextSegment.direction) {
                var overlapsPrevious = overlapSegment.previous;
                var overlapsNext = overlapSegment.next;
                var isStartCovered = overlapSegment.sortedStart <= nonSortedStart && nonSortedStart <= overlapSegment.sortedEnd;
                var isEndCovered = overlapSegment.sortedStart <= nonSortedEnd && nonSortedEnd <= overlapSegment.sortedEnd;
                if (isStartCovered && isEndCovered) {
                    var nonSortedStart1 = overlapSegment.direction === 'Left' || overlapSegment.direction === 'Right' ? overlapSegment.startPoint.x : overlapSegment.startPoint.y;
                    var nonSortedEnd1 = overlapSegment.direction === 'Left' || overlapSegment.direction === 'Right' ? overlapSegment.endPoint.x : overlapSegment.endPoint.y;
                    if (segment.direction !== overlapSegment.direction) {
                        var temp = nonSortedStart1;
                        nonSortedStart1 = nonSortedEnd1;
                        nonSortedEnd1 = temp;
                    }
                    var startDistance = Math.abs(nonSortedStart - nonSortedStart1);
                    var endDistance = Math.abs(nonSortedEnd - nonSortedEnd1);
                    if (startDistance >= endDistance) {
                        isEndCovered = false;
                    }
                    else {
                        if (nextSegment.next && segment.direction === nextSegment.next.direction &&
                            this.segmentTree.findOverlappingSegments(nextSegment).length !== 0) {
                            isEndCovered = false;
                        }
                        else {
                            isStartCovered = false;
                        }
                    }
                }
                if (isStartCovered) {
                    if (segment.direction === overlapSegment.direction && overlapsNext) {
                        shiftDirection = connector_2.getOppositeDirection(overlapsNext.direction);
                    }
                    else Eif (segment.direction !== overlapSegment.direction && overlapsPrevious) {
                        shiftDirection = connector_2.getOppositeDirection(overlapsPrevious.direction);
                    }
                    else {
                        shiftDirection = connector_2.getOppositeDirection(previousSegment.direction);
                    }
                }
                else if (isEndCovered) {
                    if (segment.direction === overlapSegment.direction && overlapsPrevious) {
                        shiftDirection = overlapsPrevious.direction;
                    }
                    else if (segment.direction !== overlapSegment.direction && overlapsNext) {
                        shiftDirection = connector_2.getOppositeDirection(overlapsNext.direction);
                    }
                    else {
                        shiftDirection = connector_2.getOppositeDirection(previousSegment.direction);
                    }
                }
                else {
                    shiftDirection = nextSegment.direction;
                }
            }
            else {
                if (!overlapSegment.previous || !overlapSegment.next ||
                    overlapSegment.previous.direction === overlapSegment.next.direction) {
                    shiftDirection = nextSegment.direction;
                }
                else {
                    var maxStart = Math.max(segment.sortedStart, overlapSegment.sortedStart);
                    var minEnd = Math.min(segment.sortedEnd, overlapSegment.sortedEnd);
                    var currentRatio = (minEnd - maxStart) / (segment.sortedEnd - segment.sortedStart);
                    var overlapRatio = (minEnd - maxStart) / (overlapSegment.sortedEnd - overlapSegment.sortedStart);
                    shiftDirection = currentRatio >= overlapRatio ? nextSegment.direction : previousSegment.direction;
                }
            }
            return shiftDirection;
        };
        AvoidLineOverlapping.prototype.adjustSegment = function (segment, directionToShift, isOppositeShifting, adjustOverlapsOnly) {
            if (adjustOverlapsOnly === void 0) { adjustOverlapsOnly = false; }
            var currentCoord = segment.coordinate;
            var previousSegment = segment.previous;
            var nextSegment = segment.next;
            var maximumAllowedShift = this.calculateMaximumSegmentShift(previousSegment, nextSegment, directionToShift);
            var directionMultiplier = directionToShift === 'Right' || directionToShift === 'Bottom' ? 1 : -1;
            var shiftDelta = directionMultiplier * AvoidLineOverlapping.ConnectorSpacing;
            if (Math.abs(shiftDelta) <= maximumAllowedShift) {
                this.segmentTree.removeSegment(segment);
                segment.updateCoordinate(shiftDelta);
                this.segmentTree.addSegment(segment);
                if (!this.currentSegments.has(segment)) {
                    this.currentSegments.set(segment, currentCoord);
                }
                Eif (this.diagram.lineRoutingModule) {
                    var startPoint = segment.startPoint;
                    var endPoint = segment.endPoint;
                    if (!this.diagram.lineRoutingModule.isPathWalkable(startPoint, endPoint, this.diagram)) {
                        Iif (!adjustOverlapsOnly && segment === this.rootShiftingSegment) {
                            this.segmentTree.removeSegment(segment);
                            segment.updateCoordinate(shiftDelta * -1);
                            this.segmentTree.addSegment(segment);
                            var overlappingSegments_2 = this.segmentTree.findOverlappingSegments(segment);
                            if (overlappingSegments_2.length !== 0) {
                                var oppositeDirection = connector_2.getOppositeDirection(directionToShift);
                                return this.resolveOverlappingAtGivenDirection(segment, overlappingSegments_2, oppositeDirection, isOppositeShifting, true);
                            }
                        }
                        this.requireReroute = true;
                        return false;
                    }
                }
                var overlappingSegments = this.segmentTree.findOverlappingSegments(segment);
                if (overlappingSegments.length !== 0) {
                    Iif (this.modifiedSegments.size !== 0 && this.modifiedSegments.size > (this.currentSegments.size + 1)) {
                        return false;
                    }
                    return this.resolveOverlappingAtGivenDirection(segment, overlappingSegments, directionToShift, isOppositeShifting, adjustOverlapsOnly);
                }
            }
            else if (!adjustOverlapsOnly && segment === this.rootShiftingSegment) {
                var overlappingSegments = this.segmentTree.findOverlappingSegments(segment);
                Eif (overlappingSegments.length !== 0) {
                    var oppositeDirection = connector_2.getOppositeDirection(directionToShift);
                    return this.resolveOverlappingAtGivenDirection(segment, overlappingSegments, oppositeDirection, isOppositeShifting, true);
                }
            }
            return segment.coordinate !== currentCoord;
        };
        AvoidLineOverlapping.prototype.calculateMaximumSegmentShift = function (previousSegment, nextSegment, directionToShift) {
            var previousLength = Math.abs(previousSegment.sortedEnd - previousSegment.sortedStart);
            var nextLength = Math.abs(nextSegment.sortedEnd - nextSegment.sortedStart);
            var connector = this.connectorMappings.get(previousSegment);
            Eif (connector) {
                if (!previousSegment.previous) {
                    var sourceDecoratorLength = 5;
                    Iif (connector.sourceDecorator.shape !== 'None') {
                        sourceDecoratorLength = Math.max(connector.sourceDecorator.width, connector.sourceDecorator.height);
                    }
                    previousLength -= sourceDecoratorLength + 5;
                }
                if (!nextSegment.next) {
                    var targetDecoratorLength = 5;
                    Eif (connector.targetDecorator.shape !== 'None') {
                        targetDecoratorLength = Math.max(connector.targetDecorator.width, connector.targetDecorator.height);
                    }
                    nextLength -= targetDecoratorLength + 5;
                }
            }
            if (previousSegment.direction === nextSegment.direction) {
                return directionToShift === nextSegment.direction ? nextLength : previousLength;
            }
            else {
                return directionToShift === nextSegment.direction ? Math.min(previousLength, nextLength) : Number.MAX_VALUE;
            }
        };
        AvoidLineOverlapping.prototype.destroy = function () {
        };
        AvoidLineOverlapping.prototype.getModuleName = function () {
            return 'AvoidLineOverlapping';
        };
        AvoidLineOverlapping.ConnectorSpacing = 5;
        AvoidLineOverlapping.maxReRouteLimit = 5;
        return AvoidLineOverlapping;
    }());
    exports.AvoidLineOverlapping = AvoidLineOverlapping;
});