move pixel to/from coordinate calculation functions from Map to MapRenderer

This commit is contained in:
Éric Lemoine
2012-08-16 15:35:33 +02:00
parent 9eb031ed89
commit 70587af947
2 changed files with 102 additions and 123 deletions

View File

@@ -26,7 +26,6 @@ goog.require('goog.fx.Dragger');
goog.require('goog.fx.anim');
goog.require('goog.fx.anim.Animated');
goog.require('goog.object');
goog.require('goog.vec.Mat4');
goog.require('ol.Collection');
goog.require('ol.Color');
goog.require('ol.Coordinate');
@@ -93,18 +92,6 @@ ol.Map = function(
*/
this.mapToUserTransform_ = ol.Projection.cloneTransform;
/**
* @private
* @type {goog.vec.Mat4.Number}
*/
this.coordinateToPixelMatrix_ = goog.vec.Mat4.createNumber();
/**
* @private
* @type {goog.vec.Mat4.Number}
*/
this.pixelToCoordinateMatrix_ = goog.vec.Mat4.createNumber();
/**
* @private
* @type {boolean}
@@ -123,12 +110,6 @@ ol.Map = function(
*/
this.freezeRenderingCount_ = 0;
/**
* @private
* @type {boolean}
*/
this.matriciesDirty_ = true;
/**
* @private
* @type {Element}
@@ -196,26 +177,10 @@ ol.Map = function(
goog.events.listen(this.viewportSizeMonitor_, goog.events.EventType.RESIZE,
this.handleViewportResize, false, this);
goog.events.listen(
this, ol.Object.getChangedEventType(ol.MapProperty.CENTER),
this.handleCenterChanged, false, this);
goog.events.listen(
this, ol.Object.getChangedEventType(ol.MapProperty.PROJECTION),
this.handleProjectionChanged, false, this);
goog.events.listen(
this, ol.Object.getChangedEventType(ol.MapProperty.RESOLUTION),
this.handleResolutionChanged, false, this);
goog.events.listen(
this, ol.Object.getChangedEventType(ol.MapProperty.ROTATION),
this.handleRotationChanged, false, this);
goog.events.listen(
this, ol.Object.getChangedEventType(ol.MapProperty.SIZE),
this.handleSizeChanged, false, this);
goog.events.listen(
this, ol.Object.getChangedEventType(ol.MapProperty.USER_PROJECTION),
this.handleUserProjectionChanged, false, this);
@@ -291,10 +256,7 @@ goog.exportProperty(
*/
ol.Map.prototype.getCoordinateFromPixel = function(pixel) {
if (this.isDef()) {
this.updateMatrices_();
var vec3 = [pixel.x, pixel.y, 0];
goog.vec.Mat4.multVec3(this.pixelToCoordinateMatrix_, vec3, vec3);
return new ol.Coordinate(vec3[0], vec3[1]);
return this.renderer_.getCoordinateFromPixel(pixel);
} else {
return undefined;
}
@@ -346,10 +308,7 @@ ol.Map.prototype.getLayers = function() {
*/
ol.Map.prototype.getPixelFromCoordinate = function(coordinate) {
if (this.isDef()) {
this.updateMatrices_();
var vec3 = [coordinate.x, coordinate.y, 0];
goog.vec.Mat4.multVec3(this.coordinateToPixelMatrix_, vec3, vec3);
return new ol.Pixel(vec3[0], vec3[1]);
return this.renderer_.getPixelFromCoordinate(coordinate);
} else {
return undefined;
}
@@ -509,14 +468,6 @@ ol.Map.prototype.handleBrowserEvent = function(browserEvent, opt_type) {
};
/**
* @protected
*/
ol.Map.prototype.handleCenterChanged = function() {
this.matriciesDirty_ = true;
};
/**
* @param {goog.fx.DragEvent} dragEvent Drag event.
*/
@@ -534,30 +485,6 @@ ol.Map.prototype.handleProjectionChanged = function() {
};
/**
* @protected
*/
ol.Map.prototype.handleResolutionChanged = function() {
this.matriciesDirty_ = true;
};
/**
* @protected
*/
ol.Map.prototype.handleRotationChanged = function() {
this.matriciesDirty_ = true;
};
/**
* @protected
*/
ol.Map.prototype.handleSizeChanged = function() {
this.matriciesDirty_ = true;
};
/**
* @protected
*/
@@ -733,50 +660,6 @@ goog.exportProperty(
ol.Map.prototype.setUserProjection);
/**
* @private
*/
ol.Map.prototype.updateMatrices_ = function() {
if (this.matriciesDirty_) {
var center = /** @type {!ol.Coordinate} */ this.getCenter();
var resolution = /** @type {number} */ this.getResolution();
var rotation = this.getRotation();
var size = /** @type {!ol.Size} */ this.getSize();
goog.vec.Mat4.makeIdentity(this.coordinateToPixelMatrix_);
goog.vec.Mat4.translate(this.coordinateToPixelMatrix_,
size.width / 2,
size.height / 2,
0);
goog.vec.Mat4.scale(this.coordinateToPixelMatrix_,
1 / resolution,
-1 / resolution,
1);
if (this.canRotate() && goog.isDef(rotation)) {
goog.vec.Mat4.rotate(this.coordinateToPixelMatrix_,
-rotation,
0,
0,
1);
}
goog.vec.Mat4.translate(this.coordinateToPixelMatrix_,
-center.x,
-center.y,
0);
var inverted = goog.vec.Mat4.invert(
this.coordinateToPixelMatrix_, this.pixelToCoordinateMatrix_);
goog.asserts.assert(inverted);
this.matriciesDirty_ = false;
}
};
/**
*/
ol.Map.prototype.render = function() {

View File

@@ -4,6 +4,7 @@ goog.require('goog.Disposable');
goog.require('goog.events');
goog.require('goog.fx.anim');
goog.require('goog.fx.anim.Animated');
goog.require('goog.vec.Mat4');
goog.require('ol.Map');
goog.require('ol.MapProperty');
@@ -43,6 +44,24 @@ ol.MapRenderer = function(target, map) {
*/
this.layersListenerKeys_ = null;
/**
* @private
* @type {goog.vec.Mat4.Number}
*/
this.coordinateToPixelMatrix_ = goog.vec.Mat4.createNumber();
/**
* @private
* @type {goog.vec.Mat4.Number}
*/
this.pixelToCoordinateMatrix_ = goog.vec.Mat4.createNumber();
/**
* @private
* @type {boolean}
*/
this.matricesDirty_ = true;
/**
* @private
* @type {Array.<number>}
@@ -155,7 +174,9 @@ ol.MapRenderer.prototype.handleBackgroundColorChanged = goog.nullFunction;
/**
* @protected
*/
ol.MapRenderer.prototype.handleCenterChanged = goog.nullFunction;
ol.MapRenderer.prototype.handleCenterChanged = function() {
this.matricesDirty_ = true;
};
/**
@@ -251,19 +272,25 @@ ol.MapRenderer.prototype.removeLayerRenderer = function(layer) {
/**
* @protected
*/
ol.MapRenderer.prototype.handleResolutionChanged = goog.nullFunction;
ol.MapRenderer.prototype.handleResolutionChanged = function() {
this.matricesDirty_ = true;
};
/**
* @protected
*/
ol.MapRenderer.prototype.handleRotationChanged = goog.nullFunction;
ol.MapRenderer.prototype.handleRotationChanged = function() {
this.matricesDirty_ = true;
};
/**
* @protected
*/
ol.MapRenderer.prototype.handleSizeChanged = goog.nullFunction;
ol.MapRenderer.prototype.handleSizeChanged = function() {
this.matricesDirty_ = true;
};
/**
@@ -278,3 +305,72 @@ ol.MapRenderer.prototype.render = function() {
});
return animate;
};
/**
* @private
*/
ol.MapRenderer.prototype.updateMatrices_ = function() {
if (this.matricesDirty_) {
var map = this.map;
var center = /** @type {!ol.Coordinate} */ map.getCenter();
var resolution = /** @type {number} */ map.getResolution();
var rotation = map.getRotation();
var size = /** @type {!ol.Size} */ map.getSize();
goog.vec.Mat4.makeIdentity(this.coordinateToPixelMatrix_);
goog.vec.Mat4.translate(this.coordinateToPixelMatrix_,
size.width / 2,
size.height / 2,
0);
goog.vec.Mat4.scale(this.coordinateToPixelMatrix_,
1 / resolution,
-1 / resolution,
1);
if (this.canRotate() && goog.isDef(rotation)) {
goog.vec.Mat4.rotate(this.coordinateToPixelMatrix_,
-rotation,
0,
0,
1);
}
goog.vec.Mat4.translate(this.coordinateToPixelMatrix_,
-center.x,
-center.y,
0);
var inverted = goog.vec.Mat4.invert(
this.coordinateToPixelMatrix_, this.pixelToCoordinateMatrix_);
goog.asserts.assert(inverted);
this.matricesDirty_ = false;
}
};
/**
* @param {ol.Pixel} pixel Pixel.
* @return {ol.Coordinate} Coordinate.
*/
ol.MapRenderer.prototype.getCoordinateFromPixel = function(pixel) {
this.updateMatrices_();
var vec3 = [pixel.x, pixel.y, 0];
goog.vec.Mat4.multVec3(this.pixelToCoordinateMatrix_, vec3, vec3);
return new ol.Coordinate(vec3[0], vec3[1]);
};
/**
* @param {ol.Coordinate} coordinate Coordinate.
* @return {ol.Pixel} Pixel.
*/
ol.MapRenderer.prototype.getPixelFromCoordinate = function(coordinate) {
this.updateMatrices_();
var vec3 = [coordinate.x, coordinate.y, 0];
goog.vec.Mat4.multVec3(this.coordinateToPixelMatrix_, vec3, vec3);
return new ol.Pixel(vec3[0], vec3[1]);
};