# all files / Github/bpmn-js/lib/features/modeling/behavior/util/ LineAttachmentUtil.js

94.64% Statements 53/56
85.71% Branches 24/28
100% Functions 9/9
94.64% Lines 53/56

    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
    2301×
    1×
    1×
    1×

    1884×

    942×

    115×

    115×

    224×
    224×

    224×
    3×

    221×
    221×

    221×

    221×

    224×

    224×
    86×

    224×

    138×

    138×

    224×

    224×
    142×
    142×

    115×

    221×
    221×
    221×
    221×

    221×
    221×
    221×

    221×
    221×

    221×

    221×

    221×

    221×
    221×
    221×

    221×

    221×
    3×

    218×

    218×
    436×

    436×

    839×

    138×

    1×

    310×

     var sqrt = Math.sqrt,
        min = Math.min,
        max = Math.max,
        abs = Math.abs;

    /**
     * Calculate the square (power to two) of a number.
     *
     * @param {Number} n
     *
     * @return {Number}
     */
    function sq(n) {
      return Math.pow(n, 2);
    }

    /**
     * Get distance between two points.
     *
     * @param {Point} p1
     * @param {Point} p2
     *
     * @return {Number}
     */
    function getDistance(p1, p2) {
      return sqrt(sq(p1.x - p2.x) + sq(p1.y - p2.y));
    }

    /**
     * Return the attachment of the given point on the specified line.
     *
     * The attachment is either a bendpoint (attached to the given point)
     * or segment (attached to a location on a line segment) attachment:
     *
     * ```javascript
     * var pointAttachment = {
     *   type: 'bendpoint',
     *   bendpointIndex: 3,
     *   position: { x: 10, y: 10 } // the attach point on the line
     * };
     *
     * var segmentAttachment = {
     *   type: 'segment',
     *   segmentIndex: 2,
     *   relativeLocation: 0.31, // attach point location between 0 (at start) and 1 (at end)
     *   position: { x: 10, y: 10 } // the attach point on the line
     * };
     * ```
     *
     * @param {Point} point
     * @param {Array<Point>} line
     *
     * @return {Object} attachment
     */
    export function getAttachment(point, line) {

      var idx = 0,
          segmentStart,
          segmentEnd,
          segmentStartDistance,
          segmentEndDistance,
          attachmentPosition,
          minDistance,
          intersections,
          attachment,
          attachmentDistance,
          closestAttachmentDistance,
          closestAttachment;

      for (idx = 0; idx < line.length - 1; idx++) {

        segmentStart = line[idx];
        segmentEnd = line[idx + 1];

        if (pointsEqual(segmentStart, segmentEnd)) {
          intersections = [ segmentStart ];
        } else {
          segmentStartDistance = getDistance(point, segmentStart);
          segmentEndDistance = getDistance(point, segmentEnd);

          minDistance = min(segmentStartDistance, segmentEndDistance);

          intersections = getCircleSegmentIntersections(segmentStart, segmentEnd, point, minDistance);
        }

        Iif (intersections.length < 1) {
          throw new Error('expected between [1, 2] circle -> line intersections');
        }

        // one intersection -> bendpoint attachment
        if (intersections.length === 1) {
          attachment = {
            type: 'bendpoint',
            position: intersections[0],
            segmentIndex: idx,
            bendpointIndex: pointsEqual(segmentStart, intersections[0]) ? idx : idx + 1
          };
        }

        // two intersections -> segment attachment
        if (intersections.length === 2) {

          attachmentPosition = mid(intersections[0], intersections[1]);

          attachment = {
            type: 'segment',
            position: attachmentPosition,
            segmentIndex: idx,
            relativeLocation: getDistance(segmentStart, attachmentPosition) / getDistance(segmentStart, segmentEnd)
          };
        }

        attachmentDistance = getDistance(attachment.position, point);

        if (!closestAttachment || closestAttachmentDistance > attachmentDistance) {
          closestAttachment = attachment;
          closestAttachmentDistance = attachmentDistance;
        }
      }

      return closestAttachment;
    }

    /**
     * Gets the intersection between a circle and a line segment.
     *
     * @param {Point} s1 segment start
     * @param {Point} s2 segment end
     * @param {Point} cc circle center
     * @param {Number} cr circle radius
     *
     * @return {Array<Point>} intersections
     */
    function getCircleSegmentIntersections(s1, s2, cc, cr) {

      var baX = s2.x - s1.x;
      var baY = s2.y - s1.y;
      var caX = cc.x - s1.x;
      var caY = cc.y - s1.y;

      var a = baX * baX + baY * baY;
      var bBy2 = baX * caX + baY * caY;
      var c = caX * caX + caY * caY - cr * cr;

      var pBy2 = bBy2 / a;
      var q = c / a;

      var disc = pBy2 * pBy2 - q;

      // check against negative value to work around
      // negative, very close to zero results (-4e-15)
      // being produced in some environments
      Iif (disc < 0 && disc > -0.000001) {
        disc = 0;
      }

      Iif (disc < 0) {
        return [];
      }

      // if disc == 0 ... dealt with later
      var tmpSqrt = sqrt(disc);
      var abScalingFactor1 = -pBy2 + tmpSqrt;
      var abScalingFactor2 = -pBy2 - tmpSqrt;

      var i1 = {
        x: s1.x - baX * abScalingFactor1,
        y: s1.y - baY * abScalingFactor1
      };

      if (disc === 0) { // abScalingFactor1 == abScalingFactor2
        return [ i1 ];
      }

      var i2 = {
        x: s1.x - baX * abScalingFactor2,
        y: s1.y - baY * abScalingFactor2
      };

      // return only points on line segment
      return [ i1, i2 ].filter(function(p) {
        return isPointInSegment(p, s1, s2);
      });
    }

    function isPointInSegment(p, segmentStart, segmentEnd) {
      return (
        fenced(p.x, segmentStart.x, segmentEnd.x) &&
        fenced(p.y, segmentStart.y, segmentEnd.y)
      );
    }

    function fenced(n, rangeStart, rangeEnd) {

      // use matching threshold to work around
      // precisison errors in intersection computation

      return (
        n >= min(rangeStart, rangeEnd) - EQUAL_THRESHOLD &&
        n <= max(rangeStart, rangeEnd) + EQUAL_THRESHOLD
      );
    }

    /**
     * Calculate mid of two points.
     *
     * @param {Point} p1
     * @param {Point} p2
     *
     * @return {Point}
     */
    function mid(p1, p2) {

      return {
        x: (p1.x + p2.x) / 2,
        y: (p1.y + p2.y) / 2
      };
    }

    var EQUAL_THRESHOLD = 0.1;

    function pointsEqual(p1, p2) {

      return (
        abs(p1.x - p2.x) <= EQUAL_THRESHOLD &&
        abs(p1.y - p2.y) <= EQUAL_THRESHOLD
      );
    }