Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement From<Point<T>> for Vector<T> #1460

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/geometry/point_conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -70,14 +70,27 @@ where
}
}

impl<T: Scalar + Zero + One, D: DimName> From<OPoint<T, D>> for OVector<T, DimNameSum<D, U1>>
impl<T: Scalar + Zero + One> From<Point2<T>> for Vector3<T> {
#[inline]
fn from(p: Point2<T>) -> Self {
p.to_homogeneous()
}
}

impl<T: Scalar + Zero + One> From<Point3<T>> for Vector4<T> {
#[inline]
fn from(p: Point3<T>) -> Self {
p.to_homogeneous()
}
}

impl<T: Scalar, D: DimName> From<OPoint<T, D>> for OVector<T, D>
where
D: DimNameAdd<U1>,
DefaultAllocator: Allocator<DimNameSum<D, U1>> + Allocator<D>,
DefaultAllocator: Allocator<D>,
{
#[inline]
fn from(t: OPoint<T, D>) -> Self {
t.to_homogeneous()
fn from(p: OPoint<T, D>) -> Self {
p.coords
}
}

Expand Down
38 changes: 37 additions & 1 deletion tests/geometry/point.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use na::{Point3, Vector3, Vector4};
use na::{Point2, Point3, Point4, Vector2, Vector3, Vector4};
use num::Zero;

#[test]
Expand Down Expand Up @@ -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<f64> = 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<f64> = 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<f64> = 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<f64> = 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<f64> = p.into();
assert_eq!(v1, expected);
}
Loading