Skip to content

Commit 14b22b2

Browse files
authored
Merge pull request #7723 from AnalyticalGraphicsInc/removeSamples
Add functions to remove samples from SampledProperty.
2 parents afe5e3e + 524ecd0 commit 14b22b2

File tree

5 files changed

+309
-38
lines changed

5 files changed

+309
-38
lines changed

CHANGES.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Change Log
66
##### Additions :tada:
77
* Added new parameter to `PolylineGlowMaterial` called `taperPower`, that works similar to the existing `glowPower` parameter, to taper the back of the line away. [#7626](https://github.com/AnalyticalGraphicsInc/cesium/pull/7626)
88
* Added support for the `KHR_texture_transform` glTF extension. [#7549](https://github.com/AnalyticalGraphicsInc/cesium/pull/7549)
9+
* Added functions to remove samples from `SampledProperty` and `SampledPositionProperty`. [#7723](https://github.com/AnalyticalGraphicsInc/cesium/pull/7723)
910
* Added support for color-to-alpha with a threshold on imagery layers. [#7727](https://github.com/AnalyticalGraphicsInc/cesium/pull/7727)
1011

1112
##### Fixes :wrench:

Source/DataSources/SampledPositionProperty.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
define([
22
'../Core/Cartesian3',
3+
'../Core/Check',
34
'../Core/defaultValue',
45
'../Core/defined',
56
'../Core/defineProperties',
@@ -11,6 +12,7 @@ define([
1112
'./SampledProperty'
1213
], function(
1314
Cartesian3,
15+
Check,
1416
defaultValue,
1517
defined,
1618
defineProperties,
@@ -210,12 +212,8 @@ define([
210212
*/
211213
SampledPositionProperty.prototype.getValueInReferenceFrame = function(time, referenceFrame, result) {
212214
//>>includeStart('debug', pragmas.debug);
213-
if (!defined(time)) {
214-
throw new DeveloperError('time is required.');
215-
}
216-
if (!defined(referenceFrame)) {
217-
throw new DeveloperError('referenceFrame is required.');
218-
}
215+
Check.defined('time', time);
216+
Check.defined('referenceFrame', referenceFrame);
219217
//>>includeEnd('debug');
220218

221219
result = this._property.getValue(time, result);
@@ -277,6 +275,25 @@ define([
277275
this._property.addSamplesPackedArray(packedSamples, epoch);
278276
};
279277

278+
/**
279+
* Removes a sample at the given time, if present.
280+
*
281+
* @param {JulianDate} time The sample time.
282+
* @returns {Boolean} <code>true</code> if a sample at time was removed, <code>false</code> otherwise.
283+
*/
284+
SampledPositionProperty.prototype.removeSample = function(time) {
285+
this._property.removeSample(time);
286+
};
287+
288+
/**
289+
* Removes all samples for the given time interval.
290+
*
291+
* @param {TimeInterval} time The time interval for which to remove all samples.
292+
*/
293+
SampledPositionProperty.prototype.removeSamples = function(timeInterval) {
294+
this._property.removeSamples(timeInterval);
295+
};
296+
280297
/**
281298
* Compares this property to the provided property and returns
282299
* <code>true</code> if they are equal, <code>false</code> otherwise.

Source/DataSources/SampledProperty.js

+67-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
define([
22
'../Core/binarySearch',
3+
'../Core/Check',
34
'../Core/defaultValue',
45
'../Core/defined',
56
'../Core/defineProperties',
@@ -10,6 +11,7 @@ define([
1011
'../Core/LinearApproximation'
1112
], function(
1213
binarySearch,
14+
Check,
1315
defaultValue,
1416
defined,
1517
defineProperties,
@@ -166,9 +168,7 @@ define([
166168
*/
167169
function SampledProperty(type, derivativeTypes) {
168170
//>>includeStart('debug', pragmas.debug);
169-
if (!defined(type)) {
170-
throw new DeveloperError('type is required.');
171-
}
171+
Check.defined('type', type);
172172
//>>includeEnd('debug');
173173

174174
var innerType = type;
@@ -372,9 +372,7 @@ define([
372372
*/
373373
SampledProperty.prototype.getValue = function(time, result) {
374374
//>>includeStart('debug', pragmas.debug);
375-
if (!defined(time)) {
376-
throw new DeveloperError('time is required.');
377-
}
375+
Check.defined('time', time);
378376
//>>includeEnd('debug');
379377

380378
var times = this._times;
@@ -531,7 +529,7 @@ define([
531529
};
532530

533531
/**
534-
* Adds a new sample
532+
* Adds a new sample.
535533
*
536534
* @param {JulianDate} time The sample time.
537535
* @param {Packable} value The value at the provided time.
@@ -542,14 +540,10 @@ define([
542540
var hasDerivatives = defined(innerDerivativeTypes);
543541

544542
//>>includeStart('debug', pragmas.debug);
545-
if (!defined(time)) {
546-
throw new DeveloperError('time is required.');
547-
}
548-
if (!defined(value)) {
549-
throw new DeveloperError('value is required.');
550-
}
551-
if (hasDerivatives && !defined(derivatives)) {
552-
throw new DeveloperError('derivatives is required.');
543+
Check.defined('time', time);
544+
Check.defined('value', value);
545+
if (hasDerivatives) {
546+
Check.defined('derivatives', derivatives);
553547
}
554548
//>>includeEnd('debug');
555549

@@ -570,7 +564,7 @@ define([
570564
};
571565

572566
/**
573-
* Adds an array of samples
567+
* Adds an array of samples.
574568
*
575569
* @param {JulianDate[]} times An array of JulianDate instances where each index is a sample time.
576570
* @param {Packable[]} values The array of values, where each value corresponds to the provided times index.
@@ -584,12 +578,8 @@ define([
584578
var hasDerivatives = defined(innerDerivativeTypes);
585579

586580
//>>includeStart('debug', pragmas.debug);
587-
if (!defined(times)) {
588-
throw new DeveloperError('times is required.');
589-
}
590-
if (!defined(values)) {
591-
throw new DeveloperError('values is required.');
592-
}
581+
Check.defined('times', times);
582+
Check.defined('values', values);
593583
if (times.length !== values.length) {
594584
throw new DeveloperError('times and values must be the same length.');
595585
}
@@ -627,16 +617,68 @@ define([
627617
*/
628618
SampledProperty.prototype.addSamplesPackedArray = function(packedSamples, epoch) {
629619
//>>includeStart('debug', pragmas.debug);
630-
if (!defined(packedSamples)) {
631-
throw new DeveloperError('packedSamples is required.');
632-
}
620+
Check.defined('packedSamples', packedSamples);
633621
//>>includeEnd('debug');
634622

635623
mergeNewSamples(epoch, this._times, this._values, packedSamples, this._packedLength);
636624
this._updateTableLength = true;
637625
this._definitionChanged.raiseEvent(this);
638626
};
639627

628+
/**
629+
* Removes a sample at the given time, if present.
630+
*
631+
* @param {JulianDate} time The sample time.
632+
* @returns {Boolean} <code>true</code> if a sample at time was removed, <code>false</code> otherwise.
633+
*/
634+
SampledProperty.prototype.removeSample = function(time) {
635+
//>>includeStart('debug', pragmas.debug);
636+
Check.defined('time', time);
637+
//>>includeEnd('debug');
638+
639+
var index = binarySearch(this._times, time, JulianDate.compare);
640+
if (index < 0) {
641+
return false;
642+
}
643+
removeSamples(this, index, 1);
644+
return true;
645+
};
646+
647+
function removeSamples(property, startIndex, numberToRemove) {
648+
var packedLength = property._packedLength;
649+
property._times.splice(startIndex, numberToRemove);
650+
property._values.splice(startIndex * packedLength, numberToRemove * packedLength);
651+
property._updateTableLength = true;
652+
property._definitionChanged.raiseEvent(property);
653+
}
654+
655+
/**
656+
* Removes all samples for the given time interval.
657+
*
658+
* @param {TimeInterval} time The time interval for which to remove all samples.
659+
*/
660+
SampledProperty.prototype.removeSamples = function(timeInterval) {
661+
//>>includeStart('debug', pragmas.debug);
662+
Check.defined('timeInterval', timeInterval);
663+
//>>includeEnd('debug');
664+
665+
var times = this._times;
666+
var startIndex = binarySearch(times, timeInterval.start, JulianDate.compare);
667+
if (startIndex < 0) {
668+
startIndex = ~startIndex;
669+
} else if (!timeInterval.isStartIncluded) {
670+
++startIndex;
671+
}
672+
var stopIndex = binarySearch(times, timeInterval.stop, JulianDate.compare);
673+
if (stopIndex < 0) {
674+
stopIndex = ~stopIndex;
675+
} else if (timeInterval.isStopIncluded) {
676+
++stopIndex;
677+
}
678+
679+
removeSamples(this, startIndex, stopIndex - startIndex);
680+
};
681+
640682
/**
641683
* Compares this property to the provided property and returns
642684
* <code>true</code> if they are equal, <code>false</code> otherwise.

Specs/DataSources/SampledPositionPropertySpec.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defineSuite([
66
'Core/LagrangePolynomialApproximation',
77
'Core/LinearApproximation',
88
'Core/ReferenceFrame',
9+
'Core/TimeInterval',
910
'DataSources/PositionProperty'
1011
], function(
1112
SampledPositionProperty,
@@ -15,6 +16,7 @@ defineSuite([
1516
LagrangePolynomialApproximation,
1617
LinearApproximation,
1718
ReferenceFrame,
19+
TimeInterval,
1820
PositionProperty) {
1921
'use strict';
2022

@@ -110,8 +112,8 @@ defineSuite([
110112
});
111113

112114
it('addSample works', function() {
113-
var values = [new Cartesian3(7, 8, 9), new Cartesian3(8, 9, 10), new Cartesian3(9, 10, 11)];
114115
var times = [new JulianDate(0, 0), new JulianDate(1, 0), new JulianDate(2, 0)];
116+
var values = [new Cartesian3(7, 8, 9), new Cartesian3(8, 9, 10), new Cartesian3(9, 10, 11)];
115117

116118
var property = new SampledPositionProperty();
117119
property.addSample(times[0], values[0]);
@@ -125,8 +127,8 @@ defineSuite([
125127
});
126128

127129
it('addSamples works', function() {
128-
var values = [new Cartesian3(7, 8, 9), new Cartesian3(8, 9, 10), new Cartesian3(9, 10, 11)];
129130
var times = [new JulianDate(0, 0), new JulianDate(1, 0), new JulianDate(2, 0)];
131+
var values = [new Cartesian3(7, 8, 9), new Cartesian3(8, 9, 10), new Cartesian3(9, 10, 11)];
130132

131133
var property = new SampledPositionProperty();
132134
property.addSamples(times, values);
@@ -136,6 +138,50 @@ defineSuite([
136138
expect(property.getValue(new JulianDate(0.5, 0))).toEqual(new Cartesian3(7.5, 8.5, 9.5));
137139
});
138140

141+
it('can remove a sample at a date', function() {
142+
var times = [new JulianDate(0, 0), new JulianDate(1, 0), new JulianDate(2, 0)];
143+
var values = [new Cartesian3(7, 8, 9), new Cartesian3(18, 19, 110), new Cartesian3(9, 10, 11)];
144+
145+
var property = new SampledPositionProperty();
146+
property.addSamples(times, values);
147+
148+
var listener = jasmine.createSpy('listener');
149+
property.definitionChanged.addEventListener(listener);
150+
151+
property.removeSample(times[1]);
152+
153+
expect(listener).toHaveBeenCalledWith(property);
154+
155+
expect(property.getValue(times[0])).toEqual(values[0]);
156+
// removing the sample at times[1] causes the property to interpolate
157+
expect(property.getValue(times[1])).toEqual(new Cartesian3(8, 9, 10));
158+
expect(property.getValue(times[2])).toEqual(values[2]);
159+
});
160+
161+
it('can remove samples for a time interval', function() {
162+
var times = [new JulianDate(0, 0), new JulianDate(1, 0), new JulianDate(2, 0), new JulianDate(3, 0)];
163+
var values = [new Cartesian3(7, 8, 9), new Cartesian3(18, 19, 110), new Cartesian3(19, 20, 110), new Cartesian3(10, 11, 12)];
164+
165+
var property = new SampledPositionProperty();
166+
property.addSamples(times, values);
167+
168+
var listener = jasmine.createSpy('listener');
169+
property.definitionChanged.addEventListener(listener);
170+
171+
property.removeSamples(new TimeInterval({
172+
start: times[1],
173+
stop: times[2]
174+
}));
175+
176+
expect(listener).toHaveBeenCalledWith(property);
177+
178+
expect(property.getValue(times[0])).toEqual(values[0]);
179+
// removing the samples causes the property to interpolate
180+
expect(property.getValue(times[1])).toEqual(new Cartesian3(8, 9, 10));
181+
expect(property.getValue(times[2])).toEqual(new Cartesian3(9, 10, 11));
182+
expect(property.getValue(times[3])).toEqual(values[3]);
183+
});
184+
139185
it('addSamplesPackedArray works with derivatives', function() {
140186
var data = [0, 7, 8, 9, 1, 0, 0, 1, 8, 9, 10, 0, 1, 0, 2, 9, 10, 11, 0, 0, 1];
141187
var epoch = new JulianDate(0, 0);

0 commit comments

Comments
 (0)