@nakafa/aksara-contracts 0.32.0 → 0.34.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.
@@ -0,0 +1,7 @@
1
+ import { BigDecimal } from "effect";
2
+ /** Checks the rendered chord separating the two finite ends of one arc. */
3
+ export declare function arcEndpointsUnresolved(radius: number, sweepDegrees: number, threshold: BigDecimal.BigDecimal): boolean;
4
+ /** Checks whether one arc's non-zero curvature collapses into its chord. */
5
+ export declare function arcCurvatureUnresolved(radius: number, sweepDegrees: number, threshold: BigDecimal.BigDecimal): boolean;
6
+ /** Checks both authored endpoints against their nearest cardinal axes. */
7
+ export declare function arcEndpointAxesUnresolved(radius: number, startDegrees: number, sweepDegrees: number, threshold: BigDecimal.BigDecimal): boolean;
@@ -0,0 +1,82 @@
1
+ import { BigDecimal } from "effect";
2
+ import { GEOMETRY_TOLERANCE } from "#contracts/math/base";
3
+ import { decimal } from "#contracts/math/rational";
4
+ const PI = BigDecimal.fromStringUnsafe("3.1415926535897932384626433832795028841971693993751");
5
+ const TWO = BigDecimal.fromBigInt(2n);
6
+ const NINETY = BigDecimal.fromBigInt(90n);
7
+ const ONE_EIGHTY = BigDecimal.fromBigInt(180n);
8
+ const THREE_SIXTY = BigDecimal.fromBigInt(360n);
9
+ const SEVEN_TWENTY = BigDecimal.fromBigInt(720n);
10
+ const TRIGONOMETRIC_ERROR = decimal(GEOMETRY_TOLERANCE);
11
+ const MINIMUM_ERROR = decimal(Number.MIN_VALUE);
12
+ /** Bounds a sine estimate for an angle that may fall below Number.MIN_VALUE. */
13
+ function stableSine(exactRadians, numericRadians) {
14
+ const value = BigDecimal.isZero(exactRadians) || numericRadians === 0
15
+ ? exactRadians
16
+ : decimal(Math.sin(numericRadians));
17
+ const scale = BigDecimal.max(BigDecimal.abs(exactRadians), BigDecimal.abs(value));
18
+ return {
19
+ error: BigDecimal.max(MINIMUM_ERROR, BigDecimal.multiply(scale, TRIGONOMETRIC_ERROR)),
20
+ value,
21
+ };
22
+ }
23
+ /** Multiplies one bounded decimal by an exact scale. */
24
+ function scaleBounded(bounded, scale) {
25
+ const magnitude = BigDecimal.abs(scale);
26
+ return {
27
+ error: BigDecimal.multiply(bounded.error, magnitude),
28
+ value: BigDecimal.multiply(bounded.value, scale),
29
+ };
30
+ }
31
+ /** Squares one bounded decimal with explicit propagated error. */
32
+ function squareBounded(bounded) {
33
+ return {
34
+ error: BigDecimal.sum(BigDecimal.multiply(BigDecimal.multiply(TWO, BigDecimal.abs(bounded.value)), bounded.error), BigDecimal.multiply(bounded.error, bounded.error)),
35
+ value: BigDecimal.multiply(bounded.value, bounded.value),
36
+ };
37
+ }
38
+ /** Proves that one non-zero bounded measure lies below the threshold. */
39
+ function boundedMeasureUnresolved(bounded, threshold) {
40
+ const magnitude = BigDecimal.abs(bounded.value);
41
+ return (!BigDecimal.isZero(magnitude) &&
42
+ BigDecimal.isLessThan(BigDecimal.sum(magnitude, bounded.error), threshold));
43
+ }
44
+ /** Measures one endpoint's shortest authored offset from a cardinal axis. */
45
+ function endpointAxisMeasure(radius, exactDegrees, numericDegrees) {
46
+ const nearestCardinal = Math.round(numericDegrees / 90);
47
+ const exactDelta = BigDecimal.subtract(exactDegrees, BigDecimal.multiply(decimal(nearestCardinal), NINETY));
48
+ const exactRadians = BigDecimal.divideUnsafe(BigDecimal.multiply(exactDelta, PI), ONE_EIGHTY);
49
+ const numericDelta = numericDegrees - nearestCardinal * 90;
50
+ return scaleBounded(stableSine(exactRadians, numericDelta * (Math.PI / 180)), decimal(Math.abs(radius)));
51
+ }
52
+ /** Resolves the endpoint chord and curvature sagitta of one finite arc. */
53
+ function arcMeasures(radius, sweepDegrees) {
54
+ const directedSweep = Math.abs(sweepDegrees);
55
+ const minorSweep = Math.min(directedSweep, 360 - directedSweep);
56
+ const halfRadians = BigDecimal.divideUnsafe(BigDecimal.multiply(decimal(minorSweep), PI), THREE_SIXTY);
57
+ const halfSine = stableSine(halfRadians, minorSweep * (Math.PI / 360));
58
+ const directedQuarterRadians = BigDecimal.divideUnsafe(BigDecimal.multiply(decimal(directedSweep), PI), SEVEN_TWENTY);
59
+ const quarterSine = stableSine(directedQuarterRadians, directedSweep * (Math.PI / 720));
60
+ const doubledRadius = BigDecimal.multiply(decimal(radius), TWO);
61
+ return {
62
+ chord: scaleBounded(halfSine, doubledRadius),
63
+ sagitta: scaleBounded(squareBounded(quarterSine), doubledRadius),
64
+ };
65
+ }
66
+ /** Checks the rendered chord separating the two finite ends of one arc. */
67
+ export function arcEndpointsUnresolved(radius, sweepDegrees, threshold) {
68
+ return boundedMeasureUnresolved(arcMeasures(Math.abs(radius), sweepDegrees).chord, threshold);
69
+ }
70
+ /** Checks whether one arc's non-zero curvature collapses into its chord. */
71
+ export function arcCurvatureUnresolved(radius, sweepDegrees, threshold) {
72
+ return boundedMeasureUnresolved(arcMeasures(Math.abs(radius), sweepDegrees).sagitta, threshold);
73
+ }
74
+ /** Checks both authored endpoints against their nearest cardinal axes. */
75
+ export function arcEndpointAxesUnresolved(radius, startDegrees, sweepDegrees, threshold) {
76
+ const exactStart = decimal(startDegrees);
77
+ const exactEnd = BigDecimal.sum(exactStart, decimal(sweepDegrees));
78
+ return [
79
+ endpointAxisMeasure(radius, exactStart, startDegrees),
80
+ endpointAxisMeasure(radius, exactEnd, startDegrees + sweepDegrees),
81
+ ].some((measure) => boundedMeasureUnresolved(measure, threshold));
82
+ }
@@ -0,0 +1,7 @@
1
+ import type { Schema } from "effect";
2
+ import type { PlaneLabelAnchor, PlaneMathFrame, PlaneMathObject } from "#contracts/math/plane";
3
+ import type { SpaceLabelAnchor, SpaceMathFrame, SpaceMathObject, SpaceMathView } from "#contracts/math/space";
4
+ /** 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
+ /** 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[];
@@ -0,0 +1,131 @@
1
+ import { arcContained, axisContains, circleContained, cuboidContained, quadraticContained, } from "#contracts/math/extent";
2
+ import { infinitePathIntersectsBox } from "#contracts/math/intersection";
3
+ /** Checks one plane point against both inclusive frame ranges. */
4
+ function planeContains(frame, point) {
5
+ return [
6
+ { coordinate: point.x, range: frame.x },
7
+ { coordinate: point.y, range: frame.y },
8
+ ].every(({ coordinate, range }) => axisContains(range, coordinate));
9
+ }
10
+ /** Checks one space point against all three inclusive frame ranges. */
11
+ function spaceContains(frame, point) {
12
+ return [
13
+ { coordinate: point.x, range: frame.x },
14
+ { coordinate: point.y, range: frame.y },
15
+ { coordinate: point.z, range: frame.z },
16
+ ].every(({ coordinate, range }) => axisContains(range, coordinate));
17
+ }
18
+ /** Maps one plane path onto the Cartesian frame slabs it traverses. */
19
+ function planeAxes(frame, start, through) {
20
+ return [
21
+ { range: frame.x, start: start.x, through: through.x },
22
+ { range: frame.y, start: start.y, through: through.y },
23
+ ];
24
+ }
25
+ /** Maps one space path onto the Cartesian frame slabs it traverses. */
26
+ function spaceAxes(frame, start, through) {
27
+ return [
28
+ { range: frame.x, start: start.x, through: through.x },
29
+ { range: frame.y, start: start.y, through: through.y },
30
+ { range: frame.z, start: start.z, through: through.z },
31
+ ];
32
+ }
33
+ /** Checks whether one plane object has complete visible geometry in its frame. */
34
+ function planeObjectContained(frame, object) {
35
+ if (object.kind === "line") {
36
+ const [start, through] = object.through;
37
+ return infinitePathIntersectsBox(object.kind, planeAxes(frame, start, through));
38
+ }
39
+ if (object.kind === "ray") {
40
+ return infinitePathIntersectsBox(object.kind, planeAxes(frame, object.from, object.through));
41
+ }
42
+ if (object.kind === "point") {
43
+ return planeContains(frame, object.at);
44
+ }
45
+ if (object.kind === "segment") {
46
+ return [object.from, object.to].every((point) => planeContains(frame, point));
47
+ }
48
+ if (object.kind === "polyline" || object.kind === "polygon") {
49
+ return object.vertices.every((point) => planeContains(frame, point));
50
+ }
51
+ if (object.kind === "circle") {
52
+ return circleContained(frame, object);
53
+ }
54
+ if (object.kind === "arc") {
55
+ return arcContained(frame, object);
56
+ }
57
+ return quadraticContained(frame, object);
58
+ }
59
+ /** Checks whether one space object has complete visible geometry in its frame. */
60
+ function spaceObjectContained(frame, object) {
61
+ if (object.kind === "line") {
62
+ const [start, through] = object.through;
63
+ return infinitePathIntersectsBox(object.kind, spaceAxes(frame, start, through));
64
+ }
65
+ if (object.kind === "ray") {
66
+ return infinitePathIntersectsBox(object.kind, spaceAxes(frame, object.from, object.through));
67
+ }
68
+ if (object.kind === "point") {
69
+ return spaceContains(frame, object.at);
70
+ }
71
+ if (object.kind === "segment") {
72
+ return [object.from, object.to].every((point) => spaceContains(frame, point));
73
+ }
74
+ if (object.kind === "polyline" || object.kind === "polygon") {
75
+ return object.vertices.every((point) => spaceContains(frame, point));
76
+ }
77
+ return cuboidContained(frame, object);
78
+ }
79
+ /** Reports every finite plane object or label that escapes its authored frame. */
80
+ export function planeBoundsIssues(frame, objects, labels) {
81
+ return [
82
+ ...objects.flatMap((object, index) => planeObjectContained(frame, object)
83
+ ? []
84
+ : [
85
+ {
86
+ issue: "Expected plane geometry visible inside its frame.",
87
+ path: ["objects", index],
88
+ },
89
+ ]),
90
+ ...labels.flatMap((label, index) => planeContains(frame, label.at)
91
+ ? []
92
+ : [
93
+ {
94
+ issue: "Expected a plane label anchor inside the Cartesian frame.",
95
+ path: ["labels", index, "at"],
96
+ },
97
+ ]),
98
+ ];
99
+ }
100
+ /** Reports every finite space object, label, or view target outside its frame. */
101
+ export function spaceBoundsIssues(frame, objects, labels, view) {
102
+ const viewIssues = view.kind === "isometric" &&
103
+ view.target !== undefined &&
104
+ !spaceContains(frame, view.target)
105
+ ? [
106
+ {
107
+ issue: "Expected an isometric target inside the Cartesian frame.",
108
+ path: ["view", "target"],
109
+ },
110
+ ]
111
+ : [];
112
+ return [
113
+ ...objects.flatMap((object, index) => spaceObjectContained(frame, object)
114
+ ? []
115
+ : [
116
+ {
117
+ issue: "Expected space geometry visible inside its frame.",
118
+ path: ["objects", index],
119
+ },
120
+ ]),
121
+ ...labels.flatMap((label, index) => spaceContains(frame, label.at)
122
+ ? []
123
+ : [
124
+ {
125
+ issue: "Expected a space label anchor inside the Cartesian frame.",
126
+ path: ["labels", index, "at"],
127
+ },
128
+ ]),
129
+ ...viewIssues,
130
+ ];
131
+ }
@@ -0,0 +1,7 @@
1
+ import type { BigDecimal } from "effect";
2
+ import type { SceneCoordinate, ScenePath } from "#contracts/math/coordinate";
3
+ import type { PlaneMathObject } from "#contracts/math/plane";
4
+ /** Finds reportable scene coordinates that collapse on the same axis. */
5
+ export declare function coordinateCollisionPaths(coordinates: readonly SceneCoordinate[], threshold: BigDecimal.BigDecimal): ScenePath[];
6
+ /** Finds non-zero radius deltas between concentric circles or arcs. */
7
+ export declare function concentricRadiusCollisionPaths(objects: readonly PlaneMathObject[], threshold: BigDecimal.BigDecimal): ScenePath[];
@@ -0,0 +1,41 @@
1
+ import { unresolvedProximityIndexes } from "#contracts/math/proximity";
2
+ import { numberRatio } from "#contracts/math/rational";
3
+ import { radialGeometryPath } from "#contracts/math/scene";
4
+ /** Keeps one authored issue path for each exact path value. */
5
+ function uniquePaths(paths) {
6
+ return [
7
+ ...new Map(paths.map((path) => [JSON.stringify(path), path])).values(),
8
+ ];
9
+ }
10
+ /** Finds reportable scene coordinates that collapse on the same axis. */
11
+ export function coordinateCollisionPaths(coordinates, threshold) {
12
+ const axes = ["x", "y", "z"];
13
+ return uniquePaths(axes.flatMap((axis) => {
14
+ const entries = coordinates.filter((entry) => entry.axis === axis);
15
+ const unresolved = unresolvedProximityIndexes(entries, threshold);
16
+ return entries.flatMap((entry, index) => entry.reportable && unresolved.has(index) ? [entry.path] : []);
17
+ }));
18
+ }
19
+ /** Finds non-zero radius deltas between concentric circles or arcs. */
20
+ export function concentricRadiusCollisionPaths(objects, threshold) {
21
+ const groups = new Map();
22
+ for (const [index, object] of objects.entries()) {
23
+ if (object.kind !== "arc" && object.kind !== "circle") {
24
+ continue;
25
+ }
26
+ const key = `${object.center.x}:${object.center.y}`;
27
+ const group = groups.get(key) ?? [];
28
+ group.push({ index, kind: object.kind, value: numberRatio(object.radius) });
29
+ groups.set(key, group);
30
+ }
31
+ const paths = [];
32
+ for (const group of groups.values()) {
33
+ const unresolved = unresolvedProximityIndexes(group, threshold);
34
+ for (const [entryIndex, entry] of group.entries()) {
35
+ if (unresolved.has(entryIndex)) {
36
+ paths.push(radialGeometryPath(entry.kind, entry.index));
37
+ }
38
+ }
39
+ }
40
+ return paths;
41
+ }
@@ -0,0 +1,32 @@
1
+ import type { BigDecimal } from "effect";
2
+ import type { PlanePoint, SpacePoint } from "#contracts/math/base";
3
+ import { type AxisTraversal } from "#contracts/math/intersection";
4
+ import type { PlaneMathFrame } from "#contracts/math/plane";
5
+ import { type RenderThreshold } from "#contracts/math/precision";
6
+ import { type ExactRatio } from "#contracts/math/rational";
7
+ import type { SpaceMathFrame } from "#contracts/math/space";
8
+ export type SceneAxis = "x" | "y" | "z";
9
+ export type ScenePath = Array<number | string>;
10
+ /** One ordered exact coordinate projected from a complete visual scene. */
11
+ export interface SceneCoordinate {
12
+ readonly axis: SceneAxis;
13
+ readonly error?: BigDecimal.BigDecimal;
14
+ readonly path: ScenePath;
15
+ readonly reportable: boolean;
16
+ readonly value: ExactRatio;
17
+ }
18
+ export type SceneFrame = PlaneMathFrame | SpaceMathFrame;
19
+ interface SceneAxisTraversal extends AxisTraversal {
20
+ readonly axis: SceneAxis;
21
+ }
22
+ /** Creates one reportable exact scene coordinate. */
23
+ export declare function sceneCoordinate(axis: SceneAxis, path: ScenePath, value: ExactRatio, reportable?: boolean, error?: BigDecimal.BigDecimal): SceneCoordinate;
24
+ /** Projects every axis of one finite point in authored order. */
25
+ export declare function pointCoordinates(point: PlanePoint | SpacePoint, path: ScenePath): SceneCoordinate[];
26
+ /** Seeds every frame edge before reportable scene coordinates. */
27
+ export declare function frameCoordinates(frame: SceneFrame): SceneCoordinate[];
28
+ /** Maps one directed path onto every axis of its authored frame. */
29
+ export declare function pathAxes(frame: SceneFrame, from: PlanePoint | SpacePoint, through: PlanePoint | SpacePoint): SceneAxisTraversal[];
30
+ /** Projects the canonical exact endpoints of one frame-clipped line or ray. */
31
+ export declare function clippedPathCoordinates(frame: SceneFrame, kind: "line" | "ray", from: PlanePoint | SpacePoint, through: PlanePoint | SpacePoint, path: ScenePath, threshold: RenderThreshold): SceneCoordinate[];
32
+ export {};
@@ -0,0 +1,61 @@
1
+ import { infinitePathInterval, } from "#contracts/math/intersection";
2
+ import { visiblePathResolvable, } from "#contracts/math/precision";
3
+ import { affineRatio, numberRatio, } from "#contracts/math/rational";
4
+ /** Creates one reportable exact scene coordinate. */
5
+ export function sceneCoordinate(axis, path, value, reportable = true, error) {
6
+ return error === undefined
7
+ ? { axis, path, reportable, value }
8
+ : { axis, error, path, reportable, value };
9
+ }
10
+ /** Projects every axis of one finite point in authored order. */
11
+ export function pointCoordinates(point, path) {
12
+ return [
13
+ sceneCoordinate("x", [...path, "x"], numberRatio(point.x)),
14
+ sceneCoordinate("y", [...path, "y"], numberRatio(point.y)),
15
+ ...("z" in point
16
+ ? [sceneCoordinate("z", [...path, "z"], numberRatio(point.z))]
17
+ : []),
18
+ ];
19
+ }
20
+ /** Seeds every frame edge before reportable scene coordinates. */
21
+ export function frameCoordinates(frame) {
22
+ const axes = [
23
+ ["x", frame.x],
24
+ ["y", frame.y],
25
+ ...("z" in frame ? [["z", frame.z]] : []),
26
+ ];
27
+ return axes.flatMap(([axis, range]) => [
28
+ sceneCoordinate(axis, ["frame", axis], numberRatio(range.min), false),
29
+ sceneCoordinate(axis, ["frame", axis], numberRatio(range.max), false),
30
+ ]);
31
+ }
32
+ /** Maps one directed path onto every axis of its authored frame. */
33
+ export function pathAxes(frame, from, through) {
34
+ const axes = [
35
+ { axis: "x", range: frame.x, start: from.x, through: through.x },
36
+ { axis: "y", range: frame.y, start: from.y, through: through.y },
37
+ ];
38
+ return "z" in frame && "z" in from && "z" in through
39
+ ? [
40
+ ...axes,
41
+ {
42
+ axis: "z",
43
+ range: frame.z,
44
+ start: from.z,
45
+ through: through.z,
46
+ },
47
+ ]
48
+ : axes;
49
+ }
50
+ /** Projects the canonical exact endpoints of one frame-clipped line or ray. */
51
+ export function clippedPathCoordinates(frame, kind, from, through, path, threshold) {
52
+ const axes = pathAxes(frame, from, through);
53
+ const interval = infinitePathInterval(kind, axes);
54
+ if (!(interval?.entry && interval.exit)) {
55
+ return [];
56
+ }
57
+ if (!visiblePathResolvable(kind, axes, threshold)) {
58
+ return [];
59
+ }
60
+ return [interval.entry, interval.exit].flatMap((parameter) => axes.map((axis) => sceneCoordinate(axis.axis, path, affineRatio(axis.start, axis.through, parameter))));
61
+ }
@@ -0,0 +1,51 @@
1
+ import { BigDecimal } from "effect";
2
+ import type { PlaneMathFrame, PlaneMathObject } from "#contracts/math/plane";
3
+ import { type ExactRatio } from "#contracts/math/rational";
4
+ import type { SpaceMathFrame, SpaceMathObject } from "#contracts/math/space";
5
+ export type AxisRange = PlaneMathFrame["x"];
6
+ /** Checks one finite coordinate against its inclusive authored range. */
7
+ export declare function axisContains(range: AxisRange, coordinate: number): boolean;
8
+ export interface RadialOffset {
9
+ readonly error: BigDecimal.BigDecimal;
10
+ readonly x: BigDecimal.BigDecimal;
11
+ readonly y: BigDecimal.BigDecimal;
12
+ }
13
+ /** Returns every endpoint or cardinal offset that bounds one radial object. */
14
+ export declare function radialOffsets(object: Extract<PlaneMathObject, {
15
+ readonly kind: "arc" | "circle";
16
+ }>): readonly RadialOffset[];
17
+ /** Checks the exact endpoints and cardinal extrema of one directed arc. */
18
+ export declare function arcContained(frame: PlaneMathFrame, object: Extract<PlaneMathObject, {
19
+ readonly kind: "arc";
20
+ }>): boolean;
21
+ /** Checks one circle through exact radius-to-boundary comparisons. */
22
+ export declare function circleContained(frame: PlaneMathFrame, object: Extract<PlaneMathObject, {
23
+ readonly kind: "circle";
24
+ }>): boolean;
25
+ /** One exact input and output pair at a quadratic domain extremum. */
26
+ export interface QuadraticExtremum {
27
+ readonly input: ExactRatio;
28
+ readonly output: ExactRatio;
29
+ }
30
+ /** Returns both domain endpoints and the in-domain vertex of one quadratic. */
31
+ export declare function quadraticExtrema(object: Extract<PlaneMathObject, {
32
+ readonly kind: "quadratic";
33
+ }>): readonly QuadraticExtremum[];
34
+ /** Checks the complete quadratic domain and exact endpoint or vertex range. */
35
+ export declare function quadraticContained(frame: PlaneMathFrame, object: Extract<PlaneMathObject, {
36
+ readonly kind: "quadratic";
37
+ }>): boolean;
38
+ export interface CuboidExtent {
39
+ readonly axis: "x" | "y" | "z";
40
+ readonly center: number;
41
+ readonly dimension: "height" | "length" | "width";
42
+ readonly extent: BigDecimal.BigDecimal;
43
+ }
44
+ /** Returns the exact positive half-extent on every cuboid axis. */
45
+ export declare function cuboidExtents(object: Extract<SpaceMathObject, {
46
+ readonly kind: "cuboid";
47
+ }>): readonly CuboidExtent[];
48
+ /** Checks one cuboid through exact half-extent-to-boundary comparisons. */
49
+ export declare function cuboidContained(frame: SpaceMathFrame, object: Extract<SpaceMathObject, {
50
+ readonly kind: "cuboid";
51
+ }>): boolean;
@@ -0,0 +1,143 @@
1
+ import { BigDecimal } from "effect";
2
+ import { GEOMETRY_TOLERANCE } from "#contracts/math/base";
3
+ import { decimal, decimalRatio, makeRatio, numberRatio, ratioInRange, } from "#contracts/math/rational";
4
+ const ZERO = BigDecimal.fromBigInt(0n);
5
+ /** Checks one finite coordinate against its inclusive authored range. */
6
+ export function axisContains(range, coordinate) {
7
+ return coordinate >= range.min && coordinate <= range.max;
8
+ }
9
+ /** Checks one translated coordinate within its explicit numeric error envelope. */
10
+ function axisContainsTranslated(range, origin, offset, error) {
11
+ const coordinate = BigDecimal.sum(decimal(origin), offset);
12
+ return (BigDecimal.isGreaterThanOrEqualTo(coordinate, BigDecimal.subtract(decimal(range.min), error)) &&
13
+ BigDecimal.isLessThanOrEqualTo(coordinate, BigDecimal.sum(decimal(range.max), error)));
14
+ }
15
+ /** Normalizes one angle to the canonical half-open degree interval. */
16
+ function normalizeDegrees(value) {
17
+ if (value >= 0 && value < 360) {
18
+ return value;
19
+ }
20
+ const remainder = value % 360;
21
+ return remainder < 0 ? remainder + 360 : remainder;
22
+ }
23
+ /** Returns whether one cardinal angle lies on the authored directed arc. */
24
+ function arcContainsAngle(start, sweep, angle) {
25
+ return sweep > 0
26
+ ? normalizeDegrees(angle - start) <= sweep
27
+ : normalizeDegrees(start - angle) <= -sweep;
28
+ }
29
+ /** Resolves exact cardinals or a trig offset with its renderer error envelope. */
30
+ function arcOffset(radius, angle) {
31
+ const normalized = normalizeDegrees(angle);
32
+ const exactRadius = decimal(radius);
33
+ const exact = BigDecimal.fromBigInt(0n);
34
+ if (normalized === 0) {
35
+ return { error: exact, x: exactRadius, y: exact };
36
+ }
37
+ if (normalized === 90) {
38
+ return { error: exact, x: exact, y: exactRadius };
39
+ }
40
+ if (normalized === 180) {
41
+ return { error: exact, x: BigDecimal.negate(exactRadius), y: exact };
42
+ }
43
+ if (normalized === 270) {
44
+ return { error: exact, x: exact, y: BigDecimal.negate(exactRadius) };
45
+ }
46
+ const radians = (normalized * Math.PI) / 180;
47
+ const error = decimal(Math.max(Number.MIN_VALUE, radius * GEOMETRY_TOLERANCE));
48
+ return {
49
+ error,
50
+ x: decimal(radius * Math.cos(radians)),
51
+ y: decimal(radius * Math.sin(radians)),
52
+ };
53
+ }
54
+ /** Returns every endpoint or cardinal offset that bounds one radial object. */
55
+ export function radialOffsets(object) {
56
+ if (object.kind === "circle") {
57
+ return [0, 90, 180, 270].map((angle) => arcOffset(object.radius, angle));
58
+ }
59
+ const angles = [
60
+ object.startDegrees,
61
+ object.startDegrees + object.sweepDegrees,
62
+ ];
63
+ for (const angle of [0, 90, 180, 270]) {
64
+ if (arcContainsAngle(object.startDegrees, object.sweepDegrees, angle)) {
65
+ angles.push(angle);
66
+ }
67
+ }
68
+ return angles.map((angle) => arcOffset(object.radius, angle));
69
+ }
70
+ /** Checks the exact endpoints and cardinal extrema of one directed arc. */
71
+ export function arcContained(frame, object) {
72
+ return radialOffsets(object).every((offset) => axisContainsTranslated(frame.x, object.center.x, offset.x, offset.error) &&
73
+ axisContainsTranslated(frame.y, object.center.y, offset.y, offset.error));
74
+ }
75
+ /** Checks one circle through exact radius-to-boundary comparisons. */
76
+ export function circleContained(frame, object) {
77
+ return radialOffsets(object).every((offset) => axisContainsTranslated(frame.x, object.center.x, offset.x, offset.error) &&
78
+ axisContainsTranslated(frame.y, object.center.y, offset.y, offset.error));
79
+ }
80
+ /** Evaluates one quadratic exactly at a finite authored input. */
81
+ function quadraticValue(object, input) {
82
+ const value = decimal(input);
83
+ const { a, b, c } = object.coefficients;
84
+ return BigDecimal.sumAll([
85
+ BigDecimal.multiply(BigDecimal.multiply(decimal(a), value), value),
86
+ BigDecimal.multiply(decimal(b), value),
87
+ decimal(c),
88
+ ]);
89
+ }
90
+ /** Returns both domain endpoints and the in-domain vertex of one quadratic. */
91
+ export function quadraticExtrema(object) {
92
+ const endpoints = [object.domain.min, object.domain.max].map((input) => ({
93
+ input: numberRatio(input),
94
+ output: decimalRatio(quadraticValue(object, input)),
95
+ }));
96
+ const a = decimal(object.coefficients.a);
97
+ const b = decimal(object.coefficients.b);
98
+ const fourA = BigDecimal.multiply(decimal(4), a);
99
+ const vertexInput = makeRatio(BigDecimal.negate(b), BigDecimal.multiply(decimal(2), a));
100
+ if (!ratioInRange(vertexInput, object.domain.min, object.domain.max)) {
101
+ return endpoints;
102
+ }
103
+ const vertexOutput = makeRatio(BigDecimal.subtract(BigDecimal.multiply(fourA, decimal(object.coefficients.c)), BigDecimal.multiply(b, b)), fourA);
104
+ return [...endpoints, { input: vertexInput, output: vertexOutput }];
105
+ }
106
+ /** Checks the complete quadratic domain and exact endpoint or vertex range. */
107
+ export function quadraticContained(frame, object) {
108
+ const inputRange = object.inputAxis === "x" ? frame.x : frame.y;
109
+ const outputRange = object.inputAxis === "x" ? frame.y : frame.x;
110
+ if (![object.domain.min, object.domain.max].every((input) => axisContains(inputRange, input))) {
111
+ return false;
112
+ }
113
+ return quadraticExtrema(object).every(({ output }) => ratioInRange(output, outputRange.min, outputRange.max));
114
+ }
115
+ /** Returns the exact positive half-extent on every cuboid axis. */
116
+ export function cuboidExtents(object) {
117
+ const half = decimal(0.5);
118
+ return [
119
+ {
120
+ axis: "x",
121
+ center: object.center.x,
122
+ dimension: "length",
123
+ extent: BigDecimal.multiply(decimal(object.size.length), half),
124
+ },
125
+ {
126
+ axis: "y",
127
+ center: object.center.y,
128
+ dimension: "height",
129
+ extent: BigDecimal.multiply(decimal(object.size.height), half),
130
+ },
131
+ {
132
+ axis: "z",
133
+ center: object.center.z,
134
+ dimension: "width",
135
+ extent: BigDecimal.multiply(decimal(object.size.width), half),
136
+ },
137
+ ];
138
+ }
139
+ /** Checks one cuboid through exact half-extent-to-boundary comparisons. */
140
+ export function cuboidContained(frame, object) {
141
+ return cuboidExtents(object).every(({ axis, center, extent }) => axisContainsTranslated(frame[axis], center, extent, ZERO) &&
142
+ axisContainsTranslated(frame[axis], center, BigDecimal.negate(extent), ZERO));
143
+ }
@@ -0,0 +1,19 @@
1
+ import { type AxisRange } from "#contracts/math/extent";
2
+ import { type ExactRatio } from "#contracts/math/rational";
3
+ /** One Cartesian slab traversed by the same parametric path. */
4
+ export interface AxisTraversal {
5
+ readonly range: AxisRange;
6
+ readonly start: number;
7
+ readonly through: number;
8
+ }
9
+ type InfinitePathKind = "line" | "ray";
10
+ /** Exact finite sides of one clipped infinite path parameter interval. */
11
+ export interface InfinitePathInterval {
12
+ readonly entry: ExactRatio | undefined;
13
+ readonly exit: ExactRatio | undefined;
14
+ }
15
+ /** Clips one infinite line or ray against an inclusive axis-aligned box. */
16
+ export declare function infinitePathInterval(kind: InfinitePathKind, axes: readonly AxisTraversal[]): InfinitePathInterval | undefined;
17
+ /** Checks whether one infinite line or ray intersects an inclusive axis-aligned box. */
18
+ export declare function infinitePathIntersectsBox(kind: InfinitePathKind, axes: readonly AxisTraversal[]): boolean;
19
+ export {};
@@ -0,0 +1,41 @@
1
+ import { axisContains } from "#contracts/math/extent";
2
+ import { compareRatios, differenceRatio, numberRatio, } from "#contracts/math/rational";
3
+ /** Keeps the greater finite side of one lower parameter boundary. */
4
+ function later(current, candidate) {
5
+ return current === undefined || compareRatios(current, candidate) < 0
6
+ ? candidate
7
+ : current;
8
+ }
9
+ /** Keeps the lesser finite side of one upper parameter boundary. */
10
+ function earlier(current, candidate) {
11
+ return current === undefined || compareRatios(candidate, current) < 0
12
+ ? candidate
13
+ : current;
14
+ }
15
+ /** Clips one infinite line or ray against an inclusive axis-aligned box. */
16
+ export function infinitePathInterval(kind, axes) {
17
+ let entry = kind === "ray" ? numberRatio(0) : undefined;
18
+ let exit;
19
+ for (const axis of axes) {
20
+ if (axis.start === axis.through) {
21
+ if (!axisContains(axis.range, axis.start)) {
22
+ return;
23
+ }
24
+ continue;
25
+ }
26
+ const first = differenceRatio(axis.range.min, axis.start, axis.through);
27
+ const second = differenceRatio(axis.range.max, axis.start, axis.through);
28
+ const near = compareRatios(first, second) <= 0 ? first : second;
29
+ const far = near === first ? second : first;
30
+ entry = later(entry, near);
31
+ exit = earlier(exit, far);
32
+ if (exit !== undefined && compareRatios(entry, exit) > 0) {
33
+ return;
34
+ }
35
+ }
36
+ return { entry, exit };
37
+ }
38
+ /** Checks whether one infinite line or ray intersects an inclusive axis-aligned box. */
39
+ export function infinitePathIntersectsBox(kind, axes) {
40
+ return infinitePathInterval(kind, axes) !== undefined;
41
+ }