|
| 1 | +/** |
| 2 | +* Copyright 2012-2018, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | +/* |
| 10 | + Usage example: |
| 11 | +
|
| 12 | + var x = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]; |
| 13 | + var y = x, z = x; |
| 14 | + var len = x.length * y.length * z.length; |
| 15 | + var u=[],v=[],w=[]; for (var i=0; i<len; i++) { u.push(1+Math.sin(i)); v.push(Math.cos(i)); w.push(Math.sin(i*0.3)*0.3); } |
| 16 | + var cx=[],cy=[],cz=[]; for (var i=0; i<7; i++) for(var j=0; j<7; j++) { cx.push(-5); cy.push(i-3); cz.push(j-3); } |
| 17 | +
|
| 18 | + Plotly.newPlot(gd, [{ |
| 19 | + type: 'streamline', |
| 20 | + cx, cy, cz, |
| 21 | + u, v, w, |
| 22 | + x, y, z, |
| 23 | + bounds: [[-5, -5, -5], [5, 5, 5]], |
| 24 | + widthScale: 100, |
| 25 | + colormap:'portland' |
| 26 | + }], { |
| 27 | + scene: { |
| 28 | + xaxis: {range: [-5, 5]}, |
| 29 | + yaxis: {range: [-5, 5]}, |
| 30 | + zaxis: {range: [-5, 5]} |
| 31 | + } |
| 32 | + }) |
| 33 | +
|
| 34 | +*/ |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +'use strict'; |
| 39 | + |
| 40 | +var tube2mesh = require('gl-streamtube3d'); |
| 41 | +var createMesh = tube2mesh.createTubeMesh; |
| 42 | + |
| 43 | +function Mesh3DTrace(scene, mesh, uid) { |
| 44 | + this.scene = scene; |
| 45 | + this.uid = uid; |
| 46 | + this.mesh = mesh; |
| 47 | + this.name = ''; |
| 48 | + this.color = '#fff'; |
| 49 | + this.data = null; |
| 50 | + this.showContour = false; |
| 51 | +} |
| 52 | + |
| 53 | +var proto = Mesh3DTrace.prototype; |
| 54 | + |
| 55 | +proto.handlePick = function(selection) { |
| 56 | + if(selection.object === this.mesh) { |
| 57 | + var selectIndex = selection.index = selection.data.index; |
| 58 | + |
| 59 | + selection.traceCoordinate = [ |
| 60 | + this.data.x[selectIndex], |
| 61 | + this.data.y[selectIndex], |
| 62 | + this.data.z[selectIndex] |
| 63 | + ]; |
| 64 | + |
| 65 | + var text = this.data.text; |
| 66 | + if(Array.isArray(text) && text[selectIndex] !== undefined) { |
| 67 | + selection.textLabel = text[selectIndex]; |
| 68 | + } else if(text) { |
| 69 | + selection.textLabel = text; |
| 70 | + } |
| 71 | + |
| 72 | + return true; |
| 73 | + } |
| 74 | +}; |
| 75 | + |
| 76 | +function zip3(x, y, z) { |
| 77 | + var result = new Array(x.length); |
| 78 | + for(var i = 0; i < x.length; ++i) { |
| 79 | + result[i] = [x[i], y[i], z[i]]; |
| 80 | + } |
| 81 | + return result; |
| 82 | +} |
| 83 | + |
| 84 | +function convert(scene, trace) { |
| 85 | + var layout = scene.fullSceneLayout; |
| 86 | + |
| 87 | + // Unpack position data |
| 88 | + function toDataCoords(axis, coord, scale) { |
| 89 | + return coord.map(function(x) { |
| 90 | + return axis.d2l(x) * scale; |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + function min(a) { |
| 95 | + var result = a[0]; |
| 96 | + for (var i=1; i<a.length; i++) { |
| 97 | + if (a[i] < result) { |
| 98 | + result = a[i]; |
| 99 | + } |
| 100 | + } |
| 101 | + return result; |
| 102 | + } |
| 103 | + |
| 104 | + function max(a) { |
| 105 | + var result = a[0]; |
| 106 | + for (var i=1; i<a.length; i++) { |
| 107 | + if (a[i] > result) { |
| 108 | + result = a[i]; |
| 109 | + } |
| 110 | + } |
| 111 | + return result; |
| 112 | + } |
| 113 | + |
| 114 | + var params = { |
| 115 | + startingPositions: zip3( |
| 116 | + toDataCoords(layout.xaxis, trace.cx, scene.dataScale[0]), |
| 117 | + toDataCoords(layout.yaxis, trace.cy, scene.dataScale[1]), |
| 118 | + toDataCoords(layout.zaxis, trace.cz, scene.dataScale[2]) |
| 119 | + ), |
| 120 | + meshgrid: [ |
| 121 | + toDataCoords(layout.xaxis, trace.x, scene.dataScale[0]), |
| 122 | + toDataCoords(layout.yaxis, trace.y, scene.dataScale[1]), |
| 123 | + toDataCoords(layout.zaxis, trace.z, scene.dataScale[2]) |
| 124 | + ], |
| 125 | + vectors: zip3( |
| 126 | + toDataCoords(layout.xaxis, trace.u, scene.dataScale[0]), |
| 127 | + toDataCoords(layout.yaxis, trace.v, scene.dataScale[1]), |
| 128 | + toDataCoords(layout.zaxis, trace.w, scene.dataScale[2]) |
| 129 | + ), |
| 130 | + getDivergence: function(np, v, scale) { return scale; }, |
| 131 | + colormap: trace.colormap, |
| 132 | + maxLength: trace.maxLength, |
| 133 | + widthScale: trace.widthScale |
| 134 | + }; |
| 135 | + |
| 136 | + var bounds = trace.bounds || [ |
| 137 | + [min(trace.x.concat(trace.cx)), min(trace.y.concat(trace.cy)), min(trace.z.concat(trace.cz))], |
| 138 | + [max(trace.x.concat(trace.cx)), max(trace.y.concat(trace.cy)), max(trace.z.concat(trace.cz))] |
| 139 | + ]; |
| 140 | + |
| 141 | + bounds = [ |
| 142 | + [ |
| 143 | + layout.xaxis.d2l(bounds[0][0]) * scene.dataScale[0], |
| 144 | + layout.yaxis.d2l(bounds[0][1]) * scene.dataScale[1], |
| 145 | + layout.zaxis.d2l(bounds[0][2]) * scene.dataScale[2] |
| 146 | + ], |
| 147 | + [ |
| 148 | + layout.xaxis.d2l(bounds[1][0]) * scene.dataScale[0], |
| 149 | + layout.yaxis.d2l(bounds[1][1]) * scene.dataScale[1], |
| 150 | + layout.zaxis.d2l(bounds[1][2]) * scene.dataScale[2] |
| 151 | + ], |
| 152 | + ]; |
| 153 | + |
| 154 | + var meshData = tube2mesh( |
| 155 | + params, |
| 156 | + bounds |
| 157 | + ); |
| 158 | + |
| 159 | + return meshData; |
| 160 | +}; |
| 161 | + |
| 162 | +proto.update = function(trace) { |
| 163 | + var meshData = convert(trace); |
| 164 | + this.mesh.update(meshData); |
| 165 | +}; |
| 166 | + |
| 167 | +proto.dispose = function() { |
| 168 | + this.scene.glplot.remove(this.mesh); |
| 169 | + this.mesh.dispose(); |
| 170 | +}; |
| 171 | + |
| 172 | +function createMesh3DTrace(scene, trace) { |
| 173 | + var gl = scene.glplot.gl; |
| 174 | + var meshData = convert(scene, trace); |
| 175 | + var mesh = createMesh(gl, meshData); |
| 176 | + var result = new Mesh3DTrace(scene, mesh, trace.uid); |
| 177 | + result.data = {hoverinfo: 'skip'}; |
| 178 | + |
| 179 | + mesh._trace = result; |
| 180 | + scene.glplot.add(mesh); |
| 181 | + |
| 182 | + return result; |
| 183 | +} |
| 184 | + |
| 185 | +module.exports = createMesh3DTrace; |
0 commit comments