Skip to content

Commit 9068017

Browse files
committedJun 18, 2020
no longer depend on cwise and optimize for 2d operations
1 parent 6652273 commit 9068017

File tree

3 files changed

+62
-326
lines changed

3 files changed

+62
-326
lines changed
 

‎package-lock.json

+5-314
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"gl-heatmap2d": "^1.0.6",
8181
"gl-line3d": "1.2.1",
8282
"gl-mat4": "^1.2.0",
83+
"gl-matrix-invert": "^1.0.0",
8384
"gl-mesh3d": "^2.3.1",
8485
"gl-plot2d": "^1.4.5",
8586
"gl-plot3d": "^2.4.6",
@@ -100,7 +101,7 @@
100101
"mouse-event-offset": "^3.0.2",
101102
"mouse-wheel": "^1.2.0",
102103
"ndarray": "^1.0.19",
103-
"ndarray-homography": "^1.0.0",
104+
"ndarray-linear-interpolate": "^1.0.0",
104105
"parse-svg-path": "^0.1.2",
105106
"point-cluster": "^3.1.8",
106107
"polybooljs": "^1.2.0",

‎src/traces/surface/convert.js

+55-11
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
var createSurface = require('gl-surface3d');
1313

1414
var ndarray = require('ndarray');
15-
var homography = require('ndarray-homography');
15+
var invert = require('gl-matrix-invert');
16+
var ndarrayInterp2d = require('ndarray-linear-interpolate').d2;
17+
18+
var interp2d = require('../heatmap/interp2d');
19+
var findEmpties = require('../heatmap/find_empties');
1620

1721
var isArrayOrTypedArray = require('../../lib').isArrayOrTypedArray;
1822
var parseColorScale = require('../../lib/gl_format_color').parseColorScale;
1923
var str2RgbaArray = require('../../lib/str2rgbarray');
2024
var extractOpts = require('../../components/colorscale').extractOpts;
2125

22-
var interp2d = require('../heatmap/interp2d');
23-
var findEmpties = require('../heatmap/find_empties');
24-
2526
function SurfaceTrace(scene, surface, uid) {
2627
this.scene = scene;
2728
this.uid = uid;
@@ -319,6 +320,50 @@ proto.estimateScale = function(resSrc, axis) {
319320
return (scale > 1) ? scale : 1;
320321
};
321322

323+
// based on Mikola Lysenko's ndarray-homography
324+
// see https://github.com/scijs/ndarray-homography
325+
326+
function fnHomography(out, inp, X) {
327+
var n = 2; // we only use 2 dimensions here
328+
var i, j;
329+
for(i = 0; i < n; ++i) {
330+
out[i] = X[(n + 1) * n + i];
331+
for(j = 0; j < n; ++j) {
332+
out[i] += X[(n + 1) * j + i] * inp[j];
333+
}
334+
}
335+
var w = X[(n + 1) * (n + 1) - 1];
336+
for(j = 0; j < n; ++j) {
337+
w += X[(n + 1) * j + n] * inp[j];
338+
}
339+
var wr = 1 / w;
340+
for(i = 0; i < n; ++i) {
341+
out[i] *= wr;
342+
}
343+
return out;
344+
}
345+
346+
function homography(dest, src, X) {
347+
warp(dest, src, fnHomography, X);
348+
return dest;
349+
}
350+
351+
// based on Mikola Lysenko's ndarray-warp
352+
// see https://github.com/scijs/ndarray-warp
353+
354+
function warp(dest, src, func, X) {
355+
var warped = [0, 0];
356+
var ni = dest.shape[0];
357+
var nj = dest.shape[1];
358+
for(var i = 0; i < ni; i++) {
359+
for(var j = 0; j < nj; j++) {
360+
func(warped, [i, j], X);
361+
dest.set(i, j, ndarrayInterp2d(src, warped[0], warped[1]));
362+
}
363+
}
364+
return dest;
365+
}
366+
322367
proto.refineCoords = function(coords) {
323368
var scaleW = this.dataScaleX;
324369
var scaleH = this.dataScaleY;
@@ -333,18 +378,17 @@ proto.refineCoords = function(coords) {
333378
var padWidth = 1 + width + 1;
334379
var padHeight = 1 + height + 1;
335380
var padImg = ndarray(new Float32Array(padWidth * padHeight), [padWidth, padHeight]);
381+
var X = invert([], [
382+
scaleW, 0, 0,
383+
0, scaleH, 0,
384+
0, 0, 1
385+
]);
336386

337387
for(var i = 0; i < coords.length; ++i) {
338388
this.surface.padField(padImg, coords[i]);
339389

340390
var scaledImg = ndarray(new Float32Array(newWidth * newHeight), [newWidth, newHeight]);
341-
homography(scaledImg, padImg,
342-
[
343-
scaleW, 0, 0,
344-
0, scaleH, 0,
345-
0, 0, 1
346-
]
347-
);
391+
homography(scaledImg, padImg, X);
348392
coords[i] = scaledImg;
349393
}
350394
};

0 commit comments

Comments
 (0)
Please sign in to comment.