From 67088110e0bfb952992aa0fcaa4732d9a4eb3de7 Mon Sep 17 00:00:00 2001 From: fwcd Date: Sun, 23 Mar 2025 02:57:39 +0100 Subject: [PATCH 1/6] Add Vec3 --- lighthouse-protocol/src/utils/mod.rs | 2 + lighthouse-protocol/src/utils/vec3.rs | 87 +++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 lighthouse-protocol/src/utils/vec3.rs diff --git a/lighthouse-protocol/src/utils/mod.rs b/lighthouse-protocol/src/utils/mod.rs index 822bd4d..d44dc41 100644 --- a/lighthouse-protocol/src/utils/mod.rs +++ b/lighthouse-protocol/src/utils/mod.rs @@ -6,6 +6,7 @@ mod rotation; mod sqrt; mod unity; mod vec2; +mod vec3; mod zero; pub use color::*; @@ -16,4 +17,5 @@ pub use rotation::*; pub use sqrt::*; pub use unity::*; pub use vec2::*; +pub use vec3::*; pub use zero::*; diff --git a/lighthouse-protocol/src/utils/vec3.rs b/lighthouse-protocol/src/utils/vec3.rs new file mode 100644 index 0000000..364449b --- /dev/null +++ b/lighthouse-protocol/src/utils/vec3.rs @@ -0,0 +1,87 @@ +use std::{fmt, ops::{Add, AddAssign, Mul, Neg, Sub, SubAssign}}; + +use serde::{Deserialize, Serialize}; + +use super::{Sqrt, Zero}; + +/// A 3D vector. +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct Vec3 { + pub x: T, + pub y: T, + pub z: T, +} + +impl Vec3 { + /// Creates a mew position. + pub const fn new(x: T, y: T, z: T) -> Self { + Self { x, y, z } + } + + /// Maps a function over the vector. + pub fn map(self, mut f: impl FnMut(T) -> U) -> Vec3 { + Vec3 { + x: f(self.x), + y: f(self.y), + z: f(self.z), + } + } +} + +impl Zero for Vec3 where T: Zero { + /// The origin. + const ZERO: Self = Self::new(T::ZERO, T::ZERO, T::ZERO); +} + +impl Vec3 where T: Add + Mul + Sqrt + Copy { + /// The vector's length. + pub fn length(&self) -> T { + (self.x * self.x + self.y * self.y + self.z * self.z).sqrt() + } +} + +impl fmt::Display for Vec3 where T: fmt::Display { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "({}, {}, {})", self.x, self.y, self.z) + } +} + +impl Add for Vec3 where T: Add { + type Output = Self; + + fn add(self, rhs: Vec3) -> Self { + Self::new(self.x + rhs.x, self.y + rhs.y, self.z + rhs.z) + } +} + +impl Neg for Vec3 where T: Neg { + type Output = Self; + + fn neg(self) -> Self { + Self::new(-self.x, -self.y, -self.z) + } +} + +impl Sub for Vec3 where T: Sub { + type Output = Self; + + fn sub(self, rhs: Vec3) -> Self { + Self::new(self.x - rhs.x, self.y - rhs.y, self.z - rhs.z) + } +} + +impl AddAssign for Vec3 where T: AddAssign { + fn add_assign(&mut self, rhs: Vec3) { + self.x += rhs.x; + self.y += rhs.y; + self.z += rhs.z; + } +} + +impl SubAssign for Vec3 where T: SubAssign { + fn sub_assign(&mut self, rhs: Vec3) { + self.x -= rhs.x; + self.y -= rhs.y; + self.z -= rhs.z; + } +} From 17f242084211c40bb3911da64bee29d16aeab13a Mon Sep 17 00:00:00 2001 From: fwcd Date: Sun, 23 Mar 2025 02:59:43 +0100 Subject: [PATCH 2/6] Add missing rename_all to OrientationEvent ...for consistency --- lighthouse-protocol/src/input/orientation_event.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/lighthouse-protocol/src/input/orientation_event.rs b/lighthouse-protocol/src/input/orientation_event.rs index 8aa748d..8650a69 100644 --- a/lighthouse-protocol/src/input/orientation_event.rs +++ b/lighthouse-protocol/src/input/orientation_event.rs @@ -4,6 +4,7 @@ use super::EventSource; /// A device orientation event. #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] pub struct OrientationEvent { /// The client identifier. pub source: EventSource, From ce64dab537fc57580e4ba7f04d0bb46c6af267ac Mon Sep 17 00:00:00 2001 From: fwcd Date: Sun, 23 Mar 2025 03:00:29 +0100 Subject: [PATCH 3/6] Add MotionEvent --- lighthouse-protocol/src/input/mod.rs | 2 ++ lighthouse-protocol/src/input/motion_event.rs | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 lighthouse-protocol/src/input/motion_event.rs diff --git a/lighthouse-protocol/src/input/mod.rs b/lighthouse-protocol/src/input/mod.rs index a0435bc..05fca69 100644 --- a/lighthouse-protocol/src/input/mod.rs +++ b/lighthouse-protocol/src/input/mod.rs @@ -9,6 +9,7 @@ mod key_event; mod key_modifiers; mod legacy_input_event; mod midi_event; +mod motion_event; mod mouse_button; mod mouse_event; mod orientation_event; @@ -25,6 +26,7 @@ pub use key_event::*; pub use key_modifiers::*; pub use legacy_input_event::*; pub use midi_event::*; +pub use motion_event::*; pub use mouse_button::*; pub use mouse_event::*; pub use orientation_event::*; diff --git a/lighthouse-protocol/src/input/motion_event.rs b/lighthouse-protocol/src/input/motion_event.rs new file mode 100644 index 0000000..f54fd5f --- /dev/null +++ b/lighthouse-protocol/src/input/motion_event.rs @@ -0,0 +1,22 @@ +use serde::{Deserialize, Serialize}; + +use crate::Vec3; + +use super::EventSource; + +/// A device motion event. +#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] +#[serde(rename_all = "camelCase")] +pub struct MotionEvent { + /// The client identifier. + pub source: EventSource, + /// The acceleration in 3D space in m/s^2. + pub acceleration: Option>>, + /// The acceleration in 3D space (including gravity) in m/s^2. + pub acceleration_including_gravity: Option>>, + + // TODO: rotation rate + + /// The granularity of these events in ms. + pub interval: f64, +} From bea5012229ba7245d900ff0e1b24c9fa1d09bd06 Mon Sep 17 00:00:00 2001 From: fwcd Date: Sun, 23 Mar 2025 03:02:05 +0100 Subject: [PATCH 4/6] Add Rot3 for representing 3D rotations --- lighthouse-protocol/src/utils/mod.rs | 2 ++ lighthouse-protocol/src/utils/rot3.rs | 32 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 lighthouse-protocol/src/utils/rot3.rs diff --git a/lighthouse-protocol/src/utils/mod.rs b/lighthouse-protocol/src/utils/mod.rs index d44dc41..eda8e67 100644 --- a/lighthouse-protocol/src/utils/mod.rs +++ b/lighthouse-protocol/src/utils/mod.rs @@ -2,6 +2,7 @@ mod color; mod direction; mod rect; mod rem_euclid; +mod rot3; mod rotation; mod sqrt; mod unity; @@ -13,6 +14,7 @@ pub use color::*; pub use direction::*; pub use rect::*; pub use rem_euclid::*; +pub use rot3::*; pub use rotation::*; pub use sqrt::*; pub use unity::*; diff --git a/lighthouse-protocol/src/utils/rot3.rs b/lighthouse-protocol/src/utils/rot3.rs new file mode 100644 index 0000000..5a82b91 --- /dev/null +++ b/lighthouse-protocol/src/utils/rot3.rs @@ -0,0 +1,32 @@ +use serde::{Deserialize, Serialize}; + +use super::Zero; + +/// A 3D rotation. +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] +pub struct Rot3 { + pub alpha: T, + pub beta: T, + pub gamma: T, +} + +impl Rot3 { + /// Creates a mew position. + pub const fn new(alpha: T, beta: T, gamma: T) -> Self { + Self { alpha, beta, gamma } + } + + /// Maps a function over the vector. + pub fn map(self, mut f: impl FnMut(T) -> U) -> Rot3 { + Rot3 { + alpha: f(self.alpha), + beta: f(self.beta), + gamma: f(self.gamma), + } + } +} + +impl Zero for Rot3 where T: Zero { + /// The origin. + const ZERO: Self = Self::new(T::ZERO, T::ZERO, T::ZERO); +} From 038c18eeced600c7d161a83b65f952c6446abd94 Mon Sep 17 00:00:00 2001 From: fwcd Date: Sun, 23 Mar 2025 03:02:37 +0100 Subject: [PATCH 5/6] Add MotionEvent.rotation_rate --- lighthouse-protocol/src/input/motion_event.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lighthouse-protocol/src/input/motion_event.rs b/lighthouse-protocol/src/input/motion_event.rs index f54fd5f..2bfc132 100644 --- a/lighthouse-protocol/src/input/motion_event.rs +++ b/lighthouse-protocol/src/input/motion_event.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::Vec3; +use crate::{Rot3, Vec3}; use super::EventSource; @@ -14,9 +14,8 @@ pub struct MotionEvent { pub acceleration: Option>>, /// The acceleration in 3D space (including gravity) in m/s^2. pub acceleration_including_gravity: Option>>, - - // TODO: rotation rate - + /// The rotation rate in deg/s on the three rotation axes. + pub rotation_rate: Option>>, /// The granularity of these events in ms. pub interval: f64, } From 029f4a6f4255a1fc7ede28d769d4a5515f108148 Mon Sep 17 00:00:00 2001 From: fwcd Date: Sun, 23 Mar 2025 03:06:01 +0100 Subject: [PATCH 6/6] Add motion events to enum --- lighthouse-protocol/src/input/input_event.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lighthouse-protocol/src/input/input_event.rs b/lighthouse-protocol/src/input/input_event.rs index c581da3..3adf521 100644 --- a/lighthouse-protocol/src/input/input_event.rs +++ b/lighthouse-protocol/src/input/input_event.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use crate::Direction; -use super::{EventSource, GamepadEvent, KeyEvent, MidiEvent, MouseEvent, OrientationEvent, UnknownEvent}; +use super::{EventSource, GamepadEvent, KeyEvent, MidiEvent, MotionEvent, MouseEvent, OrientationEvent, UnknownEvent}; /// A user input event, as generated by the new frontend (LUNA). #[derive(Debug, Serialize, Deserialize, PartialEq, Clone)] @@ -13,6 +13,7 @@ pub enum InputEvent { Gamepad(GamepadEvent), Midi(MidiEvent), Orientation(OrientationEvent), + Motion(MotionEvent), #[serde(untagged)] Unknown(UnknownEvent), } @@ -25,6 +26,7 @@ impl InputEvent { InputEvent::Mouse(MouseEvent { source, .. }) => source, InputEvent::Gamepad(GamepadEvent { source, .. }) => source, InputEvent::Orientation(OrientationEvent { source, .. }) => source, + InputEvent::Motion(MotionEvent { source, .. }) => source, InputEvent::Midi(MidiEvent { source, .. }) => source, InputEvent::Unknown(UnknownEvent { source, .. }) => source, }