Skip to content

Commit

Permalink
Merge pull request #6836 from AnalyticalGraphicsInc/computeMidpoint
Browse files Browse the repository at this point in the history
Add Cartesian3.midpoint
  • Loading branch information
bagnell authored Jul 24, 2018
2 parents f1e58f9 + 2266524 commit d3d3089
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Change Log
* Added `TimeDynamicPointCloud` for playback of time-dynamic point cloud data, where each frame is a 3D Tiles Point Cloud tile. [#6721](https://github.com/AnalyticalGraphicsInc/cesium/pull/6721)
* Added `CoplanarPolygonGeometry` and `CoplanarPolygonGeometryOutline` for drawing polygons composed of coplanar positions that are not necessarily on the ellipsoid surface. [#6769](https://github.com/AnalyticalGraphicsInc/cesium/pull/6769)
* Improved support for polygon entities using `perPositionHeight`, including supporting vertical polygons. This also improves KML compatibility. [#6791](https://github.com/AnalyticalGraphicsInc/cesium/pull/6791)
* Added `Cartesian3.midpoint` to compute the midpoint between two `Cartesian3` positions [#6836](https://github.com/AnalyticalGraphicsInc/cesium/pull/6836)

##### Deprecated :hourglass_flowing_sand:
* Support for 3D Tiles `content.url` is deprecated to reflect updates to the [3D Tiles spec](https://github.com/AnalyticalGraphicsInc/3d-tiles/pull/301). Use `content.uri instead`. Support for `content.url` will remain for backwards compatibility. [#6744](https://github.com/AnalyticalGraphicsInc/cesium/pull/6744)
Expand Down
4 changes: 1 addition & 3 deletions Source/Core/ApproximateTerrainHeights.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ define([
ellipsoid.cartographicToCartesian(Rectangle.southwest(rectangle, scratchDiagonalCartographic),
scratchDiagonalCartesianSW);

Cartesian3.subtract(scratchDiagonalCartesianSW, scratchDiagonalCartesianNE, scratchCenterCartesian);
Cartesian3.add(scratchDiagonalCartesianNE,
Cartesian3.multiplyByScalar(scratchCenterCartesian, 0.5, scratchCenterCartesian), scratchCenterCartesian);
Cartesian3.midpoint(scratchDiagonalCartesianSW, scratchDiagonalCartesianNE, scratchCenterCartesian);
var surfacePosition = ellipsoid.scaleToGeodeticSurface(scratchCenterCartesian, scratchSurfaceCartesian);
if (defined(surfacePosition)) {
var distance = Cartesian3.distance(scratchCenterCartesian, surfacePosition);
Expand Down
6 changes: 2 additions & 4 deletions Source/Core/AxisAlignedBoundingBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ define([

//If center was not defined, compute it.
if (!defined(center)) {
center = Cartesian3.add(this.minimum, this.maximum, new Cartesian3());
Cartesian3.multiplyByScalar(center, 0.5, center);
center = Cartesian3.midpoint(this.minimum, this.maximum, new Cartesian3());
} else {
center = Cartesian3.clone(center);
}
Expand Down Expand Up @@ -111,8 +110,7 @@ define([
maximum.y = maximumY;
maximum.z = maximumZ;

var center = Cartesian3.add(minimum, maximum, result.center);
Cartesian3.multiplyByScalar(center, 0.5, center);
result.center = Cartesian3.midpoint(minimum, maximum, result.center);

return result;
};
Expand Down
10 changes: 4 additions & 6 deletions Source/Core/BoundingSphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ define([
maxBoxPt.y = yMax.y;
maxBoxPt.z = zMax.z;

var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
var naiveCenter = Cartesian3.midpoint(minBoxPt, maxBoxPt, fromPointsNaiveCenterScratch);

// Begin 2nd pass to find naive radius and modify the ritter sphere.
var naiveRadius = 0;
Expand Down Expand Up @@ -451,7 +451,7 @@ define([
maxBoxPt.y = yMax.y;
maxBoxPt.z = zMax.z;

var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
var naiveCenter = Cartesian3.midpoint(minBoxPt, maxBoxPt, fromPointsNaiveCenterScratch);

// Begin 2nd pass to find naive radius and modify the ritter sphere.
var naiveRadius = 0;
Expand Down Expand Up @@ -609,7 +609,7 @@ define([
maxBoxPt.y = yMax.y;
maxBoxPt.z = zMax.z;

var naiveCenter = Cartesian3.multiplyByScalar(Cartesian3.add(minBoxPt, maxBoxPt, fromPointsScratch), 0.5, fromPointsNaiveCenterScratch);
var naiveCenter = Cartesian3.midpoint(minBoxPt, maxBoxPt, fromPointsNaiveCenterScratch);

// Begin 2nd pass to find naive radius and modify the ritter sphere.
var naiveRadius = 0;
Expand Down Expand Up @@ -673,9 +673,7 @@ define([
result = new BoundingSphere();
}

var center = result.center;
Cartesian3.add(corner, oppositeCorner, center);
Cartesian3.multiplyByScalar(center, 0.5, center);
var center = Cartesian3.midpoint(corner, oppositeCorner, result.center);
result.radius = Cartesian3.distance(center, oppositeCorner);
return result;
};
Expand Down
21 changes: 21 additions & 0 deletions Source/Core/Cartesian3.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,27 @@ define([
return result;
};

/**
* Computes the midpoint between the right and left Cartesian.
* @param {Cartesian3} left The first Cartesian.
* @param {Cartesian3} right The second Cartesian.
* @param {Cartesian3} result The object onto which to store the result.
* @returns {Cartesian3} The midpoint.
*/
Cartesian3.midpoint = function(left, right, result) {
//>>includeStart('debug', pragmas.debug);
Check.typeOf.object('left', left);
Check.typeOf.object('right', right);
Check.typeOf.object('result', result);
//>>includeEnd('debug');

result.x = (left.x + right.x) * 0.5;
result.y = (left.y + right.y) * 0.5;
result.z = (left.z + right.z) * 0.5;

return result;
};

/**
* Returns a Cartesian3 position from longitude and latitude values given in degrees.
*
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/CorridorGeometryLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ define([
var leftEdge = calculatedPositions[1];
startPoint = Cartesian3.fromArray(calculatedPositions[1], leftEdge.length - 3, startPoint);
endPoint = Cartesian3.fromArray(calculatedPositions[0], 0, endPoint);
cornerPoint = Cartesian3.multiplyByScalar(Cartesian3.add(startPoint, endPoint, cornerPoint), 0.5, cornerPoint);
cornerPoint = Cartesian3.midpoint(startPoint, endPoint, cornerPoint);
var firstEndCap = computeRoundCorner(cornerPoint, startPoint, endPoint, CornerType.ROUNDED, false);

var length = calculatedPositions.length - 1;
var rightEdge = calculatedPositions[length - 1];
leftEdge = calculatedPositions[length];
startPoint = Cartesian3.fromArray(rightEdge, rightEdge.length - 3, startPoint);
endPoint = Cartesian3.fromArray(leftEdge, 0, endPoint);
cornerPoint = Cartesian3.multiplyByScalar(Cartesian3.add(startPoint, endPoint, cornerPoint), 0.5, cornerPoint);
cornerPoint = Cartesian3.midpoint(startPoint, endPoint, cornerPoint);
var lastEndCap = computeRoundCorner(cornerPoint, startPoint, endPoint, CornerType.ROUNDED, false);

return [firstEndCap, lastEndCap];
Expand Down
28 changes: 28 additions & 0 deletions Specs/Core/Cartesian3Spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,34 @@ defineSuite([
expect(left).toEqual(expectedResult);
});

it('midpoint works with a result parameter', function() {
var left = new Cartesian3(0.0, 0.0, 6.0);
var right = new Cartesian3(0.0, 0.0, -6.0);
var result = new Cartesian3();
var expectedResult = new Cartesian3(0.0, 0.0, 0.0);
var returnedResult = Cartesian3.midpoint(left, right, result);
expect(returnedResult).toBe(result);
expect(result).toEqual(expectedResult);
});

it('midpoint throws with no left', function() {
expect(function() {
return Cartesian3.midpoint(undefined, new Cartesian3(), new Cartesian3());
}).toThrowDeveloperError();
});

it('midpoint throws with no right', function() {
expect(function() {
return Cartesian3.midpoint(new Cartesian3(), undefined, new Cartesian3());
}).toThrowDeveloperError();
});

it('midpoint throws with no result', function() {
expect(function() {
return Cartesian3.midpoint(new Cartesian3(), new Cartesian3(), undefined);
}).toThrowDeveloperError();
});

it('fromSpherical throws with no spherical parameter', function() {
expect(function() {
Cartesian3.fromSpherical(undefined);
Expand Down

0 comments on commit d3d3089

Please sign in to comment.