Skip to content

Make surface contour levels configurable #3469

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

Merged
merged 16 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/traces/isosurface/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function makeSliceAttr(axLetter) {
role: 'info',
description: [
'Specifies the location(s) of slices on the axis.',
'When not locations specified slices would be created for',
'When not specified slices would be created for',
'all points of the axis', axLetter, 'except start and end.'
].join(' ')
},
Expand Down
57 changes: 57 additions & 0 deletions src/traces/surface/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,62 @@ function makeContourAttr(axLetter) {
'dimension are drawn.'
].join(' ')
},
start: {
valType: 'number',
dflt: null,
role: 'style',
editType: 'plot',
// impliedEdits: {'^autocontour': false},
description: [
'Sets the starting contour level value.',
'Must be less than `contours.end`'
].join(' ')
},
end: {
valType: 'number',
dflt: null,
role: 'style',
editType: 'plot',
// impliedEdits: {'^autocontour': false},
description: [
'Sets the end contour level value.',
'Must be more than `contours.start`'
].join(' ')
},
size: {
valType: 'number',
dflt: null,
min: 0,
role: 'style',
editType: 'plot',
// impliedEdits: {'^autocontour': false},
description: [
'Sets the step between each contour level.',
'Must be positive.'
].join(' ')
},
locations: {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need locations and relative to ✅ this feature?

I would prefer making surface on-par with contour to start.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Let's put this on 1.47.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Dropped those features in 98aebc5 as surface code is already pretty complicated for us to maintain.

valType: 'data_array',
dflt: false,
role: 'info',
description: [
'Specifies the world location(s) of contours on the', axLetter, 'axis.'
].join(' ')
},
relative: {
valType: 'number',
arrayOk: true,
role: 'info',
dflt: 0,
min: 0,
max: 1,
description: [
'Specifies the local location(s) of contours on the', axLetter, 'axis.',
'For exampe 1.0 could also be applied to locate contours on data points.',
'Or 0.5 could also be applied to locate contours between data points.',
'Values less than or equal to zero and greater than one would be ignored.'
].join(' ')
},
project: {
x: makeContourProjAttr('x'),
y: makeContourProjAttr('y'),
Expand Down Expand Up @@ -284,3 +340,4 @@ colorscaleAttrs('', {

attrs.x.editType = attrs.y.editType = attrs.z.editType = 'calc+clearAxisTypes';
attrs.transforms = undefined;
delete attrs.contours.z.relative;
119 changes: 107 additions & 12 deletions src/traces/surface/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ function SurfaceTrace(scene, surface, uid) {
this.surface = surface;
this.data = null;
this.showContour = [false, false, false];
this.contourStart = [null, null, null];
this.contourEnd = [null, null, null];
this.contourSize = [0, 0, 0];
this.contourLocations = [[], [], []];
this.contourRelative = [0, 0]; // note: only available on x & y not z.
this.minValues = [Infinity, Infinity, Infinity];
this.maxValues = [-Infinity, -Infinity, -Infinity];
this.dataScaleX = 1.0;
this.dataScaleY = 1.0;
this.refineData = true;
this._interpolatedZ = false;
this.objectOffset = [0, 0, 0];
}

var proto = SurfaceTrace.prototype;
Expand Down Expand Up @@ -349,19 +354,99 @@ proto.refineCoords = function(coords) {
}
};

function insertIfNewLevel(arr, newValue) {
var found = false;
for(var k = 0; k < arr.length; k++) {
if(newValue === arr[k]) {
found = true;
break;
}
}
if(found === false) arr.push(newValue);
}

proto.setContourLevels = function() {
var nlevels = [[], [], []];
var newLevels = [[], [], []];
var useNewLevels = [false, false, false];
var needsUpdate = false;

for(var i = 0; i < 3; ++i) {
var i, j, value;

for(i = 0; i < 3; ++i) {
if(this.showContour[i]) {
needsUpdate = true;
nlevels[i] = this.scene.contourLevels[i];

if(i < 2) {
var ratios = Array.isArray(this.contourRelative[i]) ?
this.contourRelative[i] : [this.contourRelative[i]];

for(var k = 0; k < ratios.length; k++) {
var ratio = ratios[k];
if(ratio !== 0) {

var len = (i === 0) ?
this.data.z[0].length :
this.data._ylength;

for(var q = (ratio < 1) ? 1 : 0; q < len; q++) {

var here = (i === 0) ?
this.getXat(q, 0) * this.scene.dataScale[i] :
this.getYat(0, q) * this.scene.dataScale[i];

if(ratio < 1) {
var prev = (i === 0) ?
this.getXat(q - 1, 0) * this.scene.dataScale[i] :
this.getYat(0, q - 1) * this.scene.dataScale[i];

value = here * ratio + prev * (1 - ratio);
} else {
value = here;
}

insertIfNewLevel(newLevels[i], value);
useNewLevels[i] = true;
}
}
}
}

var locations = this.contourLocations[i];
if(locations !== false) useNewLevels[i] = true;
if(locations.length) {
for(j = 0; j < locations.length; j++) {
value = locations[j] * this.scene.dataScale[i];

insertIfNewLevel(newLevels[i], value);
}
}

if(
this.contourSize[i] > 0 &&
this.contourStart[i] !== null &&
this.contourEnd[i] !== null &&
this.contourEnd[i] > this.contourStart[i]
) {
useNewLevels[i] = true;

for(j = this.contourStart[i]; j < this.contourEnd[i]; j += this.contourSize[i]) {
value = j * this.scene.dataScale[i];
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tried re-using setContours? It probably gives similar results, but it would be nice to double-check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that on a different branch. Also wanted to add autocontours and ncontours in this PR.
But unfortunately that could add too much complexity while surface contours are in 3 directions not only z.


insertIfNewLevel(newLevels[i], value);
}
}

}
}

if(needsUpdate) {
this.surface.update({ levels: nlevels });
var allLevels = [[], [], []];
for(i = 0; i < 3; ++i) {
if(this.showContour[i]) {
allLevels[i] = useNewLevels[i] ? newLevels[i] : this.scene.contourLevels[i];
}
}
this.surface.update({ levels: allLevels });
}
};

Expand Down Expand Up @@ -456,15 +541,15 @@ proto.update = function(data) {
}

for(i = 0; i < 3; i++) {
data._objectOffset[i] = 0.5 * (this.minValues[i] + this.maxValues[i]);
this.objectOffset[i] = 0.5 * (this.minValues[i] + this.maxValues[i]);
}

for(i = 0; i < 3; i++) {
for(j = 0; j < xlen; j++) {
for(k = 0; k < ylen; k++) {
v = rawCoords[i][j][k];
if(v !== null && v !== undefined) {
rawCoords[i][j][k] -= data._objectOffset[i];
rawCoords[i][j][k] -= this.objectOffset[i];
}
}
}
Expand Down Expand Up @@ -564,8 +649,22 @@ proto.update = function(data) {
surface.highlightTint[i] = params.contourTint[i] = 1;
}
params.contourWidth[i] = contourParams.width;

this.contourStart[i] = contourParams.start;
this.contourEnd[i] = contourParams.end;
this.contourSize[i] = contourParams.size;

this.contourLocations[i] = contourParams.locations;
if(i < 2) this.contourRelative[i] = contourParams.relative;
} else {
this.showContour[i] = false;

this.contourStart[i] = null;
this.contourEnd[i] = null;
this.contourSize[i] = 0;

this.contourLocations[i] = [];
if(i < 2) this.contourRelative[i] = 0;
}

if(contourParams.highlight) {
Expand All @@ -579,11 +678,7 @@ proto.update = function(data) {
params.vertexColor = true;
}

params.objectOffset = [
data._objectOffset[0],
data._objectOffset[1],
data._objectOffset[2]
];
params.objectOffset = this.objectOffset;

params.coords = coords;
surface.update(params);
Expand Down
8 changes: 6 additions & 2 deletions src/traces/surface/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
traceOut._xlength = (Array.isArray(x) && Lib.isArrayOrTypedArray(x[0])) ? z.length : z[0].length;
traceOut._ylength = z.length;

traceOut._objectOffset = [0, 0, 0];

var handleCalendarDefaults = Registry.getComponentMethod('calendars', 'handleTraceDefaults');
handleCalendarDefaults(traceIn, traceOut, ['x', 'y', 'z'], layout);

Expand Down Expand Up @@ -79,12 +77,18 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
coerce(contourDim + '.color');
coerce(contourDim + '.width');
coerce(contourDim + '.usecolormap');
coerce(contourDim + '.locations');
if(i < 2) coerce(contourDim + '.relative');
}

if(highlight) {
coerce(contourDim + '.highlightcolor');
coerce(contourDim + '.highlightwidth');
}

coerce(contourDim + '.start');
coerce(contourDim + '.end');
coerce(contourDim + '.size');
}

// backward compatibility block
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
91 changes: 91 additions & 0 deletions test/image/mocks/gl3d_surface_contour_between-xy-data-points.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
{
"data": [
{
"type": "surface",
"contours": {
"x": { "show": true, "relative": 1 },
"y": { "show": true, "relative": 1 },
"z": { "show": false }
},
"x": [0, 0.25, 0.5, 0.75, 1],
"y": [0, 0.25, 0.5, 0.75, 1],
"z": [
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0]
]
},
{
"type": "surface",
"contours": {
"x": { "show": true, "relative": 0.5 },
"y": { "show": true, "relative": 0.5 },
"z": { "show": false }
},
"x": [1.5, 1.75, 2, 2.25, 2.5],
"y": [0, 0.25, 0.5, 0.75, 1],
"z": [
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0]
]
},
{
"type": "surface",
"contours": {
"x": { "show": true },
"y": { "show": true },
"z": { "show": false }
},
"x": [0, 0.25, 0.5, 0.75, 1],
"y": [1.5, 1.75, 2, 2.25, 2.5],
"z": [
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0]
]
},
{
"type": "surface",
"contours": {
"x": { "show": true, "relative": [0.25, 0.75] },
"y": { "show": true, "relative": [0.333, 0.666] },
"z": { "show": false }
},
"x": [1.5, 1.75, 2, 2.25, 2.5],
"y": [1.5, 1.75, 2, 2.25, 2.5],
"z": [
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0],
[1, 0, 1, 0, 1],
[0, 1, 0, 1, 0]
]
}
],
"layout": {
"title": "Surface contours on or between all x & y data points",
"width": 900,
"height": 600,
"scene": {
"xaxis": { "nticks": 12 },
"yaxis": { "nticks": 12 },
"zaxis": { "nticks": 12 },
"camera": {
"eye": { "x": 1, "y": 0, "z": 0.75 },
"center": { "x": 0, "y": 0, "z": 0 }
},
"aspectratio": {
"x": 1,
"y": 1,
"z": 0.2
}
}
}
}
Loading