Skip to content

Commit 48248ae

Browse files
authored
Merge pull request dimforge#845 from dimforge/glam
Add conversions from/to glam types.
2 parents a798d1c + 35ffdc9 commit 48248ae

34 files changed

+816
-256
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ This project adheres to [Semantic Versioning](https://semver.org/).
66

77
## [0.25.2] - WIP
88
### Added
9+
- A `convert-glam` cargo feature to enable implementations of `From` traits to convert
10+
between `glam` types and `nalgebra` types.
11+
- A `convert-glam-unchecked` cargo feature to enable some extra `glam`/`nalgebra` conversions that may
12+
lead to unexpected results if used improperly. For example, this enables the conversion from a
13+
`glam::Mat4` to a `na::Isometry3`. This conversion will be cheap (without any check) but willlead to
14+
unexpected results if the glam matrix contains non-isometric components (like scaling for example).
915
- A `cast` method has been added to most types. This can be used to change the
1016
type of the components of a given entity. Example: `vector.cast::<f32>()`.
1117

Cargo.toml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,33 @@ path = "src/lib.rs"
2424
[features]
2525
default = [ "std" ]
2626
std = [ "matrixmultiply", "simba/std" ]
27-
rand-no-std = [ "rand-package" ]
28-
rand = [ "rand-no-std", "rand-package/std", "rand-package/std_rng", "rand_distr" ]
29-
arbitrary = [ "quickcheck" ]
30-
serde-serialize = [ "serde", "num-complex/serde" ]
31-
abomonation-serialize = [ "abomonation" ]
3227
sparse = [ ]
3328
debug = [ "approx/num-complex", "rand" ]
3429
alloc = [ ]
3530
io = [ "pest", "pest_derive" ]
3631
compare = [ "matrixcompare-core" ]
3732
libm = [ "simba/libm" ]
3833
libm-force = [ "simba/libm_force" ]
39-
proptest-support = [ "proptest" ]
4034
no_unsound_assume_init = [ ]
4135

42-
# This feature is only used for tests, and enables tests that require more time to run
43-
slow-tests = []
36+
# Conversion
37+
convert-mint = [ "mint" ]
38+
convert-glam = [ "glam" ]
39+
convert-glam-unchecked = [ "convert-glam" ] # Unable edgy conversions like Mat4 -> Isometry3
40+
convert-bytemuck = [ "bytemuck" ]
41+
42+
# Serialization
43+
serde-serialize = [ "serde", "num-complex/serde" ]
44+
abomonation-serialize = [ "abomonation" ]
45+
46+
# Randomness
47+
rand-no-std = [ "rand-package" ]
48+
rand = [ "rand-no-std", "rand-package/std", "rand-package/std_rng", "rand_distr" ]
49+
50+
# Tests
51+
arbitrary = [ "quickcheck" ]
52+
proptest-support = [ "proptest" ]
53+
slow-tests = []
4454

4555
[dependencies]
4656
typenum = "1.12"
@@ -57,6 +67,7 @@ matrixmultiply = { version = "0.3", optional = true }
5767
serde = { version = "1.0", default-features = false, features = [ "derive" ], optional = true }
5868
abomonation = { version = "0.7", optional = true }
5969
mint = { version = "0.5", optional = true }
70+
glam = { version = "0.13", optional = true }
6071
quickcheck = { version = "1", optional = true }
6172
pest = { version = "2", optional = true }
6273
pest_derive = { version = "2", optional = true }

src/base/conversion.rs

Lines changed: 0 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#[cfg(all(feature = "alloc", not(feature = "std")))]
22
use alloc::vec::Vec;
3-
#[cfg(feature = "mint")]
4-
use mint;
53
use simba::scalar::{SubsetOf, SupersetOf};
64
use std::convert::{AsMut, AsRef, From, Into};
75
use std::mem;
@@ -235,119 +233,6 @@ impl_from_into_asref_2D!(
235233
(U6, U2) => (6, 2); (U6, U3) => (6, 3); (U6, U4) => (6, 4); (U6, U5) => (6, 5); (U6, U6) => (6, 6);
236234
);
237235

238-
#[cfg(feature = "mint")]
239-
macro_rules! impl_from_into_mint_1D(
240-
($($NRows: ident => $VT:ident [$SZ: expr]);* $(;)*) => {$(
241-
impl<N> From<mint::$VT<N>> for MatrixMN<N, $NRows, U1>
242-
where N: Scalar,
243-
DefaultAllocator: Allocator<N, $NRows, U1> {
244-
#[inline]
245-
fn from(v: mint::$VT<N>) -> Self {
246-
unsafe {
247-
let mut res = Self::new_uninitialized();
248-
ptr::copy_nonoverlapping(&v.x, (*res.as_mut_ptr()).data.ptr_mut(), $SZ);
249-
250-
res.assume_init()
251-
}
252-
}
253-
}
254-
255-
impl<N, S> Into<mint::$VT<N>> for Matrix<N, $NRows, U1, S>
256-
where N: Scalar,
257-
S: ContiguousStorage<N, $NRows, U1> {
258-
#[inline]
259-
fn into(self) -> mint::$VT<N> {
260-
unsafe {
261-
let mut res: mint::$VT<N> = mem::MaybeUninit::uninit().assume_init();
262-
ptr::copy_nonoverlapping(self.data.ptr(), &mut res.x, $SZ);
263-
res
264-
}
265-
}
266-
}
267-
268-
impl<N, S> AsRef<mint::$VT<N>> for Matrix<N, $NRows, U1, S>
269-
where N: Scalar,
270-
S: ContiguousStorage<N, $NRows, U1> {
271-
#[inline]
272-
fn as_ref(&self) -> &mint::$VT<N> {
273-
unsafe {
274-
mem::transmute(self.data.ptr())
275-
}
276-
}
277-
}
278-
279-
impl<N, S> AsMut<mint::$VT<N>> for Matrix<N, $NRows, U1, S>
280-
where N: Scalar,
281-
S: ContiguousStorageMut<N, $NRows, U1> {
282-
#[inline]
283-
fn as_mut(&mut self) -> &mut mint::$VT<N> {
284-
unsafe {
285-
mem::transmute(self.data.ptr_mut())
286-
}
287-
}
288-
}
289-
)*}
290-
);
291-
292-
// Implement for vectors of dimension 2 .. 4.
293-
#[cfg(feature = "mint")]
294-
impl_from_into_mint_1D!(
295-
U2 => Vector2[2];
296-
U3 => Vector3[3];
297-
U4 => Vector4[4];
298-
);
299-
300-
#[cfg(feature = "mint")]
301-
macro_rules! impl_from_into_mint_2D(
302-
($(($NRows: ty, $NCols: ty) => $MV:ident{ $($component:ident),* }[$SZRows: expr]);* $(;)*) => {$(
303-
impl<N> From<mint::$MV<N>> for MatrixMN<N, $NRows, $NCols>
304-
where N: Scalar,
305-
DefaultAllocator: Allocator<N, $NRows, $NCols> {
306-
#[inline]
307-
fn from(m: mint::$MV<N>) -> Self {
308-
unsafe {
309-
let mut res = Self::new_uninitialized();
310-
let mut ptr = (*res.as_mut_ptr()).data.ptr_mut();
311-
$(
312-
ptr::copy_nonoverlapping(&m.$component.x, ptr, $SZRows);
313-
ptr = ptr.offset($SZRows);
314-
)*
315-
let _ = ptr;
316-
res.assume_init()
317-
}
318-
}
319-
}
320-
321-
impl<N> Into<mint::$MV<N>> for MatrixMN<N, $NRows, $NCols>
322-
where N: Scalar,
323-
DefaultAllocator: Allocator<N, $NRows, $NCols> {
324-
#[inline]
325-
fn into(self) -> mint::$MV<N> {
326-
unsafe {
327-
let mut res: mint::$MV<N> = mem::MaybeUninit::uninit().assume_init();
328-
let mut ptr = self.data.ptr();
329-
$(
330-
ptr::copy_nonoverlapping(ptr, &mut res.$component.x, $SZRows);
331-
ptr = ptr.offset($SZRows);
332-
)*
333-
let _ = ptr;
334-
res
335-
}
336-
}
337-
}
338-
)*}
339-
);
340-
341-
// Implement for matrices with shape 2x2 .. 4x4.
342-
#[cfg(feature = "mint")]
343-
impl_from_into_mint_2D!(
344-
(U2, U2) => ColumnMatrix2{x, y}[2];
345-
(U2, U3) => ColumnMatrix2x3{x, y, z}[2];
346-
(U3, U3) => ColumnMatrix3{x, y, z}[3];
347-
(U3, U4) => ColumnMatrix3x4{x, y, z, w}[3];
348-
(U4, U4) => ColumnMatrix4{x, y, z, w}[4];
349-
);
350-
351236
impl<'a, N, R, C, RStride, CStride> From<MatrixSlice<'a, N, R, C, RStride, CStride>>
352237
for Matrix<N, R, C, ArrayStorage<N, R, C>>
353238
where

src/base/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ mod conversion;
2222
mod edition;
2323
pub mod indexing;
2424
mod matrix;
25-
#[cfg(feature = "alga")]
26-
mod matrix_alga;
2725
mod matrix_simba;
2826
mod matrix_slice;
2927
mod norm;

src/geometry/mod.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ mod op_macros;
66
mod abstract_rotation;
77

88
mod point;
9-
#[cfg(feature = "alga")]
10-
mod point_alga;
119
mod point_alias;
1210
mod point_construction;
1311
mod point_conversion;
@@ -16,8 +14,6 @@ mod point_ops;
1614
mod point_simba;
1715

1816
mod rotation;
19-
#[cfg(feature = "alga")]
20-
mod rotation_alga;
2117
mod rotation_alias;
2218
mod rotation_construction;
2319
mod rotation_conversion;
@@ -27,32 +23,24 @@ mod rotation_simba; // TODO: implement Rotation methods.
2723
mod rotation_specialization;
2824

2925
mod quaternion;
30-
#[cfg(feature = "alga")]
31-
mod quaternion_alga;
3226
mod quaternion_construction;
3327
mod quaternion_conversion;
3428
mod quaternion_coordinates;
3529
mod quaternion_ops;
3630
mod quaternion_simba;
3731

3832
mod dual_quaternion;
39-
#[cfg(feature = "alga")]
40-
mod dual_quaternion_alga;
4133
mod dual_quaternion_construction;
4234
mod dual_quaternion_conversion;
4335
mod dual_quaternion_ops;
4436

4537
mod unit_complex;
46-
#[cfg(feature = "alga")]
47-
mod unit_complex_alga;
4838
mod unit_complex_construction;
4939
mod unit_complex_conversion;
5040
mod unit_complex_ops;
5141
mod unit_complex_simba;
5242

5343
mod translation;
54-
#[cfg(feature = "alga")]
55-
mod translation_alga;
5644
mod translation_alias;
5745
mod translation_construction;
5846
mod translation_conversion;
@@ -61,8 +49,6 @@ mod translation_ops;
6149
mod translation_simba;
6250

6351
mod isometry;
64-
#[cfg(feature = "alga")]
65-
mod isometry_alga;
6652
mod isometry_alias;
6753
mod isometry_construction;
6854
mod isometry_conversion;
@@ -71,8 +57,6 @@ mod isometry_ops;
7157
mod isometry_simba;
7258

7359
mod similarity;
74-
#[cfg(feature = "alga")]
75-
mod similarity_alga;
7660
mod similarity_alias;
7761
mod similarity_construction;
7862
mod similarity_conversion;
@@ -82,8 +66,6 @@ mod similarity_simba;
8266
mod swizzle;
8367

8468
mod transform;
85-
#[cfg(feature = "alga")]
86-
mod transform_alga;
8769
mod transform_alias;
8870
mod transform_construction;
8971
mod transform_conversion;

src/geometry/point_conversion.rs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@ use crate::base::allocator::Allocator;
66
use crate::base::dimension::{DimName, DimNameAdd, DimNameSum, U1};
77
use crate::base::{DefaultAllocator, Matrix, Scalar, VectorN};
88

9-
#[cfg(feature = "mint")]
10-
use crate::base::dimension::{U2, U3};
11-
#[cfg(feature = "mint")]
12-
use crate::base::storage::{Storage, StorageMut};
139
use crate::geometry::Point;
14-
#[cfg(feature = "mint")]
15-
use mint;
16-
#[cfg(feature = "mint")]
17-
use std::convert::{AsMut, AsRef, From, Into};
10+
1811
/*
1912
* This file provides the following conversions:
2013
* =============================================
2114
*
2215
* Point -> Point
2316
* Point -> Vector (homogeneous)
24-
*
25-
* mint::Point <-> Point
2617
*/
2718

2819
impl<N1, N2, D> SubsetOf<Point<N2, D>> for Point<N1, D>
@@ -80,57 +71,6 @@ where
8071
}
8172
}
8273

83-
#[cfg(feature = "mint")]
84-
macro_rules! impl_from_into_mint_1D(
85-
($($NRows: ident => $PT:ident, $VT:ident [$SZ: expr]);* $(;)*) => {$(
86-
impl<N> From<mint::$PT<N>> for Point<N, $NRows>
87-
where N: Scalar {
88-
#[inline]
89-
fn from(p: mint::$PT<N>) -> Self {
90-
Self {
91-
coords: VectorN::from(mint::$VT::from(p)),
92-
}
93-
}
94-
}
95-
96-
impl<N> Into<mint::$PT<N>> for Point<N, $NRows>
97-
where N: Scalar {
98-
#[inline]
99-
fn into(self) -> mint::$PT<N> {
100-
let mint_vec: mint::$VT<N> = self.coords.into();
101-
mint::$PT::from(mint_vec)
102-
}
103-
}
104-
105-
impl<N> AsRef<mint::$PT<N>> for Point<N, $NRows>
106-
where N: Scalar {
107-
#[inline]
108-
fn as_ref(&self) -> &mint::$PT<N> {
109-
unsafe {
110-
&*(self.coords.data.ptr() as *const mint::$PT<N>)
111-
}
112-
}
113-
}
114-
115-
impl<N> AsMut<mint::$PT<N>> for Point<N, $NRows>
116-
where N: Scalar {
117-
#[inline]
118-
fn as_mut(&mut self) -> &mut mint::$PT<N> {
119-
unsafe {
120-
&mut *(self.coords.data.ptr_mut() as *mut mint::$PT<N>)
121-
}
122-
}
123-
}
124-
)*}
125-
);
126-
127-
// Implement for points of dimension 2, 3.
128-
#[cfg(feature = "mint")]
129-
impl_from_into_mint_1D!(
130-
U2 => Point2, Vector2[2];
131-
U3 => Point3, Vector3[3];
132-
);
133-
13474
impl<N: Scalar + Zero + One, D: DimName> From<Point<N, D>> for VectorN<N, DimNameSum<D, U1>>
13575
where
13676
D: DimNameAdd<U1>,

0 commit comments

Comments
 (0)