@immugio/three-math-extensions 0.0.17 → 0.1.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
@@ -7,13 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).
9
9
 
10
- ## [0.0.17](https://github.com/Immugio/three-math-extensions/compare/16.15.10...0.0.17)
10
+ ## [0.1.0](https://github.com/Immugio/three-math-extensions/compare/16.15.10...0.1.0)
11
11
 
12
12
  ### Commits
13
13
 
14
+ - Add Polygon [`629cff8`](https://github.com/Immugio/three-math-extensions/commit/629cff8ecbb963477e8ea76d7f8b16d95435cbad)
14
15
  - Vec2.fromPoint and Vec3.fromPoint should accept null [`4b871af`](https://github.com/Immugio/three-math-extensions/commit/4b871af297bdcbe8584f1e2b99d602247b77687c)
15
16
 
16
- ## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.0.16...16.15.10) - 2023-01-02
17
+ ## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.0.17...16.15.10) - 2023-01-02
18
+
19
+ ## [0.0.17](https://github.com/Immugio/three-math-extensions/compare/0.0.16...0.0.17) - 2023-01-02
20
+
21
+ ### Commits
22
+
23
+ - Vec2.fromPoint and Vec3.fromPoint should accept null [`4b871af`](https://github.com/Immugio/three-math-extensions/commit/4b871af297bdcbe8584f1e2b99d602247b77687c)
17
24
 
18
25
  ## [0.0.16](https://github.com/Immugio/three-math-extensions/compare/0.0.15...0.0.16) - 2022-12-28
19
26
 
package/cjs/Polygon.js ADDED
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Polygon = void 0;
4
+ const Vec2_1 = require("./Vec2");
5
+ const Size2_1 = require("./Size2");
6
+ class Polygon {
7
+ contour;
8
+ holes;
9
+ constructor(contour, holes) {
10
+ this.contour = contour;
11
+ this.holes = holes;
12
+ }
13
+ static fromPoints(contour, holes) {
14
+ return new Polygon(contour.map(p => Vec2_1.Vec2.fromPoint(p)), holes?.map(h => h.map(p => Vec2_1.Vec2.fromPoint(p))));
15
+ }
16
+ get size() {
17
+ const { minX, maxX, minY, maxY } = this.boundingBox();
18
+ return new Size2_1.Size2(maxX - minX, maxY - minY);
19
+ }
20
+ centerOnOrigin() {
21
+ const center = this.center();
22
+ function centerPoints(points) {
23
+ for (const point of points) {
24
+ point.x -= center.x;
25
+ point.y -= center.y;
26
+ }
27
+ }
28
+ centerPoints(this.contour);
29
+ for (const hole of this.holes || []) {
30
+ centerPoints(hole);
31
+ }
32
+ return this;
33
+ }
34
+ center() {
35
+ const { minX, maxX, minY, maxY } = this.boundingBox();
36
+ const x = (maxX + minX) / 2;
37
+ const y = (maxY + minY) / 2;
38
+ return new Vec2_1.Vec2(x, y);
39
+ }
40
+ ensureLastPoint() {
41
+ function ensure(points) {
42
+ if (points[0].x !== points.at(-1).x || points[0].y !== points.at(-1).y) {
43
+ points.push(points[0].clone());
44
+ }
45
+ }
46
+ ensure(this.contour);
47
+ for (const hole of this.holes || []) {
48
+ ensure(hole);
49
+ }
50
+ return this;
51
+ }
52
+ boundingBox() {
53
+ let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
54
+ for (const p of this.contour) {
55
+ if (minX > p.x)
56
+ minX = p.x;
57
+ if (maxX < p.x)
58
+ maxX = p.x;
59
+ if (minY > p.y)
60
+ minY = p.y;
61
+ if (maxY < p.y)
62
+ maxY = p.y;
63
+ }
64
+ return { minX, maxX, minY, maxY };
65
+ }
66
+ toBoundingPolygon() {
67
+ const bounding = this.boundingBox();
68
+ return new Polygon([
69
+ new Vec2_1.Vec2(bounding.minX, bounding.minY),
70
+ new Vec2_1.Vec2(bounding.maxX, bounding.minY),
71
+ new Vec2_1.Vec2(bounding.maxX, bounding.maxY),
72
+ new Vec2_1.Vec2(bounding.minX, bounding.maxY),
73
+ ]);
74
+ }
75
+ flip() {
76
+ const centerX = this.center().x;
77
+ this.flipSingle(centerX, this.contour);
78
+ this.holes?.forEach(hole => this.flipSingle(centerX, hole));
79
+ return this;
80
+ }
81
+ flipSingle(centerX, poly) {
82
+ for (const point of poly) {
83
+ const xDistanceToCenter = Math.abs(centerX - point.x);
84
+ point.x = point.x < centerX ? centerX + xDistanceToCenter : centerX - xDistanceToCenter;
85
+ }
86
+ }
87
+ }
88
+ exports.Polygon = Polygon;
package/cjs/Size2.js ADDED
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Size2 = void 0;
4
+ class Size2 {
5
+ width;
6
+ height;
7
+ constructor(width, height) {
8
+ this.width = width;
9
+ this.height = height;
10
+ }
11
+ }
12
+ exports.Size2 = Size2;
package/esm/Polygon.js ADDED
@@ -0,0 +1,84 @@
1
+ import { Vec2 } from "./Vec2";
2
+ import { Size2 } from "./Size2";
3
+ export class Polygon {
4
+ contour;
5
+ holes;
6
+ constructor(contour, holes) {
7
+ this.contour = contour;
8
+ this.holes = holes;
9
+ }
10
+ static fromPoints(contour, holes) {
11
+ return new Polygon(contour.map(p => Vec2.fromPoint(p)), holes?.map(h => h.map(p => Vec2.fromPoint(p))));
12
+ }
13
+ get size() {
14
+ const { minX, maxX, minY, maxY } = this.boundingBox();
15
+ return new Size2(maxX - minX, maxY - minY);
16
+ }
17
+ centerOnOrigin() {
18
+ const center = this.center();
19
+ function centerPoints(points) {
20
+ for (const point of points) {
21
+ point.x -= center.x;
22
+ point.y -= center.y;
23
+ }
24
+ }
25
+ centerPoints(this.contour);
26
+ for (const hole of this.holes || []) {
27
+ centerPoints(hole);
28
+ }
29
+ return this;
30
+ }
31
+ center() {
32
+ const { minX, maxX, minY, maxY } = this.boundingBox();
33
+ const x = (maxX + minX) / 2;
34
+ const y = (maxY + minY) / 2;
35
+ return new Vec2(x, y);
36
+ }
37
+ ensureLastPoint() {
38
+ function ensure(points) {
39
+ if (points[0].x !== points.at(-1).x || points[0].y !== points.at(-1).y) {
40
+ points.push(points[0].clone());
41
+ }
42
+ }
43
+ ensure(this.contour);
44
+ for (const hole of this.holes || []) {
45
+ ensure(hole);
46
+ }
47
+ return this;
48
+ }
49
+ boundingBox() {
50
+ let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
51
+ for (const p of this.contour) {
52
+ if (minX > p.x)
53
+ minX = p.x;
54
+ if (maxX < p.x)
55
+ maxX = p.x;
56
+ if (minY > p.y)
57
+ minY = p.y;
58
+ if (maxY < p.y)
59
+ maxY = p.y;
60
+ }
61
+ return { minX, maxX, minY, maxY };
62
+ }
63
+ toBoundingPolygon() {
64
+ const bounding = this.boundingBox();
65
+ return new Polygon([
66
+ new Vec2(bounding.minX, bounding.minY),
67
+ new Vec2(bounding.maxX, bounding.minY),
68
+ new Vec2(bounding.maxX, bounding.maxY),
69
+ new Vec2(bounding.minX, bounding.maxY),
70
+ ]);
71
+ }
72
+ flip() {
73
+ const centerX = this.center().x;
74
+ this.flipSingle(centerX, this.contour);
75
+ this.holes?.forEach(hole => this.flipSingle(centerX, hole));
76
+ return this;
77
+ }
78
+ flipSingle(centerX, poly) {
79
+ for (const point of poly) {
80
+ const xDistanceToCenter = Math.abs(centerX - point.x);
81
+ point.x = point.x < centerX ? centerX + xDistanceToCenter : centerX - xDistanceToCenter;
82
+ }
83
+ }
84
+ }
package/esm/Size2.js ADDED
@@ -0,0 +1,8 @@
1
+ export class Size2 {
2
+ width;
3
+ height;
4
+ constructor(width, height) {
5
+ this.width = width;
6
+ this.height = height;
7
+ }
8
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immugio/three-math-extensions",
3
- "version": "0.0.17",
3
+ "version": "0.1.0",
4
4
  "description": "Set of utilities for 2d and 3d line math built on top of three.js",
5
5
  "author": "Jan Mikeska <janmikeska@gmail.com>",
6
6
  "license": "ISC",
package/src/Polygon.ts ADDED
@@ -0,0 +1,99 @@
1
+ import { Point2 } from "./Point2";
2
+ import { Vec2 } from "./Vec2";
3
+ import { Size2 } from "./Size2";
4
+
5
+ export class Polygon {
6
+
7
+ constructor(public contour: Vec2[], public holes?: Vec2[][]) {
8
+ }
9
+
10
+ public static fromPoints(contour: Point2[], holes?: Point2[][]) {
11
+ return new Polygon(contour.map(p => Vec2.fromPoint(p)), holes?.map(h => h.map(p => Vec2.fromPoint(p))));
12
+ }
13
+
14
+ public get size(): Size2 {
15
+ const {minX, maxX, minY, maxY} = this.boundingBox();
16
+ return new Size2(maxX - minX, maxY - minY);
17
+ }
18
+
19
+ public centerOnOrigin(): Polygon {
20
+ const center = this.center();
21
+
22
+ function centerPoints(points: Vec2[]): void {
23
+ for (const point of points) {
24
+ point.x -= center.x;
25
+ point.y -= center.y;
26
+ }
27
+ }
28
+
29
+ centerPoints(this.contour);
30
+
31
+ for (const hole of this.holes || []) {
32
+ centerPoints(hole);
33
+ }
34
+
35
+ return this;
36
+ }
37
+
38
+ public center(): Vec2 {
39
+ const {minX, maxX, minY, maxY} = this.boundingBox();
40
+
41
+ const x = (maxX + minX) / 2;
42
+ const y = (maxY + minY) / 2;
43
+
44
+ return new Vec2(x, y);
45
+ }
46
+
47
+ public ensureLastPoint(): Polygon {
48
+ function ensure(points: Vec2[]): void {
49
+ if (points[0].x !== points.at(-1).x || points[0].y !== points.at(-1).y) {
50
+ points.push(points[0].clone());
51
+ }
52
+ }
53
+
54
+ ensure(this.contour);
55
+
56
+ for (const hole of this.holes || []) {
57
+ ensure(hole);
58
+ }
59
+
60
+ return this;
61
+ }
62
+
63
+ public boundingBox(): { minX: number, maxX: number, minY: number, maxY: number } {
64
+ let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
65
+
66
+ for (const p of this.contour) {
67
+ if (minX > p.x) minX = p.x;
68
+ if (maxX < p.x) maxX = p.x;
69
+ if (minY > p.y) minY = p.y;
70
+ if (maxY < p.y) maxY = p.y;
71
+ }
72
+
73
+ return {minX, maxX, minY, maxY};
74
+ }
75
+
76
+ public toBoundingPolygon(): Polygon {
77
+ const bounding = this.boundingBox();
78
+ return new Polygon([
79
+ new Vec2(bounding.minX, bounding.minY),
80
+ new Vec2(bounding.maxX, bounding.minY),
81
+ new Vec2(bounding.maxX, bounding.maxY),
82
+ new Vec2(bounding.minX, bounding.maxY),
83
+ ]);
84
+ }
85
+
86
+ public flip(): Polygon {
87
+ const centerX = this.center().x;
88
+ this.flipSingle(centerX, this.contour);
89
+ this.holes?.forEach(hole => this.flipSingle(centerX, hole));
90
+ return this;
91
+ }
92
+
93
+ private flipSingle(centerX: number, poly: Vec2[]) {
94
+ for (const point of poly) {
95
+ const xDistanceToCenter = Math.abs(centerX - point.x);
96
+ point.x = point.x < centerX ? centerX + xDistanceToCenter : centerX - xDistanceToCenter;
97
+ }
98
+ }
99
+ }
package/src/Size2.ts ADDED
@@ -0,0 +1,4 @@
1
+ export class Size2 {
2
+ constructor(public width: number, public height: number) {
3
+ }
4
+ }
@@ -0,0 +1,22 @@
1
+ import { Point2 } from "./Point2";
2
+ import { Vec2 } from "./Vec2";
3
+ import { Size2 } from "./Size2";
4
+ export declare class Polygon {
5
+ contour: Vec2[];
6
+ holes?: Vec2[][];
7
+ constructor(contour: Vec2[], holes?: Vec2[][]);
8
+ static fromPoints(contour: Point2[], holes?: Point2[][]): Polygon;
9
+ get size(): Size2;
10
+ centerOnOrigin(): Polygon;
11
+ center(): Vec2;
12
+ ensureLastPoint(): Polygon;
13
+ boundingBox(): {
14
+ minX: number;
15
+ maxX: number;
16
+ minY: number;
17
+ maxY: number;
18
+ };
19
+ toBoundingPolygon(): Polygon;
20
+ flip(): Polygon;
21
+ private flipSingle;
22
+ }
@@ -0,0 +1,5 @@
1
+ export declare class Size2 {
2
+ width: number;
3
+ height: number;
4
+ constructor(width: number, height: number);
5
+ }