Skip to content

feat: Introduce SampledProperty new function getSample #12253

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

Merged
merged 8 commits into from
Nov 20, 2024
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
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 `Entity.trackingReferenceFrame` property to allow tracking entities in their own inertial reference frame. [#12194](https://github.com/CesiumGS/cesium/pull/12194)

##### Fixes :wrench:
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);
};

/**
* Retrieves the time of the provided sample associated with the index. A negative index accesses the list of samples in reverse order.
*
* @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.typeOf.number("index", index);
//>>includeEnd('debug');

const times = this._times;
const len = times.length;
if (!defined(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