@immugio/three-math-extensions 0.0.13 → 0.0.14

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.
@@ -0,0 +1,167 @@
1
+ [@immugio/three-math-extensions](../README.md) / [Exports](../modules.md) / Vec2
2
+
3
+ # Class: Vec2
4
+
5
+ Vec2 represents a 2D vector. It extends `Vector2` from the `threejs` library.
6
+
7
+ ## Hierarchy
8
+
9
+ - `Vector2`
10
+
11
+ ↳ **`Vec2`**
12
+
13
+ ## Table of contents
14
+
15
+ ### Constructors
16
+
17
+ - [constructor](Vec2.md#constructor)
18
+
19
+ ### Methods
20
+
21
+ - [in3DSpace](Vec2.md#in3dspace)
22
+ - [isNear](Vec2.md#isnear)
23
+ - [moveTowards](Vec2.md#movetowards)
24
+ - [roundIfCloseToInteger](Vec2.md#roundifclosetointeger)
25
+ - [fromPoint](Vec2.md#frompoint)
26
+
27
+ ## Constructors
28
+
29
+ ### constructor
30
+
31
+ • **new Vec2**(`x?`, `y?`)
32
+
33
+ #### Parameters
34
+
35
+ | Name | Type |
36
+ | :------ | :------ |
37
+ | `x?` | `number` |
38
+ | `y?` | `number` |
39
+
40
+ #### Inherited from
41
+
42
+ Vector2.constructor
43
+
44
+ #### Defined in
45
+
46
+ node_modules/@types/three/src/math/Vector2.d.ts:140
47
+
48
+ ## Methods
49
+
50
+ ### in3DSpace
51
+
52
+ ▸ **in3DSpace**(`y?`): [`Vec3`](Vec3.md)
53
+
54
+ Projects this Vec2 instance to a Vec3 instance in 3D space. Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
55
+
56
+ #### Parameters
57
+
58
+ | Name | Type | Default value | Description |
59
+ | :------ | :------ | :------ | :------ |
60
+ | `y` | `number` | `0` | The y value of the new Vec3 instance. |
61
+
62
+ #### Returns
63
+
64
+ [`Vec3`](Vec3.md)
65
+
66
+ A new Vec3 instance.
67
+
68
+ #### Defined in
69
+
70
+ [src/Vec2.ts:51](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec2.ts#L51)
71
+
72
+ ___
73
+
74
+ ### isNear
75
+
76
+ ▸ **isNear**(`v`, `maxDistance?`): `boolean`
77
+
78
+ Determines if this Vec2 instance is near the target Vec2.
79
+ maxDistance is the maximum distance between the two vectors within which they are considered `near`.
80
+
81
+ #### Parameters
82
+
83
+ | Name | Type | Default value |
84
+ | :------ | :------ | :------ |
85
+ | `v` | `Vector2` | `undefined` |
86
+ | `maxDistance` | `number` | `undefined` |
87
+
88
+ #### Returns
89
+
90
+ `boolean`
91
+
92
+ #### Defined in
93
+
94
+ [src/Vec2.ts:59](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec2.ts#L59)
95
+
96
+ ___
97
+
98
+ ### moveTowards
99
+
100
+ ▸ **moveTowards**(`target`, `amount`): [`Vec2`](Vec2.md)
101
+
102
+ Moves this Vec2 instance towards the target Vec2 by the given amount.
103
+
104
+ #### Parameters
105
+
106
+ | Name | Type | Description |
107
+ | :------ | :------ | :------ |
108
+ | `target` | `Vector2` | The target Vec2. |
109
+ | `amount` | `number` | The distance to move. |
110
+
111
+ #### Returns
112
+
113
+ [`Vec2`](Vec2.md)
114
+
115
+ This Vec2 instance.
116
+
117
+ #### Defined in
118
+
119
+ [src/Vec2.ts:25](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec2.ts#L25)
120
+
121
+ ___
122
+
123
+ ### roundIfCloseToInteger
124
+
125
+ ▸ **roundIfCloseToInteger**(`max?`): [`Vec2`](Vec2.md)
126
+
127
+ Rounds the x and y values of this Vec2 instance if they are close to an integer value.
128
+
129
+ #### Parameters
130
+
131
+ | Name | Type | Default value | Description |
132
+ | :------ | :------ | :------ | :------ |
133
+ | `max` | `number` | `0.000000000001` | The maximum difference between the value and the nearest integer. |
134
+
135
+ #### Returns
136
+
137
+ [`Vec2`](Vec2.md)
138
+
139
+ This Vec2 instance.
140
+
141
+ #### Defined in
142
+
143
+ [src/Vec2.ts:36](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec2.ts#L36)
144
+
145
+ ___
146
+
147
+ ### fromPoint
148
+
149
+ ▸ `Static` **fromPoint**(`point`): [`Vec2`](Vec2.md)
150
+
151
+ Creates a new Vec2 instance from an {x, y} object.
152
+
153
+ #### Parameters
154
+
155
+ | Name | Type | Description |
156
+ | :------ | :------ | :------ |
157
+ | `point` | `Point2` | The {x, y} instance. |
158
+
159
+ #### Returns
160
+
161
+ [`Vec2`](Vec2.md)
162
+
163
+ A new Vec2 instance.
164
+
165
+ #### Defined in
166
+
167
+ [src/Vec2.ts:15](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec2.ts#L15)
@@ -0,0 +1,288 @@
1
+ [@immugio/three-math-extensions](../README.md) / [Exports](../modules.md) / Vec3
2
+
3
+ # Class: Vec3
4
+
5
+ Vec3 represents a 2D vector. It extends `Vector3` from the `threejs` library.
6
+
7
+ ## Hierarchy
8
+
9
+ - `Vector3`
10
+
11
+ ↳ **`Vec3`**
12
+
13
+ ## Table of contents
14
+
15
+ ### Constructors
16
+
17
+ - [constructor](Vec3.md#constructor)
18
+
19
+ ### Methods
20
+
21
+ - [addX](Vec3.md#addx)
22
+ - [addY](Vec3.md#addy)
23
+ - [clone](Vec3.md#clone)
24
+ - [closest](Vec3.md#closest)
25
+ - [horizontalDistanceTo](Vec3.md#horizontaldistanceto)
26
+ - [isNear](Vec3.md#isnear)
27
+ - [moveHalfWayTowards](Vec3.md#movehalfwaytowards)
28
+ - [moveTowards](Vec3.md#movetowards)
29
+ - [onPlan](Vec3.md#onplan)
30
+ - [toPointWithFlippedYZ](Vec3.md#topointwithflippedyz)
31
+ - [fromPoint](Vec3.md#frompoint)
32
+
33
+ ## Constructors
34
+
35
+ ### constructor
36
+
37
+ • **new Vec3**(`x?`, `y?`, `z?`)
38
+
39
+ #### Parameters
40
+
41
+ | Name | Type |
42
+ | :------ | :------ |
43
+ | `x?` | `number` |
44
+ | `y?` | `number` |
45
+ | `z?` | `number` |
46
+
47
+ #### Inherited from
48
+
49
+ Vector3.constructor
50
+
51
+ #### Defined in
52
+
53
+ node_modules/@types/three/src/math/Vector3.d.ts:26
54
+
55
+ ## Methods
56
+
57
+ ### addX
58
+
59
+ ▸ **addX**(`x`): [`Vec3`](Vec3.md)
60
+
61
+ Adds x amount to this Vec3 instance and return this
62
+
63
+ #### Parameters
64
+
65
+ | Name | Type |
66
+ | :------ | :------ |
67
+ | `x` | `number` |
68
+
69
+ #### Returns
70
+
71
+ [`Vec3`](Vec3.md)
72
+
73
+ #### Defined in
74
+
75
+ [src/Vec3.ts:65](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L65)
76
+
77
+ ___
78
+
79
+ ### addY
80
+
81
+ ▸ **addY**(`y`): [`Vec3`](Vec3.md)
82
+
83
+ Adds y amount to this Vec3 instance and return this
84
+
85
+ #### Parameters
86
+
87
+ | Name | Type |
88
+ | :------ | :------ |
89
+ | `y` | `number` |
90
+
91
+ #### Returns
92
+
93
+ [`Vec3`](Vec3.md)
94
+
95
+ #### Defined in
96
+
97
+ [src/Vec3.ts:56](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L56)
98
+
99
+ ___
100
+
101
+ ### clone
102
+
103
+ ▸ **clone**(): [`Vec3`](Vec3.md)
104
+
105
+ #### Returns
106
+
107
+ [`Vec3`](Vec3.md)
108
+
109
+ #### Overrides
110
+
111
+ Vector3.clone
112
+
113
+ #### Defined in
114
+
115
+ [src/Vec3.ts:114](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L114)
116
+
117
+ ___
118
+
119
+ ### closest
120
+
121
+ ▸ **closest**(`...points`): [`Vec3`](Vec3.md)
122
+
123
+ Returns a clone of the point closest to this from the given points.
124
+
125
+ #### Parameters
126
+
127
+ | Name | Type |
128
+ | :------ | :------ |
129
+ | `...points` | `Vector3`[] |
130
+
131
+ #### Returns
132
+
133
+ [`Vec3`](Vec3.md)
134
+
135
+ #### Defined in
136
+
137
+ [src/Vec3.ts:74](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L74)
138
+
139
+ ___
140
+
141
+ ### horizontalDistanceTo
142
+
143
+ ▸ **horizontalDistanceTo**(`point`): `number`
144
+
145
+ Get distance to another vector while ignoring the y-axis.
146
+
147
+ #### Parameters
148
+
149
+ | Name | Type |
150
+ | :------ | :------ |
151
+ | `point` | `Vector3` |
152
+
153
+ #### Returns
154
+
155
+ `number`
156
+
157
+ #### Defined in
158
+
159
+ [src/Vec3.ts:98](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L98)
160
+
161
+ ___
162
+
163
+ ### isNear
164
+
165
+ ▸ **isNear**(`v`, `maxDistance?`): `boolean`
166
+
167
+ Determines if this Vec2 instance is near the target Vec2.
168
+ maxDistance is the maximum distance between the two vectors within which they are considered `near`.
169
+
170
+ #### Parameters
171
+
172
+ | Name | Type | Default value |
173
+ | :------ | :------ | :------ |
174
+ | `v` | `Vector3` | `undefined` |
175
+ | `maxDistance` | `number` | `undefined` |
176
+
177
+ #### Returns
178
+
179
+ `boolean`
180
+
181
+ #### Defined in
182
+
183
+ [src/Vec3.ts:106](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L106)
184
+
185
+ ___
186
+
187
+ ### moveHalfWayTowards
188
+
189
+ ▸ **moveHalfWayTowards**(`target`): [`Vec3`](Vec3.md)
190
+
191
+ Moves this Vec3 instance halfway towards the target Vec3 by the given amount.
192
+
193
+ #### Parameters
194
+
195
+ | Name | Type | Description |
196
+ | :------ | :------ | :------ |
197
+ | `target` | `Vector3` | The target Vec3. |
198
+
199
+ #### Returns
200
+
201
+ [`Vec3`](Vec3.md)
202
+
203
+ This Vec3 instance.
204
+
205
+ #### Defined in
206
+
207
+ [src/Vec3.ts:43](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L43)
208
+
209
+ ___
210
+
211
+ ### moveTowards
212
+
213
+ ▸ **moveTowards**(`target`, `amount`): [`Vec3`](Vec3.md)
214
+
215
+ Moves this Vec3 instance towards the target Vec3 by the given amount.
216
+
217
+ #### Parameters
218
+
219
+ | Name | Type | Description |
220
+ | :------ | :------ | :------ |
221
+ | `target` | `Vector3` | The target Vec3. |
222
+ | `amount` | `number` | The distance to move. |
223
+
224
+ #### Returns
225
+
226
+ [`Vec3`](Vec3.md)
227
+
228
+ This Vec3 instance.
229
+
230
+ #### Defined in
231
+
232
+ [src/Vec3.ts:27](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L27)
233
+
234
+ ___
235
+
236
+ ### onPlan
237
+
238
+ ▸ **onPlan**(): [`Vec2`](Vec2.md)
239
+
240
+ Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ignored.
241
+
242
+ #### Returns
243
+
244
+ [`Vec2`](Vec2.md)
245
+
246
+ #### Defined in
247
+
248
+ [src/Vec3.ts:90](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L90)
249
+
250
+ ___
251
+
252
+ ### toPointWithFlippedYZ
253
+
254
+ ▸ **toPointWithFlippedYZ**(): [`Vec3`](Vec3.md)
255
+
256
+ Returns a clone of this Vec3 instance with y and z swapped.
257
+
258
+ #### Returns
259
+
260
+ [`Vec3`](Vec3.md)
261
+
262
+ #### Defined in
263
+
264
+ [src/Vec3.ts:83](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L83)
265
+
266
+ ___
267
+
268
+ ### fromPoint
269
+
270
+ ▸ `Static` **fromPoint**(`point`): [`Vec3`](Vec3.md)
271
+
272
+ Creates a new Vec3 instance from an {x, y, z} object.
273
+
274
+ #### Parameters
275
+
276
+ | Name | Type | Description |
277
+ | :------ | :------ | :------ |
278
+ | `point` | `Point3` | The {x, y, z} instance. |
279
+
280
+ #### Returns
281
+
282
+ [`Vec3`](Vec3.md)
283
+
284
+ A new Vec3 instance.
285
+
286
+ #### Defined in
287
+
288
+ [src/Vec3.ts:17](https://github.com/Immugio/three-math-extensions/blob/70c8d5e/src/Vec3.ts#L17)
@@ -0,0 +1,12 @@
1
+ [@immugio/three-math-extensions](README.md) / Exports
2
+
3
+ # @immugio/three-math-extensions
4
+
5
+ ## Table of contents
6
+
7
+ ### Classes
8
+
9
+ - [Line2D](classes/Line2D.md)
10
+ - [Line3D](classes/Line3D.md)
11
+ - [Vec2](classes/Vec2.md)
12
+ - [Vec3](classes/Vec3.md)
package/esm/Vec2.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Vector2 } from "three";
2
2
  import { Vec3 } from "./Vec3";
3
3
  /**
4
- * Vec2 represents a 2D vector. It extends `Vector2` from the `three` library.
4
+ * Vec2 represents a 2D vector. It extends `Vector2` from the `threejs` library.
5
5
  */
6
6
  export class Vec2 extends Vector2 {
7
7
  /**
@@ -38,12 +38,12 @@ export class Vec2 extends Vector2 {
38
38
  return this;
39
39
  }
40
40
  /**
41
- * Projects this Vec2 instance to a Vec3 instance in 3D space.
42
- * @param z - The z value of the new Vec3 instance.
41
+ * Projects this Vec2 instance to a Vec3 instance in 3D space. Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
42
+ * @param y - The y value of the new Vec3 instance.
43
43
  * @returns A new Vec3 instance.
44
44
  */
45
- in3DSpace(z = 0) {
46
- return new Vec3(this.x, z, this.y);
45
+ in3DSpace(y = 0) {
46
+ return new Vec3(this.x, y, this.y);
47
47
  }
48
48
  /**
49
49
  * Determines if this Vec2 instance is near the target Vec2.
package/esm/Vec3.js CHANGED
@@ -1,10 +1,24 @@
1
1
  import { Vector3 } from "three";
2
2
  import { Vec2 } from "./Vec2";
3
+ /**
4
+ * Vec3 represents a 2D vector. It extends `Vector3` from the `threejs` library.
5
+ */
3
6
  export class Vec3 extends Vector3 {
4
7
  #target;
8
+ /**
9
+ * Creates a new Vec3 instance from an {x, y, z} object.
10
+ * @param point - The {x, y, z} instance.
11
+ * @returns A new Vec3 instance.
12
+ */
5
13
  static fromPoint(point) {
6
14
  return new Vec3(point.x, point.y, point.z);
7
15
  }
16
+ /**
17
+ * Moves this Vec3 instance towards the target Vec3 by the given amount.
18
+ * @param target - The target Vec3.
19
+ * @param amount - The distance to move.
20
+ * @returns This Vec3 instance.
21
+ */
8
22
  moveTowards(target, amount) {
9
23
  if (this.#target === undefined) {
10
24
  this.#target = new Vector3();
@@ -13,40 +27,65 @@ export class Vec3 extends Vector3 {
13
27
  this.add(this.#target.sub(this).normalize().multiplyScalar(amount));
14
28
  return this;
15
29
  }
16
- centerTowards(target) {
30
+ /**
31
+ * Moves this Vec3 instance halfway towards the target Vec3 by the given amount.
32
+ * @param target - The target Vec3.
33
+ * @returns This Vec3 instance.
34
+ */
35
+ moveHalfWayTowards(target) {
17
36
  if (this.#target === undefined) {
18
37
  this.#target = new Vector3();
19
38
  }
20
39
  return this.moveTowards(target, this.distanceTo(target) / 2);
21
40
  }
41
+ /**
42
+ * Adds y amount to this Vec3 instance and return this
43
+ * @param y
44
+ */
22
45
  addY(y) {
23
46
  this.y += y;
24
47
  return this;
25
48
  }
49
+ /**
50
+ * Adds x amount to this Vec3 instance and return this
51
+ * @param x
52
+ */
26
53
  addX(x) {
27
54
  this.x += x;
28
55
  return this;
29
56
  }
30
- scale(p) {
31
- this.x *= p.x;
32
- this.y *= p.y;
33
- this.z *= p.z;
34
- return this;
35
- }
57
+ /**
58
+ * Returns a clone of the point closest to this from the given points.
59
+ * @param points
60
+ */
36
61
  closest(...points) {
37
62
  const withDistances = points.map(p => ({ point: p, distance: this.distanceTo(p) }));
38
63
  const closest = withDistances.reduce((a, b) => a.distance < b.distance ? a : b);
39
64
  return Vec3.fromPoint(closest.point);
40
65
  }
66
+ /**
67
+ * Returns a clone of this Vec3 instance with y and z swapped.
68
+ */
41
69
  toPointWithFlippedYZ() {
42
70
  return new Vec3(this.x, this.z, this.y);
43
71
  }
72
+ /**
73
+ * Projects this Vec3 instance onto 2d plan. Vec3.z becomes Vec2.y and Vec3.y is ignored.
74
+ */
44
75
  onPlan() {
45
76
  return new Vec2(this.x, this.z);
46
77
  }
78
+ /**
79
+ * Get distance to another vector while ignoring the y-axis.
80
+ * @param point
81
+ */
47
82
  horizontalDistanceTo(point) {
48
83
  return new Vector3(this.x, 0, this.z).distanceTo(new Vector3(point.x, 0, point.z));
49
84
  }
85
+ /**
86
+ * Determines if this Vec2 instance is near the target Vec2.
87
+ * maxDistance is the maximum distance between the two vectors within which they are considered `near`.
88
+ */
50
89
  isNear(v, maxDistance = undefined) {
51
90
  if (maxDistance === undefined) {
52
91
  return this.equals(v);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@immugio/three-math-extensions",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
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",
@@ -40,6 +40,8 @@
40
40
  "jest": "^29.2.2",
41
41
  "rimraf": "^3.0.2",
42
42
  "ts-jest": "^29.0.0",
43
+ "typedoc": "^0.23.23",
44
+ "typedoc-plugin-markdown": "^3.14.0",
43
45
  "typescript": "4.7.4"
44
46
  },
45
47
  "peerDependencies": {
package/src/Vec2.ts CHANGED
@@ -3,7 +3,7 @@ import { Vec3 } from "./Vec3";
3
3
  import { Point2 } from "./Point2";
4
4
 
5
5
  /**
6
- * Vec2 represents a 2D vector. It extends `Vector2` from the `three` library.
6
+ * Vec2 represents a 2D vector. It extends `Vector2` from the `threejs` library.
7
7
  */
8
8
  export class Vec2 extends Vector2 {
9
9
 
@@ -44,12 +44,12 @@ export class Vec2 extends Vector2 {
44
44
  }
45
45
 
46
46
  /**
47
- * Projects this Vec2 instance to a Vec3 instance in 3D space.
48
- * @param z - The z value of the new Vec3 instance.
47
+ * Projects this Vec2 instance to a Vec3 instance in 3D space. Vec2.y becomes Vec3.z. and Vec3.y is provided as an argument.
48
+ * @param y - The y value of the new Vec3 instance.
49
49
  * @returns A new Vec3 instance.
50
50
  */
51
- public in3DSpace(z: number = 0): Vec3 {
52
- return new Vec3(this.x, z, this.y);
51
+ public in3DSpace(y: number = 0): Vec3 {
52
+ return new Vec3(this.x, y, this.y);
53
53
  }
54
54
 
55
55
  /**