geom-center-and-normalize 2.0.0 → 2.2.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 CHANGED
@@ -1,6 +1,36 @@
1
1
  # Changelog
2
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.
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.2.0](https://github.com/vorg/geom-center-and-normalize/compare/v2.1.1...v2.2.0) (2024-04-16)
6
+
7
+
8
+ ### Features
9
+
10
+ * rename scale to normalizedSize ([8007608](https://github.com/vorg/geom-center-and-normalize/commit/800760830ee69d2a882d74447ce97e56ead7e857))
11
+
12
+
13
+
14
+ ## [2.1.1](https://github.com/vorg/geom-center-and-normalize/compare/v2.1.0...v2.1.1) (2024-03-14)
15
+
16
+
17
+
18
+ # [2.1.0](https://github.com/vorg/geom-center-and-normalize/compare/v2.0.0...v2.1.0) (2024-03-14)
19
+
20
+
21
+ ### Bug Fixes
22
+
23
+ * handle division by 0 for aabb size 0 ([26f146c](https://github.com/vorg/geom-center-and-normalize/commit/26f146cc25b32617eb03fcf71285bc8d419550aa))
24
+
25
+
26
+ ### Features
27
+
28
+ * add support for flat arrays ([300c4ad](https://github.com/vorg/geom-center-and-normalize/commit/300c4ad31f233e6b5f93816bfbb06c37b8fcf188))
29
+ * make center and normalize options ([0a051bb](https://github.com/vorg/geom-center-and-normalize/commit/0a051bbf477f3d130bc25262f9a432b400bf255f))
30
+ * replace normalize option with scaling as boolean/number ([fe23ff9](https://github.com/vorg/geom-center-and-normalize/commit/fe23ff9eb06cb7f135667cbaaac977c8b475b67a))
31
+ * separate normalize and scale parameters ([56efcd3](https://github.com/vorg/geom-center-and-normalize/commit/56efcd354e6a5088d5d7474a30339262ee9fadfe))
32
+
33
+
4
34
 
5
35
  # [2.0.0](https://github.com/vorg/geom-center-and-normalize/compare/v1.0.2...v2.0.0) (2023-02-02)
6
36
 
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # geom-center-and-normalize
2
2
 
3
- Center a simplicial complex geometry's positions and scale them 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
5
  ## Installation
6
6
 
@@ -31,15 +31,17 @@ const normalizedPositions = centerAndNormalize(positions);
31
31
 
32
32
  ## API
33
33
 
34
- #### `centerAndNormalize(positions): positions`
34
+ #### `centerAndNormalize(positions, [options]): positions`
35
35
 
36
36
  **Parameters**
37
37
 
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], ...)`)
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], ...)`)
39
+
40
+ - options: `{ center: boolean, normalize: boolean, normalizedSize: number }` – enable/disable centering/normalizing and control normalized scaling. `center` and `normalize` defaults to `true`. Scale defaults to `1`.
39
41
 
40
42
  **Returns**
41
43
 
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]`.
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 `normalizedSize` (`[-halfScale, -halfScale, -halfScale] x [halfScale, halfScale, halfScale]`).
43
45
 
44
46
  ## License
45
47
 
package/index.js CHANGED
@@ -1,25 +1,38 @@
1
1
  import { avec3, vec3 } from "pex-math";
2
2
  import { aabb } from "pex-geom";
3
3
 
4
- function centerAndNormalize(positions) {
5
- const isTypedArray = !Array.isArray(positions);
6
- const stride = isTypedArray ? 3 : 1;
4
+ const TEMP_VEC3 = vec3.create();
7
5
 
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);
6
+ function centerAndNormalize(
7
+ positions,
8
+ { center = true, normalize = true, normalizedSize = 1 } = {},
9
+ ) {
10
+ const isFlatArray = !positions[0]?.length;
11
+ const positionsCount = positions.length / (isFlatArray ? 3 : 1);
13
12
 
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);
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
+ normalizedSize = normalizedSize / (Math.max(...size) || 1);
19
+ }
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, normalizedSize);
26
+ if (!center) avec3.add(positions, i, bboxCenter, 0);
27
+ }
18
28
  } else {
19
- const p = vec3.copy(positions[i]);
20
- vec3.sub(p, center);
21
- vec3.scale(p, scale);
22
- positions[i] = p;
29
+ vec3.set(TEMP_VEC3, positions[i]);
30
+ vec3.sub(TEMP_VEC3, bboxCenter);
31
+ if (normalize) {
32
+ vec3.scale(TEMP_VEC3, normalizedSize);
33
+ if (!center) vec3.add(TEMP_VEC3, bboxCenter);
34
+ }
35
+ vec3.set(positions[i], TEMP_VEC3);
23
36
  }
24
37
  }
25
38
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "geom-center-and-normalize",
3
- "version": "2.0.0",
4
- "description": "Center a simplicial complex geometry's positions and scale them to fill 1x1x1 bounding box.",
3
+ "version": "2.2.0",
4
+ "description": "Center a simplicial complex geometry's positions in place and scale them to fill a defined bounding box.",
5
5
  "keywords": [
6
6
  "geom",
7
7
  "center",
@@ -25,17 +25,18 @@
25
25
  "type": "module",
26
26
  "exports": "./index.js",
27
27
  "main": "index.js",
28
+ "types": "types/index.d.ts",
28
29
  "scripts": {
29
30
  "release": "snowdev release",
30
31
  "test": "node --test",
31
32
  "test:watch": "node --test --watch test"
32
33
  },
33
34
  "dependencies": {
34
- "pex-geom": "^3.0.0-alpha.3",
35
- "pex-math": "^4.0.0-alpha.3"
35
+ "pex-geom": "^3.0.1",
36
+ "pex-math": "^4.1.0"
36
37
  },
37
38
  "engines": {
38
- "node": ">=18.0.0",
39
- "npm": ">=8.0.0"
39
+ "node": ">=20.0.0",
40
+ "npm": ">=9.6.4"
40
41
  }
41
42
  }
package/types/index.d.ts CHANGED
@@ -1,2 +1,6 @@
1
1
  export default centerAndNormalize;
2
- declare function centerAndNormalize(positions: any): any;
2
+ declare function centerAndNormalize(positions: any, { center, normalize, normalizedSize }?: {
3
+ center?: boolean;
4
+ normalize?: boolean;
5
+ normalizedSize?: number;
6
+ }): any;