From 1575cd5952b2c0a34e180b893b92683a7472b7a6 Mon Sep 17 00:00:00 2001 From: hpinkos Date: Tue, 1 May 2018 12:38:07 -0400 Subject: [PATCH 1/2] add Rectangle.equalsEpsilon --- CHANGES.md | 5 +++++ Source/Core/Rectangle.js | 30 +++++++++++++++++++++++++----- Specs/Core/RectangleSpec.js | 16 ++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index dc0c56274c77..50d73805a7e3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,11 @@ Change Log ========== +### 1.46 - 2018-06-01 + +##### Additions :tada: +* Added `Rectangle.equalsEpsilon` + ### 1.45 - 2018-05-01 ##### Major Announcements :loudspeaker: diff --git a/Source/Core/Rectangle.js b/Source/Core/Rectangle.js index 70d5dd5a48a1..c7496c9420b6 100644 --- a/Source/Core/Rectangle.js +++ b/Source/Core/Rectangle.js @@ -365,6 +365,30 @@ define([ return result; }; + /** + * Compares the provided Rectangles componentwise and returns + * true if they pass an absolute or relative tolerance test, + * false otherwise. + * + * @param {Rectangle} [left] The first Rectangle. + * @param {Rectangle} [right] The second Rectangle. + * @param {Number} absoluteEpsilon The absolute epsilon tolerance to use for equality testing. + * @returns {Boolean} true if left and right are within the provided epsilon, false otherwise. + */ + Rectangle.equalsEpsilon = function(left, right, absoluteEpsilon) { + //>>includeStart('debug', pragmas.debug); + Check.typeOf.number('absoluteEpsilon', absoluteEpsilon); + //>>includeEnd('debug'); + + return (left === right) || + (defined(left) && + defined(right) && + (Math.abs(left.west - right.west) <= absoluteEpsilon) && + (Math.abs(left.south - right.south) <= absoluteEpsilon) && + (Math.abs(left.east - right.east) <= absoluteEpsilon) && + (Math.abs(left.north - right.north) <= absoluteEpsilon)); + }; + /** * Duplicates this Rectangle. * @@ -418,11 +442,7 @@ define([ Check.typeOf.number('epsilon', epsilon); //>>includeEnd('debug'); - return defined(other) && - (Math.abs(this.west - other.west) <= epsilon) && - (Math.abs(this.south - other.south) <= epsilon) && - (Math.abs(this.east - other.east) <= epsilon) && - (Math.abs(this.north - other.north) <= epsilon); + return Rectangle.equalsEpsilon(this, other, epsilon); }; /** diff --git a/Specs/Core/RectangleSpec.js b/Specs/Core/RectangleSpec.js index dd0580ebdb39..b85022978f03 100644 --- a/Specs/Core/RectangleSpec.js +++ b/Specs/Core/RectangleSpec.js @@ -248,6 +248,22 @@ defineSuite([ expect(Rectangle.equals(rectangle, undefined)).toEqual(false); }); + it('Static equals epsilon works in all cases', function() { + var rectangle1 = new Rectangle(0.1, 0.2, 0.3, 0.4); + expect(Rectangle.equalsEpsilon(rectangle1, new Rectangle(0.1, 0.2, 0.3, 0.4), 0.0)).toEqual(true); + expect(Rectangle.equalsEpsilon(rectangle1, new Rectangle(0.5, 0.2, 0.3, 0.4), 0.0)).toEqual(false); + expect(Rectangle.equalsEpsilon(rectangle1, new Rectangle(0.1, 0.5, 0.3, 0.4), 0.0)).toEqual(false); + expect(Rectangle.equalsEpsilon(rectangle1, new Rectangle(0.1, 0.2, 0.5, 0.4), 0.0)).toEqual(false); + expect(Rectangle.equalsEpsilon(rectangle1, new Rectangle(0.1, 0.2, 0.3, 0.5), 0.0)).toEqual(false); + expect(Rectangle.equalsEpsilon(rectangle1, new Rectangle(0.5, 0.2, 0.3, 0.4), 0.4)).toEqual(true); + expect(Rectangle.equalsEpsilon(rectangle1, new Rectangle(0.1, 0.5, 0.3, 0.4), 0.3)).toEqual(true); + expect(Rectangle.equalsEpsilon(rectangle1, new Rectangle(0.1, 0.2, 0.5, 0.4), 0.2)).toEqual(true); + expect(Rectangle.equalsEpsilon(rectangle1, new Rectangle(0.1, 0.2, 0.3, 0.5), 0.1)).toEqual(true); + expect(Rectangle.equalsEpsilon(rectangle1, undefined, 0.0)).toEqual(false); + expect(Rectangle.equalsEpsilon(undefined, rectangle1, 0.0)).toEqual(false); + expect(Rectangle.equalsEpsilon(rectangle1, rectangle1, 0.0)).toEqual(true); + }); + it('Equals epsilon works in all cases', function() { var rectangle = new Rectangle(0.1, 0.2, 0.3, 0.4); expect(rectangle.equalsEpsilon(new Rectangle(0.1, 0.2, 0.3, 0.4), 0.0)).toEqual(true); From 2204f62492af99471a4c9f29767d9d0fb9e92a34 Mon Sep 17 00:00:00 2001 From: Hannah Date: Mon, 7 May 2018 16:46:21 -0400 Subject: [PATCH 2/2] Update CHANGES.md --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index f22c32473534..eae7d53fa1a4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,7 +16,7 @@ Change Log * Added `PostProcessStage` which takes a fragment shader that processes the color and depth texture from the stage run before it. * Added `PostProcessStageComposite` for multi-stage post-processes like depth of field. * Added a new Sandcastle label `Post Processing` to showcase the different built-in post-process stages. - * Added `Rectangle.equalsEpsilon` [#6533](https://github.com/AnalyticalGraphicsInc/cesium/pull/6533) + * Added `Rectangle.equalsEpsilon` for comparing the equality of two rectangles [#6533](https://github.com/AnalyticalGraphicsInc/cesium/pull/6533) ##### Fixes :wrench: * Fixed a bug causing custom TilingScheme classes to not be able to use a GeographicProjection. [#6524](https://github.com/AnalyticalGraphicsInc/cesium/pull/6524)