From 7364a7ca22342fae5d95a1af1f4532359a672b1c Mon Sep 17 00:00:00 2001 From: David Manthey Date: Tue, 6 Apr 2021 11:13:22 -0400 Subject: [PATCH] Improve unstepped contours mapping of color and range. 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. --- CHANGELOG.md | 5 +++ src/contourFeature.js | 8 +++-- src/webgl/contourFeature.frag | 2 +- tests/cases/contourFeature.js | 40 ++++++++++++++++++++++ tests/external-data/base-images.tgz.sha512 | 2 +- 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 648f8973fa..24510bb9b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/contourFeature.js b/src/contourFeature.js index 9ccefc2432..ae5c78b26b 100644 --- a/src/contourFeature.js +++ b/src/contourFeature.js @@ -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 @@ -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. */ /** @@ -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 */ diff --git a/src/webgl/contourFeature.frag b/src/webgl/contourFeature.frag index e18416cad2..11f40da3e9 100644 --- a/src/webgl/contourFeature.frag +++ b/src/webgl/contourFeature.frag @@ -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. diff --git a/tests/cases/contourFeature.js b/tests/cases/contourFeature.js index f399802360..351f651217 100644 --- a/tests/cases/contourFeature.js +++ b/tests/cases/contourFeature.js @@ -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); + }); }); }); diff --git a/tests/external-data/base-images.tgz.sha512 b/tests/external-data/base-images.tgz.sha512 index 57e243bd87..bb379de6ad 100644 --- a/tests/external-data/base-images.tgz.sha512 +++ b/tests/external-data/base-images.tgz.sha512 @@ -1 +1 @@ -81bfb7b494d0c93b3992bed1ef9f3c505c8f5959c6b07951b0ce421f197921bb1ed5d3f650344361078fa2184acefad3a0a21057f13b122bb573e2fc34f63c7e \ No newline at end of file +346dcdcf6e88aa9bfde684a311a452431af01b055f007e73839d42aa19ce0846af20d0bc296f7e1acca0af2759896d647dbbdbf07b20428f0e10464a1764c77e