Keep curvature calculations finite for valid road geometry - #101
Open
FrogAi wants to merge 1 commit into
Open
Conversation
FrogAi
force-pushed
the
codex/keep-curvature-geometry-finite
branch
from
August 10, 2026 02:54
d068322 to
d7c5918
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Developer summary
CalculateCurvaturecan produceNaNangle and arc values for valid straight, near-collinear, and inverse-trig-boundary road geometry. Those values then poison weighted curve averaging and can reject otherwise valid next-way connections.This keeps the calculation finite by preserving float64 distance precision internally, using a cancellation-resistant form of Heron's formula, and applying the finite straight-line limit. The exported
DistanceTo(Position) float32API and its rounding are unchanged.Verification
mainfails the focused primitive and downstream-pipeline matrix; this branch passes it 100 consecutive times.go test ./...,go test -race ./...,go vet ./..., andgo build ./...pass on Linux with Go 1.25.1.Compatibility
Schemas, settings, dependencies, file formats, duplicate-point behavior, and the public distance API are unchanged. The numerical fixtures establish the failure and correction, but do not measure how often real routes encounter it.
Engineering record and audit trail
Root cause
CalculateCurvaturenarrowed all three side lengths to float32 before applying Heron's formula. Valid near-collinear coordinates could therefore acquire a negative radicand. Exact straight geometry divided through an infinite radius, while a valid near-semicircle fixture could put the inverse-cosine input just below-1. Each path producedNaNangle or arc fields.The consequence is not limited to the offending point.
GetAverageCurvaturesweights each curvature byArcLength, so oneNaNarc poisons the three-element window. The averaged curvature and target velocity then becomeNaN, and later comparisons silently drop that point.isValidConnectionalso rejectsAbs(NaN) <= threshold, so otherwise valid next-way connections can be discarded.Implementation
DistanceTo(Position) float32as the public wrapper.0, angle0, and arc length equal to the endpoint span so weighted smoothing retains straight distance.2*asin(min(1, chord*curvature/2)), then derive arc length asangle/curvature.Red/green behavior
1daead9d7c5918NaN0, angle0, arc222.459666459 mNaN1.55736201944e-5 1/m, all scalars finiteNaN0.137769958673 1/m, all scalars finiteNaN0.00211377609422 1/m, target29.9810823142 m/sThe focused matrix also covers four duplicate layouts, reversal symmetry, a deterministic 10,000-triple finite-coordinate sweep, and fixtures bracketing the
0.10,0.15, and0.30connection thresholds.An earlier differential campaign against the same production math change covered 252 circle fixtures, 600,000 generated road triples at three jitter levels, and 1,000,000
DistanceTopairs. It found no candidateNaN/infinite scalars and preserved the exported float32 distance results bit-for-bit. This is synthetic numerical evidence, not a prevalence study.Final validation
The final rebased, single-commit head is
d7c5918396e2ad9c6c686864f538978522be432eon current main1daead9757c9fb5830c47bf928eddebf377a6fbb.NaNcases-count=100go test ./...go test -race ./...go vet ./...go build ./...git diff --checkScope limits
This establishes the demonstrated numerical failure modes, finite downstream behavior for the supplied fixtures, and connection-threshold preservation at the tested boundaries. It does not establish route prevalence, on-road frequency or impact, global pole/antimeridian behavior, or a policy for invalid GPS coordinates.