Coordinate conversion rounding errors were causing test breakages; fixed

This commit is contained in:
Ian Gilman 2014-11-24 11:46:33 -08:00
parent b8a1762a95
commit 66517dab8d
2 changed files with 49 additions and 25 deletions

View File

@ -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
);
},

View File

@ -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
);
},