Skip to main content

Module ransac

Module ransac 

Source
Expand description

RANSAC robust model fitting (lines and planes).

RANSAC (RANdom SAmple Consensus) is an iterative method for estimating the parameters of a mathematical model from a set of observed data that contains a large fraction of outliers. This module provides robust fitting of:

  • 2D lines (a x + b y + c = 0, normalized a² + b² = 1) via ransac_fit_line.
  • 3D planes (a x + b y + c z + d = 0, normalized a² + b² + c² = 1) via ransac_fit_plane.

The implementation follows the classic Fischler–Bolles algorithm with an adaptive stopping criterion: the required number of iterations is recomputed whenever a better consensus set is found, so clean data terminates quickly while noisy data exhausts the iteration budget.

§Robustness details

The fit complements Slice 21’s Weiszfeld geometric_median: where the geometric median is robust to outliers in a location estimate, RANSAC is robust to outliers in a parametric model estimate. After the consensus set is found, the model is refined with total (orthogonal) least squares on the inliers, which minimizes the perpendicular distances rather than the vertical residuals — appropriate when there is no privileged axis.

§Determinism

No external RNG is used. Sampling relies on a deterministic Knuth MMIX linear congruential generator seeded by RansacOptions::seed, so identical inputs and seeds always produce identical results (see the seed-reproducibility tests). This honors the project’s SciRS2 policy (no rand / rand_distr).

§Example

use oxigdal_algorithms::vector::{Point, RansacOptions, ransac_fit_line};

// Points on the line y = 2x + 1 with one gross outlier.
let points = [
    Point::new(0.0, 1.0),
    Point::new(1.0, 3.0),
    Point::new(2.0, 5.0),
    Point::new(3.0, 7.0),
    Point::new(4.0, 9.0),
    Point::new(2.0, 50.0), // outlier
];
let result = ransac_fit_line(&points, &RansacOptions::default())?;
assert!(result.converged);
assert!(!result.inliers.contains(&5)); // outlier rejected

Structs§

RansacLineModel
A 2D line in implicit form a x + b y + c = 0 with the normal (a, b) normalized so that a² + b² = 1.
RansacOptions
Configuration for RANSAC fitting.
RansacPlaneModel
A 3D plane in implicit form a x + b y + c z + d = 0 with the normal (a, b, c) normalized so that a² + b² + c² = 1.
RansacResult
Result of a RANSAC fit.

Functions§

ransac_fit_line
Robustly fits a 2D line to points using RANSAC.
ransac_fit_plane
Robustly fits a 3D plane to points using RANSAC.