@immugio/three-math-extensions 0.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.
- package/.eslintrc.json +32 -0
- package/README +1 -0
- package/cjs/Line2D.js +527 -0
- package/cjs/Line3D.js +373 -0
- package/cjs/Point2.js +2 -0
- package/cjs/Point3.js +2 -0
- package/cjs/Vec2.js +19 -0
- package/cjs/Vec3.js +57 -0
- package/cjs/index.js +11 -0
- package/jest.config.js +6 -0
- package/package.json +44 -0
- package/src/Line2D.ts +615 -0
- package/src/Line3D.ts +448 -0
- package/src/Point2.ts +4 -0
- package/src/Point3.ts +6 -0
- package/src/Vec2.ts +18 -0
- package/src/Vec3.ts +70 -0
- package/src/__tests__/Line2D.spec.ts +329 -0
- package/src/__tests__/Line3D.join.spec.ts +28 -0
- package/src/__tests__/Line3D.joinLines.spec.ts +72 -0
- package/src/__tests__/Line3D.spec.ts +321 -0
- package/src/index.ts +4 -0
- package/tsconfig-cjs.json +9 -0
- package/tsconfig.json +13 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { Line2D } from "../Line2D";
|
|
2
|
+
import { Vector2 } from "three";
|
|
3
|
+
import { Vec2 } from "../Vec2";
|
|
4
|
+
import { Point2 } from "../Point2";
|
|
5
|
+
|
|
6
|
+
describe("Line", () => {
|
|
7
|
+
it("should be created", () => {
|
|
8
|
+
const start = new Vec2(1, 2);
|
|
9
|
+
const end = new Vec2(3, 4);
|
|
10
|
+
const line = new Line2D(start, end, 20);
|
|
11
|
+
expect(line).toEqual(new Line2D(start, end, 20));
|
|
12
|
+
expect(line.start).toBe(start);
|
|
13
|
+
expect(line.end).toBe(end);
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("should be created fromCoordinates", () => {
|
|
17
|
+
const line = Line2D.fromCoordinates(1, 2, 3, 4, 10);
|
|
18
|
+
expect(line.start.x).toEqual(1);
|
|
19
|
+
expect(line.start.y).toEqual(2);
|
|
20
|
+
expect(line.end.x).toEqual(3);
|
|
21
|
+
expect(line.end.y).toEqual(4);
|
|
22
|
+
expect(line.index).toEqual(10);
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("should be created fromPoints", () => {
|
|
26
|
+
const line = Line2D.fromPoints({ x: 1, y: 2 }, { x: 3, y: 4 }, 10);
|
|
27
|
+
expect(line).toEqual(new Line2D(new Vec2(1, 2), new Vec2(3, 4), 10));
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("center should return the center of the line", () => {
|
|
31
|
+
const line = Line2D.fromCoordinates(-2, -2, 2, 2);
|
|
32
|
+
expect(line.center.x).toEqual(0);
|
|
33
|
+
expect(line.center.y).toEqual(0);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("resize should resize the line by the given length", () => {
|
|
37
|
+
const line = Line2D.fromCoordinates(-2, 1, 2, 1);
|
|
38
|
+
const originalCenter = line.center;
|
|
39
|
+
expect(line.length).toEqual(4);
|
|
40
|
+
|
|
41
|
+
const resizeDistance = 2;
|
|
42
|
+
line.resize(resizeDistance);
|
|
43
|
+
|
|
44
|
+
expect(line.length).toEqual(6);
|
|
45
|
+
expect(line.center).toEqual(originalCenter);
|
|
46
|
+
expect(line.start.x).toEqual(-3);
|
|
47
|
+
expect(line.end.x).toEqual(3);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("moveStartPoint should move p1 on the line by the given amount", () => {
|
|
51
|
+
const line = Line2D.fromCoordinates(-2, 1, 2, 1);
|
|
52
|
+
expect(line.length).toEqual(4);
|
|
53
|
+
|
|
54
|
+
line.moveStartPoint(2);
|
|
55
|
+
expect(line.length).toEqual(6);
|
|
56
|
+
expect(line.start.x).toEqual(-4);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test("moveEndPoint should move p1 on the line by the given amount", () => {
|
|
60
|
+
const line = Line2D.fromCoordinates(-2, 1, 2, 1);
|
|
61
|
+
expect(line.length).toEqual(4);
|
|
62
|
+
|
|
63
|
+
line.moveEndPoint(2);
|
|
64
|
+
expect(line.length).toEqual(6);
|
|
65
|
+
expect(line.end.x).toEqual(4);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("flip should swap the end points", () => {
|
|
69
|
+
const line = Line2D.fromCoordinates(-2, 1, 2, 1);
|
|
70
|
+
const original = line.clone();
|
|
71
|
+
line.flip();
|
|
72
|
+
expect(line.start.equals(original.end)).toBe(true);
|
|
73
|
+
expect(line.end.equals(original.start)).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("rotate should rotate the endpoints around the line center", () => {
|
|
77
|
+
const line = Line2D.fromCoordinates(10, 0, 20, 0);
|
|
78
|
+
line.rotate(Math.PI / 2); // 90 degrees counterclockwise
|
|
79
|
+
expect(line.start.x).toEqual(15);
|
|
80
|
+
expect(line.start.y).toEqual(-5);
|
|
81
|
+
expect(line.end.x).toEqual(15);
|
|
82
|
+
expect(line.end.y).toEqual(5);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("translate should rotate the endpoints around the line center", () => {
|
|
86
|
+
const line = Line2D.fromCoordinates(10, 0, 20, 0);
|
|
87
|
+
line.translate({ x: 10, y: 100 });
|
|
88
|
+
expect(line.start.x).toEqual(20);
|
|
89
|
+
expect(line.start.y).toEqual(100);
|
|
90
|
+
expect(line.end.x).toEqual(30);
|
|
91
|
+
expect(line.end.y).toEqual(100);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
test.each([
|
|
95
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 5, 5, true],
|
|
96
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 10, true],
|
|
97
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 15, 15, true],
|
|
98
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 16, 16, false],
|
|
99
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 16, false],
|
|
100
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 4, false],
|
|
101
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 4, 4, false],
|
|
102
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 15, 16, false],
|
|
103
|
+
])("Line.isPointOnLineSection should return true if the point is on the line section", (line: Line2D, x, y, expected: boolean) => {
|
|
104
|
+
expect(line.isPointOnLineSection({x, y})).toBe(expected);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test.each([
|
|
108
|
+
[Line2D.fromCoordinates(5, 5, 15, 5), 10, 6, .9, false],
|
|
109
|
+
[Line2D.fromCoordinates(5, 5, 15, 5), 10, 6, 1, true],
|
|
110
|
+
])("Line.isPointCloseToAndBesideLineSection should return true if the point is within expected distance of the line section", (line: Line2D, x, y, tolerance, expected: boolean) => {
|
|
111
|
+
expect(line.isPointCloseToAndBesideLineSection({x, y}, tolerance)).toBe(expected);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
test.each([
|
|
115
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 5, 5, true],
|
|
116
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 10, true],
|
|
117
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 15, 15, true],
|
|
118
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 16, 16, false],
|
|
119
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 16, true],
|
|
120
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 4, true],
|
|
121
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 4, 4, false],
|
|
122
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 15, 16, false],
|
|
123
|
+
])("Line.isPointBesideLineSection should return true if the point is beside the line section", (line: Line2D, x, y, expected: boolean) => {
|
|
124
|
+
expect(line.isPointBesideLineSection({x, y})).toBe(expected);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test.each([
|
|
128
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 5, 5, 0],
|
|
129
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 10, 0],
|
|
130
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 15, 15, 0],
|
|
131
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 16, 16, 0],
|
|
132
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 16, 4.242640687119285],
|
|
133
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 4, 4.242640687119285],
|
|
134
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 4, 4, 0],
|
|
135
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 11, 0.7071067811865476],
|
|
136
|
+
])("Line.distanceToPointOnInfiniteLine should return true if the point is beside the line section. Line: (%s, x: %s, y: %s) expected: %s", (line: Line2D, x, y, expected: number) => {
|
|
137
|
+
expect(line.distanceToPointOnInfiniteLine({x, y})).toBe(expected);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test.each([
|
|
141
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 5, 5, true],
|
|
142
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 10, 10, true],
|
|
143
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 15, 15, true],
|
|
144
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 16, 16, true],
|
|
145
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 4, 4, true],
|
|
146
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), 15, 16, false],
|
|
147
|
+
])("Line.isPointOnInfiniteLine should return true if the point is on the infinite line", (line: Line2D, x, y, expected: boolean) => {
|
|
148
|
+
expect(line.isPointOnInfiniteLine({x, y})).toBe(expected);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test.each([
|
|
152
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(15, 5, 20, 5), true], // Other on the right, touching
|
|
153
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), Line2D.fromCoordinates(15, 15, 16, 16), true], // Other on the right, touching, angle
|
|
154
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(15, 5, 20, 6), false], // Other on the right, touching but lines are not collinear
|
|
155
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(14, 5, 20, 5), true], // Other on the right, overlapping
|
|
156
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(9, 5, 20, 5), true], // Other completely covers Line
|
|
157
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(16, 5, 20, 5), false], // Other on the left, with gap
|
|
158
|
+
])("Line.canJoinLine should return true if the point is on the line %s, other: %s, expected: %s" , (line: Line2D, other: Line2D, expected: boolean) => {
|
|
159
|
+
expect(line.isCollinearWithTouchOrOverlap(other)).toBe(expected);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test.each([
|
|
163
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(15, 5, 20, 5), Line2D.fromCoordinates(10, 5, 20, 5)], // Other on the right, touching
|
|
164
|
+
[Line2D.fromCoordinates(5, 5, 15, 15), Line2D.fromCoordinates(15, 15, 16, 16), Line2D.fromCoordinates(5, 5, 16, 16)], // Other on the right, touching, angle
|
|
165
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(15, 5, 20, 6), null], // Other on the right, touching but lines are not collinear
|
|
166
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(14, 5, 20, 5), Line2D.fromCoordinates(10, 5, 20, 5)], // Other on the right, overlapping
|
|
167
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(9, 5, 20, 5), Line2D.fromCoordinates(9, 5, 20, 5)], // Other completely covers Line
|
|
168
|
+
[Line2D.fromCoordinates(10, 5, 15, 5), Line2D.fromCoordinates(16, 5, 20, 5), null], // Other on the left, with gap
|
|
169
|
+
])("Line.joinLine returns correctly joined lines", (line: Line2D, other: Line2D, expected: Line2D) => {
|
|
170
|
+
const result = Line2D.joinLine(line, other);
|
|
171
|
+
if (result === null) {
|
|
172
|
+
expect(result).toBe(null);
|
|
173
|
+
} else {
|
|
174
|
+
expect(result.start.x).toEqual(expected.start.x);
|
|
175
|
+
expect(result.start.y).toEqual(expected.start.y);
|
|
176
|
+
expect(result.end.x).toEqual(expected.end.x);
|
|
177
|
+
expect(result.end.y).toEqual(expected.end.y);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("joinLines returns correctly joined lines", () => {
|
|
182
|
+
const gr1_Line1 = Line2D.fromCoordinates(15, 0, 20, 0);
|
|
183
|
+
const gr1_Line2 = Line2D.fromCoordinates(20, 0, 55, 0);
|
|
184
|
+
|
|
185
|
+
const gr2_Line1 = Line2D.fromCoordinates(915, 0, 920, 0);
|
|
186
|
+
const gr2_Line2 = Line2D.fromCoordinates(920, 0, 955, 0);
|
|
187
|
+
|
|
188
|
+
const result = Line2D.joinLines([gr1_Line1, gr1_Line2, gr2_Line1, gr2_Line2]);
|
|
189
|
+
expect(result.length).toEqual(2);
|
|
190
|
+
|
|
191
|
+
const gr1 = result.filter(l => l.start.equals(gr1_Line1.start) && l.end.equals(gr1_Line2.end));
|
|
192
|
+
expect(gr1.length).toEqual(1);
|
|
193
|
+
|
|
194
|
+
const gr2 = result.filter(l => l.start.equals(gr2_Line1.start) && l.end.equals(gr2_Line2.end));
|
|
195
|
+
expect(gr2.length).toEqual(1);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
test("closestPointOnInfiniteLine returns the closes point to the infinite line", () => {
|
|
199
|
+
const line = Line2D.fromCoordinates(0, 0, 10, 0);
|
|
200
|
+
const point = new Vector2(11, 1);
|
|
201
|
+
const closest = line.closestPointOnInfiniteLine(point);
|
|
202
|
+
expect(closest.x).toEqual(11);
|
|
203
|
+
expect(closest.y).toEqual(0);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("clipOverflow clip the overlapping sections from the other line", () => {
|
|
207
|
+
const line = Line2D.fromCoordinates(0, 0, 10, 0);
|
|
208
|
+
const other = Line2D.fromCoordinates(-5, 0, 15, 0);
|
|
209
|
+
line.trimExcess(other);
|
|
210
|
+
expect(other.start).toEqual(line.start);
|
|
211
|
+
expect(other.end).toEqual(line.end);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("clipOverflow clip the overlapping sections from the other line", () => {
|
|
215
|
+
const line = Line2D.fromCoordinates(0, 0, 15, 0);
|
|
216
|
+
const other = Line2D.fromCoordinates(1, 0, 10, 0);
|
|
217
|
+
line.extendToEnds(other, 1);
|
|
218
|
+
expect(other.start).toEqual(line.start); // Only the first point is extended because it's within tolerance
|
|
219
|
+
expect(other.end.x).toEqual(10); // The second point is not extended because it's outside tolerance
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
test.each([
|
|
223
|
+
[new Vector2(15, 0), new Vector2(7.5, 7.5)],
|
|
224
|
+
[new Vector2(2, 2), new Vector2(2, 2)],
|
|
225
|
+
[new Vector2(-1, -5), new Vector2(0, 0)],
|
|
226
|
+
[new Vector2(20, 30), new Vector2(15, 15)],
|
|
227
|
+
])("Line.closestPointOnLine should return a point laying directly on the line", (source: Vector2, expected: Vector2) => {
|
|
228
|
+
const line = Line2D.fromCoordinates(0, 0, 15, 15);
|
|
229
|
+
const closest = line.closestPointOnLine(source);
|
|
230
|
+
expect(closest).toEqual(expected);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test.each([
|
|
234
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(0, 0, 10, 0), "Touch from the left but not overlapping", false],
|
|
235
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(20, 0, 30, 0), "Touch from the right but not overlapping", false],
|
|
236
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(22, 0, 30, 0), "Not touch, no overlap", false],
|
|
237
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(0, 1, 11, 0), "Not a real overlap, lines are not collinear", false],
|
|
238
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(0, 0, 11, 0), "Overlap from the left", true],
|
|
239
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(19, 0, 30, 0), "Overlap from the right", true],
|
|
240
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(11, 0, 19, 0), "Line completely covers Other", true],
|
|
241
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(0, 0, 30, 0), "Line is completely covered by Other", true],
|
|
242
|
+
])("Line.overlaps should return true if the lines overlap. Line: %s, other: %s, description: %s expected: %s", (line: Line2D, other: Line2D, description: string, expected: boolean) => {
|
|
243
|
+
expect(line.overlaps(other)).toBe(expected);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
test.each([
|
|
247
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(0, 0, 10, 0), "Touch from the left but not overlapping", null],
|
|
248
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(20, 0, 30, 0), "Touch from the right but not overlapping", null],
|
|
249
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(22, 0, 30, 0), "Not touch, no overlap", null],
|
|
250
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(0, 1, 11, 0), "Not a real overlap, lines are not collinear", null],
|
|
251
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(0, 0, 11, 0), "Overlap from the left", Line2D.fromCoordinates(10, 0, 11, 0)],
|
|
252
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(19, 0, 30, 0), "Overlap from the right", Line2D.fromCoordinates(19, 0, 20, 0)],
|
|
253
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(11, 0, 19, 0), "Line completely covers Other", Line2D.fromCoordinates(11, 0, 19, 0)],
|
|
254
|
+
[Line2D.fromCoordinates(10, 0, 20, 0), Line2D.fromCoordinates(0, 0, 30, 0), "Line is completely covered by Other", Line2D.fromCoordinates(10, 0, 20, 0)],
|
|
255
|
+
[Line2D.fromCoordinates(3600, 1350, 3600, 2050), Line2D.fromCoordinates(3600, 1950, 3600, 2650), "Vertical Line is covered by Other on bottom", Line2D.fromCoordinates(3600, 1950, 3600, 2050)],
|
|
256
|
+
])("Line.getOverlap should return overlap. Line: %s, other: %s, description: %s expected: %s", (line: Line2D, other: Line2D, description: string, expected: Line2D) => {
|
|
257
|
+
const result = line.getOverlap(other);
|
|
258
|
+
if (expected) {
|
|
259
|
+
expect(result.start.equals(expected.start)).toBe(true);
|
|
260
|
+
expect(result.end.equals(expected.end)).toBe(true);
|
|
261
|
+
} else {
|
|
262
|
+
expect(result).toBeNull();
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
test.each([
|
|
267
|
+
[
|
|
268
|
+
Line2D.fromCoordinates(3550, 2400, 150, 2400),
|
|
269
|
+
[Line2D.fromCoordinates(650, 2400, 150, 2400), Line2D.fromCoordinates(3550, 2400, 3050, 2400), Line2D.fromCoordinates(2404, 2400, 1604, 2400)],
|
|
270
|
+
"Horizontal line with 2 clips on on the ends and one middle",
|
|
271
|
+
[Line2D.fromCoordinates(3050, 2400, 2404, 2400), Line2D.fromCoordinates(1604, 2400, 650, 2400)],
|
|
272
|
+
],
|
|
273
|
+
[
|
|
274
|
+
Line2D.fromCoordinates(3600, 150, 3600, 2450),
|
|
275
|
+
[Line2D.fromCoordinates(3600, 927, 3600, 1427)],
|
|
276
|
+
"Vertical line with 1 clip in the middle middle",
|
|
277
|
+
[Line2D.fromCoordinates(3600, 150, 3600, 927), Line2D.fromCoordinates(3600, 1427, 3600, 2450)],
|
|
278
|
+
],
|
|
279
|
+
[
|
|
280
|
+
Line2D.fromCoordinates(6050, 3100, 3800, 3100),
|
|
281
|
+
[Line2D.fromCoordinates(6150, 3100, 3700, 3100)],
|
|
282
|
+
"Clip line overlaps the source line on both ends",
|
|
283
|
+
[],
|
|
284
|
+
],
|
|
285
|
+
])("Line.clipLines should return clipLines. Line: %s, others: %s, description: %s expected: %s", (line: Line2D, others: Line2D[], description: string, expected: Line2D[]) => {
|
|
286
|
+
const result = Line2D.clipLines(line, others);
|
|
287
|
+
expect(result.length).toEqual(expected.length);
|
|
288
|
+
expected.forEach((e, i) => {
|
|
289
|
+
expect(result[i].equals(e)).toBe(true);
|
|
290
|
+
});
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
test.each([
|
|
294
|
+
[
|
|
295
|
+
Line2D.fromCoordinates(0, 50, 100, 50),
|
|
296
|
+
Line2D.fromCoordinates(50, 0, 50, 100),
|
|
297
|
+
"Horizontal with vertical, intersected in the middle",
|
|
298
|
+
{ x: 50, y: 50 }
|
|
299
|
+
],
|
|
300
|
+
[
|
|
301
|
+
Line2D.fromCoordinates(0, 50, 100, 50),
|
|
302
|
+
Line2D.fromCoordinates(50, 0, 51, 100),
|
|
303
|
+
"Horizontal with vertical, intersected in the middle, but the angle in not 90 degrees",
|
|
304
|
+
null
|
|
305
|
+
],
|
|
306
|
+
[
|
|
307
|
+
Line2D.fromCoordinates(0, 50, 100, 50),
|
|
308
|
+
Line2D.fromCoordinates(50, 0, 50, 49),
|
|
309
|
+
"Horizontal with vertical, 1 unit away from intersection",
|
|
310
|
+
null
|
|
311
|
+
],
|
|
312
|
+
])("Line.hasIntersectionWithAngle should return expected intersection. Line: %s, others: %s, description: %s expected: %s", (line: Line2D, other: Line2D, description: string, expected: Point2) => {
|
|
313
|
+
const result = line.hasIntersectionWithAngle(other, Math.PI / 2);
|
|
314
|
+
expect(result).toEqual(expected);
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
test("Line.chunk should split lines in to multiple with expected length", () => {
|
|
318
|
+
const size = 10;
|
|
319
|
+
const line = Line2D.fromCoordinates(0, 0, 88, 0);
|
|
320
|
+
const chunks = line.chunk(size);
|
|
321
|
+
expect(chunks.length).toEqual(9);
|
|
322
|
+
|
|
323
|
+
expect(chunks[0].length).toEqual(size);
|
|
324
|
+
expect(chunks[0].start.equals(line.start)).toBe(true);
|
|
325
|
+
|
|
326
|
+
expect(chunks[chunks.length - 1].length).toEqual(8);
|
|
327
|
+
expect(chunks[chunks.length - 1].end.equals(line.end)).toBe(true);
|
|
328
|
+
});
|
|
329
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Line3D } from "../Line3D";
|
|
2
|
+
import { Vec3 } from "../Vec3";
|
|
3
|
+
|
|
4
|
+
describe("Line3d.jointLine", () => {
|
|
5
|
+
describe.each([
|
|
6
|
+
{ message: "Same direction - Lines aren't parallel", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), b: new Line3D(new Vec3(10, 0, 0), new Vec3(20, 0, 1)), expected: null },
|
|
7
|
+
{ message: "Same direction - Lines don't overlap" ,a: new Line3D(new Vec3(0, 0, 0), new Vec3(5, 0, 0)), b: new Line3D(new Vec3(11, 0, 0), new Vec3(12, 0, 0)), expected: null},
|
|
8
|
+
{ message: "Same direction - This line entirely covers the other line", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), b: new Line3D(new Vec3(1, 0, 0), new Vec3(9, 0, 0)), expected: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)) },
|
|
9
|
+
{ message: "Same direction - The other line entirely covers this line", a: new Line3D(new Vec3(1, 0, 0), new Vec3(9, 0, 0)), b: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), expected: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)) },
|
|
10
|
+
{ message: "Same direction - This line is overlapped by the start of the other line", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), b: new Line3D(new Vec3(5, 0, 0), new Vec3(15, 0, 0)), expected: new Line3D(new Vec3(0, 0, 0), new Vec3(15, 0, 0)) },
|
|
11
|
+
{ message: "Same direction - This line is overlapped by the end of the other line", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), b: new Line3D(new Vec3(-5, 0, 0), new Vec3(5, 0, 0)), expected: new Line3D(new Vec3(-5, 0, 0), new Vec3(10, 0, 0)) },
|
|
12
|
+
|
|
13
|
+
{ message: "Opposite direction - Lines aren't parallel", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), b: new Line3D(new Vec3(20, 0, 0), new Vec3(10, 0, 1)), expected: null },
|
|
14
|
+
{ message: "Opposite direction - Lines don't overlap", a: new Line3D(new Vec3(0, 0, 0), new Vec3(5, 0, 0)), b: new Line3D(new Vec3(12, 0, 0), new Vec3(11, 0, 0)), expected: null },
|
|
15
|
+
{ message: "Opposite direction - This line entirely covers the other line", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), b: new Line3D(new Vec3(9, 0, 0), new Vec3(1, 0, 0)), expected: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)) },
|
|
16
|
+
{ message: "Opposite direction - The other line entirely covers this line", a: new Line3D(new Vec3(1, 0, 0), new Vec3(9, 0, 0)), b: new Line3D(new Vec3(10, 0, 0), new Vec3(0, 0, 0)), expected: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)) },
|
|
17
|
+
{ message: "Opposite direction - This line is overlapped by the start of the other line", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), b: new Line3D(new Vec3(15, 0, 0), new Vec3(5, 0, 0)), expected: new Line3D(new Vec3(0, 0, 0), new Vec3(15, 0, 0)) },
|
|
18
|
+
{ message: "Opposite direction - This line is overlapped by the end of the other line", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)), b: new Line3D(new Vec3(5, 0, 0), new Vec3(-5, 0, 0)), expected: new Line3D(new Vec3(-5, 0, 0), new Vec3(10, 0, 0)) },
|
|
19
|
+
|
|
20
|
+
{ message: "Angle - Lines aren't parallel", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 9, 0)), b: new Line3D(new Vec3(5, 5, 0), new Vec3(15, 15, 0)), expected: null },
|
|
21
|
+
{ message: "Angle - This line is overlapped by the start of the other line", a: new Line3D(new Vec3(0, 0, 0), new Vec3(10, 10, 0)), b: new Line3D(new Vec3(5, 5, 0), new Vec3(15, 15, 0)), expected: new Line3D(new Vec3(0, 0, 0), new Vec3(15, 15, 0)) },
|
|
22
|
+
])("joint", ({message, a, b, expected }) => {
|
|
23
|
+
const result = a.joinLine(b);
|
|
24
|
+
test(`${message} - expected ${JSON.stringify(expected)}, received: ${JSON.stringify(result)}`, () => {
|
|
25
|
+
expect(result).toEqual(expected);
|
|
26
|
+
});
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { Line3D } from "../Line3D";
|
|
2
|
+
import { Vec3 } from "../Vec3";
|
|
3
|
+
|
|
4
|
+
describe("Line3d.jointLines", () => {
|
|
5
|
+
describe.each([
|
|
6
|
+
{
|
|
7
|
+
message: "Single line",
|
|
8
|
+
source: [
|
|
9
|
+
new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)),
|
|
10
|
+
], expected: [
|
|
11
|
+
new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)),
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
message: "2 lines with gap",
|
|
16
|
+
source: [
|
|
17
|
+
new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)),
|
|
18
|
+
new Line3D(new Vec3(11, 0, 0), new Vec3(20, 0, 0)),
|
|
19
|
+
], expected: [
|
|
20
|
+
new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)),
|
|
21
|
+
new Line3D(new Vec3(11, 0, 0), new Vec3(20, 0, 0)),
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
message: "Three lines touching",
|
|
26
|
+
source: [
|
|
27
|
+
new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)),
|
|
28
|
+
new Line3D(new Vec3(10, 0, 0), new Vec3(20, 0, 0)),
|
|
29
|
+
new Line3D(new Vec3(20, 0, 0), new Vec3(30, 0, 0)),
|
|
30
|
+
], expected: [
|
|
31
|
+
new Line3D(new Vec3(0, 0, 0), new Vec3(30, 0, 0)),
|
|
32
|
+
]
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
message: "Three lines touching, not ordered",
|
|
36
|
+
source: [
|
|
37
|
+
new Line3D(new Vec3(10, 0, 0), new Vec3(20, 0, 0)),
|
|
38
|
+
new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)),
|
|
39
|
+
new Line3D(new Vec3(20, 0, 0), new Vec3(30, 0, 0)),
|
|
40
|
+
], expected: [
|
|
41
|
+
new Line3D(new Vec3(0, 0, 0), new Vec3(30, 0, 0)),
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
message: "Sources is composed of complex overlapping, covering and duplicated lines",
|
|
46
|
+
source: [
|
|
47
|
+
new Line3D(new Vec3(10, 0, 0), new Vec3(20, 0, 0)), // Group 1
|
|
48
|
+
new Line3D(new Vec3(0, 0, 0), new Vec3(10, 0, 0)),
|
|
49
|
+
new Line3D(new Vec3(20, 0, 0), new Vec3(30, 0, 0)),
|
|
50
|
+
new Line3D(new Vec3(10, 0, 0), new Vec3(20, 0, 0)),
|
|
51
|
+
new Line3D(new Vec3(-5, 0, 0), new Vec3(10, 0, 0)),
|
|
52
|
+
new Line3D(new Vec3(29, 0, 0), new Vec3(35, 0, 0)),
|
|
53
|
+
|
|
54
|
+
new Line3D(new Vec3(100 + 10, 0, 0), new Vec3(100 + 20, 0, 0)), // Group 2 - same axes but shifted by x: 100
|
|
55
|
+
new Line3D(new Vec3(100, 0, 0), new Vec3(100 + 10, 0, 0)),
|
|
56
|
+
new Line3D(new Vec3(100 + 20, 0, 0), new Vec3(100 + 30, 0, 0)),
|
|
57
|
+
new Line3D(new Vec3(100 + 10, 0, 0), new Vec3(100 + 20, 0, 0)),
|
|
58
|
+
new Line3D(new Vec3(100 + -5, 0, 0), new Vec3(100 + 10, 0, 0)),
|
|
59
|
+
new Line3D(new Vec3(100 + 29, 0, 0), new Vec3(100 + 35, 0, 0)),
|
|
60
|
+
], expected: [
|
|
61
|
+
new Line3D(new Vec3(-5, 0, 0), new Vec3(35, 0, 0)),
|
|
62
|
+
new Line3D(new Vec3(100 + -5, 0, 0), new Vec3(100 + 35, 0, 0)),
|
|
63
|
+
]
|
|
64
|
+
},
|
|
65
|
+
])("jointLines", ({ message, source, expected }) => {
|
|
66
|
+
const result = Line3D.joinLines(source);
|
|
67
|
+
result.sort((a, b) => a.start.x - b.start.x); // Order of the result is not guaranteed
|
|
68
|
+
test(`${message} - expected ${JSON.stringify(expected)}, received: ${JSON.stringify(result)}`, () => {
|
|
69
|
+
expect(result).toEqual(expected);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
});
|