Skip to content
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

Implement generic interpolation for points and directions, with binar… #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
203 changes: 49 additions & 154 deletions dist/LeafletPlayback.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,135 +198,31 @@ L.Playback.MoveableMarker = L.Marker.extend({
});

L.Playback = L.Playback || {};



function search(o, v, i){
var h = o.length, l = -1, m;
while(h - l > 1)
if(o[m = h + l >> 1] < v) l = m;
else h = m;
return o[h] != v ? i ? h : -1 : h;
}
L.Playback.Track = L.Class.extend({

initialize : function (geoJSON, options) {
options = options || {};
var tickLen = options.tickLen || 250;
var sampleTimes = geoJSON.properties.time;
var samples = geoJSON.geometry.coordinates;
this._staleTime = options.staleTime || 60*60*1000;
this._fadeMarkersWhenStale = options.fadeMarkersWhenStale || false;

this._geoJSON = geoJSON;
this._tickLen = tickLen;
this._ticks = [];
this._ticks = samples;
this._times = sampleTimes;
this._marker = null;
this._orientations = [];

var sampleTimes = geoJSON.properties.time;

this._orientIcon = options.orientIcons;
var previousOrientation;

var samples = geoJSON.geometry.coordinates;
var currSample = samples[0];
var nextSample = samples[1];

var currSampleTime = sampleTimes[0];
var t = currSampleTime; // t is used to iterate through tick times
var nextSampleTime = sampleTimes[1];
var tmod = t % tickLen; // ms past a tick time
var rem,
ratio;

// handle edge case of only one t sample
if (sampleTimes.length === 1) {
if (tmod !== 0)
t += tickLen - tmod;
this._ticks[t] = samples[0];
this._orientations[t] = 0;
this._startTime = t;
this._endTime = t;
return;
}

// interpolate first tick if t not a tick time
if (tmod !== 0) {
rem = tickLen - tmod;
ratio = rem / (nextSampleTime - currSampleTime);
t += rem;
this._ticks[t] = this._interpolatePoint(currSample, nextSample, ratio);
this._orientations[t] = this._directionOfPoint(currSample,nextSample);
previousOrientation = this._orientations[t];
} else {
this._ticks[t] = currSample;
this._orientations[t] = this._directionOfPoint(currSample,nextSample);
previousOrientation = this._orientations[t];
}

this._startTime = t;
t += tickLen;
while (t < nextSampleTime) {
ratio = (t - currSampleTime) / (nextSampleTime - currSampleTime);
this._ticks[t] = this._interpolatePoint(currSample, nextSample, ratio);
this._orientations[t] = this._directionOfPoint(currSample,nextSample);
previousOrientation = this._orientations[t];
t += tickLen;
}

// iterating through the rest of the samples
for (var i = 1, len = samples.length; i < len; i++) {
currSample = samples[i];
nextSample = samples[i + 1];
t = currSampleTime = sampleTimes[i];
nextSampleTime = sampleTimes[i + 1];

tmod = t % tickLen;
if (tmod !== 0 && nextSampleTime) {
rem = tickLen - tmod;
ratio = rem / (nextSampleTime - currSampleTime);
t += rem;
this._ticks[t] = this._interpolatePoint(currSample, nextSample, ratio);
if(nextSample){
this._orientations[t] = this._directionOfPoint(currSample,nextSample);
previousOrientation = this._orientations[t];
} else {
this._orientations[t] = previousOrientation;
}
} else {
this._ticks[t] = currSample;
if(nextSample){
this._orientations[t] = this._directionOfPoint(currSample,nextSample);
previousOrientation = this._orientations[t];
} else {
this._orientations[t] = previousOrientation;
}
}

t += tickLen;
while (t < nextSampleTime) {
ratio = (t - currSampleTime) / (nextSampleTime - currSampleTime);

if (nextSampleTime - currSampleTime > options.maxInterpolationTime){
this._ticks[t] = currSample;

if(nextSample){
this._orientations[t] = this._directionOfPoint(currSample,nextSample);
previousOrientation = this._orientations[t];
} else {
this._orientations[t] = previousOrientation;
}
}
else {
this._ticks[t] = this._interpolatePoint(currSample, nextSample, ratio);
if(nextSample) {
this._orientations[t] = this._directionOfPoint(currSample,nextSample);
previousOrientation = this._orientations[t];
} else {
this._orientations[t] = previousOrientation;
}
}

t += tickLen;
}
}

// the last t in the while would be past bounds
this._endTime = t - tickLen;
this._lastTick = this._ticks[this._endTime];

this._startTime = sampleTimes[0];
this._endTime = sampleTimes[sampleTimes.length - 1];
},

_interpolatePoint : function (start, end, ratio) {
Expand Down Expand Up @@ -373,11 +269,11 @@ L.Playback.Track = L.Class.extend({
},

getFirstTick : function () {
return this._ticks[this._startTime];
return this._ticks[0];
},

getLastTick : function () {
return this._ticks[this._endTime];
return this._ticks[this._ticks.length - 1];
},

getStartTime : function () {
Expand All @@ -389,26 +285,7 @@ L.Playback.Track = L.Class.extend({
},

getTickMultiPoint : function () {
var t = this.getStartTime();
var endT = this.getEndTime();
var coordinates = [];
var time = [];
while (t <= endT) {
time.push(t);
coordinates.push(this.tick(t));
t += this._tickLen;
}

return {
type : 'Feature',
geometry : {
type : 'MultiPoint',
coordinates : coordinates
},
properties : {
time : time
}
};
return this._geoJSON;
},

trackPresentAtTick : function(timestamp)
Expand All @@ -422,21 +299,43 @@ L.Playback.Track = L.Class.extend({
},

tick : function (timestamp) {
if (timestamp > this._endTime)
timestamp = this._endTime;
if (timestamp < this._startTime)
timestamp = this._startTime;
return this._ticks[timestamp];
if (timestamp >= this._endTime)
return this.getLastTick();
if (timestamp <= this._startTime)
return this.getFirstTick();

var samples = this._ticks;

var times = this._times;

var index = search(times, timestamp, true);

var currSampleTime = times[index - 1],
nextSampleTime = times[index],
currSample = samples[index - 1],
nextSample = samples[index];

if (nextSampleTime === timestamp) {
return nextSample;
}

var ratio = (timestamp - currSampleTime) / (nextSampleTime - currSampleTime);

return this._interpolatePoint(currSample, nextSample, ratio);
},

courseAtTime: function(timestamp)
{
//return 90;
if (timestamp > this._endTime)
timestamp = this._endTime;
if (timestamp < this._startTime)
timestamp = this._startTime;
return this._orientations[timestamp];
var samples = this._ticks;

var times = this._times;

var index = search(times, timestamp, true);

var currSample = samples[index - 1],
nextSample = samples[index];

return currSample && nextSample ? this._directionOfPoint(currSample,nextSample) : 0;
},

setMarker : function(timestamp, options){
Expand Down Expand Up @@ -677,10 +576,6 @@ L.Playback.Clock = L.Class.extend({
setCursor: function (ms) {
var time = parseInt(ms);
if (!time) return;
var mod = time % this._tickLen;
if (mod !== 0) {
time += this._tickLen - mod;
}
this._cursor = time;
this._trackController.tock(this._cursor, 0);
this._callbacks(this._cursor);
Expand Down
Loading