Skip to content

Commit 3b3ef9d

Browse files
committed
improve vectorScale algo by using a "local" algo
1 parent 5df0b14 commit 3b3ef9d

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

cone.js

+26-23
Original file line numberDiff line numberDiff line change
@@ -169,33 +169,39 @@ module.exports = function(vectorfield, bounds) {
169169
var minX = 1/0, maxX = -1/0;
170170
var minY = 1/0, maxY = -1/0;
171171
var minZ = 1/0, maxZ = -1/0;
172-
var v2 = null;
172+
var p2 = null;
173+
var u2 = null;
173174
var positionVectors = [];
174-
var minSeparation = 1/0;
175+
var vectorScale = 1/0;
175176
for (var i = 0; i < positions.length; i++) {
176-
var v1 = positions[i];
177-
minX = Math.min(v1[0], minX);
178-
maxX = Math.max(v1[0], maxX);
179-
minY = Math.min(v1[1], minY);
180-
maxY = Math.max(v1[1], maxY);
181-
minZ = Math.min(v1[2], minZ);
182-
maxZ = Math.max(v1[2], maxZ);
177+
var p = positions[i];
178+
minX = Math.min(p[0], minX);
179+
maxX = Math.max(p[0], maxX);
180+
minY = Math.min(p[1], minY);
181+
maxY = Math.max(p[1], maxY);
182+
minZ = Math.min(p[2], minZ);
183+
maxZ = Math.max(p[2], maxZ);
183184
var u;
184185
if (meshgrid) {
185-
u = sampleMeshgrid(v1, vectors, meshgrid, true);
186+
u = sampleMeshgrid(p, vectors, meshgrid, true);
186187
} else {
187188
u = vectors[i];
188189
}
189190
if (V.length(u) > maxNorm) {
190191
maxNorm = V.length(u);
191192
}
192-
if (v2) {
193-
var separation = V.distance(v1, v2);
194-
if (separation < minSeparation) {
195-
minSeparation = separation;
196-
}
193+
if (i) {
194+
// Find vector scale [w/ units of time] using "successive" positions
195+
// (not "adjacent" with would be O(n^2)),
196+
//
197+
// The vector scale corresponds to the minimum "time" to travel across two
198+
// two adjacent positions at the average velocity of those two adjacent positions
199+
vectorScale = Math.min(vectorScale,
200+
2 * V.distance(p2, p) / (V.length(u2) + V.length(u))
201+
);
197202
}
198-
v2 = v1;
203+
p2 = p;
204+
u2 = u;
199205
positionVectors.push(u);
200206
}
201207
var minV = [minX, minY, minZ];
@@ -207,17 +213,14 @@ module.exports = function(vectorfield, bounds) {
207213
if (maxNorm === 0) {
208214
maxNorm = 1;
209215
}
216+
210217
// Inverted max norm would map vector with norm maxNorm to 1 coord space units in length
211218
var invertedMaxNorm = 1 / maxNorm;
212219

213-
if (!isFinite(minSeparation) || isNaN(minSeparation)) {
214-
minSeparation = 1.0;
220+
if (!isFinite(vectorScale) || isNaN(vectorScale)) {
221+
vectorScale = 1.0;
215222
}
216-
217-
// Inverted max norm multiplied scaled by smallest found vector position distance:
218-
// Maps a vector with norm maxNorm to minSeparation coord space units in length.
219-
// In practice, scales maxNorm vectors so that they are just long enough to reach the adjacent vector position.
220-
geo.vectorScale = invertedMaxNorm * minSeparation;
223+
geo.vectorScale = vectorScale;
221224

222225
var nml = vec3(0,1,0);
223226

0 commit comments

Comments
 (0)