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

feat: Introduce SampledProperty new function getSample #12253

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

##### Additions :tada:

- Added `getSample` to `SampledProperty` to get the time of samples. [#12253](https://github.com/CesiumGS/cesium/pull/12253)
- Added `ScreenSpaceCameraController.maximumTiltAngle` to limit how much the camera can tilt. [#12169](https://github.com/CesiumGS/cesium/pull/12169)

### 1.122 - 2024-10-01
Expand Down
24 changes: 24 additions & 0 deletions packages/engine/Source/DataSources/SampledProperty.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,30 @@ SampledProperty.prototype.addSamples = function (
this._definitionChanged.raiseEvent(this);
};

/**
* Gets the time of the sample. Negative for backward.
*
* @param {number} index The index of samples list.
* @returns {JulianDate | undefined} The JulianDate time of the sample, or undefined if failed.
*/
SampledProperty.prototype.getSample = function (index) {
//>>includeStart('debug', pragmas.debug);
Check.defined("index", index);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you think it would be good to check that the index is within range? Or is it better to allow an out of range index and return undefined?

if (index < -this._times.length || index >= this._times.length) {
throw new DeveloperError("Index out of bounds.");
}

//>>includeEnd('debug');

const times = this._times;
const len = times.length;
if (!len) {
return undefined;
}

if (index < 0) {
index += len;
}

return times[index];
};

/**
* Adds samples as a single packed array where each new sample is represented as a date,
* followed by the packed representation of the corresponding value and derivatives.
Expand Down
22 changes: 22 additions & 0 deletions packages/engine/Specs/DataSources/SampledPropertySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ describe("DataSources/SampledProperty", function () {
expect(property.getValue(new JulianDate(0.5, 0))).toEqual(7.5);
});

it("getSample works", function () {
const times = [
new JulianDate(0, 0),
new JulianDate(1, 0),
new JulianDate(2, 0),
];
const values = [7, 8, 9];

const property = new SampledProperty(Number);
property.addSamples(times, values);

expect(property.getSample(0)).toEqual(times[0]);
expect(property.getSample(1)).toEqual(times[1]);
expect(property.getSample(2)).toEqual(times[2]);
expect(property.getSample(3)).toBe(undefined);

expect(property.getSample(-1)).toBe(times[2]);
expect(property.getSample(-2)).toBe(times[1]);
expect(property.getSample(-3)).toBe(times[0]);
expect(property.getSample(-4)).toBe(undefined);
});

it("can remove a sample at a date", function () {
const times = [
new JulianDate(0, 0),
Expand Down
Loading