@bitbybit-dev/base 1.6.0 → 1.6.2

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.
Files changed (38) hide show
  1. package/lib/api/GlobalCDNProvider.js +1 -1
  2. package/lib/api/inputs/base-inputs.d.ts +25 -2
  3. package/lib/api/inputs/color-inputs.d.ts +54 -1
  4. package/lib/api/inputs/color-inputs.js +73 -1
  5. package/lib/api/inputs/frame-inputs.d.ts +2 -1
  6. package/lib/api/inputs/frame-inputs.js +2 -1
  7. package/lib/api/inputs/math-inputs.d.ts +62 -0
  8. package/lib/api/inputs/math-inputs.js +82 -0
  9. package/lib/api/inputs/mesh-inputs.d.ts +16 -0
  10. package/lib/api/inputs/mesh-inputs.js +14 -0
  11. package/lib/api/inputs/transforms-inputs.d.ts +76 -0
  12. package/lib/api/inputs/transforms-inputs.js +85 -0
  13. package/lib/api/resolved-inputs/index.d.ts +10 -0
  14. package/lib/api/services/color.d.ts +36 -2
  15. package/lib/api/services/color.js +59 -9
  16. package/lib/api/services/helpers/color/color-forms.constants.d.ts +7 -0
  17. package/lib/api/services/helpers/color/color-forms.constants.js +7 -0
  18. package/lib/api/services/helpers/color/color-forms.d.ts +2 -0
  19. package/lib/api/services/helpers/color/color-forms.js +24 -0
  20. package/lib/api/services/helpers/geometry/quaternions.constants.d.ts +3 -0
  21. package/lib/api/services/helpers/geometry/quaternions.constants.js +3 -0
  22. package/lib/api/services/helpers/geometry/quaternions.d.ts +14 -0
  23. package/lib/api/services/helpers/geometry/quaternions.js +71 -0
  24. package/lib/api/services/helpers/geometry/scalars.constants.d.ts +1 -0
  25. package/lib/api/services/helpers/geometry/scalars.constants.js +1 -0
  26. package/lib/api/services/helpers/geometry/scalars.d.ts +3 -0
  27. package/lib/api/services/helpers/geometry/scalars.js +29 -1
  28. package/lib/api/services/helpers/mesh/mesh-proximity.constants.d.ts +1 -0
  29. package/lib/api/services/helpers/mesh/mesh-proximity.constants.js +1 -0
  30. package/lib/api/services/helpers/mesh/mesh-proximity.d.ts +1 -0
  31. package/lib/api/services/helpers/mesh/mesh-proximity.js +40 -40
  32. package/lib/api/services/math.d.ts +35 -0
  33. package/lib/api/services/math.js +56 -1
  34. package/lib/api/services/mesh.d.ts +17 -0
  35. package/lib/api/services/mesh.js +27 -1
  36. package/lib/api/services/transforms.d.ts +119 -1
  37. package/lib/api/services/transforms.js +158 -30
  38. package/package.json +1 -1
@@ -1,7 +1,7 @@
1
1
  import * as Inputs from "../inputs";
2
2
  import { InputError, resolveDto } from "../kernel-calls";
3
3
  import { signedVolumeOf } from "./helpers/mesh/mesh-measures";
4
- import { pointInTree, treesNested, treesWithin, triangleTree } from "./helpers/mesh/mesh-proximity";
4
+ import { closestOnTriangle, pointInTree, treesNested, treesWithin, triangleTree } from "./helpers/mesh/mesh-proximity";
5
5
  import { triangulateFace } from "./helpers/geometry/triangulation";
6
6
  import { frustum, icosphere } from "./helpers/mesh/mesh-primitives";
7
7
  import { axesAlong, isTriple, pointToWorld } from "./helpers/geometry/frame-axes";
@@ -80,6 +80,32 @@ export class MeshBitByBit {
80
80
  signedDistanceToPlane(inputs) {
81
81
  return this.vector.dot({ first: inputs.plane.normal, second: inputs.point }) - inputs.plane.d;
82
82
  }
83
+ /**
84
+ * Finds the point of a triangle nearest to another point, with the weight of each corner in it.
85
+ *
86
+ * Each corner times its weight, added up, gives the point; the weights add up to 1. Corners on one
87
+ * line act as the segment they span.
88
+ * Example: point [0.25,0.25,2] and triangle [[0,0,0], [1,0,0], [0,1,0]] -> { point: [0.25,0.25,0], weights: [0.5,0.25,0.25] }
89
+ * @param inputs - The point and the triangle
90
+ * @returns The nearest point and the weight of each corner in it
91
+ * @group traingle
92
+ * @shortname closest point on triangle
93
+ * @drawable false
94
+ * @example
95
+ * ```typescript
96
+ * const nearest = bitbybit.mesh.closestPointOnTriangle({ point: [0.25, 0.25, 2], triangle: [[0, 0, 0], [1, 0, 0], [0, 1, 0]] });
97
+ * ```
98
+ */
99
+ closestPointOnTriangle(inputs) {
100
+ if (!isTriple(inputs.point)) {
101
+ throw new InputError("`point` has to be three finite numbers.", "point");
102
+ }
103
+ if (!Array.isArray(inputs.triangle) || inputs.triangle.length !== TRIANGLE_CORNERS || !inputs.triangle.every(isTriple)) {
104
+ throw new InputError("`triangle` has to be three corners of three finite numbers each.", "triangle");
105
+ }
106
+ const [a, b, c] = inputs.triangle;
107
+ return closestOnTriangle(inputs.point, a, b, c);
108
+ }
83
109
  /**
84
110
  * Finds the plane a triangle lies in: its unit normal and its distance from the origin along
85
111
  * that normal.
@@ -301,6 +301,125 @@ export declare class Transforms {
301
301
  * @drawable false
302
302
  */
303
303
  identity(): Base.TransformMatrix;
304
+ /**
305
+ * Builds a quaternion that turns about an axis by an angle in degrees, positive
306
+ * counter-clockwise when the axis points toward you.
307
+ *
308
+ * A quaternion holds a rotation as four numbers with none of the gimbal lock of three angles;
309
+ * glTF and the renderers store rotations this way.
310
+ * Example: axis [0,0,1], angle 90 -> [0, 0, 0.7071, 0.7071]
311
+ * @param inputs - The axis and the angle in degrees
312
+ * @returns The rotation as [x, y, z, w]
313
+ * @group quaternion
314
+ * @shortname quaternion axis angle
315
+ * @drawable false
316
+ * @example
317
+ * ```typescript
318
+ * const quarter = bitbybit.transforms.quaternionFromAxisAngle({ axis: [0, 0, 1], angle: 90 });
319
+ * ```
320
+ */
321
+ quaternionFromAxisAngle(inputs: Inputs.Transforms.AxisAngleDto): Base.Quaternion;
322
+ /**
323
+ * Builds a quaternion from three angles in degrees: a turn about the X axis, then about Y, then
324
+ * about Z, each about the fixed world axes, as `rotationCenterX`, `rotationCenterY` and
325
+ * `rotationCenterZ` applied in that order.
326
+ *
327
+ * Example: [0, 0, 90] -> [0, 0, 0.7071, 0.7071]
328
+ * @param inputs - The angles about X, Y and Z
329
+ * @returns The rotation as [x, y, z, w]
330
+ * @group quaternion
331
+ * @shortname quaternion from euler
332
+ * @drawable false
333
+ * @example
334
+ * ```typescript
335
+ * const turn = bitbybit.transforms.quaternionFromEuler({ angles: [30, 0, 90] });
336
+ * ```
337
+ */
338
+ quaternionFromEuler(inputs: Inputs.Transforms.EulerAnglesDto): Base.Quaternion;
339
+ /**
340
+ * Reads a quaternion back as the three angles in degrees that `quaternionFromEuler` takes,
341
+ * about X, then Y, then Z.
342
+ *
343
+ * Y stays between -90 and 90. Within a ten-thousandth of a degree of either end, X and Z turn
344
+ * about one line, so X is 0 and Z takes the whole turn.
345
+ * Example: [0, 0, 0.7071, 0.7071] -> [0, 0, 90]
346
+ * @param inputs - The rotation
347
+ * @returns The angles about X, Y and Z
348
+ * @group quaternion
349
+ * @shortname quaternion to euler
350
+ * @drawable false
351
+ * @example
352
+ * ```typescript
353
+ * const angles = bitbybit.transforms.quaternionToEuler({ quaternion: [0, 0, 0.7071, 0.7071] });
354
+ * ```
355
+ */
356
+ quaternionToEuler(inputs: Inputs.Transforms.QuaternionDto): Base.Vector3;
357
+ /**
358
+ * Combines two rotations into the one that turns by `first` and then by `second`.
359
+ *
360
+ * Order matters: turning about X then Z differs from turning about Z then X.
361
+ * Example: a quarter turn about X, then a quarter turn about Z -> one rotation doing both
362
+ * @param inputs - The rotation applied first and the one applied after it
363
+ * @returns The combined rotation as [x, y, z, w]
364
+ * @group quaternion
365
+ * @shortname quaternion combine
366
+ * @drawable false
367
+ * @example
368
+ * ```typescript
369
+ * const both = bitbybit.transforms.quaternionCombine({ first: [0.7071, 0, 0, 0.7071], second: [0, 0, 0.7071, 0.7071] });
370
+ * ```
371
+ */
372
+ quaternionCombine(inputs: Inputs.Transforms.TwoQuaternionsDto): Base.Quaternion;
373
+ /**
374
+ * Turns a vector, or a point about the origin, by a rotation.
375
+ *
376
+ * Example: [1, 0, 0] turned by a quarter turn about Z -> [0, 1, 0]
377
+ * @param inputs - The rotation and the vector
378
+ * @returns The turned vector
379
+ * @group quaternion
380
+ * @shortname quaternion rotate vector
381
+ * @drawable false
382
+ * @example
383
+ * ```typescript
384
+ * const turned = bitbybit.transforms.quaternionRotateVector({ quaternion: [0, 0, 0.7071, 0.7071], vector: [1, 0, 0] });
385
+ * ```
386
+ */
387
+ quaternionRotateVector(inputs: Inputs.Transforms.QuaternionVectorDto): Base.Vector3;
388
+ /**
389
+ * Gives the rotation that turns the world axes onto a frame's: X onto its direction and Z onto
390
+ * its normal. The origin is left out.
391
+ *
392
+ * The direction is squared to the normal first, so a rough direction is enough.
393
+ * Example: normal [0,0,1], direction [0,1,0] -> a quarter turn about Z
394
+ * @param inputs - The frame
395
+ * @returns The rotation as [x, y, z, w]
396
+ * @group quaternion
397
+ * @shortname quaternion from frame
398
+ * @drawable false
399
+ * @example
400
+ * ```typescript
401
+ * const turn = bitbybit.transforms.quaternionFromFrame({ frame: { origin: [0, 0, 0], normal: [0, 0, 1], direction: [0, 1, 0] } });
402
+ * ```
403
+ */
404
+ quaternionFromFrame(inputs: Inputs.Frame.FrameDto): Base.Quaternion;
405
+ /**
406
+ * Writes a rotation as the transformation matrix the other methods here build, so it can be
407
+ * applied with `point.transformPoints` or combined with moves.
408
+ *
409
+ * Example: a quarter turn about Z -> the matrix that takes [1,0,0] to [0,1,0]
410
+ * @param inputs - The rotation
411
+ * @returns The matrix, 16 numbers in column-major order
412
+ * @group quaternion
413
+ * @shortname quaternion to matrix
414
+ * @drawable false
415
+ * @example
416
+ * ```typescript
417
+ * const matrix = bitbybit.transforms.quaternionToMatrix({ quaternion: [0, 0, 0.7071, 0.7071] });
418
+ * const points = bitbybit.point.transformPoints({ points: [[1, 0, 0]], transformation: [matrix] });
419
+ * ```
420
+ */
421
+ quaternionToMatrix(inputs: Inputs.Transforms.QuaternionDto): Base.TransformMatrix;
422
+ private unitQuaternionOf;
304
423
  private translation;
305
424
  private scaling;
306
425
  private rotationAxis;
@@ -308,7 +427,6 @@ export declare class Transforms {
308
427
  private rotationY;
309
428
  private rotationZ;
310
429
  private rotationYawPitchRoll;
311
- private rotationMatrixFromQuat;
312
430
  private stretchDirection;
313
431
  private matricesOf;
314
432
  }
@@ -1,6 +1,8 @@
1
1
  import * as Inputs from "../inputs";
2
2
  import { InputError, resolveDto } from "../kernel-calls";
3
3
  import { composed, isRigidMatrix, isTransformMatrix, linearDeterminant, rigidInverse } from "./helpers/geometry/matrices";
4
+ import { degreesOfQuaternion, isQuaternion, quaternionAbout, quaternionMatrix, quaternionOfAxes, quaternionOfDegrees, quaternionProduct, quaternionTurned, unitQuaternion } from "./helpers/geometry/quaternions";
5
+ import { isTriple, squareFrame, unitOf } from "./helpers/geometry/frame-axes";
4
6
  /**
5
7
  * Builds transformation matrices for moving, rotating, scaling and stretching geometry. A
6
8
  * transformation is a 4x4 matrix as 16 numbers in column-major order; most methods return a short
@@ -387,6 +389,161 @@ export class Transforms {
387
389
  identity() {
388
390
  return [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0];
389
391
  }
392
+ /**
393
+ * Builds a quaternion that turns about an axis by an angle in degrees, positive
394
+ * counter-clockwise when the axis points toward you.
395
+ *
396
+ * A quaternion holds a rotation as four numbers with none of the gimbal lock of three angles;
397
+ * glTF and the renderers store rotations this way.
398
+ * Example: axis [0,0,1], angle 90 -> [0, 0, 0.7071, 0.7071]
399
+ * @param inputs - The axis and the angle in degrees
400
+ * @returns The rotation as [x, y, z, w]
401
+ * @group quaternion
402
+ * @shortname quaternion axis angle
403
+ * @drawable false
404
+ * @example
405
+ * ```typescript
406
+ * const quarter = bitbybit.transforms.quaternionFromAxisAngle({ axis: [0, 0, 1], angle: 90 });
407
+ * ```
408
+ */
409
+ quaternionFromAxisAngle(inputs) {
410
+ const resolved = resolveDto(Inputs.Transforms.AxisAngleDto, inputs);
411
+ const axis = unitOf(resolved.axis);
412
+ if (axis === undefined || !Number.isFinite(resolved.angle)) {
413
+ throw new InputError("`axis` has to be three finite numbers not all 0, and `angle` a finite number.", axis === undefined ? "axis" : "angle");
414
+ }
415
+ return quaternionAbout(axis, resolved.angle);
416
+ }
417
+ /**
418
+ * Builds a quaternion from three angles in degrees: a turn about the X axis, then about Y, then
419
+ * about Z, each about the fixed world axes, as `rotationCenterX`, `rotationCenterY` and
420
+ * `rotationCenterZ` applied in that order.
421
+ *
422
+ * Example: [0, 0, 90] -> [0, 0, 0.7071, 0.7071]
423
+ * @param inputs - The angles about X, Y and Z
424
+ * @returns The rotation as [x, y, z, w]
425
+ * @group quaternion
426
+ * @shortname quaternion from euler
427
+ * @drawable false
428
+ * @example
429
+ * ```typescript
430
+ * const turn = bitbybit.transforms.quaternionFromEuler({ angles: [30, 0, 90] });
431
+ * ```
432
+ */
433
+ quaternionFromEuler(inputs) {
434
+ const resolved = resolveDto(Inputs.Transforms.EulerAnglesDto, inputs);
435
+ if (!isTriple(resolved.angles)) {
436
+ throw new InputError("`angles` has to be three finite numbers.", "angles");
437
+ }
438
+ return quaternionOfDegrees(resolved.angles);
439
+ }
440
+ /**
441
+ * Reads a quaternion back as the three angles in degrees that `quaternionFromEuler` takes,
442
+ * about X, then Y, then Z.
443
+ *
444
+ * Y stays between -90 and 90. Within a ten-thousandth of a degree of either end, X and Z turn
445
+ * about one line, so X is 0 and Z takes the whole turn.
446
+ * Example: [0, 0, 0.7071, 0.7071] -> [0, 0, 90]
447
+ * @param inputs - The rotation
448
+ * @returns The angles about X, Y and Z
449
+ * @group quaternion
450
+ * @shortname quaternion to euler
451
+ * @drawable false
452
+ * @example
453
+ * ```typescript
454
+ * const angles = bitbybit.transforms.quaternionToEuler({ quaternion: [0, 0, 0.7071, 0.7071] });
455
+ * ```
456
+ */
457
+ quaternionToEuler(inputs) {
458
+ return degreesOfQuaternion(this.unitQuaternionOf(inputs.quaternion, "quaternion"));
459
+ }
460
+ /**
461
+ * Combines two rotations into the one that turns by `first` and then by `second`.
462
+ *
463
+ * Order matters: turning about X then Z differs from turning about Z then X.
464
+ * Example: a quarter turn about X, then a quarter turn about Z -> one rotation doing both
465
+ * @param inputs - The rotation applied first and the one applied after it
466
+ * @returns The combined rotation as [x, y, z, w]
467
+ * @group quaternion
468
+ * @shortname quaternion combine
469
+ * @drawable false
470
+ * @example
471
+ * ```typescript
472
+ * const both = bitbybit.transforms.quaternionCombine({ first: [0.7071, 0, 0, 0.7071], second: [0, 0, 0.7071, 0.7071] });
473
+ * ```
474
+ */
475
+ quaternionCombine(inputs) {
476
+ return quaternionProduct(this.unitQuaternionOf(inputs.second, "second"), this.unitQuaternionOf(inputs.first, "first"));
477
+ }
478
+ /**
479
+ * Turns a vector, or a point about the origin, by a rotation.
480
+ *
481
+ * Example: [1, 0, 0] turned by a quarter turn about Z -> [0, 1, 0]
482
+ * @param inputs - The rotation and the vector
483
+ * @returns The turned vector
484
+ * @group quaternion
485
+ * @shortname quaternion rotate vector
486
+ * @drawable false
487
+ * @example
488
+ * ```typescript
489
+ * const turned = bitbybit.transforms.quaternionRotateVector({ quaternion: [0, 0, 0.7071, 0.7071], vector: [1, 0, 0] });
490
+ * ```
491
+ */
492
+ quaternionRotateVector(inputs) {
493
+ if (!isTriple(inputs.vector)) {
494
+ throw new InputError("`vector` has to be three finite numbers.", "vector");
495
+ }
496
+ return quaternionTurned(this.unitQuaternionOf(inputs.quaternion, "quaternion"), inputs.vector);
497
+ }
498
+ /**
499
+ * Gives the rotation that turns the world axes onto a frame's: X onto its direction and Z onto
500
+ * its normal. The origin is left out.
501
+ *
502
+ * The direction is squared to the normal first, so a rough direction is enough.
503
+ * Example: normal [0,0,1], direction [0,1,0] -> a quarter turn about Z
504
+ * @param inputs - The frame
505
+ * @returns The rotation as [x, y, z, w]
506
+ * @group quaternion
507
+ * @shortname quaternion from frame
508
+ * @drawable false
509
+ * @example
510
+ * ```typescript
511
+ * const turn = bitbybit.transforms.quaternionFromFrame({ frame: { origin: [0, 0, 0], normal: [0, 0, 1], direction: [0, 1, 0] } });
512
+ * ```
513
+ */
514
+ quaternionFromFrame(inputs) {
515
+ const axes = squareFrame(inputs.frame.origin, inputs.frame.normal, inputs.frame.direction);
516
+ if (typeof axes === "string") {
517
+ throw new InputError(`The frame's \`${axes}\` has no length, or its direction runs along its normal.`, "frame");
518
+ }
519
+ return quaternionOfAxes(axes);
520
+ }
521
+ /**
522
+ * Writes a rotation as the transformation matrix the other methods here build, so it can be
523
+ * applied with `point.transformPoints` or combined with moves.
524
+ *
525
+ * Example: a quarter turn about Z -> the matrix that takes [1,0,0] to [0,1,0]
526
+ * @param inputs - The rotation
527
+ * @returns The matrix, 16 numbers in column-major order
528
+ * @group quaternion
529
+ * @shortname quaternion to matrix
530
+ * @drawable false
531
+ * @example
532
+ * ```typescript
533
+ * const matrix = bitbybit.transforms.quaternionToMatrix({ quaternion: [0, 0, 0.7071, 0.7071] });
534
+ * const points = bitbybit.point.transformPoints({ points: [[1, 0, 0]], transformation: [matrix] });
535
+ * ```
536
+ */
537
+ quaternionToMatrix(inputs) {
538
+ return quaternionMatrix(this.unitQuaternionOf(inputs.quaternion, "quaternion"));
539
+ }
540
+ unitQuaternionOf(quaternion, name) {
541
+ const unit = isQuaternion(quaternion) ? unitQuaternion(quaternion) : undefined;
542
+ if (unit === undefined) {
543
+ throw new InputError(`\`${name}\` has to be four finite numbers, not all 0.`, name);
544
+ }
545
+ return unit;
546
+ }
390
547
  translation(x, y, z) {
391
548
  return [1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, x, y, z, 1.0];
392
549
  }
@@ -449,36 +606,7 @@ export class Transforms {
449
606
  const y = sinYaw * cosPitch * cosRoll - cosYaw * sinPitch * sinRoll;
450
607
  const z = cosYaw * cosPitch * sinRoll - sinYaw * sinPitch * cosRoll;
451
608
  const w = cosYaw * cosPitch * cosRoll + sinYaw * sinPitch * sinRoll;
452
- return this.rotationMatrixFromQuat(x, y, z, w);
453
- }
454
- rotationMatrixFromQuat(x, y, z, w) {
455
- const xx = x * x;
456
- const yy = y * y;
457
- const zz = z * z;
458
- const xy = x * y;
459
- const zw = z * w;
460
- const zx = z * x;
461
- const yw = y * w;
462
- const yz = y * z;
463
- const xw = x * w;
464
- const m = this.identity();
465
- m[0] = 1.0 - 2.0 * (yy + zz);
466
- m[1] = 2.0 * (xy + zw);
467
- m[2] = 2.0 * (zx - yw);
468
- m[3] = 0.0;
469
- m[4] = 2.0 * (xy - zw);
470
- m[5] = 1.0 - 2.0 * (zz + xx);
471
- m[6] = 2.0 * (yz + xw);
472
- m[7] = 0.0;
473
- m[8] = 2.0 * (zx + yw);
474
- m[9] = 2.0 * (yz - xw);
475
- m[10] = 1.0 - 2.0 * (yy + xx);
476
- m[11] = 0.0;
477
- m[12] = 0.0;
478
- m[13] = 0.0;
479
- m[14] = 0.0;
480
- m[15] = 1.0;
481
- return m;
609
+ return quaternionMatrix([x, y, z, w]);
482
610
  }
483
611
  stretchDirection(direction, scale) {
484
612
  const d = this.vector.normalized({ vector: direction });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitbybit-dev/base",
3
- "version": "1.6.0",
3
+ "version": "1.6.2",
4
4
  "description": "Bit By Bit Developers Base CAD Library to Program Geometry",
5
5
  "homepage": "https://docs.bitbybit.dev/modules/Bit",
6
6
  "main": "index.js",