Skip to content

Commit

Permalink
Add "bits" parameter to "loadGeometry" (#2858)
Browse files Browse the repository at this point in the history
* Add "bits" parameter to "loadGeometry"

* Eliminate magic numbers

* Simplify loadGeometry bounds lookup
  • Loading branch information
lucaswoj authored Jul 13, 2016
1 parent bdadb1d commit f8d7d1a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
6 changes: 3 additions & 3 deletions js/data/bucket/line_bucket.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ var COS_HALF_SHARP_CORNER = Math.cos(75 / 2 * (Math.PI / 180));
var SHARP_CORNER_OFFSET = 15;

// The number of bits that is used to store the line distance in the buffer.
var LINE_DISTANCE_BUFFER_BITS = 14;
var LINE_DISTANCE_BUFFER_BITS = 15;

// We don't have enough bits for the line distance as we'd like to have, so
// use this value to scale the line distance (in tile units) down to a smaller
// value. This lets us store longer distances while sacrificing precision.
var LINE_DISTANCE_SCALE = 1 / 2;

// The maximum line distance, in tile units, that fits in the buffer.
var MAX_LINE_DISTANCE = Math.pow(2, LINE_DISTANCE_BUFFER_BITS) / LINE_DISTANCE_SCALE;
var MAX_LINE_DISTANCE = Math.pow(2, LINE_DISTANCE_BUFFER_BITS - 1) / LINE_DISTANCE_SCALE;


module.exports = LineBucket;
Expand Down Expand Up @@ -86,7 +86,7 @@ LineBucket.prototype.programInterfaces = {
};

LineBucket.prototype.addFeature = function(feature) {
var lines = loadGeometry(feature);
var lines = loadGeometry(feature, LINE_DISTANCE_BUFFER_BITS);
for (var i = 0; i < lines.length; i++) {
this.addLine(
lines[i],
Expand Down
37 changes: 28 additions & 9 deletions js/data/load_geometry.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,39 @@
'use strict';

var util = require('../util/util');

var EXTENT = require('./bucket').EXTENT;
var EXTENT_MIN = EXTENT * -2;
var EXTENT_MAX = (EXTENT * 2) - 1;
var assert = require('assert');


// These bounds define the minimum and maximum supported coordinate values.
// While visible coordinates are within [0, EXTENT], tiles may theoretically
// contain cordinates within [-Infinity, Infinity]. Our range is limited by the
// number of bits used to represent the coordinate.
function createBounds(bits) {
return {
min: -1 * Math.pow(2, bits - 1),
max: Math.pow(2, bits - 1) - 1
};
}

var boundsLookup = {
15: createBounds(15),
16: createBounds(16)
};

/**
* Loads a geometry from a VectorTileFeature and scales it to the common extent
* used internally.
* @param {VectorTileFeature} feature
* @param {number} [bits=16] The number of signed integer bits available to store
* each coordinate. A warning will be issued if any coordinate will not fits
* in the specified number of bits.
* @private
*/
module.exports = function loadGeometry(feature) {
module.exports = function loadGeometry(feature, bits) {
var bounds = boundsLookup[bits || 16];
assert(bounds);

var scale = EXTENT / feature.extent;
var geometry = feature.loadGeometry();
for (var r = 0; r < geometry.length; r++) {
Expand All @@ -22,11 +44,8 @@ module.exports = function loadGeometry(feature) {
// points and we need to do the same to avoid renering differences.
point.x = Math.round(point.x * scale);
point.y = Math.round(point.y * scale);
if (
point.x < EXTENT_MIN ||
point.x > EXTENT_MAX ||
point.y < EXTENT_MIN ||
point.y > EXTENT_MAX) {

if (point.x < bounds.min || point.x > bounds.max || point.y < bounds.min || point.y > bounds.max) {
util.warnOnce('Geometry exceeds allowed extent, reduce your vector tile buffer size');
}
}
Expand Down
3 changes: 1 addition & 2 deletions test/js/data/load_geometry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('loadGeometry extent error', function(t) {
}
};

loadGeometry(feature);
loadGeometry(feature, 15);

t.equal(numWarnings, 1);

Expand All @@ -42,4 +42,3 @@ test('loadGeometry extent error', function(t) {

t.end();
});

0 comments on commit f8d7d1a

Please sign in to comment.