Skip to content

Commit 55d0622

Browse files
authored
Merge pull request #7188 from AnalyticalGraphicsInc/coplanar-polygon-direction
Improve CoplanarPolygonGeometry normal calculation
2 parents 1f2204d + 23788c6 commit 55d0622

File tree

4 files changed

+61
-11
lines changed

4 files changed

+61
-11
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Change Log
2323
* Fixed entity visibility issue related to setting an entity show property and altering or adding entity geometry [#7156](https://github.com/AnalyticalGraphicsInc/cesium/pull/7156)
2424
* Fixed accuracy of rotation matrix generated by `VelocityOrientationProperty` [#6641](https://github.com/AnalyticalGraphicsInc/cesium/pull/6641)
2525
* Fixed clipping plane crash when adding a plane to an empty collection. [#7168](https://github.com/AnalyticalGraphicsInc/cesium/pull/7168)
26+
* Fixed texture coordinate calculation for polygon entities with `perPositionHeight` [#7188](https://github.com/AnalyticalGraphicsInc/cesium/pull/7188)
2627

2728
### 1.50 - 2018-10-01
2829

Source/Core/CoplanarPolygonGeometry.js

+34-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ define([
1010
'./CoplanarPolygonGeometryLibrary',
1111
'./defaultValue',
1212
'./defined',
13+
'./Ellipsoid',
1314
'./Geometry',
1415
'./GeometryAttribute',
1516
'./GeometryAttributes',
@@ -34,6 +35,7 @@ define([
3435
CoplanarPolygonGeometryLibrary,
3536
defaultValue,
3637
defined,
38+
Ellipsoid,
3739
Geometry,
3840
GeometryAttribute,
3941
GeometryAttributes,
@@ -62,6 +64,7 @@ define([
6264
var quaternionScratch = new Quaternion();
6365
var textureMatrixScratch = new Matrix3();
6466
var tangentRotationScratch = new Matrix3();
67+
var surfaceNormalScratch = new Cartesian3();
6568

6669
function createGeometryFromPolygon(polygon, vertexFormat, boundingRectangle, stRotation, projectPointTo2D, normal, tangent, bitangent) {
6770
var positions = polygon.positions;
@@ -208,6 +211,7 @@ define([
208211
* @param {PolygonHierarchy} options.polygonHierarchy A polygon hierarchy that can include holes.
209212
* @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
210213
* @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
214+
* @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
211215
*
212216
* @example
213217
* var polygon = new Cesium.CoplanarPolygonGeometry({
@@ -233,13 +237,14 @@ define([
233237
this._vertexFormat = VertexFormat.clone(vertexFormat);
234238
this._polygonHierarchy = polygonHierarchy;
235239
this._stRotation = defaultValue(options.stRotation, 0.0);
240+
this._ellipsoid = Ellipsoid.clone(defaultValue(options.ellipsoid, Ellipsoid.WGS84));
236241
this._workerName = 'createCoplanarPolygonGeometry';
237242

238243
/**
239244
* The number of elements used to pack the object into an array.
240245
* @type {Number}
241246
*/
242-
this.packedLength = PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) + VertexFormat.packedLength + 2;
247+
this.packedLength = PolygonGeometryLibrary.computeHierarchyPackedLength(polygonHierarchy) + VertexFormat.packedLength + Ellipsoid.packedLength + 2;
243248
}
244249

245250
/**
@@ -249,6 +254,7 @@ define([
249254
* @param {Cartesian3[]} options.positions An array of positions that defined the corner points of the polygon.
250255
* @param {VertexFormat} [options.vertexFormat=VertexFormat.DEFAULT] The vertex attributes to be computed.
251256
* @param {Number} [options.stRotation=0.0] The rotation of the texture coordinates, in radians. A positive rotation is counter-clockwise.
257+
* @param {Ellipsoid} [options.ellipsoid=Ellipsoid.WGS84] The ellipsoid to be used as a reference.
252258
* @returns {CoplanarPolygonGeometry}
253259
*
254260
* @example
@@ -278,7 +284,8 @@ define([
278284
positions : options.positions
279285
},
280286
vertexFormat : options.vertexFormat,
281-
stRotation : options.stRotation
287+
stRotation : options.stRotation,
288+
ellipsoid : options.ellipsoid
282289
};
283290
return new CoplanarPolygonGeometry(newOptions);
284291
};
@@ -302,6 +309,9 @@ define([
302309

303310
startingIndex = PolygonGeometryLibrary.packPolygonHierarchy(value._polygonHierarchy, array, startingIndex);
304311

312+
Ellipsoid.pack(value._ellipsoid, array, startingIndex);
313+
startingIndex += Ellipsoid.packedLength;
314+
305315
VertexFormat.pack(value._vertexFormat, array, startingIndex);
306316
startingIndex += VertexFormat.packedLength;
307317

@@ -311,6 +321,7 @@ define([
311321
return array;
312322
};
313323

324+
var scratchEllipsoid = Ellipsoid.clone(Ellipsoid.UNIT_SPHERE);
314325
var scratchVertexFormat = new VertexFormat();
315326
var scratchOptions = {
316327
polygonHierarchy : {}
@@ -334,6 +345,9 @@ define([
334345
startingIndex = polygonHierarchy.startingIndex;
335346
delete polygonHierarchy.startingIndex;
336347

348+
var ellipsoid = Ellipsoid.unpack(array, startingIndex, scratchEllipsoid);
349+
startingIndex += Ellipsoid.packedLength;
350+
337351
var vertexFormat = VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
338352
startingIndex += VertexFormat.packedLength;
339353

@@ -345,6 +359,7 @@ define([
345359
}
346360

347361
result._polygonHierarchy = polygonHierarchy;
362+
result._ellipsoid = Ellipsoid.clone(ellipsoid, result._ellipsoid);
348363
result._vertexFormat = VertexFormat.clone(vertexFormat, result._vertexFormat);
349364
result._stRotation = stRotation;
350365
result.packedLength = packedLength;
@@ -371,22 +386,33 @@ define([
371386
var normal = scratchNormal;
372387
var tangent = scratchTangent;
373388
var bitangent = scratchBitangent;
389+
var axis1 = axis1Scratch;
390+
var axis2 = axis2Scratch;
374391

375-
var validGeometry = CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments(outerPositions, centerScratch, axis1Scratch, axis2Scratch);
392+
var validGeometry = CoplanarPolygonGeometryLibrary.computeProjectTo2DArguments(outerPositions, centerScratch, axis1, axis2);
376393
if (!validGeometry) {
377394
return undefined;
378395
}
379-
var projectPoints = CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction(centerScratch, axis1Scratch, axis2Scratch);
380-
var projectPoint = CoplanarPolygonGeometryLibrary.createProjectPointTo2DFunction(centerScratch, axis1Scratch, axis2Scratch);
381396

382-
normal = Cartesian3.cross(axis1Scratch, axis2Scratch, normal);
397+
normal = Cartesian3.cross(axis1, axis2, normal);
383398
normal = Cartesian3.normalize(normal, normal);
384399

400+
if (!Cartesian3.equalsEpsilon(centerScratch, Cartesian3.ZERO, CesiumMath.EPSILON6)) {
401+
var surfaceNormal = polygonGeometry._ellipsoid.geodeticSurfaceNormal(centerScratch, surfaceNormalScratch);
402+
if (Cartesian3.dot(normal, surfaceNormal) < 0) {
403+
normal = Cartesian3.negate(normal, normal);
404+
axis1 = Cartesian3.negate(axis1, axis1);
405+
}
406+
}
407+
408+
var projectPoints = CoplanarPolygonGeometryLibrary.createProjectPointsTo2DFunction(centerScratch, axis1, axis2);
409+
var projectPoint = CoplanarPolygonGeometryLibrary.createProjectPointTo2DFunction(centerScratch, axis1, axis2);
410+
385411
if (vertexFormat.tangent) {
386-
tangent = Cartesian3.clone(axis1Scratch, tangent);
412+
tangent = Cartesian3.clone(axis1, tangent);
387413
}
388414
if (vertexFormat.bitangent) {
389-
bitangent = Cartesian3.clone(axis2Scratch, bitangent);
415+
bitangent = Cartesian3.clone(axis2, bitangent);
390416
}
391417

392418
var results = PolygonGeometryLibrary.polygonsFromHierarchy(polygonHierarchy, projectPoints, false);

Source/Workers/createCoplanarPolygonOutlineGeometry.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
define([
22
'../Core/defined',
3-
'../Core/CoplanarPolygonOutlineGeometry'
3+
'../Core/CoplanarPolygonOutlineGeometry',
4+
'../Core/Ellipsoid'
45
], function(
56
defined,
6-
CoplanarPolygonOutlineGeometry) {
7+
CoplanarPolygonOutlineGeometry,
8+
Ellipsoid) {
79
'use strict';
810

911
function createCoplanarPolygonOutlineGeometry(polygonGeometry, offset) {
1012
if (defined(offset)) {
1113
polygonGeometry = CoplanarPolygonOutlineGeometry.unpack(polygonGeometry, offset);
1214
}
15+
polygonGeometry._ellipsoid = Ellipsoid.clone(polygonGeometry._ellipsoid);
1316
return CoplanarPolygonOutlineGeometry.createGeometry(polygonGeometry);
1417
}
1518

Specs/Core/CoplanarPolygonGeometrySpec.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,25 @@ defineSuite([
105105
expect(p.indices.length).toEqual(numTriangles * 3);
106106
});
107107

108+
it('flips normal to roughly match surface normal', function() {
109+
var p = CoplanarPolygonGeometry.createGeometry(CoplanarPolygonGeometry.fromPositions({
110+
vertexFormat : VertexFormat.ALL,
111+
positions : Cartesian3.fromDegreesArrayHeights([
112+
90.0, -1.0, 0.0,
113+
90.0, 1.0, 0.0,
114+
92.0, 1.0, 0.0,
115+
92.0, -1.0, 0.0
116+
])
117+
}));
118+
119+
var center = Cartesian3.fromDegrees(91.0, 0.0);
120+
var expectedNormal = Ellipsoid.WGS84.geodeticSurfaceNormal(center);
121+
122+
var actual = Cartesian3.unpack(p.attributes.normal.values);
123+
124+
expect(expectedNormal).toEqualEpsilon(actual, CesiumMath.EPSILON6);
125+
});
126+
108127
var positions = Cartesian3.fromDegreesArray([
109128
-12.4, 3.5,
110129
-12.0, 3.5,
@@ -148,7 +167,8 @@ defineSuite([
148167
addPositions(packedInstance, holePositions0);
149168
packedInstance.push(3.0, 0.0);
150169
addPositions(packedInstance, holePositions1);
151-
packedInstance.push(1, 0, 0, 0, 0, 0, 0, 41);
170+
packedInstance.push(Ellipsoid.WGS84.radii.x, Ellipsoid.WGS84.radii.y, Ellipsoid.WGS84.radii.z);
171+
packedInstance.push(1, 0, 0, 0, 0, 0, 0, 44);
152172
createPackableSpecs(CoplanarPolygonGeometry, polygon, packedInstance);
153173
});
154174

0 commit comments

Comments
 (0)