From e9fa08fccb436184de499a367f7bd13a8239f8bf Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Sun, 3 Mar 2013 20:53:13 +0100 Subject: [PATCH] Use in-place transforms where possible --- src/ol/control/mousepositioncontrol.js | 5 +++-- src/ol/geolocation.js | 10 ++++++---- src/ol/projection.js | 19 ++++++++++++++++++- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/ol/control/mousepositioncontrol.js b/src/ol/control/mousepositioncontrol.js index 0f3cfb9400..60a667f4a0 100644 --- a/src/ol/control/mousepositioncontrol.js +++ b/src/ol/control/mousepositioncontrol.js @@ -178,8 +178,9 @@ ol.control.MousePosition.prototype.updateHTML_ = function(pixel) { var map = this.getMap(); var coordinate = map.getCoordinateFromPixel(pixel); if (!goog.isNull(coordinate)) { - var output = this.transform_([coordinate.x, coordinate.y]); - coordinate = new ol.Coordinate(output[0], output[1]); + var vertex = [coordinate.x, coordinate.y]; + vertex = this.transform_(vertex, vertex); + coordinate = new ol.Coordinate(vertex[0], vertex[1]); if (goog.isDef(this.coordinateFormat_)) { html = this.coordinateFormat_(coordinate); } else { diff --git a/src/ol/geolocation.js b/src/ol/geolocation.js index bb90723f15..5920f92328 100644 --- a/src/ol/geolocation.js +++ b/src/ol/geolocation.js @@ -79,9 +79,10 @@ ol.Geolocation.prototype.handleProjectionChanged_ = function() { this.transformFn_ = ol.projection.getTransform( ol.projection.getFromCode('EPSG:4326'), projection); if (!goog.isNull(this.position_)) { - var output = this.transformFn_([this.position_.x, this.position_.y]); + var vertex = [this.position_.x, this.position_.y]; + vertex = this.transformFn_(vertex, vertex, 2); this.set(ol.GeolocationProperty.POSITION, - new ol.Coordinate(output[0], output[1])); + new ol.Coordinate(vertex[0], vertex[1])); } } }; @@ -109,9 +110,10 @@ ol.Geolocation.prototype.positionChange_ = function(position) { this.set(ol.GeolocationProperty.HEADING, goog.isNull(coords.heading) ? undefined : goog.math.toRadians(coords.heading)); this.position_ = new ol.Coordinate(coords.longitude, coords.latitude); - var output = this.transformFn_([coords.longitude, coords.latitude]); + var vertex = [coords.longitude, coords.latitude]; + vertex = this.transformFn_(vertex, vertex, 2); this.set(ol.GeolocationProperty.POSITION, - new ol.Coordinate(output[0], output[1])); + new ol.Coordinate(vertex[0], vertex[1])); this.set(ol.GeolocationProperty.SPEED, goog.isNull(coords.speed) ? undefined : coords.speed); }; diff --git a/src/ol/projection.js b/src/ol/projection.js index 86ac70b6a5..30cc5deb2c 100644 --- a/src/ol/projection.js +++ b/src/ol/projection.js @@ -458,6 +458,14 @@ ol.projection.getTransformFromCodes = function(sourceCode, destinationCode) { * @return {Array.} Input coordinate array (same array as input). */ ol.projection.identityTransform = function(input, opt_output, opt_dimension) { + if (goog.isDef(opt_output) && input !== opt_output) { + // TODO: consider making this a warning instead + goog.asserts.assert(false, 'This should not be used internally.'); + for (var i = 0, ii = input.length; i < ii; ++i) { + opt_output[i] = input[i]; + } + input = opt_output; + } return input; }; @@ -470,7 +478,16 @@ ol.projection.identityTransform = function(input, opt_output, opt_dimension) { * values). */ ol.projection.cloneTransform = function(input, opt_output, opt_dimension) { - return input.slice(); + var output; + if (goog.isDef(opt_output)) { + for (var i = 0, ii = input.length; i < ii; ++i) { + opt_output[i] = input[i]; + } + output = opt_output; + } else { + output = input.slice(); + } + return output; };