Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rectangle.equalsEpsilon #6533

Merged
merged 3 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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