diff --git a/CHANGES.md b/CHANGES.md index 3de3106365cb..eae7d53fa1a4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -16,6 +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` 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) 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);