@thi.ng/geom 7.0.1 → 8.0.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.
Files changed (251) hide show
  1. package/CHANGELOG.md +196 -1
  2. package/README.md +357 -114
  3. package/aabb.d.ts +26 -1
  4. package/api/aabb.d.ts +5 -4
  5. package/api/aabb.js +10 -7
  6. package/api/apc.d.ts +5 -3
  7. package/api/arc.d.ts +6 -5
  8. package/api/arc.js +3 -4
  9. package/api/bpatch.d.ts +6 -3
  10. package/api/bpatch.js +5 -3
  11. package/api/circle.d.ts +4 -3
  12. package/api/circle.js +9 -6
  13. package/api/complex-polygon.d.ts +7 -3
  14. package/api/complex-polygon.js +14 -4
  15. package/api/cubic.d.ts +8 -5
  16. package/api/cubic.js +12 -6
  17. package/api/cubic3.d.ts +15 -0
  18. package/api/cubic3.js +38 -0
  19. package/api/ellipse.d.ts +4 -3
  20. package/api/ellipse.js +7 -8
  21. package/api/extra.d.ts +15 -0
  22. package/api/extra.js +19 -0
  23. package/api/group.d.ts +12 -11
  24. package/api/group.js +4 -5
  25. package/api/group3.d.ts +30 -0
  26. package/api/group3.js +48 -0
  27. package/api/line.d.ts +8 -5
  28. package/api/line.js +8 -8
  29. package/api/line3.d.ts +15 -0
  30. package/api/line3.js +29 -0
  31. package/api/path.d.ts +14 -11
  32. package/api/path.js +17 -12
  33. package/api/path3.d.ts +28 -0
  34. package/api/path3.js +91 -0
  35. package/api/plane.d.ts +4 -3
  36. package/api/plane.js +7 -4
  37. package/api/points.d.ts +8 -10
  38. package/api/points.js +7 -20
  39. package/api/points3.d.ts +13 -0
  40. package/api/points3.js +21 -0
  41. package/api/polygon.d.ts +7 -4
  42. package/api/polygon.js +5 -3
  43. package/api/polygon3.d.ts +14 -0
  44. package/api/polygon3.js +24 -0
  45. package/api/polyline.d.ts +18 -4
  46. package/api/polyline.js +8 -5
  47. package/api/polyline3.d.ts +28 -0
  48. package/api/polyline3.js +31 -0
  49. package/api/quad.d.ts +7 -4
  50. package/api/quad.js +5 -3
  51. package/api/quad3.d.ts +7 -4
  52. package/api/quad3.js +6 -4
  53. package/api/quadratic.d.ts +8 -5
  54. package/api/quadratic.js +12 -6
  55. package/api/quadratic3.d.ts +15 -0
  56. package/api/quadratic3.js +38 -0
  57. package/api/ray.d.ts +4 -3
  58. package/api/ray.js +6 -7
  59. package/api/ray3.d.ts +14 -0
  60. package/api/ray3.js +33 -0
  61. package/api/rect.d.ts +5 -3
  62. package/api/rect.js +11 -8
  63. package/api/sphere.d.ts +4 -3
  64. package/api/sphere.js +8 -5
  65. package/api/text.d.ts +4 -3
  66. package/api/text.js +8 -5
  67. package/api/triangle.d.ts +7 -4
  68. package/api/triangle.js +5 -3
  69. package/api/triangle3.d.ts +14 -0
  70. package/api/triangle3.js +26 -0
  71. package/api.d.ts +193 -0
  72. package/api.js +10 -0
  73. package/apply-transforms.d.ts +33 -11
  74. package/apply-transforms.js +24 -4
  75. package/arc-length.d.ts +18 -4
  76. package/arc-length.js +18 -3
  77. package/arc.d.ts +1 -1
  78. package/area.d.ts +6 -10
  79. package/area.js +3 -3
  80. package/as-cubic.d.ts +31 -5
  81. package/as-cubic.js +86 -24
  82. package/as-path.d.ts +14 -7
  83. package/as-path.js +49 -24
  84. package/as-polygon.d.ts +20 -5
  85. package/as-polygon.js +46 -12
  86. package/as-polyline.d.ts +18 -5
  87. package/as-polyline.js +29 -23
  88. package/as-sector.d.ts +13 -0
  89. package/as-sector.js +18 -0
  90. package/as-svg.d.ts +31 -9
  91. package/as-svg.js +21 -18
  92. package/bounds.d.ts +12 -2
  93. package/bounds.js +15 -0
  94. package/bpatch.d.ts +26 -1
  95. package/center-of-weight.d.ts +22 -0
  96. package/center-of-weight.js +23 -0
  97. package/center.d.ts +11 -5
  98. package/center.js +9 -4
  99. package/centroid-of-bounds.d.ts +12 -0
  100. package/centroid-of-bounds.js +9 -0
  101. package/centroid.d.ts +14 -4
  102. package/centroid.js +16 -17
  103. package/circle.d.ts +1 -1
  104. package/classify-point.d.ts +3 -1
  105. package/classify-point.js +7 -4
  106. package/clip-convex.d.ts +27 -8
  107. package/clip-convex.js +52 -17
  108. package/closest-point.d.ts +1 -1
  109. package/complex-polygon-from-path.d.ts +1 -1
  110. package/complex-polygon.d.ts +1 -1
  111. package/convex-hull.d.ts +9 -2
  112. package/convex-hull.js +3 -3
  113. package/convolve.d.ts +72 -0
  114. package/convolve.js +33 -0
  115. package/cubic.d.ts +1 -1
  116. package/cubic.js +7 -7
  117. package/cubic3.d.ts +8 -0
  118. package/cubic3.js +14 -0
  119. package/edges.d.ts +2 -2
  120. package/ellipse.d.ts +1 -1
  121. package/extra.d.ts +14 -0
  122. package/extra.js +5 -0
  123. package/fit-into-bounds.d.ts +14 -4
  124. package/fit-into-bounds.js +6 -6
  125. package/flip.d.ts +21 -5
  126. package/flip.js +19 -12
  127. package/from-tessellation.d.ts +81 -0
  128. package/from-tessellation.js +27 -0
  129. package/group.d.ts +2 -2
  130. package/group3.d.ts +16 -0
  131. package/group3.js +5 -0
  132. package/index.d.ts +36 -1
  133. package/index.js +36 -1
  134. package/internal/bounds.d.ts +3 -4
  135. package/internal/copy.d.ts +8 -7
  136. package/internal/copy.js +3 -7
  137. package/internal/dispatch.d.ts +1 -1
  138. package/internal/error.d.ts +3 -0
  139. package/internal/error.js +6 -0
  140. package/internal/pclike.d.ts +3 -2
  141. package/internal/pclike.js +6 -0
  142. package/internal/points-as-shape.d.ts +11 -2
  143. package/internal/points-as-shape.js +10 -1
  144. package/internal/split.d.ts +2 -2
  145. package/internal/split.js +13 -8
  146. package/internal/transform.d.ts +6 -22
  147. package/internal/transform.js +2 -21
  148. package/internal/vertices.d.ts +3 -2
  149. package/internal/vertices.js +3 -1
  150. package/intersects.d.ts +5 -4
  151. package/intersects.js +6 -4
  152. package/line.d.ts +1 -1
  153. package/line3.d.ts +6 -0
  154. package/line3.js +9 -0
  155. package/map-point.d.ts +1 -1
  156. package/normalized-path.d.ts +17 -0
  157. package/normalized-path.js +23 -0
  158. package/offset.d.ts +16 -2
  159. package/offset.js +2 -2
  160. package/package.json +149 -35
  161. package/path-builder.d.ts +42 -13
  162. package/path-builder.js +68 -42
  163. package/path-from-cubics.d.ts +26 -0
  164. package/path-from-cubics.js +39 -0
  165. package/path-from-svg.d.ts +1 -1
  166. package/path-from-svg.js +29 -29
  167. package/path.d.ts +2 -52
  168. package/path.js +1 -58
  169. package/path3.d.ts +16 -0
  170. package/path3.js +5 -0
  171. package/plane.d.ts +11 -1
  172. package/plane.js +3 -0
  173. package/point-at.d.ts +1 -1
  174. package/point-inside.d.ts +3 -1
  175. package/point-inside.js +4 -1
  176. package/points.d.ts +2 -3
  177. package/points.js +2 -4
  178. package/points3.d.ts +5 -0
  179. package/points3.js +5 -0
  180. package/polygon.d.ts +5 -5
  181. package/polygon3.d.ts +5 -0
  182. package/polygon3.js +5 -0
  183. package/polyline.d.ts +2 -2
  184. package/polyline3.d.ts +5 -0
  185. package/polyline3.js +5 -0
  186. package/proximity.d.ts +5 -3
  187. package/proximity.js +2 -2
  188. package/quad.d.ts +2 -7
  189. package/quad.js +1 -29
  190. package/quad3.d.ts +8 -0
  191. package/quad3.js +30 -0
  192. package/quadratic.d.ts +1 -1
  193. package/quadratic3.d.ts +7 -0
  194. package/quadratic3.js +11 -0
  195. package/ray.d.ts +1 -1
  196. package/ray.js +2 -2
  197. package/ray3.d.ts +5 -0
  198. package/ray3.js +6 -0
  199. package/rect.d.ts +1 -1
  200. package/resample.d.ts +45 -4
  201. package/resample.js +26 -16
  202. package/rotate-around-axis.d.ts +33 -0
  203. package/rotate-around-axis.js +57 -0
  204. package/rotate.d.ts +15 -2
  205. package/rotate.js +47 -45
  206. package/rounded-rect.d.ts +25 -0
  207. package/rounded-rect.js +18 -0
  208. package/scale-with-center.d.ts +2 -2
  209. package/scale.d.ts +24 -2
  210. package/scale.js +59 -43
  211. package/scatter.d.ts +1 -1
  212. package/simplify.d.ts +8 -2
  213. package/simplify.js +7 -4
  214. package/smooth-poly.d.ts +27 -0
  215. package/smooth-poly.js +11 -0
  216. package/sphere.d.ts +1 -1
  217. package/split-arclength.d.ts +6 -2
  218. package/split-at.d.ts +18 -2
  219. package/split-at.js +66 -34
  220. package/split-near.d.ts +13 -2
  221. package/split-near.js +23 -27
  222. package/subdiv-curve.d.ts +81 -27
  223. package/subdiv-curve.js +44 -28
  224. package/tangent-at.d.ts +1 -1
  225. package/tessellate.d.ts +72 -18
  226. package/tessellate.js +53 -8
  227. package/text.d.ts +1 -1
  228. package/transform-vertices.d.ts +30 -6
  229. package/transform-vertices.js +36 -38
  230. package/transform.d.ts +30 -2
  231. package/transform.js +54 -40
  232. package/translate.d.ts +12 -2
  233. package/translate.js +42 -43
  234. package/triangle.d.ts +1 -1
  235. package/triangle3.d.ts +6 -0
  236. package/triangle3.js +8 -0
  237. package/union.d.ts +11 -2
  238. package/union.js +6 -7
  239. package/unmap-point.d.ts +1 -1
  240. package/vertices.d.ts +3 -3
  241. package/vertices.js +14 -4
  242. package/volume.d.ts +1 -1
  243. package/warp-points.d.ts +35 -4
  244. package/warp-points.js +2 -0
  245. package/with-attribs.d.ts +4 -5
  246. package/internal/rotate.d.ts +0 -5
  247. package/internal/rotate.js +0 -8
  248. package/internal/scale.d.ts +0 -5
  249. package/internal/scale.js +0 -12
  250. package/internal/translate.d.ts +0 -5
  251. package/internal/translate.js +0 -8
@@ -0,0 +1,25 @@
1
+ import type { Vec } from "@thi.ng/vectors";
2
+ import type { Attribs } from "./api.js";
3
+ import { Path } from "./api/path.js";
4
+ /**
5
+ * Creates a new 2d rounded rect {@link Path}, using the given corner radius or
6
+ * radii.
7
+ *
8
+ * @remarks
9
+ * If multiple `radii` are given, the interpretation logic is the same as:
10
+ * https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/roundRect
11
+ *
12
+ * - number: all corners
13
+ * - `[top-left-and-bottom-right, top-right-and-bottom-left]`
14
+ * - `[top-left, top-right-and-bottom-left, bottom-right]`
15
+ * - `[top-left, top-right, bottom-right, bottom-left]`
16
+ *
17
+ * No arc segments will be generated for those corners where the radius <= 0
18
+ *
19
+ * @param pos
20
+ * @param size
21
+ * @param radii
22
+ * @param attribs
23
+ */
24
+ export declare const roundedRect: (pos: Vec, [w, h]: Vec, radii: number | [number, number] | [number, number, number] | [number, number, number, number], attribs?: Attribs) => Path;
25
+ //# sourceMappingURL=rounded-rect.d.ts.map
@@ -0,0 +1,18 @@
1
+ import { isNumber } from "@thi.ng/checks/is-number";
2
+ import { Path } from "./api/path.js";
3
+ import { pathBuilder } from "./path-builder.js";
4
+ const roundedRect = (pos, [w, h], radii, attribs) => {
5
+ const [tl, tr, br, bl] = isNumber(radii) ? [radii, radii, radii, radii] : radii.length === 2 ? [radii[0], radii[1], radii[0], radii[1]] : radii.length === 3 ? [radii[0], radii[1], radii[2], radii[1]] : radii;
6
+ const b = pathBuilder(attribs).moveTo([pos[0] + tl, pos[1]]).hlineTo(w - tl - tr, true);
7
+ if (tr > 0) b.arcTo([tr, tr], [tr, tr], 0, false, true, true);
8
+ b.vlineTo(h - tr - br, true);
9
+ if (br > 0) b.arcTo([-br, br], [br, br], 0, false, true, true);
10
+ b.hlineTo(-(w - br - bl), true);
11
+ if (bl > 0) b.arcTo([-bl, -bl], [bl, bl], 0, false, true, true);
12
+ b.vlineTo(-(h - bl - tl), true);
13
+ if (tl > 0) b.arcTo([tl, -tl], [tl, tl], 0, false, true, true);
14
+ return b.current().close();
15
+ };
16
+ export {
17
+ roundedRect
18
+ };
@@ -1,4 +1,4 @@
1
- import type { IShape } from "@thi.ng/geom-api";
1
+ import type { IShape } from "./api.js";
2
2
  import type { ReadonlyVec } from "@thi.ng/vectors";
3
3
  /**
4
4
  * Applies a sequence of translate and scale operations to return an uniformly
@@ -16,5 +16,5 @@ import type { ReadonlyVec } from "@thi.ng/vectors";
16
16
  * @param center
17
17
  * @param factor
18
18
  */
19
- export declare const scaleWithCenter: (shape: IShape, center: ReadonlyVec, factor: number) => IShape<IShape<any>>;
19
+ export declare const scaleWithCenter: <T extends IShape>(shape: T, center: ReadonlyVec, factor: number) => T;
20
20
  //# sourceMappingURL=scale-with-center.d.ts.map
package/scale.d.ts CHANGED
@@ -1,6 +1,17 @@
1
1
  import type { MultiFn2 } from "@thi.ng/defmulti";
2
- import type { IShape } from "@thi.ng/geom-api";
3
2
  import type { ReadonlyVec } from "@thi.ng/vectors";
3
+ import type { IShape, IShape2, IShape3 } from "./api.js";
4
+ import { Circle } from "./api/circle.js";
5
+ import { Ellipse } from "./api/ellipse.js";
6
+ /**
7
+ * Function overrides for {@link scale}.
8
+ */
9
+ export type ScaleFn = {
10
+ (shape: Circle, factor: number): Circle;
11
+ (shape: Circle, factor: ReadonlyVec): Ellipse;
12
+ <T extends IShape2>(shape: T, factor: number | ReadonlyVec): T;
13
+ <T extends IShape3>(shape: T, factor: number | ReadonlyVec): T;
14
+ } & MultiFn2<IShape, number | ReadonlyVec, IShape>;
4
15
  /**
5
16
  * Scales given shape uniformly or non-uniformly by given `factor`.
6
17
  *
@@ -12,20 +23,31 @@ import type { ReadonlyVec } from "@thi.ng/vectors";
12
23
  *
13
24
  * - {@link AABB}
14
25
  * - {@link Arc}
26
+ * - {@link BPatch}
15
27
  * - {@link Circle}
16
28
  * - {@link ComplexPolygon}
17
29
  * - {@link Cubic}
30
+ * - {@link Cubic3}
18
31
  * - {@link Ellipse}
32
+ * - {@link Extra} (returns `undefined`)
19
33
  * - {@link Group}
34
+ * - {@link Group3}
20
35
  * - {@link Line}
36
+ * - {@link Line3}
21
37
  * - {@link Path}
38
+ * - {@link Path3}
22
39
  * - {@link Points}
23
40
  * - {@link Points3}
24
41
  * - {@link Polygon}
42
+ * - {@link Polygon3}
25
43
  * - {@link Polyline}
44
+ * - {@link Polyline3}
26
45
  * - {@link Quad}
46
+ * - {@link Quad3}
27
47
  * - {@link Quadratic}
48
+ * - {@link Quadratic3}
28
49
  * - {@link Ray}
50
+ * - {@link Ray3}
29
51
  * - {@link Rect}
30
52
  * - {@link Sphere}
31
53
  * - {@link Text}
@@ -34,5 +56,5 @@ import type { ReadonlyVec } from "@thi.ng/vectors";
34
56
  * @param shape
35
57
  * @param factor
36
58
  */
37
- export declare const scale: MultiFn2<IShape, number | ReadonlyVec, IShape>;
59
+ export declare const scale: ScaleFn;
38
60
  //# sourceMappingURL=scale.d.ts.map
package/scale.js CHANGED
@@ -1,41 +1,50 @@
1
1
  import { isNumber } from "@thi.ng/checks/is-number";
2
2
  import { defmulti } from "@thi.ng/defmulti/defmulti";
3
3
  import { unsupported } from "@thi.ng/errors/unsupported";
4
- import { mul2, mul3 } from "@thi.ng/vectors/mul";
4
+ import { mul, mul2, mul3 } from "@thi.ng/vectors/mul";
5
5
  import { mulN2, mulN3 } from "@thi.ng/vectors/muln";
6
- import { normalize2 } from "@thi.ng/vectors/normalize";
6
+ import { normalize2, normalize3 } from "@thi.ng/vectors/normalize";
7
7
  import { AABB } from "./api/aabb.js";
8
8
  import { Circle } from "./api/circle.js";
9
- import { ComplexPolygon } from "./api/complex-polygon.js";
10
- import { Cubic } from "./api/cubic.js";
11
9
  import { Ellipse } from "./api/ellipse.js";
12
- import { Line } from "./api/line.js";
13
- import { Path } from "./api/path.js";
14
- import { Points, Points3 } from "./api/points.js";
15
- import { Polygon } from "./api/polygon.js";
16
- import { Polyline } from "./api/polyline.js";
17
- import { Quad } from "./api/quad.js";
18
- import { Quadratic } from "./api/quadratic.js";
19
10
  import { Ray } from "./api/ray.js";
11
+ import { Ray3 } from "./api/ray3.js";
20
12
  import { Rect } from "./api/rect.js";
21
13
  import { Sphere } from "./api/sphere.js";
22
14
  import { Text } from "./api/text.js";
23
- import { Triangle } from "./api/triangle.js";
24
15
  import { __asVec } from "./internal/args.js";
25
16
  import { __copyAttribs } from "./internal/copy.js";
26
17
  import { __dispatch } from "./internal/dispatch.js";
27
- import { __scaledShape as tx } from "./internal/scale.js";
28
18
  import { __segmentTransformer } from "./internal/transform.js";
29
19
  const scale = defmulti(
30
20
  __dispatch,
31
- {},
21
+ {
22
+ bpatch: "points",
23
+ complexpoly: "group",
24
+ cubic: "points",
25
+ cubic3: "points3",
26
+ group3: "group",
27
+ line: "points",
28
+ line3: "points3",
29
+ path3: "path",
30
+ poly: "points",
31
+ poly3: "points3",
32
+ polyline: "points",
33
+ polyline3: "points3",
34
+ quad: "points",
35
+ quad3: "points3",
36
+ quadratic: "points",
37
+ quadratic3: "points3",
38
+ tri: "points",
39
+ tri3: "points3"
40
+ },
32
41
  {
33
42
  aabb: ($, delta) => {
34
43
  delta = __asVec(delta, 3);
35
44
  return new AABB(
36
45
  mul3([], $.pos, delta),
37
46
  mul3([], $.size, delta),
38
- __copyAttribs($)
47
+ __copyAttribs($.attribs)
39
48
  );
40
49
  },
41
50
  arc: ($, delta) => {
@@ -48,51 +57,55 @@ const scale = defmulti(
48
57
  circle: ($, delta) => isNumber(delta) ? new Circle(
49
58
  mulN2([], $.pos, delta),
50
59
  $.r * delta,
51
- __copyAttribs($)
60
+ __copyAttribs($.attribs)
52
61
  ) : new Ellipse(
53
62
  mul2([], $.pos, delta),
54
63
  mulN2([], delta, $.r),
55
- __copyAttribs($)
56
- ),
57
- complexpoly: ($, delta) => new ComplexPolygon(
58
- scale($.boundary, delta),
59
- $.children.map((child) => scale(child, delta))
64
+ __copyAttribs($.attribs)
60
65
  ),
61
- cubic: tx(Cubic),
62
66
  ellipse: ($, delta) => {
63
67
  delta = __asVec(delta);
64
68
  return new Ellipse(
65
69
  mul2([], $.pos, delta),
66
70
  mul2([], $.r, delta),
67
- __copyAttribs($)
71
+ __copyAttribs($.attribs)
68
72
  );
69
73
  },
74
+ extra: ($) => $,
70
75
  group: ($, delta) => $.copyTransformed((x) => scale(x, delta)),
71
- line: tx(Line),
72
76
  path: ($, delta) => {
73
77
  delta = __asVec(delta);
74
- const $scaleSegments = __segmentTransformer(
75
- (geo) => scale(geo, delta),
76
- (p) => mul2([], p, delta)
77
- );
78
- return new Path(
79
- $scaleSegments($.segments),
80
- $.subPaths.map($scaleSegments),
81
- __copyAttribs($)
78
+ return $.copyTransformed(
79
+ __segmentTransformer(
80
+ (geo) => scale(geo, delta),
81
+ (p) => mul([], p, delta)
82
+ )
82
83
  );
83
84
  },
84
- points: tx(Points),
85
- points3: tx(Points3),
86
- poly: tx(Polygon),
87
- polyline: tx(Polyline),
88
- quad: tx(Quad),
89
- quadratic: tx(Quadratic),
85
+ points: ($, delta) => $.copyTransformed(
86
+ (points) => points.map(
87
+ isNumber(delta) ? (x) => mulN2([], x, delta) : (x) => mul2([], x, delta)
88
+ )
89
+ ),
90
+ points3: ($, delta) => $.copyTransformed(
91
+ (points) => points.map(
92
+ isNumber(delta) ? (x) => mulN3([], x, delta) : (x) => mul3([], x, delta)
93
+ )
94
+ ),
90
95
  ray: ($, delta) => {
91
96
  delta = __asVec(delta);
92
97
  return new Ray(
93
98
  mul2([], $.pos, delta),
94
99
  normalize2(null, mul2([], $.dir, delta)),
95
- __copyAttribs($)
100
+ __copyAttribs($.attribs)
101
+ );
102
+ },
103
+ ray3: ($, delta) => {
104
+ delta = __asVec(delta);
105
+ return new Ray3(
106
+ mul3([], $.pos, delta),
107
+ normalize3(null, mul3([], $.dir, delta)),
108
+ __copyAttribs($.attribs)
96
109
  );
97
110
  },
98
111
  rect: ($, delta) => {
@@ -100,16 +113,19 @@ const scale = defmulti(
100
113
  return new Rect(
101
114
  mul2([], $.pos, delta),
102
115
  mul2([], $.size, delta),
103
- __copyAttribs($)
116
+ __copyAttribs($.attribs)
104
117
  );
105
118
  },
106
119
  sphere: ($, delta) => isNumber(delta) ? new Sphere(
107
120
  mulN3([], $.pos, delta),
108
121
  $.r * delta,
109
- __copyAttribs($)
122
+ __copyAttribs($.attribs)
110
123
  ) : unsupported("can't non-uniformly scale sphere"),
111
- text: ($, delta) => new Text(mul2([], $.pos, __asVec(delta)), $.body, __copyAttribs($)),
112
- tri: tx(Triangle)
124
+ text: ($, delta) => new Text(
125
+ mul2([], $.pos, __asVec(delta)),
126
+ $.body,
127
+ __copyAttribs($.attribs)
128
+ )
113
129
  }
114
130
  );
115
131
  export {
package/scatter.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { IShape } from "@thi.ng/geom-api";
1
+ import type { IShape } from "./api.js";
2
2
  import type { Vec } from "@thi.ng/vectors";
3
3
  /**
4
4
  * Produces `num` random points for which {@link pointInside} succeeds for the
package/simplify.d.ts CHANGED
@@ -1,5 +1,11 @@
1
1
  import type { MultiFn1O } from "@thi.ng/defmulti";
2
- import type { IShape } from "@thi.ng/geom-api";
2
+ import type { IShape, IShape2 } from "./api.js";
3
+ /**
4
+ * Function overrides for {@link simplify}.
5
+ */
6
+ export type SimplifyFn = {
7
+ <T extends IShape2>(shape: IShape2, eps?: number): T;
8
+ } & MultiFn1O<IShape, number, IShape>;
3
9
  /**
4
10
  * Simplifies given 2D shape boundary using Douglas-Peucker algorithm
5
11
  * (implemented by
@@ -21,5 +27,5 @@ import type { IShape } from "@thi.ng/geom-api";
21
27
  * @param shape
22
28
  * @param threshold
23
29
  */
24
- export declare const simplify: MultiFn1O<IShape, number, IShape>;
30
+ export declare const simplify: SimplifyFn;
25
31
  //# sourceMappingURL=simplify.d.ts.map
package/simplify.js CHANGED
@@ -15,7 +15,7 @@ const simplify = defmulti(
15
15
  complexpoly: ($, eps) => new ComplexPolygon(
16
16
  simplify($.boundary, eps),
17
17
  $.children.map((child) => simplify(child, eps)),
18
- __copyAttribs($)
18
+ __copyAttribs($.attribs)
19
19
  ),
20
20
  path: ($, eps = 1e-6) => {
21
21
  const $simplifySegments = (segments) => {
@@ -51,11 +51,14 @@ const simplify = defmulti(
51
51
  return new Path(
52
52
  $simplifySegments($.segments),
53
53
  $.subPaths.map($simplifySegments),
54
- __copyAttribs($)
54
+ __copyAttribs($.attribs)
55
55
  );
56
56
  },
57
- poly: ($, eps = 1e-6) => new Polygon(_simplify($.points, eps, true), __copyAttribs($)),
58
- polyline: ($, eps = 1e-6) => new Polyline(_simplify($.points, eps), __copyAttribs($))
57
+ poly: ($, eps = 1e-6) => new Polygon(
58
+ _simplify($.points, eps, true),
59
+ __copyAttribs($.attribs)
60
+ ),
61
+ polyline: ($, eps = 1e-6) => new Polyline(_simplify($.points, eps), __copyAttribs($.attribs))
59
62
  }
60
63
  );
61
64
  export {
@@ -0,0 +1,27 @@
1
+ import type { ReadonlyVec } from "@thi.ng/vectors";
2
+ import type { Attribs, CubicOpts, SamplingOpts } from "./api.js";
3
+ import { Polygon } from "./api/polygon.js";
4
+ import { Polyline } from "./api/polyline.js";
5
+ import { type AsPathOpts } from "./as-path.js";
6
+ /**
7
+ * Creates a new polygon from given initial `points` and optional `attribs`. The
8
+ * polygon is then first converted into a cubic path using `pathOpts` and then
9
+ * back into a polygon by sampling the path using `polyOpts`. Returns that final
10
+ * polygon result.
11
+ *
12
+ * @param points
13
+ * @param attribs
14
+ * @param pathOpts
15
+ * @param polyOpts
16
+ */
17
+ export declare const smoothPolygon: (points: ReadonlyVec[], attribs?: Attribs, pathOpts?: Partial<CubicOpts>, polyOpts?: Partial<SamplingOpts>) => Polygon;
18
+ /**
19
+ * Like {@link smoothPolygon}, but for polylines.
20
+ *
21
+ * @param points
22
+ * @param attribs
23
+ * @param pathOpts
24
+ * @param polyOpts
25
+ */
26
+ export declare const smoothPolyline: (points: ReadonlyVec[], attribs?: Attribs, pathOpts?: Partial<AsPathOpts>, polyOpts?: Partial<SamplingOpts>) => Polyline;
27
+ //# sourceMappingURL=smooth-poly.d.ts.map
package/smooth-poly.js ADDED
@@ -0,0 +1,11 @@
1
+ import { Polygon } from "./api/polygon.js";
2
+ import { Polyline } from "./api/polyline.js";
3
+ import { asPath } from "./as-path.js";
4
+ import { asPolygon } from "./as-polygon.js";
5
+ import { asPolyline } from "./as-polyline.js";
6
+ const smoothPolygon = (points, attribs, pathOpts, polyOpts) => asPolygon(asPath(new Polygon(points, attribs), pathOpts), polyOpts)[0];
7
+ const smoothPolyline = (points, attribs, pathOpts, polyOpts) => asPolyline(asPath(new Polyline(points, attribs), pathOpts), polyOpts)[0];
8
+ export {
9
+ smoothPolygon,
10
+ smoothPolyline
11
+ };
package/sphere.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Attribs } from "@thi.ng/geom-api";
1
+ import type { Attribs } from "./api.js";
2
2
  import type { ReadonlyVec, Vec } from "@thi.ng/vectors";
3
3
  import { Sphere } from "./api/sphere.js";
4
4
  export declare function sphere(pos: Vec, r: number, attribs?: Attribs): Sphere;
@@ -1,6 +1,10 @@
1
1
  import type { MultiFn2 } from "@thi.ng/defmulti";
2
- import type { IShape } from "@thi.ng/geom-api";
2
+ import type { IShape } from "./api.js";
3
3
  import { Group } from "./api/group.js";
4
+ /**
5
+ * Function overrides for {@link splitArcLength}.
6
+ */
7
+ export type SplitArcLengthFn = {} & MultiFn2<IShape, number, Group>;
4
8
  /**
5
9
  * Splits given shape into {@link Polyline} segments of given (max) arc length.
6
10
  * Returns a {@link Group} of shapes/polylines.
@@ -66,5 +70,5 @@ import { Group } from "./api/group.js";
66
70
  * @param shape
67
71
  * @param dist
68
72
  */
69
- export declare const splitArcLength: MultiFn2<IShape, number, Group>;
73
+ export declare const splitArcLength: SplitArcLengthFn;
70
74
  //# sourceMappingURL=split-arclength.d.ts.map
package/split-at.d.ts CHANGED
@@ -1,6 +1,17 @@
1
1
  import type { Maybe } from "@thi.ng/api";
2
2
  import type { MultiFn2 } from "@thi.ng/defmulti";
3
- import type { IShape } from "@thi.ng/geom-api";
3
+ import type { IShape, IShape2, IShape3 } from "./api.js";
4
+ import { Arc } from "./api/arc.js";
5
+ import type { Circle } from "./api/circle.js";
6
+ /**
7
+ * Function overrides for {@link splitAt}.
8
+ */
9
+ export type SplitAtFn = {
10
+ (shape: Arc, t: number): [Arc, Arc];
11
+ (shape: Circle, t: number): [Arc, Arc];
12
+ <T extends IShape2>(shape: T, t: number): T[];
13
+ <T extends IShape3>(shape: T, t: number): T[];
14
+ } & MultiFn2<IShape, number, Maybe<IShape[]>>;
4
15
  /**
5
16
  * Splits given shape in 2 parts at normalized parametric position `t`.
6
17
  *
@@ -8,13 +19,18 @@ import type { IShape } from "@thi.ng/geom-api";
8
19
  * Currently only implemented for:
9
20
  *
10
21
  * - {@link Arc}
22
+ * - {@link Circle}
11
23
  * - {@link Cubic}
24
+ * - {@link Cubic3}
12
25
  * - {@link Line}
26
+ * - {@link Line3}
13
27
  * - {@link Polyline}
28
+ * - {@link Polyline3}
14
29
  * - {@link Quadratic}
30
+ * - {@link Quadratic3}
15
31
  *
16
32
  * @param shape
17
33
  * @param t
18
34
  */
19
- export declare const splitAt: MultiFn2<IShape, number, Maybe<IShape[]>>;
35
+ export declare const splitAt: SplitAtFn;
20
36
  //# sourceMappingURL=split-at.d.ts.map
package/split-at.js CHANGED
@@ -2,59 +2,91 @@ import { defmulti } from "@thi.ng/defmulti/defmulti";
2
2
  import { Sampler } from "@thi.ng/geom-resample/sampler";
3
3
  import { cubicSplitAt } from "@thi.ng/geom-splines/cubic-split";
4
4
  import { quadraticSplitAt } from "@thi.ng/geom-splines/quadratic-split";
5
+ import { absDiff } from "@thi.ng/math/abs";
6
+ import { PI, TAU } from "@thi.ng/math/api";
5
7
  import { fit01 } from "@thi.ng/math/fit";
6
- import { set } from "@thi.ng/vectors/set";
8
+ import { set2 } from "@thi.ng/vectors/set";
7
9
  import { Arc } from "./api/arc.js";
8
- import { Cubic } from "./api/cubic.js";
9
- import { Line } from "./api/line.js";
10
- import { Polyline } from "./api/polyline.js";
11
- import { Quadratic } from "./api/quadratic.js";
12
10
  import { __copyAttribs } from "./internal/copy.js";
13
11
  import { __dispatch } from "./internal/dispatch.js";
14
12
  import { __pointArraysAsShapes } from "./internal/points-as-shape.js";
15
- import { __splitLine } from "./internal/split.js";
13
+ import { __splitLineAt } from "./internal/split.js";
16
14
  const splitAt = defmulti(
17
15
  __dispatch,
18
- {},
19
16
  {
20
- arc: ($, t) => {
21
- const theta = fit01(t, $.start, $.end);
17
+ cubic3: "cubic",
18
+ line3: "line",
19
+ polyline3: "polyline",
20
+ quadratic3: "quadratic"
21
+ },
22
+ {
23
+ arc: ({ pos, r, start, end, axis, cw, attribs }, t) => {
24
+ const theta = fit01(t, start, end);
22
25
  return [
23
26
  new Arc(
24
- set([], $.pos),
25
- set([], $.r),
26
- $.axis,
27
- $.start,
27
+ set2([], pos),
28
+ set2([], r),
29
+ axis,
30
+ start,
28
31
  theta,
29
- $.xl,
30
- $.cw,
31
- __copyAttribs($)
32
+ absDiff(start, theta) >= PI,
33
+ cw,
34
+ __copyAttribs(attribs)
32
35
  ),
33
36
  new Arc(
34
- set([], $.pos),
35
- set([], $.r),
36
- $.axis,
37
+ set2([], pos),
38
+ set2([], r),
39
+ axis,
37
40
  theta,
38
- $.end,
39
- $.xl,
40
- $.cw,
41
- __copyAttribs($)
41
+ end,
42
+ absDiff(theta, end) >= PI,
43
+ cw,
44
+ __copyAttribs(attribs)
42
45
  )
43
46
  ];
44
47
  },
45
- cubic: ({ attribs, points }, t) => cubicSplitAt(points[0], points[1], points[2], points[3], t).map(
46
- (pts) => new Cubic(pts, { ...attribs })
47
- ),
48
- line: ({ attribs, points }, t) => __splitLine(points[0], points[1], t).map(
49
- (pts) => new Line(pts, { ...attribs })
48
+ circle: ({ pos, r, attribs }, t) => {
49
+ const theta = t * TAU;
50
+ return [
51
+ new Arc(
52
+ set2([], pos),
53
+ [r, r],
54
+ 0,
55
+ 0,
56
+ theta,
57
+ t >= 0.5,
58
+ true,
59
+ __copyAttribs(attribs)
60
+ ),
61
+ new Arc(
62
+ set2([], pos),
63
+ [r, r],
64
+ 0,
65
+ theta,
66
+ TAU,
67
+ t < 0.5,
68
+ true,
69
+ __copyAttribs(attribs)
70
+ )
71
+ ];
72
+ },
73
+ cubic: ({ attribs, points: [a, b, c, d], constructor: ctor }, t) => __pointArraysAsShapes(
74
+ ctor,
75
+ cubicSplitAt(a, b, c, d, t),
76
+ attribs,
77
+ false
50
78
  ),
51
- polyline: ($, t) => __pointArraysAsShapes(
52
- Polyline,
53
- new Sampler($.points).splitAt(t),
54
- $.attribs
79
+ line: __splitLineAt,
80
+ polyline: ({ attribs, points, constructor: ctor }, t) => __pointArraysAsShapes(
81
+ ctor,
82
+ new Sampler(points).splitAt(t),
83
+ attribs
55
84
  ),
56
- quadratic: ({ attribs, points }, t) => quadraticSplitAt(points[0], points[1], points[2], t).map(
57
- (pts) => new Quadratic(pts, { ...attribs })
85
+ quadratic: ({ attribs, points: [a, b, c], constructor: ctor }, t) => __pointArraysAsShapes(
86
+ ctor,
87
+ quadraticSplitAt(a, b, c, t),
88
+ attribs,
89
+ false
58
90
  )
59
91
  }
60
92
  );
package/split-near.d.ts CHANGED
@@ -1,7 +1,14 @@
1
1
  import type { Maybe } from "@thi.ng/api";
2
2
  import type { MultiFn2 } from "@thi.ng/defmulti";
3
- import type { IShape } from "@thi.ng/geom-api";
4
3
  import type { ReadonlyVec } from "@thi.ng/vectors";
4
+ import type { IShape, IShape2, IShape3 } from "./api.js";
5
+ /**
6
+ * Function overrides for {@link splitNearPoint}.
7
+ */
8
+ export type SplitNearPointFn = {
9
+ <T extends IShape2>(shape: T, p: ReadonlyVec): T[];
10
+ <T extends IShape3>(shape: T, p: ReadonlyVec): T[];
11
+ } & MultiFn2<IShape, ReadonlyVec, Maybe<IShape[]>>;
5
12
  /**
6
13
  * Similar to {@link splitAt}, but instead of taking a normalized parametric
7
14
  * split position, splits the given curve at the closest point to `p`.
@@ -10,12 +17,16 @@ import type { ReadonlyVec } from "@thi.ng/vectors";
10
17
  * Currently only implemented for:
11
18
  *
12
19
  * - {@link Cubic}
20
+ * - {@link Cubic3}
13
21
  * - {@link Line}
22
+ * - {@link Line3}
14
23
  * - {@link Polyline}
24
+ * - {@link Polyline3}
15
25
  * - {@link Quadratic}
26
+ * - {@link Quadratic3}
16
27
  *
17
28
  * @param shape - shape to operate on
18
29
  * @param p - split point
19
30
  */
20
- export declare const splitNearPoint: MultiFn2<IShape, ReadonlyVec, Maybe<IShape[]>>;
31
+ export declare const splitNearPoint: SplitNearPointFn;
21
32
  //# sourceMappingURL=split-near.d.ts.map