geom-center-and-normalize 1.0.3 → 2.1.1

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 ADDED
@@ -0,0 +1,36 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4
+
5
+ ## [2.1.1](https://github.com/vorg/geom-center-and-normalize/compare/v2.1.0...v2.1.1) (2024-03-14)
6
+
7
+
8
+
9
+ # [2.1.0](https://github.com/vorg/geom-center-and-normalize/compare/v2.0.0...v2.1.0) (2024-03-14)
10
+
11
+
12
+ ### Bug Fixes
13
+
14
+ * handle division by 0 for aabb size 0 ([26f146c](https://github.com/vorg/geom-center-and-normalize/commit/26f146cc25b32617eb03fcf71285bc8d419550aa))
15
+
16
+
17
+ ### Features
18
+
19
+ * add support for flat arrays ([300c4ad](https://github.com/vorg/geom-center-and-normalize/commit/300c4ad31f233e6b5f93816bfbb06c37b8fcf188))
20
+ * make center and normalize options ([0a051bb](https://github.com/vorg/geom-center-and-normalize/commit/0a051bbf477f3d130bc25262f9a432b400bf255f))
21
+ * replace normalize option with scaling as boolean/number ([fe23ff9](https://github.com/vorg/geom-center-and-normalize/commit/fe23ff9eb06cb7f135667cbaaac977c8b475b67a))
22
+ * separate normalize and scale parameters ([56efcd3](https://github.com/vorg/geom-center-and-normalize/commit/56efcd354e6a5088d5d7474a30339262ee9fadfe))
23
+
24
+
25
+
26
+ # [2.0.0](https://github.com/vorg/geom-center-and-normalize/compare/v1.0.2...v2.0.0) (2023-02-02)
27
+
28
+
29
+ ### Features
30
+
31
+ * add support for typed array geometries and update positions in place ([97ebbf1](https://github.com/vorg/geom-center-and-normalize/commit/97ebbf1422239f1455061d1edb145a289e0eb712))
32
+
33
+
34
+ ### BREAKING CHANGES
35
+
36
+ * use ESM
package/README.md CHANGED
@@ -1,36 +1,48 @@
1
1
  # geom-center-and-normalize
2
2
 
3
- Centers the geometry and scales it to fill 1x1x1 bounding box
3
+ Center a simplicial complex geometry's positions in place and scale them to fill a defined bounding box.
4
4
 
5
- ## Usage
5
+ ## Installation
6
6
 
7
7
  ```bash
8
- npm install geom-center-and-normalize --save
8
+ npm install geom-center-and-normalize
9
+ ```
10
+
11
+ ## Usage
12
+
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
+ // ]
9
30
  ```
10
31
 
11
- #### `normalizedPositions = centerAndNormalize(positions)`
32
+ ## API
12
33
 
13
- Parameters:
14
- `positions` - list of [x, y, z]
34
+ #### `centerAndNormalize(positions, [options]): positions`
15
35
 
16
- Returns:
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]
36
+ **Parameters**
18
37
 
19
- ## Example
38
+ - positions: `TypedArray | Array | Array<[x, y, z]>` – simplicial complex geometry positions (eg. `new Float32Array([x, y, z, x, y, z, ...])/new Array(x, y, z, x, y, z, ...)` or `new Array([x, y, z], [x, y, z], ...)`)
20
39
 
21
- ```javascript
22
- var centerAndNormalize = require('geom-center-and-normalize')
40
+ - options: `{ center: boolean, normalize: boolean, scale: number }` – enable/disable centering/normalizing and control normalized scaling. `center` and `normalize` defaults to `true`. Scale defaults to `1`.
23
41
 
24
- var positions = [[1, 1, 1], [2, 2, 2], [3, 5, 3]]
25
- var normalizedPositions = centerAndNormalize(positions)
42
+ **Returns**
26
43
 
27
- // normalizedPositions = [
28
- // [ -0.25, -0.5, -0.25 ],
29
- // [ 0, -0.25, 0 ],
30
- // [ 0.25, 0.5, 0.25 ]
31
- // ]
32
- ```
44
+ - positions: `TypedArray | Array | Array<[x, y, z]>` – The positions parameter array updated, centered at `[0, 0, 0]` and normalized to a bounding box of `scale` size (`[-halfScale, -halfScale, -halfScale] x [halfScale, halfScale, halfScale]`).
33
45
 
34
46
  ## License
35
47
 
36
- MIT, see [LICENSE.md](http://github.com/vorg/geom-center-and-scale/blob/master/LICENSE.md) for details.
48
+ MIT. See [license file](https://github.com/vorg/geom-center-and-normalize/blob/master/LICENSE.md).
package/index.js CHANGED
@@ -1,20 +1,42 @@
1
- var aabb = require('pex-geom/aabb')
2
- var vec3 = require('pex-math/vec3')
1
+ import { avec3, vec3 } from "pex-math";
2
+ import { aabb } from "pex-geom";
3
3
 
4
- function centerAndNormalize (positions) {
5
- var result = aabb.fromPoints(positions)
6
- var center = aabb.center(result)
7
- var size = aabb.size(result)
8
- var scale = Math.max(size[0], Math.max(size[1], size[2]))
4
+ const TEMP_VEC3 = vec3.create();
9
5
 
10
- var newPositions = []
11
- for (var i = 0; i < positions.length; i++) {
12
- var p = vec3.copy(positions[i])
13
- vec3.sub(p, center)
14
- vec3.scale(p, 1 / scale)
15
- newPositions.push(p)
6
+ function centerAndNormalize(
7
+ positions,
8
+ { center = true, normalize = true, scale = 1 } = {},
9
+ ) {
10
+ const isFlatArray = !positions[0]?.length;
11
+ const positionsCount = positions.length / (isFlatArray ? 3 : 1);
12
+
13
+ const bbox = aabb.fromPoints(aabb.create(), positions);
14
+ const bboxCenter = aabb.center(bbox);
15
+
16
+ if (normalize) {
17
+ const size = aabb.size(bbox);
18
+ scale = scale / (Math.max(...size) || 1);
16
19
  }
17
- return newPositions
20
+
21
+ for (let i = 0; i < positionsCount; i++) {
22
+ if (isFlatArray) {
23
+ avec3.sub(positions, i, bboxCenter, 0);
24
+ if (normalize) {
25
+ avec3.scale(positions, i, scale);
26
+ if (!center) avec3.add(positions, i, bboxCenter, 0);
27
+ }
28
+ } else {
29
+ vec3.set(TEMP_VEC3, positions[i]);
30
+ vec3.sub(TEMP_VEC3, bboxCenter);
31
+ if (normalize) {
32
+ vec3.scale(TEMP_VEC3, scale);
33
+ if (!center) vec3.add(TEMP_VEC3, bboxCenter);
34
+ }
35
+ vec3.set(positions[i], TEMP_VEC3);
36
+ }
37
+ }
38
+
39
+ return positions;
18
40
  }
19
41
 
20
- module.exports = centerAndNormalize
42
+ export default centerAndNormalize;
package/package.json CHANGED
@@ -1,36 +1,42 @@
1
1
  {
2
2
  "name": "geom-center-and-normalize",
3
- "version": "1.0.3",
4
- "description": "Centers the geometry and scales it to fill 1x1x1 bounding box",
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": "^2.0.2",
15
- "pex-math": "^2.1.1"
16
- },
17
- "devDependencies": {},
18
- "scripts": {
19
- "test": "node test.js"
20
- },
3
+ "version": "2.1.1",
4
+ "description": "Center a simplicial complex geometry's positions in place and scale them to fill a defined 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
- "repository": {
29
- "type": "git",
30
- "url": "git://github.com/vorg/geom-center-and-normalize.git"
31
- },
32
16
  "homepage": "https://github.com/vorg/geom-center-and-normalize",
33
- "bugs": {
34
- "url": "https://github.com/vorg/geom-center-and-normalize/issues"
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
+ "types": "types/index.d.ts",
29
+ "scripts": {
30
+ "release": "snowdev release",
31
+ "test": "node --test",
32
+ "test:watch": "node --test --watch test"
33
+ },
34
+ "dependencies": {
35
+ "pex-geom": "^3.0.1",
36
+ "pex-math": "^4.1.0"
37
+ },
38
+ "engines": {
39
+ "node": ">=20.0.0",
40
+ "npm": ">=9.6.4"
35
41
  }
36
42
  }
@@ -0,0 +1,6 @@
1
+ export default centerAndNormalize;
2
+ declare function centerAndNormalize(positions: any, { center, normalize, scale }?: {
3
+ center?: boolean;
4
+ normalize?: boolean;
5
+ scale?: number;
6
+ }): any;