Skip to content

feat(features2d): implement keypoint types and shared utilities#68

Open
kalwalt wants to merge 1 commit into
feat/m5-features2d-modulefrom
feat/issue-57-keypoints
Open

feat(features2d): implement keypoint types and shared utilities#68
kalwalt wants to merge 1 commit into
feat/m5-features2d-modulefrom
feat/issue-57-keypoints

Conversation

@kalwalt
Copy link
Copy Markdown
Member

@kalwalt kalwalt commented May 21, 2026

PR Summary


Detailed Description

This PR implements complete and robust KeyPoint properties, custom constructors, circle-circle overlap (IoU) math, coordinate converters, and mathematical selection/sorting utilities, directly resolving Issue #57.

All mathematical kernels and conversion logic are ported from original OpenCV C++ source (modules/core/src/types.cpp) to guarantee 100% behavioral parity:

  1. KeyPoint::overlap Circle-Circle IoU:
    • Compares keypoints by exact circular boundary intersection over union.
    • Includes precise clamping -1.0..=1.0 of cosine variables before computing .acos() to eliminate domain errors resulting from floating-point imprecision.
    • Properly handles enclosure conditions where one circle is fully within another, returning the exact enclosing area ratio min_radius^2 / max_radius^2.
  2. Coordinate Converters:
    • convert_to_points: Efficient conversion from &[KeyPoint] to Vec<Point2f>.
    • convert_to_points_masked: Validates indices against boundaries and negative integers, returning safe Result<Vec<Point2f>, PureCvError>.
    • convert_from_points: Maps &[Point2f] to Vec<KeyPoint> utilizing OpenCV default conventions where the orientation parameter (angle) defaults strictly to -1.0.
  3. Sorting & Filtering:
    • sort_by_response: high-performance NaN-safe sorting using partial_cmp().unwrap_or(Ordering::Equal) in either ascending or descending order.
    • retain_best: Extracted top N strongest response keypoints efficiently.

Review Checklist

General

  • Code follows project style guidelines (cargo fmt --check passes).
  • Self-review completed.
  • Comments added for complex logic (e.g. circle-circle intersection formulas).
  • No debugging code left.
  • No sensitive data exposed.

Code Quality

  • Zero FFI or unsafe Rust blocks are introduced (fully safe pure-Rust code).
  • Zero external dependencies are added (serde and other potential dependencies were avoided to keep the crate lightweight).
  • Functions are focused, clear, and documented.
  • No cloning on copyable types (Point2f implements Copy and is passed directly without redundant .clone()).

Testing

  • Extensive test suite implemented in src/features2d/tests.rs with 8 new robust unit tests.
  • All 267 unit tests + 28 doc-tests in the crate pass cleanly.

Risk Assessment

Factor Score Details Mitigation
Size 1/10 Small isolated additions (+315 lines) Minimal impact, reviewed as a single logical unit.
Complexity 3/10 Circle-circle overlap geometry uses trigonometric functions. Clamped cosine operands to prevent precision edge-case NaN/acos domain crashes.
Dependency 0/10 Zero external dependencies added. Maintain crate isolation and light weight.
Breaking Change 0/10 Only additive APIs or fully backwards-compatible overrides. Complete safety, existing client builds are unaffected.

Test Coverage

Area Before After Change
src/features2d/keypoint.rs 0% (Skeleton only) 100% +100% ✅
Overall Crate Tests 259 267 +8 passing unit tests ✅
  • Unit Tests Added:
    • test_keypoint_default
    • test_keypoint_new
    • test_keypoint_convert_to_points
    • test_keypoint_convert_to_points_masked
    • test_keypoint_convert_from_points
    • test_keypoint_sort_by_response
    • test_keypoint_retain_best
    • test_keypoint_overlap

Visual Aids

Circular Overlap Cases:

graph TD
    A[Overlap Types] --> B[No Overlap: dist >= r1 + r2]
    A --> C[Partial Overlap: dist < r1 + r2]
    A --> D[Full Enclosure: dist + min_r <= max_r]
    
    B --> B1[Returns 0.0]
    C --> C1[Calculates intersection area using segment/triangle math]
    D --> D1[Returns ratio min_r^2 / max_r^2]
Loading

Size Recommendations

At only 315 lines of highly cohesive changes, this PR is at a perfect size for review. No splitting is recommended or needed.


Review Automation

  • Lints: cargo clippy --all-targets --all-features was executed and is 100% clean with zero warnings on the implemented code.
  • Formatting: cargo fmt --check succeeds.
  • Tests: cargo test runs and passes successfully.

Implement robust KeyPoint properties, custom constructors, circle-circle overlap IoU calculations, coordinate conversion utilities, and response sorting mechanisms matching the exact OpenCV C++ types.cpp implementations.

Refs: #57, #65
@kalwalt kalwalt self-assigned this May 21, 2026
@kalwalt kalwalt added enhancement New feature or request rust-code rust Pull requests that update rust code tests features2D-module labels May 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request features2D-module rust Pull requests that update rust code rust-code tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant