From 66517dab8de289c88592550b56ca9630f299527c Mon Sep 17 00:00:00 2001 From: Ian Gilman Date: Mon, 24 Nov 2014 11:46:33 -0800 Subject: [PATCH] Coordinate conversion rounding errors were causing test breakages; fixed --- src/tiledimage.js | 37 ++++++++++++++++++++++++------------- src/viewport.js | 37 +++++++++++++++++++++++++------------ 2 files changed, 49 insertions(+), 25 deletions(-) diff --git a/src/tiledimage.js b/src/tiledimage.js index c345765b..446adb4a 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -184,6 +184,12 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag return new $.Point(this.source.dimensions.x, this.source.dimensions.y); }, + // private + _viewportToImageDelta: function( viewerX, viewerY ) { + return new $.Point(viewerX * (this.source.dimensions.x / this._scale), + viewerY * ((this.source.dimensions.y * this.contentAspectX) / this._scale)); + }, + /** * Translates from OpenSeadragon viewer coordinate system to image coordinate system. * This method can be called either by passing X,Y coordinates or an @@ -200,9 +206,13 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag return this.viewportToImageCoordinates( viewerX.x, viewerX.y ); } - var contentSize = this.source.dimensions; - return new $.Point((viewerX - this._worldX) * (contentSize.x / this._scale), - (viewerY - this._worldY) * ((contentSize.y * this.contentAspectX) / this._scale)); + return this._viewportToImageDelta(viewerX - this._worldX, viewerY - this._worldY); + }, + + // private + _imageToViewportDelta: function( imageX, imageY ) { + return new $.Point((imageX / this.source.dimensions.x) * this._scale, + (imageY / this.source.dimensions.y / this.contentAspectX) * this._scale); }, /** @@ -221,9 +231,10 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag return this.imageToViewportCoordinates( imageX.x, imageX.y ); } - var contentSize = this.source.dimensions; - return new $.Point(this._worldX + ((imageX / contentSize.x) * this._scale), - this._worldY + ((imageY / contentSize.y / this.contentAspectX) * this._scale)); + var point = this._imageToViewportDelta(imageX, imageY); + point.x += this._worldX; + point.y += this._worldY; + return point; }, /** @@ -254,14 +265,14 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag coordA = this.imageToViewportCoordinates( imageX, imageY ); - coordB = this.imageToViewportCoordinates( - imageX + pixelWidth, imageY + pixelHeight + coordB = this._imageToViewportDelta( + pixelWidth, pixelHeight ); return new $.Rect( coordA.x, coordA.y, - coordB.x - coordA.x, - coordB.y - coordA.y + coordB.x, + coordB.y ); }, @@ -291,12 +302,12 @@ $.extend($.TiledImage.prototype, $.EventSource.prototype, /** @lends OpenSeadrag ); } coordA = this.viewportToImageCoordinates( viewerX, viewerY ); - coordB = this.viewportToImageCoordinates(viewerX + pointWidth, viewerY + pointHeight); + coordB = this._viewportToImageDelta(pointWidth, pointHeight); return new $.Rect( coordA.x, coordA.y, - coordB.x - coordA.x, - coordB.y - coordA.y + coordB.x, + coordB.y ); }, diff --git a/src/viewport.js b/src/viewport.js index c56ef7f0..a9edabe8 100644 --- a/src/viewport.js +++ b/src/viewport.js @@ -938,6 +938,13 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{ ); }, + // private + _viewportToImageDelta: function( viewerX, viewerY ) { + var scale = this.homeBounds.width; + return new $.Point(viewerX * (this.contentSize.x / scale), + viewerY * ((this.contentSize.y * this.contentAspectX) / scale)); + }, + /** * Translates from OpenSeadragon viewer coordinate system to image coordinate system. * This method can be called either by passing X,Y coordinates or an @@ -959,9 +966,14 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{ $.console.error('[Viewport.viewportToImageCoordinates] is not accurate with multi-image; use TiledImage.viewportToImageCoordinates instead.'); } + return this._viewportToImageDelta(viewerX - this.homeBounds.x, viewerY - this.homeBounds.y); + }, + + // private + _imageToViewportDelta: function( imageX, imageY ) { var scale = this.homeBounds.width; - return new $.Point((viewerX - this.homeBounds.x) * (this.contentSize.x / scale), - (viewerY - this.homeBounds.y) * ((this.contentSize.y * this.contentAspectX) / scale)); + return new $.Point((imageX / this.contentSize.x) * scale, + (imageY / this.contentSize.y / this.contentAspectX) * scale); }, /** @@ -985,9 +997,10 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{ $.console.error('[Viewport.imageToViewportCoordinates] is not accurate with multi-image; use TiledImage.imageToViewportCoordinates instead.'); } - var scale = this.homeBounds.width; - return new $.Point(this.homeBounds.x + ((imageX / this.contentSize.x) * scale), - this.homeBounds.y + ((imageY / this.contentSize.y / this.contentAspectX) * scale)); + var point = this._imageToViewportDelta(imageX, imageY); + point.x += this.homeBounds.x; + point.y += this.homeBounds.y; + return point; }, /** @@ -1020,14 +1033,14 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{ coordA = this.imageToViewportCoordinates( imageX, imageY ); - coordB = this.imageToViewportCoordinates( - imageX + pixelWidth, imageY + pixelHeight + coordB = this._imageToViewportDelta( + pixelWidth, pixelHeight ); return new $.Rect( coordA.x, coordA.y, - coordB.x - coordA.x, - coordB.y - coordA.y + coordB.x, + coordB.y ); }, @@ -1059,12 +1072,12 @@ $.Viewport.prototype = /** @lends OpenSeadragon.Viewport.prototype */{ } coordA = this.viewportToImageCoordinates( viewerX, viewerY ); - coordB = this.viewportToImageCoordinates(viewerX + pointWidth, viewerY + pointHeight); + coordB = this._viewportToImageDelta(pointWidth, pointHeight); return new $.Rect( coordA.x, coordA.y, - coordB.x - coordA.x, - coordB.y - coordA.y + coordB.x, + coordB.y ); },