Skip to content

Commit

Permalink
Restore the array access of vectors and quaternions
Browse files Browse the repository at this point in the history
  • Loading branch information
plule committed Mar 23, 2024
1 parent ac3efb4 commit 79911b9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
25 changes: 18 additions & 7 deletions src/leap_vector.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
use derive_deref::Deref;
use std::ops::Deref;

use leap_sys::{_LEAP_VECTOR__bindgen_ty_1__bindgen_ty_1, LEAP_VECTOR};
use leap_sys::{_LEAP_VECTOR__bindgen_ty_1, _LEAP_VECTOR__bindgen_ty_1__bindgen_ty_1, LEAP_VECTOR};

#[doc = " A three element, floating-point vector."]
#[doc = " @since 3.0.0"]
#[derive(Deref)]
pub struct LeapVector<'a>(pub(crate) &'a _LEAP_VECTOR__bindgen_ty_1__bindgen_ty_1);
pub struct LeapVector<'a>(pub(crate) &'a _LEAP_VECTOR__bindgen_ty_1);

impl<'a> From<&'a LEAP_VECTOR> for LeapVector<'a> {
fn from(vector: &'a LEAP_VECTOR) -> Self {
Self(unsafe { &vector.__bindgen_anon_1.__bindgen_anon_1 })
Self(&vector.__bindgen_anon_1)
}
}

impl<'a> LeapVector<'a> {
pub fn array(&self) -> [f32; 3] {
unsafe { self.0.v }
}

/// Convert to a [glam::Vec3]
#[cfg(feature = "glam")]
pub fn into_glam(&self) -> glam::Vec3 {
Expand All @@ -22,11 +25,19 @@ impl<'a> LeapVector<'a> {

/// Convert to a [nalgebra::Vector3]
#[cfg(feature = "nalgebra")]
pub fn to_nalgebra(&self) -> nalgebra::Vector3<f32> {
pub fn into_nalgebra(&self) -> nalgebra::Vector3<f32> {
nalgebra::Vector3::new(self.x, self.y, self.z)
}
}

impl<'a> Deref for LeapVector<'a> {
type Target = _LEAP_VECTOR__bindgen_ty_1__bindgen_ty_1;

fn deref(&self) -> &Self::Target {
unsafe { &self.0.__bindgen_anon_1 }
}
}

#[cfg(feature = "glam")]
impl From<LeapVector<'_>> for glam::Vec3 {
fn from(v: LeapVector) -> glam::Vec3 {
Expand All @@ -37,7 +48,7 @@ impl From<LeapVector<'_>> for glam::Vec3 {
#[cfg(feature = "nalgebra")]
impl From<LeapVector<'_>> for nalgebra::Vector3<f32> {
fn from(v: LeapVector) -> nalgebra::Vector3<f32> {
v.to_nalgebra()
v.into_nalgebra()
}
}

Expand Down
26 changes: 19 additions & 7 deletions src/quaternion.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
use derive_deref::Deref;
use leap_sys::{_LEAP_QUATERNION__bindgen_ty_1__bindgen_ty_1, LEAP_QUATERNION};
use leap_sys::{
_LEAP_QUATERNION__bindgen_ty_1, _LEAP_QUATERNION__bindgen_ty_1__bindgen_ty_1, LEAP_QUATERNION,
};

#[doc = " A four element, floating point quaternion. @since 3.1.2"]
#[derive(Deref)]
pub struct Quaternion<'a>(pub(crate) &'a _LEAP_QUATERNION__bindgen_ty_1__bindgen_ty_1);
pub struct Quaternion<'a>(pub(crate) &'a _LEAP_QUATERNION__bindgen_ty_1);

impl<'a> From<&'a LEAP_QUATERNION> for Quaternion<'a> {
fn from(quaternion: &'a LEAP_QUATERNION) -> Self {
Self(unsafe { &quaternion.__bindgen_anon_1.__bindgen_anon_1 })
Self(&quaternion.__bindgen_anon_1)
}
}

impl<'a> Quaternion<'a> {
pub fn array(&self) -> [f32; 4] {
unsafe { self.0.v }
}

/// Convert to a [glam::Quat]
#[cfg(feature = "glam")]
pub fn into_glam(&self) -> glam::Quat {
Expand All @@ -20,13 +24,21 @@ impl<'a> Quaternion<'a> {

/// Convert to a [nalgebra::UnitQuaternion]
#[cfg(feature = "nalgebra")]
pub fn to_nalgebra(&self) -> nalgebra::UnitQuaternion<f32> {
pub fn into_nalgebra(&self) -> nalgebra::UnitQuaternion<f32> {
nalgebra::UnitQuaternion::new_unchecked(nalgebra::Quaternion::new(
self.w, self.x, self.y, self.z,
))
}
}

impl<'a> core::ops::Deref for Quaternion<'a> {
type Target = _LEAP_QUATERNION__bindgen_ty_1__bindgen_ty_1;

fn deref(&self) -> &Self::Target {
unsafe { &self.0.__bindgen_anon_1 }
}
}

#[cfg(feature = "glam")]
impl From<Quaternion<'_>> for glam::Quat {
fn from(q: Quaternion) -> glam::Quat {
Expand All @@ -37,6 +49,6 @@ impl From<Quaternion<'_>> for glam::Quat {
#[cfg(feature = "nalgebra")]
impl From<Quaternion<'_>> for nalgebra::UnitQuaternion<f32> {
fn from(q: Quaternion) -> nalgebra::UnitQuaternion<f32> {
q.to_nalgebra()
q.into_nalgebra()
}
}

0 comments on commit 79911b9

Please sign in to comment.