|
| 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 | +'use strict'; |
| 11 | + |
| 12 | +var cone2mesh = require('gl-cone3d'); |
| 13 | +var createMesh = cone2mesh.createConeMesh; |
| 14 | + |
| 15 | +function Mesh3DTrace(scene, mesh, uid) { |
| 16 | + this.scene = scene; |
| 17 | + this.uid = uid; |
| 18 | + this.mesh = mesh; |
| 19 | + this.name = ''; |
| 20 | + this.color = '#fff'; |
| 21 | + this.data = null; |
| 22 | + this.showContour = false; |
| 23 | +} |
| 24 | + |
| 25 | +var proto = Mesh3DTrace.prototype; |
| 26 | + |
| 27 | +proto.handlePick = function(selection) { |
| 28 | + if(selection.object === this.mesh) { |
| 29 | + var selectIndex = selection.index = selection.data.index; |
| 30 | + |
| 31 | + selection.traceCoordinate = [ |
| 32 | + this.data.x[selectIndex], |
| 33 | + this.data.y[selectIndex], |
| 34 | + this.data.z[selectIndex] |
| 35 | + ]; |
| 36 | + |
| 37 | + var text = this.data.text; |
| 38 | + if(Array.isArray(text) && text[selectIndex] !== undefined) { |
| 39 | + selection.textLabel = text[selectIndex]; |
| 40 | + } else if(text) { |
| 41 | + selection.textLabel = text; |
| 42 | + } |
| 43 | + |
| 44 | + return true; |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +function zip3(x, y, z) { |
| 49 | + var result = new Array(x.length); |
| 50 | + for(var i = 0; i < x.length; ++i) { |
| 51 | + result[i] = [x[i], y[i], z[i]]; |
| 52 | + } |
| 53 | + return result; |
| 54 | +} |
| 55 | + |
| 56 | +function convert(scene, trace) { |
| 57 | + var layout = scene.fullSceneLayout; |
| 58 | + |
| 59 | + // Unpack position data |
| 60 | + function toDataCoords(axis, coord, scale) { |
| 61 | + return coord.map(function(x) { |
| 62 | + return axis.d2l(x) * scale; |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + var meshData = cone2mesh({ |
| 67 | + positions: zip3( |
| 68 | + toDataCoords(layout.xaxis, trace.cx, scene.dataScale[0]), |
| 69 | + toDataCoords(layout.yaxis, trace.cy, scene.dataScale[1]), |
| 70 | + toDataCoords(layout.zaxis, trace.cz, scene.dataScale[2]) |
| 71 | + ), |
| 72 | + vectors: zip3( |
| 73 | + toDataCoords(layout.xaxis, trace.u, scene.dataScale[0]), |
| 74 | + toDataCoords(layout.yaxis, trace.v, scene.dataScale[1]), |
| 75 | + toDataCoords(layout.zaxis, trace.w, scene.dataScale[2]) |
| 76 | + ), |
| 77 | + colormap: 'portland' |
| 78 | + }); |
| 79 | + |
| 80 | + return meshData; |
| 81 | +}; |
| 82 | + |
| 83 | +proto.update = function(data) { |
| 84 | + this.data = data; |
| 85 | + this.mesh.update(convert(this.scene, data)); |
| 86 | +}; |
| 87 | + |
| 88 | +proto.dispose = function() { |
| 89 | + this.scene.glplot.remove(this.mesh); |
| 90 | + this.mesh.dispose(); |
| 91 | +}; |
| 92 | + |
| 93 | +function createMesh3DTrace(scene, data) { |
| 94 | + var gl = scene.glplot.gl; |
| 95 | + var meshData = convert(scene, data); |
| 96 | + var mesh = createMesh(gl, meshData); |
| 97 | + var result = new Mesh3DTrace(scene, mesh, data.uid); |
| 98 | + |
| 99 | + result.data = data; |
| 100 | + mesh._trace = result; |
| 101 | + scene.glplot.add(mesh); |
| 102 | + |
| 103 | + return result; |
| 104 | +} |
| 105 | + |
| 106 | +module.exports = createMesh3DTrace; |
0 commit comments