Skip to content

Add optional intensity parameter for surfaces #314

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

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions devtools/test_dashboard/test_gl3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@ plots['scatter3d-colorscale'] = require('@mocks/gl3d_scatter3d-colorscale.json')
plots['autocolorscale'] = require('@mocks/gl3d_autocolorscale.json');
plots['nan-holes'] = require('@mocks/gl3d_nan-holes.json');
plots['tetrahedra'] = require('@mocks/gl3d_tet.json');
plots['surface-intensity'] = require('@mocks/gl3d_surface_intensity.json');

plotButtons(plots, figDir);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"gl-scatter3d": "^1.0.4",
"gl-select-box": "^1.0.1",
"gl-spikes2d": "^1.0.1",
"gl-surface3d": "^1.1.1",
"gl-surface3d": "^1.2.2",
Copy link
Contributor

Choose a reason for hiding this comment

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

the png files below should be in ./test/image/baselines/ not ./test/image/mocks/

"mouse-change": "^1.1.1",
"mouse-wheel": "^1.0.2",
"ndarray": "^1.0.16",
Expand Down
5 changes: 5 additions & 0 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,11 @@ function cleanData(data, existingData) {
if(cont.colorscale === 'YIGnBu') cont.colorscale = 'YlGnBu';
if(cont.colorscale === 'YIOrRd') cont.colorscale = 'YlOrRd';
}
if(Plots.traceIs(trace, 'surface')) {
if('zmin' in trace) trace.cmin = trace.zmin;
if('zmax' in trace) trace.cmax = trace.zmax;
if('zauto' in trace) trace.cauto = trace.zauto;
}

// prune empty containers made before the new nestedProperty
if(emptyContainer(trace, 'line')) delete trace.line;
Expand Down
19 changes: 16 additions & 3 deletions src/traces/surface/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,16 @@ module.exports = {
valType: 'data_array',
description: 'Sets the text elements associated with each z value.'
},
zauto: colorscaleAttrs.zauto,
zmin: colorscaleAttrs.zmin,
zmax: colorscaleAttrs.zmax,
surfacecolor: {
valType: 'data_array',
description: [
'Sets the surface intensity values,',
'used for setting a color scale independent of z'
].join(' ')
},
cauto: colorscaleAttrs.zauto,
cmin: colorscaleAttrs.zmin,
cmax: colorscaleAttrs.zmax,
colorscale: colorscaleAttrs.colorscale,
autocolorscale: extendFlat({}, colorscaleAttrs.autocolorscale,
{dflt: false}),
Expand Down Expand Up @@ -161,5 +168,11 @@ module.exports = {

_nestedModules: { // nested module coupling
'colorbar': 'Colorbar'
},

_deprecated: {
zauto: colorscaleAttrs.zauto,
zmin: colorscaleAttrs.zmin,
zmax: colorscaleAttrs.zmax
}
};
6 changes: 5 additions & 1 deletion src/traces/surface/calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ var colorscaleCalc = require('../../components/colorscale/calc');

// Compute auto-z and autocolorscale if applicable
module.exports = function calc(gd, trace) {
colorscaleCalc(trace, trace.z, '', 'z');
if(trace.surfacecolor) {
colorscaleCalc(trace, trace.surfacecolor, '', 'c');
} else {
colorscaleCalc(trace, trace.z, '', 'c');
}
};
47 changes: 47 additions & 0 deletions src/traces/surface/colorbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* Copyright 2012-2016, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/


'use strict';

var d3 = require('d3');
var isNumeric = require('fast-isnumeric');

var Lib = require('../../lib');
var Plots = require('../../plots/plots');
var getColorscale = require('../../components/colorscale/get_scale');
var drawColorbar = require('../../components/colorbar/draw');


module.exports = function colorbar(gd, cd) {
var trace = cd[0].trace,
cbId = 'cb' + trace.uid,
scl = getColorscale(trace.colorscale),
zmin = trace.cmin,
zmax = trace.cmax,
vals = trace.surfacecolor || trace.z;

if(!isNumeric(zmin)) zmin = Lib.aggNums(Math.min, null, vals);
if(!isNumeric(zmax)) zmax = Lib.aggNums(Math.max, null, vals);

gd._fullLayout._infolayer.selectAll('.' + cbId).remove();

if(!trace.showscale) {
Plots.autoMargin(gd, cbId);
return;
}

var cb = cd[0].t.cb = drawColorbar(gd, cbId);
cb.fillcolor(d3.scale.linear()
.domain(scl.map(function(v) { return zmin + v[0]*(zmax-zmin); }))
.range(scl.map(function(v) { return v[1]; })))
.filllevels({start: zmin, end: zmax, size: (zmax-zmin)/254})
.options(trace.colorbar)();

Lib.markTime('done colorbar');
};
25 changes: 18 additions & 7 deletions src/traces/surface/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ var str2RgbaArray = require('../../lib/str2rgbarray');

var MIN_RESOLUTION = 128;


function SurfaceTrace(scene, surface, uid) {
this.scene = scene;
this.uid = uid;
Expand Down Expand Up @@ -136,7 +135,7 @@ function refine(coords) {
Math.floor((coords[0].shape[1]) * scaleF+1)|0 ];
var nsize = nshape[0] * nshape[1];

for(var i = 0; i < 3; ++i) {
for(var i = 0; i < coords.length; ++i) {
var padImg = padField(coords[i]);
var scaledImg = ndarray(new Float32Array(nsize), nshape);
homography(scaledImg, padImg, [scaleF, 0, 0,
Expand Down Expand Up @@ -230,9 +229,6 @@ proto.update = function(data) {
});
}

//Refine if necessary
this.dataScale = refine(coords);

var params = {
colormap: colormap,
levels: [[], [], []],
Expand All @@ -249,10 +245,24 @@ proto.update = function(data) {
dynamicColor: [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]],
dynamicWidth: [1, 1, 1],
dynamicTint: [1, 1, 1],
opacity: 1,
colorBounds: [data.zmin * scaleFactor[2], data.zmax * scaleFactor[2]]
opacity: 1
};

//Refine if necessary
if(data.surfacecolor) {
var intensity = ndarray(
new Float32Array(xlen * ylen), [xlen, ylen]);
fill(intensity, function(row, col) {
return data.surfacecolor[col][row];
});
coords.push(intensity);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

In order to preserve backward compatibility, we'll need to set the intensity field to data.z when data.surfacecolor is not set, before passing it to gl-surface3d.


this.dataScale = refine(coords);

if(data.surfacecolor) {
params.intensity = coords.pop();
}

if('opacity' in data) {
if(data.opacity < 1) {
Expand Down Expand Up @@ -300,6 +310,7 @@ proto.update = function(data) {
}

params.coords = coords;

surface.update(params);

surface.highlightEnable = highlightEnable;
Expand Down
4 changes: 3 additions & 1 deletion src/traces/surface/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
coerce('hidesurface');
coerce('opacity');

coerce('surfacecolor');

coerce('colorscale');

var dims = ['x', 'y', 'z'];
Expand Down Expand Up @@ -86,6 +88,6 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
}

colorscaleDefaults(
traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'z'}
traceIn, traceOut, layout, coerce, {prefix: '', cLetter: 'c'}
);
};
2 changes: 1 addition & 1 deletion src/traces/surface/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var Surface = {};

Surface.attributes = require('./attributes');
Surface.supplyDefaults = require('./defaults');
Surface.colorbar = require('../heatmap/colorbar');
Surface.colorbar = require('./colorbar');
Surface.calc = require('./calc');
Surface.plot = require('./convert');

Expand Down
Binary file added test/image/baselines/gl3d_surface_intensity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/image/mocks/gl3d_autocolorscale.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
["zero two", "one two", "two two"]
],
"autocolorscale": true,
"zmin": 0,
"zmax": "50"
"cmin": 0,
"cmax": "50"
Copy link
Contributor

Choose a reason for hiding this comment

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

The old zmin and zmax attributes should still work.

}
],
"layout": {
Expand Down
Binary file added test/image/mocks/gl3d_autocolorscale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/mocks/gl3d_chrisp-nan-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/mocks/gl3d_contour-lines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/mocks/gl3d_cufflinks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/mocks/gl3d_surface-lighting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading