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

Fix geojson EllipsoidRhumbLine error for duplicated points #12460

Merged
merged 4 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## 1.127 - 2025-03-03

### @cesium/engine

#### Fixes :wrench:

- Fixed error when there are duplicated points in polygon/polyline geometries with `ArcType.RHUMB` [#12460](https://github.com/CesiumGS/cesium/pull/12460)

## 1.126 - 2025-02-03

### @cesium/engine
Expand Down
2 changes: 1 addition & 1 deletion packages/engine/Source/Core/EllipsoidRhumbLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ EllipsoidRhumbLine.prototype.interpolateUsingFraction = function (
/**
* Provides the location of a point at the indicated distance along the rhumb line.
*
* @param {number} distance The distance from the inital point to the point of interest along the rhumbLine.
* @param {number} distance The distance from the initial point to the point of interest along the rhumbLine.
* @param {Cartographic} [result] The object in which to store the result.
* @returns {Cartographic} The location of the point along the rhumb line.
*
Expand Down
30 changes: 26 additions & 4 deletions packages/engine/Source/Core/PolygonGeometryLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,18 @@ PolygonGeometryLibrary.subdivideTexcoordRhumbLine = function (
return texcoords;
};

/**
* Subdivide the line between 2 points every minDistance length
* If the points are already closer than minDistance the first will be returned
*
* @private
* @param {Ellipsoid} ellipsoid
* @param {Cartesian3} p0 start point
* @param {Cartesian3} p1 end point
* @param {number} minDistance minimum distance between points in radians
* @param {number[]} [result] if provided positions will be packed into this array starting at index 0
* @returns {number[]} Cartesian3 positions packed into an array
*/
PolygonGeometryLibrary.subdivideRhumbLine = function (
ellipsoid,
p0,
Expand All @@ -323,15 +335,25 @@ PolygonGeometryLibrary.subdivideRhumbLine = function (
const c1 = ellipsoid.cartesianToCartographic(p1, scratchCartographic1);
const rhumb = new EllipsoidRhumbLine(c0, c1, ellipsoid);

if (!defined(result)) {
result = [];
}

if (rhumb.surfaceDistance <= minDistance) {
// no need to try and subdivide a line that's already shorter than the min distance
// this also inherently handles duplicated points which would have 0 distance
result.length = 3;
result[0] = p0.x;
result[1] = p0.y;
result[2] = p0.z;
return result;
}

const n = rhumb.surfaceDistance / minDistance;
const countDivide = Math.max(0, Math.ceil(CesiumMath.log2(n)));
const numVertices = Math.pow(2, countDivide);
const distanceBetweenVertices = rhumb.surfaceDistance / numVertices;

if (!defined(result)) {
result = [];
}

const positions = result;
positions.length = numVertices * 3;

Expand Down
53 changes: 52 additions & 1 deletion packages/engine/Specs/Core/PolygonGeometryLibrarySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,58 @@ import {
} from "../../index.js";

describe("Core/PolygonGeometryLibrary", function () {
describe("splitPolygonByPlane", function () {
describe("subdivideRhumbLine", () => {
it("returns first point if both points are the same", function () {
const p0 = new Cartesian3(3813220.0, -5085291.0, 527179.0);
const p1 = new Cartesian3(3813220.0, -5085291.0, 527179.0);
const positions = PolygonGeometryLibrary.subdivideRhumbLine(
Ellipsoid.WGS84,
p0,
p1,
2,
);
expect(positions.length).toEqual(3);
expect(positions).toEqual([3813220.0, -5085291.0, 527179.0]);
});

it("returns first point if the points are closer than minDistance", function () {
const p0 = new Cartesian3(3813220.0, -5085291.0, 527179.0);
const p1 = new Cartesian3(3813220.0, -5085291.0, 527179.0 + 1);
// actual surface distance is ~0.997
const positions = PolygonGeometryLibrary.subdivideRhumbLine(
Ellipsoid.WGS84,
p0,
p1,
2,
);
expect(positions.length).toEqual(3);
expect(positions).toEqual([3813220.0, -5085291.0, 527179.0]);
});

it("subdivides the line between 2 points", function () {
const p0 = new Cartesian3(3813220.0, -5085291.0, 527179.0);
const p1 = new Cartesian3(3813220.0, -5085291.0, 527179.0 + 5);
// actual surface distance is ~4.983
const positions = PolygonGeometryLibrary.subdivideRhumbLine(
Ellipsoid.WGS84,
p0,
p1,
2,
);
expect(positions.length).toEqual(12);
expect(positions).toEqualEpsilon(
[
3813220.447295841, -5085291.596511482, 527179.0622555692,
3813220.3851130935, -5085291.513584885, 527180.3036009098,
3813220.3229302, -5085291.430658091, 527181.5449462304,
3813220.2607471617, -5085291.347731101, 527182.7862915307,
],
CesiumMath.EPSILON7,
);
});
});

describe("splitPolygonsOnEquator", function () {
it("splits a simple polygon at the equator", function () {
const positions = Cartesian3.unpackArray([
3813220.0, -5085291.0, 527179.0, 3701301.0, -5097773.0, -993503.0,
Expand Down
Loading