Skip to content

Commit

Permalink
Add math type conversions to mint types (#4753)
Browse files Browse the repository at this point in the history
### What

* Fixes #3292

Also fixes `Vec4D::w()` returning z instead!

Adds missing extensions to Mat4.

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using newly built examples:
[app.rerun.io](https://app.rerun.io/pr/4753/index.html)
* Using examples from latest `main` build:
[app.rerun.io](https://app.rerun.io/pr/4753/index.html?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[app.rerun.io](https://app.rerun.io/pr/4753/index.html?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG

- [PR Build Summary](https://build.rerun.io/pr/4753)
- [Docs
preview](https://rerun.io/preview/82e8f1caab378afe4fb99032e10eaf7d59b28c95/docs)
<!--DOCS-PREVIEW-->
- [Examples
preview](https://rerun.io/preview/82e8f1caab378afe4fb99032e10eaf7d59b28c95/examples)
<!--EXAMPLES-PREVIEW-->
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)
  • Loading branch information
Wumpf authored Jan 9, 2024
1 parent 5c0d0fd commit 2f27d91
Show file tree
Hide file tree
Showing 22 changed files with 582 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ memory-stats = "1.1"
mimalloc = "0.1.29"
mime = "0.3"
mime_guess2 = "2.0"
mint = "0.5.9"
natord = "1.0.9"
ndarray = "0.15"
ndarray-rand = "0.14"
Expand Down
4 changes: 3 additions & 1 deletion crates/re_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ features = ["all"]
default = []

## All features except `testing`.
all = ["ecolor", "glam", "image", "serde"]
all = ["ecolor", "glam", "image", "mint", "serde"]

## Enables the `datagen` module, which exposes a number of tools for generating random data for
## tests and benchmarks.
Expand Down Expand Up @@ -78,6 +78,7 @@ uuid = { workspace = true, features = ["serde", "v4", "js"] }
ecolor = { workspace = true, optional = true }
glam = { workspace = true, optional = true }
image = { workspace = true, optional = true, default-features = false }
mint = { workspace = true, optional = true }
rand = { workspace = true, optional = true, features = ["std", "std_rng"] }
serde = { workspace = true, optional = true, features = ["derive", "rc"] }
zune-core = { workspace = true, optional = true }
Expand All @@ -89,6 +90,7 @@ zune-jpeg = { workspace = true, optional = true }
# External
glam.workspace = true
itertools.workspace = true
mint.workspace = true
similar-asserts.workspace = true


Expand Down
11 changes: 11 additions & 0 deletions crates/re_types/src/components/half_sizes2d_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ impl From<HalfSizes2D> for glam::Vec3 {
Self::new(extent.x(), extent.y(), 0.0)
}
}

#[cfg(feature = "mint")]
impl From<HalfSizes2D> for mint::Vector2<f32> {
#[inline]
fn from(extent: HalfSizes2D) -> Self {
Self {
x: extent.x(),
y: extent.y(),
}
}
}
12 changes: 12 additions & 0 deletions crates/re_types/src/components/half_sizes3d_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,15 @@ impl From<HalfSizes3D> for glam::Vec3 {
Self::new(extent.x(), extent.y(), extent.z())
}
}

#[cfg(feature = "mint")]
impl From<HalfSizes3D> for mint::Vector3<f32> {
#[inline]
fn from(extent: HalfSizes3D) -> Self {
Self {
x: extent.x(),
y: extent.y(),
z: extent.z(),
}
}
}
1 change: 1 addition & 0 deletions crates/re_types/src/components/pinhole_projection_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl PinholeProjection {
}

#[test]
#[cfg(feature = "glam")]
fn test_pinhole() {
let fl = Vec2D::from([600.0, 600.0]);
let pp = glam::Vec2::from([300.0, 240.0]);
Expand Down
19 changes: 19 additions & 0 deletions crates/re_types/src/components/position2d_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,22 @@ impl crate::SizeBytes for Position2D {
v.heap_size_bytes()
}
}

#[cfg(feature = "mint")]
impl From<Position2D> for mint::Point2<f32> {
#[inline]
fn from(position: Position2D) -> Self {
Self {
x: position.x(),
y: position.y(),
}
}
}

#[cfg(feature = "mint")]
impl From<mint::Point2<f32>> for Position2D {
#[inline]
fn from(position: mint::Point2<f32>) -> Self {
Self(Vec2D([position.x, position.y]))
}
}
20 changes: 20 additions & 0 deletions crates/re_types/src/components/position3d_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ impl From<Position3D> for glam::Vec3 {
Self::new(pt.x(), pt.y(), pt.z())
}
}

#[cfg(feature = "mint")]
impl From<Position3D> for mint::Point3<f32> {
#[inline]
fn from(position: Position3D) -> Self {
Self {
x: position.x(),
y: position.y(),
z: position.z(),
}
}
}

#[cfg(feature = "mint")]
impl From<mint::Point3<f32>> for Position3D {
#[inline]
fn from(position: mint::Point3<f32>) -> Self {
Self(Vec3D([position.x, position.y, position.z]))
}
}
8 changes: 8 additions & 0 deletions crates/re_types/src/components/rotation3d_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ impl From<Rotation3D> for glam::Quat {
val.0.into()
}
}

#[cfg(feature = "mint")]
impl From<Rotation3D> for mint::Quaternion<f32> {
#[inline]
fn from(val: Rotation3D) -> Self {
val.0.into()
}
}
16 changes: 16 additions & 0 deletions crates/re_types/src/components/vector2d_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,19 @@ impl Vector2D {
pub const ZERO: Self = Self(crate::datatypes::Vec2D::ZERO);
pub const ONE: Self = Self(crate::datatypes::Vec2D::ONE);
}

#[cfg(feature = "glam")]
impl From<Vector2D> for glam::Vec2 {
#[inline]
fn from(v: Vector2D) -> Self {
Self::new(v.x(), v.y())
}
}

#[cfg(feature = "mint")]
impl From<Vector2D> for mint::Vector2<f32> {
#[inline]
fn from(v: Vector2D) -> Self {
Self { x: v.x(), y: v.y() }
}
}
20 changes: 20 additions & 0 deletions crates/re_types/src/components/vector3d_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,23 @@ impl Vector3D {
pub const ZERO: Self = Self(crate::datatypes::Vec3D::ZERO);
pub const ONE: Self = Self(crate::datatypes::Vec3D::ONE);
}

#[cfg(feature = "glam")]
impl From<Vector3D> for glam::Vec3 {
#[inline]
fn from(v: Vector3D) -> Self {
Self::new(v.x(), v.y(), v.z())
}
}

#[cfg(feature = "mint")]
impl From<Vector3D> for mint::Vector3<f32> {
#[inline]
fn from(v: Vector3D) -> Self {
Self {
x: v.x(),
y: v.y(),
z: v.z(),
}
}
}
16 changes: 16 additions & 0 deletions crates/re_types/src/datatypes/mat3x3_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,19 @@ impl From<glam::Mat3> for Mat3x3 {
Self::from(v.to_cols_array_2d())
}
}

#[cfg(feature = "mint")]
impl From<Mat3x3> for mint::ColumnMatrix3<f32> {
#[inline]
fn from(v: Mat3x3) -> Self {
v.0.into()
}
}

#[cfg(feature = "mint")]
impl From<mint::ColumnMatrix3<f32>> for Mat3x3 {
#[inline]
fn from(v: mint::ColumnMatrix3<f32>) -> Self {
std::convert::From::<[[f32; 3]; 3]>::from([v.x.into(), v.y.into(), v.z.into()])
}
}
105 changes: 105 additions & 0 deletions crates/re_types/src/datatypes/mat4x4_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use super::Mat4x4;

use crate::datatypes::Vec4D;

impl Mat4x4 {
#[rustfmt::skip]
pub const IDENTITY: Mat4x4 = Mat4x4([
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
]);

/// Returns the matrix column for the given `index`.
///
/// # Panics
///
/// Panics if `index` is greater than 2.
#[inline]
pub fn col(&self, index: usize) -> Vec4D {
match index {
0 => [self.0[0], self.0[1], self.0[2], self.0[3]].into(),
1 => [self.0[4], self.0[5], self.0[6], self.0[7]].into(),
2 => [self.0[8], self.0[9], self.0[10], self.0[11]].into(),
3 => [self.0[12], self.0[13], self.0[14], self.0[15]].into(),
_ => panic!("index out of bounds"),
}
}
}

impl<Idx> std::ops::Index<Idx> for Mat4x4
where
Idx: std::slice::SliceIndex<[f32]>,
{
type Output = Idx::Output;

#[inline]
fn index(&self, index: Idx) -> &Self::Output {
&self.0[index]
}
}

impl From<[Vec4D; 4]> for Mat4x4 {
#[inline]
#[rustfmt::skip]
fn from(v: [Vec4D; 4]) -> Self {
Self([
v[0].x(), v[0].y(), v[0].z(), v[0].w(),
v[1].x(), v[1].y(), v[1].z(), v[1].w(),
v[2].x(), v[2].y(), v[2].z(), v[2].w(),
v[3].x(), v[3].y(), v[3].z(), v[3].w(),
])
}
}

impl From<Mat4x4> for [Vec4D; 4] {
fn from(val: Mat4x4) -> Self {
[
[val.0[0], val.0[1], val.0[2], val.0[3]].into(),
[val.0[4], val.0[5], val.0[6], val.0[7]].into(),
[val.0[8], val.0[9], val.0[10], val.0[11]].into(),
[val.0[12], val.0[13], val.0[14], val.0[15]].into(),
]
}
}

impl From<[[f32; 4]; 4]> for Mat4x4 {
#[inline]
fn from(v: [[f32; 4]; 4]) -> Self {
Self::from([Vec4D(v[0]), Vec4D(v[1]), Vec4D(v[2]), Vec4D(v[3])])
}
}

#[cfg(feature = "glam")]
impl From<Mat4x4> for glam::Mat4 {
#[inline]
fn from(v: Mat4x4) -> Self {
let [x, y, z, w]: [Vec4D; 4] = v.into();
Self::from_cols(x.into(), y.into(), z.into(), w.into())
}
}

#[cfg(feature = "glam")]
impl From<glam::Mat4> for Mat4x4 {
#[inline]
fn from(v: glam::Mat4) -> Self {
Self::from(v.to_cols_array_2d())
}
}

#[cfg(feature = "mint")]
impl From<Mat4x4> for mint::ColumnMatrix4<f32> {
#[inline]
fn from(v: Mat4x4) -> Self {
v.0.into()
}
}

#[cfg(feature = "mint")]
impl From<mint::ColumnMatrix4<f32>> for Mat4x4 {
#[inline]
fn from(v: mint::ColumnMatrix4<f32>) -> Self {
std::convert::From::<[[f32; 4]; 4]>::from([v.x.into(), v.y.into(), v.z.into(), v.w.into()])
}
}
1 change: 1 addition & 0 deletions crates/re_types/src/datatypes/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions crates/re_types/src/datatypes/quaternion_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,19 @@ impl From<glam::Quat> for Quaternion {
Self::from_xyzw(q.to_array())
}
}

#[cfg(feature = "mint")]
impl From<Quaternion> for mint::Quaternion<f32> {
#[inline]
fn from(val: Quaternion) -> Self {
val.0.into()
}
}

#[cfg(feature = "mint")]
impl From<mint::Quaternion<f32>> for Quaternion {
#[inline]
fn from(val: mint::Quaternion<f32>) -> Self {
Self::from_xyzw(val.into())
}
}
19 changes: 19 additions & 0 deletions crates/re_types/src/datatypes/rotation3d_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,22 @@ impl From<glam::Quat> for Rotation3D {
Rotation3D::Quaternion(val.into())
}
}

#[cfg(feature = "mint")]
impl From<Rotation3D> for mint::Quaternion<f32> {
#[inline]
fn from(val: Rotation3D) -> Self {
match val {
Rotation3D::Quaternion(v) => v.into(),
Rotation3D::AxisAngle(a) => a.into(),
}
}
}

#[cfg(feature = "mint")]
impl From<mint::Quaternion<f32>> for Rotation3D {
#[inline]
fn from(val: mint::Quaternion<f32>) -> Self {
Rotation3D::Quaternion(val.into())
}
}
9 changes: 9 additions & 0 deletions crates/re_types/src/datatypes/rotation_axis_angle_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ impl From<RotationAxisAngle> for glam::Quat {
.unwrap_or_default()
}
}

#[cfg(feature = "mint")]
impl From<RotationAxisAngle> for mint::Quaternion<f32> {
#[inline]
fn from(val: RotationAxisAngle) -> Self {
let (s, c) = (val.angle.radians() * 0.5).sin_cos();
[val.axis.x() * s, val.axis.y() * s, val.axis.z() * s, c].into()
}
}
Loading

0 comments on commit 2f27d91

Please sign in to comment.