|
Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
|
<portals/geometry.hpp> supplies CellPoint, CellSize and CellRect in the portals namespace. Coordinates and extents are measured in terminal cells and use std::int32_t: columns increase rightward and rows downward from the upper-left origin. Negative origins are valid for local/offscreen geometry. Canvas logical coordinates and future GUI units require an explicit transform; these types define no such conversions.
CellSize is an available extent and may store negative components. Its normalized() result clamps each negative component to zero. CellRect::from performs that normalization and checks both exclusive ends before creating a rectangle. Rectangles have private storage, nonnegative extents and representable origins/ends. A default rectangle is empty at (0, 0).
The cell set is [x, x + width) × [y, y + height). Either zero extent makes it empty. Right/bottom edges are excluded, so touching edges have no shared interior. Clipping is rectangle.intersection(clip). Intersection cannot fail for valid rectangles: its origin is the per-axis maximum of the input origins and each extent is the overlap on that axis, clamped to zero. An empty result retains this origin and any overlap on the other axis; it need not be located inside either input. Equality compares origin and extents, even for empty rectangles. contains(point) always returns false on empties. contains(rectangle) uses cell-set inclusion, so every empty rectangle is a subset of every rectangle, regardless of its position. Intersection is commutative and idempotent, including degenerate rectangles.
Creation and translation return std::expected<T, GeometryError> with COORDINATE_OVERFLOW if an origin or exclusive end would leave the int32_t range. This local error type handles the one recoverable geometry failure; it does not commit the rest of the library to an error policy. Expected fits the nonthrowing, allocation-free layout boundary and keeps failure observable. Inputs already have int32_t components; callers converting wider or fractional values must validate them before passing them here. No saturating coordinates, wrapping or unchecked implicit unit conversions are provided.
All addition/subtraction is performed in int64_t before checked narrowing or under a valid-rectangle invariant. An intersection's positive extent is at most either input extent, which proves that narrowing it is safe. The maximum positive extent is INT32_MAX; the full signed coordinate span is intentionally not one representable rectangle. A nonempty rectangle beginning at INT32_MAX is rejected because its exclusive end would not fit. Translating even an empty rectangle checks its origin; translating a nonempty rectangle checks its ends as well.
Every parameter/result is an owning value with independent lifetime and no borrowed storage. These operations are thread-agnostic, have no callbacks, I/O, allocation or exceptions, and do not mutate inputs or invalidate other values. Concurrent reads are safe; callers synchronize shared assignment. Geometry changes do not dispatch layout/paint invalidation: the UI owner requests invalidation when installing new geometry. No runtime tree or GUI functionality is implied.
The headless portals.geometry test checks integer limits, normalization, translation failures and half-open edges, then exhausts a bounded deterministic rectangle/cell domain against an independent interval/cell-set oracle. Checks remain active in Release with NDEBUG; no random seed or external test library is required.
Reference: C++23 expected specification, fixed-width integer types.