diff --git a/CHANGES.md b/CHANGES.md index 31522154fcbc..c182989bf614 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -8,6 +8,7 @@ Change Log ##### Additions :tada: * Added support for a promise to a resource for `CesiumTerrainProvider`, `createTileMapServiceImageryProvider` and `Cesium3DTileset` [#6204](https://github.com/AnalyticalGraphicsInc/cesium/pull/6204) +* Added `Cesium.Math.cbrt`. [#6222](https://github.com/AnalyticalGraphicsInc/cesium/pull/6222) * `Resource` class [#6205](https://github.com/AnalyticalGraphicsInc/cesium/issues/6205) * Added `put`, `patch`, `delete`, `options` and `head` methods, so it can be used for all XHR requests. * Added `preserveQueryParameters` parameter to `getDerivedResource`, to allow us to append query parameters instead of always replacing them. diff --git a/Source/Core/Math.js b/Source/Core/Math.js index 3c90ed58b95c..174889ce7c48 100644 --- a/Source/Core/Math.js +++ b/Source/Core/Math.js @@ -835,6 +835,20 @@ define([ return Math.log(number) / Math.log(base); }; + function cbrt(number) { + var result = Math.pow(Math.abs(number), 1.0 / 3.0); + return number < 0.0 ? -result : result; + } + + /** + * Finds the cube root of a number. + * Returns NaN if number is not provided. + * + * @param {Number} [number] The number. + * @returns {Number} The result. + */ + CesiumMath.cbrt = defined(Math.cbrt) ? Math.cbrt : cbrt; + /** * @private */ diff --git a/Source/Scene/PointCloud3DTileContent.js b/Source/Scene/PointCloud3DTileContent.js index 7fbc50ceb4a8..4941ee3240af 100644 --- a/Source/Scene/PointCloud3DTileContent.js +++ b/Source/Scene/PointCloud3DTileContent.js @@ -13,6 +13,7 @@ define([ '../Core/DeveloperError', '../Core/FeatureDetection', '../Core/getStringFromTypedArray', + '../Core/Math', '../Core/Matrix3', '../Core/Matrix4', '../Core/oneTimeWarning', @@ -51,6 +52,7 @@ define([ DeveloperError, FeatureDetection, getStringFromTypedArray, + CesiumMath, Matrix3, Matrix4, oneTimeWarning, @@ -466,7 +468,7 @@ define([ // Typical use case is leaves, where lower estimates of interpoint distance might // lead to underattenuation. var sphereVolume = content._tile.contentBoundingVolume.boundingSphere.volume(); - content._baseResolutionApproximation = Math.pow(sphereVolume / pointsLength, 1/3); // IE doesn't support cbrt + content._baseResolutionApproximation = CesiumMath.cbrt(sphereVolume / pointsLength); } var scratchPointSizeAndTilesetTimeAndGeometricErrorAndDepthMultiplier = new Cartesian4(); diff --git a/Specs/Core/MathSpec.js b/Specs/Core/MathSpec.js index 30dd0c169df9..26a4e1b523ed 100644 --- a/Specs/Core/MathSpec.js +++ b/Specs/Core/MathSpec.js @@ -434,4 +434,12 @@ defineSuite([ CesiumMath.logBase(64, undefined); }).toThrowDeveloperError(); }); + + it('cbrt', function() { + expect(CesiumMath.cbrt(27.0)).toEqual(3.0); + expect(CesiumMath.cbrt(-27.0)).toEqual(-3.0); + expect(CesiumMath.cbrt(0.0)).toEqual(0.0); + expect(CesiumMath.cbrt(1.0)).toEqual(1.0); + expect(CesiumMath.cbrt()).toEqual(NaN); + }); });