Skip to content

Commit

Permalink
Add functions to remove samples from SampledProperty.
Browse files Browse the repository at this point in the history
  • Loading branch information
shunter committed Apr 8, 2019
1 parent 9b6ab7b commit 4e46d04
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 38 deletions.
29 changes: 23 additions & 6 deletions Source/DataSources/SampledPositionProperty.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
define([
'../Core/Cartesian3',
'../Core/Check',
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties',
Expand All @@ -11,6 +12,7 @@ define([
'./SampledProperty'
], function(
Cartesian3,
Check,
defaultValue,
defined,
defineProperties,
Expand Down Expand Up @@ -210,12 +212,8 @@ define([
*/
SampledPositionProperty.prototype.getValueInReferenceFrame = function(time, referenceFrame, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(time)) {
throw new DeveloperError('time is required.');
}
if (!defined(referenceFrame)) {
throw new DeveloperError('referenceFrame is required.');
}
Check.defined('time', time);
Check.defined('referenceFrame', referenceFrame);
//>>includeEnd('debug');

result = this._property.getValue(time, result);
Expand Down Expand Up @@ -277,6 +275,25 @@ define([
this._property.addSamplesPackedArray(packedSamples, epoch);
};

/**
* Removes a sample at the given time, if present.
*
* @param {JulianDate} time The sample time.
* @returns {Boolean} <code>true</code> if a sample at time was removed, <code>false</code> otherwise.
*/
SampledPositionProperty.prototype.removeSample = function(time) {
this._property.removeSample(time);
};

/**
* Removes all samples for the given time interval.
*
* @param {TimeInterval} time The time interval for which to remove all samples.
*/
SampledPositionProperty.prototype.removeSamples = function(timeInterval) {
this._property.removeSamples(timeInterval);
};

/**
* Compares this property to the provided property and returns
* <code>true</code> if they are equal, <code>false</code> otherwise.
Expand Down
92 changes: 67 additions & 25 deletions Source/DataSources/SampledProperty.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
define([
'../Core/binarySearch',
'../Core/Check',
'../Core/defaultValue',
'../Core/defined',
'../Core/defineProperties',
Expand All @@ -10,6 +11,7 @@ define([
'../Core/LinearApproximation'
], function(
binarySearch,
Check,
defaultValue,
defined,
defineProperties,
Expand Down Expand Up @@ -166,9 +168,7 @@ define([
*/
function SampledProperty(type, derivativeTypes) {
//>>includeStart('debug', pragmas.debug);
if (!defined(type)) {
throw new DeveloperError('type is required.');
}
Check.defined('type', type);
//>>includeEnd('debug');

var innerType = type;
Expand Down Expand Up @@ -372,9 +372,7 @@ define([
*/
SampledProperty.prototype.getValue = function(time, result) {
//>>includeStart('debug', pragmas.debug);
if (!defined(time)) {
throw new DeveloperError('time is required.');
}
Check.defined('time', time);
//>>includeEnd('debug');

var times = this._times;
Expand Down Expand Up @@ -531,7 +529,7 @@ define([
};

/**
* Adds a new sample
* Adds a new sample.
*
* @param {JulianDate} time The sample time.
* @param {Packable} value The value at the provided time.
Expand All @@ -542,14 +540,10 @@ define([
var hasDerivatives = defined(innerDerivativeTypes);

//>>includeStart('debug', pragmas.debug);
if (!defined(time)) {
throw new DeveloperError('time is required.');
}
if (!defined(value)) {
throw new DeveloperError('value is required.');
}
if (hasDerivatives && !defined(derivatives)) {
throw new DeveloperError('derivatives is required.');
Check.defined('time', time);
Check.defined('value', value);
if (hasDerivatives) {
Check.defined('derivatives', derivatives);
}
//>>includeEnd('debug');

Expand All @@ -570,7 +564,7 @@ define([
};

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

//>>includeStart('debug', pragmas.debug);
if (!defined(times)) {
throw new DeveloperError('times is required.');
}
if (!defined(values)) {
throw new DeveloperError('values is required.');
}
Check.defined('times', times);
Check.defined('values', values);
if (times.length !== values.length) {
throw new DeveloperError('times and values must be the same length.');
}
Expand Down Expand Up @@ -627,16 +617,68 @@ define([
*/
SampledProperty.prototype.addSamplesPackedArray = function(packedSamples, epoch) {
//>>includeStart('debug', pragmas.debug);
if (!defined(packedSamples)) {
throw new DeveloperError('packedSamples is required.');
}
Check.defined('packedSamples', packedSamples);
//>>includeEnd('debug');

mergeNewSamples(epoch, this._times, this._values, packedSamples, this._packedLength);
this._updateTableLength = true;
this._definitionChanged.raiseEvent(this);
};

/**
* Removes a sample at the given time, if present.
*
* @param {JulianDate} time The sample time.
* @returns {Boolean} <code>true</code> if a sample at time was removed, <code>false</code> otherwise.
*/
SampledProperty.prototype.removeSample = function(time) {
//>>includeStart('debug', pragmas.debug);
Check.defined('time', time);
//>>includeEnd('debug');

var index = binarySearch(this._times, time, JulianDate.compare);
if (index < 0) {
return false;
}
removeSamples(this, index, 1);
return true;
};

function removeSamples(property, startIndex, numberToRemove) {
var packedLength = property._packedLength;
property._times.splice(startIndex, numberToRemove);
property._values.splice(startIndex * packedLength, numberToRemove * packedLength);
property._updateTableLength = true;
property._definitionChanged.raiseEvent(property);
}

/**
* Removes all samples for the given time interval.
*
* @param {TimeInterval} time The time interval for which to remove all samples.
*/
SampledProperty.prototype.removeSamples = function(timeInterval) {
//>>includeStart('debug', pragmas.debug);
Check.defined('timeInterval', timeInterval);
//>>includeEnd('debug');

var times = this._times;
var startIndex = binarySearch(times, timeInterval.start, JulianDate.compare);
if (startIndex < 0) {
startIndex = ~startIndex;
} else if (!timeInterval.isStartIncluded) {
++startIndex;
}
var stopIndex = binarySearch(times, timeInterval.stop, JulianDate.compare);
if (stopIndex < 0) {
stopIndex = ~stopIndex;
} else if (timeInterval.isStopIncluded) {
++stopIndex;
}

removeSamples(this, startIndex, stopIndex - startIndex);
};

/**
* Compares this property to the provided property and returns
* <code>true</code> if they are equal, <code>false</code> otherwise.
Expand Down
50 changes: 48 additions & 2 deletions Specs/DataSources/SampledPositionPropertySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defineSuite([
'Core/LagrangePolynomialApproximation',
'Core/LinearApproximation',
'Core/ReferenceFrame',
'Core/TimeInterval',
'DataSources/PositionProperty'
], function(
SampledPositionProperty,
Expand All @@ -15,6 +16,7 @@ defineSuite([
LagrangePolynomialApproximation,
LinearApproximation,
ReferenceFrame,
TimeInterval,
PositionProperty) {
'use strict';

Expand Down Expand Up @@ -110,8 +112,8 @@ defineSuite([
});

it('addSample works', function() {
var values = [new Cartesian3(7, 8, 9), new Cartesian3(8, 9, 10), new Cartesian3(9, 10, 11)];
var times = [new JulianDate(0, 0), new JulianDate(1, 0), new JulianDate(2, 0)];
var values = [new Cartesian3(7, 8, 9), new Cartesian3(8, 9, 10), new Cartesian3(9, 10, 11)];

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

it('addSamples works', function() {
var values = [new Cartesian3(7, 8, 9), new Cartesian3(8, 9, 10), new Cartesian3(9, 10, 11)];
var times = [new JulianDate(0, 0), new JulianDate(1, 0), new JulianDate(2, 0)];
var values = [new Cartesian3(7, 8, 9), new Cartesian3(8, 9, 10), new Cartesian3(9, 10, 11)];

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

it('can remove a sample at a date', function() {
var times = [new JulianDate(0, 0), new JulianDate(1, 0), new JulianDate(2, 0)];
var values = [new Cartesian3(7, 8, 9), new Cartesian3(18, 19, 110), new Cartesian3(9, 10, 11)];

var property = new SampledPositionProperty();
property.addSamples(times, values);

var listener = jasmine.createSpy('listener');
property.definitionChanged.addEventListener(listener);

property.removeSample(times[1]);

expect(listener).toHaveBeenCalledWith(property);

expect(property.getValue(times[0])).toEqual(values[0]);
// removing the sample at times[1] causes the property to interpolate
expect(property.getValue(times[1])).toEqual(new Cartesian3(8, 9, 10));
expect(property.getValue(times[2])).toEqual(values[2]);
});

it('can remove samples for a time interval', function() {
var times = [new JulianDate(0, 0), new JulianDate(1, 0), new JulianDate(2, 0), new JulianDate(3, 0)];
var values = [new Cartesian3(7, 8, 9), new Cartesian3(18, 19, 110), new Cartesian3(19, 20, 110), new Cartesian3(10, 11, 12)];

var property = new SampledPositionProperty();
property.addSamples(times, values);

var listener = jasmine.createSpy('listener');
property.definitionChanged.addEventListener(listener);

property.removeSamples(new TimeInterval({
start: times[1],
stop: times[2]
}));

expect(listener).toHaveBeenCalledWith(property);

expect(property.getValue(times[0])).toEqual(values[0]);
// removing the samples causes the property to interpolate
expect(property.getValue(times[1])).toEqual(new Cartesian3(8, 9, 10));
expect(property.getValue(times[2])).toEqual(new Cartesian3(9, 10, 11));
expect(property.getValue(times[3])).toEqual(values[3]);
});

it('addSamplesPackedArray works with derivatives', function() {
var data = [0, 7, 8, 9, 1, 0, 0, 1, 8, 9, 10, 0, 1, 0, 2, 9, 10, 11, 0, 0, 1];
var epoch = new JulianDate(0, 0);
Expand Down
Loading

0 comments on commit 4e46d04

Please sign in to comment.