Fix home bounds with rotation. Fix #567 and #463

This commit is contained in:
Antoine Vandecreme 2015-12-05 19:18:56 -05:00
parent 5e362554e2
commit 1d04ceadff
4 changed files with 73 additions and 46 deletions

View File

@ -175,7 +175,7 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
$.console.assert(bounds.width > 0, "[Viewport.setHomeBounds] bounds.width must be greater than 0"); $.console.assert(bounds.width > 0, "[Viewport.setHomeBounds] bounds.width must be greater than 0");
$.console.assert(bounds.height > 0, "[Viewport.setHomeBounds] bounds.height must be greater than 0"); $.console.assert(bounds.height > 0, "[Viewport.setHomeBounds] bounds.height must be greater than 0");
this.homeBounds = bounds.clone(); this.homeBounds = bounds.clone().rotate(this.degrees).getBoundingBox();
this.contentSize = this.homeBounds.getSize().times(contentFactor); this.contentSize = this.homeBounds.getSize().times(contentFactor);
this.contentAspectX = this.contentSize.x / this.contentSize.y; this.contentAspectX = this.contentSize.x / this.contentSize.y;
this.contentAspectY = this.contentSize.y / this.contentSize.x; this.contentAspectY = this.contentSize.y / this.contentSize.x;
@ -808,12 +808,18 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
* @return {OpenSeadragon.Viewport} Chainable. * @return {OpenSeadragon.Viewport} Chainable.
*/ */
setRotation: function(degrees) { setRotation: function(degrees) {
if( !( this.viewer && this.viewer.drawer.canRotate() ) ) { if (!this.viewer || !this.viewer.drawer.canRotate()) {
return this; return this;
} }
degrees = ( degrees + 360 ) % 360; degrees = degrees % 360;
if (degrees < 0) {
degrees += 360;
}
this.degrees = degrees; this.degrees = degrees;
this.setHomeBounds(
this.viewer.world.getHomeBounds(),
this.viewer.world.getContentFactor());
this.viewer.forceRedraw(); this.viewer.forceRedraw();
/** /**
@ -826,10 +832,7 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{
* @property {Number} degrees - The number of degrees the rotation was set to. * @property {Number} degrees - The number of degrees the rotation was set to.
* @property {?Object} userData - Arbitrary subscriber-defined object. * @property {?Object} userData - Arbitrary subscriber-defined object.
*/ */
if (this.viewer !== null)
{
this.viewer.raiseEvent('rotate', {"degrees": degrees}); this.viewer.raiseEvent('rotate', {"degrees": degrees});
}
return this; return this;
}, },

View File

@ -68,6 +68,24 @@
ok( Util.equalsWithVariance( value1, value2, variance ), message + " Expected:" + value1 + " Found: " + value2 + " Variance: " + variance ); ok( Util.equalsWithVariance( value1, value2, variance ), message + " Expected:" + value1 + " Found: " + value2 + " Variance: " + variance );
}, },
// ----------
assertPointsEquals: function (pointA, pointB, precision, message) {
Util.assessNumericValue(pointA.x, pointB.x, precision, message + " x: ");
Util.assessNumericValue(pointA.y, pointB.y, precision, message + " y: ");
},
// ----------
assertRectangleEquals: function (rectA, rectB, precision, message) {
Util.assessNumericValue(rectA.x, rectB.x, precision, message + " x: ");
Util.assessNumericValue(rectA.y, rectB.y, precision, message + " y: ");
Util.assessNumericValue(rectA.width, rectB.width, precision,
message + " width: ");
Util.assessNumericValue(rectA.height, rectB.height, precision,
message + " height: ");
Util.assessNumericValue(rectA.degrees, rectB.degrees, precision,
message + " degrees: ");
},
// ---------- // ----------
timeWatcher: function ( time ) { timeWatcher: function ( time ) {
time = time || 2000; time = time || 2000;

View File

@ -6,22 +6,6 @@
var precision = 0.000000001; var precision = 0.000000001;
function assertPointsEquals(pointA, pointB, message) {
Util.assessNumericValue(pointA.x, pointB.x, precision, message + " x: ");
Util.assessNumericValue(pointA.y, pointB.y, precision, message + " y: ");
}
function assertRectangleEquals(rectA, rectB, message) {
Util.assessNumericValue(rectA.x, rectB.x, precision, message + " x: ");
Util.assessNumericValue(rectA.y, rectB.y, precision, message + " y: ");
Util.assessNumericValue(rectA.width, rectB.width, precision,
message + " width: ");
Util.assessNumericValue(rectA.height, rectB.height, precision,
message + " height: ");
Util.assessNumericValue(rectA.degrees, rectB.degrees, precision,
message + " degrees: ");
}
test('Constructor', function() { test('Constructor', function() {
var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 5); var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 5);
strictEqual(rect.x, 1, 'rect.x should be 1'); strictEqual(rect.x, 1, 'rect.x should be 1');
@ -84,7 +68,7 @@
rect.degrees = 45; rect.degrees = 45;
expected = new OpenSeadragon.Point(1 / Math.sqrt(2), 1 / Math.sqrt(2)); expected = new OpenSeadragon.Point(1 / Math.sqrt(2), 1 / Math.sqrt(2));
assertPointsEquals(expected, rect.getTopRight(), Util.assertPointsEquals(expected, rect.getTopRight(), precision,
"Incorrect top right point with rotation."); "Incorrect top right point with rotation.");
}); });
@ -95,7 +79,7 @@
rect.degrees = 45; rect.degrees = 45;
expected = new OpenSeadragon.Point(-1 / Math.sqrt(2), 1 / Math.sqrt(2)); expected = new OpenSeadragon.Point(-1 / Math.sqrt(2), 1 / Math.sqrt(2));
assertPointsEquals(expected, rect.getBottomLeft(), Util.assertPointsEquals(expected, rect.getBottomLeft(), precision,
"Incorrect bottom left point with rotation."); "Incorrect bottom left point with rotation.");
}); });
@ -106,17 +90,17 @@
rect.degrees = 45; rect.degrees = 45;
expected = new OpenSeadragon.Point(0, Math.sqrt(2)); expected = new OpenSeadragon.Point(0, Math.sqrt(2));
assertPointsEquals(expected, rect.getBottomRight(), Util.assertPointsEquals(expected, rect.getBottomRight(), precision,
"Incorrect bottom right point with 45 rotation."); "Incorrect bottom right point with 45 rotation.");
rect.degrees = 90; rect.degrees = 90;
expected = new OpenSeadragon.Point(-1, 1); expected = new OpenSeadragon.Point(-1, 1);
assertPointsEquals(expected, rect.getBottomRight(), Util.assertPointsEquals(expected, rect.getBottomRight(), precision,
"Incorrect bottom right point with 90 rotation."); "Incorrect bottom right point with 90 rotation.");
rect.degrees = 135; rect.degrees = 135;
expected = new OpenSeadragon.Point(-Math.sqrt(2), 0); expected = new OpenSeadragon.Point(-Math.sqrt(2), 0);
assertPointsEquals(expected, rect.getBottomRight(), Util.assertPointsEquals(expected, rect.getBottomRight(), precision,
"Incorrect bottom right point with 135 rotation."); "Incorrect bottom right point with 135 rotation.");
}); });
@ -127,17 +111,17 @@
rect.degrees = 45; rect.degrees = 45;
expected = new OpenSeadragon.Point(0, 0.5 * Math.sqrt(2)); expected = new OpenSeadragon.Point(0, 0.5 * Math.sqrt(2));
assertPointsEquals(expected, rect.getCenter(), Util.assertPointsEquals(expected, rect.getCenter(), precision,
"Incorrect bottom right point with 45 rotation."); "Incorrect bottom right point with 45 rotation.");
rect.degrees = 90; rect.degrees = 90;
expected = new OpenSeadragon.Point(-0.5, 0.5); expected = new OpenSeadragon.Point(-0.5, 0.5);
assertPointsEquals(expected, rect.getCenter(), Util.assertPointsEquals(expected, rect.getCenter(), precision,
"Incorrect bottom right point with 90 rotation."); "Incorrect bottom right point with 90 rotation.");
rect.degrees = 135; rect.degrees = 135;
expected = new OpenSeadragon.Point(-0.5 * Math.sqrt(2), 0); expected = new OpenSeadragon.Point(-0.5 * Math.sqrt(2), 0);
assertPointsEquals(expected, rect.getCenter(), Util.assertPointsEquals(expected, rect.getCenter(), precision,
"Incorrect bottom right point with 135 rotation."); "Incorrect bottom right point with 135 rotation.");
}); });
@ -145,14 +129,16 @@
var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 45); var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 45);
var expected = new OpenSeadragon.Rect(2, 4, 6, 8, 45); var expected = new OpenSeadragon.Rect(2, 4, 6, 8, 45);
var actual = rect.times(2); var actual = rect.times(2);
assertRectangleEquals(expected, actual, "Incorrect x2 rectangles."); Util.assertRectangleEquals(expected, actual, precision,
"Incorrect x2 rectangles.");
}); });
test('translate', function() { test('translate', function() {
var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 45); var rect = new OpenSeadragon.Rect(1, 2, 3, 4, 45);
var expected = new OpenSeadragon.Rect(2, 4, 3, 4, 45); var expected = new OpenSeadragon.Rect(2, 4, 3, 4, 45);
var actual = rect.translate(new OpenSeadragon.Point(1, 2)); var actual = rect.translate(new OpenSeadragon.Point(1, 2));
assertRectangleEquals(expected, actual, "Incorrect translation."); Util.assertRectangleEquals(expected, actual, precision,
"Incorrect translation.");
}); });
test('union', function() { test('union', function() {
@ -160,7 +146,7 @@
var rect2 = new OpenSeadragon.Rect(0, 1, 1, 1); var rect2 = new OpenSeadragon.Rect(0, 1, 1, 1);
var expected = new OpenSeadragon.Rect(0, 1, 4, 4); var expected = new OpenSeadragon.Rect(0, 1, 4, 4);
var actual = rect1.union(rect2); var actual = rect1.union(rect2);
assertRectangleEquals(expected, actual, Util.assertRectangleEquals(expected, actual, precision,
"Incorrect union with horizontal rectangles."); "Incorrect union with horizontal rectangles.");
rect1 = new OpenSeadragon.Rect(0, -Math.sqrt(2), 2, 2, 45); rect1 = new OpenSeadragon.Rect(0, -Math.sqrt(2), 2, 2, 45);
@ -171,7 +157,7 @@
3 + Math.sqrt(2), 3 + Math.sqrt(2),
2 + Math.sqrt(2)); 2 + Math.sqrt(2));
actual = rect1.union(rect2); actual = rect1.union(rect2);
assertRectangleEquals(expected, actual, Util.assertRectangleEquals(expected, actual, precision,
"Incorrect union with non horizontal rectangles."); "Incorrect union with non horizontal rectangles.");
}); });
@ -185,27 +171,27 @@
1, 1,
45); 45);
var actual = rect.rotate(-675); var actual = rect.rotate(-675);
assertRectangleEquals(expected, actual, Util.assertRectangleEquals(expected, actual, precision,
"Incorrect rectangle after rotation of -675deg around center."); "Incorrect rectangle after rotation of -675deg around center.");
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 33); expected = new OpenSeadragon.Rect(0, 0, 2, 1, 33);
actual = rect.rotate(33, rect.getTopLeft()); actual = rect.rotate(33, rect.getTopLeft());
assertRectangleEquals(expected, actual, Util.assertRectangleEquals(expected, actual, precision,
"Incorrect rectangle after rotation of 33deg around topLeft."); "Incorrect rectangle after rotation of 33deg around topLeft.");
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 101); expected = new OpenSeadragon.Rect(0, 0, 2, 1, 101);
actual = rect.rotate(101, rect.getTopLeft()); actual = rect.rotate(101, rect.getTopLeft());
assertRectangleEquals(expected, actual, Util.assertRectangleEquals(expected, actual, precision,
"Incorrect rectangle after rotation of 187deg around topLeft."); "Incorrect rectangle after rotation of 187deg around topLeft.");
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 187); expected = new OpenSeadragon.Rect(0, 0, 2, 1, 187);
actual = rect.rotate(187, rect.getTopLeft()); actual = rect.rotate(187, rect.getTopLeft());
assertRectangleEquals(expected, actual, Util.assertRectangleEquals(expected, actual, precision,
"Incorrect rectangle after rotation of 187deg around topLeft."); "Incorrect rectangle after rotation of 187deg around topLeft.");
expected = new OpenSeadragon.Rect(0, 0, 2, 1, 300); expected = new OpenSeadragon.Rect(0, 0, 2, 1, 300);
actual = rect.rotate(300, rect.getTopLeft()); actual = rect.rotate(300, rect.getTopLeft());
assertRectangleEquals(expected, actual, Util.assertRectangleEquals(expected, actual, precision,
"Incorrect rectangle after rotation of 300deg around topLeft."); "Incorrect rectangle after rotation of 300deg around topLeft.");
}); });
@ -218,17 +204,17 @@
rect.degrees = 90; rect.degrees = 90;
var expected = new OpenSeadragon.Rect(-3, 0, 3, 2); var expected = new OpenSeadragon.Rect(-3, 0, 3, 2);
assertRectangleEquals(expected, rect.getBoundingBox(), Util.assertRectangleEquals(expected, rect.getBoundingBox(), precision,
"Bounding box of rect rotated 90deg."); "Bounding box of rect rotated 90deg.");
rect.degrees = 180; rect.degrees = 180;
var expected = new OpenSeadragon.Rect(-2, -3, 2, 3); var expected = new OpenSeadragon.Rect(-2, -3, 2, 3);
assertRectangleEquals(expected, rect.getBoundingBox(), Util.assertRectangleEquals(expected, rect.getBoundingBox(), precision,
"Bounding box of rect rotated 180deg."); "Bounding box of rect rotated 180deg.");
rect.degrees = 270; rect.degrees = 270;
var expected = new OpenSeadragon.Rect(0, -2, 3, 2); var expected = new OpenSeadragon.Rect(0, -2, 3, 2);
assertRectangleEquals(expected, rect.getBoundingBox(), Util.assertRectangleEquals(expected, rect.getBoundingBox(), precision,
"Bounding box of rect rotated 270deg."); "Bounding box of rect rotated 270deg.");
}); });

View File

@ -218,6 +218,26 @@
}); });
}); });
asyncTest('getHomeBoundsWithRotation', function() {
function openHandler() {
viewer.removeHandler('open', openHandler);
var viewport = viewer.viewport;
viewport.setRotation(-675);
Util.assertRectangleEquals(
viewport.getHomeBounds(),
new OpenSeadragon.Rect(
(1 - Math.sqrt(2)) / 2,
(1 - Math.sqrt(2)) / 2,
Math.sqrt(2),
Math.sqrt(2)),
0.00000001,
"Test getHomeBounds with degrees = -675");
start();
}
viewer.addHandler('open', openHandler);
viewer.open(DZI_PATH);
});
asyncTest('getHomeZoom', function() { asyncTest('getHomeZoom', function() {
reopenViewerHelper({ reopenViewerHelper({
property: 'defaultZoomLevel', property: 'defaultZoomLevel',