Skip to content

feat(core): unit registry for world_units; per-dimension display units; SI <-> nav's normalized frame - #632

Merged
transfix merged 1 commit into
masterfrom
feat/world-units-registry
Oct 16, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/world-units-registry

Conversation

@transfix

Copy link
Copy Markdown
Owner

Why

Doc 31's physics tier needs the following before the Jolt work starts:

  • More quantities. world_units covered eight dimensions. The energy ledger and the vehicle plant also need power, pressure, temperature, curvature, charge and energy per distance.
  • Custom units. Applications need to add units the built-in table does not have.
  • The normalized frame. nav's plant adapter needs a correct, tested way to move SI quantities into the normalized frame its kernels and weights use.

What

  • cvc::unit_registry (inc/cvc/core/unit_registry.h):
    • Dimensions each have an SI unit and a length exponent.
    • Units come in three kinds: linear, affine (degC, degF) and function (dBm, or any invertible pair).
    • Built-ins: SI and imperial for every world_units dimension, plus nmi kn kWh Wh/km kWh/100km psi bar rpm mAh acre gal and others. Every factor comes from cvc::units.
    • Custom units: add_dimension, add_linear, add_affine and add_function add units at run time.
    • Errors: duplicates, bad factors, unknown units and conversions across dimensions throw the new cvc::unit_error.
    • Copyable and thread-safe. A function unit's callable runs outside the lock.
  • world_units:
    • 14 dimensions are appended to the enum, so the existing values are unchanged.
    • Each instance owns its own registry (registry()).
    • to_si, from_si and convert.
    • Per-dimension display overrides: set_display_unit, clear_display_unit and display_unit. A length override turns off km/mile promotion.
    • Every display call also takes the dimension by name, so custom dimensions work.
  • SI ↔ normalized frame:
    • to_normalized and from_normalized scale by the dimension's length exponent: velocity by S / mpwu, curvature by mpwu / S; time, angle and mass pass through.
    • si_point_to_normalized and normalized_point_to_si apply the centre in the same order as nav's snapshot (o/S + c).
    • cvc::nav::frame_of(sim_world::config) (inc/cvc/nav/nav_units.h) builds the frame for a config.
    • No kernel calls any of this, so nav traces are unaffected.
  • pycvc:
    • unit_registry is wrapped. add_function takes Python callables and re-acquires the GIL for each call; an exception raised in the callable comes back as a RuntimeError that names it.
    • The new world_units calls follow the existing _d convention.
    • world_units_normalized_frame and world_units_xyz are exposed.
  • Docs:
    • docs/WORLD_UNITS_API.md: the dimension table with exponents, the registry, display overrides, the frame, and Python.
    • docs/roadmap/NAV_UNITS_ROADMAP.md: a note on which primitives have landed.

Verification (Windows, MSVC 2022, local cvcpkg prefix)

  • world_units_test: 40/40 pass. The 25 existing tests pass unmodified, so existing conversions are unchanged. 15 tests are new:
    • exact anchors (nmi, kWh, mAh, hp, psi, gal, kn);
    • affine temperature;
    • the dBm round trip;
    • custom dimensions and units;
    • rejection cases;
    • registry isolation;
    • display overrides;
    • normalized scaling at exponents −1, 0, 1 and 2, and with mpwu ≠ 1;
    • point arithmetic matching the snapshot;
    • concurrent registration.
  • system_macro_headers_test: passes with both new headers included after <windows.h>.
  • test_pycvc_world_units.py: all pass, including 7 new cases. The world_clock, app, state, nav_train, threading and memory suites also pass.
  • clang-format 18 has been run on the changed lines.

…s; SI <-> nav's normalized frame

world_units converted through a fixed table of eight dimensions. The physics
tier needs power, pressure, temperature, curvature, charge and energy per
distance, applications need units the table does not have, and nav's plant
adapter needs to move SI quantities into the normalized frame its kernels
and weights live in.

- cvc::unit_registry (core/unit_registry.h): dimensions with an SI unit and a
  length exponent; units that are linear, affine (degC, degF) or a pair of
  functions (dBm). The built-in table covers SI and imperial for every
  world_units dimension plus nmi, kn, kWh, Wh/km, psi, rpm, mAh and others,
  with every factor from cvc::units. add_dimension / add_linear /
  add_affine / add_function drop in custom units at run time. Duplicates,
  bad factors, unknown units and conversions across dimensions throw the new
  cvc::unit_error. The registry is copyable and thread-safe. A function
  unit's callable runs outside the lock.
- world_units: 14 dimensions are appended to the enum, so the existing values
  are unchanged. Each instance owns its registry. New calls:
  - to_si / from_si / convert;
  - set_display_unit / clear_display_unit / display_unit per dimension (a
    length override turns off km/mile promotion);
  - every display call by dimension name, so custom dimensions work.
- SI <-> normalized: to_normalized / from_normalized scale by the
  dimension's length exponent, so velocity is multiplied by S / mpwu,
  curvature by mpwu / S, and time, angle and mass pass through.
  si_point_to_normalized / normalized_point_to_si apply the centre in the
  order nav's snapshot uses. cvc::nav::frame_of(sim_world::config) names the
  frame. No kernel calls any of it.
- pycvc wraps unit_registry (add_function takes Python callables and
  re-acquires the GIL per call), the new world_units calls with the _d
  convention, and normalized_frame / xyz.
- Existing conversions are unchanged: all 25 earlier world_units tests pass
  untouched. 15 new C++ tests and 7 new Python tests cover the registry,
  overrides, custom dimensions, the frame and concurrent registration. The
  header-hygiene test now includes both new headers.
- Docs: WORLD_UNITS_API.md (dimension table, registry, frame, Python) and a
  NAV_UNITS_ROADMAP.md update noting which primitives have landed.
@transfix
transfix merged commit 0a58af4 into master Oct 16, 2026
15 checks passed
@transfix
transfix deleted the feat/world-units-registry branch October 16, 2026 02:35
transfix added a commit that referenced this pull request Oct 17, 2026
Minor release. It carries:
- the world clock: lockstep and batch, followers, the sim.* binding, and nav
  on the clock (#629, #630, #631, #640);
- the units registry with SI <-> normalized-frame conversions (#632);
- cvc::physics with the Jolt backend (#641);
- the material trainer on the kinematic bicycle and its pycvc bindings
  (#642).
Bumps project(VERSION) 3.5.1 -> 3.6.0 and every cvcpkg recipe's
upstream_version to match. cvc_revision stays 1, per the recipe-version gate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant