openseadragon/test/modules/rectangle.js

304 lines
14 KiB
JavaScript
Raw Permalink Normal View History

2017-12-07 04:20:39 +03:00
/* global QUnit, Util */
2015-11-27 01:25:50 +03:00
(function() {
2017-12-07 04:20:39 +03:00
QUnit.module('Rectangle', {});
2015-11-27 01:25:50 +03:00
var precision = 0.000000001;
2017-12-07 04:20:39 +03:00
QUnit.test('Constructor', function(assert) {
2015-12-01 03:11:41 +03:00
var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 5);
2017-12-07 04:20:39 +03:00
assert.strictEqual(rect.x, 1, 'rect.x should be 1');
assert.strictEqual(rect.y, 2, 'rect.y should be 2');
assert.strictEqual(rect.width, 3, 'rect.width should be 3');
assert.strictEqual(rect.height, 4, 'rect.height should be 4');
assert.strictEqual(rect.degrees, 5, 'rect.degrees should be 5');
2015-11-27 01:25:50 +03:00
2015-12-01 03:11:41 +03:00
rect = new OpenSeadragon.Rect();
2017-12-07 04:20:39 +03:00
assert.strictEqual(rect.x, 0, 'rect.x should be 0');
assert.strictEqual(rect.y, 0, 'rect.y should be 0');
assert.strictEqual(rect.width, 0, 'rect.width should be 0');
assert.strictEqual(rect.height, 0, 'rect.height should be 0');
assert.strictEqual(rect.degrees, 0, 'rect.degrees should be 0');
2015-12-06 01:51:21 +03:00
rect = new OpenSeadragon.Rect(0, 0, 1, 2, -405);
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, Math.sqrt(2) / 2, rect.x, precision,
2015-12-06 01:51:21 +03:00
'rect.x should be sqrt(2)/2');
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, -Math.sqrt(2) / 2, rect.y, precision,
2015-12-06 01:51:21 +03:00
'rect.y should be -sqrt(2)/2');
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, 2, rect.width, precision,
2015-12-06 01:51:21 +03:00
'rect.width should be 2');
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, 1, rect.height, precision,
2015-12-06 01:51:21 +03:00
'rect.height should be 1');
2017-12-07 04:20:39 +03:00
assert.strictEqual(45, rect.degrees, 'rect.degrees should be 45');
2015-12-06 01:51:21 +03:00
rect = new OpenSeadragon.Rect(0, 0, 1, 2, 135);
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, -Math.sqrt(2), rect.x, precision,
2015-12-06 01:51:21 +03:00
'rect.x should be -sqrt(2)');
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, -Math.sqrt(2), rect.y, precision,
2015-12-06 01:51:21 +03:00
'rect.y should be -sqrt(2)');
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, 2, rect.width, precision,
2015-12-06 01:51:21 +03:00
'rect.width should be 2');
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, 1, rect.height, precision,
2015-12-06 01:51:21 +03:00
'rect.height should be 1');
2017-12-07 04:20:39 +03:00
assert.strictEqual(45, rect.degrees, 'rect.degrees should be 45');
2015-12-06 01:51:21 +03:00
rect = new OpenSeadragon.Rect(0, 0, 1, 1, 585);
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, 0, rect.x, precision,
2015-12-06 01:51:21 +03:00
'rect.x should be 0');
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, -Math.sqrt(2), rect.y, precision,
2015-12-06 01:51:21 +03:00
'rect.y should be -sqrt(2)');
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, 1, rect.width, precision,
2015-12-06 01:51:21 +03:00
'rect.width should be 1');
2017-12-07 04:20:39 +03:00
Util.assessNumericValue(assert, 1, rect.height, precision,
2015-12-06 01:51:21 +03:00
'rect.height should be 1');
2017-12-07 04:20:39 +03:00
assert.strictEqual(45, rect.degrees, 'rect.degrees should be 45');
2015-11-27 01:25:50 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('getTopLeft', function(assert) {
2015-12-01 03:11:41 +03:00
var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 5);
2015-11-27 01:25:50 +03:00
var expected = new OpenSeadragon.Point(1, 2);
2017-12-07 04:20:39 +03:00
assert.ok(expected.equals(rect.getTopLeft()), "Incorrect top left point.");
2015-11-27 01:25:50 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('getTopRight', function(assert) {
2015-12-01 03:11:41 +03:00
var rect = new OpenSeadragon.Rect(0, 0, 1, 3);
2015-11-27 01:25:50 +03:00
var expected = new OpenSeadragon.Point(1, 0);
2017-12-07 04:20:39 +03:00
assert.ok(expected.equals(rect.getTopRight()), "Incorrect top right point.");
2015-11-27 01:25:50 +03:00
rect.degrees = 45;
expected = new OpenSeadragon.Point(1 / Math.sqrt(2), 1 / Math.sqrt(2));
2017-12-07 04:20:39 +03:00
Util.assertPointsEquals(assert, expected, rect.getTopRight(), precision,
2015-12-06 01:51:21 +03:00
"Incorrect top right point with rotation.");
2015-11-27 01:25:50 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('getBottomLeft', function(assert) {
2015-12-01 03:11:41 +03:00
var rect = new OpenSeadragon.Rect(0, 0, 3, 1);
2015-11-27 01:25:50 +03:00
var expected = new OpenSeadragon.Point(0, 1);
2017-12-07 04:20:39 +03:00
assert.ok(expected.equals(rect.getBottomLeft()), "Incorrect bottom left point.");
2015-11-27 01:25:50 +03:00
rect.degrees = 45;
expected = new OpenSeadragon.Point(-1 / Math.sqrt(2), 1 / Math.sqrt(2));
2017-12-07 04:20:39 +03:00
Util.assertPointsEquals(assert, expected, rect.getBottomLeft(), precision,
2015-12-06 01:51:21 +03:00
"Incorrect bottom left point with rotation.");
2015-11-27 01:25:50 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('getBottomRight', function(assert) {
2015-12-01 03:11:41 +03:00
var rect = new OpenSeadragon.Rect(0, 0, 1, 1);
2015-11-27 01:25:50 +03:00
var expected = new OpenSeadragon.Point(1, 1);
2017-12-07 04:20:39 +03:00
assert.ok(expected.equals(rect.getBottomRight()), "Incorrect bottom right point.");
2015-11-27 01:25:50 +03:00
rect.degrees = 45;
expected = new OpenSeadragon.Point(0, Math.sqrt(2));
2017-12-07 04:20:39 +03:00
Util.assertPointsEquals(assert, expected, rect.getBottomRight(), precision,
2015-12-06 01:51:21 +03:00
"Incorrect bottom right point with 45 rotation.");
2015-11-27 01:25:50 +03:00
rect.degrees = 90;
expected = new OpenSeadragon.Point(-1, 1);
2017-12-07 04:20:39 +03:00
Util.assertPointsEquals(assert, expected, rect.getBottomRight(), precision,
2015-12-06 01:51:21 +03:00
"Incorrect bottom right point with 90 rotation.");
2015-11-27 01:25:50 +03:00
rect.degrees = 135;
expected = new OpenSeadragon.Point(-Math.sqrt(2), 0);
2017-12-07 04:20:39 +03:00
Util.assertPointsEquals(assert, expected, rect.getBottomRight(), precision,
2015-12-06 01:51:21 +03:00
"Incorrect bottom right point with 135 rotation.");
2015-11-27 01:25:50 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('getCenter', function(assert) {
2015-12-01 03:11:41 +03:00
var rect = new OpenSeadragon.Rect(0, 0, 1, 1);
2015-11-27 01:25:50 +03:00
var expected = new OpenSeadragon.Point(0.5, 0.5);
2017-12-07 04:20:39 +03:00
assert.ok(expected.equals(rect.getCenter()), "Incorrect center point.");
2015-11-27 01:25:50 +03:00
rect.degrees = 45;
expected = new OpenSeadragon.Point(0, 0.5 * Math.sqrt(2));
2017-12-07 04:20:39 +03:00
Util.assertPointsEquals(assert, expected, rect.getCenter(), precision,
2015-12-06 01:51:21 +03:00
"Incorrect bottom right point with 45 rotation.");
2015-11-27 01:25:50 +03:00
rect.degrees = 90;
expected = new OpenSeadragon.Point(-0.5, 0.5);
2017-12-07 04:20:39 +03:00
Util.assertPointsEquals(assert, expected, rect.getCenter(), precision,
2015-12-06 01:51:21 +03:00
"Incorrect bottom right point with 90 rotation.");
2015-11-27 01:25:50 +03:00
rect.degrees = 135;
expected = new OpenSeadragon.Point(-0.5 * Math.sqrt(2), 0);
2017-12-07 04:20:39 +03:00
Util.assertPointsEquals(assert, expected, rect.getCenter(), precision,
2015-12-06 01:51:21 +03:00
"Incorrect bottom right point with 135 rotation.");
});
2017-12-07 04:20:39 +03:00
QUnit.test('times', function(assert) {
2015-12-06 01:51:21 +03:00
var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 45);
var expected = new OpenSeadragon.Rect(2, 4, 6, 8, 45);
var actual = rect.times(2);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
"Incorrect x2 rectangles.");
2015-12-06 01:51:21 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('translate', function(assert) {
2015-12-06 01:51:21 +03:00
var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 45);
var expected = new OpenSeadragon.Rect(2, 4, 3, 4, 45);
var actual = rect.translate(new OpenSeadragon.Point(1, 2));
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
"Incorrect translation.");
2015-11-27 01:25:50 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('union', function(assert) {
2015-12-01 04:44:06 +03:00
var rect1 = new OpenSeadragon.Rect(2, 2, 2, 3);
var rect2 = new OpenSeadragon.Rect(0, 1, 1, 1);
var expected = new OpenSeadragon.Rect(0, 1, 4, 4);
var actual = rect1.union(rect2);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
2015-12-06 01:51:21 +03:00
"Incorrect union with horizontal rectangles.");
2015-12-01 04:44:06 +03:00
rect1 = new OpenSeadragon.Rect(0, -Math.sqrt(2), 2, 2, 45);
rect2 = new OpenSeadragon.Rect(1, 0, 2, 2, 0);
expected = new OpenSeadragon.Rect(
-Math.sqrt(2),
-Math.sqrt(2),
3 + Math.sqrt(2),
2 + Math.sqrt(2));
actual = rect1.union(rect2);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
2015-12-06 01:51:21 +03:00
"Incorrect union with non horizontal rectangles.");
2015-12-01 04:44:06 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('intersection', function(assert) {
var rect1 = new OpenSeadragon.Rect(2, 2, 2, 3);
var rect2 = new OpenSeadragon.Rect(0, 1, 1, 1);
var expected = null;
var actual = rect1.intersection(rect2);
2017-12-07 04:20:39 +03:00
assert.equal(expected, actual,
"Rectangle " + rect2 + " should not intersect " + rect1);
actual = rect2.intersection(rect1);
2017-12-07 04:20:39 +03:00
assert.equal(expected, actual,
"Rectangle " + rect1 + " should not intersect " + rect2);
rect1 = new OpenSeadragon.Rect(0, 0, 2, 1);
rect2 = new OpenSeadragon.Rect(1, 0, 2, 2);
expected = new OpenSeadragon.Rect(1, 0, 1, 1);
actual = rect1.intersection(rect2);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
"Intersection of " + rect2 + " with " + rect1 + " should be " +
expected);
actual = rect2.intersection(rect1);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
"Intersection of " + rect1 + " with " + rect2 + " should be " +
expected);
rect1 = new OpenSeadragon.Rect(0, 0, 3, 3);
rect2 = new OpenSeadragon.Rect(1, 1, 1, 1);
expected = new OpenSeadragon.Rect(1, 1, 1, 1);
actual = rect1.intersection(rect2);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
"Intersection of " + rect2 + " with " + rect1 + " should be " +
expected);
actual = rect2.intersection(rect1);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
"Intersection of " + rect1 + " with " + rect2 + " should be " +
expected);
rect1 = new OpenSeadragon.Rect(2, 2, 2, 3, 45);
rect2 = new OpenSeadragon.Rect(0, 1, 1, 1);
expected = null;
actual = rect1.intersection(rect2);
2017-12-07 04:20:39 +03:00
assert.equal(expected, actual,
"Rectangle " + rect2 + " should not intersect " + rect1);
actual = rect2.intersection(rect1);
2017-12-07 04:20:39 +03:00
assert.equal(expected, actual,
"Rectangle " + rect1 + " should not intersect " + rect2);
rect1 = new OpenSeadragon.Rect(2, 0, 2, 3, 45);
rect2 = new OpenSeadragon.Rect(0, 1, 1, 1);
expected = new OpenSeadragon.Rect(0, 1, 1, 1);
actual = rect1.intersection(rect2);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
"Intersection of " + rect2 + " with " + rect1 + " should be " +
expected);
actual = rect2.intersection(rect1);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
"Intersection of " + rect1 + " with " + rect2 + " should be " +
expected);
});
2017-12-07 04:20:39 +03:00
QUnit.test('rotate', function(assert) {
2015-12-01 03:11:41 +03:00
var rect = new OpenSeadragon.Rect(0, 0, 2, 1);
2015-11-27 01:25:50 +03:00
2015-12-01 03:11:41 +03:00
var expected = new OpenSeadragon.Rect(
1 - 1 / (2 * Math.sqrt(2)),
0.5 - 3 / (2 * Math.sqrt(2)),
2,
1,
45);
2015-12-06 01:51:21 +03:00
var actual = rect.rotate(-675);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
2015-12-06 01:51:21 +03:00
"Incorrect rectangle after rotation of -675deg around center.");
2015-11-27 01:25:50 +03:00
2015-12-01 03:11:41 +03:00
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 33);
2015-11-27 01:25:50 +03:00
actual = rect.rotate(33, rect.getTopLeft());
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
2015-12-06 01:51:21 +03:00
"Incorrect rectangle after rotation of 33deg around topLeft.");
2015-11-27 01:25:50 +03:00
2015-12-01 03:11:41 +03:00
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 101);
2015-11-27 01:25:50 +03:00
actual = rect.rotate(101, rect.getTopLeft());
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
2015-12-06 01:51:21 +03:00
"Incorrect rectangle after rotation of 187deg around topLeft.");
2015-11-27 01:25:50 +03:00
2015-12-01 03:11:41 +03:00
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 187);
2015-11-27 01:25:50 +03:00
actual = rect.rotate(187, rect.getTopLeft());
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
2015-12-06 01:51:21 +03:00
"Incorrect rectangle after rotation of 187deg around topLeft.");
2015-11-27 01:25:50 +03:00
2015-12-01 03:11:41 +03:00
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 300);
2015-11-27 01:25:50 +03:00
actual = rect.rotate(300, rect.getTopLeft());
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, actual, precision,
2015-12-06 01:51:21 +03:00
"Incorrect rectangle after rotation of 300deg around topLeft.");
2015-11-27 01:25:50 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('getBoundingBox', function(assert) {
2015-12-01 03:11:41 +03:00
var rect = new OpenSeadragon.Rect(0, 0, 2, 3);
2015-11-27 01:25:50 +03:00
var bb = rect.getBoundingBox();
2017-12-07 04:20:39 +03:00
assert.ok(rect.equals(bb), "Bounding box of horizontal rectangle should be " +
2015-12-06 01:51:21 +03:00
"identical to rectangle.");
2015-11-27 01:25:50 +03:00
rect.degrees = 90;
2015-12-01 03:11:41 +03:00
var expected = new OpenSeadragon.Rect(-3, 0, 3, 2);
2017-12-07 04:20:39 +03:00
Util.assertRectangleEquals(assert, expected, rect.getBoundingBox(), precision,
2015-12-06 01:51:21 +03:00
"Bounding box of rect rotated 90deg.");
2015-11-27 01:25:50 +03:00
rect.degrees = 180;
2017-12-07 04:20:39 +03:00
expected = new OpenSeadragon.Rect(-2, -3, 2, 3);
Util.assertRectangleEquals(assert, expected, rect.getBoundingBox(), precision,
2015-12-06 01:51:21 +03:00
"Bounding box of rect rotated 180deg.");
2015-11-27 01:25:50 +03:00
rect.degrees = 270;
2017-12-07 04:20:39 +03:00
expected = new OpenSeadragon.Rect(0, -2, 3, 2);
Util.assertRectangleEquals(assert, expected, rect.getBoundingBox(), precision,
2015-12-06 01:51:21 +03:00
"Bounding box of rect rotated 270deg.");
2015-11-27 01:25:50 +03:00
});
2017-12-07 04:20:39 +03:00
QUnit.test('containsPoint', function(assert) {
var rect = new OpenSeadragon.Rect(0, 0, 1, 1, 45);
2017-12-07 04:20:39 +03:00
assert.ok(rect.containsPoint(new OpenSeadragon.Point(0, 0)),
'Point 0,0 should be inside ' + rect);
2017-12-07 04:20:39 +03:00
assert.ok(rect.containsPoint(rect.getTopRight()),
'Top right vertex should be inside ' + rect);
2017-12-07 04:20:39 +03:00
assert.ok(rect.containsPoint(rect.getBottomRight()),
'Bottom right vertex should be inside ' + rect);
2017-12-07 04:20:39 +03:00
assert.ok(rect.containsPoint(rect.getBottomLeft()),
'Bottom left vertex should be inside ' + rect);
2017-12-07 04:20:39 +03:00
assert.ok(rect.containsPoint(rect.getCenter()),
'Center should be inside ' + rect);
2017-12-07 04:20:39 +03:00
assert.notOk(rect.containsPoint(new OpenSeadragon.Point(1, 0)),
'Point 1,0 should not be inside ' + rect);
2017-12-07 04:20:39 +03:00
assert.ok(rect.containsPoint(new OpenSeadragon.Point(0.5, 0.5)),
'Point 0.5,0.5 should be inside ' + rect);
2017-12-07 04:20:39 +03:00
assert.ok(rect.containsPoint(new OpenSeadragon.Point(0.4, 0.5)),
'Point 0.4,0.5 should be inside ' + rect);
2017-12-07 04:20:39 +03:00
assert.notOk(rect.containsPoint(new OpenSeadragon.Point(0.6, 0.5)),
'Point 0.6,0.5 should not be inside ' + rect);
});
2015-11-27 01:25:50 +03:00
})();