@nakafa/aksara-contracts 8.0.3 → 8.0.4
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/math/bounds.js
CHANGED
|
@@ -2,6 +2,7 @@ import { Array as Arr, Option, Result } from "effect";
|
|
|
2
2
|
import { arcContained, axisContains, circleContained, cuboidContained, quadraticContained, } from "#contracts/math/extent";
|
|
3
3
|
import { isPlaneFormulaPoint } from "#contracts/math/formula/field";
|
|
4
4
|
import { planeFormulaGeometry, planeFormulaPoint, } from "#contracts/math/formula/plane";
|
|
5
|
+
import { regionFilled } from "#contracts/math/formula/region";
|
|
5
6
|
import { geometryVisible } from "#contracts/math/formula/scope";
|
|
6
7
|
import { spaceFormulaGeometry } from "#contracts/math/formula/space";
|
|
7
8
|
import { infinitePathIntersectsBox } from "#contracts/math/intersection";
|
|
@@ -54,10 +55,13 @@ function planeObjectContained(frame, object, scope, labels) {
|
|
|
54
55
|
object.kind === "marker" ||
|
|
55
56
|
object.kind === "parametric" ||
|
|
56
57
|
object.kind === "path" ||
|
|
57
|
-
object.kind === "polar"
|
|
58
|
-
object.kind === "region") {
|
|
58
|
+
object.kind === "polar") {
|
|
59
59
|
return sampledVisible(planeFormulaGeometry(object, frame, scope), frame);
|
|
60
60
|
}
|
|
61
|
+
if (object.kind === "region") {
|
|
62
|
+
const filled = regionFilled(object.inequalities, frame, scope);
|
|
63
|
+
return Result.isFailure(filled) || filled.success;
|
|
64
|
+
}
|
|
61
65
|
if (object.kind === "line") {
|
|
62
66
|
const [start, through] = object.through;
|
|
63
67
|
return infinitePathIntersectsBox(object.kind, planeAxes(frame, start, through));
|
|
@@ -9,6 +9,14 @@ export declare const CONTOUR_CELLS = 240;
|
|
|
9
9
|
* squares method, so an equation such as `x^2 + y^2 = 4` draws its circle.
|
|
10
10
|
*/
|
|
11
11
|
export declare function contourLines(field: SampleField, box: SampleBox, cells?: number): readonly SampleBranch[];
|
|
12
|
+
/**
|
|
13
|
+
* Whether `contourFill` would draw anything, decided without drawing it. A cell
|
|
14
|
+
* fills when its four corners are finite and one of them is inside, because an
|
|
15
|
+
* inside corner and its two crossings already make an outline. The samples are
|
|
16
|
+
* read coarse to fine and kept, so a visible region costs a few hundred field
|
|
17
|
+
* reads instead of every cell of the grid.
|
|
18
|
+
*/
|
|
19
|
+
export declare function contourFilled(field: SampleField, box: SampleBox, cells?: number): boolean;
|
|
12
20
|
/**
|
|
13
21
|
* Shades the part of the frame where a field is at least zero, one shape per
|
|
14
22
|
* cell: the corners inside and the crossings between them, so a cell wholly
|
|
@@ -141,6 +141,49 @@ export function contourLines(field, box, cells = CONTOUR_CELLS) {
|
|
|
141
141
|
const found = Arr.flatMap(squares, (cell) => cellSegments(field, cell));
|
|
142
142
|
return join(Arr.map(found, ([from, to], index) => ({ from, index, to })));
|
|
143
143
|
}
|
|
144
|
+
/** Grid strides from coarse to fine, so a filled region is usually found in a few samples. */
|
|
145
|
+
const FILL_STRIDES = [32, 16, 8, 4, 2, 1];
|
|
146
|
+
/**
|
|
147
|
+
* Whether `contourFill` would draw anything, decided without drawing it. A cell
|
|
148
|
+
* fills when its four corners are finite and one of them is inside, because an
|
|
149
|
+
* inside corner and its two crossings already make an outline. The samples are
|
|
150
|
+
* read coarse to fine and kept, so a visible region costs a few hundred field
|
|
151
|
+
* reads instead of every cell of the grid.
|
|
152
|
+
*/
|
|
153
|
+
export function contourFilled(field, box, cells = CONTOUR_CELLS) {
|
|
154
|
+
const dx = (box.x.max - box.x.min) / cells;
|
|
155
|
+
const dy = (box.y.max - box.y.min) / cells;
|
|
156
|
+
const read = MutableHashMap.empty();
|
|
157
|
+
/** The field at one grid corner, sampled once and then read from memory. */
|
|
158
|
+
const value = (column, row) => {
|
|
159
|
+
const key = column * (cells + 1) + row;
|
|
160
|
+
return Option.getOrElse(MutableHashMap.get(read, key), () => {
|
|
161
|
+
const sample = field(box.x.min + column * dx, box.y.min + row * dy);
|
|
162
|
+
MutableHashMap.set(read, key, sample);
|
|
163
|
+
return sample;
|
|
164
|
+
});
|
|
165
|
+
};
|
|
166
|
+
/** Whether the cell lies on the grid and all four of its corners are finite. */
|
|
167
|
+
const finiteCell = (column, row) => column >= 0 &&
|
|
168
|
+
row >= 0 &&
|
|
169
|
+
column < cells &&
|
|
170
|
+
row < cells &&
|
|
171
|
+
Arr.every([
|
|
172
|
+
[column, row],
|
|
173
|
+
[column + 1, row],
|
|
174
|
+
[column + 1, row + 1],
|
|
175
|
+
[column, row + 1],
|
|
176
|
+
], ([c, r]) => Number.isFinite(value(c, r)));
|
|
177
|
+
/** Whether this corner is inside and one finite cell touching it would fill. */
|
|
178
|
+
const fills = (column, row) => value(column, row) >= 0 &&
|
|
179
|
+
Arr.some([
|
|
180
|
+
[column, row],
|
|
181
|
+
[column - 1, row],
|
|
182
|
+
[column - 1, row - 1],
|
|
183
|
+
[column, row - 1],
|
|
184
|
+
], ([c, r]) => finiteCell(c, r));
|
|
185
|
+
return Arr.some(FILL_STRIDES, (stride) => Arr.some(Arr.range(0, Math.floor(cells / stride)), (i) => Arr.some(Arr.range(0, Math.floor(cells / stride)), (j) => fills(i * stride, j * stride))));
|
|
186
|
+
}
|
|
144
187
|
/**
|
|
145
188
|
* Shades the part of the frame where a field is at least zero, one shape per
|
|
146
189
|
* cell: the corners inside and the crossings between them, so a cell wholly
|
|
@@ -14,3 +14,9 @@ export declare function regionGeometry(inequalities: readonly [string, ...string
|
|
|
14
14
|
fill: readonly import("./sample.js").SampleBranch[];
|
|
15
15
|
kind: "region";
|
|
16
16
|
}, import("./syntax.js").FormulaError>;
|
|
17
|
+
/**
|
|
18
|
+
* Whether the region where every inequality holds shows inside the box. It
|
|
19
|
+
* answers what `regionGeometry` would fill without tracing the outline, so a
|
|
20
|
+
* schema check stays cheap; drawing the region is the renderer's work.
|
|
21
|
+
*/
|
|
22
|
+
export declare function regionFilled(inequalities: readonly [string, ...string[]], box: SampleBox, scope: FormulaScope): Result.Result<boolean, import("./syntax.js").FormulaError>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Array as Arr, MutableList, Option, Order, Result, Schema, } from "effect";
|
|
2
2
|
import { SamplePointSchema, } from "#contracts/math/formula/box";
|
|
3
3
|
import { compileFormula, } from "#contracts/math/formula/compile";
|
|
4
|
-
import { contourFill, contourLines, } from "#contracts/math/formula/contour";
|
|
4
|
+
import { contourFill, contourFilled, contourLines, } from "#contracts/math/formula/contour";
|
|
5
5
|
import { readRelation } from "#contracts/math/formula/parse";
|
|
6
6
|
/** How far a boundary piece lies from one inequality's edge, and whether that edge is strict. */
|
|
7
7
|
const ConstraintGapSchema = Schema.Struct({
|
|
@@ -92,3 +92,14 @@ export function regionGeometry(inequalities, box, scope) {
|
|
|
92
92
|
};
|
|
93
93
|
});
|
|
94
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Whether the region where every inequality holds shows inside the box. It
|
|
97
|
+
* answers what `regionGeometry` would fill without tracing the outline, so a
|
|
98
|
+
* schema check stays cheap; drawing the region is the renderer's work.
|
|
99
|
+
*/
|
|
100
|
+
export function regionFilled(inequalities, box, scope) {
|
|
101
|
+
return Result.map(Result.all(Arr.map(inequalities, margin)), (margins) => {
|
|
102
|
+
const fields = Arr.map(margins, (each) => each.field(scope));
|
|
103
|
+
return contourFilled((x, y) => Math.min(...Arr.map(fields, (field) => field(x, y))), box);
|
|
104
|
+
});
|
|
105
|
+
}
|
package/package.json
CHANGED