diff --git a/src/geometry/point_conversion.rs b/src/geometry/point_conversion.rs index ac723c6bf..f7c39fec9 100644 --- a/src/geometry/point_conversion.rs +++ b/src/geometry/point_conversion.rs @@ -6,8 +6,8 @@ use crate::base::allocator::Allocator; use crate::base::dimension::{DimNameAdd, DimNameSum, U1}; use crate::base::{Const, DefaultAllocator, Matrix, OVector, Scalar}; -use crate::geometry::Point; -use crate::{DimName, OPoint}; +use crate::geometry::{Point, Point2, Point3}; +use crate::{DimName, OPoint, Vector3, Vector4}; /* * This file provides the following conversions: @@ -70,14 +70,27 @@ where } } -impl From> for OVector> +impl From> for Vector3 { + #[inline] + fn from(p: Point2) -> Self { + p.to_homogeneous() + } +} + +impl From> for Vector4 { + #[inline] + fn from(p: Point3) -> Self { + p.to_homogeneous() + } +} + +impl From> for OVector where - D: DimNameAdd, - DefaultAllocator: Allocator> + Allocator, + DefaultAllocator: Allocator, { #[inline] - fn from(t: OPoint) -> Self { - t.to_homogeneous() + fn from(p: OPoint) -> Self { + p.coords } } diff --git a/tests/geometry/point.rs b/tests/geometry/point.rs index 42adf118e..29910ac68 100644 --- a/tests/geometry/point.rs +++ b/tests/geometry/point.rs @@ -1,4 +1,4 @@ -use na::{Point3, Vector3, Vector4}; +use na::{Point2, Point3, Point4, Vector2, Vector3, Vector4}; use num::Zero; #[test] @@ -100,3 +100,39 @@ fn display_fmt_respects_modifiers() { assert_eq!(&format!("{p:.1}"), "{1.2, 3.5, 5.7}"); assert_eq!(&format!("{p:.0}"), "{1, 3, 6}"); } + +#[test] +fn homogeneous_conversions() { + // 2D -> 3D homogeneous conversion + let p = Point2::new(1.0, 2.0); + let expected = Vector3::new(1.0, 2.0, 1.0); + let v1: Vector3 = p.into(); + assert_eq!(v1, expected); + + // 3D -> 4D homogeneous conversion + let p = Point3::new(1.0, 2.0, 3.0); + let expected = Vector4::new(1.0, 2.0, 3.0, 1.0); + let v1: Vector4 = p.into(); + assert_eq!(v1, expected); +} + +#[test] +fn dimension_preserving_conversions() { + // 2D point -> 2D vector + let p = Point2::new(1.0, 2.0); + let expected = Vector2::new(1.0, 2.0); + let v1: Vector2 = p.into(); + assert_eq!(v1, expected); + + // 3D point -> 3D vector + let p = Point3::new(1.0, 2.0, 3.0); + let expected = Vector3::new(1.0, 2.0, 3.0); + let v1: Vector3 = p.into(); + assert_eq!(v1, expected); + + // 4D point -> 4D vector + let p = Point4::new(1.0, 2.0, 3.0, 4.0); + let expected = Vector4::new(1.0, 2.0, 3.0, 4.0); + let v1: Vector4 = p.into(); + assert_eq!(v1, expected); +}