Skip to content

Commit

Permalink
Merge pull request #6533 from AnalyticalGraphicsInc/rectangle-equals-…
Browse files Browse the repository at this point in the history
…epsilon

Add Rectangle.equalsEpsilon
  • Loading branch information
ggetz authored May 8, 2018
2 parents 8a5c805 + 2204f62 commit 404f286
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 25 additions & 5 deletions Source/Core/Rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,30 @@ define([
return result;
};

/**
* Compares the provided Rectangles componentwise and returns
* <code>true</code> if they pass an absolute or relative tolerance test,
* <code>false</code> 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} <code>true</code> if left and right are within the provided epsilon, <code>false</code> 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.
*
Expand Down Expand Up @@ -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);
};

/**
Expand Down
16 changes: 16 additions & 0 deletions Specs/Core/RectangleSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 404f286

Please sign in to comment.