@expofp/geometry

中文文档
3.49.2发布于 2026/11/14MITTypeScriptES Module近 30 天下载 9273

ExpoFP SDK internal: shared geometry primitives

原始使用文档
文档目录10 节
  1. Primitives
  2. Design decisions
  3. 2.5D: 2D shapes + elevation, one 3D Mesh
  4. Immutable by default, with set mutators
  5. Opt-in mutation via a target parameter
  6. Classes + free functions (the duality)
  7. Transform/merge logic defined once ( Polygon reuses Mesh )
  8. Structural input types
  9. Brands instead of instanceof
  10. Geographic ( geo )

@expofp/geometry

Zero-dependency 2.5D geometry primitives for the ExpoFP SDK: Point, Line, Box, Rect, Polygon, Mesh, plus the free functions that operate on them. No runtime dependencies (not even three).

"2.5D" means: a 3D Point vector, but the shapes are 2D geometry positioned in 3D by an elevation — a flat shape at a given height. All spatial math (distance, angle, area, containment, intersection) is computed in the xy plane; elevation positions the shape and gates intersections (see Coordinate convention). The one genuinely-3D shape is Mesh (a triangle mesh whose vertices carry real z), for renderer geometry that isn't flat.

Primitives

TypeWhat it is
PointA mutable 3D vector (x, y, z). width/height alias x/y so a point can double as a size. The low-level building block the shapes write into.
LineA segment between two 2D points (p0, p1) at a single elevation (default 0).
BoxAn axis-aligned rectangle (min/max corners, cached center/size). 2D, infinite in z (no elevation) — for bounding boxes and screen sizes. A Box structurally satisfies RectLike, so anything taking a RectLike accepts a Box (an unrotated, elevation-less rect that intersects at any height).
RectA rectangle that may be rotated: center + size + rotation + elevation (both default 0). For oriented rectangular shapes at a given height.
PolygonA planar triangulated polygon: 2D vertices + triangle indices + an elevation, with a cached bounding Box. Has containsPoint/area (well-defined because it's flat). Build a convex one from an ordered ring with Polygon.fromConvexRing.
MeshA 3D triangle mesh: 3D vertices + indices, with a cached (xy) bounding Box. Transforms only — no containsPoint/area (undefined for a non-planar mesh). For renderer geometry that isn't flat.

Shape = Box | Rect | Polygon | Mesh, discriminated by is* brand fields and narrowed with the exported guards isBox / isRect / isPolygon / isMesh.

Coordinate convention: x increases left→right, y increases top→bottom (screen, y-down). Rotation is in radians, positive = clockwise, uniform across every primitive and helper (Rect, Polygon, Mesh, and the point*/line* angle functions). elevation (and Point.z) is the out-of-plane height; positive is toward the viewer. Spatial operations are computed in the xy plane — distances, angles, areas, and containsPoint ignore z/elevation. Intersection tests (lineIntersection, intersectLineRect) compare elevation only when both operands define it (an undefined elevation — a Box, or a bare LineLike — means "any height" and always intersects); when both are defined they intersect only within an elevationTolerance (default 1e-3), and the result point's z is the shared elevation.

Design decisions

2.5D: 2D shapes + elevation, one 3D Mesh

Point is the only inherently-3D type (a vector). Every shape is 2D positioned by elevation (Box has none — it is infinite in z). Polygon is planar, so its containsPoint/area are exact; genuinely 3D geometry uses Mesh, which deliberately omits those operations (a "contains point" on a non-planar mesh would only be an xy-silhouette test).

Immutable by default, with set mutators

Shapes are immutable value objects: getters return readonly views and every transform returns a new instance, so shared geometry (e.g. a booth's rect) can't be mutated out from under another reader. Each primitive (Point, Line, Box, Rect, Polygon, Mesh) exposes a single public, docs-flagged set(...) mutator — the escape hatch the transforms write through, and the way to update fields like elevation in place.

Opt-in mutation via a target parameter

Because immutable transforms allocate, every transform also accepts an optional target to write the result into instead of allocating — the same idiom three.js uses, but inverted so the default is pure:

const moved = box.translate(offset); // new Box (immutable default)
box.translate(offset, box); // writes into box itself (opt-in mutation)
box.translate(offset, scratch); // reuse a scratch Box in a hot loop

Free functions read all inputs before writing the target, so passing the input as the target (f(x, …, x)) is safe. Use target only where profiling shows allocation matters.

Classes + free functions (the duality)

Every operation is a pure free function over structural *Like inputs, and each class exposes a thin method that forwards to it:

lineLength(line); // free function, accepts any { p0, p1 }
line.length(); // method → lineLength(this)

boxTranslate(box, offset); // free function
box.translate(offset); // method → boxTranslate(this, offset)

Free functions accept structural shapes (Point2Like, LineLike, RectLike, MeshLike, …), so callers don't need class instances and there is no class-identity coupling. Methods give discoverability and chaining.

Transform/merge logic defined once (Polygon reuses Mesh)

Polygon structurally satisfies MeshLike (its 2D vertices satisfy PointLike), so the transform and merge logic lives once on the mesh free functions — meshTranslate / meshScale / meshRotate / meshMerge (plus meshBounds). Both Mesh's and Polygon's methods forward to them via the optional target: a Polygon target keeps the result a Polygon (elevation preserved), a Mesh target keeps it a Mesh.

Structural input types

Inputs are the loose *Like types (Point2Like = { x, y }, RectLike = { center, size, rotation?, elevation? }, MeshLike, PolygonLike, …); outputs are concrete class instances. Ops like translate / expand / scale take number | Point2Like (planar shapes such as Box) or number | PointLike (the 3D Point ops) — a scalar broadcasts to both axes, a point gives per-axis values.

Brands instead of instanceof

Each primitive carries a readonly is<Name> = true field (three.js style) so type discrimination survives duplicate module copies and serialization. The Shape guards check the brand rather than instanceof, and accept only a Shape (a stray { isBox: true } object cannot masquerade as a box).

Geographic (geo)

The geo module provides pure geodetic math on a WGS-84 sphere. It is a separate coordinate space from the planar primitives above — do not mix them without an explicit bridge.

SymbolDescription
LatLng{ lat: number; lng: number } — decimal degrees; lat north-positive, lng east-positive
GeoAnchor{ local: Point2Like; geo: LatLng } — a correspondence used by the projection bridge
haversineDistance(a, b)Great-circle distance in metres
bearing(from, to)Initial compass bearing in degrees [0, 360)
destinationPoint(from, distanceM, bearingDeg)Destination LatLng given origin, distance, and bearing
projectLocalToGps(point, a, b)Maps a local Point2Like to LatLng via two anchors
projectGpsToLocal(geo, a, b)Maps a LatLng to a local Point2Like via two anchors — inverse of above

Convention (different from the planar primitives):

  • Coordinates are in degrees (not radians).
  • Bearings are compass: 0 = North, 90 = East, 180 = South, 270 = West, increasing clockwise.
  • Earth radius constant: 6 371 000 m (WGS-84 mean radius).

This is deliberately firewalled from the planar convention (angles in radians, positive-clockwise starting from the +x axis, y-down screen coordinates, Euclidean distance). The only bridge is projectLocalToGps / projectGpsToLocal, which takes validated GeoAnchor inputs — no floorplan-domain logic enters geo.

版本与发布记录

@expofp/geometry 共发布 133 个版本,首次发布于 2026/06/24;过去 12 个月发布了 133 个版本。

版本发布日期距今
3.49.2latest2026/11/14今天
0.0.0-experimental.3b0db16feat/wayfinding-compiler2026/11/13今天
3.49.12026/11/121 天前
3.49.02026/11/112 天前
3.48.02026/11/112 天前
3.47.22026/11/103 天前

其他发布通道:multi-venue → 0.0.0-experimental.cf063fc,stable → 3.47.0,eurotier → 0.0.0-experimental.6dabc65,feat/wayfinding-compiler → 0.0.0-experimental.3b0db16。

关于 @expofp/geometry 的常见问题

@expofp/geometry 是什么?

ExpoFP SDK internal: shared geometry primitives

如何安装 @expofp/geometry?

在项目目录运行 npm install @expofp/geometry,或使用 pnpm add / yarn add / bun add。

@expofp/geometry 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@expofp/geometry 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@expofp/geometry 可以商用吗?

@expofp/geometry 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@expofp/geometry 的下载量有多少?

根据 npm 官方下载统计,@expofp/geometry 在 2026/10/14 – 2026/11/12 这 30 天内被下载 9,273 次,日均约 309 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@expofp/geometry 还在维护吗?

最新版本 3.49.2 发布于 2026/11/14(今天);至今共发布 133 个版本,其中过去 12 个月发布了 133 个;首次发布于 2026/06/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。