Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for overflowing stops #23

Closed
wants to merge 2 commits into from

Conversation

yeldarby
Copy link
Contributor

This PR lets you pass overflow: true to an exponential function to disable clamping and calculate a value based on the base.

Behavior is identical between defined stops; it only varies outside of the defined range.

Linear functions

For linear functions it will use the slope between the two stops on the end of the range (or a slope of 1 if only one stop is given) and project out the line.

For example, the slope here is 1/2:

var f = MapboxGLFunction({
    type: 'exponential',
    stops: [[10, 1], [12, 2]],
    base: 1,
    overflow: true
});

t.equal(f(1), -3.5);
t.equal(f(8), 0);
t.equal(f(9), 0.5);
t.equal(f(10), 1);
t.equal(f(11), 1.5);
t.equal(f(12), 2);
t.equal(f(13), 2.5);
t.equal(f(14), 3);
t.equal(f(20), 6);

Exponential functions

For exponential functions it will use the base passed and extrapolate out from the nearest end stop.

For example, a single stop passed with a base of 2 means that it will double for each whole-number increase and halve for each whole-number decrease:

var f = MapboxGLFunction({
    type: 'exponential',
    stops: [[12, 1]],
    base: 2,
    overflow: true
});

t.equal(f(1), 1/2048);
t.equal(f(5), 1/128);
t.equal(f(11), 0.5);
t.equal(f(12), 1);
t.equal(f(13), 2);
t.equal(f(15), 8);
t.equal(f(20), 256);

Launch Checklist

  • briefly describe the changes in this PR
  • write tests for all new functionality
  • document any changes to public APIs -- I think this will need to be added to the style spec
  • get a PR review

Spawned from a discussion with @lucaswoj in mapbox/mapbox-gl-js#3062

@@ -45,7 +45,7 @@ function createFunction(parameters, defaultType) {
featureFunctionStops.push([featureFunctions[z].zoom, createFunction(featureFunctions[z])]);
}
fun = function(zoom, feature) {
return evaluateExponentialFunction({ stops: featureFunctionStops, base: parameters.base }, zoom)(zoom, feature);
return evaluateExponentialFunction({ stops: featureFunctionStops, base: parameters.base, overflow: parameters.overflow }, zoom)(zoom, feature);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm 👎 on the name "overflow"

overflow the generation of a number or some other data item that is too large for an assigned location or memory space.

The term that is typically used for this concept is "clamp" (for example c++ std::clamp, lodash clamp, Uint8ClampedArray)


} else if (i === parameters.stops.length) {
return parameters.stops[i - 1][1];
return overflow ? calculateOverflow(input, base, parameters.stops[i - 1], parameters.stops[i - 2]) : parameters.stops[i - 1][1];
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use still use interpolate in this case? Nothing within interpolate requires that input is within inputLower and inputUpper.

If not, calculateOverflow will need to handle arrays, like interpolate

@mourner
Copy link
Member

mourner commented Dec 16, 2016

Closing as stale. Let me know if you want to continue working on the PR.

@mourner mourner closed this Dec 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants