@thi.ng/geom 3.2.6 → 3.3.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,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-06-09T16:14:01Z
3
+ - **Last updated**: 2022-06-20T17:33:30Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,15 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [3.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/geom@3.3.0) (2022-06-20)
13
+
14
+ #### 🚀 Features
15
+
16
+ - update bounds() to support opt. margin ([8cdc372](https://github.com/thi-ng/umbrella/commit/8cdc372))
17
+ - update rect/aabb, add new factory fns ([f74e377](https://github.com/thi-ng/umbrella/commit/f74e377))
18
+ - add ...WithMargin() factory fns
19
+ - add AABBLike.offset() impls
20
+
12
21
  ### [3.2.1](https://github.com/thi-ng/umbrella/tree/@thi.ng/geom@3.2.1) (2022-04-07)
13
22
 
14
23
  #### 🩹 Bug fixes
package/aabb.d.ts CHANGED
@@ -6,6 +6,9 @@ export declare function aabb(pos: Vec, size: number | Vec, attribs?: Attribs): A
6
6
  export declare function aabb(size: number | Vec, attribs?: Attribs): AABB;
7
7
  export declare function aabb(attribs?: Attribs): AABB;
8
8
  export declare const aabbFromMinMax: (min: Vec, max: Vec, attribs?: Attribs) => AABB;
9
+ export declare const aabbFromMinMaxWithMargin: (min: Vec, max: Vec, margin: number, attribs?: Attribs) => AABB;
10
+ export declare const aabbFromCentroid: (centroid: Vec, size: Vec, attribs?: Attribs) => AABB;
11
+ export declare const aabbFromCentroidWithMargin: (centroid: Vec, size: Vec, margin: number, attribs?: Attribs) => AABB;
9
12
  /**
10
13
  * Returns the intersection AABB of given inputs or `undefined` if they
11
14
  * are non-overlapping.
package/aabb.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { SQRT2_2 } from "@thi.ng/math/api";
2
2
  import { add3 } from "@thi.ng/vectors/add";
3
3
  import { ZERO3 } from "@thi.ng/vectors/api";
4
+ import { maddN3 } from "@thi.ng/vectors/maddn";
4
5
  import { max3 } from "@thi.ng/vectors/max";
5
6
  import { min3 } from "@thi.ng/vectors/min";
6
7
  import { sub3 } from "@thi.ng/vectors/sub";
@@ -11,6 +12,9 @@ export function aabb(...args) {
11
12
  return new AABB(...__argsVV(args));
12
13
  }
13
14
  export const aabbFromMinMax = (min, max, attribs) => new AABB(min, sub3([], max, min), attribs);
15
+ export const aabbFromMinMaxWithMargin = (min, max, margin, attribs) => aabbFromMinMax(min, max, attribs).offset(margin);
16
+ export const aabbFromCentroid = (centroid, size, attribs) => new AABB(maddN3([], size, -0.5, centroid), size, attribs);
17
+ export const aabbFromCentroidWithMargin = (centroid, size, margin, attribs) => aabbFromCentroid(centroid, size, attribs).offset(margin);
14
18
  /**
15
19
  * Returns the intersection AABB of given inputs or `undefined` if they
16
20
  * are non-overlapping.
package/api/aabb.d.ts CHANGED
@@ -8,5 +8,6 @@ export declare class AABB implements AABBLike {
8
8
  get type(): string;
9
9
  copy(): AABB;
10
10
  max(): Vec;
11
+ offset(offset: number): this;
11
12
  }
12
13
  //# sourceMappingURL=aabb.d.ts.map
package/api/aabb.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { isNumber } from "@thi.ng/checks/is-number";
2
2
  import { add3 } from "@thi.ng/vectors/add";
3
+ import { addN3 } from "@thi.ng/vectors/addn";
3
4
  import { set } from "@thi.ng/vectors/set";
5
+ import { subN3 } from "@thi.ng/vectors/subn";
4
6
  import { __copyAttribs } from "../internal/copy.js";
5
7
  export class AABB {
6
8
  constructor(pos = [0, 0, 0], size = 1, attribs) {
@@ -17,4 +19,9 @@ export class AABB {
17
19
  max() {
18
20
  return add3([], this.pos, this.size);
19
21
  }
22
+ offset(offset) {
23
+ subN3(null, this.pos, offset);
24
+ addN3(null, this.size, offset * 2);
25
+ return this;
26
+ }
20
27
  }
package/api/rect.d.ts CHANGED
@@ -8,6 +8,7 @@ export declare class Rect implements AABBLike, IHiccupShape {
8
8
  get type(): string;
9
9
  copy(): Rect;
10
10
  max(): Vec;
11
+ offset(offset: number): this;
11
12
  toHiccup(): (string | number | Attribs | undefined)[];
12
13
  }
13
14
  //# sourceMappingURL=rect.d.ts.map
package/api/rect.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import { isNumber } from "@thi.ng/checks/is-number";
2
2
  import { add2 } from "@thi.ng/vectors/add";
3
+ import { addN2 } from "@thi.ng/vectors/addn";
3
4
  import { set2 } from "@thi.ng/vectors/set";
5
+ import { subN2 } from "@thi.ng/vectors/subn";
4
6
  import { __copyAttribs } from "../internal/copy.js";
5
7
  export class Rect {
6
8
  constructor(pos = [0, 0], size = 1, attribs) {
@@ -17,6 +19,11 @@ export class Rect {
17
19
  max() {
18
20
  return add2([], this.pos, this.size);
19
21
  }
22
+ offset(offset) {
23
+ subN2(null, this.pos, offset);
24
+ addN2(null, this.size, offset * 2);
25
+ return this;
26
+ }
20
27
  toHiccup() {
21
28
  return ["rect", this.attribs, this.pos, this.size[0], this.size[1]];
22
29
  }
package/bounds.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { MultiFn1 } from "@thi.ng/defmulti";
1
+ import type { MultiFn1O } from "@thi.ng/defmulti";
2
2
  import type { AABBLike, IShape } from "@thi.ng/geom-api";
3
- export declare const bounds: MultiFn1<IShape, AABBLike | undefined>;
3
+ export declare const bounds: MultiFn1O<IShape, number, AABBLike | undefined>;
4
4
  //# sourceMappingURL=bounds.d.ts.map
package/bounds.js CHANGED
@@ -7,18 +7,18 @@ import { comp } from "@thi.ng/transducers/comp";
7
7
  import { filter } from "@thi.ng/transducers/filter";
8
8
  import { iterator1 } from "@thi.ng/transducers/iterator";
9
9
  import { map } from "@thi.ng/transducers/map";
10
+ import { addN2 } from "@thi.ng/vectors/addn";
10
11
  import { max } from "@thi.ng/vectors/max";
11
12
  import { min } from "@thi.ng/vectors/min";
12
13
  import { mul2 } from "@thi.ng/vectors/mul";
13
14
  import { mulN2 } from "@thi.ng/vectors/muln";
14
- import { set2 } from "@thi.ng/vectors/set";
15
15
  import { sub2 } from "@thi.ng/vectors/sub";
16
16
  import { subN2 } from "@thi.ng/vectors/subn";
17
- import { aabbFromMinMax } from "./aabb.js";
17
+ import { aabbFromMinMaxWithMargin } from "./aabb.js";
18
18
  import { Rect } from "./api/rect.js";
19
19
  import { __collBounds } from "./internal/bounds.js";
20
20
  import { __dispatch } from "./internal/dispatch.js";
21
- import { rectFromMinMax } from "./rect.js";
21
+ import { rectFromMinMaxWithMargin } from "./rect.js";
22
22
  export const bounds = defmulti(__dispatch, {
23
23
  aabb: "rect",
24
24
  bpatch: "points",
@@ -27,24 +27,29 @@ export const bounds = defmulti(__dispatch, {
27
27
  quad: "points",
28
28
  tri: "points",
29
29
  }, {
30
- arc: ($) => rectFromMinMax(...arcBounds($.pos, $.r, $.axis, $.start, $.end)),
31
- circle: ($) => new Rect(subN2([], $.pos, $.r), mulN2(null, [2, 2], $.r)),
32
- cubic: ({ points }) => rectFromMinMax(...cubicBounds(points[0], points[1], points[2], points[3])),
33
- ellipse: ($) => new Rect(sub2([], $.pos, $.r), mul2(null, [2, 2], $.r)),
34
- group: ($) => {
30
+ arc: ($, margin = 0) => rectFromMinMaxWithMargin(...arcBounds($.pos, $.r, $.axis, $.start, $.end), margin),
31
+ circle: ($, margin = 0) => new Rect(subN2([], $.pos, $.r + margin), mulN2(null, [2, 2], $.r + margin)),
32
+ cubic: ({ points }, margin = 0) => rectFromMinMaxWithMargin(...cubicBounds(points[0], points[1], points[2], points[3]), margin),
33
+ ellipse: ($, margin = 0) => {
34
+ const r = addN2([], $.r, margin);
35
+ return new Rect(sub2([], $.pos, r), mul2(null, [2, 2], r));
36
+ },
37
+ group: ($, margin = 0) => {
35
38
  const res = __collBounds($.children, bounds);
36
- return res ? new Rect(...res) : undefined;
39
+ return res ? new Rect(...res).offset(margin) : undefined;
37
40
  },
38
- line: ({ points: [a, b] }) => rectFromMinMax(min([], a, b), max([], a, b)),
39
- path: (path) => {
41
+ line: ({ points: [a, b] }, margin = 0) => rectFromMinMaxWithMargin(min([], a, b), max([], a, b), margin),
42
+ path: (path, margin = 0) => {
40
43
  const b = __collBounds([
41
44
  ...iterator1(comp(map((s) => s.geo), filter((s) => !!s)), path.segments),
42
45
  ], bounds);
43
- return b ? new Rect(...b) : undefined;
46
+ return b ? new Rect(...b).offset(margin) : undefined;
44
47
  },
45
- points: ($) => rectFromMinMax(...bounds2($.points)),
46
- points3: ($) => aabbFromMinMax(...bounds3($.points)),
47
- quadratic: ({ points }) => rectFromMinMax(...quadraticBounds(points[0], points[1], points[2])),
48
- rect: ($) => $.copy(),
49
- text: ($) => new Rect(set2([], $.pos), [0, 0]),
48
+ points: ($, margin = 0) => rectFromMinMaxWithMargin(...bounds2($.points), margin),
49
+ points3: ($, margin = 0) => aabbFromMinMaxWithMargin(...bounds3($.points), margin),
50
+ quadratic: ({ points }, margin = 0) => rectFromMinMaxWithMargin(...quadraticBounds(points[0], points[1], points[2]), margin),
51
+ rect: ($, margin = 0) => margin === 0
52
+ ? $.copy()
53
+ : $.copy().offset(margin),
54
+ text: ($, margin = 0) => new Rect(subN2([], $.pos, margin), [0, 0]), // TODO
50
55
  });
@@ -2,10 +2,10 @@ import type { Fn } from "@thi.ng/api";
2
2
  import type { AABBLike, IShape } from "@thi.ng/geom-api";
3
3
  import type { ReadonlyVec, VecPair } from "@thi.ng/vectors";
4
4
  /**
5
- * Computes the total bounds for the given shape collection, which
6
- * should either contain only 2D or 3D types. No mixed dimensions are
7
- * allowed! Currently the {@link bounds} function MUST be passed in as
8
- * arg to avoid circular module dependencies.
5
+ * Computes the total bounds for the given shape collection, which should either
6
+ * contain only 2D or 3D types. No mixed dimensions are allowed! Currently the
7
+ * {@link bounds} function MUST be passed in as arg to avoid circular module
8
+ * dependencies. Returns 2-tuple of `[pos, size]`.
9
9
  *
10
10
  * @param shapes - input shapes
11
11
  * @param bounds - bbox function
@@ -3,10 +3,10 @@ import { max } from "@thi.ng/vectors/max";
3
3
  import { min } from "@thi.ng/vectors/min";
4
4
  import { sub } from "@thi.ng/vectors/sub";
5
5
  /**
6
- * Computes the total bounds for the given shape collection, which
7
- * should either contain only 2D or 3D types. No mixed dimensions are
8
- * allowed! Currently the {@link bounds} function MUST be passed in as
9
- * arg to avoid circular module dependencies.
6
+ * Computes the total bounds for the given shape collection, which should either
7
+ * contain only 2D or 3D types. No mixed dimensions are allowed! Currently the
8
+ * {@link bounds} function MUST be passed in as arg to avoid circular module
9
+ * dependencies. Returns 2-tuple of `[pos, size]`.
10
10
  *
11
11
  * @param shapes - input shapes
12
12
  * @param bounds - bbox function
@@ -1,4 +1,6 @@
1
1
  import type { Attribs, IShape, PCLike, PCLikeConstructor } from "@thi.ng/geom-api";
2
+ /** @internal */
2
3
  export declare const __copyAttribs: ($: IShape) => Attribs;
3
- export declare const __copyShape: (ctor: PCLikeConstructor, inst: PCLike) => PCLike;
4
+ /** @internal */
5
+ export declare const __copyShape: <T extends PCLike>(ctor: PCLikeConstructor, inst: T) => T;
4
6
  //# sourceMappingURL=copy.d.ts.map
package/internal/copy.js CHANGED
@@ -1,3 +1,6 @@
1
+ // thing:export
1
2
  import { copyVectors } from "@thi.ng/vectors/copy";
3
+ /** @internal */
2
4
  export const __copyAttribs = ($) => ({ ...$.attribs });
5
+ /** @internal */
3
6
  export const __copyShape = (ctor, inst) => new ctor(copyVectors(inst.points), __copyAttribs(inst));
@@ -1,4 +1,6 @@
1
1
  import type { IShape } from "@thi.ng/geom-api";
2
+ /** @internal */
2
3
  export declare const __dispatch: (x: IShape) => string | number;
4
+ /** @internal */
3
5
  export declare const __dispatch2: (a: IShape, b: IShape) => string;
4
6
  //# sourceMappingURL=dispatch.d.ts.map
@@ -1,2 +1,4 @@
1
+ /** @internal */
1
2
  export const __dispatch = (x) => x.type;
3
+ /** @internal */
2
4
  export const __dispatch2 = (a, b) => a.type + "-" + b.type;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/geom",
3
- "version": "3.2.6",
3
+ "version": "3.3.0",
4
4
  "description": "Functional, polymorphic API for 2D geometry types & SVG generation",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -42,20 +42,20 @@
42
42
  "@thi.ng/defmulti": "^2.1.7",
43
43
  "@thi.ng/equiv": "^2.1.7",
44
44
  "@thi.ng/errors": "^2.1.7",
45
- "@thi.ng/geom-api": "^3.1.14",
46
- "@thi.ng/geom-arc": "^2.1.14",
47
- "@thi.ng/geom-clip-line": "^2.1.14",
48
- "@thi.ng/geom-clip-poly": "^2.1.14",
45
+ "@thi.ng/geom-api": "^3.2.0",
46
+ "@thi.ng/geom-arc": "^2.1.15",
47
+ "@thi.ng/geom-clip-line": "^2.1.15",
48
+ "@thi.ng/geom-clip-poly": "^2.1.15",
49
49
  "@thi.ng/geom-closest-point": "^2.1.14",
50
50
  "@thi.ng/geom-hull": "^2.1.14",
51
- "@thi.ng/geom-isec": "^2.1.14",
52
- "@thi.ng/geom-poly-utils": "^2.2.6",
53
- "@thi.ng/geom-resample": "^2.1.14",
54
- "@thi.ng/geom-splines": "^2.1.14",
55
- "@thi.ng/geom-subdiv-curve": "^2.1.14",
56
- "@thi.ng/geom-tessellate": "^2.1.14",
51
+ "@thi.ng/geom-isec": "^2.1.15",
52
+ "@thi.ng/geom-poly-utils": "^2.2.7",
53
+ "@thi.ng/geom-resample": "^2.1.15",
54
+ "@thi.ng/geom-splines": "^2.1.15",
55
+ "@thi.ng/geom-subdiv-curve": "^2.1.15",
56
+ "@thi.ng/geom-tessellate": "^2.1.15",
57
57
  "@thi.ng/hiccup": "^4.2.9",
58
- "@thi.ng/hiccup-svg": "^4.2.14",
58
+ "@thi.ng/hiccup-svg": "^4.3.0",
59
59
  "@thi.ng/math": "^5.3.3",
60
60
  "@thi.ng/matrices": "^2.1.14",
61
61
  "@thi.ng/random": "^3.3.2",
@@ -247,6 +247,15 @@
247
247
  "./group": {
248
248
  "default": "./group.js"
249
249
  },
250
+ "./internal/copy": {
251
+ "default": "./internal/copy.js"
252
+ },
253
+ "./internal/dispatch": {
254
+ "default": "./internal/dispatch.js"
255
+ },
256
+ "./internal/transform": {
257
+ "default": "./internal/transform.js"
258
+ },
250
259
  "./intersects": {
251
260
  "default": "./intersects.js"
252
261
  },
@@ -362,5 +371,5 @@
362
371
  "thi.ng": {
363
372
  "year": 2013
364
373
  },
365
- "gitHead": "ab0188234419f2d9f471de80871df930e5555bd6\n"
374
+ "gitHead": "f50e292ecc7dc74a6705b07fc739bf827cc5efd7\n"
366
375
  }
package/rect.d.ts CHANGED
@@ -7,7 +7,9 @@ export declare function rect(pos: Vec, size: number | Vec, attribs?: Attribs): R
7
7
  export declare function rect(size: number | Vec, attribs?: Attribs): Rect;
8
8
  export declare function rect(attribs?: Attribs): Rect;
9
9
  export declare const rectFromMinMax: (min: Vec, max: Vec, attribs?: Attribs) => Rect;
10
+ export declare const rectFromMinMaxWithMargin: (min: Vec, max: Vec, margin: number, attribs?: Attribs) => Rect;
10
11
  export declare const rectFromCentroid: (centroid: Vec, size: Vec, attribs?: Attribs) => Rect;
12
+ export declare const rectFromCentroidWithMargin: (centroid: Vec, size: Vec, margin: number, attribs?: Attribs) => Rect;
11
13
  /**
12
14
  * Returns the intersection rect of given inputs or `undefined` if they
13
15
  * are non-overlapping.
package/rect.js CHANGED
@@ -14,7 +14,9 @@ export function rect(...args) {
14
14
  return new Rect(...__argsVV(args));
15
15
  }
16
16
  export const rectFromMinMax = (min, max, attribs) => new Rect(min, sub2([], max, min), attribs);
17
+ export const rectFromMinMaxWithMargin = (min, max, margin, attribs) => rectFromMinMax(min, max, attribs).offset(margin);
17
18
  export const rectFromCentroid = (centroid, size, attribs) => new Rect(maddN2([], size, -0.5, centroid), size, attribs);
19
+ export const rectFromCentroidWithMargin = (centroid, size, margin, attribs) => rectFromCentroid(centroid, size, attribs).offset(margin);
18
20
  /**
19
21
  * Returns the intersection rect of given inputs or `undefined` if they
20
22
  * are non-overlapping.