@nakafa/aksara-contracts 6.0.0 → 6.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assessment/figure.d.ts +468 -0
- package/dist/math/bounds.d.ts +3 -2
- package/dist/math/bounds.js +37 -8
- package/dist/math/curve.d.ts +42 -0
- package/dist/math/curve.js +55 -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 +19 -0
- package/dist/math/formula/field.js +35 -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 +62 -0
- package/dist/math/formula/plane.js +175 -0
- package/dist/math/formula/reader.d.ts +102 -0
- package/dist/math/formula/reader.js +58 -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 +37 -0
- package/dist/math/formula/scope.js +137 -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 +85 -0
- package/dist/math/plane.js +8 -2
- package/dist/math/scene.js +11 -2
- package/dist/math/solid.d.ts +124 -0
- package/dist/math/solid.js +86 -0
- package/dist/math/space.d.ts +213 -0
- package/dist/math/space.js +8 -2
- package/dist/math/visual.d.ts +454 -0
- package/package.json +57 -1
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { Array as Arr, MutableList, Order, Schema } from "effect";
|
|
2
|
+
import { clipSegment, finite, insideBox, SamplePointSchema, spanDistance, } from "#contracts/math/formula/box";
|
|
3
|
+
/** Fewest samples on each visible branch, the Aksara visuals rule. */
|
|
4
|
+
export const MINIMUM_BRANCH_SAMPLES = 200;
|
|
5
|
+
const BASE_SAMPLES = 2 * MINIMUM_BRANCH_SAMPLES;
|
|
6
|
+
const MAXIMUM_DEPTH = 12;
|
|
7
|
+
const MAXIMUM_SAMPLES = 24_000;
|
|
8
|
+
/** Largest gap between a curve and its chord, as a fraction of the frame. */
|
|
9
|
+
const FLATNESS = 0.0015;
|
|
10
|
+
/** A chord longer than this is split further even where the curve looks flat. */
|
|
11
|
+
const JUMP = 0.2;
|
|
12
|
+
/** A chord this long at the deepest split crosses a break, a step, or an asymptote. */
|
|
13
|
+
const BREAK = 0.004;
|
|
14
|
+
/** One sample: its parameter, its point, and whether a break follows it. */
|
|
15
|
+
const SampleSchema = Schema.Struct({
|
|
16
|
+
broken: Schema.mutableKey(Schema.Boolean),
|
|
17
|
+
parameter: Schema.Finite,
|
|
18
|
+
point: SamplePointSchema,
|
|
19
|
+
});
|
|
20
|
+
/**
|
|
21
|
+
* Splits one interval until the curve is flat between its samples, and marks
|
|
22
|
+
* a break where the chord still jumps at the deepest split or the curve is
|
|
23
|
+
* undefined inside it.
|
|
24
|
+
*/
|
|
25
|
+
function refine(trace, box, from, to, depth, out) {
|
|
26
|
+
const parameter = (from.parameter + to.parameter) / 2;
|
|
27
|
+
const middle = { broken: false, parameter, point: trace(parameter) };
|
|
28
|
+
const bothFinite = finite(from.point) && finite(to.point);
|
|
29
|
+
if (out.length >= MAXIMUM_SAMPLES) {
|
|
30
|
+
from.broken = from.broken || !bothFinite;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
if (depth >= MAXIMUM_DEPTH) {
|
|
34
|
+
from.broken =
|
|
35
|
+
from.broken ||
|
|
36
|
+
!bothFinite ||
|
|
37
|
+
spanDistance(box, from.point, to.point) > BREAK;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
if (bothFinite && finite(middle.point)) {
|
|
41
|
+
const chordMiddle = {
|
|
42
|
+
x: (from.point.x + to.point.x) / 2,
|
|
43
|
+
y: (from.point.y + to.point.y) / 2,
|
|
44
|
+
z: (from.point.z + to.point.z) / 2,
|
|
45
|
+
};
|
|
46
|
+
const flat = spanDistance(box, chordMiddle, middle.point) <= FLATNESS;
|
|
47
|
+
const short = spanDistance(box, from.point, to.point) <= JUMP;
|
|
48
|
+
if (flat && short) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
else if (!(finite(from.point) || finite(to.point) || finite(middle.point))) {
|
|
53
|
+
from.broken = true;
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
refine(trace, box, from, middle, depth + 1, out);
|
|
57
|
+
MutableList.append(out, middle);
|
|
58
|
+
refine(trace, box, middle, to, depth + 1, out);
|
|
59
|
+
}
|
|
60
|
+
/** Samples a parameter range densely and adaptively, keeping named values exact. */
|
|
61
|
+
function sampleRange(trace, box, min, max, named) {
|
|
62
|
+
const uniform = Arr.makeBy(BASE_SAMPLES + 1, (index) => min + ((max - min) * index) / BASE_SAMPLES);
|
|
63
|
+
const parameters = Arr.dedupe(Arr.sort(Arr.appendAll(uniform, Arr.filter(named, (value) => value > min && value < max)), Order.Number));
|
|
64
|
+
const base = Arr.map(parameters, (parameter) => ({
|
|
65
|
+
broken: false,
|
|
66
|
+
parameter,
|
|
67
|
+
point: trace(parameter),
|
|
68
|
+
}));
|
|
69
|
+
const out = MutableList.make();
|
|
70
|
+
Arr.forEach(base, (sample, index) => {
|
|
71
|
+
MutableList.append(out, sample);
|
|
72
|
+
const next = base[index + 1];
|
|
73
|
+
if (next) {
|
|
74
|
+
refine(trace, box, sample, next, 0, out);
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
return Arr.sort(MutableList.toArray(out), Order.mapInput(Order.Number, (sample) => sample.parameter));
|
|
78
|
+
}
|
|
79
|
+
/** The point a fraction of the way along a segment, and its parameter. */
|
|
80
|
+
function along(from, to, fraction) {
|
|
81
|
+
/** One coordinate the same fraction of the way along. */
|
|
82
|
+
const at = (axis) => from.point[axis] + (to.point[axis] - from.point[axis]) * fraction;
|
|
83
|
+
return {
|
|
84
|
+
broken: false,
|
|
85
|
+
parameter: from.parameter + (to.parameter - from.parameter) * fraction,
|
|
86
|
+
point: { x: at("x"), y: at("y"), z: at("z") },
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
const EDGE_STEPS = 48;
|
|
90
|
+
/**
|
|
91
|
+
* Finds where the curve itself crosses the frame edge between a sample inside
|
|
92
|
+
* and a sample outside, by halving the parameter interval, so the edge sample
|
|
93
|
+
* lies on the curve rather than on a chord.
|
|
94
|
+
*/
|
|
95
|
+
function edge(trace, box, inner, outer) {
|
|
96
|
+
let inside = inner.parameter;
|
|
97
|
+
let outside = outer.parameter;
|
|
98
|
+
for (let step = 0; step < EDGE_STEPS; step += 1) {
|
|
99
|
+
const middle = (inside + outside) / 2;
|
|
100
|
+
if (insideBox(box, trace(middle))) {
|
|
101
|
+
inside = middle;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
outside = middle;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return { broken: false, parameter: inside, point: trace(inside) };
|
|
108
|
+
}
|
|
109
|
+
/** The sample where a visible piece of a segment starts: its start, or its entry on the curve. */
|
|
110
|
+
function entry(trace, box, from, to, clip) {
|
|
111
|
+
if (clip.enter === 0) {
|
|
112
|
+
return from;
|
|
113
|
+
}
|
|
114
|
+
return clip.leave === 1
|
|
115
|
+
? edge(trace, box, to, from)
|
|
116
|
+
: along(from, to, clip.enter);
|
|
117
|
+
}
|
|
118
|
+
/** The sample where a visible piece of a segment ends: its end, or its exit on the curve. */
|
|
119
|
+
function exit(trace, box, from, to, clip) {
|
|
120
|
+
if (clip.leave === 1) {
|
|
121
|
+
return to;
|
|
122
|
+
}
|
|
123
|
+
return clip.enter === 0
|
|
124
|
+
? edge(trace, box, from, to)
|
|
125
|
+
: along(from, to, clip.leave);
|
|
126
|
+
}
|
|
127
|
+
/** Collects runs of samples, keeping only runs of two or more. */
|
|
128
|
+
function runCollector() {
|
|
129
|
+
const runs = MutableList.make();
|
|
130
|
+
let run = MutableList.make();
|
|
131
|
+
return {
|
|
132
|
+
add: (sample) => MutableList.append(run, sample),
|
|
133
|
+
close: () => {
|
|
134
|
+
const items = MutableList.toArray(run);
|
|
135
|
+
if (Arr.isReadonlyArrayNonEmpty(items) && items.length > 1) {
|
|
136
|
+
MutableList.append(runs, items);
|
|
137
|
+
}
|
|
138
|
+
run = MutableList.make();
|
|
139
|
+
},
|
|
140
|
+
empty: () => run.length === 0,
|
|
141
|
+
runs: () => MutableList.toArray(runs),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/** Splits samples into visible runs inside the frame, cut exactly at its edges. */
|
|
145
|
+
function visibleRuns(trace, box, samples) {
|
|
146
|
+
const collector = runCollector();
|
|
147
|
+
Arr.forEach(samples, (from, index) => {
|
|
148
|
+
const to = samples[index + 1];
|
|
149
|
+
const clip = to && !from.broken && finite(from.point) && finite(to.point)
|
|
150
|
+
? clipSegment(box, from.point, to.point)
|
|
151
|
+
: undefined;
|
|
152
|
+
if (!(to && clip)) {
|
|
153
|
+
collector.close();
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (clip.enter > 0 || collector.empty()) {
|
|
157
|
+
collector.close();
|
|
158
|
+
collector.add(entry(trace, box, from, to, clip));
|
|
159
|
+
}
|
|
160
|
+
collector.add(exit(trace, box, from, to, clip));
|
|
161
|
+
if (clip.leave < 1) {
|
|
162
|
+
collector.close();
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
collector.close();
|
|
166
|
+
return collector.runs();
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Samples one curve inside its frame. Every visible branch holds at least 200
|
|
170
|
+
* samples, more where it bends or climbs, with an exact sample at each named
|
|
171
|
+
* parameter value and at each frame edge it crosses. Breaks, undefined
|
|
172
|
+
* stretches, and asymptotes split the curve into separate branches.
|
|
173
|
+
*/
|
|
174
|
+
export function sampleCurve(trace, box, range, named = []) {
|
|
175
|
+
const runs = visibleRuns(trace, box, sampleRange(trace, box, range.min, range.max, named));
|
|
176
|
+
return Arr.flatMap(runs, (run) => {
|
|
177
|
+
const dense = run.length >= MINIMUM_BRANCH_SAMPLES
|
|
178
|
+
? [run]
|
|
179
|
+
: visibleRuns(trace, box, sampleRange(trace, box, Arr.headNonEmpty(run).parameter, Arr.lastNonEmpty(run).parameter, named));
|
|
180
|
+
return Arr.map(dense, (each) => Arr.map(each, (sample) => sample.point));
|
|
181
|
+
});
|
|
182
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
import type { SampleBox } from "#contracts/math/formula/box";
|
|
3
|
+
import type { FormulaScope } from "#contracts/math/formula/compile";
|
|
4
|
+
import type { FormulaParameter } from "#contracts/math/formula/field";
|
|
5
|
+
import type { FormulaGeometry } from "#contracts/math/formula/plane";
|
|
6
|
+
import type { PlaneMathObject } from "#contracts/math/plane";
|
|
7
|
+
import type { SpaceMathObject } from "#contracts/math/space";
|
|
8
|
+
/** One authored formula of an object: where it stands and what it may read. */
|
|
9
|
+
declare const FormulaFieldSchema: Schema.Struct<{
|
|
10
|
+
readonly path: Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Int]>>;
|
|
11
|
+
readonly relation: Schema.Boolean;
|
|
12
|
+
readonly text: Schema.String;
|
|
13
|
+
readonly variables: Schema.$Array<Schema.String>;
|
|
14
|
+
}>;
|
|
15
|
+
export type FormulaField = typeof FormulaFieldSchema.Type;
|
|
16
|
+
/**
|
|
17
|
+
* Lists every formula a plane object holds and the variables each may read:
|
|
18
|
+
* its input axis for a function, t for a parametric curve, theta for a polar
|
|
19
|
+
* curve, and x and y for an equation or a region.
|
|
20
|
+
*/
|
|
21
|
+
export declare function planeFormulaFields(object: PlaneMathObject): readonly FormulaField[];
|
|
22
|
+
/**
|
|
23
|
+
* Lists every formula a space object holds and the variables each may read:
|
|
24
|
+
* t for a curve, x and y for a surface z = f(x, y), u and v for a parametric
|
|
25
|
+
* surface, and the axis variable for a solid of revolution.
|
|
26
|
+
*/
|
|
27
|
+
export declare function spaceFormulaFields(object: SpaceMathObject): readonly FormulaField[];
|
|
28
|
+
/**
|
|
29
|
+
* Reports every formula that reads a name its object does not offer, and every
|
|
30
|
+
* slider whose name repeats an earlier one.
|
|
31
|
+
*/
|
|
32
|
+
export declare function formulaScopeIssues<Object>(objects: readonly Object[], fields: (object: Object) => readonly FormulaField[], parameters: readonly FormulaParameter[]): readonly Schema.FilterIssue[];
|
|
33
|
+
/** Each slider's starting value by its name, the scope a scene first draws with. */
|
|
34
|
+
export declare function parameterScope(parameters: readonly FormulaParameter[]): FormulaScope;
|
|
35
|
+
/** Whether sampled geometry shows anything inside its frame. */
|
|
36
|
+
export declare function geometryVisible(geometry: FormulaGeometry, box: SampleBox): boolean;
|
|
37
|
+
export {};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { Array as Arr, HashSet, Option, Order, Record as Rec, Result, Schema, } from "effect";
|
|
2
|
+
import { formulaNames } from "#contracts/math/formula/names";
|
|
3
|
+
import { readFormula, readRelation } from "#contracts/math/formula/parse";
|
|
4
|
+
/** One authored formula of an object: where it stands and what it may read. */
|
|
5
|
+
const FormulaFieldSchema = Schema.Struct({
|
|
6
|
+
path: Schema.Array(Schema.Union([Schema.String, Schema.Int])),
|
|
7
|
+
relation: Schema.Boolean,
|
|
8
|
+
text: Schema.String,
|
|
9
|
+
variables: Schema.Array(Schema.String),
|
|
10
|
+
});
|
|
11
|
+
/** Builds one expression field. */
|
|
12
|
+
function expression(path, text, variables) {
|
|
13
|
+
return { path, relation: false, text, variables };
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Lists every formula a plane object holds and the variables each may read:
|
|
17
|
+
* its input axis for a function, t for a parametric curve, theta for a polar
|
|
18
|
+
* curve, and x and y for an equation or a region.
|
|
19
|
+
*/
|
|
20
|
+
export function planeFormulaFields(object) {
|
|
21
|
+
if (object.kind === "function") {
|
|
22
|
+
return [expression(["formula"], object.formula, [object.inputAxis ?? "x"])];
|
|
23
|
+
}
|
|
24
|
+
if (object.kind === "parametric") {
|
|
25
|
+
return [
|
|
26
|
+
expression(["x"], object.x, ["t"]),
|
|
27
|
+
expression(["y"], object.y, ["t"]),
|
|
28
|
+
];
|
|
29
|
+
}
|
|
30
|
+
if (object.kind === "polar") {
|
|
31
|
+
return [expression(["radius"], object.radius, ["theta"])];
|
|
32
|
+
}
|
|
33
|
+
if (object.kind === "implicit") {
|
|
34
|
+
return [
|
|
35
|
+
{
|
|
36
|
+
path: ["equation"],
|
|
37
|
+
relation: true,
|
|
38
|
+
text: object.equation,
|
|
39
|
+
variables: ["x", "y"],
|
|
40
|
+
},
|
|
41
|
+
];
|
|
42
|
+
}
|
|
43
|
+
if (object.kind === "region") {
|
|
44
|
+
return Arr.map(object.inequalities, (text, index) => ({
|
|
45
|
+
path: ["inequalities", index],
|
|
46
|
+
relation: true,
|
|
47
|
+
text,
|
|
48
|
+
variables: ["x", "y"],
|
|
49
|
+
}));
|
|
50
|
+
}
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Lists every formula a space object holds and the variables each may read:
|
|
55
|
+
* t for a curve, x and y for a surface z = f(x, y), u and v for a parametric
|
|
56
|
+
* surface, and the axis variable for a solid of revolution.
|
|
57
|
+
*/
|
|
58
|
+
export function spaceFormulaFields(object) {
|
|
59
|
+
if (object.kind === "parametric" || object.kind === "surface") {
|
|
60
|
+
const variables = object.kind === "parametric" ? ["t"] : ["u", "v"];
|
|
61
|
+
return Arr.map(["x", "y", "z"], (axis) => expression([axis], object[axis], variables));
|
|
62
|
+
}
|
|
63
|
+
if (object.kind === "function") {
|
|
64
|
+
return [expression(["formula"], object.formula, ["x", "y"])];
|
|
65
|
+
}
|
|
66
|
+
if (object.kind === "revolution") {
|
|
67
|
+
return [expression(["formula"], object.formula, [object.axis])];
|
|
68
|
+
}
|
|
69
|
+
return [];
|
|
70
|
+
}
|
|
71
|
+
/** Every name one formula reads, or none when it does not parse at all. */
|
|
72
|
+
function namesOf(field) {
|
|
73
|
+
if (field.relation) {
|
|
74
|
+
return Result.match(readRelation(field.text), {
|
|
75
|
+
onFailure: () => HashSet.empty(),
|
|
76
|
+
onSuccess: ({ left, right }) => HashSet.union(formulaNames(left), formulaNames(right)),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
return Result.match(readFormula(field.text), {
|
|
80
|
+
onFailure: () => HashSet.empty(),
|
|
81
|
+
onSuccess: formulaNames,
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Reports every formula that reads a name its object does not offer, and every
|
|
86
|
+
* slider whose name repeats an earlier one.
|
|
87
|
+
*/
|
|
88
|
+
export function formulaScopeIssues(objects, fields, parameters) {
|
|
89
|
+
const names = Arr.map(parameters, ({ name }) => name);
|
|
90
|
+
const declared = Arr.dedupe(names);
|
|
91
|
+
const repeated = Arr.flatMap(parameters, ({ name }, index) => Arr.contains(Arr.take(names, index), name)
|
|
92
|
+
? [
|
|
93
|
+
{
|
|
94
|
+
issue: "Expected a unique slider name.",
|
|
95
|
+
path: ["parameters", index, "name"],
|
|
96
|
+
},
|
|
97
|
+
]
|
|
98
|
+
: []);
|
|
99
|
+
const unknown = Arr.flatMap(objects, (object, index) => Arr.flatMap(fields(object), (field) => {
|
|
100
|
+
const allowed = HashSet.fromIterable([...field.variables, ...declared]);
|
|
101
|
+
const stray = Arr.sort(Arr.fromIterable(HashSet.difference(namesOf(field), allowed)), Order.String);
|
|
102
|
+
return Arr.isReadonlyArrayEmpty(stray)
|
|
103
|
+
? []
|
|
104
|
+
: [
|
|
105
|
+
{
|
|
106
|
+
issue: `Expected only ${Arr.join([...field.variables, ...declared], ", ")} in this formula, not ${Arr.join(stray, ", ")}.`,
|
|
107
|
+
path: ["objects", index, ...field.path],
|
|
108
|
+
},
|
|
109
|
+
];
|
|
110
|
+
}));
|
|
111
|
+
return [...repeated, ...unknown];
|
|
112
|
+
}
|
|
113
|
+
/** Each slider's starting value by its name, the scope a scene first draws with. */
|
|
114
|
+
export function parameterScope(parameters) {
|
|
115
|
+
return Rec.fromEntries(Arr.map(parameters, ({ name, value }) => [name, value]));
|
|
116
|
+
}
|
|
117
|
+
/** Whether a point lies inside a box; a plane box ignores z. */
|
|
118
|
+
function inside(box, point) {
|
|
119
|
+
/** Whether a value lies inside one range, its ends included. */
|
|
120
|
+
const within = (value, range) => value >= range.min && value <= range.max;
|
|
121
|
+
return (within(point.x, box.x) &&
|
|
122
|
+
within(point.y, box.y) &&
|
|
123
|
+
(box.z === undefined || within(point.z, box.z)));
|
|
124
|
+
}
|
|
125
|
+
/** Whether sampled geometry shows anything inside its frame. */
|
|
126
|
+
export function geometryVisible(geometry, box) {
|
|
127
|
+
if (geometry.kind === "curves") {
|
|
128
|
+
return Arr.some(geometry.branches, Arr.isReadonlyArrayNonEmpty);
|
|
129
|
+
}
|
|
130
|
+
if (geometry.kind === "region") {
|
|
131
|
+
return Arr.isReadonlyArrayNonEmpty(geometry.fill);
|
|
132
|
+
}
|
|
133
|
+
if (geometry.kind === "grid") {
|
|
134
|
+
return Arr.some(geometry.grid.points, (point) => Option.exists(point, (each) => inside(box, each)));
|
|
135
|
+
}
|
|
136
|
+
return Arr.every(geometry.faces, (face) => Arr.every(face, (corner) => inside(box, corner)));
|
|
137
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Result } from "effect";
|
|
2
|
+
import type { SampleBox } from "#contracts/math/formula/box";
|
|
3
|
+
import type { FormulaScope } from "#contracts/math/formula/compile";
|
|
4
|
+
import { type FormulaGeometry } from "#contracts/math/formula/plane";
|
|
5
|
+
import type { FormulaError } from "#contracts/math/formula/syntax";
|
|
6
|
+
import type { SpaceSolidObjectSchemas } from "#contracts/math/solid";
|
|
7
|
+
/** The space objects a formula or a face defines. */
|
|
8
|
+
export type SpaceFormulaObject = (typeof SpaceSolidObjectSchemas)[number]["Type"];
|
|
9
|
+
/** Samples one space formula object, or builds the faces of a solid. */
|
|
10
|
+
export declare function spaceFormulaGeometry(object: SpaceFormulaObject, box: SampleBox, scope: FormulaScope): Result.Result<FormulaGeometry, FormulaError>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { Array as Arr, Result } from "effect";
|
|
2
|
+
import { sampleGrid } from "#contracts/math/formula/mesh";
|
|
3
|
+
import { compileText, evaluateWith, } from "#contracts/math/formula/plane";
|
|
4
|
+
import { sampleCurve } from "#contracts/math/formula/sample";
|
|
5
|
+
const FULL_TURN = { max: 2 * Math.PI, min: 0 };
|
|
6
|
+
/** Moves one point by an offset. */
|
|
7
|
+
function shifted(point, offset) {
|
|
8
|
+
return {
|
|
9
|
+
x: point.x + offset.x,
|
|
10
|
+
y: point.y + offset.y,
|
|
11
|
+
z: point.z + offset.z,
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
/** Pairs each corner of a face with the next one, wrapping to the first. */
|
|
15
|
+
function edges(face) {
|
|
16
|
+
return Arr.zip(face, Arr.append(Arr.drop(face, 1), Arr.headNonEmpty(face)));
|
|
17
|
+
}
|
|
18
|
+
/** The faces of a prism: both bases and one quadrilateral per base edge. */
|
|
19
|
+
function prismFaces(base, offset) {
|
|
20
|
+
const top = Arr.map(base, (corner) => shifted(corner, offset));
|
|
21
|
+
return [
|
|
22
|
+
[...base],
|
|
23
|
+
[...top],
|
|
24
|
+
...Arr.map(edges(base), ([from, to]) => [
|
|
25
|
+
from,
|
|
26
|
+
to,
|
|
27
|
+
shifted(to, offset),
|
|
28
|
+
shifted(from, offset),
|
|
29
|
+
]),
|
|
30
|
+
];
|
|
31
|
+
}
|
|
32
|
+
/** The faces of a pyramid: its base and one triangle per base edge. */
|
|
33
|
+
function pyramidFaces(base, apex) {
|
|
34
|
+
return [[...base], ...Arr.map(edges(base), ([from, to]) => [from, to, apex])];
|
|
35
|
+
}
|
|
36
|
+
/** A point at a radius from an axis, at a coordinate along it and an angle around it. */
|
|
37
|
+
function revolve(axis, along, radius, angle) {
|
|
38
|
+
const across = radius * Math.cos(angle);
|
|
39
|
+
const depth = radius * Math.sin(angle);
|
|
40
|
+
if (axis === "x") {
|
|
41
|
+
return { x: along, y: across, z: depth };
|
|
42
|
+
}
|
|
43
|
+
return axis === "y"
|
|
44
|
+
? { x: depth, y: along, z: across }
|
|
45
|
+
: { x: across, y: depth, z: along };
|
|
46
|
+
}
|
|
47
|
+
/** Samples one space formula object, or builds the faces of a solid. */
|
|
48
|
+
export function spaceFormulaGeometry(object, box, scope) {
|
|
49
|
+
return Result.gen(function* () {
|
|
50
|
+
if (object.kind === "prism") {
|
|
51
|
+
return {
|
|
52
|
+
faces: prismFaces(object.base, object.offset),
|
|
53
|
+
kind: "faces",
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
if (object.kind === "pyramid") {
|
|
57
|
+
return {
|
|
58
|
+
faces: pyramidFaces(object.base, object.apex),
|
|
59
|
+
kind: "faces",
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
if (object.kind === "parametric") {
|
|
63
|
+
const fx = yield* compileText(object.x);
|
|
64
|
+
const fy = yield* compileText(object.y);
|
|
65
|
+
const fz = yield* compileText(object.z);
|
|
66
|
+
const branches = sampleCurve((t) => ({
|
|
67
|
+
x: evaluateWith(fx, scope, "t", t),
|
|
68
|
+
y: evaluateWith(fy, scope, "t", t),
|
|
69
|
+
z: evaluateWith(fz, scope, "t", t),
|
|
70
|
+
}), box, object.parameter);
|
|
71
|
+
return { branches, kind: "curves" };
|
|
72
|
+
}
|
|
73
|
+
if (object.kind === "function") {
|
|
74
|
+
const f = yield* compileText(object.formula);
|
|
75
|
+
const domain = object.domain ?? { x: box.x, y: box.y };
|
|
76
|
+
const grid = sampleGrid((x, y) => ({ x, y, z: f({ ...scope, x, y }) }), domain.x, domain.y);
|
|
77
|
+
return { grid, kind: "grid" };
|
|
78
|
+
}
|
|
79
|
+
if (object.kind === "surface") {
|
|
80
|
+
const fx = yield* compileText(object.x);
|
|
81
|
+
const fy = yield* compileText(object.y);
|
|
82
|
+
const fz = yield* compileText(object.z);
|
|
83
|
+
const grid = sampleGrid((u, v) => {
|
|
84
|
+
const point = { ...scope, u, v };
|
|
85
|
+
return { x: fx(point), y: fy(point), z: fz(point) };
|
|
86
|
+
}, object.u, object.v);
|
|
87
|
+
return { grid, kind: "grid" };
|
|
88
|
+
}
|
|
89
|
+
const radius = yield* compileText(object.formula);
|
|
90
|
+
const { axis } = object;
|
|
91
|
+
const grid = sampleGrid((along, angle) => revolve(axis, along, evaluateWith(radius, scope, axis, along), angle), object.domain, FULL_TURN);
|
|
92
|
+
return { grid, kind: "grid" };
|
|
93
|
+
});
|
|
94
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** Arithmetic operators of a formula, from the loosest binding to the tightest. */
|
|
3
|
+
export declare const FormulaOperatorSchema: Schema.Literals<readonly ["+", "-", "*", "/", "^"]>;
|
|
4
|
+
export type FormulaOperator = typeof FormulaOperatorSchema.Type;
|
|
5
|
+
/** Comparisons that turn two expressions into an equation or an inequality. */
|
|
6
|
+
export declare const FormulaComparisonSchema: Schema.Literals<readonly ["=", "<", "<=", ">", ">="]>;
|
|
7
|
+
export type FormulaComparison = typeof FormulaComparisonSchema.Type;
|
|
8
|
+
/** Functions of one argument, in radians where an angle is meant. */
|
|
9
|
+
export declare const FormulaUnarySchema: Schema.Literals<readonly ["abs", "acos", "asin", "atan", "cbrt", "ceil", "cos", "cosh", "cot", "csc", "exp", "floor", "ln", "log", "sec", "sign", "sin", "sinh", "sqrt", "tan", "tanh"]>;
|
|
10
|
+
export type FormulaUnary = typeof FormulaUnarySchema.Type;
|
|
11
|
+
/**
|
|
12
|
+
* Every function a formula may call: the functions of one argument, and `min`
|
|
13
|
+
* and `max`, which take two or more. A logarithm in another base is written
|
|
14
|
+
* `log_b(x)`.
|
|
15
|
+
*/
|
|
16
|
+
export declare const FormulaFunctionSchema: Schema.Literals<readonly ["abs", "acos", "asin", "atan", "cbrt", "ceil", "cos", "cosh", "cot", "csc", "exp", "floor", "ln", "log", "sec", "sign", "sin", "sinh", "sqrt", "tan", "tanh", "max", "min"]>;
|
|
17
|
+
export type FormulaFunction = typeof FormulaFunctionSchema.Type;
|
|
18
|
+
/** Named constants, each with its exact numeric value. */
|
|
19
|
+
export declare const FORMULA_CONSTANTS: {
|
|
20
|
+
readonly e: number;
|
|
21
|
+
readonly pi: number;
|
|
22
|
+
};
|
|
23
|
+
/** The variables an object's formula is written in, chosen by its kind. */
|
|
24
|
+
export declare const FormulaVariableSchema: Schema.Literals<readonly ["theta", "t", "u", "v", "x", "y", "z"]>;
|
|
25
|
+
export type FormulaVariable = typeof FormulaVariableSchema.Type;
|
|
26
|
+
/**
|
|
27
|
+
* A slider parameter's name: one lowercase Latin letter that is neither a
|
|
28
|
+
* variable nor the constant e, so `a`, `b`, `h`, `k`, and `m` all qualify.
|
|
29
|
+
*/
|
|
30
|
+
export declare const FormulaParameterNameSchema: Schema.String;
|
|
31
|
+
/** The named type of one formula node, which a node's children repeat. */
|
|
32
|
+
export type FormulaNode = {
|
|
33
|
+
readonly kind: "number";
|
|
34
|
+
readonly value: number;
|
|
35
|
+
} | {
|
|
36
|
+
readonly kind: "name";
|
|
37
|
+
readonly name: string;
|
|
38
|
+
} | {
|
|
39
|
+
readonly kind: "negate";
|
|
40
|
+
readonly operand: FormulaNode;
|
|
41
|
+
} | {
|
|
42
|
+
readonly kind: "binary";
|
|
43
|
+
readonly left: FormulaNode;
|
|
44
|
+
readonly operator: FormulaOperator;
|
|
45
|
+
readonly right: FormulaNode;
|
|
46
|
+
} | {
|
|
47
|
+
readonly args: readonly [FormulaNode, ...FormulaNode[]];
|
|
48
|
+
readonly callee: FormulaFunction;
|
|
49
|
+
readonly kind: "call";
|
|
50
|
+
} | {
|
|
51
|
+
readonly base: FormulaNode;
|
|
52
|
+
readonly kind: "logarithm";
|
|
53
|
+
readonly value: FormulaNode;
|
|
54
|
+
};
|
|
55
|
+
/** One parsed formula expression as a tree of numbers, names, and operations. */
|
|
56
|
+
export declare const FormulaNodeSchema: Schema.Codec<FormulaNode>;
|
|
57
|
+
/** One parsed equation or inequality: two expressions and their comparison. */
|
|
58
|
+
export declare const FormulaRelationSchema: Schema.Struct<{
|
|
59
|
+
readonly comparison: Schema.Literals<readonly ["=", "<", "<=", ">", ">="]>;
|
|
60
|
+
readonly left: Schema.Codec<FormulaNode, FormulaNode, never, never>;
|
|
61
|
+
readonly right: Schema.Codec<FormulaNode, FormulaNode, never, never>;
|
|
62
|
+
}>;
|
|
63
|
+
export type FormulaRelation = typeof FormulaRelationSchema.Type;
|
|
64
|
+
/** Why a formula could not be read, so an editor can say it in any language. */
|
|
65
|
+
export declare const FormulaProblemSchema: Schema.Literals<readonly ["comparison-expected", "comparison-unexpected", "empty", "function-arguments", "group-unclosed", "operator-expected", "symbol-unexpected", "value-expected"]>;
|
|
66
|
+
export type FormulaProblem = typeof FormulaProblemSchema.Type;
|
|
67
|
+
declare const FormulaError_base: Schema.Class<FormulaError, Schema.TaggedStruct<"FormulaError", {
|
|
68
|
+
readonly message: Schema.String;
|
|
69
|
+
readonly position: Schema.Int;
|
|
70
|
+
readonly problem: Schema.Literals<readonly ["comparison-expected", "comparison-unexpected", "empty", "function-arguments", "group-unclosed", "operator-expected", "symbol-unexpected", "value-expected"]>;
|
|
71
|
+
}>, import("effect/Cause").YieldableError & {
|
|
72
|
+
readonly brand: unique symbol;
|
|
73
|
+
}>;
|
|
74
|
+
/** A formula that does not parse, with the problem and where it starts. */
|
|
75
|
+
export declare class FormulaError extends FormulaError_base {
|
|
76
|
+
}
|
|
77
|
+
export {};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
/** Arithmetic operators of a formula, from the loosest binding to the tightest. */
|
|
3
|
+
export const FormulaOperatorSchema = Schema.Literals(["+", "-", "*", "/", "^"]);
|
|
4
|
+
/** Comparisons that turn two expressions into an equation or an inequality. */
|
|
5
|
+
export const FormulaComparisonSchema = Schema.Literals([
|
|
6
|
+
"=",
|
|
7
|
+
"<",
|
|
8
|
+
"<=",
|
|
9
|
+
">",
|
|
10
|
+
">=",
|
|
11
|
+
]);
|
|
12
|
+
/** Functions of one argument, in radians where an angle is meant. */
|
|
13
|
+
export const FormulaUnarySchema = Schema.Literals([
|
|
14
|
+
"abs",
|
|
15
|
+
"acos",
|
|
16
|
+
"asin",
|
|
17
|
+
"atan",
|
|
18
|
+
"cbrt",
|
|
19
|
+
"ceil",
|
|
20
|
+
"cos",
|
|
21
|
+
"cosh",
|
|
22
|
+
"cot",
|
|
23
|
+
"csc",
|
|
24
|
+
"exp",
|
|
25
|
+
"floor",
|
|
26
|
+
"ln",
|
|
27
|
+
"log",
|
|
28
|
+
"sec",
|
|
29
|
+
"sign",
|
|
30
|
+
"sin",
|
|
31
|
+
"sinh",
|
|
32
|
+
"sqrt",
|
|
33
|
+
"tan",
|
|
34
|
+
"tanh",
|
|
35
|
+
]);
|
|
36
|
+
/**
|
|
37
|
+
* Every function a formula may call: the functions of one argument, and `min`
|
|
38
|
+
* and `max`, which take two or more. A logarithm in another base is written
|
|
39
|
+
* `log_b(x)`.
|
|
40
|
+
*/
|
|
41
|
+
export const FormulaFunctionSchema = Schema.Literals([
|
|
42
|
+
...FormulaUnarySchema.literals,
|
|
43
|
+
"max",
|
|
44
|
+
"min",
|
|
45
|
+
]);
|
|
46
|
+
/** Named constants, each with its exact numeric value. */
|
|
47
|
+
export const FORMULA_CONSTANTS = { e: Math.E, pi: Math.PI };
|
|
48
|
+
/** The variables an object's formula is written in, chosen by its kind. */
|
|
49
|
+
export const FormulaVariableSchema = Schema.Literals([
|
|
50
|
+
"theta",
|
|
51
|
+
"t",
|
|
52
|
+
"u",
|
|
53
|
+
"v",
|
|
54
|
+
"x",
|
|
55
|
+
"y",
|
|
56
|
+
"z",
|
|
57
|
+
]);
|
|
58
|
+
/**
|
|
59
|
+
* A slider parameter's name: one lowercase Latin letter that is neither a
|
|
60
|
+
* variable nor the constant e, so `a`, `b`, `h`, `k`, and `m` all qualify.
|
|
61
|
+
*/
|
|
62
|
+
export const FormulaParameterNameSchema = Schema.String.check(Schema.isPattern(/^[a-df-sw]$/u));
|
|
63
|
+
/** One parsed formula expression as a tree of numbers, names, and operations. */
|
|
64
|
+
export const FormulaNodeSchema = Schema.Union([
|
|
65
|
+
Schema.Struct({ kind: Schema.Literal("number"), value: Schema.Finite }),
|
|
66
|
+
Schema.Struct({ kind: Schema.Literal("name"), name: Schema.String }),
|
|
67
|
+
Schema.Struct({
|
|
68
|
+
kind: Schema.Literal("negate"),
|
|
69
|
+
operand: Schema.suspend(() => FormulaNodeSchema),
|
|
70
|
+
}),
|
|
71
|
+
Schema.Struct({
|
|
72
|
+
kind: Schema.Literal("binary"),
|
|
73
|
+
left: Schema.suspend(() => FormulaNodeSchema),
|
|
74
|
+
operator: FormulaOperatorSchema,
|
|
75
|
+
right: Schema.suspend(() => FormulaNodeSchema),
|
|
76
|
+
}),
|
|
77
|
+
Schema.Struct({
|
|
78
|
+
args: Schema.NonEmptyArray(Schema.suspend(() => FormulaNodeSchema)),
|
|
79
|
+
callee: FormulaFunctionSchema,
|
|
80
|
+
kind: Schema.Literal("call"),
|
|
81
|
+
}),
|
|
82
|
+
Schema.Struct({
|
|
83
|
+
base: Schema.suspend(() => FormulaNodeSchema),
|
|
84
|
+
kind: Schema.Literal("logarithm"),
|
|
85
|
+
value: Schema.suspend(() => FormulaNodeSchema),
|
|
86
|
+
}),
|
|
87
|
+
]);
|
|
88
|
+
/** One parsed equation or inequality: two expressions and their comparison. */
|
|
89
|
+
export const FormulaRelationSchema = Schema.Struct({
|
|
90
|
+
comparison: FormulaComparisonSchema,
|
|
91
|
+
left: FormulaNodeSchema,
|
|
92
|
+
right: FormulaNodeSchema,
|
|
93
|
+
});
|
|
94
|
+
/** Why a formula could not be read, so an editor can say it in any language. */
|
|
95
|
+
export const FormulaProblemSchema = Schema.Literals([
|
|
96
|
+
"comparison-expected",
|
|
97
|
+
"comparison-unexpected",
|
|
98
|
+
"empty",
|
|
99
|
+
"function-arguments",
|
|
100
|
+
"group-unclosed",
|
|
101
|
+
"operator-expected",
|
|
102
|
+
"symbol-unexpected",
|
|
103
|
+
"value-expected",
|
|
104
|
+
]);
|
|
105
|
+
/** A formula that does not parse, with the problem and where it starts. */
|
|
106
|
+
export class FormulaError extends Schema.TaggedError()("FormulaError", {
|
|
107
|
+
message: Schema.String,
|
|
108
|
+
position: Schema.Int,
|
|
109
|
+
problem: FormulaProblemSchema,
|
|
110
|
+
}) {
|
|
111
|
+
}
|