Skip to content

Fix Line3 distance for parallel and non-unit directions - #224

Merged
taughz merged 1 commit into
rai-opensource:masterfrom
Doribelove:codex/line3-distance
Oct 13, 2026
Merged

taughz merged 1 commit into
rai-opensource:masterfrom
Doribelove:codex/line3-distance

Conversation

@Doribelove

@Doribelove Doribelove commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Line3.distance() raises NameError for parallel lines and can return incorrect distances for skew or intersecting lines. For example:

from spatialmath import Line3

a = Line3.PointDir([0, 0, 0], [1, 0, 0])
b = Line3.PointDir([0, 0, 2], [0.6, 0.8, 0])
print(a.distance(b))  # before: 2.5; expected: 2.0

The old expression divides by the squared cross-product norm and uses a reciprocal product that mixes unit directions with unnormalized moments. Scaling directions can therefore change the result; a translated intersecting pair can also return a nonzero distance.

Compute the displacement between principal points and project it onto the common normal of the unit directions. For parallel directions, use the perpendicular component of that displacement and return a scalar. Apply the existing tol parameter to the parallel-direction test.

Initial validation on base f6a572c6e75e2a7e3c06e632bc34153941e00404:

  • Added regressions with the original implementation: 21 failed, 18 passed in tests/test_geom3d.py; with the fix: 39 passed.
  • Full tests suite: baseline 346 passed, 3 skipped; fixed 367 passed, 3 skipped, on Python 3.10/NumPy 2.2.6 and Python 3.12/NumPy 2.5.3. Existing plotting/deprecation warnings remain.
  • Separate seeded validation: 4,800 scalar comparisons per environment against closest points computed by numpy.linalg.lstsq, including parallel/intersecting/skew lines, sign/scale changes, rigid transformations and operand reversal.
  • Pinned pre-commit hooks on both changed files, git diff --check, wheel and sdist builds passed.

The branch is now rebased onto upstream 2de7801 (current head 9600087). On this updated tree, the complete tests suite passes with 368 passed, 3 skipped on Python 3.10.12, and the four pre-commit hooks on both changed files and git diff --check pass. The newer upstream commit adds a separate spatial-vector regression, accounting for one more passing test than the initial run. GitHub Actions has since run: all 10 platform/Python unittest jobs, the aggregate test check, and Codecov passed. The Sphinx documentation build succeeded with 22 warnings, but its final gh-pages push failed with HTTP 403 because github-actions[bot] lacks write permission for this fork PR. This patch changes only spatialmath/geom3d.py and tests/test_geom3d.py.

AI assistance: the implementation, tests and this description were prepared with Codex; the local validation commands were executed locally, and the multi-platform results above come from GitHub Actions.

Project principal-point displacement onto the common normal of unit
directions, with a scalar perpendicular-distance fallback for parallel
lines. Cover intersecting, coincident and skew lines, direction rescaling,
operand reversal and the parallel tolerance.

AI assistance: prepared with Codex; validation executed locally.
@Doribelove

Copy link
Copy Markdown
Contributor Author

I rebased this PR onto current master (2de7801), so it is no longer behind the target branch. On the new head (9600087), the complete local test suite passes with 368 passed and 3 skipped; the changed-file pre-commit hooks and git diff --check also pass. The new GitHub Actions CI run is marked action_required and needs approval from a repository maintainer before it can execute. Could someone approve that run and review the fix when convenient? Thank you.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@petercorke

Copy link
Copy Markdown
Collaborator

Thank you for your interest in SMTB, and for the nice, well-tested fix!

I dug into this a bit further and independently confirmed both bugs it fixes are real: the parallel branch on master actually raises NameError (calls a bare dot(...) that isn't defined anywhere in the module — only np.dot is), and the skew branch's norm(cross(...)) ** 2 denominator gives wrong distances at any angle other than 90° between the lines' directions — the one existing test happened to use perpendicular lines, which is exactly the case where that bug is invisible. Your new tests (parametrized across geometry × scale, including the tiny 1e-8 factors) catch both nicely.

I've filed #231 to track a related, out-of-scope issue: isparallel()/__or__ have the same non-unit-direction scale-dependency, just without the crash. Not blocking this PR.

Approving — thanks again for the contribution!

@Doribelove

Copy link
Copy Markdown
Contributor Author

Thank you for reviewing and approving the fix. I checked the remaining red sphinx / sphinx job on 9600087: make html succeeded (22 warnings), and the failure occurred only when the workflow tried to push the generated site to gh-pages. GitHub returned HTTP 403: github-actions[bot] cannot write to the upstream repository from this fork PR. All 10 unittest matrix jobs, the aggregate test check, and Codecov passed. This PR changes only the geometry code and its tests.

The repository's CI invokes the reusable Sphinx workflow for pull requests, and that workflow currently publishes unconditionally. A maintainer-side adjustment that builds docs on PRs but publishes only from trusted pushes would keep the documentation check without requiring write access from a fork PR. I updated the PR description with the current CI results; no additional commit was added to the approved patch.

Prepared with Codex on behalf of @Doribelove.

@petercorke

Copy link
Copy Markdown
Collaborator

I've approved this. CI fails because of an unmerged PR #222, not your code. There's a big backlog of PRs for RAI to merge, including a lot of my own PRs :(

@Doribelove

Copy link
Copy Markdown
Contributor Author

Thanks for clarifying, @petercorke. Understood that the remaining CI failure is covered by #222 and the merge is waiting on the RAI backlog. I appreciate the review and the independent checks.

@taughz taughz mentioned this pull request Oct 11, 2026
7 tasks done
@taughz
taughz merged commit 61a6c4f into rai-opensource:master Oct 13, 2026
24 of 26 checks passed
taughz pushed a commit that referenced this pull request Oct 16, 2026
## Summary

`Line3.isparallel()` compared the raw cross product of Plücker direction
vectors to an absolute tolerance. Scaling either representation
therefore changed whether perpendicular lines were reported as parallel.
The same classification feeds `|`, `isintersecting()` / `^`, and
`commonperp()`.

- Compare unit directions in `isparallel()` so the threshold represents
angular separation.
- Normalize both Plücker moments in `isintersecting()` so its
reciprocal-product tolerance is independent of coordinate scale.
- Build `commonperp()` from the nearest point and the cross product of
unit directions. The previous expression could also produce invalid
Plücker coordinates for nonorthogonal skew lines at unit scale.
- Add regressions for small, large, and sign-reversed scalings, a
near-parallel tolerance, crossing and skew lines, and the nearest-point
geometry.

For example, lines through `(0, 0, 0)` along `(1e-8, 0, 0)` and through
`(0, 0, 1)` along `(0, 1e-8, 0)` are perpendicular; before this change
`isparallel()` returned `True`.

This branch is based on current `master` and does not contain the
separate `distance()` fix in #224. I checked `closest_to_line()` on the
included scaled nonparallel cases; it needs no change for this issue.

## Validation

- New regression tests failed against the original implementation, then
passed with the change.
- `pytest -q --disable-warnings`: **350 passed, 3 skipped** (Python
3.10).
- Black and `git diff --check`: passed.
- Additional local geometric sampling across valid scaled line pairs
agreed with the analytical common-perpendicular location to
floating-point precision.

Fixes #231.

Implementation and tests were prepared and validated with OpenAI Codex
assistance.

---------

Signed-off-by: 李永祺 <doribelove@gmail.com>
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.

4 participants