colmap-rs/src/scene/point2d.rs

73 lines
1.9 KiB
Rust

//! The [`Point2D`] data type: an observed 2D feature in an image.
//!
//! Mirrors COLMAP's `colmap/scene/point2d.h`. A 2D point stores its pixel
//! location and, when it has been triangulated, the id of the 3D point it
//! observes (otherwise [`INVALID_POINT3D_ID`]).
use crate::math::Vec2;
use crate::types::{Point3DId, INVALID_POINT3D_ID};
/// A 2D observation in an image, optionally linked to a triangulated 3D point.
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Point2D {
/// The pixel coordinates of the observation.
pub xy: Vec2,
/// The id of the observed 3D point, or [`INVALID_POINT3D_ID`] if none.
pub point3d_id: Point3DId,
}
impl Default for Point2D {
/// A point at the origin with no associated 3D point.
#[inline]
fn default() -> Self {
Self {
xy: Vec2::zeros(),
point3d_id: INVALID_POINT3D_ID,
}
}
}
impl Point2D {
/// Creates a 2D point at `xy` that is not yet linked to a 3D point.
#[inline]
pub fn new(xy: Vec2) -> Self {
Self {
xy,
point3d_id: INVALID_POINT3D_ID,
}
}
/// Returns `true` if this observation is linked to a valid 3D point.
#[inline]
pub fn has_point3d(&self) -> bool {
self.point3d_id != INVALID_POINT3D_ID
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn new_has_no_point3d() {
let p = Point2D::new(Vec2::new(1.0, 2.0));
assert!(!p.has_point3d());
assert_eq!(p.xy, Vec2::new(1.0, 2.0));
}
#[test]
fn default_is_unlinked_origin() {
let p = Point2D::default();
assert_eq!(p.xy, Vec2::zeros());
assert!(!p.has_point3d());
}
#[test]
fn linking_sets_flag() {
let mut p = Point2D::new(Vec2::new(0.0, 0.0));
p.point3d_id = 42;
assert!(p.has_point3d());
}
}