Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Throw DeveloperError if normalize fails #3605

Merged
merged 17 commits into from
Oct 20, 2016
7 changes: 7 additions & 0 deletions Source/Core/Cartesian2.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ define([

result.x = cartesian.x / magnitude;
result.y = cartesian.y / magnitude;

//>>includeStart('debug', pragmas.debug);
if (isNaN(result.x) || isNaN(result.y)) {
throw new DeveloperError('normalized result is not a number');
}
//>>includeEnd('debug');

return result;
};

Expand Down
7 changes: 7 additions & 0 deletions Source/Core/Cartesian3.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,13 @@ define([
result.x = cartesian.x / magnitude;
result.y = cartesian.y / magnitude;
result.z = cartesian.z / magnitude;

//>>includeStart('debug', pragmas.debug);
if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z)) {
throw new DeveloperError('normalized result is not a number');
}
//>>includeEnd('debug');

return result;
};

Expand Down
7 changes: 7 additions & 0 deletions Source/Core/Cartesian4.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,13 @@ define([
result.y = cartesian.y / magnitude;
result.z = cartesian.z / magnitude;
result.w = cartesian.w / magnitude;

//>>includeStart('debug', pragmas.debug);
if (isNaN(result.x) || isNaN(result.y) || isNaN(result.z) || isNaN(result.w)) {
throw new DeveloperError('normalized result is not a number');
}
//>>includeEnd('debug');

return result;
};

Expand Down
5 changes: 3 additions & 2 deletions Source/Core/CorridorGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,9 @@ define([
var scratchCartographicMax = new Cartographic();

function computeRectangle(positions, ellipsoid, width, cornerType) {
var length = positions.length - 1;
if (length === 0) {
var cleanPositions = arrayRemoveDuplicates(positions, Cartesian3.equalsEpsilon);
Copy link
Contributor

Choose a reason for hiding this comment

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

Something weird is going on with this change. Run the below code in this branch and it will crash, however remove the first point (which is a duplicate) and everything works. So many we need to re-use the deduplicated version in other places in this file?

var viewer = new Cesium.Viewer('cesiumContainer');

var redCorridor = viewer.entities.add({
    name : 'Red corridor on surface with rounded corners and outline',
    corridor : {
        positions : Cesium.Cartesian3.fromDegreesArray([
                                                        -16.41549000000001,28.44423000000001,
            -16.41549000000001,28.44423000000001,
            -16.41505000000001,28.44430000000001,
            -16.41507000000001,28.44430000000001,
            -16.41473000000001,28.44460000000001,
            -16.41429000000001,28.44452000000001,
            -16.41430000000001,28.44453000000001,
            -16.41428000000001,28.44452000000001,
            -16.41416000000001,28.44434000000001,
            -16.41415000000001,28.44434000000001,
            -16.41397000000001,28.44408000000001
                                                    ]),
                                                    width : 1,
        height:0,
        material : Cesium.Color.RED.withAlpha(0.5),
        outline : true,
        outlineColor : Cesium.Color.RED
    }
});

viewer.zoomTo(viewer.entities);

var length = cleanPositions.length - 1;
if (length === 0 || width === 0) {
return new Rectangle();
}
var halfWidth = width * 0.5;
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/EllipsoidalOccluder.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ define([
var directionToPointScratch = new Cartesian3();

function computeScaledSpaceDirectionToPoint(ellipsoid, directionToPoint) {
if (Cartesian3.equals(directionToPoint, Cartesian3.ZERO)) {
return directionToPoint;
}

ellipsoid.transformPositionToScaledSpace(directionToPoint, directionToPointScratch);
return Cartesian3.normalize(directionToPointScratch, directionToPointScratch);
}
Expand Down
17 changes: 14 additions & 3 deletions Source/Core/IntersectionTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,15 @@ define([
if (!defined(v1)) {
throw new DeveloperError('v1 is required.');
}
if (!defined(p0)) {
throw new DeveloperError('v1 is required.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Error messages are incorrect.

}
if (!defined(p1)) {
throw new DeveloperError('v1 is required.');
}
if (!defined(p2)) {
throw new DeveloperError('v1 is required.');
}
//>>includeEnd('debug');

var ray = scratchLineSegmentTriangleRay;
Expand Down Expand Up @@ -605,9 +614,11 @@ define([
var position = ray.origin;
var direction = ray.direction;

var normal = ellipsoid.geodeticSurfaceNormal(position, firstAxisScratch);
if (Cartesian3.dot(direction, normal) >= 0.0) { // The location provided is the closest point in altitude
return position;
if (!Cartesian3.equals(position, Cartesian3.ZERO)) {
var normal = ellipsoid.geodeticSurfaceNormal(position, firstAxisScratch);
if (Cartesian3.dot(direction, normal) >= 0.0) { // The location provided is the closest point in altitude
return position;
}
}

var intersects = defined(this.rayEllipsoid(ray, ellipsoid));
Expand Down
10 changes: 9 additions & 1 deletion Source/Core/PolylineVolumeGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ define([
'./CornerType',
'./defaultValue',
'./defined',
'./deprecationWarning',
'./DeveloperError',
'./Ellipsoid',
'./Geometry',
Expand All @@ -32,6 +33,7 @@ define([
CornerType,
defaultValue,
defined,
deprecationWarning,
DeveloperError,
Ellipsoid,
Geometry,
Expand Down Expand Up @@ -163,7 +165,13 @@ define([
}

if (vertexFormat.tangent || vertexFormat.binormal) {
geometry = GeometryPipeline.computeBinormalAndTangent(geometry);
try {
geometry = GeometryPipeline.computeBinormalAndTangent(geometry);
} catch (e) {
deprecationWarning('polyline-volume-tangent-binormal', 'Unable to compute tangents and binormals for polyline volume geometry');
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use oneTimeWarning here instead. It's the same thing but it's confusing to refer to this as deprecated.

//TODO https://github.com/AnalyticalGraphicsInc/cesium/issues/3609
}

if (!vertexFormat.tangent) {
geometry.attributes.tangent = undefined;
}
Expand Down
11 changes: 9 additions & 2 deletions Source/Scene/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -2846,8 +2846,15 @@ define([
var qUnit = Cartesian3.normalize(q, scratchCartesian3_2);

// Determine the east and north directions at q.
var eUnit = Cartesian3.normalize(Cartesian3.cross(Cartesian3.UNIT_Z, q, scratchCartesian3_3), scratchCartesian3_3);
var nUnit = Cartesian3.normalize(Cartesian3.cross(qUnit, eUnit, scratchCartesian3_4), scratchCartesian3_4);
var eUnit;
var nUnit;
if (Cartesian3.equalsEpsilon(qUnit, Cartesian3.UNIT_Z, CesiumMath.EPSILON10)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since random epsilon values have bitten us in the past, is there any ryhme or reason for 10 over other values?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

10 seems sufficiently small? We picked 10 in other places.

Copy link
Contributor

Choose a reason for hiding this comment

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

"We use it in other places" is fine for now.

eUnit = new Cartesian3(0, 1, 0);
nUnit = new Cartesian3(0, 0, 1);
} else {
eUnit = Cartesian3.normalize(Cartesian3.cross(Cartesian3.UNIT_Z, qUnit, scratchCartesian3_3), scratchCartesian3_3);
nUnit = Cartesian3.normalize(Cartesian3.cross(qUnit, eUnit, scratchCartesian3_4), scratchCartesian3_4);
}

// Determine the radius of the 'limb' of the ellipsoid.
var wMagnitude = Math.sqrt(Cartesian3.magnitudeSquared(q) - 1.0);
Expand Down
7 changes: 7 additions & 0 deletions Specs/Core/Cartesian2Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ defineSuite([
expect(cartesian).toEqual(expectedResult);
});

it('normalize throws with zero vector', function() {
expect(function() {
Cartesian2.normalize(Cartesian2.ZERO, new Cartesian2());
}).toThrowDeveloperError();
});


Copy link
Contributor

Choose a reason for hiding this comment

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

whitespace

it('multiplyComponents works with a result parameter', function() {
var left = new Cartesian2(2.0, 3.0);
var right = new Cartesian2(4.0, 5.0);
Expand Down
6 changes: 6 additions & 0 deletions Specs/Core/Cartesian3Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,12 @@ defineSuite([
expect(cartesian).toEqual(expectedResult);
});

it('normalize throws with zero vector', function() {
expect(function() {
Cartesian3.normalize(Cartesian3.ZERO, new Cartesian3());
}).toThrowDeveloperError();
});

it('multiplyComponents works with a result parameter', function() {
var left = new Cartesian3(2.0, 3.0, 6.0);
var right = new Cartesian3(4.0, 5.0, 7.0);
Expand Down
6 changes: 6 additions & 0 deletions Specs/Core/Cartesian4Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,12 @@ defineSuite([
expect(cartesian).toEqual(expectedResult);
});

it('normalize throws with zero vector', function() {
expect(function() {
Cartesian4.normalize(Cartesian4.ZERO, new Cartesian4());
}).toThrowDeveloperError();
});

it('multiplyComponents works with a result parameter', function() {
var left = new Cartesian4(2.0, 3.0, 6.0, 8.0);
var right = new Cartesian4(4.0, 5.0, 7.0, 9.0);
Expand Down
3 changes: 3 additions & 0 deletions Specs/Core/IntersectionTestsSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,21 @@ defineSuite([
});

it('lineSegmentTriangle throws without p0', function() {
//TODO
Copy link
Contributor

Choose a reason for hiding this comment

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

?

expect(function() {
IntersectionTests.lineSegmentTriangle(new Cartesian3(), new Cartesian3());
}).toThrowDeveloperError();
});

it('lineSegmentTriangle throws without p1', function() {
//TODO
expect(function() {
IntersectionTests.lineSegmentTriangle(new Cartesian3(), new Cartesian3(), new Cartesian3());
}).toThrowDeveloperError();
});

it('lineSegmentTriangle throws without p2', function() {
//TODO
expect(function() {
IntersectionTests.lineSegmentTriangle(new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3());
}).toThrowDeveloperError();
Expand Down
3 changes: 3 additions & 0 deletions Specs/Core/OrientedBoundingBoxSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ defineSuite([
Matrix3.multiplyByVector(axes, tang, tang);
Matrix3.multiplyByVector(axes, binorm, binorm);
Cartesian3.cross(tang, binorm, n);
if (Cartesian3.magnitude(n) === 0) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you use return undefined; here for consistency with the rest of the function.

The rule we tend to use (but doubt we've ever codified) is that we use return undefined; in cases where the function returns actual values sometimes as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Add it to the Coding Guide to keep our flurry of non-code PRs.

}
Cartesian3.normalize(n, n);

Cartesian3.add(p0, center, p0);
Expand Down
22 changes: 21 additions & 1 deletion Specs/Core/PolylineVolumeGeometrySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,27 @@ defineSuite([
expect(m.indices.length).toEqual(44 * 3);
});

it('compute all vertex attributes', function() {
it('computes most vertex attributes', function() {
var m = PolylineVolumeGeometry.createGeometry(new PolylineVolumeGeometry({
vertexFormat : VertexFormat.POSITION_NORMAL_AND_ST,
polylinePositions : Cartesian3.fromDegreesArray([
90.0, -30.0,
90.0, -35.0
]),
cornerType: CornerType.MITERED,
shapePositions: shape
}));

var numVertices = 56;
var numTriangles = 44;
expect(m.attributes.position.values.length).toEqual(numVertices * 3);
expect(m.attributes.st.values.length).toEqual(numVertices * 2);
expect(m.attributes.normal.values.length).toEqual(numVertices * 3);
expect(m.indices.length).toEqual(numTriangles * 3);
});

//https://github.com/AnalyticalGraphicsInc/cesium/issues/3609
xit('compute all vertex attributes', function() {
var m = PolylineVolumeGeometry.createGeometry(new PolylineVolumeGeometry({
vertexFormat : VertexFormat.ALL,
polylinePositions : Cartesian3.fromDegreesArray([
Expand Down
4 changes: 3 additions & 1 deletion Specs/DataSources/KmlDataSourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3807,7 +3807,9 @@ defineSuite([
</Link>\
</NetworkLink>';

var camera = createCamera();
var camera = createCamera({
offset: Cartesian3.fromDegrees(-110, 30, 1000)
});
Camera.clone(options.camera, camera);

var kmlOptions = {
Expand Down
2 changes: 1 addition & 1 deletion Specs/DataSources/PolylineVolumeGeometryUpdaterSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ defineSuite([

function createBasicPolylineVolume() {
var polylineVolume = new PolylineVolumeGraphics();
polylineVolume.positions = new ConstantProperty(Cartesian3.fromRadiansArray([
polylineVolume.positions = new ConstantProperty(Cartesian3.fromDegreesArray([
0, 0,
1, 0,
1, 1,
Expand Down
7 changes: 7 additions & 0 deletions Specs/Scene/SceneTransformsSpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*global defineSuite*/
defineSuite([
'Scene/SceneTransforms',
'Scene/SceneMode',
'Core/Cartesian2',
'Core/Cartesian3',
'Core/Ellipsoid',
Expand All @@ -10,6 +11,7 @@ defineSuite([
'Specs/createScene'
], function(
SceneTransforms,
SceneMode,
Cartesian2,
Cartesian3,
Ellipsoid,
Expand All @@ -32,6 +34,7 @@ defineSuite([
});

beforeEach(function() {
scene.mode = SceneMode.SCENE3D;
scene.camera.position = defaultCamera.position.clone();
scene.camera.direction = defaultCamera.direction.clone();
scene.camera.up = defaultCamera.up.clone();
Expand Down Expand Up @@ -179,6 +182,10 @@ defineSuite([
});

it('returns correct drawing buffer position in 2D', function() {
scene.camera.setView({
destination : Rectangle.fromDegrees(-0.000001, -0.000001, 0.000001, 0.000001)
});

// Update scene state
scene.morphTo2D(0);
scene.renderForSpecs();
Expand Down