@immugio/three-math-extensions 0.1.0 → 0.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
@@ -7,14 +7,29 @@ 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.1.0](https://github.com/Immugio/three-math-extensions/compare/16.15.10...0.1.0)
10
+ ## [0.2.0](https://github.com/Immugio/three-math-extensions/compare/16.15.10...0.2.0)
11
11
 
12
12
  ### Commits
13
13
 
14
+ - Add Rectangle, update Polygon [`58ee875`](https://github.com/Immugio/three-math-extensions/commit/58ee87539af8f9ade186e5250cba9e01926da514)
15
+ - Vec3, Line3D - update documentation [`67d9c32`](https://github.com/Immugio/three-math-extensions/commit/67d9c328e08cc0a5599932d2f0529e97f31c9213)
14
16
  - Add Polygon [`629cff8`](https://github.com/Immugio/three-math-extensions/commit/629cff8ecbb963477e8ea76d7f8b16d95435cbad)
17
+ - Add Polygon to exports [`ed66775`](https://github.com/Immugio/three-math-extensions/commit/ed66775c33e961835b23843222b822cfd9c16b1d)
15
18
  - Vec2.fromPoint and Vec3.fromPoint should accept null [`4b871af`](https://github.com/Immugio/three-math-extensions/commit/4b871af297bdcbe8584f1e2b99d602247b77687c)
16
19
 
17
- ## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.0.17...16.15.10) - 2023-01-02
20
+ ## [16.15.10](https://github.com/Immugio/three-math-extensions/compare/0.1.1...16.15.10) - 2023-01-02
21
+
22
+ ## [0.1.1](https://github.com/Immugio/three-math-extensions/compare/0.1.0...0.1.1) - 2023-01-02
23
+
24
+ ### Commits
25
+
26
+ - Add Polygon to exports [`ed66775`](https://github.com/Immugio/three-math-extensions/commit/ed66775c33e961835b23843222b822cfd9c16b1d)
27
+
28
+ ## [0.1.0](https://github.com/Immugio/three-math-extensions/compare/0.0.17...0.1.0) - 2023-01-02
29
+
30
+ ### Commits
31
+
32
+ - Add Polygon [`629cff8`](https://github.com/Immugio/three-math-extensions/commit/629cff8ecbb963477e8ea76d7f8b16d95435cbad)
18
33
 
19
34
  ## [0.0.17](https://github.com/Immugio/three-math-extensions/compare/0.0.16...0.0.17) - 2023-01-02
20
35
 
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BoundingBox = void 0;
4
+ const Vec2_1 = require("./Vec2");
5
+ class BoundingBox {
6
+ minX;
7
+ maxX;
8
+ minY;
9
+ maxY;
10
+ constructor(minX, maxX, minY, maxY) {
11
+ this.minX = minX;
12
+ this.maxX = maxX;
13
+ this.minY = minY;
14
+ this.maxY = maxY;
15
+ }
16
+ equals(other) {
17
+ return this.minX === other.minX && this.maxX === other.maxX && this.minY === other.minY && this.maxY === other.maxY;
18
+ }
19
+ get size() {
20
+ return new Vec2_1.Vec2(this.maxX - this.minX, this.maxY - this.minY);
21
+ }
22
+ }
23
+ exports.BoundingBox = BoundingBox;
package/cjs/Polygon.js CHANGED
@@ -2,7 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Polygon = void 0;
4
4
  const Vec2_1 = require("./Vec2");
5
- const Size2_1 = require("./Size2");
5
+ const Rectangle_1 = require("./Rectangle");
6
+ const BoundingBox_1 = require("./BoundingBox");
6
7
  class Polygon {
7
8
  contour;
8
9
  holes;
@@ -15,7 +16,7 @@ class Polygon {
15
16
  }
16
17
  get size() {
17
18
  const { minX, maxX, minY, maxY } = this.boundingBox();
18
- return new Size2_1.Size2(maxX - minX, maxY - minY);
19
+ return new Vec2_1.Vec2(maxX - minX, maxY - minY);
19
20
  }
20
21
  centerOnOrigin() {
21
22
  const center = this.center();
@@ -39,7 +40,7 @@ class Polygon {
39
40
  }
40
41
  ensureLastPoint() {
41
42
  function ensure(points) {
42
- if (points[0].x !== points.at(-1).x || points[0].y !== points.at(-1).y) {
43
+ if (!points[0].equals(points.at(-1))) {
43
44
  points.push(points[0].clone());
44
45
  }
45
46
  }
@@ -61,7 +62,7 @@ class Polygon {
61
62
  if (maxY < p.y)
62
63
  maxY = p.y;
63
64
  }
64
- return { minX, maxX, minY, maxY };
65
+ return new BoundingBox_1.BoundingBox(minX, maxX, minY, maxY);
65
66
  }
66
67
  toBoundingPolygon() {
67
68
  const bounding = this.boundingBox();
@@ -84,5 +85,38 @@ class Polygon {
84
85
  point.x = point.x < centerX ? centerX + xDistanceToCenter : centerX - xDistanceToCenter;
85
86
  }
86
87
  }
88
+ toRectangle() {
89
+ const bounding = this.boundingBox();
90
+ return new Rectangle_1.Rectangle(bounding.minX, bounding.maxX, bounding.minY, bounding.maxY);
91
+ }
92
+ clone() {
93
+ return new Polygon(this.contour.map(p => p.clone()), this.holes?.map(h => h.map(p => p.clone())));
94
+ }
95
+ equals(other) {
96
+ if (this.contour.length !== other.contour.length) {
97
+ return false;
98
+ }
99
+ for (let i = 0; i < this.contour.length; i++) {
100
+ if (!this.contour[i].equals(other.contour[i])) {
101
+ return false;
102
+ }
103
+ }
104
+ if (this.holes?.length !== other.holes?.length) {
105
+ return false;
106
+ }
107
+ for (let i = 0; i < this.holes?.length; i++) {
108
+ const hole = this.holes[i];
109
+ const otherHole = other.holes[i];
110
+ if (hole.length !== otherHole.length) {
111
+ return false;
112
+ }
113
+ for (let j = 0; j < hole.length; j++) {
114
+ if (!hole[j].equals(otherHole[j])) {
115
+ return false;
116
+ }
117
+ }
118
+ }
119
+ return true;
120
+ }
87
121
  }
88
122
  exports.Polygon = Polygon;
@@ -0,0 +1,77 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Rectangle = void 0;
4
+ const Polygon_1 = require("./Polygon");
5
+ const Vec2_1 = require("./Vec2");
6
+ class Rectangle {
7
+ leftX;
8
+ rightX;
9
+ topY;
10
+ bottomY;
11
+ constructor(leftX, rightX, topY, bottomY) {
12
+ this.leftX = leftX;
13
+ this.rightX = rightX;
14
+ this.topY = topY;
15
+ this.bottomY = bottomY;
16
+ }
17
+ clone() {
18
+ return new Rectangle(this.leftX, this.rightX, this.topY, this.bottomY);
19
+ }
20
+ get size() {
21
+ return new Vec2_1.Vec2(this.rightX - this.leftX, this.topY - this.bottomY);
22
+ }
23
+ get center() {
24
+ return new Vec2_1.Vec2((this.leftX + this.rightX) / 2, (this.topY + this.bottomY) / 2);
25
+ }
26
+ get offset() {
27
+ return new Vec2_1.Vec2((this.leftX + this.rightX) / -2, (this.topY + this.bottomY) / -2);
28
+ }
29
+ overlaps(other) {
30
+ // Proof is by contradiction. Any one of four conditions guarantees that NO OVERLAP CAN EXIST.
31
+ const result = this.bottomY >= other.topY || this.topY <= other.bottomY || this.rightX <= other.leftX || this.leftX >= other.rightX;
32
+ return !result;
33
+ }
34
+ get hasSize() {
35
+ return this.rightX - this.leftX > 1 && this.topY - this.bottomY > 1;
36
+ }
37
+ translate(diff) {
38
+ this.leftX += diff.x;
39
+ this.rightX += diff.x;
40
+ this.topY += diff.y;
41
+ this.bottomY += diff.y;
42
+ return this;
43
+ }
44
+ centerOnOrigin() {
45
+ const center = this.center;
46
+ this.leftX -= center.x;
47
+ this.rightX -= center.x;
48
+ this.topY -= center.y;
49
+ this.bottomY -= center.y;
50
+ return this;
51
+ }
52
+ toPolygon() {
53
+ return new Polygon_1.Polygon(this.toPoints());
54
+ }
55
+ /**
56
+ * The polygon is always constructed as "clockwise", assuming X axis to the right and Y axis down.
57
+ */
58
+ toPoints() {
59
+ return [
60
+ new Vec2_1.Vec2(this.leftX, Math.min(this.topY, this.bottomY)),
61
+ new Vec2_1.Vec2(this.rightX, Math.min(this.topY, this.bottomY)),
62
+ new Vec2_1.Vec2(this.rightX, Math.max(this.topY, this.bottomY)),
63
+ new Vec2_1.Vec2(this.leftX, Math.max(this.topY, this.bottomY)),
64
+ ];
65
+ }
66
+ flipVertical(xCenter) {
67
+ const left = xCenter - (this.rightX - xCenter);
68
+ const right = xCenter - (this.leftX - xCenter);
69
+ this.leftX = left;
70
+ this.rightX = right;
71
+ return this;
72
+ }
73
+ equals(other) {
74
+ return !!other && this.leftX === other.leftX && this.rightX === other.rightX && this.topY === other.topY && this.bottomY === other.bottomY;
75
+ }
76
+ }
77
+ exports.Rectangle = Rectangle;
package/cjs/Vec3.js CHANGED
@@ -4,7 +4,7 @@ exports.Vec3 = void 0;
4
4
  const three_1 = require("three");
5
5
  const Vec2_1 = require("./Vec2");
6
6
  /**
7
- * Vec3 represents a 2D vector. It extends `Vector3` from the `threejs` library.
7
+ * Vec3 represents a 3D vector. It extends `Vector3` from the `threejs` library.
8
8
  */
9
9
  class Vec3 extends three_1.Vector3 {
10
10
  #target;
package/cjs/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Line3D = exports.Line2D = exports.Vec3 = exports.Vec2 = void 0;
3
+ exports.Rectangle = exports.BoundingBox = exports.Polygon = exports.Size2 = exports.Line3D = exports.Line2D = exports.Vec3 = exports.Vec2 = void 0;
4
4
  var Vec2_1 = require("./Vec2");
5
5
  Object.defineProperty(exports, "Vec2", { enumerable: true, get: function () { return Vec2_1.Vec2; } });
6
6
  var Vec3_1 = require("./Vec3");
@@ -9,3 +9,11 @@ var Line2D_1 = require("./Line2D");
9
9
  Object.defineProperty(exports, "Line2D", { enumerable: true, get: function () { return Line2D_1.Line2D; } });
10
10
  var Line3D_1 = require("./Line3D");
11
11
  Object.defineProperty(exports, "Line3D", { enumerable: true, get: function () { return Line3D_1.Line3D; } });
12
+ var Size2_1 = require("./Size2");
13
+ Object.defineProperty(exports, "Size2", { enumerable: true, get: function () { return Size2_1.Size2; } });
14
+ var Polygon_1 = require("./Polygon");
15
+ Object.defineProperty(exports, "Polygon", { enumerable: true, get: function () { return Polygon_1.Polygon; } });
16
+ var BoundingBox_1 = require("./BoundingBox");
17
+ Object.defineProperty(exports, "BoundingBox", { enumerable: true, get: function () { return BoundingBox_1.BoundingBox; } });
18
+ var Rectangle_1 = require("./Rectangle");
19
+ Object.defineProperty(exports, "Rectangle", { enumerable: true, get: function () { return Rectangle_1.Rectangle; } });
@@ -0,0 +1,117 @@
1
+ [@immugio/three-math-extensions](../README.md) / [Exports](../modules.md) / BoundingBox
2
+
3
+ # Class: BoundingBox
4
+
5
+ ## Table of contents
6
+
7
+ ### Constructors
8
+
9
+ - [constructor](BoundingBox.md#constructor)
10
+
11
+ ### Properties
12
+
13
+ - [maxX](BoundingBox.md#maxx)
14
+ - [maxY](BoundingBox.md#maxy)
15
+ - [minX](BoundingBox.md#minx)
16
+ - [minY](BoundingBox.md#miny)
17
+
18
+ ### Accessors
19
+
20
+ - [size](BoundingBox.md#size)
21
+
22
+ ### Methods
23
+
24
+ - [equals](BoundingBox.md#equals)
25
+
26
+ ## Constructors
27
+
28
+ ### constructor
29
+
30
+ • **new BoundingBox**(`minX`, `maxX`, `minY`, `maxY`)
31
+
32
+ #### Parameters
33
+
34
+ | Name | Type |
35
+ | :------ | :------ |
36
+ | `minX` | `number` |
37
+ | `maxX` | `number` |
38
+ | `minY` | `number` |
39
+ | `maxY` | `number` |
40
+
41
+ #### Defined in
42
+
43
+ [src/BoundingBox.ts:4](https://github.com/Immugio/three-math-extensions/blob/c004965/src/BoundingBox.ts#L4)
44
+
45
+ ## Properties
46
+
47
+ ### maxX
48
+
49
+ • **maxX**: `number`
50
+
51
+ #### Defined in
52
+
53
+ [src/BoundingBox.ts:4](https://github.com/Immugio/three-math-extensions/blob/c004965/src/BoundingBox.ts#L4)
54
+
55
+ ___
56
+
57
+ ### maxY
58
+
59
+ • **maxY**: `number`
60
+
61
+ #### Defined in
62
+
63
+ [src/BoundingBox.ts:4](https://github.com/Immugio/three-math-extensions/blob/c004965/src/BoundingBox.ts#L4)
64
+
65
+ ___
66
+
67
+ ### minX
68
+
69
+ • **minX**: `number`
70
+
71
+ #### Defined in
72
+
73
+ [src/BoundingBox.ts:4](https://github.com/Immugio/three-math-extensions/blob/c004965/src/BoundingBox.ts#L4)
74
+
75
+ ___
76
+
77
+ ### minY
78
+
79
+ • **minY**: `number`
80
+
81
+ #### Defined in
82
+
83
+ [src/BoundingBox.ts:4](https://github.com/Immugio/three-math-extensions/blob/c004965/src/BoundingBox.ts#L4)
84
+
85
+ ## Accessors
86
+
87
+ ### size
88
+
89
+ • `get` **size**(): [`Vec2`](Vec2.md)
90
+
91
+ #### Returns
92
+
93
+ [`Vec2`](Vec2.md)
94
+
95
+ #### Defined in
96
+
97
+ [src/BoundingBox.ts:11](https://github.com/Immugio/three-math-extensions/blob/c004965/src/BoundingBox.ts#L11)
98
+
99
+ ## Methods
100
+
101
+ ### equals
102
+
103
+ ▸ **equals**(`other`): `boolean`
104
+
105
+ #### Parameters
106
+
107
+ | Name | Type |
108
+ | :------ | :------ |
109
+ | `other` | [`BoundingBox`](BoundingBox.md) |
110
+
111
+ #### Returns
112
+
113
+ `boolean`
114
+
115
+ #### Defined in
116
+
117
+ [src/BoundingBox.ts:7](https://github.com/Immugio/three-math-extensions/blob/c004965/src/BoundingBox.ts#L7)