CesiumGS / cesium

An open-source JavaScript library for world-class 3D globes and maps :earth_americas:
https://cesium.com/cesiumjs/
Apache License 2.0
13.03k stars 3.51k forks source link

Request `SampledProperty` exposes its `_times` #12245

Closed s3xysteak closed 1 week ago

s3xysteak commented 1 month ago

Feature

It looks like:

class SampledProperty {
  // ...
  getLatestTime() {
    return this._times[this._times.length - 1]
  }
}

Assumed API

  1. sampledProperty.getLatestTime() like above.
  2. Readonly sampledProperty.times which exposed _times attribute.
  3. sampledProperty.getTime(index) which receive a number. index < 0 indicate reverse index.
  4. sampledProperty.getTimeByIndex(index) A clear but cumbersome alias of getTime.

I prefer getTime or getTimeByIndex because it is more comprehensive than getLatestTime. Expose _times correctly maybe challenging because it is a array returned an address. We don't want user can change _times by other means than addSample(s).

Use Case:

  1. Assume we have a real-time location entity:

    const pos = new Cesium.SampledProperty(Cesium.Cartesian3)
    viewer.entities.add({
    position: new Cesium.CallbackProperty(time => {
    const val = pos.getValue(time)
    return val ?? pos.getValue(pos.getLatestTime())
    })
    })

    If just use SampledProperty, the entity will hide when SampledProperty.getValue return undefined. Most time we prefer to make it stay put if there is no value returned at that time, instead of suddenly disappear and suddenly appear when the data was updated.

  2. Similarly, for a real-time rotate entity:

    const ori = new Cesium.SampledProperty(Cesium.Quaternion)
    viewer.entities.add({
    orientation: new Cesium.CallbackProperty(time => {
    const val = ori.getValue(time)
    return val ?? ori.getValue(ori.getLatestTime())
    })
    })

    When providing a null value, the entity will suddenly return to the default value (if orientation: ori). With getLatestTime() we can make it stay to wait for the updated data.

ggetz commented 1 month ago

Thanks for the suggestion @s3xysteak.

I think if we exposed this functionality, we'd likely want to name the functions getSamples for clarity.

Is this something you'd be interested in contributing?

s3xysteak commented 1 month ago

I'm happy to create a PR for this! Additionally, getSamples seems to correspond with addSamples, which receives a list and return a list. Would using the name getSample, to align with addSample, make it more intuitive? This API is like:

function getSample(index: number): Cesium.JulianDate
ggetz commented 1 month ago

@s3xysteak Sounds great!

Yes, I agree.