forked from Orillusion/orillusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Line.ts
285 lines (244 loc) · 7.61 KB
/
Line.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { Color } from './Color';
import { Matrix4 } from './Matrix4';
import { Ray } from './Ray';
import { Vector3 } from './Vector3';
import { repeat } from './MathUtil';
/**
* @internal
*/
export enum LineClassification {
COLLINEAR,
LINES_INTERSECT,
SEGMENTS_INTERSECT,
A_BISECTS_B,
B_BISECTS_A,
PARALELL,
}
/**
* @internal
*/
export enum PointClassification {
ON_LINE,
LEFT_SIDE,
RIGHT_SIDE,
}
/**
* @internal
* @group Math
*/
export class Line {
static cacluteLine0: Line = new Line(null, null);
static cacluteLine1: Line = new Line(null, null);
public start: Vector3;
public end: Vector3;
public color: Color = new Color(1, 1, 1, 1);
private _normal: Vector3;
private _normalCalculated: boolean = false;
constructor(start?: Vector3, end?: Vector3) {
this.start = start;
this.end = end;
}
public set(start: Vector3, end: Vector3) {
this.start = start;
this.end = end;
}
public getCenter(): Vector3 {
let help = Vector3.HELP_0;
this.start.subtract(this.end, help);
help.scaleBy(0.5);
help.add(this.end);
return help;
}
public inverse() {
let tmp = this.start;
this.start = this.end;
this.end = tmp;
}
public equals(l: Line): boolean {
if ((this.start == l.start && this.end == l.end) || (this.start == l.end && this.end == l.start)) return true;
return false;
}
/**
*/
public toArray() {
return [this.start.x, this.start.y, this.start.z, this.end.x, this.end.y, this.end.z];
}
/**
* @param ps
*/
public static getLines(ps: Vector3[]): Line[] {
let arr: Line[] = [];
for (let index = 0; index < ps.length; index++) {
let i0 = index;
let i1 = repeat(index + 1, ps.length);
let p0 = ps[i0];
let p1 = ps[i1];
arr.push(new Line(p0, p1));
}
return arr;
}
/**
* Determine the relationship between two straight lines
* this line A = x0, y0 and B = x1, y1
* other is A = x2, y2 and B = x3, y3
* @param other compare other line
* @param pIntersectPoint (out)Returns the intersection point of two line segments
* @return
*/
public intersection(other: Line, pIntersectPoint: Vector3 = null): LineClassification {
var denom: number = (other.end.z - other.start.z) * (this.end.x - this.start.x) - (other.end.x - other.start.x) * (this.end.z - this.start.z);
var u0: number = (other.end.x - other.start.x) * (this.start.z - other.start.z) - (other.end.z - other.start.z) * (this.start.x - other.start.x);
var u1: number = (other.start.x - this.start.x) * (this.end.z - this.start.z) - (other.start.z - this.start.z) * (this.end.x - this.start.x);
//if parallel
if (denom == 0.0) {
if (u0 == 0.0 && u1 == 0.0) return LineClassification.COLLINEAR;
else return LineClassification.PARALELL;
} else {
u0 = u0 / denom;
u1 = u1 / denom;
var x: number = this.start.x + u0 * (this.end.x - this.start.x);
var z: number = this.start.z + u0 * (this.end.z - this.start.z);
if (pIntersectPoint != null) {
pIntersectPoint.x = x;
pIntersectPoint.y = 0;
pIntersectPoint.z = z;
}
if (u0 >= 0.0 && u0 <= 1.0 && u1 >= 0.0 && u1 <= 1.0) {
return LineClassification.SEGMENTS_INTERSECT;
} else if (u1 >= 0.0 && u1 <= 1.0) {
return LineClassification.A_BISECTS_B;
} else if (u0 >= 0.0 && u0 <= 1.0) {
return LineClassification.B_BISECTS_A;
}
return LineClassification.LINES_INTERSECT;
}
}
/**
* Straight direction
* @return
*/
public getDirection(): Vector3 {
var pt: Vector3 = this.end.subtract(this.start);
var direction: Vector3 = new Vector3(pt.x, pt.y);
return direction.normalize();
}
copyFrom(line: Line) {
if (!this.start) this.start = new Vector3();
if (!this.end) this.end = new Vector3();
this.start.copyFrom(line.start);
this.end.copyFrom(line.end);
}
public static IsEqual(d1, d2) {
if (Math.abs(d1 - d2) < 1e-7) return true;
return false;
}
public static squreDistanceSegmentToSegment(lineA: Line, lineB: Line, mat?: Matrix4) {
let a_po: Vector3 = lineA.start;
let a_p1: Vector3 = lineA.end;
let b_po: Vector3 = lineB.start;
let b_p1: Vector3 = lineB.end;
let x1 = a_po.x;
let y1 = a_po.y;
let z1 = a_po.z;
let x2 = a_p1.x;
let y2 = a_p1.y;
let z2 = a_p1.z;
let x3 = b_po.x;
let y3 = b_po.y;
let z3 = b_po.z;
let x4 = b_p1.x;
let y4 = b_p1.y;
let z4 = b_p1.z;
let ux = x2 - x1;
let uy = y2 - y1;
let uz = z2 - z1;
let vx = x4 - x3;
let vy = y4 - y3;
let vz = z4 - z3;
let wx = x1 - x3;
let wy = y1 - y3;
let wz = z1 - z3;
let a = ux * ux + uy * uy + uz * uz;
let b = ux * vx + uy * vy + uz * vz;
let c = vx * vx + vy * vy + vz * vz;
let d = ux * wx + uy * wy + uz * wz;
let e = vx * wx + vy * wy + vz * wz;
let dt = a * c - b * b;
let sd = dt;
let td = dt;
let sn = 0.0;
let tn = 0.0;
if (this.IsEqual(dt, 0.0)) {
sn = 0.0;
sd = 1.0;
tn = e;
td = c;
} else {
sn = b * e - c * d;
tn = a * e - b * d;
if (sn < 0.0) {
sn = 0.0;
tn = e;
td = c;
} else if (sn > sd) {
sn = sd;
tn = e + b;
td = c;
}
}
if (tn < 0.0) {
tn = 0.0;
if (-d < 0.0)
sn = 0.0;
else if (-d > a)
sn = sd;
else {
sn = -d;
sd = a;
}
} else if (tn > td) {
tn = td;
if (-d + b < 0.0) sn = 0.0;
else if (-d + b > a) sn = sd;
else {
sn = -d + b;
sd = a;
}
}
let sc = 0.0;
let tc = 0.0;
if (this.IsEqual(sn, 0.0)) sc = 0.0;
else sc = sn / sd;
if (this.IsEqual(tn, 0.0)) tc = 0.0;
else tc = tn / td;
let dx = wx + sc * ux - tc * vx;
let dy = wy + sc * uy - tc * vy;
let dz = wz + sc * uz - tc * vz;
return dx * dx + dy * dy + dz * dz;
}
/**
* isNearLine
*/
public isNear(ray: Ray, maxDistance: number = 0, mat?: Matrix4): boolean {
// //@task to-do
let tmpP0 = Vector3.HELP_0;
let tmpP1 = Vector3.HELP_1;
tmpP0.copyFrom(ray.origin);
tmpP1.copyFrom(ray.direction);
tmpP1.scaleBy(9999);
tmpP1.add(tmpP0, tmpP1);
Line.cacluteLine0.set(tmpP0, tmpP1);
Line.cacluteLine1.copyFrom(this);
if (mat) {
mat.perspectiveMultiplyPoint3(Line.cacluteLine1.start, Line.cacluteLine1.start);
mat.perspectiveMultiplyPoint3(Line.cacluteLine1.end, Line.cacluteLine1.end);
}
let dis = Line.squreDistanceSegmentToSegment(Line.cacluteLine0, Line.cacluteLine1, mat);
if (dis + 1e-4 <= maxDistance) {
ray.length = dis;
return true;
}
ray.length = -999999;
return false;
}
}