@nakafa/aksara-contracts 6.0.0 → 6.2.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/dist/assessment/figure.d.ts +567 -6
- package/dist/math/bounds.d.ts +3 -2
- package/dist/math/bounds.js +55 -9
- package/dist/math/curve.d.ts +64 -0
- package/dist/math/curve.js +83 -0
- package/dist/math/formula/box.d.ts +61 -0
- package/dist/math/formula/box.js +68 -0
- package/dist/math/formula/compile.d.ts +11 -0
- package/dist/math/formula/compile.js +100 -0
- package/dist/math/formula/contour.d.ts +17 -0
- package/dist/math/formula/contour.js +162 -0
- package/dist/math/formula/field.d.ts +42 -0
- package/dist/math/formula/field.js +54 -0
- package/dist/math/formula/mesh.d.ts +25 -0
- package/dist/math/formula/mesh.js +28 -0
- package/dist/math/formula/names.d.ts +4 -0
- package/dist/math/formula/names.js +23 -0
- package/dist/math/formula/parse.d.ts +10 -0
- package/dist/math/formula/parse.js +240 -0
- package/dist/math/formula/plane.d.ts +75 -0
- package/dist/math/formula/plane.js +121 -0
- package/dist/math/formula/reader.d.ts +102 -0
- package/dist/math/formula/reader.js +58 -0
- package/dist/math/formula/region.d.ts +16 -0
- package/dist/math/formula/region.js +94 -0
- package/dist/math/formula/sample.d.ts +14 -0
- package/dist/math/formula/sample.js +182 -0
- package/dist/math/formula/scope.d.ts +41 -0
- package/dist/math/formula/scope.js +151 -0
- package/dist/math/formula/slider.d.ts +9 -0
- package/dist/math/formula/slider.js +32 -0
- package/dist/math/formula/space.d.ts +10 -0
- package/dist/math/formula/space.js +94 -0
- package/dist/math/formula/syntax.d.ts +77 -0
- package/dist/math/formula/syntax.js +111 -0
- package/dist/math/formula/token.d.ts +43 -0
- package/dist/math/formula/token.js +150 -0
- package/dist/math/plane.d.ts +148 -5
- package/dist/math/plane.js +16 -4
- package/dist/math/scene.js +17 -4
- package/dist/math/solid.d.ts +124 -0
- package/dist/math/solid.js +86 -0
- package/dist/math/space.d.ts +214 -0
- package/dist/math/space.js +8 -2
- package/dist/math/visual.d.ts +545 -3
- package/dist/math/visual.js +12 -2
- package/package.json +57 -1
package/dist/math/bounds.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { type Schema } from "effect";
|
|
2
|
+
import type { FormulaScope } from "#contracts/math/formula/compile";
|
|
2
3
|
import type { PlaneLabelAnchor, PlaneMathFrame, PlaneMathObject } from "#contracts/math/plane";
|
|
3
4
|
import type { SpaceLabelAnchor, SpaceMathFrame, SpaceMathObject, SpaceMathView } from "#contracts/math/space";
|
|
4
5
|
/** Reports every finite plane object or label that escapes its authored frame. */
|
|
5
|
-
export declare function planeBoundsIssues(frame: PlaneMathFrame, objects: readonly PlaneMathObject[], labels: readonly PlaneLabelAnchor[]): readonly Schema.FilterIssue[];
|
|
6
|
+
export declare function planeBoundsIssues(frame: PlaneMathFrame, objects: readonly PlaneMathObject[], labels: readonly PlaneLabelAnchor[], scope?: FormulaScope): readonly Schema.FilterIssue[];
|
|
6
7
|
/** Reports every finite space object, label, or view target outside its frame. */
|
|
7
|
-
export declare function spaceBoundsIssues(frame: SpaceMathFrame, objects: readonly SpaceMathObject[], labels: readonly SpaceLabelAnchor[], view: SpaceMathView): readonly Schema.FilterIssue[];
|
|
8
|
+
export declare function spaceBoundsIssues(frame: SpaceMathFrame, objects: readonly SpaceMathObject[], labels: readonly SpaceLabelAnchor[], view: SpaceMathView, scope?: FormulaScope): readonly Schema.FilterIssue[];
|
package/dist/math/bounds.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
import { Array as Arr } from "effect";
|
|
1
|
+
import { Array as Arr, Option, Result } from "effect";
|
|
2
2
|
import { arcContained, axisContains, circleContained, cuboidContained, quadraticContained, } from "#contracts/math/extent";
|
|
3
|
+
import { isPlaneFormulaPoint } from "#contracts/math/formula/field";
|
|
4
|
+
import { planeFormulaGeometry, planeFormulaPoint, } from "#contracts/math/formula/plane";
|
|
5
|
+
import { geometryVisible } from "#contracts/math/formula/scope";
|
|
6
|
+
import { spaceFormulaGeometry } from "#contracts/math/formula/space";
|
|
3
7
|
import { infinitePathIntersectsBox } from "#contracts/math/intersection";
|
|
4
8
|
/** Checks one plane point against both inclusive frame ranges. */
|
|
5
9
|
function planeContains(frame, point) {
|
|
@@ -31,8 +35,29 @@ function spaceAxes(frame, start, through) {
|
|
|
31
35
|
{ range: frame.z, start: start.z, through: through.z },
|
|
32
36
|
];
|
|
33
37
|
}
|
|
34
|
-
/**
|
|
35
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Whether a formula's samples show anything inside the frame. A formula that
|
|
40
|
+
* does not read is reported by its own field, so it is not reported again.
|
|
41
|
+
*/
|
|
42
|
+
function sampledVisible(geometry, frame) {
|
|
43
|
+
return Result.isFailure(geometry) || geometryVisible(geometry.success, frame);
|
|
44
|
+
}
|
|
45
|
+
/** Checks whether one plane object shows visible geometry in its frame. */
|
|
46
|
+
function planeObjectContained(frame, object, scope, labels) {
|
|
47
|
+
if (object.kind === "function") {
|
|
48
|
+
const axis = object.inputAxis ?? "x";
|
|
49
|
+
const named = Arr.flatMap(labels, ({ at, objectId }) => objectId === object.id && !isPlaneFormulaPoint(at) ? [at[axis]] : []);
|
|
50
|
+
return sampledVisible(planeFormulaGeometry(object, frame, scope, named), frame);
|
|
51
|
+
}
|
|
52
|
+
if (object.kind === "implicit" ||
|
|
53
|
+
object.kind === "loop" ||
|
|
54
|
+
object.kind === "marker" ||
|
|
55
|
+
object.kind === "parametric" ||
|
|
56
|
+
object.kind === "path" ||
|
|
57
|
+
object.kind === "polar" ||
|
|
58
|
+
object.kind === "region") {
|
|
59
|
+
return sampledVisible(planeFormulaGeometry(object, frame, scope), frame);
|
|
60
|
+
}
|
|
36
61
|
if (object.kind === "line") {
|
|
37
62
|
const [start, through] = object.through;
|
|
38
63
|
return infinitePathIntersectsBox(object.kind, planeAxes(frame, start, through));
|
|
@@ -58,7 +83,15 @@ function planeObjectContained(frame, object) {
|
|
|
58
83
|
return quadraticContained(frame, object);
|
|
59
84
|
}
|
|
60
85
|
/** Checks whether one space object has complete visible geometry in its frame. */
|
|
61
|
-
function spaceObjectContained(frame, object) {
|
|
86
|
+
function spaceObjectContained(frame, object, scope) {
|
|
87
|
+
if (object.kind === "function" ||
|
|
88
|
+
object.kind === "parametric" ||
|
|
89
|
+
object.kind === "prism" ||
|
|
90
|
+
object.kind === "pyramid" ||
|
|
91
|
+
object.kind === "revolution" ||
|
|
92
|
+
object.kind === "surface") {
|
|
93
|
+
return sampledVisible(spaceFormulaGeometry(object, frame, scope), frame);
|
|
94
|
+
}
|
|
62
95
|
if (object.kind === "line") {
|
|
63
96
|
const [start, through] = object.through;
|
|
64
97
|
return infinitePathIntersectsBox(object.kind, spaceAxes(frame, start, through));
|
|
@@ -77,10 +110,23 @@ function spaceObjectContained(frame, object) {
|
|
|
77
110
|
}
|
|
78
111
|
return cuboidContained(frame, object);
|
|
79
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* Whether a plane label starts inside its frame. A label placed by formulas is
|
|
115
|
+
* checked where the sliders start; a formula that does not read is reported by
|
|
116
|
+
* its own field, so it is not reported again.
|
|
117
|
+
*/
|
|
118
|
+
function planeLabelContained(frame, label, scope) {
|
|
119
|
+
if (!isPlaneFormulaPoint(label.at)) {
|
|
120
|
+
return planeContains(frame, label.at);
|
|
121
|
+
}
|
|
122
|
+
const placed = planeFormulaPoint(label.at, scope);
|
|
123
|
+
return (Result.isFailure(placed) ||
|
|
124
|
+
Option.exists(placed.success, (point) => planeContains(frame, point)));
|
|
125
|
+
}
|
|
80
126
|
/** Reports every finite plane object or label that escapes its authored frame. */
|
|
81
|
-
export function planeBoundsIssues(frame, objects, labels) {
|
|
127
|
+
export function planeBoundsIssues(frame, objects, labels, scope = {}) {
|
|
82
128
|
return [
|
|
83
|
-
...Arr.flatMap(objects, (object, index) => planeObjectContained(frame, object)
|
|
129
|
+
...Arr.flatMap(objects, (object, index) => planeObjectContained(frame, object, scope, labels)
|
|
84
130
|
? []
|
|
85
131
|
: [
|
|
86
132
|
{
|
|
@@ -88,7 +134,7 @@ export function planeBoundsIssues(frame, objects, labels) {
|
|
|
88
134
|
path: ["objects", index],
|
|
89
135
|
},
|
|
90
136
|
]),
|
|
91
|
-
...Arr.flatMap(labels, (label, index) =>
|
|
137
|
+
...Arr.flatMap(labels, (label, index) => planeLabelContained(frame, label, scope)
|
|
92
138
|
? []
|
|
93
139
|
: [
|
|
94
140
|
{
|
|
@@ -99,7 +145,7 @@ export function planeBoundsIssues(frame, objects, labels) {
|
|
|
99
145
|
];
|
|
100
146
|
}
|
|
101
147
|
/** Reports every finite space object, label, or view target outside its frame. */
|
|
102
|
-
export function spaceBoundsIssues(frame, objects, labels, view) {
|
|
148
|
+
export function spaceBoundsIssues(frame, objects, labels, view, scope = {}) {
|
|
103
149
|
const viewIssues = view.kind === "isometric" &&
|
|
104
150
|
view.target !== undefined &&
|
|
105
151
|
!spaceContains(frame, view.target)
|
|
@@ -111,7 +157,7 @@ export function spaceBoundsIssues(frame, objects, labels, view) {
|
|
|
111
157
|
]
|
|
112
158
|
: [];
|
|
113
159
|
return [
|
|
114
|
-
...Arr.flatMap(objects, (object, index) => spaceObjectContained(frame, object)
|
|
160
|
+
...Arr.flatMap(objects, (object, index) => spaceObjectContained(frame, object, scope)
|
|
115
161
|
? []
|
|
116
162
|
: [
|
|
117
163
|
{
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** The plane objects a formula defines, which a renderer samples itself. */
|
|
3
|
+
export declare const PlaneCurveObjectSchemas: readonly [Schema.Struct<{
|
|
4
|
+
readonly appearance: Schema.Literals<readonly ["answer", "construction", "highlight", "primary", "reference", "secondary", "warning"]>;
|
|
5
|
+
readonly id: Schema.String;
|
|
6
|
+
readonly domain: Schema.optionalKey<Schema.Struct<{
|
|
7
|
+
readonly max: Schema.Finite;
|
|
8
|
+
readonly min: Schema.Finite;
|
|
9
|
+
}>>;
|
|
10
|
+
readonly formula: Schema.String;
|
|
11
|
+
readonly inputAxis: Schema.optionalKey<Schema.Literals<readonly ["x", "y"]>>;
|
|
12
|
+
readonly kind: Schema.Literal<"function">;
|
|
13
|
+
}>, Schema.Struct<{
|
|
14
|
+
readonly appearance: Schema.Literals<readonly ["answer", "construction", "highlight", "primary", "reference", "secondary", "warning"]>;
|
|
15
|
+
readonly id: Schema.String;
|
|
16
|
+
readonly equation: Schema.String;
|
|
17
|
+
readonly kind: Schema.Literal<"implicit">;
|
|
18
|
+
}>, Schema.Struct<{
|
|
19
|
+
readonly appearance: Schema.Literals<readonly ["answer", "construction", "highlight", "primary", "reference", "secondary", "warning"]>;
|
|
20
|
+
readonly id: Schema.String;
|
|
21
|
+
readonly kind: Schema.Literal<"loop">;
|
|
22
|
+
readonly points: Schema.$Array<Schema.Struct<{
|
|
23
|
+
readonly x: Schema.String;
|
|
24
|
+
readonly y: Schema.String;
|
|
25
|
+
}>>;
|
|
26
|
+
}>, Schema.Struct<{
|
|
27
|
+
readonly appearance: Schema.Literals<readonly ["answer", "construction", "highlight", "primary", "reference", "secondary", "warning"]>;
|
|
28
|
+
readonly id: Schema.String;
|
|
29
|
+
readonly kind: Schema.Literal<"marker">;
|
|
30
|
+
readonly x: Schema.String;
|
|
31
|
+
readonly y: Schema.String;
|
|
32
|
+
}>, Schema.Struct<{
|
|
33
|
+
readonly appearance: Schema.Literals<readonly ["answer", "construction", "highlight", "primary", "reference", "secondary", "warning"]>;
|
|
34
|
+
readonly id: Schema.String;
|
|
35
|
+
readonly kind: Schema.Literal<"parametric">;
|
|
36
|
+
readonly parameter: Schema.Struct<{
|
|
37
|
+
readonly max: Schema.Finite;
|
|
38
|
+
readonly min: Schema.Finite;
|
|
39
|
+
}>;
|
|
40
|
+
readonly x: Schema.String;
|
|
41
|
+
readonly y: Schema.String;
|
|
42
|
+
}>, Schema.Struct<{
|
|
43
|
+
readonly appearance: Schema.Literals<readonly ["answer", "construction", "highlight", "primary", "reference", "secondary", "warning"]>;
|
|
44
|
+
readonly id: Schema.String;
|
|
45
|
+
readonly kind: Schema.Literal<"path">;
|
|
46
|
+
readonly points: Schema.$Array<Schema.Struct<{
|
|
47
|
+
readonly x: Schema.String;
|
|
48
|
+
readonly y: Schema.String;
|
|
49
|
+
}>>;
|
|
50
|
+
}>, Schema.Struct<{
|
|
51
|
+
readonly appearance: Schema.Literals<readonly ["answer", "construction", "highlight", "primary", "reference", "secondary", "warning"]>;
|
|
52
|
+
readonly id: Schema.String;
|
|
53
|
+
readonly angle: Schema.Struct<{
|
|
54
|
+
readonly max: Schema.Finite;
|
|
55
|
+
readonly min: Schema.Finite;
|
|
56
|
+
}>;
|
|
57
|
+
readonly kind: Schema.Literal<"polar">;
|
|
58
|
+
readonly radius: Schema.String;
|
|
59
|
+
}>, Schema.Struct<{
|
|
60
|
+
readonly appearance: Schema.Literals<readonly ["answer", "construction", "highlight", "primary", "reference", "secondary", "warning"]>;
|
|
61
|
+
readonly id: Schema.String;
|
|
62
|
+
readonly inequalities: Schema.NonEmptyArray<Schema.String>;
|
|
63
|
+
readonly kind: Schema.Literal<"region">;
|
|
64
|
+
}>];
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import { MathAxisRangeSchema } from "#contracts/math/base";
|
|
3
|
+
import { EquationTextSchema, FormulaTextSchema, InequalityTextSchema, PlaneFormulaPointSchema, } from "#contracts/math/formula/field";
|
|
4
|
+
import { MathObjectFields } from "#contracts/math/object";
|
|
5
|
+
/**
|
|
6
|
+
* The graph of y = f(x), or of x = f(y) when its input axis is y. The renderer
|
|
7
|
+
* samples it inside the frame, at least 200 times per visible branch, exactly
|
|
8
|
+
* at its domain ends and labels, and splits it at every break and asymptote.
|
|
9
|
+
* Without a domain it spans the frame along its input axis.
|
|
10
|
+
*/
|
|
11
|
+
const PlaneFunctionObjectSchema = Schema.Struct({
|
|
12
|
+
...MathObjectFields,
|
|
13
|
+
domain: Schema.optionalKey(MathAxisRangeSchema),
|
|
14
|
+
formula: FormulaTextSchema,
|
|
15
|
+
inputAxis: Schema.optionalKey(Schema.Literals(["x", "y"])),
|
|
16
|
+
kind: Schema.Literal("function"),
|
|
17
|
+
});
|
|
18
|
+
/** A curve traced by x(t) and y(t) while t runs over its parameter range. */
|
|
19
|
+
const PlaneParametricObjectSchema = Schema.Struct({
|
|
20
|
+
...MathObjectFields,
|
|
21
|
+
kind: Schema.Literal("parametric"),
|
|
22
|
+
parameter: MathAxisRangeSchema,
|
|
23
|
+
x: FormulaTextSchema,
|
|
24
|
+
y: FormulaTextSchema,
|
|
25
|
+
});
|
|
26
|
+
/** A curve r = f(theta) in polar coordinates, theta in radians over its range. */
|
|
27
|
+
const PlanePolarObjectSchema = Schema.Struct({
|
|
28
|
+
...MathObjectFields,
|
|
29
|
+
angle: MathAxisRangeSchema,
|
|
30
|
+
kind: Schema.Literal("polar"),
|
|
31
|
+
radius: FormulaTextSchema,
|
|
32
|
+
});
|
|
33
|
+
/** Every point of the frame where an equation in x and y holds, such as a conic. */
|
|
34
|
+
const PlaneImplicitObjectSchema = Schema.Struct({
|
|
35
|
+
...MathObjectFields,
|
|
36
|
+
equation: EquationTextSchema,
|
|
37
|
+
kind: Schema.Literal("implicit"),
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* The part of the frame where every inequality holds, shaded, with each
|
|
41
|
+
* boundary drawn solid for <= or >= and dashed for < or >.
|
|
42
|
+
*/
|
|
43
|
+
const PlaneRegionObjectSchema = Schema.Struct({
|
|
44
|
+
...MathObjectFields,
|
|
45
|
+
inequalities: Schema.NonEmptyArray(InequalityTextSchema),
|
|
46
|
+
kind: Schema.Literal("region"),
|
|
47
|
+
});
|
|
48
|
+
/** A point placed by formulas of the sliders, which moves as they move. */
|
|
49
|
+
const PlaneMarkerObjectSchema = Schema.Struct({
|
|
50
|
+
...MathObjectFields,
|
|
51
|
+
kind: Schema.Literal("marker"),
|
|
52
|
+
x: FormulaTextSchema,
|
|
53
|
+
y: FormulaTextSchema,
|
|
54
|
+
});
|
|
55
|
+
/**
|
|
56
|
+
* Straight segments through points placed by formulas of the sliders, such as
|
|
57
|
+
* the moving radius of a unit circle. A `path` stays open.
|
|
58
|
+
*/
|
|
59
|
+
const PlanePathObjectSchema = Schema.Struct({
|
|
60
|
+
...MathObjectFields,
|
|
61
|
+
kind: Schema.Literal("path"),
|
|
62
|
+
points: Schema.Array(PlaneFormulaPointSchema).check(Schema.isMinLength(2)),
|
|
63
|
+
});
|
|
64
|
+
/**
|
|
65
|
+
* A closed outline through points placed by formulas of the sliders, such as
|
|
66
|
+
* a right triangle with angle a: the last point joins back to the first.
|
|
67
|
+
*/
|
|
68
|
+
const PlaneLoopObjectSchema = Schema.Struct({
|
|
69
|
+
...MathObjectFields,
|
|
70
|
+
kind: Schema.Literal("loop"),
|
|
71
|
+
points: Schema.Array(PlaneFormulaPointSchema).check(Schema.isMinLength(3)),
|
|
72
|
+
});
|
|
73
|
+
/** The plane objects a formula defines, which a renderer samples itself. */
|
|
74
|
+
export const PlaneCurveObjectSchemas = [
|
|
75
|
+
PlaneFunctionObjectSchema,
|
|
76
|
+
PlaneImplicitObjectSchema,
|
|
77
|
+
PlaneLoopObjectSchema,
|
|
78
|
+
PlaneMarkerObjectSchema,
|
|
79
|
+
PlaneParametricObjectSchema,
|
|
80
|
+
PlanePathObjectSchema,
|
|
81
|
+
PlanePolarObjectSchema,
|
|
82
|
+
PlaneRegionObjectSchema,
|
|
83
|
+
];
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** One sampled position; a plane curve keeps z at zero. */
|
|
3
|
+
export declare const SamplePointSchema: Schema.Struct<{
|
|
4
|
+
readonly x: Schema.Finite;
|
|
5
|
+
readonly y: Schema.Finite;
|
|
6
|
+
readonly z: Schema.Finite;
|
|
7
|
+
}>;
|
|
8
|
+
export type SamplePoint = typeof SamplePointSchema.Type;
|
|
9
|
+
/** One closed range of values, from its least to its greatest. */
|
|
10
|
+
export declare const SampleRangeSchema: Schema.Struct<{
|
|
11
|
+
readonly max: Schema.Finite;
|
|
12
|
+
readonly min: Schema.Finite;
|
|
13
|
+
}>;
|
|
14
|
+
export type SampleRange = typeof SampleRangeSchema.Type;
|
|
15
|
+
/** The frame a sampled curve is clipped to; a plane frame has no z range. */
|
|
16
|
+
export declare const SampleBoxSchema: Schema.Struct<{
|
|
17
|
+
readonly x: Schema.Struct<{
|
|
18
|
+
readonly max: Schema.Finite;
|
|
19
|
+
readonly min: Schema.Finite;
|
|
20
|
+
}>;
|
|
21
|
+
readonly y: Schema.Struct<{
|
|
22
|
+
readonly max: Schema.Finite;
|
|
23
|
+
readonly min: Schema.Finite;
|
|
24
|
+
}>;
|
|
25
|
+
readonly z: Schema.optionalKey<Schema.Struct<{
|
|
26
|
+
readonly max: Schema.Finite;
|
|
27
|
+
readonly min: Schema.Finite;
|
|
28
|
+
}>>;
|
|
29
|
+
}>;
|
|
30
|
+
export type SampleBox = typeof SampleBoxSchema.Type;
|
|
31
|
+
/** The three axes a sampled point has, in order. */
|
|
32
|
+
export declare const AXES: readonly ["x", "y", "z"];
|
|
33
|
+
/** Whether every coordinate of a point is finite. */
|
|
34
|
+
export declare function finite(point: SamplePoint): boolean;
|
|
35
|
+
/** The frame's ranges, with a plane frame's z pinned to zero. */
|
|
36
|
+
export declare function ranges(box: SampleBox): {
|
|
37
|
+
x: {
|
|
38
|
+
readonly max: number;
|
|
39
|
+
readonly min: number;
|
|
40
|
+
};
|
|
41
|
+
y: {
|
|
42
|
+
readonly max: number;
|
|
43
|
+
readonly min: number;
|
|
44
|
+
};
|
|
45
|
+
z: {
|
|
46
|
+
readonly max: number;
|
|
47
|
+
readonly min: number;
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
/** Distance between two points measured in frame spans, so every axis counts alike. */
|
|
51
|
+
export declare function spanDistance(box: SampleBox, from: SamplePoint, to: SamplePoint): number;
|
|
52
|
+
/** Whether a point lies inside the frame, its edges included. */
|
|
53
|
+
export declare function insideBox(box: SampleBox, point: SamplePoint): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Where a segment enters and leaves the frame, as fractions along it, or
|
|
56
|
+
* nothing when it misses the frame (Liang and Barsky's clipping).
|
|
57
|
+
*/
|
|
58
|
+
export declare function clipSegment(box: SampleBox, from: SamplePoint, to: SamplePoint): {
|
|
59
|
+
enter: number;
|
|
60
|
+
leave: number;
|
|
61
|
+
} | undefined;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Array as Arr, Schema } from "effect";
|
|
2
|
+
/** One sampled position; a plane curve keeps z at zero. */
|
|
3
|
+
export const SamplePointSchema = Schema.Struct({
|
|
4
|
+
x: Schema.Finite,
|
|
5
|
+
y: Schema.Finite,
|
|
6
|
+
z: Schema.Finite,
|
|
7
|
+
});
|
|
8
|
+
/** One closed range of values, from its least to its greatest. */
|
|
9
|
+
export const SampleRangeSchema = Schema.Struct({
|
|
10
|
+
max: Schema.Finite,
|
|
11
|
+
min: Schema.Finite,
|
|
12
|
+
});
|
|
13
|
+
/** The frame a sampled curve is clipped to; a plane frame has no z range. */
|
|
14
|
+
export const SampleBoxSchema = Schema.Struct({
|
|
15
|
+
x: SampleRangeSchema,
|
|
16
|
+
y: SampleRangeSchema,
|
|
17
|
+
z: Schema.optionalKey(SampleRangeSchema),
|
|
18
|
+
});
|
|
19
|
+
/** The three axes a sampled point has, in order. */
|
|
20
|
+
export const AXES = ["x", "y", "z"];
|
|
21
|
+
/** Whether every coordinate of a point is finite. */
|
|
22
|
+
export function finite(point) {
|
|
23
|
+
return (Number.isFinite(point.x) &&
|
|
24
|
+
Number.isFinite(point.y) &&
|
|
25
|
+
Number.isFinite(point.z));
|
|
26
|
+
}
|
|
27
|
+
/** The frame's ranges, with a plane frame's z pinned to zero. */
|
|
28
|
+
export function ranges(box) {
|
|
29
|
+
return { x: box.x, y: box.y, z: box.z ?? { max: 0, min: 0 } };
|
|
30
|
+
}
|
|
31
|
+
/** Distance between two points measured in frame spans, so every axis counts alike. */
|
|
32
|
+
export function spanDistance(box, from, to) {
|
|
33
|
+
const all = ranges(box);
|
|
34
|
+
return Math.hypot(...Arr.map(AXES, (axis) => {
|
|
35
|
+
const span = all[axis].max - all[axis].min;
|
|
36
|
+
return span > 0 ? (to[axis] - from[axis]) / span : 0;
|
|
37
|
+
}));
|
|
38
|
+
}
|
|
39
|
+
/** Whether a point lies inside the frame, its edges included. */
|
|
40
|
+
export function insideBox(box, point) {
|
|
41
|
+
const all = ranges(box);
|
|
42
|
+
return (finite(point) &&
|
|
43
|
+
Arr.every(AXES, (axis) => point[axis] >= all[axis].min && point[axis] <= all[axis].max));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Where a segment enters and leaves the frame, as fractions along it, or
|
|
47
|
+
* nothing when it misses the frame (Liang and Barsky's clipping).
|
|
48
|
+
*/
|
|
49
|
+
export function clipSegment(box, from, to) {
|
|
50
|
+
const all = ranges(box);
|
|
51
|
+
let enter = 0;
|
|
52
|
+
let leave = 1;
|
|
53
|
+
for (const axis of AXES) {
|
|
54
|
+
const delta = to[axis] - from[axis];
|
|
55
|
+
const { max, min } = all[axis];
|
|
56
|
+
if (delta === 0) {
|
|
57
|
+
if (from[axis] < min || from[axis] > max) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const first = (min - from[axis]) / delta;
|
|
63
|
+
const second = (max - from[axis]) / delta;
|
|
64
|
+
enter = Math.max(enter, Math.min(first, second));
|
|
65
|
+
leave = Math.min(leave, Math.max(first, second));
|
|
66
|
+
}
|
|
67
|
+
return enter <= leave ? { enter, leave } : undefined;
|
|
68
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type FormulaNode } from "#contracts/math/formula/syntax";
|
|
2
|
+
/** The value of every variable and parameter a formula reads, by name. */
|
|
3
|
+
export type FormulaScope = Readonly<Record<string, number>>;
|
|
4
|
+
/** A compiled formula: its value for one scope, or NaN where it is undefined. */
|
|
5
|
+
export type FormulaEvaluator = (scope: FormulaScope) => number;
|
|
6
|
+
/**
|
|
7
|
+
* Compiles a parsed formula into a function of its variables. It never calls
|
|
8
|
+
* `eval` or `Function`: each node becomes a closure over its children, so a
|
|
9
|
+
* curve can be sampled thousands of times after one compile.
|
|
10
|
+
*/
|
|
11
|
+
export declare function compileFormula(node: FormulaNode): FormulaEvaluator;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Array as Arr } from "effect";
|
|
2
|
+
import { FORMULA_CONSTANTS, } from "#contracts/math/formula/syntax";
|
|
3
|
+
/** Odd root indexes tried for a negative base, so `x^(1/3)` is real for x < 0. */
|
|
4
|
+
const ODD_ROOTS = [3, 5, 7, 9, 11];
|
|
5
|
+
const ROOT_TOLERANCE = 1e-9;
|
|
6
|
+
/**
|
|
7
|
+
* Raises a base to a power. A negative base with a fractional exponent whose
|
|
8
|
+
* denominator is odd, such as 1/3 or 2/5, has a real value, so it keeps one.
|
|
9
|
+
*/
|
|
10
|
+
function power(base, exponent) {
|
|
11
|
+
const direct = base ** exponent;
|
|
12
|
+
if (!Number.isNaN(direct) || base >= 0) {
|
|
13
|
+
return direct;
|
|
14
|
+
}
|
|
15
|
+
for (const root of ODD_ROOTS) {
|
|
16
|
+
const numerator = exponent * root;
|
|
17
|
+
const whole = Math.round(numerator);
|
|
18
|
+
if (Math.abs(numerator - whole) < ROOT_TOLERANCE) {
|
|
19
|
+
return (whole % 2 === 0 ? 1 : -1) * Math.abs(base) ** exponent;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return Number.NaN;
|
|
23
|
+
}
|
|
24
|
+
/** How each operator combines its two values. */
|
|
25
|
+
const OPERATORS = {
|
|
26
|
+
"-": (left, right) => left - right,
|
|
27
|
+
"*": (left, right) => left * right,
|
|
28
|
+
"/": (left, right) => left / right,
|
|
29
|
+
"^": power,
|
|
30
|
+
"+": (left, right) => left + right,
|
|
31
|
+
};
|
|
32
|
+
/** What each function of one argument computes, in radians. */
|
|
33
|
+
const UNARY = {
|
|
34
|
+
abs: Math.abs,
|
|
35
|
+
acos: Math.acos,
|
|
36
|
+
asin: Math.asin,
|
|
37
|
+
atan: Math.atan,
|
|
38
|
+
cbrt: Math.cbrt,
|
|
39
|
+
ceil: Math.ceil,
|
|
40
|
+
cos: Math.cos,
|
|
41
|
+
cosh: Math.cosh,
|
|
42
|
+
cot: (value) => 1 / Math.tan(value),
|
|
43
|
+
csc: (value) => 1 / Math.sin(value),
|
|
44
|
+
exp: Math.exp,
|
|
45
|
+
floor: Math.floor,
|
|
46
|
+
ln: Math.log,
|
|
47
|
+
log: Math.log10,
|
|
48
|
+
sec: (value) => 1 / Math.cos(value),
|
|
49
|
+
sign: Math.sign,
|
|
50
|
+
sin: Math.sin,
|
|
51
|
+
sinh: Math.sinh,
|
|
52
|
+
sqrt: Math.sqrt,
|
|
53
|
+
tan: Math.tan,
|
|
54
|
+
tanh: Math.tanh,
|
|
55
|
+
};
|
|
56
|
+
/** Reads one name: a constant's value, or the scope's value for a variable. */
|
|
57
|
+
function compileName(name) {
|
|
58
|
+
if (name === "pi" || name === "e") {
|
|
59
|
+
const value = FORMULA_CONSTANTS[name];
|
|
60
|
+
return () => value;
|
|
61
|
+
}
|
|
62
|
+
return (scope) => scope[name] ?? Number.NaN;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Compiles a parsed formula into a function of its variables. It never calls
|
|
66
|
+
* `eval` or `Function`: each node becomes a closure over its children, so a
|
|
67
|
+
* curve can be sampled thousands of times after one compile.
|
|
68
|
+
*/
|
|
69
|
+
export function compileFormula(node) {
|
|
70
|
+
if (node.kind === "number") {
|
|
71
|
+
const { value } = node;
|
|
72
|
+
return () => value;
|
|
73
|
+
}
|
|
74
|
+
if (node.kind === "name") {
|
|
75
|
+
return compileName(node.name);
|
|
76
|
+
}
|
|
77
|
+
if (node.kind === "negate") {
|
|
78
|
+
const operand = compileFormula(node.operand);
|
|
79
|
+
return (scope) => -operand(scope);
|
|
80
|
+
}
|
|
81
|
+
if (node.kind === "binary") {
|
|
82
|
+
const left = compileFormula(node.left);
|
|
83
|
+
const right = compileFormula(node.right);
|
|
84
|
+
const combine = OPERATORS[node.operator];
|
|
85
|
+
return (scope) => combine(left(scope), right(scope));
|
|
86
|
+
}
|
|
87
|
+
if (node.kind === "logarithm") {
|
|
88
|
+
const base = compileFormula(node.base);
|
|
89
|
+
const value = compileFormula(node.value);
|
|
90
|
+
return (scope) => Math.log(value(scope)) / Math.log(base(scope));
|
|
91
|
+
}
|
|
92
|
+
const [first, ...rest] = Arr.map(node.args, compileFormula);
|
|
93
|
+
const { callee } = node;
|
|
94
|
+
if (callee === "max" || callee === "min") {
|
|
95
|
+
const pick = callee === "max" ? Math.max : Math.min;
|
|
96
|
+
return (scope) => Arr.reduce(rest, first(scope), (value, each) => pick(value, each(scope)));
|
|
97
|
+
}
|
|
98
|
+
const apply = UNARY[callee];
|
|
99
|
+
return (scope) => apply(first(scope));
|
|
100
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type SampleBox } from "#contracts/math/formula/box";
|
|
2
|
+
import type { SampleBranch } from "#contracts/math/formula/sample";
|
|
3
|
+
/** A field over the plane: its value at one point, NaN where it is undefined. */
|
|
4
|
+
export type SampleField = (x: number, y: number) => number;
|
|
5
|
+
/** Cells per frame side, enough for a smooth outline at the largest card size. */
|
|
6
|
+
export declare const CONTOUR_CELLS = 240;
|
|
7
|
+
/**
|
|
8
|
+
* Traces every curve where a field is zero inside the frame, the marching
|
|
9
|
+
* squares method, so an equation such as `x^2 + y^2 = 4` draws its circle.
|
|
10
|
+
*/
|
|
11
|
+
export declare function contourLines(field: SampleField, box: SampleBox, cells?: number): readonly SampleBranch[];
|
|
12
|
+
/**
|
|
13
|
+
* Shades the part of the frame where a field is at least zero, one shape per
|
|
14
|
+
* cell: the corners inside and the crossings between them, so a cell wholly
|
|
15
|
+
* inside is its square and a cell the outline cuts keeps only its inner part.
|
|
16
|
+
*/
|
|
17
|
+
export declare function contourFill(field: SampleField, box: SampleBox, cells?: number): readonly SampleBranch[];
|