8000 Isosurface traces by kig · Pull Request #2752 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Isosurface traces #2752

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 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
initial scaffold for isosurface traces
  • Loading branch information
kig committed Jun 18, 2018
commit 892e7ed03ab826f8f18c244ac0005279cc7259b2
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Plotly.register([
require('./surface'),
require('./mesh3d'),
require('./cone'),
require('./isosurface'),

require('./scattergeo'),
require('./choropleth'),
Expand Down
11 changes: 11 additions & 0 deletions lib/isosurface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Copyright 2012-2018, 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';

module.exports = require('../src/traces/isosurface');
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"es6-promise": "^3.0.2",
"fast-isnumeric": "^1.1.1",
"font-atlas-sdf": "^1.3.3",
"gl-isosurface3d": "https://github.com/gl-vis/gl-isosurface3d.git",
"gl-cone3d": "^1.1.0",
"gl-contour2d": "^1.1.4",
"gl-error3d": "^1.0.7",
Expand Down
182 changes: 182 additions & 0 deletions src/traces/isosurface/attributes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
/**
* Copyright 2012-2018, 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 colorAttrs = require('../../components/colorscale/color_attributes');
var colorscaleAttrs = require('../../components/colorscale/attributes');
var colorbarAttrs = require('../../components/colorbar/attributes');
var mesh3dAttrs = require('../mesh3d/attributes');
var baseAttrs = require('../../plots/attributes');

var extendFlat = require('../../lib/extend').extendFlat;

var attrs = {
x: {
valType: 'data_array',
role: 'info',
editType: 'calc+clearAxisTypes',
description: [
'Sets the x coordinates of the vector field',
'and of the displayed cones.'
].join(' ')
},
y: {
valType: 'data_array',
role: 'info',
editType: 'calc+clearAxisTypes',
description: [
'Sets the y coordinates of the vector field',
'and of the displayed cones.'
].join(' ')
},
z: {
valType: 'data_array',
role: 'info',
editType: 'calc+clearAxisTypes',
description: [
'Sets the z coordinates of the vector field',
'and of the displayed cones.'
].join(' ')
},

u: {
valType: 'data_array',
editType: 'calc',
description: 'Sets the x components of the vector field.'
},
v: {
valType: 'data_array',
editType: 'calc',
description: 'Sets the y components of the vector field.'
},
w: {
valType: 'data_array',
editType: 'calc',
description: 'Sets the z components of the vector field.'
},

// TODO add way to specify cone positions independently of the vector field
// provided, similar to MATLAB's coneplot Cx/Cy/Cz meshgrids,
// see https://www.mathworks.com/help/matlab/ref/coneplot.html
//
// Alternatively, if our goal is only to 'fill in gaps' in the vector data,
// we could try to extend the heatmap 'connectgaps' algorithm to 3D.
// From AJ: this particular algorithm which amounts to a Poisson equation,
// both for interpolation and extrapolation - is the right one to use for
// cones too. It makes a field with zero divergence, which is a good
// baseline assumption for vector fields.
//
// cones: {
// // potential attributes to add:
// //
// // - meshmode: 'cartesian-product', 'pts', 'grid'
// //
// // under `meshmode: 'grid'`
// // - (x|y|z)grid.start
// // - (x|y|z)grid.end
// // - (x|y|z)grid.size
//
// x: {
// valType: 'data_array',
// editType: 'calc',
// description: 'Sets the x coordinates of the cones to be displayed.'
// },
// y: {
// valType: 'data_array',
// editType: 'calc',
// description: 'Sets the y coordinates of the cones to be displayed.'
// },
// z: {
// valType: 'data_array',
// editType: 'calc',
// description: 'Sets the z coordinates of the cones to be displayed.'
// },
//
// editType: 'calc',
// description: [
// 'By setting `cones.x`, `cones.y` and `cones.z` to 1D arrays,',
// 'plotly creates a mesh using the cartesian product of those 3 arrays.'
// ].join(' ')
// },

sizemode: {
valType: 'enumerated',
values: ['scaled', 'absolute'],
role: 'info',
editType: 'calc',
dflt: 'scaled',
description: [
'Determines whether `sizeref` is set as a *scaled* (i.e unitless) scalar',
'(normalized by the max u/v/w norm in the vector field) or as',
'*absolute* value (in the same units as the vector field).'
].join(' ')
},
sizeref: {
valType: 'number',
role: 'info',
editType: 'calc',
min: 0,
description: [
'Adjusts the cone size scaling.',
'The size of the cones is determined by their u/v/w norm multiplied a factor and `sizeref`.',
'This factor (computed internally) corresponds to the minimum "time" to travel across',
'two successive x/y/z positions at the average velocity of those two successive positions.',
'All cones in a given trace use the same factor.',
'With `sizemode` set to *scaled*, `sizeref` is unitless, its default value is *0.5*',
'With `sizemode` set to *absolute*, `sizeref` has the same units as the u/v/w vector field,',
'its the default value is half the sample\'s maximum vector norm.'
].join(' ')
},

anchor: {
valType: 'enumerated',
role: 'info',
editType: 'calc',
values: ['tip', 'tail', 'cm', 'center'],
dflt: 'cm',
description: [
'Sets the cones\' anchor with respect to their x/y/z positions.',
'Note that *cm* denote the cone\'s center of mass which corresponds to',
'1/4 from the tail to tip.'
].join(' ')
},

text: {
valType: 'string',
role: 'info',
dflt: '',
arrayOk: true,
editType: 'calc',
description: [
'Sets the text elements associated with the cones.',
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
'these elements will be seen in the hover labels.'
].join(' ')
}
};

extendFlat(attrs, colorAttrs('', 'calc', true), {
showscale: colorscaleAttrs.showscale,
colorbar: colorbarAttrs
});
delete attrs.color;

var fromMesh3d = ['opacity', 'lightposition', 'lighting'];

fromMesh3d.forEach(function(k) {
attrs[k] = mesh3dAttrs[k];
});

attrs.hoverinfo = extendFlat({}, baseAttrs.hoverinfo, {
editType: 'calc',
flags: ['x', 'y', 'z', 'u', 'v', 'w', 'norm', 'text', 'name'],
dflt: 'x+y+z+norm+text+name'
});

module.exports = attrs;
34 changes: 34 additions & 0 deletions src/traces/isosurface/calc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright 2012-2018, 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 colorscaleCalc = require('../../components/colorscale/calc');

module.exports = function calc(gd, trace) {
var u = trace.u;
var v = trace.v;
var w = trace.w;
var len = Math.min(u.length, v.length, w.length);
var normMax = -Infinity;
var normMin = Infinity;

for(var i = 0; i < len; i++) {
var uu = u[i];
var vv = v[i];
var ww = w[i];
var norm = Math.sqrt(uu * uu + vv * vv + ww * ww);

normMax = Math.max(normMax, norm);
normMin = Math.min(normMin, norm);
}

trace._normMax = normMax;

colorscaleCalc(trace, [normMin, normMax], '', 'c');
};
147 changes: 147 additions & 0 deletions src/traces/isosurface/convert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/**
* Copyright 2012-2018, 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 isosurfacePlot = require('gl-isosurface3d');

var simpleMap = require('../../lib').simpleMap;
var parseColorScale = require('../../lib/gl_format_color').parseColorScale;

function Isosurface(scene, uid) {
this.scene = scene;
this.uid = uid;
this.mesh = null;
this.data = null;
}

var proto = Isosurface.prototype;

proto.handlePick = function(selection) {
if(selection.object === this.mesh) {
var selectIndex = selection.index = selection.data.index;
var xx = this.data.x[selectIndex];
var yy = this.data.y[selectIndex];
var zz = this.data.z[selectIndex];
var uu = this.data.u[selectIndex];
var vv = this.data.v[selectIndex];
var ww = this.data.w[selectIndex];

selection.traceCoordinate = [
xx, yy, zz,
uu, vv, ww,
Math.sqrt(uu * uu + vv * vv + ww * ww)
];

var text = this.data.text;
if(Array.isArray(text) && text[selectIndex] !== undefined) {
selection.textLabel = text[selectIndex];
} else if(text) {
selection.textLabel = text;
}

return true;
}
};

function zip3(x, y, z) {
var result = new Array(x.length);
for(var i = 0; i < x.length; i++) {
result[i] = [x[i], y[i], z[i]];
}
return result;
}

var axisName2scaleIndex = {xaxis: 0, yaxis: 1, zaxis: 2};
var anchor2coneOffset = {tip: 1, tail: 0, cm: 0.25, center: 0.5};
var anchor2coneSpan = {tip: 1, tail: 1, cm: 0.75, center: 0.5};

function convert(scene, trace) {
var sceneLayout = scene.fullSceneLayout;
var dataScale = scene.dataScale;
var isosurfaceOpts = {};

function toDataCoords(arr, axisName) {
var ax = sceneLayout[axisName];
var scale = dataScale[axisName2scaleIndex[axisName]];
return simpleMap(arr, function(v) { return ax.d2l(v) * scale; });
}

isosurfaceOpts.vectors = zip3(
toDataCoords(trace.u, 'xaxis'),
toDataCoords(trace.v, 'yaxis'),
toDataCoords(trace.w, 'zaxis')
);

isosurfaceOpts.positions = zip3(
toDataCoords(trace.x, 'xaxis'),
toDataCoords(trace.y, 'yaxis'),
toDataCoords(trace.z, 'zaxis')
);

isosurfaceOpts.colormap = parseColorScale(trace.colorscale);
isosurfaceOpts.vertexIntensityBounds = [trace.cmin / trace._normMax, trace.cmax / trace._normMax];
isosurfaceOpts.coneOffset = anchor2coneOffset[trace.anchor];

if(trace.sizemode === 'scaled') {
// unitless sizeref
isosurfaceOpts.coneSize = trace.sizeref || 0.5;
} else {
// sizeref here has unit of velocity
isosurfaceOpts.coneSize = trace.sizeref && trace._normMax ?
trace.sizeref / trace._normMax :
0.5;
}

var meshData = isosurfacePlot(isosurfaceOpts);

// pass gl-mesh3d lighting attributes
var lp = trace.lightposition;
meshData.lightPosition = [lp.x, lp.y, lp.z];
meshData.ambient = trace.lighting.ambient;
meshData.diffuse = trace.lighting.diffuse;
meshData.specular = trace.lighting.specular;
meshData.roughness = trace.lighting.roughness;
meshData.fresnel = trace.lighting.fresnel;
meshData.opacity = trace.opacity;

// stash autorange pad value
trace._pad = anchor2coneSpan[trace.anchor] * meshData.vectorScale * meshData.coneScale * trace._normMax;

return meshData;
}

proto.update = function(data) {
this.data = data;

var meshData = convert(this.scene, data);
this.mesh.update(meshData);
};

proto.dispose = function() {
this.scene.glplot.remove(this.mesh);
this.mesh.dispose();
};

function createIsosurfaceTrace(scene, data) {
var gl = scene.glplot.gl;

var meshData = convert(scene, data);
var mesh = isosurfacePlot.createMesh(gl, meshData);

var Isosurface = new Isosurface(scene, data.uid);
Isosurface.mesh = mesh;
Isosurface.data = data;
mesh._trace = Isosurface;

scene.glplot.add(mesh);

return Isosurface;
}

module.exports = createIsosurfaceTrace;
Loading
0