@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.
- package/lib/api/GlobalCDNProvider.js +1 -1
- package/lib/api/inputs/base-inputs.d.ts +25 -2
- package/lib/api/inputs/color-inputs.d.ts +54 -1
- package/lib/api/inputs/color-inputs.js +73 -1
- package/lib/api/inputs/frame-inputs.d.ts +2 -1
- package/lib/api/inputs/frame-inputs.js +2 -1
- package/lib/api/inputs/math-inputs.d.ts +62 -0
- package/lib/api/inputs/math-inputs.js +82 -0
- package/lib/api/inputs/mesh-inputs.d.ts +16 -0
- package/lib/api/inputs/mesh-inputs.js +14 -0
- package/lib/api/inputs/transforms-inputs.d.ts +76 -0
- package/lib/api/inputs/transforms-inputs.js +85 -0
- package/lib/api/resolved-inputs/index.d.ts +10 -0
- package/lib/api/services/color.d.ts +36 -2
- package/lib/api/services/color.js +59 -9
- package/lib/api/services/helpers/color/color-forms.constants.d.ts +7 -0
- package/lib/api/services/helpers/color/color-forms.constants.js +7 -0
- package/lib/api/services/helpers/color/color-forms.d.ts +2 -0
- package/lib/api/services/helpers/color/color-forms.js +24 -0
- package/lib/api/services/helpers/geometry/quaternions.constants.d.ts +3 -0
- package/lib/api/services/helpers/geometry/quaternions.constants.js +3 -0
- package/lib/api/services/helpers/geometry/quaternions.d.ts +14 -0
- package/lib/api/services/helpers/geometry/quaternions.js +71 -0
- package/lib/api/services/helpers/geometry/scalars.constants.d.ts +1 -0
- package/lib/api/services/helpers/geometry/scalars.constants.js +1 -0
- package/lib/api/services/helpers/geometry/scalars.d.ts +3 -0
- package/lib/api/services/helpers/geometry/scalars.js +29 -1
- package/lib/api/services/helpers/mesh/mesh-proximity.constants.d.ts +1 -0
- package/lib/api/services/helpers/mesh/mesh-proximity.constants.js +1 -0
- package/lib/api/services/helpers/mesh/mesh-proximity.d.ts +1 -0
- package/lib/api/services/helpers/mesh/mesh-proximity.js +40 -40
- package/lib/api/services/math.d.ts +35 -0
- package/lib/api/services/math.js +56 -1
- package/lib/api/services/mesh.d.ts +17 -0
- package/lib/api/services/mesh.js +27 -1
- package/lib/api/services/transforms.d.ts +119 -1
- package/lib/api/services/transforms.js +158 -30
- package/package.json +1 -1
|
@@ -101,8 +101,9 @@ export declare class Color {
|
|
|
101
101
|
/**
|
|
102
102
|
* Reads a hex color into an `{ r, g, b }` object with channels from 0 to 255.
|
|
103
103
|
*
|
|
104
|
-
* The text
|
|
105
|
-
*
|
|
104
|
+
* The text holds six hex digits or three, each standing for a pair, with or without `#`;
|
|
105
|
+
* anything else throws an error.
|
|
106
|
+
* Example: '#FF5733' -> { r: 255, g: 87, b: 51 }, '#f53' -> { r: 255, g: 85, b: 51 }
|
|
106
107
|
* @param inputs - The hex color
|
|
107
108
|
* @returns The color object with channels from 0 to 255
|
|
108
109
|
* @group convert
|
|
@@ -114,6 +115,39 @@ export declare class Color {
|
|
|
114
115
|
* ```
|
|
115
116
|
*/
|
|
116
117
|
hexToRgb(inputs: Inputs.Color.HexDto): Inputs.Base.ColorRGB;
|
|
118
|
+
/**
|
|
119
|
+
* Writes a hex color with an opacity as a CSS `rgba()` color, for a style that needs to see
|
|
120
|
+
* through.
|
|
121
|
+
*
|
|
122
|
+
* Example: '#ff5733' with alpha 0.5 -> 'rgba(255, 87, 51, 0.5)'
|
|
123
|
+
* @param inputs - The hex color and its opacity
|
|
124
|
+
* @returns The CSS color text
|
|
125
|
+
* @group convert
|
|
126
|
+
* @shortname hex to rgba
|
|
127
|
+
* @drawable false
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const fill = bitbybit.color.hexToRgba({ color: "#ff5733", alpha: 0.5 });
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
hexToRgba(inputs: Inputs.Color.HexAlphaDto): string;
|
|
134
|
+
/**
|
|
135
|
+
* Writes a color given by hue, saturation and lightness as a hex color: a way to pick colors
|
|
136
|
+
* that differ by hue alone, such as a palette spread round the wheel.
|
|
137
|
+
*
|
|
138
|
+
* The hue wraps round; saturation and lightness are held between 0 and 1.
|
|
139
|
+
* Example: hue 0, saturation 1, lightness 0.5 -> '#ff0000'; hue 210, 0.6, 0.5 -> '#3380cc'
|
|
140
|
+
* @param inputs - The hue in degrees, the saturation and the lightness
|
|
141
|
+
* @returns The hex color
|
|
142
|
+
* @group convert
|
|
143
|
+
* @shortname hsl to hex
|
|
144
|
+
* @drawable false
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* const blue = bitbybit.color.hslToHex({ hue: 210, saturation: 0.6, lightness: 0.5 });
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
hslToHex(inputs: Inputs.Color.HslDto): Inputs.Base.Color;
|
|
117
151
|
/**
|
|
118
152
|
* Writes three channel values as a hex color.
|
|
119
153
|
*
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import * as Inputs from "../inputs";
|
|
2
2
|
import { resolveDto } from "../kernel-calls";
|
|
3
3
|
import { linearToSrgb, srgbToLinear } from "./helpers/color/color-space";
|
|
4
|
+
import { hexChannels, hslChannels } from "./helpers/color/color-forms";
|
|
5
|
+
import { InputError } from "../kernel-calls/errors";
|
|
4
6
|
const HEX_RADIX = 16;
|
|
5
7
|
/**
|
|
6
8
|
* Colors in the two forms the library uses: a hex text such as `#ff5733`, and an object `{ r, g, b
|
|
@@ -125,8 +127,9 @@ export class Color {
|
|
|
125
127
|
/**
|
|
126
128
|
* Reads a hex color into an `{ r, g, b }` object with channels from 0 to 255.
|
|
127
129
|
*
|
|
128
|
-
* The text
|
|
129
|
-
*
|
|
130
|
+
* The text holds six hex digits or three, each standing for a pair, with or without `#`;
|
|
131
|
+
* anything else throws an error.
|
|
132
|
+
* Example: '#FF5733' -> { r: 255, g: 87, b: 51 }, '#f53' -> { r: 255, g: 85, b: 51 }
|
|
130
133
|
* @param inputs - The hex color
|
|
131
134
|
* @returns The color object with channels from 0 to 255
|
|
132
135
|
* @group convert
|
|
@@ -139,15 +142,62 @@ export class Color {
|
|
|
139
142
|
*/
|
|
140
143
|
hexToRgb(inputs) {
|
|
141
144
|
const resolved = resolveDto(Inputs.Color.HexDto, inputs);
|
|
142
|
-
const
|
|
143
|
-
if (!
|
|
145
|
+
const channels = hexChannels(resolved.color);
|
|
146
|
+
if (!channels) {
|
|
144
147
|
throw new Error(`Invalid hex color: ${resolved.color}`);
|
|
145
148
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
149
|
+
const [r, g, b] = channels;
|
|
150
|
+
return { r, g, b };
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Writes a hex color with an opacity as a CSS `rgba()` color, for a style that needs to see
|
|
154
|
+
* through.
|
|
155
|
+
*
|
|
156
|
+
* Example: '#ff5733' with alpha 0.5 -> 'rgba(255, 87, 51, 0.5)'
|
|
157
|
+
* @param inputs - The hex color and its opacity
|
|
158
|
+
* @returns The CSS color text
|
|
159
|
+
* @group convert
|
|
160
|
+
* @shortname hex to rgba
|
|
161
|
+
* @drawable false
|
|
162
|
+
* @example
|
|
163
|
+
* ```typescript
|
|
164
|
+
* const fill = bitbybit.color.hexToRgba({ color: "#ff5733", alpha: 0.5 });
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
hexToRgba(inputs) {
|
|
168
|
+
const resolved = resolveDto(Inputs.Color.HexAlphaDto, inputs);
|
|
169
|
+
if (!Number.isFinite(resolved.alpha)) {
|
|
170
|
+
throw new InputError(`\`alpha\` is ${String(resolved.alpha)}, not a finite number.`, "alpha");
|
|
171
|
+
}
|
|
172
|
+
const { r, g, b } = this.hexToRgb({ color: resolved.color });
|
|
173
|
+
const alpha = this.math.clamp({ number: resolved.alpha, min: 0, max: 1 });
|
|
174
|
+
return `rgba(${String(r)}, ${String(g)}, ${String(b)}, ${String(alpha)})`;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Writes a color given by hue, saturation and lightness as a hex color: a way to pick colors
|
|
178
|
+
* that differ by hue alone, such as a palette spread round the wheel.
|
|
179
|
+
*
|
|
180
|
+
* The hue wraps round; saturation and lightness are held between 0 and 1.
|
|
181
|
+
* Example: hue 0, saturation 1, lightness 0.5 -> '#ff0000'; hue 210, 0.6, 0.5 -> '#3380cc'
|
|
182
|
+
* @param inputs - The hue in degrees, the saturation and the lightness
|
|
183
|
+
* @returns The hex color
|
|
184
|
+
* @group convert
|
|
185
|
+
* @shortname hsl to hex
|
|
186
|
+
* @drawable false
|
|
187
|
+
* @example
|
|
188
|
+
* ```typescript
|
|
189
|
+
* const blue = bitbybit.color.hslToHex({ hue: 210, saturation: 0.6, lightness: 0.5 });
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
hslToHex(inputs) {
|
|
193
|
+
const resolved = resolveDto(Inputs.Color.HslDto, inputs);
|
|
194
|
+
const notFinite = ["hue", "saturation", "lightness"].find(name => !Number.isFinite(resolved[name]));
|
|
195
|
+
if (notFinite !== undefined) {
|
|
196
|
+
throw new InputError(`\`${notFinite}\` is ${String(resolved[notFinite])}, not a finite number.`, notFinite);
|
|
197
|
+
}
|
|
198
|
+
const held = (value) => this.math.clamp({ number: value, min: 0, max: 1 });
|
|
199
|
+
const [r, g, b] = hslChannels(resolved.hue, held(resolved.saturation), held(resolved.lightness));
|
|
200
|
+
return this.rgbToHex({ r, g, b, min: 0, max: 1 });
|
|
151
201
|
}
|
|
152
202
|
/**
|
|
153
203
|
* Writes three channel values as a hex color.
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const HUE_DEGREES_PER_STEP = 30;
|
|
2
|
+
export declare const HUE_STEPS = 12;
|
|
3
|
+
export declare const HUE_RISE_START = 3;
|
|
4
|
+
export declare const HUE_FALL_START = 9;
|
|
5
|
+
export declare const RED_OFFSET = 0;
|
|
6
|
+
export declare const GREEN_OFFSET = 8;
|
|
7
|
+
export declare const BLUE_OFFSET = 4;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { BLUE_OFFSET, GREEN_OFFSET, HUE_DEGREES_PER_STEP, HUE_FALL_START, HUE_RISE_START, HUE_STEPS, RED_OFFSET } from "./color-forms.constants";
|
|
2
|
+
const HEX_RADIX = 16;
|
|
3
|
+
const LONG_HEX = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
|
|
4
|
+
const SHORT_HEX = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
|
|
5
|
+
export function hexChannels(text) {
|
|
6
|
+
const long = LONG_HEX.exec(text);
|
|
7
|
+
if (long) {
|
|
8
|
+
return [parseInt(long[1], HEX_RADIX), parseInt(long[2], HEX_RADIX), parseInt(long[3], HEX_RADIX)];
|
|
9
|
+
}
|
|
10
|
+
const short = SHORT_HEX.exec(text);
|
|
11
|
+
if (!short) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const doubled = (digit) => parseInt(digit + digit, HEX_RADIX);
|
|
15
|
+
return [doubled(short[1]), doubled(short[2]), doubled(short[3])];
|
|
16
|
+
}
|
|
17
|
+
export function hslChannels(hue, saturation, lightness) {
|
|
18
|
+
const reach = saturation * Math.min(lightness, 1 - lightness);
|
|
19
|
+
const channel = (offset) => {
|
|
20
|
+
const step = ((offset + hue / HUE_DEGREES_PER_STEP) % HUE_STEPS + HUE_STEPS) % HUE_STEPS;
|
|
21
|
+
return lightness - reach * Math.max(-1, Math.min(step - HUE_RISE_START, HUE_FALL_START - step, 1));
|
|
22
|
+
};
|
|
23
|
+
return [channel(RED_OFFSET), channel(GREEN_OFFSET), channel(BLUE_OFFSET)];
|
|
24
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type * as Inputs from "../../../inputs";
|
|
2
|
+
import type { FrameAxes } from "./frame-axes";
|
|
3
|
+
type Quaternion = Inputs.Base.Quaternion;
|
|
4
|
+
type Vec3 = Inputs.Base.Vector3;
|
|
5
|
+
export declare const isQuaternion: (value: unknown) => value is Quaternion;
|
|
6
|
+
export declare const unitQuaternion: (quaternion: Quaternion) => Quaternion | undefined;
|
|
7
|
+
export declare const quaternionAbout: (unitAxis: Vec3, degrees: number) => Quaternion;
|
|
8
|
+
export declare const quaternionProduct: (a: Quaternion, b: Quaternion) => Quaternion;
|
|
9
|
+
export declare const quaternionOfDegrees: (angles: Vec3) => Quaternion;
|
|
10
|
+
export declare const quaternionTurned: (unit: Quaternion, vector: Vec3) => Vec3;
|
|
11
|
+
export declare const degreesOfQuaternion: (unit: Quaternion) => Vec3;
|
|
12
|
+
export declare const quaternionOfAxes: ({ x, y, z }: Pick<FrameAxes, "x" | "y" | "z">) => Quaternion;
|
|
13
|
+
export declare const quaternionMatrix: ([x, y, z, w]: Quaternion) => Inputs.Base.TransformMatrix;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { degreesOf, radiansOf } from "./scalars";
|
|
2
|
+
import { GIMBAL_SINE, QUARTER_TURN_DEGREES, QUATERNION_LENGTH } from "./quaternions.constants";
|
|
3
|
+
const HALF = 0.5;
|
|
4
|
+
export const isQuaternion = (value) => Array.isArray(value) && value.length === QUATERNION_LENGTH && value.every(n => typeof n === "number" && Number.isFinite(n));
|
|
5
|
+
export const unitQuaternion = (quaternion) => {
|
|
6
|
+
const length = Math.hypot(quaternion[0], quaternion[1], quaternion[2], quaternion[3]);
|
|
7
|
+
if (!(length > 0 && length < Infinity)) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
return [quaternion[0] / length, quaternion[1] / length, quaternion[2] / length, quaternion[3] / length];
|
|
11
|
+
};
|
|
12
|
+
export const quaternionAbout = (unitAxis, degrees) => {
|
|
13
|
+
const half = radiansOf(degrees) * HALF;
|
|
14
|
+
const sine = Math.sin(half);
|
|
15
|
+
return [unitAxis[0] * sine, unitAxis[1] * sine, unitAxis[2] * sine, Math.cos(half)];
|
|
16
|
+
};
|
|
17
|
+
export const quaternionProduct = (a, b) => [
|
|
18
|
+
a[3] * b[0] + a[0] * b[3] + a[1] * b[2] - a[2] * b[1],
|
|
19
|
+
a[3] * b[1] - a[0] * b[2] + a[1] * b[3] + a[2] * b[0],
|
|
20
|
+
a[3] * b[2] + a[0] * b[1] - a[1] * b[0] + a[2] * b[3],
|
|
21
|
+
a[3] * b[3] - a[0] * b[0] - a[1] * b[1] - a[2] * b[2],
|
|
22
|
+
];
|
|
23
|
+
export const quaternionOfDegrees = (angles) => quaternionProduct(quaternionAbout([0, 0, 1], angles[2]), quaternionProduct(quaternionAbout([0, 1, 0], angles[1]), quaternionAbout([1, 0, 0], angles[0])));
|
|
24
|
+
export const quaternionTurned = (unit, vector) => {
|
|
25
|
+
const [x, y, z, w] = unit;
|
|
26
|
+
const tx = 2 * (y * vector[2] - z * vector[1]);
|
|
27
|
+
const ty = 2 * (z * vector[0] - x * vector[2]);
|
|
28
|
+
const tz = 2 * (x * vector[1] - y * vector[0]);
|
|
29
|
+
return [vector[0] + w * tx + (y * tz - z * ty), vector[1] + w * ty + (z * tx - x * tz), vector[2] + w * tz + (x * ty - y * tx)];
|
|
30
|
+
};
|
|
31
|
+
export const degreesOfQuaternion = (unit) => {
|
|
32
|
+
const [x, y, z, w] = unit;
|
|
33
|
+
const r00 = 1 - 2 * (y * y + z * z);
|
|
34
|
+
const r01 = 2 * (x * y - z * w);
|
|
35
|
+
const r10 = 2 * (x * y + z * w);
|
|
36
|
+
const r11 = 1 - 2 * (x * x + z * z);
|
|
37
|
+
const r20 = 2 * (x * z - y * w);
|
|
38
|
+
const r21 = 2 * (y * z + x * w);
|
|
39
|
+
const r22 = 1 - 2 * (x * x + y * y);
|
|
40
|
+
if (Math.abs(r20) > GIMBAL_SINE) {
|
|
41
|
+
return [0, r20 > 0 ? -QUARTER_TURN_DEGREES : QUARTER_TURN_DEGREES, degreesOf(Math.atan2(-r01, r11))];
|
|
42
|
+
}
|
|
43
|
+
return [degreesOf(Math.atan2(r21, r22)), degreesOf(Math.asin(-r20)), degreesOf(Math.atan2(r10, r00))];
|
|
44
|
+
};
|
|
45
|
+
export const quaternionOfAxes = ({ x, y, z }) => {
|
|
46
|
+
const trace = x[0] + y[1] + z[2];
|
|
47
|
+
if (trace > 0) {
|
|
48
|
+
const root = Math.sqrt(trace + 1);
|
|
49
|
+
const share = HALF / root;
|
|
50
|
+
return [(y[2] - z[1]) * share, (z[0] - x[2]) * share, (x[1] - y[0]) * share, root * HALF];
|
|
51
|
+
}
|
|
52
|
+
if (x[0] > y[1] && x[0] > z[2]) {
|
|
53
|
+
const root = Math.sqrt(1 + x[0] - y[1] - z[2]);
|
|
54
|
+
const share = HALF / root;
|
|
55
|
+
return [root * HALF, (y[0] + x[1]) * share, (z[0] + x[2]) * share, (y[2] - z[1]) * share];
|
|
56
|
+
}
|
|
57
|
+
if (y[1] > z[2]) {
|
|
58
|
+
const root = Math.sqrt(1 + y[1] - x[0] - z[2]);
|
|
59
|
+
const share = HALF / root;
|
|
60
|
+
return [(y[0] + x[1]) * share, root * HALF, (z[1] + y[2]) * share, (z[0] - x[2]) * share];
|
|
61
|
+
}
|
|
62
|
+
const root = Math.sqrt(1 + z[2] - x[0] - y[1]);
|
|
63
|
+
const share = HALF / root;
|
|
64
|
+
return [(z[0] + x[2]) * share, (z[1] + y[2]) * share, root * HALF, (x[1] - y[0]) * share];
|
|
65
|
+
};
|
|
66
|
+
export const quaternionMatrix = ([x, y, z, w]) => [
|
|
67
|
+
1 - 2 * (y * y + z * z), 2 * (x * y + z * w), 2 * (z * x - y * w), 0,
|
|
68
|
+
2 * (x * y - z * w), 1 - 2 * (z * z + x * x), 2 * (y * z + x * w), 0,
|
|
69
|
+
2 * (z * x + y * w), 2 * (y * z - x * w), 1 - 2 * (y * y + x * x), 0,
|
|
70
|
+
0, 0, 0, 1,
|
|
71
|
+
];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const NICE_MANTISSAS: readonly [1, 2, 5];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const NICE_MANTISSAS = [1, 2, 5];
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type * as Inputs from "../../../inputs";
|
|
1
2
|
export declare const smoothstep: (value: number) => number;
|
|
2
3
|
export declare const roundedTo: (value: number, places: number) => number;
|
|
3
4
|
export declare const roundedToMillionths: (value: number) => number;
|
|
@@ -6,4 +7,6 @@ export declare const radiansOf: (degrees: number) => number;
|
|
|
6
7
|
export declare const degreesOf: (radians: number) => number;
|
|
7
8
|
export declare const angleOfCosine: (cosine: number) => number;
|
|
8
9
|
export declare const interpolated: (start: number, end: number, share: number) => number;
|
|
10
|
+
export declare function shortestAngle(from: number, to: number, fullTurn: number): number;
|
|
11
|
+
export declare function niceStep(value: number, direction: `${Inputs.Math.niceStepDirectionEnum}`): number;
|
|
9
12
|
export declare function fractionText(value: number, denominator: number): string;
|
|
@@ -1,18 +1,46 @@
|
|
|
1
|
+
import { NICE_MANTISSAS } from "./scalars.constants";
|
|
1
2
|
const MILLIONTH_PLACES = 6;
|
|
2
3
|
const DECIMAL_BASE = 10;
|
|
3
4
|
const SMOOTHSTEP_RISE = 3;
|
|
4
5
|
const HALF_TURN_DEGREES = 180;
|
|
6
|
+
const NICE_SLACK = 1e-9;
|
|
5
7
|
export const smoothstep = (value) => {
|
|
6
8
|
const t = Math.min(1, Math.max(0, value));
|
|
7
9
|
return t * t * (SMOOTHSTEP_RISE - 2 * t);
|
|
8
10
|
};
|
|
9
|
-
export const roundedTo = (value, places) =>
|
|
11
|
+
export const roundedTo = (value, places) => {
|
|
12
|
+
const rounded = Math.round(value * Math.pow(DECIMAL_BASE, places)) / Math.pow(DECIMAL_BASE, places);
|
|
13
|
+
return rounded === 0 ? 0 : rounded;
|
|
14
|
+
};
|
|
10
15
|
export const roundedToMillionths = (value) => roundedTo(value, MILLIONTH_PLACES);
|
|
11
16
|
export const fractionOf = (value) => value - Math.floor(value);
|
|
12
17
|
export const radiansOf = (degrees) => degrees * Math.PI / HALF_TURN_DEGREES;
|
|
13
18
|
export const degreesOf = (radians) => radians * HALF_TURN_DEGREES / Math.PI;
|
|
14
19
|
export const angleOfCosine = (cosine) => Math.acos(Math.min(1, Math.max(-1, cosine)));
|
|
15
20
|
export const interpolated = (start, end, share) => start + (end - start) * share;
|
|
21
|
+
export function shortestAngle(from, to, fullTurn) {
|
|
22
|
+
const half = fullTurn / 2;
|
|
23
|
+
const delta = (to - from) % fullTurn;
|
|
24
|
+
if (delta > half) {
|
|
25
|
+
return from + delta - fullTurn;
|
|
26
|
+
}
|
|
27
|
+
return delta < -half ? from + delta + fullTurn : from + delta;
|
|
28
|
+
}
|
|
29
|
+
function niceValue(mantissa, exponent) {
|
|
30
|
+
return Number(`${String(mantissa)}e${String(exponent)}`);
|
|
31
|
+
}
|
|
32
|
+
export function niceStep(value, direction) {
|
|
33
|
+
var _a, _b;
|
|
34
|
+
const exponent = Math.floor(Math.log10(value));
|
|
35
|
+
const candidates = [exponent - 1, exponent, exponent + 1].flatMap(each => NICE_MANTISSAS.map(mantissa => niceValue(mantissa, each)));
|
|
36
|
+
if (direction === "up") {
|
|
37
|
+
return (_a = candidates.find(candidate => candidate >= value * (1 - NICE_SLACK))) !== null && _a !== void 0 ? _a : candidates[candidates.length - 1];
|
|
38
|
+
}
|
|
39
|
+
if (direction === "down") {
|
|
40
|
+
return (_b = candidates.filter(candidate => candidate <= value * (1 + NICE_SLACK)).pop()) !== null && _b !== void 0 ? _b : candidates[0];
|
|
41
|
+
}
|
|
42
|
+
return candidates.reduce((best, candidate) => Math.abs(Math.log(candidate / value)) < Math.abs(Math.log(best / value)) ? candidate : best);
|
|
43
|
+
}
|
|
16
44
|
function greatestCommonDivisor(first, second) {
|
|
17
45
|
let [a, b] = [first, second];
|
|
18
46
|
while (b !== 0) {
|
|
@@ -2,6 +2,7 @@ import type * as Inputs from "../../../inputs";
|
|
|
2
2
|
import type { TreeBox, TriangleTree } from "../helper-types";
|
|
3
3
|
type Vec = Inputs.Base.Point3;
|
|
4
4
|
export declare function triangleTree(positions: ArrayLike<number>, indices: ArrayLike<number>): TriangleTree;
|
|
5
|
+
export declare function closestOnTriangle(p: Vec, a: Vec, b: Vec, c: Vec): Inputs.Base.TrianglePoint3;
|
|
5
6
|
export declare function treesWithin(first: TriangleTree, second: TriangleTree, limit: number): boolean;
|
|
6
7
|
export declare function pointInTree(tree: TriangleTree, point: Vec): boolean;
|
|
7
8
|
export declare function pointOnTree(tree: TriangleTree): Vec | undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { cross3, dot3, subtract3 } from "../geometry/vectors";
|
|
2
|
-
import { FLAT_SHARE, LEAF_TRIANGLES, PARALLEL, POINT_SHARE, PRUNE_SLACK, RAY_DIRECTIONS, WELD_SHARE } from "./mesh-proximity.constants";
|
|
1
|
+
import { add3, cross3, dot3, scale3, subtract3 } from "../geometry/vectors";
|
|
2
|
+
import { FLAT_SHARE, LEAF_TRIANGLES, TRIANGLE_SIDES, PARALLEL, POINT_SHARE, PRUNE_SLACK, RAY_DIRECTIONS, WELD_SHARE } from "./mesh-proximity.constants";
|
|
3
3
|
import { windingComponents } from "./mesh-orientation";
|
|
4
4
|
import { uniqueVectors } from "../geometry/vector-grid";
|
|
5
5
|
const CORNERS = 3;
|
|
@@ -130,47 +130,47 @@ function segmentsSquared(p1, q1, p2, q2) {
|
|
|
130
130
|
const gap = subtract3(along(p1, d1, s), along(p2, d2, t));
|
|
131
131
|
return dot3(gap, gap);
|
|
132
132
|
}
|
|
133
|
-
function
|
|
133
|
+
function nearestOnEdges(p, corners) {
|
|
134
|
+
let best;
|
|
135
|
+
let bestGap = Infinity;
|
|
136
|
+
for (let at = 0; at < TRIANGLE_SIDES; at++) {
|
|
137
|
+
const next = (at + 1) % TRIANGLE_SIDES;
|
|
138
|
+
const from = corners[at];
|
|
139
|
+
const run = subtract3(corners[next], from);
|
|
140
|
+
const length = dot3(run, run);
|
|
141
|
+
const share = length > 0 ? Math.min(1, Math.max(0, dot3(subtract3(p, from), run) / length)) : 0;
|
|
142
|
+
const point = add3(from, scale3(run, share));
|
|
143
|
+
const away = subtract3(p, point);
|
|
144
|
+
const gap = dot3(away, away);
|
|
145
|
+
if (best === undefined || gap < bestGap) {
|
|
146
|
+
const weights = [0, 0, 0];
|
|
147
|
+
weights[at] = 1 - share;
|
|
148
|
+
weights[next] = share;
|
|
149
|
+
best = { point, weights };
|
|
150
|
+
bestGap = gap;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return best;
|
|
154
|
+
}
|
|
155
|
+
export function closestOnTriangle(p, a, b, c) {
|
|
134
156
|
const ab = subtract3(b, a);
|
|
135
157
|
const ac = subtract3(c, a);
|
|
136
|
-
const
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
return dot3(bp, bp);
|
|
147
|
-
}
|
|
148
|
-
const vc = d1 * d4 - d3 * d2;
|
|
149
|
-
if (vc <= 0 && d1 >= 0 && d3 <= 0) {
|
|
150
|
-
const q = subtract3(p, along(a, ab, d1 / (d1 - d3)));
|
|
151
|
-
return dot3(q, q);
|
|
152
|
-
}
|
|
153
|
-
const cp = subtract3(p, c);
|
|
154
|
-
const d5 = dot3(ab, cp);
|
|
155
|
-
const d6 = dot3(ac, cp);
|
|
156
|
-
if (d6 >= 0 && d5 <= d6) {
|
|
157
|
-
return dot3(cp, cp);
|
|
158
|
-
}
|
|
159
|
-
const vb = d5 * d2 - d1 * d6;
|
|
160
|
-
if (vb <= 0 && d2 >= 0 && d6 <= 0) {
|
|
161
|
-
const q = subtract3(p, along(a, ac, d2 / (d2 - d6)));
|
|
162
|
-
return dot3(q, q);
|
|
163
|
-
}
|
|
164
|
-
const va = d3 * d6 - d5 * d4;
|
|
165
|
-
if (va <= 0 && d4 - d3 >= 0 && d5 - d6 >= 0) {
|
|
166
|
-
const q = subtract3(p, along(b, subtract3(c, b), (d4 - d3) / (d4 - d3 + (d5 - d6))));
|
|
167
|
-
return dot3(q, q);
|
|
158
|
+
const facing = cross3(ab, ac);
|
|
159
|
+
const area = dot3(facing, facing);
|
|
160
|
+
if (area > FLAT_SHARE * dot3(ab, ab) * dot3(ac, ac)) {
|
|
161
|
+
const foot = subtract3(p, scale3(facing, dot3(facing, subtract3(p, a)) / area));
|
|
162
|
+
const u = dot3(cross3(subtract3(b, foot), subtract3(c, foot)), facing) / area;
|
|
163
|
+
const v = dot3(cross3(subtract3(c, foot), subtract3(a, foot)), facing) / area;
|
|
164
|
+
const w = 1 - u - v;
|
|
165
|
+
if (u >= 0 && v >= 0 && w >= 0) {
|
|
166
|
+
return { point: foot, weights: [u, v, w] };
|
|
167
|
+
}
|
|
168
168
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
const
|
|
173
|
-
return dot3(
|
|
169
|
+
return nearestOnEdges(p, [a, b, c]);
|
|
170
|
+
}
|
|
171
|
+
function pointTriangleSquared(p, a, b, c) {
|
|
172
|
+
const gap = subtract3(p, closestOnTriangle(p, a, b, c).point);
|
|
173
|
+
return dot3(gap, gap);
|
|
174
174
|
}
|
|
175
175
|
function rayTriangleHit(from, direction, a, b, c) {
|
|
176
176
|
const e1 = subtract3(b, a);
|
|
@@ -55,6 +55,7 @@ export declare class MathBitByBit {
|
|
|
55
55
|
/**
|
|
56
56
|
* Rounds a number to a given number of decimal places.
|
|
57
57
|
*
|
|
58
|
+
* A small negative number rounds to 0, never to -0.
|
|
58
59
|
* Example: 1.32156 to 3 places -> 1.322
|
|
59
60
|
* @param inputs - The number and how many decimal places to keep
|
|
60
61
|
* @returns The rounded number
|
|
@@ -654,6 +655,40 @@ export declare class MathBitByBit {
|
|
|
654
655
|
* ```
|
|
655
656
|
*/
|
|
656
657
|
moveTowards(inputs: Inputs.Math.MoveTowardsDto): number;
|
|
658
|
+
/**
|
|
659
|
+
* Gives the angle that `to` names, whole turns aside, nearest to `from`, so going from `from` to
|
|
660
|
+
* it is the short way round.
|
|
661
|
+
*
|
|
662
|
+
* Animating an angle toward the result never spins the long way. `fullTurn` sets the unit.
|
|
663
|
+
* Example: 10 to 350 in degrees -> -10, 350 to 10 -> 370
|
|
664
|
+
* @param inputs - The two angles and the size of a whole turn
|
|
665
|
+
* @returns The angle to arrive at, at most half a turn from `from`
|
|
666
|
+
* @group operations
|
|
667
|
+
* @shortname shortest angle
|
|
668
|
+
* @drawable false
|
|
669
|
+
* @example
|
|
670
|
+
* ```typescript
|
|
671
|
+
* const target = bitbybit.math.shortestAngle({ from: 10, to: 350, fullTurn: 360 });
|
|
672
|
+
* ```
|
|
673
|
+
*/
|
|
674
|
+
shortestAngle(inputs: Inputs.Math.ShortestAngleDto): number;
|
|
675
|
+
/**
|
|
676
|
+
* Rounds a positive number to a step on the 1, 2, 5 scale - 0.1, 0.2, 0.5, 1, 2, 5, 10 - the
|
|
677
|
+
* sizes a grid, a ruler or a slider reads well in.
|
|
678
|
+
*
|
|
679
|
+
* A number within a billionth of a step counts as that step.
|
|
680
|
+
* Example: 0.37 up -> 0.5, down -> 0.2, nearest -> 0.5
|
|
681
|
+
* @param inputs - The number and which way to round it
|
|
682
|
+
* @returns The step
|
|
683
|
+
* @group operations
|
|
684
|
+
* @shortname nice step
|
|
685
|
+
* @drawable false
|
|
686
|
+
* @example
|
|
687
|
+
* ```typescript
|
|
688
|
+
* const step = bitbybit.math.niceStep({ number: 0.37, direction: Bit.Inputs.Math.niceStepDirectionEnum.up });
|
|
689
|
+
* ```
|
|
690
|
+
*/
|
|
691
|
+
niceStep(inputs: Inputs.Math.NiceStepDto): number;
|
|
657
692
|
/**
|
|
658
693
|
* Works out a simple arithmetic expression written as text: numbers, +, -, the multiplication
|
|
659
694
|
* sign, /, parentheses and spaces.
|
package/lib/api/services/math.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as Inputs from "../inputs";
|
|
2
2
|
import { resolveDto } from "../kernel-calls";
|
|
3
|
-
import { degreesOf, fractionOf, fractionText, interpolated, radiansOf, roundedTo, smoothstep } from "./helpers/geometry/scalars";
|
|
3
|
+
import { degreesOf, fractionOf, fractionText, interpolated, niceStep, radiansOf, roundedTo, shortestAngle, smoothstep } from "./helpers/geometry/scalars";
|
|
4
4
|
import { InputError } from "../kernel-calls/errors";
|
|
5
5
|
import { checkedUnitOfLength, lengthConverted, millimetresPerUnitOf } from "./helpers/geometry/lengths";
|
|
6
6
|
/**
|
|
@@ -91,6 +91,7 @@ export class MathBitByBit {
|
|
|
91
91
|
/**
|
|
92
92
|
* Rounds a number to a given number of decimal places.
|
|
93
93
|
*
|
|
94
|
+
* A small negative number rounds to 0, never to -0.
|
|
94
95
|
* Example: 1.32156 to 3 places -> 1.322
|
|
95
96
|
* @param inputs - The number and how many decimal places to keep
|
|
96
97
|
* @returns The rounded number
|
|
@@ -917,6 +918,60 @@ export class MathBitByBit {
|
|
|
917
918
|
}
|
|
918
919
|
return resolved.current + Math.sign(delta) * resolved.maxDelta;
|
|
919
920
|
}
|
|
921
|
+
/**
|
|
922
|
+
* Gives the angle that `to` names, whole turns aside, nearest to `from`, so going from `from` to
|
|
923
|
+
* it is the short way round.
|
|
924
|
+
*
|
|
925
|
+
* Animating an angle toward the result never spins the long way. `fullTurn` sets the unit.
|
|
926
|
+
* Example: 10 to 350 in degrees -> -10, 350 to 10 -> 370
|
|
927
|
+
* @param inputs - The two angles and the size of a whole turn
|
|
928
|
+
* @returns The angle to arrive at, at most half a turn from `from`
|
|
929
|
+
* @group operations
|
|
930
|
+
* @shortname shortest angle
|
|
931
|
+
* @drawable false
|
|
932
|
+
* @example
|
|
933
|
+
* ```typescript
|
|
934
|
+
* const target = bitbybit.math.shortestAngle({ from: 10, to: 350, fullTurn: 360 });
|
|
935
|
+
* ```
|
|
936
|
+
*/
|
|
937
|
+
shortestAngle(inputs) {
|
|
938
|
+
const resolved = resolveDto(Inputs.Math.ShortestAngleDto, inputs);
|
|
939
|
+
if (!(resolved.fullTurn > 0 && Number.isFinite(resolved.fullTurn))) {
|
|
940
|
+
throw new InputError(`\`fullTurn\` is ${String(resolved.fullTurn)}, and has to be a positive finite number.`, "fullTurn");
|
|
941
|
+
}
|
|
942
|
+
const notFinite = ["from", "to"].find(name => !Number.isFinite(resolved[name]));
|
|
943
|
+
if (notFinite !== undefined) {
|
|
944
|
+
throw new InputError(`\`${notFinite}\` is ${String(resolved[notFinite])}, not a finite number.`, notFinite);
|
|
945
|
+
}
|
|
946
|
+
return shortestAngle(resolved.from, resolved.to, resolved.fullTurn);
|
|
947
|
+
}
|
|
948
|
+
/**
|
|
949
|
+
* Rounds a positive number to a step on the 1, 2, 5 scale - 0.1, 0.2, 0.5, 1, 2, 5, 10 - the
|
|
950
|
+
* sizes a grid, a ruler or a slider reads well in.
|
|
951
|
+
*
|
|
952
|
+
* A number within a billionth of a step counts as that step.
|
|
953
|
+
* Example: 0.37 up -> 0.5, down -> 0.2, nearest -> 0.5
|
|
954
|
+
* @param inputs - The number and which way to round it
|
|
955
|
+
* @returns The step
|
|
956
|
+
* @group operations
|
|
957
|
+
* @shortname nice step
|
|
958
|
+
* @drawable false
|
|
959
|
+
* @example
|
|
960
|
+
* ```typescript
|
|
961
|
+
* const step = bitbybit.math.niceStep({ number: 0.37, direction: Bit.Inputs.Math.niceStepDirectionEnum.up });
|
|
962
|
+
* ```
|
|
963
|
+
*/
|
|
964
|
+
niceStep(inputs) {
|
|
965
|
+
const resolved = resolveDto(Inputs.Math.NiceStepDto, inputs);
|
|
966
|
+
if (!(resolved.number > 0 && Number.isFinite(resolved.number))) {
|
|
967
|
+
throw new InputError(`\`number\` is ${String(resolved.number)}, and has to be a positive finite number.`, "number");
|
|
968
|
+
}
|
|
969
|
+
const step = niceStep(resolved.number, resolved.direction);
|
|
970
|
+
if (!Number.isFinite(step)) {
|
|
971
|
+
throw new InputError(`\`number\` is ${String(resolved.number)}, too large to round to a step.`, "number");
|
|
972
|
+
}
|
|
973
|
+
return step;
|
|
974
|
+
}
|
|
920
975
|
/**
|
|
921
976
|
* Works out a simple arithmetic expression written as text: numbers, +, -, the multiplication
|
|
922
977
|
* sign, /, parentheses and spaces.
|
|
@@ -29,6 +29,23 @@ export declare class MeshBitByBit {
|
|
|
29
29
|
* ```
|
|
30
30
|
*/
|
|
31
31
|
signedDistanceToPlane(inputs: Inputs.Mesh.SignedDistanceFromPlaneToPointDto): number;
|
|
32
|
+
/**
|
|
33
|
+
* Finds the point of a triangle nearest to another point, with the weight of each corner in it.
|
|
34
|
+
*
|
|
35
|
+
* Each corner times its weight, added up, gives the point; the weights add up to 1. Corners on one
|
|
36
|
+
* line act as the segment they span.
|
|
37
|
+
* 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] }
|
|
38
|
+
* @param inputs - The point and the triangle
|
|
39
|
+
* @returns The nearest point and the weight of each corner in it
|
|
40
|
+
* @group traingle
|
|
41
|
+
* @shortname closest point on triangle
|
|
42
|
+
* @drawable false
|
|
43
|
+
* @example
|
|
44
|
+
* ```typescript
|
|
45
|
+
* const nearest = bitbybit.mesh.closestPointOnTriangle({ point: [0.25, 0.25, 2], triangle: [[0, 0, 0], [1, 0, 0], [0, 1, 0]] });
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
closestPointOnTriangle(inputs: Inputs.Mesh.PointTriangleDto): Inputs.Base.TrianglePoint3;
|
|
32
49
|
/**
|
|
33
50
|
* Finds the plane a triangle lies in: its unit normal and its distance from the origin along
|
|
34
51
|
* that normal.
|