Skip to content

Commit d3d3089

Browse files
authored
Merge pull request #6836 from AnalyticalGraphicsInc/computeMidpoint
Add Cartesian3.midpoint
2 parents f1e58f9 + 2266524 commit d3d3089

7 files changed

+59
-15
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Change Log
88
* 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)
99
* 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)
1010
* Improved support for polygon entities using `perPositionHeight`, including supporting vertical polygons. This also improves KML compatibility. [#6791](https://github.com/AnalyticalGraphicsInc/cesium/pull/6791)
11+
* Added `Cartesian3.midpoint` to compute the midpoint between two `Cartesian3` positions [#6836](https://github.com/AnalyticalGraphicsInc/cesium/pull/6836)
1112

1213
##### Deprecated :hourglass_flowing_sand:
1314
* 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)

Source/Core/ApproximateTerrainHeights.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ define([
9898
ellipsoid.cartographicToCartesian(Rectangle.southwest(rectangle, scratchDiagonalCartographic),
9999
scratchDiagonalCartesianSW);
100100

101-
Cartesian3.subtract(scratchDiagonalCartesianSW, scratchDiagonalCartesianNE, scratchCenterCartesian);
102-
Cartesian3.add(scratchDiagonalCartesianNE,
103-
Cartesian3.multiplyByScalar(scratchCenterCartesian, 0.5, scratchCenterCartesian), scratchCenterCartesian);
101+
Cartesian3.midpoint(scratchDiagonalCartesianSW, scratchDiagonalCartesianNE, scratchCenterCartesian);
104102
var surfacePosition = ellipsoid.scaleToGeodeticSurface(scratchCenterCartesian, scratchSurfaceCartesian);
105103
if (defined(surfacePosition)) {
106104
var distance = Cartesian3.distance(scratchCenterCartesian, surfacePosition);

Source/Core/AxisAlignedBoundingBox.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ define([
4141

4242
//If center was not defined, compute it.
4343
if (!defined(center)) {
44-
center = Cartesian3.add(this.minimum, this.maximum, new Cartesian3());
45-
Cartesian3.multiplyByScalar(center, 0.5, center);
44+
center = Cartesian3.midpoint(this.minimum, this.maximum, new Cartesian3());
4645
} else {
4746
center = Cartesian3.clone(center);
4847
}
@@ -111,8 +110,7 @@ define([
111110
maximum.y = maximumY;
112111
maximum.z = maximumZ;
113112

114-
var center = Cartesian3.add(minimum, maximum, result.center);
115-
Cartesian3.multiplyByScalar(center, 0.5, center);
113+
result.center = Cartesian3.midpoint(minimum, maximum, result.center);
116114

117115
return result;
118116
};

Source/Core/BoundingSphere.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ define([
178178
maxBoxPt.y = yMax.y;
179179
maxBoxPt.z = zMax.z;
180180

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

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

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

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

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

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

676-
var center = result.center;
677-
Cartesian3.add(corner, oppositeCorner, center);
678-
Cartesian3.multiplyByScalar(center, 0.5, center);
676+
var center = Cartesian3.midpoint(corner, oppositeCorner, result.center);
679677
result.radius = Cartesian3.distance(center, oppositeCorner);
680678
return result;
681679
};

Source/Core/Cartesian3.js

+21
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,27 @@ define([
778778
return result;
779779
};
780780

781+
/**
782+
* Computes the midpoint between the right and left Cartesian.
783+
* @param {Cartesian3} left The first Cartesian.
784+
* @param {Cartesian3} right The second Cartesian.
785+
* @param {Cartesian3} result The object onto which to store the result.
786+
* @returns {Cartesian3} The midpoint.
787+
*/
788+
Cartesian3.midpoint = function(left, right, result) {
789+
//>>includeStart('debug', pragmas.debug);
790+
Check.typeOf.object('left', left);
791+
Check.typeOf.object('right', right);
792+
Check.typeOf.object('result', result);
793+
//>>includeEnd('debug');
794+
795+
result.x = (left.x + right.x) * 0.5;
796+
result.y = (left.y + right.y) * 0.5;
797+
result.z = (left.z + right.z) * 0.5;
798+
799+
return result;
800+
};
801+
781802
/**
782803
* Returns a Cartesian3 position from longitude and latitude values given in degrees.
783804
*

Source/Core/CorridorGeometryLibrary.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ define([
8181
var leftEdge = calculatedPositions[1];
8282
startPoint = Cartesian3.fromArray(calculatedPositions[1], leftEdge.length - 3, startPoint);
8383
endPoint = Cartesian3.fromArray(calculatedPositions[0], 0, endPoint);
84-
cornerPoint = Cartesian3.multiplyByScalar(Cartesian3.add(startPoint, endPoint, cornerPoint), 0.5, cornerPoint);
84+
cornerPoint = Cartesian3.midpoint(startPoint, endPoint, cornerPoint);
8585
var firstEndCap = computeRoundCorner(cornerPoint, startPoint, endPoint, CornerType.ROUNDED, false);
8686

8787
var length = calculatedPositions.length - 1;
8888
var rightEdge = calculatedPositions[length - 1];
8989
leftEdge = calculatedPositions[length];
9090
startPoint = Cartesian3.fromArray(rightEdge, rightEdge.length - 3, startPoint);
9191
endPoint = Cartesian3.fromArray(leftEdge, 0, endPoint);
92-
cornerPoint = Cartesian3.multiplyByScalar(Cartesian3.add(startPoint, endPoint, cornerPoint), 0.5, cornerPoint);
92+
cornerPoint = Cartesian3.midpoint(startPoint, endPoint, cornerPoint);
9393
var lastEndCap = computeRoundCorner(cornerPoint, startPoint, endPoint, CornerType.ROUNDED, false);
9494

9595
return [firstEndCap, lastEndCap];

Specs/Core/Cartesian3Spec.js

+28
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,34 @@ defineSuite([
690690
expect(left).toEqual(expectedResult);
691691
});
692692

693+
it('midpoint works with a result parameter', function() {
694+
var left = new Cartesian3(0.0, 0.0, 6.0);
695+
var right = new Cartesian3(0.0, 0.0, -6.0);
696+
var result = new Cartesian3();
697+
var expectedResult = new Cartesian3(0.0, 0.0, 0.0);
698+
var returnedResult = Cartesian3.midpoint(left, right, result);
699+
expect(returnedResult).toBe(result);
700+
expect(result).toEqual(expectedResult);
701+
});
702+
703+
it('midpoint throws with no left', function() {
704+
expect(function() {
705+
return Cartesian3.midpoint(undefined, new Cartesian3(), new Cartesian3());
706+
}).toThrowDeveloperError();
707+
});
708+
709+
it('midpoint throws with no right', function() {
710+
expect(function() {
711+
return Cartesian3.midpoint(new Cartesian3(), undefined, new Cartesian3());
712+
}).toThrowDeveloperError();
713+
});
714+
715+
it('midpoint throws with no result', function() {
716+
expect(function() {
717+
return Cartesian3.midpoint(new Cartesian3(), new Cartesian3(), undefined);
718+
}).toThrowDeveloperError();
719+
});
720+
693721
it('fromSpherical throws with no spherical parameter', function() {
694722
expect(function() {
695723
Cartesian3.fromSpherical(undefined);

0 commit comments

Comments
 (0)