Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

improve sanity check for geodesic distance calc #7457

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Source/Core/EllipsoidGeodesic.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,18 @@ define([
ellipsoidGeodesic._uSquared = uSquared;
}

//>>includeStart('debug', pragmas.debug);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder about these scratch variables:
Why do these scratch variables live outside the only scope they're used?
Why use scratch variables like this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While debugging (the web worker) I ran into a case where the "subdivide heights" scratch array had a zero-length even though the code that was being debugged had presumably just initialized it... I reproduced it once after a refresh (left the debugger open). I've noticed strange behavior while debugging web workers with Chrome, so it's possible it's an artifact of that and not an issue with the logic (shared scratch) and some kind of race condition (between worker/worker or worker/main -- ... apologies I'm not well versed here).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scratch variables are at this scope so they can be reused each time the function is called instead of creating new instances each time. This is something we do commonly for performance. See https://github.com/AnalyticalGraphicsInc/cesium/tree/master/Documentation/Contributors/CodingGuide#result-parameters-and-scratch-variables

So you'll need to remove the pragmas here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with adding the pragma block: these scratch variables are only used for the debug check.

var scratchCart1 = new Cartesian3();
var scratchCart2 = new Cartesian3();
//>>includeEnd('debug');
function computeProperties(ellipsoidGeodesic, start, end, ellipsoid) {
var firstCartesian = Cartesian3.normalize(ellipsoid.cartographicToCartesian(start, scratchCart2), scratchCart1);
var lastCartesian = Cartesian3.normalize(ellipsoid.cartographicToCartesian(end, scratchCart2), scratchCart2);

//>>includeStart('debug', pragmas.debug);
Check.typeOf.number.greaterThanOrEquals('value', Math.abs(Math.abs(Cartesian3.angleBetween(firstCartesian, lastCartesian)) - Math.PI), 0.0125);
var startCartesian = Cartesian3.normalize(ellipsoid.cartographicToCartesian(start, scratchCart2), scratchCart1);
var endCartesian = Cartesian3.normalize(ellipsoid.cartographicToCartesian(end, scratchCart2), scratchCart2);
var includedAngle = Math.abs(Math.abs(Cartesian3.angleBetween(startCartesian, endCartesian)) - CesiumMath.PI);
// maxLambda is an approximation, assuming an oblate ellipsoid, and zero latitude (equatorial; phi = 0)
var maxLambda = (1 - (ellipsoid.maximumRadius - ellipsoid.minimumRadius) / ellipsoid.maximumRadius) * CesiumMath.PI;
Check.typeOf.number.lessThanOrEquals('value', includedAngle, maxLambda);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Check call should be the only thing inside the debug pragmas here. Everything between //>>includeStart and //>>includeEnd will be removed from the code in the minified release version, so you don't want to have any logic included in that section. It's only for throwing DeveloperErrors

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved that logic into the pragma block because it is only used for the check.

//>>includeEnd('debug');

vincentyInverseFormula(ellipsoidGeodesic, ellipsoid.maximumRadius, ellipsoid.minimumRadius,
Expand Down