Skip to content

Commit

Permalink
Update SymbolBucket unit test to work with viewport collision detecti…
Browse files Browse the repository at this point in the history
…on logic.
  • Loading branch information
ChrisLoer committed Aug 16, 2017
1 parent 9585e6f commit dbb54da
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions test/unit/data/symbol_bucket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const GlyphAtlas = require('../../../src/symbol/glyph_atlas');
const StyleLayer = require('../../../src/style/style_layer');
const util = require('../../../src/util/util');
const featureFilter = require('../../../src/style-spec/feature_filter');
const PrepareSymbol = require('../../../src/symbol/prepare_symbol');
const PlaceSymbol = require('../../../src/symbol/place_symbols');
const Transform = require('../../../src/geo/transform');

const mat4 = require('@mapbox/gl-matrix').mat4;

// Load a point feature from fixture tile.
const vt = new VectorTile(new Protobuf(fs.readFileSync(path.join(__dirname, '/../../fixtures/mbsv5-6-18-23.vector.pbf'))));
Expand All @@ -20,7 +25,21 @@ const glyphs = JSON.parse(fs.readFileSync(path.join(__dirname, '/../../fixtures/

/*eslint new-cap: 0*/
const collisionBoxArray = new CollisionBoxArray();
const collision = new CollisionTile(0, 0, 1, 1, collisionBoxArray);
const transform = new Transform();
transform.width = 100;
transform.height = 100;
transform.cameraToCenterDistance = 100;
const labelPlaneMatrix = mat4.identity(new Float64Array(16));
// This is a bogus projection matrix: all it does is make tile coordinates
// project to somewhere within the viewport, assuming a tile extent of 8192.
mat4.scale(labelPlaneMatrix, labelPlaneMatrix, [1 / 8192, 1 / 8192, 1]);
const collision = new CollisionTile(transform);
collision.setMatrix(labelPlaneMatrix);
const showCollisionBoxes = false;
const zoom = 0;
const pixelRatio = 1;
const tileID = 0;

const atlas = new GlyphAtlas();
for (const id in glyphs) {
glyphs[id].bitmap = true;
Expand Down Expand Up @@ -51,39 +70,38 @@ test('SymbolBucket', (t) => {
const options = {iconDependencies: {}, glyphDependencies: {}};

// add feature from bucket A
const a = collision.grid.keys.length;
const a = collision.grid.keysLength();
bucketA.populate([{feature}], options);
bucketA.prepare(stacks, {});
bucketA.place(collision);
PrepareSymbol.prepare(bucketA, stacks, {});
PlaceSymbol.place(bucketA, collision, showCollisionBoxes, zoom, pixelRatio, labelPlaneMatrix, tileID, collisionBoxArray);

const b = collision.grid.keys.length;
const b = collision.grid.keysLength();
t.notEqual(a, b, 'places feature');

// add same feature from bucket B
const a2 = collision.grid.keys.length;
const a2 = collision.grid.keysLength();
bucketB.populate([{feature}], options);
bucketB.prepare(stacks, {});
bucketB.place(collision);
const b2 = collision.grid.keys.length;
t.equal(a2, b2, 'detects collision and does not place feature');
PrepareSymbol.prepare(bucketB, stacks, {});
PlaceSymbol.place(bucketB, collision, showCollisionBoxes, zoom, pixelRatio, labelPlaneMatrix, tileID, collisionBoxArray);
const b2 = collision.grid.keysLength();
t.equal(b2, a2, 'detects collision and does not place feature');
t.end();
});


test('SymbolBucket integer overflow', (t) => {
t.stub(util, 'warnOnce');
t.stub(SymbolBucket, 'MAX_INSTANCES').value(5);
t.stub(SymbolBucket, 'MAX_GLYPHS').value(5);

const bucket = bucketSetup();
const options = {iconDependencies: {}, glyphDependencies: {}};

bucket.populate([{feature}], options);
bucket.prepare(stacks, {});
bucket.place(collision);
PrepareSymbol.prepare(bucket, stacks, {});
PlaceSymbol.place(bucket, collision, showCollisionBoxes, zoom, pixelRatio, labelPlaneMatrix, tileID, collisionBoxArray);

t.ok(util.warnOnce.calledTwice);
t.ok(util.warnOnce.getCall(0).calledWithMatch(/Too many (symbols|glyphs) being rendered in a tile./));
t.ok(util.warnOnce.getCall(1).calledWithMatch(/Too many (symbols|glyphs) being rendered in a tile./));
t.ok(util.warnOnce.calledOnce);
t.ok(util.warnOnce.getCall(0).calledWithMatch(/Too many glyphs being rendered in a tile./));
t.end();
});

Expand All @@ -92,9 +110,9 @@ test('SymbolBucket redo placement', (t) => {
const options = {iconDependencies: {}, glyphDependencies: {}};

bucket.populate([{feature}], options);
bucket.prepare(stacks, {});
bucket.place(collision);
bucket.place(collision);
PrepareSymbol.prepare(bucket, stacks, {});
PlaceSymbol.place(bucket, collision, showCollisionBoxes, zoom, pixelRatio, labelPlaneMatrix, tileID, collisionBoxArray);
PlaceSymbol.place(bucket, collision, showCollisionBoxes, zoom, pixelRatio, labelPlaneMatrix, tileID, collisionBoxArray);

t.end();
});

0 comments on commit dbb54da

Please sign in to comment.