Skip to content

Commit

Permalink
fix buffer offset for feature state, more resilient structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Aug 17, 2018
1 parent 31ae799 commit 784293d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/data/feature_position_map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @flow

import { register } from '../util/web_worker_transfer';
import assert from 'assert';

type SerializedFeaturePositionMap = {
ids: Float64Array;
Expand All @@ -17,22 +18,24 @@ type FeaturePosition = {
export default class FeaturePositionMap {
ids: Array<number>;
positions: Array<number>;
_bufferLen: number;
indexed: boolean;

constructor() {
this.ids = [];
this.positions = [];
this._bufferLen = 0;
this.indexed = false;
}

add(id: number, index: number, newLength: number) {
add(id: number, index: number, start: number, end: number) {
this.ids.push(id);
this.positions.push(index, this._bufferLen, newLength);
this._bufferLen = newLength;
this.positions.push(index, start, end);
}

getPositions(id: number): Array<FeaturePosition> {
// binary search for the first occurrence of id in this.ids
assert(this.indexed);

// binary search for the first occurrence of id in this.ids;
// relies on ids/positions being sorted by id, which happens in serialization
let i = 0;
let j = this.ids.length - 1;
while (i < j) {
Expand Down Expand Up @@ -71,6 +74,7 @@ export default class FeaturePositionMap {
// so TypedArray vs Array distinction that flow points out doesn't matter
map.ids = (obj.ids: any);
map.positions = (obj.positions: any);
map.indexed = true;
return map;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/data/program_configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,13 +360,15 @@ export default class ProgramConfiguration {

_buffers: Array<VertexBuffer>;
_featureMap: FeaturePositionMap;
_bufferOffset: number;

constructor() {
this.binders = {};
this.cacheKey = '';

this._buffers = [];
this._featureMap = new FeaturePositionMap();
this._bufferOffset = 0;
}

static createDynamic<Layer: TypedStyleLayer>(layer: Layer, zoom: number, filterProperties: (string) => boolean) {
Expand Down Expand Up @@ -405,8 +407,9 @@ export default class ProgramConfiguration {
this.binders[property].populatePaintArray(newLength, feature);
}
if (feature.id !== undefined) {
this._featureMap.add(+feature.id, index, newLength);
this._featureMap.add(+feature.id, index, this._bufferOffset, newLength);
}
this._bufferOffset = newLength;
}

updatePaintArrays(featureStates: FeatureStates, vtLayer: VectorTileLayer, layer: TypedStyleLayer): boolean {
Expand Down

0 comments on commit 784293d

Please sign in to comment.