- Go 100%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .github | ||
| _lookup | ||
| docs | ||
| examples | ||
| .gitignore | ||
| .golangci.yml | ||
| AGENTS.md | ||
| constructors_bench_test.go | ||
| doc.go | ||
| export_test.go | ||
| frame.go | ||
| frame_bench_test.go | ||
| frame_test.go | ||
| frame_valid_reference_test.go | ||
| go.mod | ||
| go.sum | ||
| isvalid_reference_test.go | ||
| LICENSE | ||
| README.md | ||
| screw.go | ||
| screw_bench_test.go | ||
| screw_test.go | ||
| tensor.go | ||
| tensor_diagonal_test.go | ||
| tensor_dyadic.go | ||
| tensor_dyadic_bench_test.go | ||
| tensor_dyadic_test.go | ||
| tensor_inverse_dyadic.go | ||
| tensor_inverse_dyadic_test.go | ||
| tensor_positive_reference_test.go | ||
| tensor_test.go | ||
| transform.go | ||
| transform_bench_test.go | ||
| transform_test.go | ||
| transform_validation_internal_test.go | ||
| vec.go | ||
| vec_abs_test.go | ||
| vec_bench_test.go | ||
| vec_test.go | ||
r3
Coordinate math for Euclidean 3-space (ℝ³) in Go: a Vec vector type, an
orthonormal right-handed Frame carrying the transform between a plane's local
(u, v, w) coordinates and world (x, y, z), and a Transform — a rigid
motion of the space itself.
import "github.com/lestrrat-3d/r3"
// The XZ datum: U = +X, V = +Z, so N = U × V = −Y.
f, err := r3.NewFrame(r3.Vec{}, r3.NewVec(1, 0, 0), r3.NewVec(0, 0, 1))
if err != nil {
return err // r3.ErrDegenerateFrame: zero or collinear axes
}
w := f.ToWorldUV(3, 4) // a 2D point on the plane -> world
l := f.ToLocal(w) // and back; exact, because the frame is orthonormal
A Transform moves things: place a body, pattern it, mirror it.
import "github.com/lestrrat-3d/units"
// 30° about +Z, through (10, 0, 0). Angles are typed — no bare radians.
spin, err := r3.RotationAround(
r3.NewVec(10, 0, 0), r3.NewVec(0, 0, 1), units.Degrees(30),
)
if err != nil {
return err
}
lift, err := r3.Translation(r3.NewVec(0, 0, 5))
if err != nil {
return err // r3.ErrNonFinite: a NaN or infinite displacement
}
// Compose left to right, in the order things happen. Fallible, because two
// valid transforms can compose to a translation that overflows to infinity.
place, err := spin.Then(lift)
if err != nil {
return err // r3.ErrNonFinite
}
p := place.Apply(pt) // a POINT: rotated, then moved
n := place.ApplyDir(nrm) // a DIRECTION: rotated only — never translated
back, err := place.Inverse() // exact: the transpose, not a solve
if err != nil {
return err // r3.ErrNonFinite
}
A proper Transform is a screw motion: a rotation about a line plus a slide
along it. Screw reads those parameters back, At rebuilds a fraction of the
motion, and Interpolate walks from one pose to another along the screw.
sc, err := place.Screw() // sc.Axis, sc.Point, sc.Angle (in [0, π]), sc.Slide
if err != nil {
return err // r3.ErrImproper: a reflection has no screw motion
}
step, err := sc.At(0.5) // half the rotation, half the slide
if err != nil {
return err
}
mid, err := from.Interpolate(to, 0.5) // the pose halfway from `from` to `to`
if err != nil {
return err
}
The design is in docs/screw-motion-design.md.
Scope
The package holds what lives in 3-space and what acts on it — vectors,
frames, and the transforms between them. Nothing else. It carries no document
state. lestrrat-3d/units is the only
non-stdlib dependency a build pulls in — the one thing go list -deps . reports
beyond the standard library — and its own package code imports nothing but the
standard library, so the build graph stops there. (units requires testify to run
its own tests; that is a test-only dependency of units and never reaches a
build.)
3D shapes (spheres, boxes, surfaces, solids) are deliberately out of scope. They belong to a geometry layer above, which imports this one for its coordinates. The name is the scope rule: if it lives in ℝ³, it belongs here; if it is a shape, it does not.
Invariants
- A
Frameis always orthonormal and right-handed. The only constructor isNewFrame, which orthonormalizes with Gram–Schmidt and returnsErrDegenerateFrameon zero or collinear axes. The zero valueFrame{}is invalid and says so viaIsValid(), so a frame accepted from outside can be vetted before you build on it. N()is derived, never stored (U × V), so a frame cannot disagree with its own normal.ToLocalis the transpose, not a matrix solve — exact, because the axes are orthonormal.- A
Transformis always an isometry. Scale, shear and projection are unrepresentable, not merely discouraged: nothing in the package can build one. Every operation that yields aTransformbutIdentityis fallible — the constructors (Translation,Rotation,RotationAround,Reflection,FromFrame,FromBasis) and the derivations (Then,Inverse,Screw.At,Interpolate) — and each validates what it produces, not just what it consumes, rather than admit a non-isometry. That is what buys the next two properties. - Nothing non-finite gets in. A NaN or infinite angle, position, origin or
translation is rejected with
ErrNonFinite— including a translation that overflows to infinity from inputs that were each individually finite: aReflectionacross a plane far enough out, aRotationArounda pivot far enough out, or theThenof two transforms that are each perfectly valid. ATransformthat exists is a real rigid motion, no asterisk. Composition being fallible is the bill for that; an always-nilerris a small tax, a silently infinite placement is not. - Bigness is not a fault — except in the per-point mappings. Huge-but-finite
input is built, not refused, on the cold paths:
NewFrameorthonormalizes axes out atMaxFloat64, andReflectionoffsets a mirror plane that far out, by scaling their arithmetic. They run once per frame or per feature, so the cost is nothing. The per-point mappings —Transform.Apply,Transform.ApplyDir,Frame.ToWorld,Frame.ToWorldUV,Frame.ToLocal— do not pay it: each sums its terms in fixed order, so an intermediate sum can reach±Infwhere the exact result is representable. They run once per transformed point, and overflow-safe accumulation would tax every point forever to serve coordinates that cannot exist (the unit here is the millimetre;1e308mm is ~1e289light-years). The cost is not uniform, so be precise about it:- A
Transformis never silently wrong.ThenandInverseare fallible: they catch the±Infand returnErrNonFinite, conservatively refusing a composition whose true value was representable. - The five mappings above are infallible, so at those magnitudes they hand
back a
Vecwith a non-finite component — a wrong answer, not an error, inFrame's world/local mapping exactly as much as inTransform's. AtMaxFloat64you must check the returnedVec. Everywhere a real model lives, this cannot arise.
- A
Transform.Inverseis exact — the transpose of an orthogonal matrix, never a solve. Admitting scale would cost this, and so would storing a basis that is only nearly orthonormal: the transpose inverts a truly orthonormal basis and nothing else, and one skewed by even7e-10round-trips with a drift of ~1e-8. So the linear part of everyTransformis orthonormal to machine precision, not merely inside the1e-9tolerance that admits one —FromBasis, the door a reloaded basis comes in through, orthonormalizes what it admits (Gram–Schmidt, asNewFramedoes for a frame's axes) rather than storing it verbatim. What is admitted is unchanged: a scale, a shear or a collapse is stillErrNotOrthonormal, never silently corrected into a transform you did not describe. Only tolerance-level drift is snapped straight — and handedness survives it: a reflection (det = −1) comes back a reflection, because all three vectors are orthonormalized in turn instead of the third being re-derived asEX × EY, which would flip an improper basis proper without a word.- A normal transforms like a direction (
ApplyDir). No inverse transpose, anywhere. Under a general affine map normals need one and everybody forgets; here it cannot be got wrong. Vec.Normalizereturns(Vec, bool)and never fabricates a unit vector from a zero vector. It is a divide-by-zero guard, not a geometric tolerance — and not a size limit: a vector whose length overflows, like(MaxFloat64, MaxFloat64, 0), still has a direction, and gets it ((1/√2, 1/√2, 0)).- Angles are typed (
units.Value), soRotationrejects a length, a bare scalar, and the zero value alike — a forgotten angle is an error, not a silent 0°.
Design documents
docs/screw-motion-design.md— screw decomposition,Screw.AtandTransform.Interpolate.
License
This project is source-available, and is licensed under the PolyForm Noncommercial License 1.0.0.
- Noncommercial use is free. Individuals, hobby and personal projects, research, education, nonprofits, and government may use, modify, and redistribute it at no cost, subject to the license terms.
- Commercial / business use requires a separate license. Any use by or for a business, or for commercial advantage, is not permitted under the noncommercial license. To obtain a commercial license, reach out on Bluesky at @lestrrat.bsky.social.
Contributions
This repository does not accept external pull requests.