Skip to content

Commit

Permalink
Type rest of data directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jul 5, 2017
1 parent c9900db commit 9b49769
Show file tree
Hide file tree
Showing 23 changed files with 396 additions and 133 deletions.
1 change: 1 addition & 0 deletions flow-typed/vector-tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare module "vector-tile" {
}

declare class VectorTileFeatureImpl {
static types: ['Unknown', 'Point', 'LineString', 'Polygon'];
toGeoJSON(x: number, y: number, z: number): GeoJSONFeature;
}

Expand Down
61 changes: 54 additions & 7 deletions src/data/array_group.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,52 @@
// @flow

const ProgramConfiguration = require('./program_configuration');
const createVertexArrayType = require('./vertex_array_type');

import type StyleLayer from '../style/style_layer';
import type {ProgramInterface, PaintPropertyStatistics} from './program_configuration';
import type {
StructArray,
SerializedStructArray,
SerializedStructArrayType
} from '../util/struct_array';

class Segment {
constructor(vertexOffset, primitiveOffset) {
vertexOffset: number;
primitiveOffset: number;
vertexLength: number;
primitiveLength: number;

constructor(vertexOffset: number, primitiveOffset: number) {
this.vertexOffset = vertexOffset;
this.primitiveOffset = primitiveOffset;
this.vertexLength = 0;
this.primitiveLength = 0;
}
}

export type {Segment as Segment};

export type SerializedArrayGroup = {
layoutVertexArray: SerializedStructArray,
dynamicLayoutVertexArray: SerializedStructArray,
elementArray: SerializedStructArray,
elementArray2: SerializedStructArray,
paintVertexArrays: {[string]: {
array: SerializedStructArray,
type: SerializedStructArrayType
}},
segments: Array<Object>,
segments2: Array<Object>
}

type LayerData = {
layer: StyleLayer,
programConfiguration: ProgramConfiguration,
paintVertexArray: StructArray,
paintPropertyStatistics: PaintPropertyStatistics
}

/**
* A class that manages vertex and element arrays for a bucket. It handles initialization,
* serialization for transfer to the main thread, and certain intervening mutations.
Expand All @@ -33,7 +69,18 @@ class Segment {
* @private
*/
class ArrayGroup {
constructor(programInterface, layers, zoom) {
static MAX_VERTEX_ARRAY_LENGTH: number;

globalProperties: {zoom: number};
layoutVertexArray: StructArray;
dynamicLayoutVertexArray: StructArray;
elementArray: StructArray;
elementArray2: StructArray;
layerData: {[string]: LayerData};
segments: Array<Segment>;
segments2: Array<Segment>;

constructor(programInterface: ProgramInterface, layers: Array<StyleLayer>, zoom: number) {
this.globalProperties = {zoom};

const LayoutVertexArrayType = createVertexArrayType(programInterface.layoutAttributes);
Expand Down Expand Up @@ -66,7 +113,7 @@ class ArrayGroup {
this.segments2 = [];
}

prepareSegment(numVertices) {
prepareSegment(numVertices: number): Segment {
let segment = this.segments[this.segments.length - 1];
if (!segment || segment.vertexLength + numVertices > ArrayGroup.MAX_VERTEX_ARRAY_LENGTH) {
segment = new Segment(this.layoutVertexArray.length, this.elementArray.length);
Expand All @@ -75,7 +122,7 @@ class ArrayGroup {
return segment;
}

prepareSegment2(numVertices) {
prepareSegment2(numVertices: number): Segment {
let segment = this.segments2[this.segments2.length - 1];
if (!segment || segment.vertexLength + numVertices > ArrayGroup.MAX_VERTEX_ARRAY_LENGTH) {
segment = new Segment(this.layoutVertexArray.length, this.elementArray2.length);
Expand All @@ -84,7 +131,7 @@ class ArrayGroup {
return segment;
}

populatePaintArrays(featureProperties) {
populatePaintArrays(featureProperties: Object) {
for (const key in this.layerData) {
const layerData = this.layerData[key];
if (layerData.paintVertexArray.bytesPerElement === 0) continue;
Expand All @@ -102,7 +149,7 @@ class ArrayGroup {
return this.layoutVertexArray.length === 0;
}

serialize(transferables) {
serialize(transferables?: Array<Transferable>): SerializedArrayGroup {
return {
layoutVertexArray: this.layoutVertexArray.serialize(transferables),
dynamicLayoutVertexArray: this.dynamicLayoutVertexArray && this.dynamicLayoutVertexArray.serialize(transferables),
Expand All @@ -115,7 +162,7 @@ class ArrayGroup {
}
}

function serializePaintVertexArrays(layerData, transferables) {
function serializePaintVertexArrays(layerData: {[string]: LayerData}, transferables?: Array<Transferable>) {
const paintVertexArrays = {};
for (const layerId in layerData) {
const inputArray = layerData[layerId].paintVertexArray;
Expand Down
105 changes: 67 additions & 38 deletions src/data/bucket.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
// @flow

const ArrayGroup = require('./array_group');
const BufferGroup = require('./buffer_group');
const util = require('../util/util');

import type CollisionBoxArray from '../symbol/collision_box';
import type Style from '../style/style';
import type StyleLayer from '../style/style_layer';
import type {ProgramInterface} from './program_configuration';
import type FeatureIndex, {IndexedFeature} from './feature_index';
import type {SerializedArrayGroup} from './array_group';

export type BucketParameters = {
index: number,
layers: Array<StyleLayer>,
zoom: number,
overscaling: number,
collisionBoxArray: CollisionBoxArray,
arrays?: SerializedArrayGroup
}

export type PopulateParameters = {
featureIndex: FeatureIndex,
iconDependencies: {},
glyphDependencies: {}
}

export type SerializedBucket = {
zoom: number,
layerIds: Array<string>,
arrays: SerializedArrayGroup
}

/**
* The `Bucket` class is the single point of knowledge about turning vector
* tiles into WebGL buffers.
Expand All @@ -27,16 +56,41 @@ const util = require('../util/util');
* @private
*/
class Bucket {
/**
* @param options
* @param {number} options.zoom Zoom level of the buffers being built. May be
* a fractional zoom level.
* @param options.layer A Mapbox style layer object
* @param {Object.<string, Buffer>} options.buffers The set of `Buffer`s being
* built for this tile. This object facilitates sharing of `Buffer`s be
between `Bucket`s.
*/
constructor (options, programInterface) {
index: number;
zoom: number;
overscaling: number;
layers: Array<StyleLayer>;
buffers: BufferGroup;
arrays: ArrayGroup;

+addFeature: (feature: VectorTileFeature) => void;

static deserialize(input: Array<SerializedBucket>, style: Style): {[string]: Bucket} {
const output = {};

// Guard against the case where the map's style has been set to null while
// this bucket has been parsing.
if (!style) return output;

for (const serialized of input) {
const layers = serialized.layerIds
.map((id) => style.getLayer(id))
.filter(Boolean);

if (layers.length === 0) {
continue;
}

const bucket = layers[0].createBucket(util.extend({layers}, serialized));
for (const layer of layers) {
output[layer.id] = bucket;
}
}

return output;
}

constructor(options: BucketParameters, programInterface: ProgramInterface) {
this.zoom = options.zoom;
this.overscaling = options.overscaling;
this.layers = options.layers;
Expand All @@ -49,7 +103,7 @@ class Bucket {
}
}

populate(features, options) {
populate(features: Array<IndexedFeature>, options: PopulateParameters) {
for (const feature of features) {
if (this.layers[0].filter(feature)) {
this.addFeature(feature);
Expand All @@ -66,7 +120,7 @@ class Bucket {
return this.arrays.isEmpty();
}

serialize(transferables) {
serialize(transferables?: Array<Transferable>): SerializedBucket {
return {
zoom: this.zoom,
layerIds: this.layers.map((l) => l.id),
Expand All @@ -84,34 +138,9 @@ class Bucket {
destroy() {
if (this.buffers) {
this.buffers.destroy();
this.buffers = null;
(this : any).buffers = null;
}
}
}

module.exports = Bucket;

Bucket.deserialize = function(input, style) {
// Guard against the case where the map's style has been set to null while
// this bucket has been parsing.
if (!style) return;

const output = {};

for (const serialized of input) {
const layers = serialized.layerIds
.map((id) => style.getLayer(id))
.filter(Boolean);

if (layers.length === 0) {
continue;
}

const bucket = layers[0].createBucket(util.extend({layers}, serialized));
for (const layer of layers) {
output[layer.id] = bucket;
}
}

return output;
};
10 changes: 8 additions & 2 deletions src/data/bucket/circle_bucket.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
// @flow

const Bucket = require('../bucket');
const createElementArrayType = require('../element_array_type');
const loadGeometry = require('../load_geometry');
const EXTENT = require('../extent');

import type {BucketParameters} from '../bucket';
import type {ProgramInterface} from '../program_configuration';

const circleInterface = {
layoutAttributes: [
{name: 'a_pos', components: 2, type: 'Int16'}
Expand Down Expand Up @@ -35,11 +39,13 @@ function addCircleVertex(layoutVertexArray, x, y, extrudeX, extrudeY) {
* @private
*/
class CircleBucket extends Bucket {
constructor(options) {
static programInterface: ProgramInterface;

constructor(options: BucketParameters) {
super(options, circleInterface);
}

addFeature(feature) {
addFeature(feature: VectorTileFeature) {
const arrays = this.arrays;

for (const ring of loadGeometry(feature)) {
Expand Down
10 changes: 8 additions & 2 deletions src/data/bucket/fill_bucket.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow

const Bucket = require('../bucket');
const createElementArrayType = require('../element_array_type');
Expand All @@ -7,6 +8,9 @@ const classifyRings = require('../../util/classify_rings');
const assert = require('assert');
const EARCUT_MAX_RINGS = 500;

import type {BucketParameters} from '../bucket';
import type {ProgramInterface} from '../program_configuration';

const fillInterface = {
layoutAttributes: [
{name: 'a_pos', components: 2, type: 'Int16'}
Expand All @@ -22,11 +26,13 @@ const fillInterface = {
};

class FillBucket extends Bucket {
constructor(options) {
static programInterface: ProgramInterface;

constructor(options: BucketParameters) {
super(options, fillInterface);
}

addFeature(feature) {
addFeature(feature: VectorTileFeature) {
const arrays = this.arrays;

for (const polygon of classifyRings(loadGeometry(feature), EARCUT_MAX_RINGS)) {
Expand Down
10 changes: 8 additions & 2 deletions src/data/bucket/fill_extrusion_bucket.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @flow

const Bucket = require('../bucket');
const createElementArrayType = require('../element_array_type');
Expand All @@ -8,6 +9,9 @@ const classifyRings = require('../../util/classify_rings');
const assert = require('assert');
const EARCUT_MAX_RINGS = 500;

import type {BucketParameters} from '../bucket';
import type {ProgramInterface} from '../program_configuration';

const fillExtrusionInterface = {
layoutAttributes: [
{name: 'a_pos', components: 2, type: 'Int16'},
Expand Down Expand Up @@ -41,11 +45,13 @@ function addVertex(vertexArray, x, y, nx, ny, nz, t, e) {
}

class FillExtrusionBucket extends Bucket {
constructor(options) {
static programInterface: ProgramInterface;

constructor(options: BucketParameters) {
super(options, fillExtrusionInterface);
}

addFeature(feature) {
addFeature(feature: VectorTileFeature) {
const arrays = this.arrays;

for (const polygon of classifyRings(loadGeometry(feature), EARCUT_MAX_RINGS)) {
Expand Down
Loading

0 comments on commit 9b49769

Please sign in to comment.