@carbonenginejs/core-math 0.1.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/LICENSE +21 -0
- package/NOTICE +24 -0
- package/README.md +110 -0
- package/THIRD-PARTY-NOTICES.md +38 -0
- package/package.json +74 -0
- package/src/box3.js +1329 -0
- package/src/constants.js +16 -0
- package/src/curve.js +390 -0
- package/src/geometry/box.js +132 -0
- package/src/geometry/cylinder.js +234 -0
- package/src/geometry/helpers/LICENSE +15 -0
- package/src/geometry/helpers/earcut.js +766 -0
- package/src/geometry/helpers/misc.js +103 -0
- package/src/geometry/index.js +7 -0
- package/src/geometry/json.js +115 -0
- package/src/geometry/lathe.js +148 -0
- package/src/geometry/octahedron.js +0 -0
- package/src/geometry/plane.js +76 -0
- package/src/geometry/shape.js +95 -0
- package/src/geometry/sphere.js +116 -0
- package/src/geometry/torus.js +88 -0
- package/src/index.js +29 -0
- package/src/lne3.js +488 -0
- package/src/mat3.js +131 -0
- package/src/mat4.js +600 -0
- package/src/mesh.js +298 -0
- package/src/noise.js +225 -0
- package/src/num.js +735 -0
- package/src/pln.js +740 -0
- package/src/pool.js +160 -0
- package/src/quat.js +110 -0
- package/src/ray3.js +1056 -0
- package/src/sph3.js +718 -0
- package/src/tangent.js +256 -0
- package/src/tri3.js +650 -0
- package/src/utils.js +15 -0
- package/src/vec2.js +229 -0
- package/src/vec3.js +1116 -0
- package/src/vec4.js +315 -0
- package/src/vertex.js +109 -0
package/src/pln.js
ADDED
|
@@ -0,0 +1,740 @@
|
|
|
1
|
+
import { num } from "./num.js";
|
|
2
|
+
import { vec3 } from "./vec3.js";
|
|
3
|
+
import { vec4 } from "./vec4.js";
|
|
4
|
+
import { mat3 } from "./mat3.js";
|
|
5
|
+
import { box3 } from "./box3.js";
|
|
6
|
+
import { pool } from "./pool.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Plane
|
|
10
|
+
*
|
|
11
|
+
* @typedef {Float32Array} pln
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export const pln = {};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Allocates a pooled pln
|
|
18
|
+
* @returns {Float32Array|pln}
|
|
19
|
+
*/
|
|
20
|
+
pln.alloc = function()
|
|
21
|
+
{
|
|
22
|
+
return pool.allocF32(4);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Unallocates a pooled pln
|
|
27
|
+
* @param {pln|Float32Array} a
|
|
28
|
+
*/
|
|
29
|
+
pln.unalloc = function(a)
|
|
30
|
+
{
|
|
31
|
+
pool.freeType(a);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Returns a subarray of a plane's normal
|
|
36
|
+
*
|
|
37
|
+
* @param {pln} a - source plane
|
|
38
|
+
* @returns {*} - plane normal reference
|
|
39
|
+
*/
|
|
40
|
+
pln.$normal = function(a)
|
|
41
|
+
{
|
|
42
|
+
return a.subarray(0, 3);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Clones a pln
|
|
48
|
+
*
|
|
49
|
+
* @param {pln} a - Source plane
|
|
50
|
+
* @returns {pln} - Cloned plane
|
|
51
|
+
*/
|
|
52
|
+
pln.clone = vec4.clone;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Returns the constant component of the pln
|
|
56
|
+
*
|
|
57
|
+
* @param {pln} a - Source plane
|
|
58
|
+
* @returns {number} - plane's constant
|
|
59
|
+
*/
|
|
60
|
+
pln.constant = function(a)
|
|
61
|
+
{
|
|
62
|
+
return a[3];
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Copies a pln
|
|
67
|
+
*
|
|
68
|
+
* @param {pln} a - Target plane
|
|
69
|
+
* @param {pln} b - Source plane
|
|
70
|
+
* @returns {pln} - Target plane
|
|
71
|
+
*/
|
|
72
|
+
pln.copy = vec4.copy;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Creates a plane
|
|
76
|
+
*
|
|
77
|
+
* @returns {pln}
|
|
78
|
+
*/
|
|
79
|
+
pln.create = vec4.create;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Gets the distance from a plane to a point
|
|
83
|
+
*
|
|
84
|
+
* @param {pln} a - plane to compare
|
|
85
|
+
* @param {vec3} p - Point to compare
|
|
86
|
+
* @returns {number} - The distance between them
|
|
87
|
+
*/
|
|
88
|
+
pln.distanceToPoint = function(a, p)
|
|
89
|
+
{
|
|
90
|
+
return (a[0] * p[0] + a[1] * p[1] + a[2] * p[2]) + a[3];
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Gets the distance from a plane to the components of a sphere
|
|
95
|
+
*
|
|
96
|
+
* @param {pln} a - plane to compare
|
|
97
|
+
* @param {vec3} position - sphere position to compare
|
|
98
|
+
* @param {number} radius - sphere radius to compare
|
|
99
|
+
* @returns {number} - The distance between them
|
|
100
|
+
*/
|
|
101
|
+
pln.distanceToPositionRadius = function(a, position, radius)
|
|
102
|
+
{
|
|
103
|
+
return (a[0] * position[0] + a[1] * position[1] + a[2] * position[2]) + a[3] - radius;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Gets the distance from a plane to a Float32Array(4) sphere
|
|
108
|
+
*
|
|
109
|
+
* @param {pln} a - plane to compare
|
|
110
|
+
* @param {sph3} sphere - sphere to compare
|
|
111
|
+
* @returns {number} - The distance between them
|
|
112
|
+
*/
|
|
113
|
+
pln.distanceToSph3 = function(a, sphere)
|
|
114
|
+
{
|
|
115
|
+
return (a[0] * sphere[0] + a[1] * sphere[1] + a[2] * sphere[2]) + a[3] - sphere[3];
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Gets the distance from a plane to a point's values
|
|
120
|
+
*
|
|
121
|
+
* @param {pln} a - plane to compare
|
|
122
|
+
* @param {Number} px - Point x to compare
|
|
123
|
+
* @param {Number} py - Point y to compare
|
|
124
|
+
* @param {Number} pz - Point z to compare
|
|
125
|
+
* @returns {number} - The distance between them
|
|
126
|
+
*/
|
|
127
|
+
pln.distanceToValues = function(a, px, py, pz)
|
|
128
|
+
{
|
|
129
|
+
return (a[0] * px + a[1] * py + a[2] * pz) + a[3];
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Compares two plns for equality
|
|
134
|
+
*
|
|
135
|
+
* @param {pln} a - plane to compare
|
|
136
|
+
* @param {pln} b - plane to compare
|
|
137
|
+
* @returns {boolean} - true if equal
|
|
138
|
+
*/
|
|
139
|
+
pln.equals = vec4.equals;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Compares a pln to plane components
|
|
143
|
+
*
|
|
144
|
+
* @param {pln} a - plane to compare
|
|
145
|
+
* @param {vec3} normal - plane normal to compare
|
|
146
|
+
* @param {number} constant - plane constant to compare
|
|
147
|
+
* @returns {boolean} - true if equal
|
|
148
|
+
*/
|
|
149
|
+
pln.equalsNormalConstant = function(a, normal, constant)
|
|
150
|
+
{
|
|
151
|
+
let a0 = a[0],
|
|
152
|
+
a1 = a[1],
|
|
153
|
+
a2 = a[2],
|
|
154
|
+
a3 = a[3],
|
|
155
|
+
b0 = normal[0],
|
|
156
|
+
b1 = normal[1],
|
|
157
|
+
b2 = normal[2],
|
|
158
|
+
b3 = constant;
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
Math.abs(a0 - b0) <= num.EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) &&
|
|
162
|
+
Math.abs(a1 - b1) <= num.EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) &&
|
|
163
|
+
Math.abs(a2 - b2) <= num.EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) &&
|
|
164
|
+
Math.abs(a3 - b3) <= num.EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3))
|
|
165
|
+
);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Compares two plns for exact equality
|
|
170
|
+
*
|
|
171
|
+
* @param {pln} a - plane to compare
|
|
172
|
+
* @param {pln} b - plane to compare
|
|
173
|
+
* @returns {boolean}
|
|
174
|
+
*/
|
|
175
|
+
pln.exactEquals = vec4.exactEquals;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Compares a pln to plane components for exact equality
|
|
179
|
+
*
|
|
180
|
+
* @param {pln} a - plane to compare
|
|
181
|
+
* @param {vec3} normal - plane normal to compare
|
|
182
|
+
* @param {number} constant - plane constant to compare
|
|
183
|
+
* @returns {boolean}
|
|
184
|
+
*/
|
|
185
|
+
pln.exactEqualsNormalConstant = function(a, normal, constant)
|
|
186
|
+
{
|
|
187
|
+
return a[0] === normal[0] && a[1] === normal[1] && a[2] === normal[2] && a[3] === constant;
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Extracts a pln's components
|
|
192
|
+
*
|
|
193
|
+
* @param {pln} a - Source plane
|
|
194
|
+
* @param {vec3} outNormal - Receiving vec3
|
|
195
|
+
* @returns {number} - plane constant
|
|
196
|
+
*/
|
|
197
|
+
pln.extract = function(a, outNormal)
|
|
198
|
+
{
|
|
199
|
+
outNormal[0] = a[0];
|
|
200
|
+
outNormal[1] = a[1];
|
|
201
|
+
outNormal[2] = a[2];
|
|
202
|
+
return a[3];
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Sets a pln from plane components
|
|
207
|
+
*
|
|
208
|
+
* @param {pln} out - Receiving plane
|
|
209
|
+
* @param {vec3} n - plane normal to set
|
|
210
|
+
* @param {number} c - plane constant to set
|
|
211
|
+
* @returns {pln} out - receiving plane
|
|
212
|
+
*/
|
|
213
|
+
pln.fromNormalConstant = function(out, n, c)
|
|
214
|
+
{
|
|
215
|
+
out[0] = n[0];
|
|
216
|
+
out[1] = n[1];
|
|
217
|
+
out[2] = n[2];
|
|
218
|
+
out[3] = c;
|
|
219
|
+
return out;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Sets from coplanar points
|
|
224
|
+
*
|
|
225
|
+
* @author three.js (conversion)
|
|
226
|
+
* @param {pln} out - Receiving plane
|
|
227
|
+
* @param {vec3} a - Coplanar point a
|
|
228
|
+
* @param {vec3} b - Coplanar point b
|
|
229
|
+
* @param {vec3} c - Coplanar point c
|
|
230
|
+
* @returns {pln} out - Receiving pln
|
|
231
|
+
*/
|
|
232
|
+
pln.fromCoplanarPoints = function(out, a, b, c)
|
|
233
|
+
{
|
|
234
|
+
let ax = c[0] - b[0],
|
|
235
|
+
ay = c[1] - b[1],
|
|
236
|
+
az = c[2] - b[2],
|
|
237
|
+
bx = a[0] - b[0],
|
|
238
|
+
by = a[1] - b[1],
|
|
239
|
+
bz = a[2] - b[2];
|
|
240
|
+
|
|
241
|
+
// get cross product
|
|
242
|
+
let x = ay * bz - az * by,
|
|
243
|
+
y = az * bx - ax * bz,
|
|
244
|
+
z = ax * by - ay * bx;
|
|
245
|
+
|
|
246
|
+
// normalize
|
|
247
|
+
let len = x * x + y * y + z * z;
|
|
248
|
+
if (len > 0)
|
|
249
|
+
{
|
|
250
|
+
len = 1 / Math.sqrt(len);
|
|
251
|
+
out[0] = x * len;
|
|
252
|
+
out[1] = y * len;
|
|
253
|
+
out[2] = z * len;
|
|
254
|
+
}
|
|
255
|
+
else
|
|
256
|
+
{
|
|
257
|
+
out[0] = 0;
|
|
258
|
+
out[1] = 0;
|
|
259
|
+
out[2] = 0;
|
|
260
|
+
throw new Error("Normalization error");
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// set coplanar point
|
|
264
|
+
out[3] = -(a[0] * out[0] + a[1] * out[1] + a[2] * out[2]);
|
|
265
|
+
return out;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Sets a pln from normal and a coplanar point
|
|
270
|
+
*
|
|
271
|
+
* @param {pln} out - receiving plane
|
|
272
|
+
* @param {vec3} normal - normal
|
|
273
|
+
* @param {vec3} point - coplanar point
|
|
274
|
+
* @returns {pln} out - receiving plane
|
|
275
|
+
*/
|
|
276
|
+
pln.fromNormalAndCoplanarPoint = function(out, normal, point)
|
|
277
|
+
{
|
|
278
|
+
out[0] = normal[0];
|
|
279
|
+
out[1] = normal[1];
|
|
280
|
+
out[2] = normal[2];
|
|
281
|
+
out[3] = -(point[0] * normal[0] + point[1] * normal[1] + point[2] * normal[2]);
|
|
282
|
+
return out;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/**
|
|
286
|
+
* Gets a pln's coplanar point
|
|
287
|
+
*
|
|
288
|
+
* @param {vec3} out - receiving vec3
|
|
289
|
+
* @param {pln} a - the source plane
|
|
290
|
+
* @returns {vec3} out - receiving vec3
|
|
291
|
+
*/
|
|
292
|
+
pln.getCoplanarPoint = function(out, a)
|
|
293
|
+
{
|
|
294
|
+
out[0] = a[0] * -a[3];
|
|
295
|
+
out[1] = a[1] * -a[3];
|
|
296
|
+
out[2] = a[2] * -a[3];
|
|
297
|
+
return out;
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Sets a vec3 with the intersection point of a plane and a Float32Array(6) line
|
|
302
|
+
* - Returns null if there was no intersection, or the receiving vec3 if there was
|
|
303
|
+
*
|
|
304
|
+
* @author Three.js (conversion)
|
|
305
|
+
* @param {vec3} out - receiving vec3
|
|
306
|
+
* @param {pln} a - plane
|
|
307
|
+
* @param {(lne3|Float32Array)} l - line
|
|
308
|
+
* @returns {(null|vec3)} null|out - null or receiving vec3
|
|
309
|
+
*/
|
|
310
|
+
pln.getIntersectLne3 = function(out, a, l)
|
|
311
|
+
{
|
|
312
|
+
let lsx = l[0],
|
|
313
|
+
lsy = l[1],
|
|
314
|
+
lsz = l[2],
|
|
315
|
+
lex = l[3],
|
|
316
|
+
ley = l[4],
|
|
317
|
+
lez = l[5];
|
|
318
|
+
|
|
319
|
+
// Clear the out in case of fails?
|
|
320
|
+
out[0] = 0;
|
|
321
|
+
out[1] = 0;
|
|
322
|
+
out[2] = 0;
|
|
323
|
+
|
|
324
|
+
// Get line delta
|
|
325
|
+
let dirX = lex - lsx,
|
|
326
|
+
dirY = ley - lsy,
|
|
327
|
+
dirZ = lez - lsz;
|
|
328
|
+
|
|
329
|
+
// Get dot of the plane normal and line delta
|
|
330
|
+
let den = a[0] * dirX + a[1] * dirY + a[2] * dirZ;
|
|
331
|
+
|
|
332
|
+
if (den === 0)
|
|
333
|
+
{
|
|
334
|
+
// Check if distance to the line start is 0
|
|
335
|
+
if ((a[0] * lsx + a[1] * lsy + a[2] * lsz) + a[3] === 0)
|
|
336
|
+
{
|
|
337
|
+
out[0] = lsx;
|
|
338
|
+
out[1] = lsy;
|
|
339
|
+
out[2] = lsz;
|
|
340
|
+
return out;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
throw new Error("Denominator error");
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
let t = -((lsx * a[0] + lsy * a[1] + lsz * a[2]) + a[3]) / den;
|
|
347
|
+
|
|
348
|
+
if (t < 0 || t > 1)
|
|
349
|
+
{
|
|
350
|
+
throw new Error("Normalization error");
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
out[0] = (dirX * t) + lsx;
|
|
354
|
+
out[1] = (dirY * t) + lsy;
|
|
355
|
+
out[2] = (dirZ * t) + lsz;
|
|
356
|
+
return out;
|
|
357
|
+
};
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Sets a vec3 with the intersection point of a plane and a 3d line's components
|
|
362
|
+
* Returns null if there was no intersection, or the receiving vec3 if there was
|
|
363
|
+
*
|
|
364
|
+
* @param {vec3} out
|
|
365
|
+
* @param {pln} a
|
|
366
|
+
* @param {vec3} lineStart
|
|
367
|
+
* @param {vec3} lineEnd
|
|
368
|
+
* @returns {vec3}
|
|
369
|
+
*/
|
|
370
|
+
pln.getIntersectStartEnd = function(out, a, lineStart, lineEnd)
|
|
371
|
+
{
|
|
372
|
+
const vec6_0 = box3.alloc();
|
|
373
|
+
box3.from(vec6_0, lineStart, lineEnd);
|
|
374
|
+
pln.getIntersectLne3(out, a, vec6_0);
|
|
375
|
+
box3.unalloc(vec6_0);
|
|
376
|
+
return out;
|
|
377
|
+
};
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Sets a vec3 with the normal component of the pln
|
|
381
|
+
*
|
|
382
|
+
* @param {vec3} out - receiving vec3
|
|
383
|
+
* @param {pln} a - source plane
|
|
384
|
+
* @returns {vec3} out - receiving vec3
|
|
385
|
+
*/
|
|
386
|
+
pln.getNormal = function(out, a)
|
|
387
|
+
{
|
|
388
|
+
out[0] = a[0];
|
|
389
|
+
out[1] = a[1];
|
|
390
|
+
out[2] = a[2];
|
|
391
|
+
return out;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Gets an orthographic point
|
|
396
|
+
*
|
|
397
|
+
* @param {vec3} out - receiving vec3
|
|
398
|
+
* @param {pln} a - the plane to project from
|
|
399
|
+
* @param {vec3} p - the point to project
|
|
400
|
+
* @returns {vec3} out - receiving vec3
|
|
401
|
+
*/
|
|
402
|
+
pln.getOrthoPoint = function(out, a, p)
|
|
403
|
+
{
|
|
404
|
+
let pMag = (a[0] * p[0] + a[1] * p[1] + a[2] * p[2]) + a[3];
|
|
405
|
+
vec3.multiplyScalar(out, a, pMag);
|
|
406
|
+
return out;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Gets a projected point
|
|
411
|
+
*
|
|
412
|
+
* @param {vec3} out - receiving vec3
|
|
413
|
+
* @param {pln} a - the plane to project from
|
|
414
|
+
* @param {vec3} p - the point to project
|
|
415
|
+
* @returns {vec3} out - receiving vec3
|
|
416
|
+
*/
|
|
417
|
+
pln.getProjectedPoint = function(out, a, p)
|
|
418
|
+
{
|
|
419
|
+
pln.getOrthoPoint(out, a, p);
|
|
420
|
+
out[0] = -(out[0] - p[0]);
|
|
421
|
+
out[1] = -(out[1] - p[1]);
|
|
422
|
+
out[2] = -(out[2] - p[2]);
|
|
423
|
+
return out;
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Checks if a plane intersects min and max bounds
|
|
428
|
+
*
|
|
429
|
+
* @param {pln} a - plane to compare
|
|
430
|
+
* @param {vec3} min - box min bounds to compare
|
|
431
|
+
* @param {vec3} max - box max bounds to compare
|
|
432
|
+
* @returns {boolean} - true if intersection occurs
|
|
433
|
+
*/
|
|
434
|
+
pln.intersectsBounds = function(a, min, max)
|
|
435
|
+
{
|
|
436
|
+
const box3_0 = box3.from(box3.alloc(), min, max);
|
|
437
|
+
let result = box3.intersectsPln(box3_0, a);
|
|
438
|
+
box3.unalloc(box3_0);
|
|
439
|
+
return result;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Checks if a plane intersects a Float32Array(6) bounding box
|
|
444
|
+
*
|
|
445
|
+
* @param {pln} a - plane to compare
|
|
446
|
+
* @param {(box3|Float32Array)} b - box to compare
|
|
447
|
+
* @returns {boolean} - true if intersection occurs
|
|
448
|
+
*/
|
|
449
|
+
pln.intersectsBox3 = function(a, b)
|
|
450
|
+
{
|
|
451
|
+
return box3.intersectsPln(b, a);
|
|
452
|
+
};
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Checks if a plane intersects a Float32Array(6) lne3
|
|
456
|
+
*
|
|
457
|
+
* @author three.js (conversion)
|
|
458
|
+
* @param {pln} a - plane to compare
|
|
459
|
+
* @param {lne3|Float32Array} l - line to compare
|
|
460
|
+
* @returns {boolean} - true if intersection occurs
|
|
461
|
+
*/
|
|
462
|
+
pln.intersectsLne3 = function(a, l)
|
|
463
|
+
{
|
|
464
|
+
let startSign = (a[0] * l[0] + a[1] * l[1] + a[2] * l[2]) + a[3];
|
|
465
|
+
let endSign = (a[0] * l[3] + a[1] * l[4] + a[2] * l[5]) + a[3];
|
|
466
|
+
return (startSign < 0 && endSign > 0) || (endSign < 0 && startSign > 0);
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Checks if a plane intersects spherical bounds
|
|
471
|
+
*
|
|
472
|
+
* @param {pln} a - plane to compare
|
|
473
|
+
* @param {vec3} position - sphere position to compare
|
|
474
|
+
* @param {number} radius - sphere radius compare
|
|
475
|
+
* @returns {boolean} - true if intersection occurs
|
|
476
|
+
*/
|
|
477
|
+
pln.intersectsPositionRadius = function(a, position, radius)
|
|
478
|
+
{
|
|
479
|
+
let distance = position[0] * a[0] + position[1] * a[1] + position[2] * a[2] + a[3];
|
|
480
|
+
return Math.abs(distance) <= radius;
|
|
481
|
+
};
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Checks if a plane intersects a sph3
|
|
485
|
+
*
|
|
486
|
+
* @param {pln} a - plane to compare
|
|
487
|
+
* @param {sph3} s - sphere to compare
|
|
488
|
+
* @returns {boolean} - true if intersection occurs
|
|
489
|
+
*/
|
|
490
|
+
pln.intersectsSph3 = function(a, s)
|
|
491
|
+
{
|
|
492
|
+
let distance = s[0] * a[0] + s[1] * a[1] + s[2] * a[2] + a[3];
|
|
493
|
+
return Math.abs(distance) <= s[3];
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Checks if a plane intersects a lne3's components
|
|
498
|
+
*
|
|
499
|
+
* @param {pln} a - plane to compare
|
|
500
|
+
* @param {vec3} start - line start to compare
|
|
501
|
+
* @param {vec3} end - line end to compare
|
|
502
|
+
* @returns {boolean} - true if intersection occurs
|
|
503
|
+
*/
|
|
504
|
+
pln.intersectsStartEnd = function(a, start, end)
|
|
505
|
+
{
|
|
506
|
+
let startSign = (a[0] * start[0] + a[1] * start[1] + a[2] * start[2]) + a[3];
|
|
507
|
+
let endSign = (a[0] * end[0] + a[1] * end[1] + a[2] * end[2]) + a[3];
|
|
508
|
+
return (startSign < 0 && endSign > 0) || (endSign < 0 && startSign > 0);
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Negates a plane
|
|
513
|
+
*
|
|
514
|
+
* @param {pln} out - receiving plane
|
|
515
|
+
* @param {pln} a - the plane to negate
|
|
516
|
+
* @returns {pln} out - receiving plane
|
|
517
|
+
*/
|
|
518
|
+
pln.negate = function(out, a)
|
|
519
|
+
{
|
|
520
|
+
out[0] = -a[0];
|
|
521
|
+
out[1] = -a[1];
|
|
522
|
+
out[2] = -a[2];
|
|
523
|
+
out[3] = a[3] * -1;
|
|
524
|
+
return out;
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Normalizes a plane
|
|
529
|
+
*
|
|
530
|
+
* @param {pln} out - receiving plane
|
|
531
|
+
* @param {pln} a - the plane to normalize
|
|
532
|
+
* @returns {pln} out - receiving plane
|
|
533
|
+
*/
|
|
534
|
+
pln.normalize = function(out, a)
|
|
535
|
+
{
|
|
536
|
+
let x = a[0],
|
|
537
|
+
y = a[1],
|
|
538
|
+
z = a[2];
|
|
539
|
+
|
|
540
|
+
let len = x * x + y * y + z * z;
|
|
541
|
+
|
|
542
|
+
if (len > 0)
|
|
543
|
+
{
|
|
544
|
+
len = 1 / Math.sqrt(len);
|
|
545
|
+
out[0] = a[0] * len;
|
|
546
|
+
out[1] = a[1] * len;
|
|
547
|
+
out[2] = a[2] * len;
|
|
548
|
+
out[3] = a[3] * len;
|
|
549
|
+
}
|
|
550
|
+
else
|
|
551
|
+
{
|
|
552
|
+
out[0] = 0;
|
|
553
|
+
out[1] = 0;
|
|
554
|
+
out[2] = 0;
|
|
555
|
+
throw new Error("Normalization error");
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
return out;
|
|
559
|
+
};
|
|
560
|
+
|
|
561
|
+
|
|
562
|
+
/**
|
|
563
|
+
* Sets a plane from values
|
|
564
|
+
*
|
|
565
|
+
* @param {pln} out
|
|
566
|
+
* @param {number} nX
|
|
567
|
+
* @param {number} nY
|
|
568
|
+
* @param {number} nZ
|
|
569
|
+
* @param {number} c
|
|
570
|
+
* @returns {pln} out
|
|
571
|
+
*/
|
|
572
|
+
pln.set = vec4.set;
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Sets a plane from values and then normalizes the results
|
|
577
|
+
* @param {pln} out
|
|
578
|
+
* @param {Number} nx
|
|
579
|
+
* @param {Number} ny
|
|
580
|
+
* @param {Number} nz
|
|
581
|
+
* @param {Number} c
|
|
582
|
+
* @returns {pln} out
|
|
583
|
+
*/
|
|
584
|
+
pln.setAndNormalize = function(out, nx, ny, nz, c)
|
|
585
|
+
{
|
|
586
|
+
out[0] = nx;
|
|
587
|
+
out[1] = ny;
|
|
588
|
+
out[2] = nz;
|
|
589
|
+
out[3] = c;
|
|
590
|
+
return pln.normalize(out, out);
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* Sets a pln from an array at an optional offset
|
|
596
|
+
*
|
|
597
|
+
* @param {pln} out
|
|
598
|
+
* @param {Array} arr
|
|
599
|
+
* @param {number} [index=0]
|
|
600
|
+
* @returns {pln} out
|
|
601
|
+
*/
|
|
602
|
+
pln.setArray = vec4.setArray;
|
|
603
|
+
|
|
604
|
+
/**
|
|
605
|
+
* Sets an array at an optional offset, with the values of a pln
|
|
606
|
+
*
|
|
607
|
+
* @param {pln} a
|
|
608
|
+
* @param {Array} arr
|
|
609
|
+
* @param {number} [offset = 0]
|
|
610
|
+
* @returns {pln} a
|
|
611
|
+
*/
|
|
612
|
+
pln.toArray = function(a, arr, offset = 0)
|
|
613
|
+
{
|
|
614
|
+
arr[offset] = a[0];
|
|
615
|
+
arr[offset + 1] = a[1];
|
|
616
|
+
arr[offset + 2] = a[2];
|
|
617
|
+
arr[offset + 3] = a[3];
|
|
618
|
+
return a;
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Transforms a plane by a mat4
|
|
623
|
+
*
|
|
624
|
+
* @author three.js (conversion)
|
|
625
|
+
* @param {pln} out - the receiving plane
|
|
626
|
+
* @param {pln} a - the plane to transform
|
|
627
|
+
* @param {mat4} m - the affine matrix to transform with
|
|
628
|
+
* @param {mat3} [nMatrix] - optional normal matrix
|
|
629
|
+
* @returns {pln} out - the receiving plane
|
|
630
|
+
*/
|
|
631
|
+
pln.transformMat4 = function(out, a, m, nMatrix)
|
|
632
|
+
{
|
|
633
|
+
let mat3_0;
|
|
634
|
+
if (!nMatrix)
|
|
635
|
+
{
|
|
636
|
+
mat3_0 = mat3.alloc();
|
|
637
|
+
nMatrix = mat3.normalFromMat4(mat3_0, m);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Coplanar Point
|
|
641
|
+
let cpX = a[0] * -a[3],
|
|
642
|
+
cpY = a[1] * -a[3],
|
|
643
|
+
cpZ = a[2] * -a[3];
|
|
644
|
+
|
|
645
|
+
// Create reference point from Coplanar Point transformed by the affine mat4
|
|
646
|
+
let rX = m[0] * cpX + m[4] * cpY + m[8] * cpZ + m[12],
|
|
647
|
+
rY = m[1] * cpX + m[5] * cpY + m[9] * cpZ + m[13],
|
|
648
|
+
rZ = m[2] * cpX + m[6] * cpY + m[10] * cpZ + m[14];
|
|
649
|
+
|
|
650
|
+
// Transform plane normal by normal matrix
|
|
651
|
+
let nX = a[0],
|
|
652
|
+
nY = a[1],
|
|
653
|
+
nZ = a[2],
|
|
654
|
+
pX = nX * nMatrix[0] + nY * nMatrix[3] + nZ * nMatrix[6],
|
|
655
|
+
pY = nX * nMatrix[1] + nY * nMatrix[4] + nZ * nMatrix[7],
|
|
656
|
+
pZ = nX * nMatrix[2] + nY * nMatrix[5] + nZ * nMatrix[8];
|
|
657
|
+
|
|
658
|
+
if (mat3_0) mat3.unalloc(mat3_0);
|
|
659
|
+
|
|
660
|
+
// Normalize plane normal
|
|
661
|
+
let len = pX * pX + pY * pY + pZ * pZ;
|
|
662
|
+
if (len > 0)
|
|
663
|
+
{
|
|
664
|
+
len = 1 / Math.sqrt(len);
|
|
665
|
+
out[0] = pX * len;
|
|
666
|
+
out[1] = pY * len;
|
|
667
|
+
out[2] = pZ * len;
|
|
668
|
+
}
|
|
669
|
+
else
|
|
670
|
+
{
|
|
671
|
+
out[0] = 0;
|
|
672
|
+
out[1] = 0;
|
|
673
|
+
out[2] = 0;
|
|
674
|
+
throw new Error("Normalization error");
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
// recalculate constant from negative dot of reference point and the resulting plane normal
|
|
678
|
+
out[3] = -(rX * out[0] + rY * out[1] + rZ * out[2]);
|
|
679
|
+
|
|
680
|
+
return out;
|
|
681
|
+
};
|
|
682
|
+
|
|
683
|
+
/**
|
|
684
|
+
* Translates a plane with the given vector
|
|
685
|
+
*
|
|
686
|
+
* @param {pln} out - the receiving plane
|
|
687
|
+
* @param {pln} a - the plane to translate
|
|
688
|
+
* @param {vec3} v - the vector to translate with
|
|
689
|
+
* @returns {pln} out - the receiving plane
|
|
690
|
+
*/
|
|
691
|
+
pln.translate = function(out, a, v)
|
|
692
|
+
{
|
|
693
|
+
out[0] = a[0];
|
|
694
|
+
out[1] = a[1];
|
|
695
|
+
out[2] = a[2];
|
|
696
|
+
out[3] = a[3] - (v[0] * a[0] + v[1] * a[1] + v[2] * a[2]);
|
|
697
|
+
return out;
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
export const {
|
|
701
|
+
alloc,
|
|
702
|
+
unalloc,
|
|
703
|
+
$normal,
|
|
704
|
+
clone,
|
|
705
|
+
constant,
|
|
706
|
+
copy,
|
|
707
|
+
create,
|
|
708
|
+
distanceToPoint,
|
|
709
|
+
distanceToPositionRadius,
|
|
710
|
+
distanceToSph3,
|
|
711
|
+
distanceToValues,
|
|
712
|
+
equals,
|
|
713
|
+
equalsNormalConstant,
|
|
714
|
+
exactEquals,
|
|
715
|
+
exactEqualsNormalConstant,
|
|
716
|
+
extract,
|
|
717
|
+
fromNormalConstant,
|
|
718
|
+
fromCoplanarPoints,
|
|
719
|
+
fromNormalAndCoplanarPoint,
|
|
720
|
+
getCoplanarPoint,
|
|
721
|
+
getIntersectLne3,
|
|
722
|
+
getIntersectStartEnd,
|
|
723
|
+
getNormal,
|
|
724
|
+
getOrthoPoint,
|
|
725
|
+
getProjectedPoint,
|
|
726
|
+
intersectsBounds,
|
|
727
|
+
intersectsBox3,
|
|
728
|
+
intersectsLne3,
|
|
729
|
+
intersectsPositionRadius,
|
|
730
|
+
intersectsSph3,
|
|
731
|
+
intersectsStartEnd,
|
|
732
|
+
negate,
|
|
733
|
+
normalize,
|
|
734
|
+
set,
|
|
735
|
+
setAndNormalize,
|
|
736
|
+
setArray,
|
|
737
|
+
toArray,
|
|
738
|
+
transformMat4,
|
|
739
|
+
translate
|
|
740
|
+
} = pln;
|