├── .gitignore ├── .npmignore ├── test └── test.js ├── package.json ├── LICENSE ├── README.md └── interp.js /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules/* -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules/* 16 | test/* -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var ndarray = require("ndarray") 2 | var interp = require("../interp.js") 3 | var zeros = require("zeros") 4 | 5 | 6 | var x = zeros([3, 3]) 7 | x.set(1, 1, 1.0) 8 | 9 | for(var u=0.0; u<=3.0; u+=0.25) { 10 | var row = [] 11 | for(var v=0.0; v<=3.0; v+=0.25) { 12 | row.push(interp(x, u, v)) 13 | } 14 | console.log(row.join(" ")) 15 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ndarray-linear-interpolate", 3 | "version": "1.0.0", 4 | "description": "Multilinear interplation for ndarrays", 5 | "main": "interp.js", 6 | "directories": { 7 | "test": "test" 8 | }, 9 | "dependencies": {}, 10 | "devDependencies": { 11 | "ndarray": "^1.0.0", 12 | "tape": "^2.12.3", 13 | "zeros": "0.0.0" 14 | }, 15 | "scripts": { 16 | "test": "tape test/*.js" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "git://github.com/mikolalysenko/ndarray-linear-interpolate.git" 21 | }, 22 | "keywords": [ 23 | "ndarray", 24 | "linear", 25 | "multilinear", 26 | "bilinear", 27 | "trilinear", 28 | "interpolate", 29 | "grid", 30 | "image", 31 | "volume", 32 | "density" 33 | ], 34 | "author": "Mikola Lysenko", 35 | "license": "MIT", 36 | "readmeFilename": "README.md", 37 | "gitHead": "60da06a29b5aed5fb640d6233cb32b93dd252a2d" 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2013 Mikola Lysenko 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ndarray-linear-interpolate 2 | ========================== 3 | Multilinear (ie [bilinear](http://en.wikipedia.org/wiki/Bilinear_interpolation)/[trilinear](http://en.wikipedia.org/wiki/Trilinear_interpolation)) interpolation for ndarrays. 4 | 5 | Example 6 | ======= 7 | 8 | ```javascript 9 | var ndarray = require("ndarray") 10 | var interp = require("ndarray-linear-interpolate") 11 | 12 | var x = ndarray(new Float32Array(9), [3, 3]) 13 | x.set(1, 1, 1.0) 14 | 15 | for(var u=0.0, u<=3.0; u+=0.25) { 16 | var row = [] 17 | for(var v=0.0; v<=3.0; v+=0.25) { 18 | row.push(interp(x, u, v)) 19 | } 20 | console.log(row.join(" ")) 21 | } 22 | 23 | //Prints out: 24 | // 25 | // 0 0 0 0 0 0 0 0 0 0 0 0 0 26 | // 0 0.0625 0.125 0.1875 0.25 0.1875 0.125 0.0625 0 0 0 0 0 27 | // 0 0.125 0.25 0.375 0.5 0.375 0.25 0.125 0 0 0 0 0 28 | // 0 0.1875 0.375 0.5625 0.75 0.5625 0.375 0.1875 0 0 0 0 0 29 | // 0 0.25 0.5 0.75 1 0.75 0.5 0.25 0 0 0 0 0 30 | // 0 0.1875 0.375 0.5625 0.75 0.5625 0.375 0.1875 0 0 0 0 0 31 | // 0 0.125 0.25 0.375 0.5 0.375 0.25 0.125 0 0 0 0 0 32 | // 0 0.0625 0.125 0.1875 0.25 0.1875 0.125 0.0625 0 0 0 0 0 33 | // 0 0 0 0 0 0 0 0 0 0 0 0 0 34 | // 0 0 0 0 0 0 0 0 0 0 0 0 0 35 | // 0 0 0 0 0 0 0 0 0 0 0 0 0 36 | // 0 0 0 0 0 0 0 0 0 0 0 0 0 37 | // 0 0 0 0 0 0 0 0 0 0 0 0 0 38 | // 39 | ``` 40 | 41 | # Install 42 | 43 | npm install ndarray-linear-interpolate 44 | 45 | ### `require("ndarray-linear-interpolate")(array, x, y, z, ...)` 46 | Interpolates values on an ndarray. 47 | 48 | * `array` the array to interpolate from 49 | * `x, y, z...` the coordinate to evaluate the interpolated grid at 50 | 51 | **Returns** The multilinearly interpolated value 52 | 53 | **Note** To avoid the overhead of variable arguments, special interfaces are available for 1, 2 and 3 dimensions. You can access these using: 54 | 55 | * `require("ndarray-linear-interpolate").d1` 56 | * `require("ndarray-linear-interpolate").d2` 57 | * `require("ndarray-linear-interpolate").d3` 58 | 59 | 60 | # Credits 61 | (c) 2013 Mikola Lysenko. MIT License 62 | 63 | -------------------------------------------------------------------------------- /interp.js: -------------------------------------------------------------------------------- 1 | "use strict" 2 | 3 | function interp1d(arr, x) { 4 | var ix = Math.floor(x) 5 | , fx = x - ix 6 | , s0 = 0 <= ix && ix < arr.shape[0] 7 | , s1 = 0 <= ix+1 && ix+1 < arr.shape[0] 8 | , w0 = s0 ? +arr.get(ix) : 0.0 9 | , w1 = s1 ? +arr.get(ix+1) : 0.0 10 | return (1.0-fx)*w0 + fx*w1 11 | } 12 | 13 | function interp2d(arr, x, y) { 14 | var ix = Math.floor(x) 15 | , fx = x - ix 16 | , s0 = 0 <= ix && ix < arr.shape[0] 17 | , s1 = 0 <= ix+1 && ix+1 < arr.shape[0] 18 | , iy = Math.floor(y) 19 | , fy = y - iy 20 | , t0 = 0 <= iy && iy < arr.shape[1] 21 | , t1 = 0 <= iy+1 && iy+1 < arr.shape[1] 22 | , w00 = s0&&t0 ? arr.get(ix ,iy ) : 0.0 23 | , w01 = s0&&t1 ? arr.get(ix ,iy+1) : 0.0 24 | , w10 = s1&&t0 ? arr.get(ix+1,iy ) : 0.0 25 | , w11 = s1&&t1 ? arr.get(ix+1,iy+1) : 0.0 26 | return (1.0-fy) * ((1.0-fx)*w00 + fx*w10) + fy * ((1.0-fx)*w01 + fx*w11) 27 | } 28 | 29 | function interp3d(arr, x, y, z) { 30 | var ix = Math.floor(x) 31 | , fx = x - ix 32 | , s0 = 0 <= ix && ix < arr.shape[0] 33 | , s1 = 0 <= ix+1 && ix+1 < arr.shape[0] 34 | , iy = Math.floor(y) 35 | , fy = y - iy 36 | , t0 = 0 <= iy && iy < arr.shape[1] 37 | , t1 = 0 <= iy+1 && iy+1 < arr.shape[1] 38 | , iz = Math.floor(z) 39 | , fz = z - iz 40 | , u0 = 0 <= iz && iz < arr.shape[2] 41 | , u1 = 0 <= iz+1 && iz+1 < arr.shape[2] 42 | , w000 = s0&&t0&&u0 ? arr.get(ix,iy,iz) : 0.0 43 | , w010 = s0&&t1&&u0 ? arr.get(ix,iy+1,iz) : 0.0 44 | , w100 = s1&&t0&&u0 ? arr.get(ix+1,iy,iz) : 0.0 45 | , w110 = s1&&t1&&u0 ? arr.get(ix+1,iy+1,iz) : 0.0 46 | , w001 = s0&&t0&&u1 ? arr.get(ix,iy,iz+1) : 0.0 47 | , w011 = s0&&t1&&u1 ? arr.get(ix,iy+1,iz+1) : 0.0 48 | , w101 = s1&&t0&&u1 ? arr.get(ix+1,iy,iz+1) : 0.0 49 | , w111 = s1&&t1&&u1 ? arr.get(ix+1,iy+1,iz+1) : 0.0 50 | return (1.0-fz) * ((1.0-fy) * ((1.0-fx)*w000 + fx*w100) + fy * ((1.0-fx)*w010 + fx*w110)) + fz * ((1.0-fy) * ((1.0-fx)*w001 + fx*w101) + fy * ((1.0-fx)*w011 + fx*w111)) 51 | } 52 | 53 | function interpNd(arr) { 54 | var d = arr.shape.length|0 55 | , ix = new Array(d) 56 | , fx = new Array(d) 57 | , s0 = new Array(d) 58 | , s1 = new Array(d) 59 | , i, t 60 | for(i=0; i