geom-center-and-normalize 1.0.2 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +15 -0
- package/README.md +30 -20
- package/index.js +24 -15
- package/package.json +29 -24
- package/types/index.d.ts +2 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
|
+
|
|
5
|
+
# [2.0.0](https://github.com/vorg/geom-center-and-normalize/compare/v1.0.2...v2.0.0) (2023-02-02)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* add support for typed array geometries and update positions in place ([97ebbf1](https://github.com/vorg/geom-center-and-normalize/commit/97ebbf1422239f1455061d1edb145a289e0eb712))
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### BREAKING CHANGES
|
|
14
|
+
|
|
15
|
+
* use ESM
|
package/README.md
CHANGED
|
@@ -1,36 +1,46 @@
|
|
|
1
1
|
# geom-center-and-normalize
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Center a simplicial complex geometry's positions and scale them to fill 1x1x1 bounding box.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Installation
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
npm install geom-center-and-normalize
|
|
8
|
+
npm install geom-center-and-normalize
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
## Usage
|
|
12
12
|
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
```js
|
|
14
|
+
import centerAndNormalize from "geom-center-and-normalize";
|
|
15
|
+
|
|
16
|
+
const positions = new Float32Array([
|
|
17
|
+
// p1 [x, y, z]
|
|
18
|
+
1, 1, 1,
|
|
19
|
+
// p2 [x, y, z]
|
|
20
|
+
2, 2, 2,
|
|
21
|
+
// p3 [x, y, z]
|
|
22
|
+
3, 5, 3,
|
|
23
|
+
]);
|
|
24
|
+
const normalizedPositions = centerAndNormalize(positions);
|
|
25
|
+
// => Float32Array(9) [
|
|
26
|
+
// -0.25, -0.5, -0.25,
|
|
27
|
+
// 0, -0.25, 0,
|
|
28
|
+
// 0.25, 0.5, 0.25,
|
|
29
|
+
// ]
|
|
30
|
+
```
|
|
15
31
|
|
|
16
|
-
|
|
17
|
-
List of positions [x, y, z] centered at [0, 0, 0] and normalized to bounding box [-0.5, -0.5, -0.5] x [0.5, 0.5, 0.5]
|
|
32
|
+
## API
|
|
18
33
|
|
|
19
|
-
|
|
34
|
+
#### `centerAndNormalize(positions): positions`
|
|
20
35
|
|
|
21
|
-
|
|
22
|
-
var centerAndNormalize = require('geom-center-and-normalize')
|
|
36
|
+
**Parameters**
|
|
23
37
|
|
|
24
|
-
|
|
25
|
-
var normalizedPositions = centerAndNormalize(positions)
|
|
38
|
+
- positions: `TypedArray|Array` – simplicial complex geometry positions (eg. `new Float32Array([x, y, z, x, y, z, ...])` or `new Array([x, y, z], [x, y, z], ...)`)
|
|
26
39
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// [ 0.25, 0.5, 0.25 ]
|
|
31
|
-
// ]
|
|
32
|
-
```
|
|
40
|
+
**Returns**
|
|
41
|
+
|
|
42
|
+
- positions: `TypedArray|Array` – The positions parameter array updated, centered at `[0, 0, 0]` and normalized to a unit bounding box `[-0.5, -0.5, -0.5] x [0.5, 0.5, 0.5]`.
|
|
33
43
|
|
|
34
44
|
## License
|
|
35
45
|
|
|
36
|
-
MIT
|
|
46
|
+
MIT. See [license file](https://github.com/vorg/geom-center-and-normalize/blob/master/LICENSE.md).
|
package/index.js
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { avec3, vec3 } from "pex-math";
|
|
2
|
+
import { aabb } from "pex-geom";
|
|
3
3
|
|
|
4
|
-
function centerAndNormalize
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var size = AABB.size(aabb)
|
|
8
|
-
var scale = Math.max(size[0], Math.max(size[1], size[2]))
|
|
4
|
+
function centerAndNormalize(positions) {
|
|
5
|
+
const isTypedArray = !Array.isArray(positions);
|
|
6
|
+
const stride = isTypedArray ? 3 : 1;
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
const bbox = aabb.create();
|
|
9
|
+
aabb.fromPoints(bbox, positions);
|
|
10
|
+
const center = aabb.center(bbox);
|
|
11
|
+
const size = aabb.size(bbox);
|
|
12
|
+
const scale = 1 / Math.max(...size);
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < positions.length / stride; i++) {
|
|
15
|
+
if (isTypedArray) {
|
|
16
|
+
avec3.sub(positions, i, center, 0);
|
|
17
|
+
avec3.scale(positions, i, scale);
|
|
18
|
+
} else {
|
|
19
|
+
const p = vec3.copy(positions[i]);
|
|
20
|
+
vec3.sub(p, center);
|
|
21
|
+
vec3.scale(p, scale);
|
|
22
|
+
positions[i] = p;
|
|
23
|
+
}
|
|
16
24
|
}
|
|
17
|
-
|
|
25
|
+
|
|
26
|
+
return positions;
|
|
18
27
|
}
|
|
19
28
|
|
|
20
|
-
|
|
29
|
+
export default centerAndNormalize;
|
package/package.json
CHANGED
|
@@ -1,36 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "geom-center-and-normalize",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"license": "MIT",
|
|
7
|
-
"author": {
|
|
8
|
-
"name": "Marcin Ignac",
|
|
9
|
-
"email": "marcin.ignac@gmail.com",
|
|
10
|
-
"url": "https://github.com/vorg"
|
|
11
|
-
},
|
|
12
|
-
"dependencies": {
|
|
13
|
-
"clone": "^1.0.2",
|
|
14
|
-
"pex-geom": "git://github.com/variablestudio/pex-geom",
|
|
15
|
-
"pex-math": "git://github.com/variablestudio/pex-math"
|
|
16
|
-
},
|
|
17
|
-
"devDependencies": {},
|
|
18
|
-
"scripts": {
|
|
19
|
-
"test": "node test.js"
|
|
20
|
-
},
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Center a simplicial complex geometry's positions and scale them to fill 1x1x1 bounding box.",
|
|
21
5
|
"keywords": [
|
|
22
6
|
"geom",
|
|
7
|
+
"center",
|
|
8
|
+
"normalize",
|
|
23
9
|
"simplicial",
|
|
24
10
|
"complex",
|
|
25
11
|
"3d",
|
|
12
|
+
"pex",
|
|
13
|
+
"geometry",
|
|
26
14
|
"webgl"
|
|
27
15
|
],
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
|
|
16
|
+
"homepage": "https://github.com/vorg/geom-center-and-normalize",
|
|
17
|
+
"bugs": "https://github.com/vorg/geom-center-and-normalize/issues",
|
|
18
|
+
"repository": "vorg/geom-center-and-normalize",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "Marcin Ignac <marcin.ignac@gmail.com> (https://github.com/vorg)",
|
|
21
|
+
"contributors": [
|
|
22
|
+
"Damien Seguin (https://github.com/dmnsgn)"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": "./index.js",
|
|
27
|
+
"main": "index.js",
|
|
28
|
+
"scripts": {
|
|
29
|
+
"release": "snowdev release",
|
|
30
|
+
"test": "node --test",
|
|
31
|
+
"test:watch": "node --test --watch test"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"pex-geom": "^3.0.0-alpha.3",
|
|
35
|
+
"pex-math": "^4.0.0-alpha.3"
|
|
31
36
|
},
|
|
32
|
-
"
|
|
33
|
-
|
|
34
|
-
"
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18.0.0",
|
|
39
|
+
"npm": ">=8.0.0"
|
|
35
40
|
}
|
|
36
41
|
}
|
package/types/index.d.ts
ADDED