Skip to content

Commit

Permalink
Improve unstepped contours mapping of color and range.
Browse files Browse the repository at this point in the history
For stepped data, the range values need to have one more entry than the
color values, since the range values define the divisions where colors
change.  For non-stepped data, we were still asking for one more range
entry than color entry, but the final range entry was not used.  Allow a
one-to-one mapping for non-stepped data, as this is more obvious.
  • Loading branch information
manthey committed Apr 6, 2021
1 parent 47a3fc3 commit 7364a7c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## Unreleased

### Improvements
- Contours with continuous (unstepped) data can specify the same number of values in `rangeValues` and `colorRange`

## Version 1.0.0

### Improvements
Expand Down
8 changes: 6 additions & 2 deletions src/contourFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var meshFeature = require('./meshFeature');
* passed the {@link geo.meshFeature.meshInfo} object.
*
* @typedef {geo.meshFeature.meshSpec} geo.contourFeature.contourSpec
* @extends geo.meshFeature.meshSpec
* @property {number} [min] Minimum contour value. If unspecified, taken from
* the computed minimum of the `value` style.
* @property {number} [max] Maximum contour value. If unspecified, taken from
Expand All @@ -63,7 +64,8 @@ var meshFeature = require('./meshFeature');
* @property {number[]} [rangeValues] An array used to map values to the
* `colorRange`. By default, values are spaced linearly. If specified, the
* entries must be increasing weakly monotonic, and there must be one more
* entry then the length of `colorRange`.
* entry then the length of `colorRange` if the contour is stepped, or the
* same length as the `colorRange` if unstepped.
*/

/**
Expand Down Expand Up @@ -162,7 +164,9 @@ var contourFeature = function (arg) {
minmax = util.getMinMaxValues(result.value, contour.get('min')(result), contour.get('max')(result));
result.minValue = minmax.min;
result.maxValue = minmax.max;
if (!rangeValues || rangeValues.length !== result.colorMap.length + 1) {
if (!rangeValues || !result.colorMap ||
(rangeValues.length !== result.colorMap.length + 1 && (
stepped || rangeValues.length !== result.colorMap.length))) {
rangeValues = null;
}
if (rangeValues) { /* ensure increasing monotonicity */
Expand Down
2 changes: 1 addition & 1 deletion src/webgl/contourFeature.frag
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main () {
step = steps - 0.5;
}
} else {
step = valueVar;
step = valueVar + 0.5;
}
// our texture is padded on either end by a repeated value to ensure
// we interpolate smoothly at the ends.
Expand Down
40 changes: 40 additions & 0 deletions tests/cases/contourFeature.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,45 @@ describe('Contour Feature', function () {
expect(result.elements.length).toBe(21); /* 7 tri. * 3 pts. */
expect(result.pos.length).toBe(24); /* 8 distinct points * 3 coor. */
});

it('stepped range and color', function () {
var contour1 = {
gridWidth: 7,
gridHeight: 2,
x0: -30,
y0: -30,
dx: 6,
dy: 6,
colorRange: ['red', 'black', 'blue'],
rangeValues: [0, 6, 12, 15],
values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
stepped: true
};
var contour = layer.createFeature('contour', {
contour: contour1, style: {value: 0}}).data(contour1.values);
var result = contour._createContours();
expect(result.rangeValues.length).toBe(4);
expect(result.factor).toBe(0.2);
});

it('continuous range and color', function () {
var contour1 = {
gridWidth: 7,
gridHeight: 2,
x0: -30,
y0: -30,
dx: 6,
dy: 6,
colorRange: ['red', 'black', 'blue'],
rangeValues: [0, 6, 15],
values: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
stepped: false
};
var contour = layer.createFeature('contour', {
contour: contour1, style: {value: 0}}).data(contour1.values);
var result = contour._createContours();
expect(result.rangeValues.length).toBe(3);
expect(result.factor).toBe(0.2);
});
});
});
2 changes: 1 addition & 1 deletion tests/external-data/base-images.tgz.sha512
Original file line number Diff line number Diff line change
@@ -1 +1 @@
81bfb7b494d0c93b3992bed1ef9f3c505c8f5959c6b07951b0ce421f197921bb1ed5d3f650344361078fa2184acefad3a0a21057f13b122bb573e2fc34f63c7e
346dcdcf6e88aa9bfde684a311a452431af01b055f007e73839d42aa19ce0846af20d0bc296f7e1acca0af2759896d647dbbdbf07b20428f0e10464a1764c77e

0 comments on commit 7364a7c

Please sign in to comment.