├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules 2 | 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # geo-3d-transform-mat4 2 | 3 | Transforms the positions of a few common geometry position formats by a 4 | [gl-mat4](https://github.com/stackgl/gl-mat4) matrix. 5 | 6 | Supported formats are: 7 | 8 | * Flat arrays `[1,2,3,4,5,6]` 9 | * Array of arrays `[[1,2,3], [4,5,6]]` 10 | * Array of TypedArrays `[new Float32Array([1,2,3]), new Float32Array([4,5,6])]` 11 | * TypedArray `new Float32Array([1,2,3,4,5,6])` 12 | * [ndarray](https://www.npmjs.com/package/ndarray) `ndarray(new Float32Array([1,2,3,4,5,6]))` 13 | 14 | ## Install 15 | 16 | ```sh 17 | npm install geo-3d-transform-mat4 18 | ``` 19 | 20 | ## Example 21 | ```js 22 | var mat4 = require('gl-mat4'); 23 | var tform = require('geo-3d-transform-mat4'); 24 | 25 | var positions = [ 26 | [1, 0, 0], 27 | [0, 1, 0], 28 | [0, 0, 1] 29 | ]; 30 | 31 | var scale = mat4.create(); 32 | mat4.scale(scale, scale, [2,2,2]); 33 | 34 | var out = tform(positions, scale); 35 | 36 | // out = [ 37 | // [2, 0, 0], 38 | // [0, 2, 0], 39 | // [0, 0, 2] 40 | // ] 41 | ``` 42 | 43 | ## API 44 | ```js 45 | var tform = require('geo-3d-transform-mat4'); 46 | ``` 47 | 48 | #### `tform(positions, matrix4)` 49 | 50 | Returns a copy of `positions` that has been transformed by `matrix4`. The output 51 | format will be the same as `positions` was provided in. 52 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var geoid = require('geo-identify-position-format'); 4 | var geoconv = require('geo-convert-position-format'); 5 | var vec3 = require('gl-vec3'); 6 | 7 | module.exports = transform; 8 | 9 | function transform(positions, m) { 10 | var oldfmt = geoid.identify(positions); 11 | var newpos = geoconv.convert(positions, geoid.ARRAY_OF_ARRAYS, 3); 12 | for (var i = 0; i < newpos.length; i++) { 13 | vec3.transformMat4(newpos[i], newpos[i], m); 14 | } 15 | newpos = geoconv.convert(newpos, oldfmt, 3); 16 | return newpos; 17 | } 18 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "geo-3d-transform-mat4", 3 | "version": "1.0.0", 4 | "description": "Transform geometry positions with a 4x4 transformation matrix.", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "nodeunit test.js" 8 | }, 9 | "keywords": [ 10 | "stackgl", 11 | "geometry" 12 | ], 13 | "author": "Rye Terrell", 14 | "license": "Unlicense", 15 | "dependencies": { 16 | "geo-convert-position-format": "^1.0.0", 17 | "geo-identify-position-format": "^1.0.2", 18 | "gl-vec3": "^1.0.3" 19 | }, 20 | "devDependencies": { 21 | "gl-mat4": "^1.1.4", 22 | "nodeunit": "^0.9.1" 23 | }, 24 | "repository": { 25 | "type": "git", 26 | "url": "git+https://github.com/wwwtyro/geo-3d-transform-mat4.git" 27 | }, 28 | "bugs": { 29 | "url": "https://github.com/wwwtyro/geo-3d-transform-mat4/issues" 30 | }, 31 | "homepage": "https://github.com/wwwtyro/geo-3d-transform-mat4#readme" 32 | } 33 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var mat4 = require('gl-mat4'); 4 | var transform = require('./index.js'); 5 | 6 | var src = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]; 7 | 8 | function cmp(a, b) { 9 | if (Math.abs(a[0] - b[0]) > 1e-10) { 10 | return false; 11 | } 12 | if (Math.abs(a[1] - b[1]) > 1e-10) { 13 | return false; 14 | } 15 | if (Math.abs(a[2] - b[2]) > 1e-10) { 16 | return false; 17 | } 18 | return true; 19 | } 20 | 21 | exports.testRotation = function(test) { 22 | test.expect(3); 23 | var rot = mat4.create(); 24 | mat4.rotateX(rot, rot, Math.PI/2); 25 | var out = transform(src, rot); 26 | test.ok(cmp(out[0], [1, 0, 0])); 27 | test.ok(cmp(out[1], [0, 0, 1])); 28 | test.ok(cmp(out[2], [0, -1, 0])); 29 | test.done(); 30 | } 31 | 32 | exports.testScale = function(test) { 33 | test.expect(3); 34 | var scale = mat4.create(); 35 | mat4.scale(scale, scale, [2,2,2]); 36 | var out = transform(src, scale); 37 | test.ok(cmp(out[0], [2, 0, 0])); 38 | test.ok(cmp(out[1], [0, 2, 0])); 39 | test.ok(cmp(out[2], [0, 0, 2])); 40 | test.done(); 41 | } 42 | 43 | exports.testTranslation = function(test) { 44 | test.expect(3); 45 | var trans = mat4.create(); 46 | mat4.translate(trans, trans, [1,2,3]); 47 | var out = transform(src, trans); 48 | test.ok(cmp(out[0], [2, 2, 3])); 49 | test.ok(cmp(out[1], [1, 3, 3])); 50 | test.ok(cmp(out[2], [1, 2, 4])); 51 | test.done(); 52 | } 53 | --------------------------------------------------------------------------------