@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.
@@ -0,0 +1,88 @@
1
+ import { toJSON } from "./json.js";
2
+ import { vec3 } from "../vec3.js";
3
+
4
+
5
+ export function createTorus(options = {})
6
+ {
7
+ let {
8
+ radius = 1,
9
+ tube = 0.4,
10
+ radialSegments = 12,
11
+ tubularSegments = 49,
12
+ arc = Math.PI * 2
13
+ } = options;
14
+
15
+ radialSegments = Math.floor(radialSegments);
16
+ tubularSegments = Math.floor(tubularSegments);
17
+
18
+ const
19
+ indices = [],
20
+ positions = [],
21
+ uvs = [],
22
+ normals = [];
23
+
24
+ const
25
+ center = vec3.alloc(),
26
+ vertex = vec3.alloc(),
27
+ normal = vec3.alloc();
28
+
29
+ // generate vertices, normals and uvs
30
+ for (let j = 0; j <= radialSegments; j++)
31
+ {
32
+ for (let i = 0; i <= tubularSegments; i++)
33
+ {
34
+ const
35
+ u = i / tubularSegments * arc,
36
+ v = j / radialSegments * Math.PI * 2;
37
+
38
+ // vertex
39
+ vertex[0] = (radius + tube * Math.cos(v)) * Math.cos(u);
40
+ vertex[1] = (radius + tube * Math.cos(v)) * Math.sin(u);
41
+ vertex[2] = tube * Math.sin(v);
42
+ positions.push(vertex[0], vertex[1], vertex[2]);
43
+
44
+ // normal
45
+ center[0] = radius * Math.cos(u);
46
+ center[1] = radius * Math.sin(u);
47
+ vec3.subtract(normal, vertex, center);
48
+ vec3.normalize(normal, normal);
49
+ normals.push(normal[0], normal[1], normal[2]);
50
+
51
+ // uv
52
+ uvs.push(i / tubularSegments);
53
+ uvs.push(j / radialSegments);
54
+ }
55
+ }
56
+
57
+ vec3.unalloc(center);
58
+ vec3.unalloc(vertex);
59
+ vec3.unalloc(normal);
60
+
61
+ // generate indices
62
+ for (let j = 1; j <= radialSegments; j++)
63
+ {
64
+ for (let i = 1; i <= tubularSegments; i++)
65
+ {
66
+ // indices
67
+ const
68
+ a = (tubularSegments + 1) * j + i - 1,
69
+ b = (tubularSegments + 1) * (j - 1) + i - 1,
70
+ c = (tubularSegments + 1) * (j - 1) + i,
71
+ d = (tubularSegments + 1) * j + i;
72
+ // faces
73
+ indices.push(a, b, d);
74
+ indices.push(b, c, d);
75
+ }
76
+ }
77
+
78
+ const result = toJSON(indices, positions, uvs, normals);
79
+ result.factory = createTorus;
80
+ result.options = {
81
+ radius,
82
+ tube,
83
+ radialSegments,
84
+ tubularSegments,
85
+ arc
86
+ };
87
+ return result;
88
+ }
package/src/index.js ADDED
@@ -0,0 +1,29 @@
1
+ export * as num from "./num.js";
2
+ export * as vec2 from "./vec2.js";
3
+ export * as vec3 from "./vec3.js";
4
+ export * as vec4 from "./vec4.js";
5
+ export * as quat from "./quat.js";
6
+ export * as mat3 from "./mat3.js";
7
+ export * as mat4 from "./mat4.js";
8
+
9
+ export * as box3 from "./box3.js";
10
+ export * as tri3 from "./tri3.js";
11
+ export * as lne3 from "./lne3.js";
12
+ export * as pln from "./pln.js";
13
+ export * as ray3 from "./ray3.js";
14
+ export * as sph3 from "./sph3.js";
15
+
16
+ export * as pool from "./pool.js";
17
+ export * as noise from "./noise.js";
18
+ export * as curve from "./curve.js";
19
+
20
+ export * as geometry from "./geometry/index.js";
21
+ export * as vertex from "./vertex.js";
22
+ export * as mesh from "./mesh.js";
23
+ export * as tangent from "./tangent.js";
24
+
25
+
26
+ /**
27
+ * TypedArray
28
+ * @typedef {Float64Array|Float32Array|Uint32Array|Uint16Array|Uint8Array|Uint8ClampedArray|Int32Array|Int16Array|Int8Array} TypedArray
29
+ */
package/src/lne3.js ADDED
@@ -0,0 +1,488 @@
1
+ import { vec3 } from "./vec3.js";
2
+ import { box3 } from "./box3.js";
3
+ import { pool } from "./pool.js";
4
+
5
+ /**
6
+ * 3D Line
7
+ * @typedef {Float32Array} lne3
8
+ */
9
+ export const lne3 = {};
10
+
11
+ /**
12
+ * Allocates a pooled lne3
13
+ * @returns {Float32Array|lne3}
14
+ */
15
+ lne3.alloc = function()
16
+ {
17
+ return pool.allocF32(6);
18
+ };
19
+
20
+ /**
21
+ * Unallocates a pooled lne3
22
+ * @param {Float32Array|lne3} a
23
+ */
24
+ lne3.unalloc = function(a)
25
+ {
26
+ pool.freeType(a);
27
+ };
28
+
29
+ /**
30
+ * Line3 End methods
31
+ *
32
+ * @param {lne3} a
33
+ * @returns {TypedArray}
34
+ */
35
+ lne3.$end = function (a)
36
+ {
37
+ return a.subarray(3, 6);
38
+ };
39
+
40
+ /**
41
+ * Line3 start helper methods
42
+ *
43
+ * @param {lne3} a
44
+ * @returns {TypedArray}
45
+ */
46
+ lne3.$start = function (a)
47
+ {
48
+ return a.subarray(0, 3);
49
+ };
50
+
51
+ /**
52
+ * Clones a lne3
53
+ *
54
+ * @param {lne3} a
55
+ * @returns {lne3}
56
+ */
57
+ lne3.clone = function (a)
58
+ {
59
+ let out = new Float32Array(6);
60
+ out[0] = a[0];
61
+ out[1] = a[1];
62
+ out[2] = a[2];
63
+ out[3] = a[3];
64
+ out[4] = a[4];
65
+ out[5] = a[5];
66
+ return out;
67
+ };
68
+
69
+ /**
70
+ * Returns a point parameter based on the closest point as projected on the line segment.
71
+ * If clamp to line is true, then the returned value will be between 0 and 1
72
+ *
73
+ * @author three.js authors (converted)
74
+ * @param {vec3} a - source lne3
75
+ * @param {vec3} point - point to compare
76
+ * @param {boolean} clampToLine - optional setting to clamp the result to the lne3
77
+ * @returns {number} - closest point parameter
78
+ */
79
+ lne3.closestPointToPointParameter = function(a, point, clampToLine)
80
+ {
81
+ const
82
+ startP = vec3.alloc(),
83
+ startEnd = vec3.alloc();
84
+
85
+ startP[0] = point[0] - a[0];
86
+ startP[1] = point[1] - a[1];
87
+ startP[2] = point[2] - a[2];
88
+
89
+ startEnd[0] = a[3] - a[0];
90
+ startEnd[1] = a[4] - a[1];
91
+ startEnd[2] = a[5] - a[2];
92
+
93
+ let startEnd2 = startEnd[0] * startEnd[0] + startEnd[1] * startEnd[1] + startEnd[2] * startEnd[2],
94
+ startEnd_startP = startEnd[0] * startP[0] + startEnd[1] * startP[1] + startEnd[2] * startP[2],
95
+ t = startEnd_startP / startEnd2;
96
+
97
+ vec3.unalloc(startP);
98
+ vec3.unalloc(startEnd);
99
+
100
+ if (clampToLine) t = Math.max(0, Math.min(1, t));
101
+ return t;
102
+
103
+ };
104
+
105
+ /**
106
+ * Copies the values from one lne3 into another
107
+ *
108
+ * @param {lne3} out
109
+ * @param {lne3} a
110
+ * @returns {lne3} out
111
+ */
112
+ lne3.copy = function (out, a)
113
+ {
114
+ out[0] = a[0];
115
+ out[1] = a[1];
116
+ out[2] = a[2];
117
+ out[3] = a[3];
118
+ out[4] = a[4];
119
+ out[5] = a[5];
120
+ return out;
121
+ };
122
+
123
+ /**
124
+ * Copies the start component from one lne3 into another
125
+ *
126
+ * @param {lne3} out
127
+ * @param {lne3} a
128
+ * @returns {lne3} out
129
+ */
130
+ lne3.copyStart = function (out, a)
131
+ {
132
+ out[0] = a[0];
133
+ out[1] = a[1];
134
+ out[2] = a[2];
135
+ return out;
136
+ };
137
+
138
+ /**
139
+ * Copies the end component from one lne3 into another
140
+ *
141
+ * @param {lne3} out
142
+ * @param {lne3} a
143
+ * @returns {lne3} out
144
+ */
145
+ lne3.copyEnd =function (out, a)
146
+ {
147
+ out[3] = a[3];
148
+ out[4] = a[4];
149
+ out[5] = a[5];
150
+ return out;
151
+ };
152
+
153
+ /**
154
+ * Creates a lne3
155
+ *
156
+ * @returns {lne3}
157
+ */
158
+ lne3.create = function ()
159
+ {
160
+ return new Float32Array(6);
161
+ };
162
+
163
+ /**
164
+ * Checks two lne3's for equality
165
+ *
166
+ * @param {lne3} a
167
+ * @param {lne3} b
168
+ * @returns {boolean}
169
+ */
170
+ lne3.equals = box3.equals;
171
+
172
+ /**
173
+ * Checks a lne3 against it's components for equality
174
+ *
175
+ * @param {lne3} a
176
+ * @param {vec3} min
177
+ * @param {vec3} max
178
+ * @returns {boolean}
179
+ */
180
+ lne3.equalsStartEnd = box3.equalsBounds;
181
+
182
+ /**
183
+ * Checks for box3 exact equality
184
+ *
185
+ * @param {lne3} a
186
+ * @param {lne3} b
187
+ * @returns {boolean}
188
+ */
189
+ lne3.exactEquals = box3.exactEquals;
190
+
191
+ /**
192
+ * Checks for exact equality between a lne3 and components
193
+ *
194
+ * @param {lne3} a
195
+ * @param {vec3} min
196
+ * @param {vec3} max
197
+ * @returns {boolean}
198
+ */
199
+ lne3.exactEqualsStartEnd = box3.exactEqualsBounds;
200
+
201
+ /**
202
+ * Sets a lne3 from an array at an optional offset
203
+ *
204
+ * @param {lne3} out
205
+ * @param {Array} arr
206
+ * @param {number} [index=0]
207
+ * @returns {lne3}
208
+ */
209
+ lne3.fromArray = box3.fromArray;
210
+
211
+ /**
212
+ * Sets a line from start and end components
213
+ *
214
+ * @param {lne3} out
215
+ * @param {vec3} start
216
+ * @param {vec3} end
217
+ * @returns {lne3} out
218
+ */
219
+ lne3.fromStartEnd = box3.fromBounds;
220
+
221
+ /**
222
+ * Returns a vector at a certain position along a lne3
223
+ *
224
+ * @author three.js authors (converted)
225
+ * @param {vec3} out - receiving vec3
226
+ * @param {lne3} a - source lne3
227
+ * @param {number} t - Float representing the start (0) and end (1) of the line
228
+ * @returns {vec3} [out] - receiving vec3
229
+ */
230
+ lne3.get = function(out, a, t)
231
+ {
232
+ if (t < 0 || t > 1)
233
+ {
234
+ out[0] = 0;
235
+ out[1] = 0;
236
+ out[2] = 0;
237
+ throw new Error("Normalization error");
238
+ }
239
+ else
240
+ {
241
+ out[0] = (a[3] - a[0]) * t + a[0];
242
+ out[1] = (a[4] - a[1]) * t + a[1];
243
+ out[2] = (a[5] - a[2]) * t + a[2];
244
+ }
245
+ return out;
246
+ };
247
+
248
+ /**
249
+ * Sets a vec3 from the lne3's center
250
+ *
251
+ * @param {vec3} out - receiving vec3
252
+ * @param {lne3} a - source lne3
253
+ * @returns {vec3} out - receiving vec3
254
+ */
255
+ lne3.getCenter = function(out, a)
256
+ {
257
+ out[0] = (a[0] + a[3]) * 0.5;
258
+ out[1] = (a[1] + a[4]) * 0.5;
259
+ out[2] = (a[2] + a[5]) * 0.5;
260
+ return out;
261
+ };
262
+
263
+ /**
264
+ * Returns the closets point on a lne3 to a given point.
265
+ * - If clamp to line is true, then the returned value will be clamped to the line segment.
266
+ *
267
+ * @author three.js authors (converted)
268
+ * @param {vec3} out - receiving vec3
269
+ * @param {lne3} a - source lne3
270
+ * @param {vec3} point - point to compare
271
+ * @param {boolean} clampToLine - optional setting to clamp the result to a line segment
272
+ * @returns {vec3} out - receiving vec3
273
+ */
274
+ lne3.getClosestPointToPoint = function(out, a, point, clampToLine)
275
+ {
276
+ // const x = a.subarray(0, 3);
277
+ return lne3.get(out, a, lne3.closestPointToPointParameter(a, point, clampToLine));
278
+ };
279
+
280
+ /**
281
+ * Gets the end component of a lne3
282
+ *
283
+ * @param {vec3} out
284
+ * @param {lne3} a
285
+ * @returns {vec3} out
286
+ */
287
+ lne3.getEnd = box3.getMax;
288
+
289
+ /**
290
+ * Gets a lne3's delta
291
+ *
292
+ * @param {vec3} out - receiving vec3
293
+ * @param {lne3} a - source lne3
294
+ * @returns {vec3} out - receiving vec3
295
+ */
296
+ lne3.getDelta = function(out, a)
297
+ {
298
+ out[0] = a[3] - a[0];
299
+ out[1] = a[4] - a[1];
300
+ out[2] = a[5] - a[2];
301
+ return out;
302
+ };
303
+
304
+ /**
305
+ * Gets the start component of a lne3
306
+ *
307
+ * @param {vec3} out
308
+ * @param {lne3} a
309
+ * @returns {vec3} out
310
+ */
311
+ lne3.getStart = box3.getMin;
312
+
313
+ /**
314
+ * Checks for intersection with a plane's components
315
+ *
316
+ * @param {lne3} a - receiving lne3
317
+ * @param {vec3} n - plane normal
318
+ * @param {number} c - plane constant
319
+ * @returns {boolean}
320
+ */
321
+ lne3.intersectsNormalConstant = function(a, n, c)
322
+ {
323
+ const
324
+ startSign = (n[0] * a[0] + n[1] * a[1] + n[2] * a[2]) + c,
325
+ endSign = (n[0] * a[3] + n[1] * a[4] + n[2] * a[5]) + c;
326
+
327
+ return (startSign < 0 && endSign > 0) || (endSign < 0 && startSign > 0);
328
+ };
329
+
330
+ /**
331
+ * Gets the length of the line
332
+ *
333
+ * @param {lne3} a - source lne3
334
+ * @returns {number} - distance
335
+ */
336
+ lne3.length = function(a)
337
+ {
338
+ let x = a[0] - a[3],
339
+ y = a[1] - a[4],
340
+ z = a[2] - a[5];
341
+
342
+ return Math.sqrt(x * x + y * y + z * z);
343
+ };
344
+
345
+ /**
346
+ * Sets a lne3 from values
347
+ *
348
+ * @param out - Receiving lne3
349
+ * @param sX
350
+ * @param sY
351
+ * @param sZ
352
+ * @param eX
353
+ * @param eY
354
+ * @param eZ
355
+ * @returns {lne3}
356
+ */
357
+ lne3.set = box3.set;
358
+
359
+ /**
360
+ * Gets the squared length of the lne3
361
+ *
362
+ * @param {lne3} a - source line
363
+ * @returns {number} - squared distance
364
+ */
365
+ lne3.squaredLength = function(a)
366
+ {
367
+ let x = a[0] - a[3],
368
+ y = a[1] - a[4],
369
+ z = a[2] - a[5];
370
+
371
+ return x * x + y * y + z * z;
372
+ };
373
+
374
+ /**
375
+ * Sets an array from the lne3
376
+ *
377
+ * @param {lne3} a
378
+ * @param {Array} arr
379
+ * @param {number} [index]
380
+ * @returns {lne3} a
381
+ */
382
+ lne3.toArray = box3.toArray;
383
+
384
+ /**
385
+ * Sets a start and end vector from a lne3
386
+ *
387
+ * @param {lne3} a
388
+ * @param {vec3} start
389
+ * @param {vec3} end
390
+ * @returns {lne3} a
391
+ */
392
+ lne3.toStartEnd = box3.toBounds;
393
+
394
+ /**
395
+ * Sets the line start from a vector
396
+ * @param {lne3} out
397
+ * @param {vec3} v
398
+ */
399
+ lne3.setStart = box3.setMinBounds;
400
+
401
+ /**
402
+ * Sets the line end from a vector
403
+ * @param {lne3} out
404
+ * @param {vec3} v
405
+ */
406
+ lne3.setEnd = box3.setMaxBounds;
407
+
408
+ /**
409
+ * Transforms a lne3 by a mat4
410
+ *
411
+ * @param {lne3} out
412
+ * @param {lne3} a
413
+ * @param {mat4} m
414
+ * @returns {lne3} out
415
+ */
416
+ lne3.transformMat4 = function(out, a, m)
417
+ {
418
+ let ax = a[0],
419
+ ay = a[1],
420
+ az = a[2],
421
+ bx = a[3],
422
+ by = a[4],
423
+ bz = a[5];
424
+
425
+ out[0] = m[0] * ax + m[4] * ay + m[8] * az + m[12];
426
+ out[1] = m[1] * ax + m[5] * ay + m[9] * az + m[13];
427
+ out[2] = m[2] * ax + m[6] * ay + m[10] * az + m[14];
428
+
429
+ out[3] = m[0] * bx + m[4] * by + m[8] * bz + m[12];
430
+ out[4] = m[1] * bx + m[5] * by + m[9] * bz + m[13];
431
+ out[5] = m[2] * bx + m[6] * by + m[10] * bz + m[14];
432
+
433
+ return out;
434
+ };
435
+
436
+ /**
437
+ * Translates a lne3
438
+ *
439
+ * @param {lne3} out
440
+ * @param {lne3} a
441
+ * @param {vec3} v
442
+ * @returns {lne3} out
443
+ */
444
+ lne3.translate = function(out, a, v)
445
+ {
446
+ out[0] = a[0] + v[0];
447
+ out[1] = a[1] + v[1];
448
+ out[2] = a[2] + v[2];
449
+ out[3] = a[3] + v[0];
450
+ out[4] = a[4] + v[1];
451
+ out[5] = a[5] + v[2];
452
+ return out;
453
+ };
454
+
455
+ export const {
456
+ alloc,
457
+ unalloc,
458
+ $end,
459
+ $start,
460
+ clone,
461
+ closestPointToPointParameter,
462
+ copy,
463
+ copyStart,
464
+ copyEnd,
465
+ create,
466
+ equals,
467
+ equalsStartEnd,
468
+ exactEquals,
469
+ exactEqualsStartEnd,
470
+ fromArray,
471
+ fromStartEnd,
472
+ get,
473
+ getCenter,
474
+ getClosestPointToPoint,
475
+ getEnd,
476
+ getDelta,
477
+ getStart,
478
+ intersectsNormalConstant,
479
+ length,
480
+ set,
481
+ squaredLength,
482
+ toArray,
483
+ toStartEnd,
484
+ setStart,
485
+ setEnd,
486
+ transformMat4,
487
+ translate
488
+ } = lne3;
package/src/mat3.js ADDED
@@ -0,0 +1,131 @@
1
+ import { mat3 as glMat3 } from "gl-matrix";
2
+ import { pool } from "./pool.js";
3
+
4
+ const mat3 = { ...glMat3 };
5
+
6
+ export { mat3 };
7
+
8
+ /**
9
+ * Allocates a pooled mat3
10
+ * @returns {Float32Array|mat3}
11
+ */
12
+ mat3.alloc = function()
13
+ {
14
+ return pool.allocF32(9);
15
+ };
16
+
17
+ /**
18
+ * Unallocates a pooled mat3
19
+ * @param {mat3|Float32Array} a
20
+ */
21
+ mat3.unalloc = function(a)
22
+ {
23
+ pool.freeType(a);
24
+ };
25
+
26
+ /**
27
+ * Sets a mat3 from an array at an option offset
28
+ *
29
+ * @param {mat3} out
30
+ * @param {Array} arr
31
+ * @param {number} [offset=0]
32
+ * @returns {mat3} out
33
+ */
34
+ mat3.fromArray = function(out, arr, offset = 0)
35
+ {
36
+ out[0] = arr[offset];
37
+ out[1] = arr[offset + 1];
38
+ out[2] = arr[offset + 2];
39
+ out[3] = arr[offset + 3];
40
+ out[4] = arr[offset + 4];
41
+ out[5] = arr[offset + 5];
42
+ out[6] = arr[offset + 6];
43
+ out[7] = arr[offset + 7];
44
+ out[8] = arr[offset + 8];
45
+ return out;
46
+ };
47
+
48
+ /**
49
+ * Sets a mat3 from an array at an option offset
50
+ *
51
+ * @param {mat3} out
52
+ * @param {Array} arr
53
+ * @param {number} [index=0]
54
+ * @returns {mat3} out
55
+ */
56
+ mat3.setArray = function(out, arr, index = 0)
57
+ {
58
+ out[0] = arr[index];
59
+ out[1] = arr[index + 1];
60
+ out[2] = arr[index + 2];
61
+ out[3] = arr[index + 3];
62
+ out[4] = arr[index + 4];
63
+ out[5] = arr[index + 5];
64
+ out[6] = arr[index + 6];
65
+ out[7] = arr[index + 7];
66
+ out[8] = arr[index + 8];
67
+ return out;
68
+ };
69
+
70
+ /**
71
+ * Sets an array at an optional offset
72
+ *
73
+ * @param {mat3} a
74
+ * @param {Array} arr
75
+ * @param {number} [index=0]
76
+ * @returns {mat3} a
77
+ */
78
+ mat3.toArray = function(a, arr, index = 0)
79
+ {
80
+ arr[index] = a[0];
81
+ arr[index + 1] = a[1];
82
+ arr[index + 2] = a[2];
83
+ arr[index + 3] = a[3];
84
+ arr[index + 4] = a[4];
85
+ arr[index + 5] = a[5];
86
+ arr[index + 6] = a[6];
87
+ arr[index + 7] = a[7];
88
+ arr[index + 8] = a[8];
89
+ return a;
90
+ };
91
+
92
+ export const {
93
+ add,
94
+ adjoint,
95
+ clone,
96
+ copy,
97
+ create,
98
+ determinant,
99
+ equals,
100
+ exactEquals,
101
+ frob,
102
+ fromMat2d,
103
+ fromMat4,
104
+ fromQuat,
105
+ fromRotation,
106
+ fromScaling,
107
+ fromTranslation,
108
+ fromValues,
109
+ identity,
110
+ invert,
111
+ mul,
112
+ multiply,
113
+ multiplyScalar,
114
+ multiplyScalarAndAdd,
115
+ normalFromMat4,
116
+ projection,
117
+ rotate,
118
+ scale,
119
+ set,
120
+ str,
121
+ sub,
122
+ subtract,
123
+ translate,
124
+ transpose,
125
+ alloc,
126
+ unalloc,
127
+ fromArray,
128
+ setArray,
129
+ toArray
130
+ } = mat3;
131
+