1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! This module contains a dimension-generic parameterized line, `Line<D>`, which serves as the
//! shared foundation for the 2D and 3D line types. It is structured similarly to
//! [`SurfacePoint`](crate::common::SurfacePoint): the generic features live here, while the
//! dimension-specific behavior (normals and intersections in 2D, plane/sphere/circle
//! intersections in 3D, axis constructors, and isometry multiplication operators) lives in the
//! `geom2` and `geom3` modules.
use crate::Result;
use crate::common::PCoords;
use crate::common::consensus::{ConsensusModel, Magsac};
use crate::common::svd_basis::SvdBasis;
use crate::na::{AbstractRotation, Isometry, Point, SVector};
use serde::{Deserialize, Serialize};
/// A parameterized line in D-dimensional space: `P(t) = origin + t * direction`.
///
/// The direction is not required to be normalized; use [`new_normalize`](Line::new_normalize)
/// for a unit-speed parameterization where `t` equals arc length from the origin.
///
/// `Line<D>` is the base for `Line2` and `Line3`, which are two of `engeom`'s geometric primitives.
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub struct Line<const D: usize> {
pub origin: Point<f64, D>,
pub direction: SVector<f64, D>,
}
impl<const D: usize> Line<D> {
/// Create a line from an origin point and a direction vector (stored as-is, not normalized).
pub fn new(origin: Point<f64, D>, direction: SVector<f64, D>) -> Self {
Self { origin, direction }
}
/// Create a line from an origin point and a direction vector, normalizing the direction so
/// that the parameter `t` equals arc length from the origin.
pub fn new_normalize(origin: Point<f64, D>, direction: SVector<f64, D>) -> Self {
Self::new(origin, direction.normalize())
}
/// Create a line through two points. The direction is `p2 - p1` (not normalized).
pub fn from_points(p1: &impl PCoords<D>, p2: &impl PCoords<D>) -> Self {
let origin = Point::from(p1.coords());
Self::new(origin, p2.coords() - p1.coords())
}
/// Fit a line to a set of points using singular value decomposition, resulting in a
/// least-squares fitting. The resulting parameterized line will have its t=0 sitting at the
/// center of the SVD result.
///
/// # Arguments
///
/// * `points`: a slice of coordinates to fit the line to
/// * `weights`: if `Some`, this must be a slice of floating points the same length as `points`,
/// with the weight value to multiply each point residual by.
///
/// returns: Result<Line<{ D }>, Box<dyn Error, Global>>
///
/// # Examples
///
/// ```
///
/// ```
pub fn from_fit(points: &[impl PCoords<D>], weights: Option<&[f64]>) -> Result<Self> {
let basis = SvdBasis::from_points(points, weights)
.ok_or("Failed to fit line with singular value decomposition")?;
Ok(Line::new(basis.center, basis.largest().into_inner()))
}
/// Fit a line to a set of points using MAGSAC++ robust consensus estimation.
///
/// Unlike an ordinary least-squares fit ([`Line::from_fit`]), this rejects gross outliers by
/// taking an upper bound on the inlier noise (`sigma_max`) rather than a hard inlier/outlier
/// threshold, and refines each candidate with noise-marginalized iteratively reweighted least
/// squares. It is substantially less sensitive to `sigma_max` than RANSAC is to its threshold,
/// as long as `sigma_max` is not chosen smaller than the actual noise.
///
/// The resulting line has its origin at the centroid of the inlier set and a unit-length
/// direction vector.
///
/// # Arguments
///
/// * `points`: the points to fit the line to
/// * `sigma_max`: the upper bound on the expected inlier noise, in the same units as the points
/// * `options`: an optional [`Magsac`] configuration to override the iteration count, refinement
/// steps, confidence, or RNG seed. Its `sigma_max` field is overridden by the `sigma_max`
/// argument.
///
/// returns: Result<Line<{ D }>, Box<dyn Error, Global>>
pub fn from_consensus(
points: &[Point<f64, D>],
sigma_max: f64,
options: Option<Magsac>,
) -> Result<Self> {
let mut magsac = options.unwrap_or_else(|| Magsac::new(sigma_max));
magsac.sigma_max = sigma_max;
let fit = magsac.fit::<D, Line<D>>(points)?;
Ok(fit.model)
}
/// Returns a new line with the same origin, but with the direction inverted.
pub fn reversed(&self) -> Self {
Self::new(self.origin, -self.direction)
}
/// Returns a new line with the same origin but a normalized direction, so that `t` equals arc
/// length from the origin.
pub fn normalized(&self) -> Self {
Self::new(self.origin, self.direction.normalize())
}
/// Returns the point at parameter `t`: `P(t) = origin + t * direction`.
pub fn at(&self, t: f64) -> Point<f64, D> {
self.origin + self.direction * t
}
/// Returns a new line with the origin shifted by a given amount along the direction of the
/// line. The direction of the new line is the same as the original line. The original is left
/// unchanged.
///
/// If the direction is not of unit length, keep in mind this shift will be proportional to
/// the length of the direction vector.
pub fn shifted_origin(&self, delta_t: f64) -> Self {
Self::new(self.origin + self.direction * delta_t, self.direction)
}
/// Returns the parameter `t` such that `P(t)` is the closest point on the line to `point`.
/// Equivalent to the scalar projection of `(point - origin)` onto `direction`, divided by
/// `|direction|²`.
pub fn scalar_project(&self, point: &impl PCoords<D>) -> f64 {
(point.coords() - self.origin.coords).dot(&self.direction) / self.direction.norm_squared()
}
/// Returns the closest point on the line to `point`.
pub fn closest_point(&self, point: &impl PCoords<D>) -> Point<f64, D> {
self.at(self.scalar_project(point))
}
/// Returns the perpendicular distance from `point` to the line.
pub fn distance_to(&self, point: &impl PCoords<D>) -> f64 {
let pt = Point::from(point.coords());
(pt - self.closest_point(&pt)).norm()
}
/// Return a new line whose direction is spherically interpolated between this instance and
/// `other`, and whose origin is _linearly interpolated_ between this instance and `other`.
///
/// A value of `t=0` will return this line, and a value of `t=1` will return `other`.
///
/// # Arguments
///
/// * `other`: the other line to interpolate to
/// * `t`: the interpolation parameter, does _not_ need to be bounded between [0, 1]
///
/// returns: Line<{ D }>
///
/// # Examples
///
/// ```
///
/// ```
pub fn slerp(&self, other: &Line<D>, t: f64) -> Self {
let new_direction = self.direction.slerp(&other.direction, t);
let shift = other.origin - self.origin;
Self::new(self.origin + shift * t, new_direction)
}
/// Returns a new line with both origin and direction transformed by the given isometry.
pub fn transformed_by<R>(&self, iso: &Isometry<f64, R, D>) -> Self
where
R: AbstractRotation<f64, D>,
{
Self::new(
iso * self.origin,
iso.rotation.transform_vector(&self.direction),
)
}
}
impl<const D: usize> ConsensusModel<D> for Line<D> {
type Point = Point<f64, D>;
const SAMPLE_SIZE: usize = 2;
fn from_sample(sample: &[Point<f64, D>]) -> Option<Self> {
let line = Line::from_points(&sample[0], &sample[1]);
// Reject degenerate (coincident) samples, whose direction vector is effectively zero.
(line.direction.norm() > 1e-12).then_some(line)
}
fn residual(&self, point: &Point<f64, D>) -> f64 {
self.distance_to(point)
}
fn refine_weighted(points: &[Point<f64, D>], weights: &[f64], _initial: &Self) -> Option<Self> {
// The MAGSAC++ refinement step is a single weighted least-squares fit, which for a line is
// exactly the weighted SVD fit provided by `from_fit`.
Line::from_fit(points, Some(weights)).ok()
}
}
impl<const D: usize> PCoords<D> for Line<D> {
fn coords(&self) -> SVector<f64, D> {
self.origin.coords
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::random_geometry::{RandomGeometry, RandomGeometry2};
use crate::{Point2, Point3, Vector2, Vector3};
use approx::assert_relative_eq;
use rand::RngExt;
#[test]
fn at_endpoints() {
let line = Line::<2>::new(Point2::new(1.0, 2.0), Vector2::new(0.0, 1.0));
assert_relative_eq!(line.at(0.0), Point2::new(1.0, 2.0), epsilon = 1e-12);
assert_relative_eq!(line.at(1.0), Point2::new(1.0, 3.0), epsilon = 1e-12);
}
#[test]
fn new_normalize_gives_unit_direction() {
let line = Line::<3>::new_normalize(Point3::origin(), Vector3::new(3.0, 0.0, 0.0));
assert_relative_eq!(line.direction.norm(), 1.0, epsilon = 1e-12);
}
#[test]
fn from_points_direction_is_difference() {
let line = Line::<3>::from_points(&Point3::new(1.0, 0.0, 0.0), &Point3::new(4.0, 0.0, 0.0));
assert_relative_eq!(line.direction, Vector3::new(3.0, 0.0, 0.0), epsilon = 1e-12);
}
#[test]
fn closest_point_perpendicular_drop() {
let line = Line::<3>::new(Point3::origin(), Vector3::x());
assert_relative_eq!(
line.closest_point(&Point3::new(4.0, 3.0, 0.0)),
Point3::new(4.0, 0.0, 0.0),
epsilon = 1e-12
);
}
#[test]
fn distance_to_known_value() {
let line = Line::<3>::new(Point3::origin(), Vector3::x());
assert_relative_eq!(
line.distance_to(&Point3::new(0.0, 3.0, 4.0)),
5.0,
epsilon = 1e-12
);
}
#[test]
fn shifted_origin_moves_along_direction() {
let line = Line::<2>::new(Point2::new(0.0, 0.0), Vector2::new(2.0, 0.0));
let shifted = line.shifted_origin(1.5);
assert_relative_eq!(shifted.origin, Point2::new(3.0, 0.0), epsilon = 1e-12);
let shifted_again = shifted.shifted_origin(-1.0);
assert_relative_eq!(shifted_again.origin, Point2::new(1.0, 0.0), epsilon = 1e-12);
}
#[test]
fn from_fit_recovers_axis_aligned_line() {
let points = vec![
Point3::new(0.0, 1.0, 2.0),
Point3::new(1.0, 1.0, 2.0),
Point3::new(2.0, 1.0, 2.0),
Point3::new(3.0, 1.0, 2.0),
];
let line = Line::<3>::from_fit(&points, None).unwrap();
assert_relative_eq!(line.origin, Point3::new(1.5, 1.0, 2.0), epsilon = 1e-10);
let dir = line.direction.normalize();
assert_relative_eq!(dir.x.abs(), 1.0, epsilon = 1e-10);
assert_relative_eq!(dir.y, 0.0, epsilon = 1e-10);
assert_relative_eq!(dir.z, 0.0, epsilon = 1e-10);
}
#[test]
fn from_fit_origin_is_at_centroid() {
let points = vec![
Point2::new(0.0, 0.0),
Point2::new(2.0, 0.0),
Point2::new(4.0, 0.0),
Point2::new(6.0, 0.0),
];
let line = Line::<2>::from_fit(&points, None).unwrap();
assert_relative_eq!(line.origin, Point2::new(3.0, 0.0), epsilon = 1e-10);
}
#[test]
fn from_fit_minimizes_perpendicular_residuals() {
// A noisy but clearly linear point set: every point should end up close to the fitted
// line, much closer than to a line built from just two of the points.
let points = vec![
Point2::new(0.0, 0.05),
Point2::new(1.0, 0.95),
Point2::new(2.0, 2.05),
Point2::new(3.0, 2.95),
Point2::new(4.0, 4.05),
];
let line = Line::<2>::from_fit(&points, None).unwrap();
for p in &points {
assert!(
line.distance_to(p) < 0.1,
"point {p:?} too far from fitted line"
);
}
}
#[test]
fn from_fit_uniform_weights_match_unweighted() {
let points = vec![
Point3::new(0.0, 0.0, 5.0),
Point3::new(1.0, 0.3, 5.2),
Point3::new(0.2, 1.0, 4.8),
Point3::new(1.0, 1.0, 5.1),
];
let unweighted = Line::<3>::from_fit(&points, None).unwrap();
let weights = vec![1.0; points.len()];
let weighted = Line::<3>::from_fit(&points, Some(&weights)).unwrap();
assert_relative_eq!(weighted.origin, unweighted.origin, epsilon = 1e-10);
assert_relative_eq!(weighted.direction, unweighted.direction, epsilon = 1e-10);
}
#[test]
fn from_fit_heavily_weighted_points_pull_fit_toward_them() {
// Two clusters of points on either side of the origin; heavily weighting one cluster
// should pull the fitted line's origin towards it.
let points = vec![
Point2::new(0.0, 0.0),
Point2::new(1.0, 0.0),
Point2::new(0.0, 10.0),
Point2::new(1.0, 10.0),
];
let weights = vec![1.0, 1.0, 100.0, 100.0];
let line = Line::<2>::from_fit(&points, Some(&weights)).unwrap();
assert!(line.origin.y > 5.0);
}
#[test]
fn from_fit_empty_points_is_error() {
let points: Vec<Point3> = vec![];
assert!(Line::<3>::from_fit(&points, None).is_err());
}
#[test]
fn from_fit_single_point_is_error() {
let points = vec![Point3::new(1.0, 2.0, 3.0)];
assert!(Line::<3>::from_fit(&points, None).is_err());
}
#[test]
fn from_fit_coincident_points_is_error() {
let points = vec![Point3::new(1.0, 1.0, 1.0), Point3::new(1.0, 1.0, 1.0)];
assert!(Line::<3>::from_fit(&points, None).is_err());
}
fn line_noise<const D: usize>(line: &Line<D>, n: usize, sigma: f64) -> Vec<Point<f64, D>> {
let mut rand = RandomGeometry::<D>::new();
(0..n)
.map(|_| line.at(rand.f64_sym(10.0)) + rand.gaussian_vector(sigma))
.collect()
}
#[test]
fn stress_from_fit_recovers_known_line() {
let mut rng = rand::rng();
for _ in 0..200 {
let origin = Point3::new(
rng.random_range(-10.0..10.0),
rng.random_range(-10.0..10.0),
rng.random_range(-10.0..10.0),
);
let direction = Vector3::new(
rng.random_range(-1.0..1.0),
rng.random_range(-1.0..1.0),
rng.random_range(-1.0..1.0),
)
.normalize();
let true_line = Line::<3>::new(origin, direction);
let points: Vec<Point3> = (0..20).map(|i| true_line.at(i as f64 - 10.0)).collect();
let fit = Line::<3>::from_fit(&points, None).unwrap();
// The fitted line should pass through (or very near) every input point, since they
// are exactly collinear.
for p in &points {
assert_relative_eq!(fit.distance_to(p), 0.0, epsilon = 1e-8);
}
// The fitted direction should be parallel (or anti-parallel) to the true direction.
let fit_dir = fit.direction.normalize();
assert_relative_eq!(fit_dir.dot(&direction).abs(), 1.0, epsilon = 1e-8);
}
}
// from_consensus tests ────────────────────────────────────────────────────────
#[test]
fn from_consensus_rejects_outliers() {
let mut rand = RandomGeometry2::new();
// Inliers lie along the line y = 0.5 with a small deterministic perturbation.
let true_line = Line::<2>::new(Point2::new(0.0, 0.5), Vector2::new(1.0, 0.0));
let decoy_line = Line::<2>::new(Point2::new(0.0, 0.0), Vector2::new(1.0, 1.0));
let inliers = line_noise(&true_line, 150, 0.01);
let mut points = [inliers.clone(), line_noise(&decoy_line, 50, 0.05)].concat();
// A dense cluster of gross outliers well off the line.
let center = Point2::new(-2.0, 3.0);
for _i in 0..50 {
points.push(center + rand.gaussian_vector(1.0));
}
let magsac = Magsac {
sigma_max: 0.02,
max_iterations: Some(400),
refinement_steps: 4,
confidence: 0.99,
seed: Some(42),
};
let fit = magsac
.fit_filtered::<2, Line<2>, _>(&points, |_| true)
.unwrap();
// Every inlier should lie very close to the recovered line
for i in inliers.iter() {
assert!(fit.model.distance_to(i) < 0.01 * 6.0);
}
// The direction should be parallel (or anti-parallel) to the true horizontal line.
let dir = fit.model.direction.normalize();
assert_relative_eq!(
dir.dot(&true_line.direction.normalize()).abs(),
1.0,
epsilon = 1e-2
);
}
#[test]
fn from_consensus_convenience_recovers_line_3d() {
// A clean set of collinear 3D points; the convenience method should recover the line.
let true_line =
Line::<3>::new_normalize(Point3::new(1.0, -2.0, 3.0), Vector3::new(0.5, 1.0, -0.25));
let points: Vec<Point3> = (0..60)
.map(|i| true_line.at(i as f64 * 0.1 - 3.0))
.collect();
let line = Line::<3>::from_consensus(&points, 0.01, None).unwrap();
for p in &points {
assert!(line.distance_to(p) < 1e-3);
}
let dir = line.direction.normalize();
assert_relative_eq!(
dir.dot(&true_line.direction.normalize()).abs(),
1.0,
epsilon = 1e-6
);
}
}