Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
likangning93 committed Sep 10, 2018
1 parent 66c0646 commit 0d2d47a
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 62 deletions.
7 changes: 3 additions & 4 deletions Apps/Sandcastle/gallery/Custom Projection.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@
}));
}

// Add lat/long points
for (var x = -175; x < 180; x += 10) {
for (var y = -85; y < 90; y += 10) {
addPoint(x, y);
for (var longitude = -175; longitude < 180; longitude += 10) {
for (var latitude = -85; latitude < 90; latitude += 10) {
addPoint(longitude, latitude);
}
}

Expand Down
7 changes: 3 additions & 4 deletions Apps/Sandcastle/gallery/Proj4Js Projection.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@
}));
}

// Add lat/long points
for (var x = -175; x < 180; x += 10) {
for (var y = -85; y < 90; y += 10) {
addPoint(x, y);
for (var longitude = -175; longitude < 180; longitude += 10) {
for (var latitude = -85; latitude < 90; latitude += 10) {
addPoint(longitude, latitude);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/CustomProjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ define([
CustomProjection.prototype.project = function(cartographic, result) {
//>>includeStart('debug', pragmas.debug);
if (!this._ready) {
throw new DeveloperError('CustomProjection is not loaded. User CustomProjection.readyPromise or waith for CustomProjection.ready to be true.');
throw new DeveloperError('CustomProjection is not loaded. User CustomProjection.readyPromise or wait for CustomProjection.ready to be true.');
}
Check.defined('cartographic', cartographic);
//>>includeEnd('debug');
Expand Down Expand Up @@ -180,7 +180,7 @@ define([
CustomProjection.prototype.unproject = function(cartesian, result) {
//>>includeStart('debug', pragmas.debug);
if (!this._ready) {
throw new DeveloperError('CustomProjection is not loaded. User CustomProjection.readyPromise or waith for CustomProjection.ready to be true.');
throw new DeveloperError('CustomProjection is not loaded. User CustomProjection.readyPromise or wait for CustomProjection.ready to be true.');
}
Check.defined('cartesian', cartesian);
//>>includeEnd('debug');
Expand Down
42 changes: 17 additions & 25 deletions Source/Core/Proj4Projection.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ define([
'./defaultValue',
'./defined',
'./defineProperties',
'./Ellipsoid'
'./Ellipsoid',
'./oneTimeWarning'
], function(
proj4,
Cartesian3,
Expand All @@ -17,16 +18,15 @@ define([
defaultValue,
defined,
defineProperties,
Ellipsoid) {
Ellipsoid,
oneTimeWarning) {
'use strict';

/**
* MapProjection using proj4js. This projection is only to be used with Ellipsoid.WGS84.
* Users should exercise caution when using local-area projections, as local area projections
* may produce unexpected results outside their specified boundaries.
*
* Assumes Ellipsoid WGS84.
*
* @alias Proj4Projection
* @constructor
*
Expand All @@ -39,8 +39,6 @@ define([
var wkt = defaultValue(wellKnownText, 'EPSG:3857'); // web mercator
this._wkt = wkt;
this._projection = proj4(wkt);
this._forwardFailed = false;
this._inverseFailed = false;

heightScale = defaultValue(heightScale, 1.0);
this._heightScale = heightScale;
Expand Down Expand Up @@ -84,7 +82,7 @@ define([
}
});

var projectionArray = [0, 0];
var scratchProjectionArray = [0.0, 0.0];
/**
* Projects a set of {@link Cartographic} coordinates, in radians, to map coordinates, in meters based on
* the specified projection.
Expand All @@ -106,19 +104,16 @@ define([
}

// without clamp proj4 might crash
projectionArray[0] = CesiumMath.clamp(CesiumMath.toDegrees(cartographic.longitude), -180 + CesiumMath.EPSILON7, 180 - CesiumMath.EPSILON7);
projectionArray[1] = CesiumMath.clamp(CesiumMath.toDegrees(cartographic.latitude), -90 + CesiumMath.EPSILON7, 90 - CesiumMath.EPSILON7);
scratchProjectionArray[0] = CesiumMath.clamp(CesiumMath.toDegrees(cartographic.longitude), -180.0 + CesiumMath.EPSILON7, 180.0 - CesiumMath.EPSILON7);
scratchProjectionArray[1] = CesiumMath.clamp(CesiumMath.toDegrees(cartographic.latitude), -90.0 + CesiumMath.EPSILON7, 90.0 - CesiumMath.EPSILON7);

var projected;
try {
projected = this._projection.forward(projectionArray);
projected = this._projection.forward(scratchProjectionArray);
} catch(e) {
if (!this._forwardFailed) {
// Log a warning the first time a projection fails
console.warn('proj4js forward failed for ' + projectionArray + ' with projection ' + this._wkt);
this._forwardFailed = true;
}
projected = [0, 0];
// Log a warning the first time a projection fails
oneTimeWarning('proj4js-project', 'proj4js forward failed for ' + scratchProjectionArray + ' with projection ' + this._wkt);
projected = [0.0, 0.0];
}

result.x = projected[0];
Expand Down Expand Up @@ -147,19 +142,16 @@ define([
if (!defined(result)) {
result = new Cartographic();
}
projectionArray[0] = cartesian.x;
projectionArray[1] = cartesian.y;
scratchProjectionArray[0] = cartesian.x;
scratchProjectionArray[1] = cartesian.y;

var projected;
try {
projected = this._projection.inverse(projectionArray);
projected = this._projection.inverse(scratchProjectionArray);
} catch(e) {
if (!this._inverseFailed) {
// Log a warning the first time a projection fails
console.warn('proj4js inverse failed for ' + projectionArray + ' with projection ' + this._wkt);
this._inverseFailed = true;
}
projected = [0, 0];
// Log a warning the first time a projection fails
oneTimeWarning('proj4js-unproject', 'proj4js inverse failed for ' + scratchProjectionArray + ' with projection ' + this._wkt);
projected = [0.0, 0.0];
}

result.longitude = CesiumMath.toRadians(projected[0]);
Expand Down
55 changes: 39 additions & 16 deletions Source/Core/SerializedMapProjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ define([
'./defined',
'./DeveloperError',
'./Ellipsoid',
'./freezeObject',
'./GeographicProjection',
'./Proj4Projection',
'./WebMercatorProjection'
Expand All @@ -15,11 +16,19 @@ define([
defined,
DeveloperError,
Ellipsoid,
freezeObject,
GeographicProjection,
Proj4Projection,
WebMercatorProjection) {
'use strict';

var ProjectionType = freezeObject({
GEOGRAPHIC : 0,
WEBMERCATOR : 1,
PROJ4JS : 2,
CUSTOM : 3
});

/**
* Serializes and Deserializes MapProjections.
*
Expand All @@ -33,12 +42,28 @@ define([
Check.defined('mapProjection', mapProjection);
//>>includeEnd('debug');

this.isMercator = mapProjection instanceof WebMercatorProjection;
this.isGeographic = mapProjection instanceof GeographicProjection;
this.wellKnownText = mapProjection.wellKnownText;
this.heightScale = mapProjection.heightScale;
this.url = mapProjection.url;
this.functionName = mapProjection.functionName;
var projectionType = ProjectionType.GEOGRAPHIC;
var wellKnownText;
var heightScale;
var url;
var functionName;
if (mapProjection instanceof WebMercatorProjection) {
projectionType = ProjectionType.WEBMERCATOR;
} else if (mapProjection instanceof Proj4Projection) {
projectionType = ProjectionType.PROJ4JS;
wellKnownText = mapProjection.wellKnownText;
heightScale = mapProjection.heightScale;
} else if (mapProjection instanceof CustomProjection) {
projectionType = ProjectionType.CUSTOM;
url = mapProjection.url;
functionName = mapProjection.functionName;
}

this.projectionType = projectionType;
this.wellKnownText = wellKnownText;
this.heightScale = heightScale;
this.url = url;
this.functionName = functionName;

this.packedEllipsoid = Ellipsoid.pack(mapProjection.ellipsoid, new Array(Ellipsoid.packedLength));
}
Expand All @@ -56,21 +81,19 @@ define([

var ellipsoid = Ellipsoid.unpack(serializedMapProjection.packedEllipsoid);
var projection;
var projectionType = serializedMapProjection.projectionType;

if (defined(serializedMapProjection.url) && defined(serializedMapProjection.url)) {
if (projectionType === ProjectionType.GEOGRAPHIC) {
projection = new GeographicProjection(ellipsoid);
} else if (projectionType === ProjectionType.WEBMERCATOR) {
projection = new WebMercatorProjection(ellipsoid);
} else if (projectionType === ProjectionType.PROJ4JS) {
projection = new Proj4Projection(serializedMapProjection.wellKnownText, serializedMapProjection.heightScale);
} else if (projectionType === ProjectionType.CUSTOM) {
projection = new CustomProjection(serializedMapProjection.url, serializedMapProjection.functionName, ellipsoid);
return projection.readyPromise;
}

if (serializedMapProjection.isMercator) {
projection = new WebMercatorProjection(ellipsoid);
}
if (serializedMapProjection.isGeographic) {
projection = new GeographicProjection(ellipsoid);
}
if (defined(serializedMapProjection.wellKnownText)) {
projection = new Proj4Projection(serializedMapProjection.wellKnownText, serializedMapProjection.heightScale);
}
if (defined(projection)) {
return when.resolve(projection);
}
Expand Down
20 changes: 10 additions & 10 deletions Source/Core/TerrainEncoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ define([
* The terrain mesh should contain projected positions for 2D space.
* @type {Boolean}
*/
this.has2dPositions = defined(center2D);
this.hasPositions2D = defined(center2D);
}

TerrainEncoding.prototype.encode = function(vertexBuffer, bufferIndex, position, uv, height, normalToPack, webMercatorT, position2D) {
Expand Down Expand Up @@ -210,7 +210,7 @@ define([
}

// Don't quantize 2D positions in custom projections
if (this.has2dPositions) {
if (this.hasPositions2D) {
Cartesian3.subtract(position2D, this.center2D, cartesian3Scratch);
vertexBuffer[bufferIndex++] = cartesian3Scratch.x;
vertexBuffer[bufferIndex++] = cartesian3Scratch.y;
Expand Down Expand Up @@ -271,7 +271,7 @@ define([

TerrainEncoding.prototype.getOctEncodedNormal = function(buffer, index, result) {
var stride = this.getStride();
index = (index + 1) * stride - (this.has2dPositions ? 3 : 1);
index = (index + 1) * stride - (this.hasPositions2D ? 3 : 1);

var temp = buffer[index] / 256.0;
var x = Math.floor(temp);
Expand Down Expand Up @@ -299,7 +299,7 @@ define([
++vertexStride;
}

if (this.has2dPositions) {
if (this.hasPositions2D) {
vertexStride += 3;
}

Expand Down Expand Up @@ -330,7 +330,7 @@ define([
var sizeInBytes = ComponentDatatype.getSizeInBytes(datatype);
var stride;
var terrainAttributes;
var num2DComponents = this.has2dPositions ? 3 : 0;
var num2DComponents = this.hasPositions2D ? 3 : 0;

if (this.quantization === TerrainQuantization.NONE) {
var position3DAndHeightLength = 4;
Expand Down Expand Up @@ -361,7 +361,7 @@ define([
offsetInBytes : position3DAndHeightLength * sizeInBytes,
strideInBytes : stride
}];
if (this.has2dPositions) {
if (this.hasPositions2D) {
terrainAttributes.push({
index : attributesNoneAnd2D.position2D,
vertexBuffer : buffer,
Expand Down Expand Up @@ -402,7 +402,7 @@ define([
strideInBytes : stride
}];

if (this.has2dPositions) {
if (this.hasPositions2D) {
terrainAttributes.push({
index : attributesAnd2D.position2D,
vertexBuffer : buffer,
Expand All @@ -416,7 +416,7 @@ define([
return terrainAttributes;
}

if (this.has2dPositions) {
if (this.hasPositions2D) {
stride = (numCompressed0 + num2DComponents) * sizeInBytes;
return [{
index : attributes.compressed0,
Expand All @@ -443,7 +443,7 @@ define([
};

TerrainEncoding.prototype.getAttributeLocations = function() {
if (this.has2dPositions) {
if (this.hasPositions2D) {
if (this.quantization === TerrainQuantization.NONE) {
return attributesNoneAnd2D;
}
Expand All @@ -469,7 +469,7 @@ define([
result.matrix = Matrix4.clone(encoding.matrix);
result.hasVertexNormals = encoding.hasVertexNormals;
result.hasWebMercatorT = encoding.hasWebMercatorT;
result.has2dPositions = encoding.has2dPositions;
result.hasPositions2D = encoding.hasPositions2D;
result.center2D = Cartesian3.clone(encoding.center2D);
return result;
};
Expand Down
2 changes: 1 addition & 1 deletion Source/Scene/TileTerrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ define([

var mesh = this.mesh;
Cartesian3.clone(mesh.center, surfaceTile.center);
if (mesh.encoding.has2dPositions) {
if (mesh.encoding.hasPositions2D) {
Cartesian3.clone(mesh.encoding.center2D, surfaceTile.center2D);
}
surfaceTile.minimumHeight = mesh.minimumHeight;
Expand Down

0 comments on commit 0d2d47a

Please sign in to comment.