CesiumGS / cesium

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

Request `SampledProperty` exposes its `_times` #12245

Open s3xysteak opened 3 weeks ago

s3xysteak commented 3 weeks 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 3 weeks 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 2 weeks 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 2 weeks ago

@s3xysteak Sounds great!

Yes, I agree.