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

pragmatically and precisely compute ellipse rectangle on demand #6382

Merged
merged 3 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions Source/Core/CircleGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ define([
'./defineProperties',
'./EllipseGeometry',
'./Ellipsoid',
'./Rectangle',
'./VertexFormat'
], function(
Cartesian3,
Expand All @@ -15,6 +16,7 @@ define([
defineProperties,
EllipseGeometry,
Ellipsoid,
Rectangle,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this needed anymore?

VertexFormat) {
'use strict';

Expand Down
70 changes: 29 additions & 41 deletions Source/Core/EllipseGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,37 +653,30 @@ define([
};
}

var scratchEnuToFixedMatrix = new Matrix4();
var scratchFixedToEnuMatrix = new Matrix4();
var scratchRotationMatrix = new Matrix3();
var scratchRectanglePoints = [new Cartesian3(), new Cartesian3(), new Cartesian3(), new Cartesian3()];
var scratchCartographicPoints = [new Cartographic(), new Cartographic(), new Cartographic(), new Cartographic()];

function computeRectangle(center, ellipsoid, semiMajorAxis, semiMinorAxis, rotation) {
Transforms.eastNorthUpToFixedFrame(center, ellipsoid, scratchEnuToFixedMatrix);
Matrix4.inverseTransformation(scratchEnuToFixedMatrix, scratchFixedToEnuMatrix);

// Find the 4 extreme points of the ellipse in ENU
var i;
for (i = 0; i < 4; ++i) {
Cartesian3.clone(Cartesian3.ZERO, scratchRectanglePoints[i]);
}
scratchRectanglePoints[0].x += semiMajorAxis;
scratchRectanglePoints[1].x -= semiMajorAxis;
scratchRectanglePoints[2].y += semiMinorAxis;
scratchRectanglePoints[3].y -= semiMinorAxis;

Matrix3.fromRotationZ(rotation, scratchRotationMatrix);
for (i = 0; i < 4; ++i) {
// Apply the rotation
Matrix3.multiplyByVector(scratchRotationMatrix, scratchRectanglePoints[i], scratchRectanglePoints[i]);

// Convert back to fixed and then to cartographic
Matrix4.multiplyByPoint(scratchEnuToFixedMatrix, scratchRectanglePoints[i], scratchRectanglePoints[i]);
ellipsoid.cartesianToCartographic(scratchRectanglePoints[i], scratchCartographicPoints[i]);
}

return Rectangle.fromCartographicArray(scratchCartographicPoints);
function computeRectangle(ellipseGeometry) {
var cep = EllipseGeometryLibrary.computeEllipsePositions({
center : ellipseGeometry._center,
semiMajorAxis : ellipseGeometry._semiMajorAxis,
semiMinorAxis : ellipseGeometry._semiMinorAxis,
rotation : ellipseGeometry._rotation,
granularity : ellipseGeometry._granularity
}, false, true);
var positionsFlat = cep.outerPositions;
var positionsCount = positionsFlat.length / 3;
var positions = new Array(positionsCount);
for (var i = 0; i < positionsCount; ++i) {
positions[i] = Cartesian3.fromArray(positionsFlat, i * 3);
}
var rectangle = Rectangle.fromCartesianArray(positions);
// Rectangle width goes beyond 180 degrees when the ellipse crosses a pole.
// When this happens, make the rectangle into a "circle" around the pole
if (rectangle.width > CesiumMath.PI) {
rectangle.north = rectangle.north > 0.0 ? CesiumMath.PI_OVER_TWO - CesiumMath.EPSILON7 : rectangle.north;
rectangle.south = rectangle.south < 0.0 ? CesiumMath.EPSILON7 - CesiumMath.PI_OVER_TWO : rectangle.south;
rectangle.east = CesiumMath.PI;
rectangle.west = -CesiumMath.PI;
}
return rectangle;
}

/**
Expand Down Expand Up @@ -766,14 +759,14 @@ define([
this._shadowVolume = defaultValue(options.shadowVolume, false);
this._workerName = 'createEllipseGeometry';

this._rectangle = computeRectangle(this._center, this._ellipsoid, semiMajorAxis, semiMinorAxis, this._rotation);
this._rectangle = undefined;
}

/**
* The number of elements used to pack the object into an array.
* @type {Number}
*/
EllipseGeometry.packedLength = Cartesian3.packedLength + Ellipsoid.packedLength + VertexFormat.packedLength + Rectangle.packedLength + 9;
EllipseGeometry.packedLength = Cartesian3.packedLength + Ellipsoid.packedLength + VertexFormat.packedLength + 9;

/**
* Stores the provided instance into the provided array.
Expand Down Expand Up @@ -805,9 +798,6 @@ define([
VertexFormat.pack(value._vertexFormat, array, startingIndex);
startingIndex += VertexFormat.packedLength;

Rectangle.pack(value._rectangle, array, startingIndex);
startingIndex += Rectangle.packedLength;

array[startingIndex++] = value._semiMajorAxis;
array[startingIndex++] = value._semiMinorAxis;
array[startingIndex++] = value._rotation;
Expand All @@ -824,7 +814,6 @@ define([
var scratchCenter = new Cartesian3();
var scratchEllipsoid = new Ellipsoid();
var scratchVertexFormat = new VertexFormat();
var scratchRectangle = new Rectangle();
var scratchOptions = {
center : scratchCenter,
ellipsoid : scratchEllipsoid,
Expand Down Expand Up @@ -865,9 +854,6 @@ define([
var vertexFormat = VertexFormat.unpack(array, startingIndex, scratchVertexFormat);
startingIndex += VertexFormat.packedLength;

var rectangle = Rectangle.unpack(array, startingIndex, scratchRectangle);
startingIndex += Rectangle.packedLength;

var semiMajorAxis = array[startingIndex++];
var semiMinorAxis = array[startingIndex++];
var rotation = array[startingIndex++];
Expand Down Expand Up @@ -902,7 +888,6 @@ define([
result._extrudedHeight = extrudedHeight;
result._extrude = extrude;
result._shadowVolume = shadowVolume;
result._rectangle = Rectangle.clone(rectangle);

return result;
};
Expand Down Expand Up @@ -980,6 +965,9 @@ define([
*/
rectangle : {
get : function() {
if (!defined(this._rectangle)) {
this._rectangle = computeRectangle(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

It can be in a separate PR, but we should also update polygon, corridor and rectangle to work this way

}
return this._rectangle;
}
}
Expand Down
13 changes: 5 additions & 8 deletions Specs/Core/CircleGeometrySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ defineSuite([
'Core/Cartesian3',
'Core/Ellipsoid',
'Core/Math',
'Core/Rectangle',
'Core/VertexFormat',
'Specs/createPackableSpecs'
], function(
CircleGeometry,
Cartesian3,
Ellipsoid,
CesiumMath,
Rectangle,
VertexFormat,
createPackableSpecs) {
'use strict';
Expand Down Expand Up @@ -160,15 +158,14 @@ defineSuite([
});

var r = ellipse.rectangle;
expect(r.north).toEqual(0.6989665987920752);
expect(r.south).toEqual(0.6986522252554146);
expect(r.east).toEqual(-1.3192254919769824);
expect(r.west).toEqual(-1.319634495353805);
expect(r.north).toEqual(0.698966597893341);
expect(r.south).toEqual(0.698652226072367);
expect(r.east).toEqual(-1.3192254919753026);
expect(r.west).toEqual(-1.3196344953554853);
});

var center = Cartesian3.fromDegrees(0,0);
var ellipsoid = Ellipsoid.WGS84;
var rectangle = new Rectangle(-1.5678559428873852e-7, -1.578422502906833e-7, 1.5678559428873852e-7, 1.578422502906833e-7);
var packableInstance = new CircleGeometry({
vertexFormat : VertexFormat.POSITION_AND_ST,
ellipsoid : ellipsoid,
Expand All @@ -177,6 +174,6 @@ defineSuite([
radius : 1.0,
stRotation : CesiumMath.PI_OVER_TWO
});
var packedInstance = [center.x, center.y, center.z, ellipsoid.radii.x, ellipsoid.radii.y, ellipsoid.radii.z, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, rectangle.west, rectangle.south, rectangle.east, rectangle.north, 1.0, 1.0, 0.0, CesiumMath.PI_OVER_TWO, 0.0, 0.1, 0.0, 0.0, 0.0];
var packedInstance = [center.x, center.y, center.z, ellipsoid.radii.x, ellipsoid.radii.y, ellipsoid.radii.z, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, CesiumMath.PI_OVER_TWO, 0.0, 0.1, 0.0, 0.0, 0.0];
createPackableSpecs(CircleGeometry, packableInstance, packedInstance);
});
19 changes: 15 additions & 4 deletions Specs/Core/EllipseGeometrySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ defineSuite([
'Core/Cartesian3',
'Core/Ellipsoid',
'Core/Math',
'Core/Rectangle',
'Core/VertexFormat',
'Specs/createPackableSpecs'
], function(
EllipseGeometry,
Cartesian3,
Ellipsoid,
CesiumMath,
Rectangle,
VertexFormat,
createPackableSpecs) {
'use strict';
Expand Down Expand Up @@ -250,11 +248,24 @@ defineSuite([
expect(r.south).toEqualEpsilon(0.6986522252554146, CesiumMath.EPSILON7);
expect(r.east).toEqualEpsilon(-1.3190209903056758, CesiumMath.EPSILON7);
expect(r.west).toEqualEpsilon(-1.3198389970251112, CesiumMath.EPSILON7);

// Polar ellipse
center = Cartesian3.fromDegrees(0.0, 90);
ellipse = new EllipseGeometry({
center : center,
semiMajorAxis : 2000.0,
semiMinorAxis : 1000.0
});

r = ellipse.rectangle;
expect(r.north).toEqualEpsilon(CesiumMath.PI_OVER_TWO - CesiumMath.EPSILON7, CesiumMath.EPSILON7);
expect(r.south).toEqualEpsilon(1.570483806950967, CesiumMath.EPSILON7);
expect(r.east).toEqualEpsilon(CesiumMath.PI, CesiumMath.EPSILON7);
expect(r.west).toEqualEpsilon(-CesiumMath.PI, CesiumMath.EPSILON7);
});

var center = Cartesian3.fromDegrees(0,0);
var ellipsoid = Ellipsoid.WGS84;
var rectangle = new Rectangle(-1.5678559428873852e-7, -1.578422502906833e-7, 1.5678559428873852e-7, 1.578422502906833e-7);
var packableInstance = new EllipseGeometry({
vertexFormat : VertexFormat.POSITION_AND_ST,
ellipsoid : ellipsoid,
Expand All @@ -264,7 +275,7 @@ defineSuite([
semiMinorAxis : 1.0,
stRotation : CesiumMath.PI_OVER_TWO
});
var packedInstance = [center.x, center.y, center.z, ellipsoid.radii.x, ellipsoid.radii.y, ellipsoid.radii.z, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, rectangle.west, rectangle.south, rectangle.east, rectangle.north, 1.0, 1.0, 0.0, CesiumMath.PI_OVER_TWO, 0.0, 0.1, 0.0, 0.0, 0.0];
var packedInstance = [center.x, center.y, center.z, ellipsoid.radii.x, ellipsoid.radii.y, ellipsoid.radii.z, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, CesiumMath.PI_OVER_TWO, 0.0, 0.1, 0.0, 0.0, 0.0];
createPackableSpecs(EllipseGeometry, packableInstance, packedInstance);

});