From 83f55b1fb1ccfe7558e2ac7c9b116a8874c8fb1c Mon Sep 17 00:00:00 2001 From: Nico Rehwaldt Date: Fri, 28 Apr 2017 16:25:53 +0200 Subject: [PATCH] fix(modeling): handle label layouting edge case Related to #669 --- .../modeling/behavior/util/LabelLayoutUtil.js | 5 +- .../behavior/util/LineAttachmentUtil.js | 26 +------ .../behavior/util/LabelLayoutUtilSpec.js | 75 +++++++++++++++++++ 3 files changed, 82 insertions(+), 24 deletions(-) create mode 100644 test/spec/features/modeling/behavior/util/LabelLayoutUtilSpec.js diff --git a/lib/features/modeling/behavior/util/LabelLayoutUtil.js b/lib/features/modeling/behavior/util/LabelLayoutUtil.js index e7cc8d73..0f41022f 100644 --- a/lib/features/modeling/behavior/util/LabelLayoutUtil.js +++ b/lib/features/modeling/behavior/util/LabelLayoutUtil.js @@ -112,7 +112,7 @@ function getLabelAdjustment(label, newWaypoints, oldWaypoints, hints) { oldLabelLineIndex = attachment.segmentIndex, newLabelLineIndex = findNewLabelLineStartIndex(oldWaypoints, newWaypoints, attachment, hints); - if ( newLabelLineIndex === null ) { + if (newLabelLineIndex === null) { return { x: x, y: y }; } @@ -210,8 +210,9 @@ function getLine(waypoints, idx) { } function getRelativeFootPosition(line, foot) { + var length = getDistancePointPoint(line[0], line[1]), lengthToFoot = getDistancePointPoint(line[0], foot); - return lengthToFoot / length; + return length === 0 ? 0 : lengthToFoot / length; } diff --git a/lib/features/modeling/behavior/util/LineAttachmentUtil.js b/lib/features/modeling/behavior/util/LineAttachmentUtil.js index e490d2da..34d86d17 100644 --- a/lib/features/modeling/behavior/util/LineAttachmentUtil.js +++ b/lib/features/modeling/behavior/util/LineAttachmentUtil.js @@ -151,10 +151,12 @@ function getCircleSegmentIntersections(s1, s2, cc, cr) { var disc = pBy2 * pBy2 - q; - // round disc to 10 digits to work around + // check against negative value to work around // negative, very close to zero results (-4e-15) // being produced in some environments - disc = round(disc, 10); + if (disc < 0 && disc > -0.000001) { + disc = 0; + } if (disc < 0) { return []; @@ -220,26 +222,6 @@ function mid(p1, p2) { }; } -/** - * Round number to precision - * - * @param {Number} number - * @param {Integer} [precision=1] - * - * @return {Number} - */ -function round(number, precision) { - - if (typeof precision === 'undefined') { - return Math.round(number); - } - - var factor = Math.pow(10, precision); - var tempNumber = number * factor; - var roundedTempNumber = Math.round(tempNumber); - return roundedTempNumber / factor; -} - var EQUAL_THRESHOLD = 0.1; function pointsEqual(p1, p2) { diff --git a/test/spec/features/modeling/behavior/util/LabelLayoutUtilSpec.js b/test/spec/features/modeling/behavior/util/LabelLayoutUtilSpec.js new file mode 100644 index 00000000..b4afc458 --- /dev/null +++ b/test/spec/features/modeling/behavior/util/LabelLayoutUtilSpec.js @@ -0,0 +1,75 @@ +'use strict'; + +var getLabelAdjustment = require('lib/features/modeling/behavior/util/LabelLayoutUtil').getLabelAdjustment; + + +describe('modeling/behavior/util - LabelLayoutUtil#getLabelAdjustment', function() { + + describe('should recognize on the line label', function() { + + var newLine = [ + { x: 10, y: 10 }, + // - + { x: 15, y: 10 }, + // | + { x: 15, y: 5 }, + // - + { x: 30, y: 5 } + ]; + + + it('horizontal', function() { + + // given + var line = [ + { x: 10, y: 10 }, + // - + { x: 20, y: 10 } + ]; + + // label with center { x: 5, y: 10 } + + var label = { + x: 0, + y: 5, + width: 10, + height: 10 + }; + + // when + var delta = getLabelAdjustment(label, newLine, line, { connectionStart: true }); + + // then + expect(delta).to.eql({ x: 0, y: 0 }); + }); + + + it('zero-length line', function() { + + // given + var line = [ + { x: 10, y: 10 }, + // - + { x: 10, y: 10 } + ]; + + // label with center { x: 5, y: 10 } + + var label = { + x: 0, + y: 5, + width: 10, + height: 10 + }; + + // when + var delta = getLabelAdjustment(label, newLine, line, { connectionStart: true }); + + // then + expect(delta).to.eql({ x: 0, y: 0 }); + }); + + }); + + +});