@immugio/three-math-extensions 0.0.4 → 0.0.6

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.
@@ -1,350 +0,0 @@
1
- import { Line3D } from "../Line3D";
2
- import { Vec3 } from "../Vec3";
3
-
4
- const defaultLine = () => new Line3D(new Vec3(-10, 0, 0), new Vec3(10, 0, 0));
5
-
6
- describe("Line3d", () => {
7
- it("should create a line with expected values", () => {
8
- const line = new Line3D(new Vec3(1, 2, 3), new Vec3(4, 5, 6));
9
- expect(line.start).toEqual(new Vec3(1, 2, 3));
10
- expect(line.end).toEqual(new Vec3(4, 5, 6));
11
- });
12
-
13
- it("should create a single line polygon from a 2 points polygon", () => {
14
- const start = new Vec3(1, 2, 20);
15
- const end = new Vec3(3, 4, 20);
16
- const lines = Line3D.fromPolygon([start, end]);
17
- expect(lines.length).toEqual(1);
18
- expect(lines[0]).toEqual(new Line3D(start, end));
19
- });
20
-
21
- it("should create a 2 lines polygon from a 3 points polygon", () => {
22
- const p1 = new Vec3(1, 2, 20);
23
- const p2 = new Vec3(3, 4, 20);
24
- const p3 = new Vec3(5, 2, 20);
25
- const lines = Line3D.fromPolygon([p1, p2, p3]);
26
- expect(lines.length).toEqual(2);
27
- expect(lines[0]).toEqual(new Line3D(p1, p2));
28
- expect(lines[1]).toEqual(new Line3D(p2, p3));
29
- });
30
-
31
- it("should create a closed 3 lines polygon from a 3 points polygon", () => {
32
- const p1 = new Vec3(1, 2, 20);
33
- const p2 = new Vec3(3, 4, 20);
34
- const p3 = new Vec3(5, 2, 20);
35
- const lines = Line3D.fromPolygon([p1, p2, p3], true);
36
- expect(lines.length).toEqual(3);
37
- expect(lines[0]).toEqual(new Line3D(p1, p2));
38
- expect(lines[1]).toEqual(new Line3D(p2, p3));
39
- expect(lines[2]).toEqual(new Line3D(p3, p1));
40
- });
41
-
42
- it("should return the expected center", () => {
43
- const line = defaultLine();
44
- expect(line.center).toEqual(new Vec3(0, 0, 0));
45
- });
46
-
47
- it("should resize should resize the line by the given length", () => {
48
- const line = defaultLine();
49
- const originalCenter = line.center;
50
- expect(line.length).toEqual(20);
51
-
52
- const resizeDistance = 2;
53
- line.resize(resizeDistance);
54
-
55
- expect(line.length).toEqual(22);
56
- expect(line.center).toEqual(originalCenter);
57
- expect(line.start.x).toEqual(-11);
58
- expect(line.end.x).toEqual(11);
59
- });
60
-
61
- it("should setLength to required", () => {
62
- const line = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
63
- const originalCenter = line.center;
64
- expect(line.length).toEqual(10);
65
-
66
- const newSize = 2;
67
- line.setLength(newSize);
68
-
69
- expect(line.length).toEqual(newSize);
70
- expect(line.center).toEqual(originalCenter);
71
- expect(line.start.x).toEqual(4);
72
- expect(line.end.x).toEqual(6);
73
- });
74
-
75
- it("should project the line on another", () => {
76
- // Arrange
77
- const other = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
78
- const line = new Line3D(new Vec3(-1, 1, 0), new Vec3(10, 1, 0));
79
-
80
- // Act
81
- const projected = line.projectOn(other, false);
82
-
83
- // Assert
84
- expect(projected.start).toEqual(new Vec3(-1, 0, 0));
85
- expect(projected.end).toEqual(new Vec3(10, 0, 0));
86
- });
87
-
88
- it("should project and clamp the line on another", () => {
89
- // Arrange
90
- const other = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
91
- const line = new Line3D(new Vec3(-1, 1, 0), new Vec3(10, 1, 0));
92
-
93
- // Act
94
- const projected = line.projectOn(other, true);
95
-
96
- // Assert
97
- expect(projected.start).toEqual(new Vec3(0, 0, 0));
98
- expect(projected.end).toEqual(new Vec3(10, 0, 0));
99
- });
100
-
101
- it("should contain point", () => {
102
- // Arrange
103
- const line = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
104
-
105
- // Act
106
- const contains = line.containsPoint(new Vec3(5, 0, 0));
107
-
108
- // Assert
109
- expect(contains).toBeTruthy();
110
- });
111
-
112
- it("should not contain point", () => {
113
- // Arrange
114
- const line = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
115
-
116
- // Act
117
- const contains = line.containsPoint(new Vec3(5, 1, 0));
118
-
119
- // Assert
120
- expect(contains).toBeFalsy();
121
- });
122
-
123
- it.each([
124
- ["lines are the same", new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), true],
125
- ["overlap from right", new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), new Line3D(new Vec3(5, 0, 0), new Vec3(11, 0, 0)), true],
126
- ["overlap from left", new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), new Line3D(new Vec3(-5, 0, 0), new Vec3(5, 0, 0)), true],
127
- ["second inside the first", new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), new Line3D(new Vec3(5, 0, 0), new Vec3(6, 0, 0)), true],
128
- ["fist inside the second", new Line3D(new Vec3(5, 0, 0), new Vec3(6, 0, 0)), new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), true],
129
- ["no overlap", new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), new Line3D(new Vec3(11, 0, 0), new Vec3(12, 0, 0)), false],
130
- ["no overlap, but touching ends", new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), new Line3D(new Vec3(10, 0, 0), new Vec3(12, 0, 0)), false],
131
- ["not parallel", new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), new Line3D(new Vec3(5, 0, 0), new Vec3(10, 1, 0)), false],
132
- ])("should detect overlap of two lines - %s", (reason, line1, line2, expected) => {
133
- // Act
134
- const result = line1.overlaps(line2);
135
-
136
- // Assert
137
- expect(result).toEqual(expected);
138
- });
139
-
140
- it("should trim the line and return two offcuts, clip well within source", () => {
141
- // Arrange
142
- const source = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
143
- const clip = new Line3D(new Vec3(3, 0, 0), new Vec3(7, 0, 0));
144
-
145
- // Act
146
- const result = source.clipLine(clip);
147
- const groupResult = source.clipLines([clip]);
148
-
149
- // Assert
150
- expect(result.length).toEqual(2);
151
-
152
- expect(result[0].start).toEqual(new Vec3(0, 0, 0));
153
- expect(result[0].end).toEqual(new Vec3(3, 0, 0));
154
-
155
- expect(result[1].start).toEqual(new Vec3(7, 0, 0));
156
- expect(result[1].end).toEqual(new Vec3(10, 0, 0));
157
-
158
- expect(groupResult.sort((a, b) => a.start.x - b.start.x)).toEqual(result.sort((a, b) => a.start.x - b.start.x));
159
- });
160
-
161
- it("should trim the lines even if the clips are overlapping and return two offcuts", () => {
162
- // Arrange
163
- const source = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
164
- const clips = [
165
- new Line3D(new Vec3(-3, 0, 0), new Vec3(2, 0, 0)),
166
- new Line3D(new Vec3(3, 0, 0), new Vec3(6, 0, 0)),
167
- new Line3D(new Vec3(4, 0, 0), new Vec3(7, 0, 0)),
168
- ];
169
-
170
- // Act
171
- const result = source.clipLines(clips);
172
-
173
- // Assert
174
- result.sort((a, b) => a.start.x - b.start.x); // Order from the clipping is not guaranteed
175
- expect(result).toEqual([
176
- new Line3D(new Vec3(2, 0, 0), new Vec3(3, 0, 0)),
177
- new Line3D(new Vec3(7, 0, 0), new Vec3(10, 0, 0)),
178
- ]);
179
- });
180
-
181
- it.each([
182
- { tolerance: 0 },
183
- { tolerance: 1 },
184
- ])("should trim the line and return two offcuts even if the source and trim run in opposite direction and are not perfectly parallel when sufficient tolerance is provided", ({ tolerance }) => {
185
- // Arrange
186
- const source = new Line3D(new Vec3(
187
- 1907.0952296605503,
188
- 0,
189
- 5258.129694575135
190
- ), new Vec3(
191
- 1907.0952296605506,
192
- 0,
193
- 4302.1493774205865
194
- ));
195
- const clip = new Line3D(new Vec3(
196
- 1907.0952296605503,
197
- 0,
198
- 4762.083899346474
199
- ), new Vec3(
200
- 1907.0952296605503,
201
- 0,
202
- 4954.083899346474
203
- ));
204
-
205
- // Act
206
- const result = source.clipLine(clip, tolerance);
207
-
208
- // Assert
209
- if (tolerance === 0) {
210
- expect(result).toEqual([source]);
211
- } else {
212
- expect(result).toEqual([
213
- new Line3D(new Vec3(1907.0952296605503, 0, 5258.129694575135), new Vec3(1907.0952296605503, 0, 4954.083899346474)),
214
- new Line3D(new Vec3(1907.0952296605503, 0, 4762.083899346474), new Vec3(1907.0952296605506, 0, 4302.1493774205865)),
215
- ]);
216
- }
217
- });
218
-
219
-
220
- it("should trim the line and return right side offcuts, clip is completely contained in source", () => {
221
- // Arrange
222
- const source = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
223
- const clip = new Line3D(new Vec3(0, 0, 0), new Vec3(7, 0, 0));
224
-
225
- // Act
226
- const result = source.clipLine(clip);
227
- const groupResult = source.clipLines([clip]);
228
-
229
- // Assert
230
- expect(result.length).toEqual(1);
231
- expect(result[0].start).toEqual(new Vec3(7, 0, 0));
232
- expect(result[0].end).toEqual(new Vec3(10, 0, 0));
233
- expect(result).toEqual(groupResult);
234
- });
235
-
236
- it("should trim the line and return right side offcuts, clip overlapping from left", () => {
237
- // Arrange
238
- const source = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
239
- const clip = new Line3D(new Vec3(-1, 0, 0), new Vec3(7, 0, 0));
240
-
241
- // Act
242
- const result = source.clipLine(clip);
243
- const groupResult = source.clipLines([clip]);
244
-
245
- // Assert
246
- expect(result.length).toEqual(1);
247
- expect(result[0].start).toEqual(new Vec3(7, 0, 0));
248
- expect(result[0].end).toEqual(new Vec3(10, 0, 0));
249
- expect(result).toEqual(groupResult);
250
- });
251
-
252
- it("should trim the line and return left side off-cut, clip is completely contained in source", () => {
253
- // Arrange
254
- const source = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
255
- const clip = new Line3D(new Vec3(5, 0, 0), new Vec3(10, 0, 0));
256
-
257
- // Act
258
- const result = source.clipLine(clip);
259
- const groupResult = source.clipLines([clip]);
260
-
261
- // Assert
262
- expect(result.length).toEqual(1);
263
- expect(result[0].start).toEqual(new Vec3(0, 0, 0));
264
- expect(result[0].end).toEqual(new Vec3(5, 0, 0));
265
- expect(result).toEqual(groupResult);
266
- });
267
-
268
- it("should trim the line and return left side off-cut, clip overlapping from right", () => {
269
- // Arrange
270
- const source = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
271
- const clip = new Line3D(new Vec3(5, 0, 0), new Vec3(15, 0, 0));
272
-
273
- // Act
274
- const result = source.clipLine(clip);
275
- const groupResult = source.clipLines([clip]);
276
-
277
- // Assert
278
- expect(result.length).toEqual(1);
279
- expect(result[0].start).toEqual(new Vec3(0, 0, 0));
280
- expect(result[0].end).toEqual(new Vec3(5, 0, 0));
281
- expect(result).toEqual(groupResult);
282
- });
283
-
284
- it("should trim the line and return no off-cut as it matches the source entirely", () => {
285
- // Arrange
286
- const source = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
287
- const clip = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
288
-
289
- // Act
290
- const result = source.clipLine(clip);
291
- const groupResult = source.clipLines([clip]);
292
-
293
- // Assert
294
- expect(result.length).toEqual(0);
295
- expect(result).toEqual(groupResult);
296
- });
297
-
298
- it("should trim the line and return no off-cut as it overlaps the source", () => {
299
- // Arrange
300
- const source = new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0));
301
- const clip = new Line3D(new Vec3(-1, 0, 0), new Vec3(11, 0, 0));
302
-
303
- // Act
304
- const result = source.clipLine(clip);
305
- const groupResult = source.clipLines([clip]);
306
-
307
- // Assert
308
- expect(result.length).toEqual(0);
309
- expect(result).toEqual(groupResult);
310
- });
311
-
312
- it("should trim the line with opposite direction", () => {
313
- // Arrange
314
- const source = new Line3D(new Vec3(6, 0, 0), new Vec3(10, 0, 0));
315
- const clip = new Line3D(new Vec3(7, 0, 0), new Vec3(4, 0, 0));
316
-
317
- // Act
318
- const result = source.clipLine(clip);
319
- const groupResult = source.clipLines([clip]);
320
-
321
- // Assert
322
- expect(result).toEqual([new Line3D(new Vec3(7, 0, 0), new Vec3(10, 0, 0))]);
323
- expect(result).toEqual(groupResult);
324
- });
325
-
326
- it.each([
327
- // Arrange
328
- { a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), b: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), expected: true },
329
- { a: new Line3D(new Vec3(10, 0, 0), new Vec3(0, 0, 0)), b: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), expected: true },
330
- { a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 1, 0)), b: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), expected: false },
331
- ])("should determine if the lines are parallel %j", ({ a, b, expected }) => {
332
- // Act
333
- const areParallel = a.isParallelTo(b);
334
- // Assert
335
- expect(areParallel).toBe(expected);
336
- });
337
-
338
- it("chunk should split the line in to multiple with expected length", () => {
339
- const size = 10;
340
- const line = new Line3D(new Vec3(0, 0, 0), new Vec3(88, 0, 0));
341
- const chunks = line.chunk(size);
342
- expect(chunks.length).toEqual(9);
343
-
344
- expect(chunks[0].length).toEqual(size);
345
- expect(chunks[0].start.equals(line.start)).toBe(true);
346
-
347
- expect(chunks[chunks.length - 1].length).toEqual(8);
348
- expect(chunks[chunks.length - 1].end.equals(line.end)).toBe(true);
349
- });
350
- });
package/tsconfig-cjs.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "module": "commonjs",
5
- "outDir": "./cjs",
6
- "declaration": false,
7
- "declarationDir": null
8
- }
9
- }
package/tsconfig.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "outDir": "./esm",
4
- "declaration": true,
5
- "declarationDir": "./types",
6
- "importHelpers": true,
7
- "target": "ESNext",
8
- "module": "ESNext",
9
- "moduleResolution": "node",
10
- "lib": [ "esnext", "dom" ]
11
- },
12
- "exclude": ["src/**/*.test.ts", "src/**/*.spec.ts"]
13
- }