Skip to content

Commit

Permalink
minor labelling cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Mar 10, 2015
1 parent 1a4fda4 commit 97563be
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 62 deletions.
41 changes: 19 additions & 22 deletions js/data/symbol_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var shapeIcon = Shaping.shapeIcon;
var getGlyphQuads = Quads.getGlyphQuads;
var getIconQuads = Quads.getIconQuads;
var clipLine = require('../symbol/clip_line');
var Point = require('point-geometry');

var CollisionFeature = require('../symbol/collision_feature');

Expand Down Expand Up @@ -365,39 +366,35 @@ SymbolBucket.prototype.addToDebugBuffers = function() {

this.elementGroups.collisionBox = new ElementGroups(this.buffers.collisionBoxVertex);
this.elementGroups.collisionBox.makeRoomFor(0);

var cos = Math.cos(-this.collision.angle);
var sin = Math.sin(-this.collision.angle);
var buffer = this.buffers.collisionBoxVertex;
var angle = -this.collision.angle;

This comment has been minimized.

Copy link
@mourner

mourner Mar 11, 2015

Member

The original rationale for this was to calculate cos/sin once to avoid doing this tons of times inside the loop, but I'm not sure whether the win is significant. Worth measuring though.

This comment has been minimized.

Copy link
@ansis

ansis Mar 11, 2015

Author Contributor

This particular case doesn't matter because it's only for debugging.

I just checked the other spot I switched to rotate (in quad.js). It's definitely significant there, so I'll switch that back.


for (var j = 0; j < this.symbolFeatures.length; j++) {
for (var i = 0; i < 2; i++) {
var feature = this.symbolFeatures[j][i === 0 ? 'text' : 'icon'];
if (!feature) continue;

var boxes = feature.boxes;

for (var b = 0; b < boxes.length; b++) {
var box = boxes[b];
var anchor = box.anchor;

var tl = { x: box.x1 * cos - box.y1 * sin, y: box.x1 * sin + box.y1 * cos };
var tr = { x: box.x2 * cos - box.y1 * sin, y: box.x2 * sin + box.y1 * cos };
var br = { x: box.x2 * cos - box.y2 * sin, y: box.x2 * sin + box.y2 * cos };
var bl = { x: box.x1 * cos - box.y2 * sin, y: box.x1 * sin + box.y2 * cos };

var maxZoom = this.collision.zoom + Math.log(box.maxScale) / Math.LN2;
var placementZoom = this.collision.zoom + Math.log(box.placementScale) / Math.LN2;
maxZoom = Math.max(0, Math.min(24, maxZoom));
placementZoom = Math.max(0, Math.min(24, placementZoom));

this.buffers.collisionBoxVertex.add(anchor, tl, maxZoom, placementZoom);
this.buffers.collisionBoxVertex.add(anchor, tr, maxZoom, placementZoom);
this.buffers.collisionBoxVertex.add(anchor, tr, maxZoom, placementZoom);
this.buffers.collisionBoxVertex.add(anchor, br, maxZoom, placementZoom);
this.buffers.collisionBoxVertex.add(anchor, br, maxZoom, placementZoom);
this.buffers.collisionBoxVertex.add(anchor, bl, maxZoom, placementZoom);
this.buffers.collisionBoxVertex.add(anchor, bl, maxZoom, placementZoom);
this.buffers.collisionBoxVertex.add(anchor, tl, maxZoom, placementZoom);
var tl = new Point(box.x1, box.y1)._rotate(angle);
var tr = new Point(box.x2, box.y1)._rotate(angle);
var bl = new Point(box.x1, box.y2)._rotate(angle);
var br = new Point(box.x2, box.y2)._rotate(angle);

var maxZoom = Math.max(0, Math.min(25, this.collision.zoom + Math.log(box.maxScale) / Math.LN2));
var placementZoom = Math.max(0, Math.min(25, this.collision.zoom + Math.log(box.placementScale) / Math.LN2));

buffer.add(anchor, tl, maxZoom, placementZoom);
buffer.add(anchor, tr, maxZoom, placementZoom);
buffer.add(anchor, tr, maxZoom, placementZoom);
buffer.add(anchor, br, maxZoom, placementZoom);
buffer.add(anchor, br, maxZoom, placementZoom);
buffer.add(anchor, bl, maxZoom, placementZoom);
buffer.add(anchor, bl, maxZoom, placementZoom);
buffer.add(anchor, tl, maxZoom, placementZoom);

this.elementGroups.collisionBox.current.vertexLength += 8;
}
Expand Down
4 changes: 4 additions & 0 deletions js/render/draw_collision_debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ function drawPlacementDebug(painter, layer, posMatrix, tile) {
var buffer = tile.buffers.collisionBoxVertex;
var shader = painter.collisionBoxShader;

gl.enable(gl.STENCIL_TEST);

gl.switchShader(shader, posMatrix);
buffer.bind(gl, shader);
gl.lineWidth(3);
Expand All @@ -27,4 +29,6 @@ function drawPlacementDebug(painter, layer, posMatrix, tile) {
var begin = elementGroups.groups[0].vertexStartIndex;
var len = elementGroups.groups[0].vertexLength;
gl.drawArrays(gl.LINES, begin, len);

gl.disable(gl.STENCIL_TEST);
}
13 changes: 6 additions & 7 deletions js/source/vector_tile_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ VectorTileSource.prototype = util.inherit(Evented, {
},

_redoTilePlacement: function(tile) {
var source = this;

if (tile.redoingPlacement) {
tile.redoWhenDone = true;
Expand All @@ -120,17 +119,17 @@ VectorTileSource.prototype = util.inherit(Evented, {
this.dispatcher.send('redo placement', {
id: tile.uid,
source: this.id,
angle: source.map.transform.angle,
collisionDebug: source.map.collisionDebug
}, done, tile.workerID);
angle: this.map.transform.angle,
collisionDebug: this.map.collisionDebug
}, done.bind(this), tile.workerID);

function done(_, data) {
tile.reloadSymbolData(data, source.map.painter);
source.fire('tile.load', {tile: tile});
tile.reloadSymbolData(data, this.map.painter);
this.fire('tile.load', {tile: tile});

tile.redoingPlacement = false;
if (tile.redoWhenDone) {
source._redoTilePlacement(tile);
this._redoTilePlacement(tile);
tile.redoWhenDone = false;
}
}
Expand Down
1 change: 0 additions & 1 deletion js/source/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ util.extend(Worker.prototype, {
var source = params.source,
id = params.id;


if (this.loaded[source] && this.loaded[source][id]) {
var tile = this.loaded[source][id];
var result = tile.redoPlacement(params.angle, params.collisionDebug);
Expand Down
7 changes: 1 addition & 6 deletions js/source/worker_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,10 +238,8 @@ WorkerTile.prototype.redoPlacement = function(angle, collisionDebug) {
var buffers = new BufferSet();
var transferables = [];
var elementGroups = {};

//console.time('redo placement');

var collision = this.collision;

collision.reset(angle);

var bucketsInOrder = this.bucketsInOrder;
Expand All @@ -254,13 +252,10 @@ WorkerTile.prototype.redoPlacement = function(angle, collisionDebug) {
}
}


for (var k in buffers) {
transferables.push(buffers[k].array);
}

//console.timeEnd('redo placement');

return {
result: {
elementGroups: elementGroups,
Expand Down
26 changes: 8 additions & 18 deletions js/symbol/collision_feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,35 @@ function CollisionFeature(geometry, anchor, shaped, boxScale, padding, alignLine
}
}

function bboxifyLabel(polyline, anchor, labelLength, size) {
var step = size / 2;

// polyline: array of coordinates [ {x: x0, y: y0}, ..., {x: xn, y: yn} ]
// anchor: { segment: i, x: x0, y: y0 }
// labelLength: length of labels in pixel units
// size: length of the box sides

function bboxifyLabel(line, anchor, labelLength, boxSize) {
// Determine the bounding boxes needed to cover the label in the
// neighborhood of the anchor.

// Keep track of segment lengths
var cumulativeDistances = getCumulativeDistances(polyline);

var anchorSegmentDistance = anchor.dist(polyline[anchor.segment]);
var step = boxSize / 2;

var cumulativeDistances = getCumulativeDistances(line);
var anchorSegmentDistance = anchor.dist(line[anchor.segment]);
var anchorLineCoordinate = cumulativeDistances[anchor.segment] + anchorSegmentDistance;

// Determine where the 1st and last bounding boxes
// lie on the line reference frame
var labelStartLineCoordinate = anchorLineCoordinate - 0.5 * labelLength;

// offset the center of the first box by half a box
labelStartLineCoordinate += size / 2;
labelStartLineCoordinate += boxSize / 2;

var nBoxes = Math.floor(labelLength / step);

// Create boxes with constant packing
var bboxes = [];
var nBoxes = Math.floor(labelLength / step);
for (var i = 0; i < nBoxes; i++) {

var lineCoordinate = labelStartLineCoordinate + i * step;

var p = getPointAtDistance(cumulativeDistances, lineCoordinate, polyline);
var p = getPointAtDistance(cumulativeDistances, lineCoordinate, line);
var distanceToAnchor = Math.abs(lineCoordinate - anchorLineCoordinate);
var distanceToInnerEdge = Math.max(distanceToAnchor - step / 2, 0);
var maxScale = labelLength / 2 / distanceToInnerEdge;

bboxes.push(new CollisionBox(p, -size / 2, -size / 2, size / 2, size / 2, maxScale));
bboxes.push(new CollisionBox(p, -boxSize / 2, -boxSize / 2, boxSize / 2, boxSize / 2, maxScale));
}

return bboxes;
Expand Down
8 changes: 0 additions & 8 deletions js/symbol/collision_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ CollisionTile.prototype.reset = function(angle) {
this.angle = angle;
};

CollisionTile.prototype.addLayer = function(placementLayer) {
this.placementLayers.push(placementLayer);

for (var i = 0; i < placementLayer.features.length; i++) {
this.placeFeature(placementLayer.features[i]);
}
};

CollisionTile.prototype.placeFeature = function(feature) {

var minPlacementScale = this.minScale;
Expand Down

0 comments on commit 97563be

Please sign in to comment.