-
Notifications
You must be signed in to change notification settings - Fork 2
/
cone.js
139 lines (122 loc) · 3.49 KB
/
cone.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"use strict";
var vec3 = require('gl-vec3');
module.exports = function(vectorfield, bounds) {
var positions = vectorfield.positions;
var vectors = vectorfield.vectors;
var geo = {
positions: [],
vertexIntensity: [],
vertexIntensityBounds: vectorfield.vertexIntensityBounds,
vectors: [],
cells: [],
coneOffset: vectorfield.coneOffset,
colormap: vectorfield.colormap
};
if (vectorfield.positions.length === 0) {
if (bounds) {
bounds[0] = [0,0,0];
bounds[1] = [0,0,0];
}
return geo;
}
// Compute bounding box for the dataset.
// Compute maximum velocity for the dataset to use for scaling the cones.
var maxNorm = 0;
var minX = Infinity, maxX = -Infinity;
var minY = Infinity, maxY = -Infinity;
var minZ = Infinity, maxZ = -Infinity;
var p2 = null;
var u2 = null;
var positionVectors = [];
var vectorScale = Infinity;
var skipIt = false;
var rawSizemodemode = vectorfield.coneSizemode === 'raw';
for (var i = 0; i < positions.length; i++) {
var p = positions[i];
minX = Math.min(p[0], minX);
maxX = Math.max(p[0], maxX);
minY = Math.min(p[1], minY);
maxY = Math.max(p[1], maxY);
minZ = Math.min(p[2], minZ);
maxZ = Math.max(p[2], maxZ);
var u = vectors[i];
if (vec3.length(u) > maxNorm) {
maxNorm = vec3.length(u);
}
if (i && !rawSizemodemode) {
// Find vector scale [w/ units of time] using "successive" positions
// (not "adjacent" with would be O(n^2)),
//
// The vector scale corresponds to the minimum "time" to travel across two
// two adjacent positions at the average velocity of those two adjacent positions
var q = (2 * vec3.distance(p2, p) / (vec3.length(u2) + vec3.length(u)));
if(q) {
vectorScale = Math.min(vectorScale, q);
skipIt = false;
} else {
skipIt = true;
}
}
if(!skipIt) {
p2 = p;
u2 = u;
}
positionVectors.push(u);
}
var minV = [minX, minY, minZ];
var maxV = [maxX, maxY, maxZ];
if (bounds) {
bounds[0] = minV;
bounds[1] = maxV;
}
if (maxNorm === 0) {
maxNorm = 1;
}
// Inverted max norm would map vector with norm maxNorm to 1 coord space units in length
var invertedMaxNorm = 1 / maxNorm;
if (!isFinite(vectorScale)) {
vectorScale = 1.0;
}
geo.vectorScale = vectorScale;
var coneScale = vectorfield.coneSize || (
rawSizemodemode ? 1 :0.5
);
if (vectorfield.absoluteConeSize) {
coneScale = vectorfield.absoluteConeSize * invertedMaxNorm;
}
geo.coneScale = coneScale;
// Build the cone model.
for (var i = 0, j = 0; i < positions.length; i++) {
var p = positions[i];
var x = p[0], y = p[1], z = p[2];
var d = positionVectors[i];
var intensity = vec3.length(d) * invertedMaxNorm;
for (var k = 0, l = 8; k < l; k++) {
geo.positions.push([x, y, z, j++]);
geo.positions.push([x, y, z, j++]);
geo.positions.push([x, y, z, j++]);
geo.positions.push([x, y, z, j++]);
geo.positions.push([x, y, z, j++]);
geo.positions.push([x, y, z, j++]);
geo.vectors.push(d);
geo.vectors.push(d);
geo.vectors.push(d);
geo.vectors.push(d);
geo.vectors.push(d);
geo.vectors.push(d);
geo.vertexIntensity.push(intensity, intensity, intensity);
geo.vertexIntensity.push(intensity, intensity, intensity);
var m = geo.positions.length;
geo.cells.push([m-6, m-5, m-4], [m-3, m-2, m-1]);
}
}
return geo;
};
var shaders = require('./lib/shaders');
module.exports.createMesh = require('./create_mesh');
module.exports.createConeMesh = function(gl, params) {
return module.exports.createMesh(gl, params, {
shaders: shaders,
traceType: 'cone'
});
}