Skip to content

Commit

Permalink
match serialization/deserialization convention
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Mar 16, 2016
1 parent 2f78b41 commit fe8dad8
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 27 deletions.
16 changes: 10 additions & 6 deletions js/data/bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ Bucket.prototype.createStructArrays = function() {
var vertexBufferName = this.getBufferName(shaderName, 'vertex');
var vertexAddMethodName = this.getAddMethodName(shaderName, 'vertex');

var VertexArrayType = new StructArrayType(shaderInterface.attributes, { alignment: Buffer.VERTEX_ATTRIBUTE_ALIGNMENT });
var VertexArrayType = new StructArrayType({
members: shaderInterface.attributes,
alignment: Buffer.VERTEX_ATTRIBUTE_ALIGNMENT
});

structArrays[vertexBufferName] = new VertexArrayType();
structArrayTypes[vertexBufferName] = VertexArrayType.serialize();
Expand Down Expand Up @@ -265,11 +268,12 @@ function createElementAddMethod(buffer) {
}

function createElementBufferType(components) {
return new StructArrayType([{
type: Buffer.ELEMENT_ATTRIBUTE_TYPE,
name: 'vertices',
components: components || 3
}]);
return new StructArrayType({
members: [{
type: Buffer.ELEMENT_ATTRIBUTE_TYPE,
name: 'vertices',
components: components || 3
}]});
}

function capitalize(string) {
Expand Down
10 changes: 6 additions & 4 deletions js/data/feature_tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ var vt = require('vector-tile');
var Protobuf = require('pbf');
var GeoJSONFeature = require('../util/vectortile_to_geojson');

var FeatureIndexArray = new StructArrayType([
var FeatureIndexArray = new StructArrayType({
members: [
// the index of the feature in the original vectortile
{ type: 'Uint32', name: 'featureIndex' },
// the source layer the feature appears in
{ type: 'Uint16', name: 'sourceLayerIndex' },
// the bucket the feature appears in
{ type: 'Uint16', name: 'bucketIndex' }
]);
]});

var FilteredFeatureIndexArray = new StructArrayType([
var FilteredFeatureIndexArray = new StructArrayType({
members: [
// the index of the feature in the original vectortile
{ type: 'Uint32', name: 'featureIndex' },
// the source layer the feature appears in
Expand All @@ -29,7 +31,7 @@ var FilteredFeatureIndexArray = new StructArrayType([
{ type: 'Uint16', name: 'bucketIndex' },
// the layer the feature appears in
{ type: 'Uint16', name: 'layerIndex' }
]);
]});

module.exports = FeatureTree;

Expand Down
5 changes: 3 additions & 2 deletions js/symbol/collision_box.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var Point = require('point-geometry');
* @private
*/

var CollisionBoxArray = module.exports = new StructArrayType([
var CollisionBoxArray = module.exports = new StructArrayType({
members: [
// the box is centered around the anchor point
{ type: 'Int16', name: 'anchorPointX' },
{ type: 'Int16', name: 'anchorPointY' },
Expand Down Expand Up @@ -69,7 +70,7 @@ var CollisionBoxArray = module.exports = new StructArrayType([
{ type: 'Int16', name: 'bbox3' },

{ type: 'Float32', name: 'placementScale' }
]);
]});

util.extendAll(CollisionBoxArray.prototype.StructType.prototype, {
get anchorPoint() {
Expand Down
14 changes: 8 additions & 6 deletions js/util/struct_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ var structArrayTypeCache = {};
*
* @example
*
* var PointArrayType = new StructArrayType([
* var PointArrayType = new StructArrayType({
* members: [
* { type: 'Int16', name: 'x' },
* { type: 'Int16', name: 'y' }
* ]);
* ]});
*
* var pointArray = new PointArrayType();
* pointArray.emplaceBack(10, 15);
Expand All @@ -66,14 +67,13 @@ var structArrayTypeCache = {};
*
* @private
*/
function StructArrayType(members, options) {
function StructArrayType(options) {

var key = JSON.stringify({ members: members, options: options });
var key = JSON.stringify(options);
if (structArrayTypeCache[key]) {
return structArrayTypeCache[key];
}

if (options === undefined) options = {};
if (options.alignment === undefined) options.alignment = 1;

function StructType() {
Expand All @@ -86,7 +86,7 @@ function StructArrayType(members, options) {
var maxSize = 0;
var usedTypes = ['Uint8'];

StructType.prototype.members = members.map(function(member) {
StructType.prototype.members = options.members.map(function(member) {
member = {
name: member.name,
type: member.type,
Expand Down Expand Up @@ -114,6 +114,7 @@ function StructArrayType(members, options) {
return member;
});

StructType.prototype.alignment = options.alignment;
StructType.prototype.BYTE_SIZE = align(offset, Math.max(maxSize, options.alignment));

function StructArrayType() {
Expand Down Expand Up @@ -141,6 +142,7 @@ function StructArrayType(members, options) {
function serializeStructArrayType() {
return {
members: this.prototype.StructType.prototype.members,
alignment: this.prototype.StructType.prototype.alignment,
BYTES_PER_ELEMENT: this.prototype.BYTES_PER_ELEMENT
};
}
Expand Down
11 changes: 7 additions & 4 deletions test/js/data/buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ var StructArrayType = require('../../../js/util/struct_array');

test('Buffer', function(t) {

var TestArray = new StructArrayType([
{ type: 'Int16', name: 'map' },
{ type: 'Int16', name: 'box', components: 2 }
], { alignment: 4 });
var TestArray = new StructArrayType({
members: [
{ type: 'Int16', name: 'map' },
{ type: 'Int16', name: 'box', components: 2 }
],
alignment: 4
});


t.test('constructs itself', function(t) {
Expand Down
14 changes: 9 additions & 5 deletions test/js/util/struct_array.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ var StructArrayType = require('../../../js/util/struct_array');

test('StructArray', function(t) {

var TestArray = new StructArrayType([
{ type: 'Int16', name: 'map' },
{ type: 'Int16', name: 'box', components: 2 }
], { alignment: 4 });
var TestArray = new StructArrayType({
members: [
{ type: 'Int16', name: 'map' },
{ type: 'Int16', name: 'box', components: 2 }
],
alignment: 4
});

t.test('type defined', function(t) {

Expand All @@ -24,7 +27,8 @@ test('StructArray', function(t) {
type: 'Int16',
offset: 4
}],
BYTES_PER_ELEMENT: 8
BYTES_PER_ELEMENT: 8,
alignment: 4
});

t.end();
Expand Down

0 comments on commit fe8dad8

Please sign in to comment.