From 0d4e17b627e3ef9a9d846f05267c84e3c68ba76c Mon Sep 17 00:00:00 2001 From: Ryan Lester Date: Fri, 2 Sep 2016 10:31:36 -0400 Subject: [PATCH] Use the squared distance when comparing tiles. This drops a Math.sqrt calculation from every tile iterated over while drawing, improving performance. --- src/point.js | 12 ++++++++++++ src/tile.js | 7 ++++--- src/tiledimage.js | 16 ++++++++-------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/point.js b/src/point.js index 7d4f6740..fa35eb18 100644 --- a/src/point.js +++ b/src/point.js @@ -148,6 +148,18 @@ $.Point.prototype = { ); }, + /** + * Compute the squared distance between this point and another point. + * Useful for optimizing things like comparing distances. + * @function + * @param {OpenSeadragon.Point} point The point to compute the distance with. + * @returns {Number} The distance between the 2 points + */ + squaredDistanceTo: function( point ) { + return Math.pow( this.x - point.x, 2 ) + + Math.pow( this.y - point.y, 2 ); + }, + /** * Apply a function to each coordinate of this point and return a new point. * @function diff --git a/src/tile.js b/src/tile.js index 04f3c08a..350cd62e 100644 --- a/src/tile.js +++ b/src/tile.js @@ -154,11 +154,12 @@ $.Tile = function(level, x, y, bounds, exists, url, context2D) { */ this.opacity = null; /** - * The distance of this tile to the viewport center. - * @member {Number} distance + * The squared distance of this tile to the viewport center. + * Use for comparing tiles. + * @member {Number} squaredDistance * @memberof OpenSeadragon.Tile# */ - this.distance = null; + this.squaredDistance = null; /** * The visibility score of this tile. * @member {Number} visibility diff --git a/src/tiledimage.js b/src/tiledimage.js index eff98b6c..c65b0503 100644 --- a/src/tiledimage.js +++ b/src/tiledimage.js @@ -1295,12 +1295,12 @@ function positionTile( tile, overlap, viewport, viewportCenter, levelVisibility, boundsSize.x *= tiledImage._scaleSpring.current.value; boundsSize.y *= tiledImage._scaleSpring.current.value; - var positionC = viewport.pixelFromPointNoRotate(boundsTL, true), - positionT = viewport.pixelFromPointNoRotate(boundsTL, false), - sizeC = viewport.deltaPixelsFromPointsNoRotate(boundsSize, true), - sizeT = viewport.deltaPixelsFromPointsNoRotate(boundsSize, false), - tileCenter = positionT.plus( sizeT.divide( 2 ) ), - tileDistance = viewportCenter.distanceTo( tileCenter ); + var positionC = viewport.pixelFromPointNoRotate(boundsTL, true), + positionT = viewport.pixelFromPointNoRotate(boundsTL, false), + sizeC = viewport.deltaPixelsFromPointsNoRotate(boundsSize, true), + sizeT = viewport.deltaPixelsFromPointsNoRotate(boundsSize, false), + tileCenter = positionT.plus( sizeT.divide( 2 ) ), + tileSquaredDistance = viewportCenter.squaredDistanceTo( tileCenter ); if ( !overlap ) { sizeC = sizeC.plus( new $.Point( 1, 1 ) ); @@ -1308,7 +1308,7 @@ function positionTile( tile, overlap, viewport, viewportCenter, levelVisibility, tile.position = positionC; tile.size = sizeC; - tile.distance = tileDistance; + tile.squaredDistance = tileSquaredDistance; tile.visibility = levelVisibility; } @@ -1452,7 +1452,7 @@ function compareTiles( previousBest, tile ) { if ( tile.visibility > previousBest.visibility ) { return tile; } else if ( tile.visibility == previousBest.visibility ) { - if ( tile.distance < previousBest.distance ) { + if ( tile.squaredDistance < previousBest.squaredDistance ) { return tile; } }