Skip to content

Commit

Permalink
Merge pull request #68 from plule/glam_nalgebra
Browse files Browse the repository at this point in the history
Add support for glam and nalgebra
  • Loading branch information
plule authored Mar 23, 2024
2 parents d3d1db5 + 3bf1d03 commit ac3efb4
Show file tree
Hide file tree
Showing 8 changed files with 238 additions and 8 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
matrix:
include:
- sdk-redist-ref: gemini
cargo-features: --no-default-features --features gemini
cargo-features: --no-default-features --features nalgebra --features glam --features gemini
- sdk-redist-ref: orion
cargo-features: --no-default-features
cargo-features: --no-default-features --features nalgebra --features glam
name: check
runs-on: windows-latest

Expand All @@ -31,7 +31,7 @@ jobs:
- name: Check Tests
run: cargo check ${{ matrix.cargo-features }} --tests
- name: Doc
run: cargo doc
run: cargo doc --features nalgebra --features glam
- name: Style
run: cargo fmt -- --check
- name: Clippy
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
matrix:
include:
- sdk-redist-ref: gemini
cargo-features: --no-default-features --features gemini
cargo-features: --no-default-features --features nalgebra --features glam --features gemini
- sdk-redist-ref: orion
cargo-features: --no-default-features
cargo-features: --no-default-features --features nalgebra --features glam
name: build
runs-on: windows-latest

Expand All @@ -39,7 +39,7 @@ jobs:
- name: Build Tests
run: cargo build ${{ matrix.cargo-features }} --tests
- name: Doc
run: cargo doc
run: cargo doc --features nalgebra --features glam
- name: Style
run: cargo fmt -- --check
- name: Clippy
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
### Added

- All the wrapper structures now implement `Deref` of the wrapped `leap-sys` structures
- The opt-in features `glam` and `nalgebra` add conversion method for vectors and quaternions to these libraries.

### Changed

Expand Down
107 changes: 107 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ exclude = ["images/"]
[features]
default = ["gemini"]
gemini = []
glam = ["dep:glam"]
nalgebra = ["dep:nalgebra"]

[dependencies]
leap-sys = "0.2"
num_enum = "0.7.2"
thiserror = "1"
bitflags = "2"
derive_deref = "1.1.1"
glam = { version = "0.26", optional = true }
nalgebra = { version = "0.32", optional = true }

[dev-dependencies]
throbber = "0.1"
image = "0.25.0"
tempfile = "3.10.1"

[package.metadata.docs.rs]
features = ["glam", "nalgebra"]
57 changes: 57 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,63 @@ for _ in 0..10 {
}
```

## `glam` and `nalgebra` Integration

`leaprs` includes opt-in `glam` and `nalgebra` integrations.

They are two popular linear algebra library. Both are commonly used, and these features can help integrating with ecosystem using these crates.
`glam` is used by `Bevy`, and `nalgebra` is used by `Fyrox`.

### glam

`cargo build --features glam`

```rust
use leaprs::*;
use glam::{Vec3, Quat};

let mut c = Connection::create(ConnectionConfig::default()).unwrap();
c.open().unwrap();
for _ in 0..10 {
if let Ok(msg) = c.poll(1000) {
match msg.event() {
Event::Tracking(e) => {
for hand in e.hands() {
let position: Vec3 = hand.palm().position().into();
let orientation: Quat = hand.palm().orientation().into();
}
},
_ => {}
}
}
}
```

### nalgebra

`cargo build --features nalgebra`

```rust
use leaprs::*;
use nalgebra::{Vector3, UnitQuaternion};

let mut c = Connection::create(ConnectionConfig::default()).unwrap();
c.open().unwrap();
for _ in 0..10 {
if let Ok(msg) = c.poll(1000) {
match msg.event() {
Event::Tracking(e) => {
for hand in e.hands() {
let position: Vector3<f32> = hand.palm().position().into();
let orientation: UnitQuaternion<f32> = hand.palm().orientation().into();
}
},
_ => {}
}
}
}
```

## License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Expand Down
32 changes: 30 additions & 2 deletions src/leap_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,43 @@ impl<'a> From<&'a LEAP_VECTOR> for LeapVector<'a> {
}
}

impl<'a> LeapVector<'a> {
/// Convert to a [glam::Vec3]
#[cfg(feature = "glam")]
pub fn into_glam(&self) -> glam::Vec3 {
glam::Vec3::new(self.x, self.y, self.z)
}

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

#[cfg(feature = "glam")]
impl From<LeapVector<'_>> for glam::Vec3 {
fn from(v: LeapVector) -> glam::Vec3 {
v.into_glam()
}
}

#[cfg(feature = "nalgebra")]
impl From<LeapVector<'_>> for nalgebra::Vector3<f32> {
fn from(v: LeapVector) -> nalgebra::Vector3<f32> {
v.to_nalgebra()
}
}

/// Build a native [LEAP_VECTOR].
/// Useful for method taking an owned [LEAP_VECTOR] as argument.
pub fn build_leap_vector(v: [f32; 3]) -> LEAP_VECTOR {
pub(crate) fn build_leap_vector(v: [f32; 3]) -> LEAP_VECTOR {
LEAP_VECTOR {
__bindgen_anon_1: { leap_sys::_LEAP_VECTOR__bindgen_ty_1 { v } },
}
}

/// Convert a [LEAP_VECTOR] to an array.
pub fn leap_vector_to_array(v: LEAP_VECTOR) -> [f32; 3] {
pub(crate) fn leap_vector_to_array(v: LEAP_VECTOR) -> [f32; 3] {
unsafe { v.__bindgen_anon_1.v }
}
Loading

0 comments on commit ac3efb4

Please sign in to comment.