summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2025-04-07 05:07:29 -0400
committerLLLL Colonq <llll@colonq>2025-04-07 05:07:29 -0400
commit7b3157308a0359320f5d098acfa80c6f3bff5a54 (patch)
tree74343fb53a42ba6805bdb165740606c0d0983e20
parenta3073cfa729d4bc2796d6956764d77bde049aad1 (diff)
Add some utility functions
-rw-r--r--Cargo.lock29
-rw-r--r--Cargo.toml1
-rw-r--r--src/utils.rs28
3 files changed, 52 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 58884c5..80d345a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1068,6 +1068,12 @@ dependencies = [
]
[[package]]
+name = "heck"
+version = "0.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
+
+[[package]]
name = "hermit-abi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2477,6 +2483,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
[[package]]
+name = "strum"
+version = "0.27.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f64def088c51c9510a8579e3c5d67c65349dcf755e5479ad3d010aa6454e2c32"
+dependencies = [
+ "strum_macros",
+]
+
+[[package]]
+name = "strum_macros"
+version = "0.27.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c77a8c5abcaf0f9ce05d62342b7d298c346515365c36b673df4ebe3ced01fde8"
+dependencies = [
+ "heck",
+ "proc-macro2",
+ "quote",
+ "rustversion",
+ "syn",
+]
+
+[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -2649,6 +2677,7 @@ dependencies = [
"reqwest",
"serde",
"serde_json",
+ "strum",
"tobj",
"tracing-wasm",
"wasm-bindgen",
diff --git a/Cargo.toml b/Cargo.toml
index ea4902b..38c2063 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -15,6 +15,7 @@ codegen-units = 1
opt-level = 2
[dependencies]
+strum = {version = "*", features = ["derive"]} # utility macros for enums
glow = {version = "=0.13.1", features = []} # rendering
tobj = "*" # loader for .obj meshes loader
gltf = {path = "deps/gltf", features = ["extras", "import", "names", "utils"]} # loader for .gltf scenes
diff --git a/src/utils.rs b/src/utils.rs
index 5d89310..05e9251 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,4 +1,7 @@
+use std::f32::consts::PI;
+
use serde::{Serialize, Deserialize};
+use strum::EnumIter;
pub type Erm<T> = color_eyre::Result<T>;
@@ -35,7 +38,7 @@ pub fn install_error_handler() {
color_eyre::eyre::set_hook(Box::new(move |_| Box::new(ErrorHandler))).expect("failed to install error handler");
}
-#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
+#[derive(Clone, Copy, Debug, EnumIter, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum Cardinal {
North,
South,
@@ -43,8 +46,6 @@ pub enum Cardinal {
East,
}
-pub const CARDINALS: [Cardinal; 4] = [Cardinal::North, Cardinal::South, Cardinal::West, Cardinal::East];
-
impl Cardinal {
pub fn to_string(&self) -> &'static str {
match self {
@@ -94,9 +95,9 @@ impl Cardinal {
pub fn angle(&self) -> f32 {
match self {
Self::North => 0.0,
- Self::South => std::f32::consts::PI,
- Self::West => std::f32::consts::PI / 2.0,
- Self::East => 3.0 * std::f32::consts::PI / 2.0,
+ Self::South => PI,
+ Self::West => PI / 2.0,
+ Self::East => 3.0 * PI / 2.0,
}
}
@@ -108,8 +109,23 @@ impl Cardinal {
Self::East => self.turn_ccw(),
}
}
+
+ pub fn angle_between(&self, o: &Self) -> f32 {
+ if o == &self.turn_cw() { -PI / 2.0 }
+ else if o == &self.turn_ccw() { PI / 2.0 }
+ else if o == &self.turn_cw().turn_cw() { PI }
+ else { 0.0 }
+ }
}
pub fn lerp(a: f32, b: f32, t: f32) -> f32 {
a + t.clamp(0.0, 1.0) * (b - a)
}
+
+pub fn dir_lerp(a: &glam::Vec3, b: glam::Vec3, t: f32) -> glam::Vec3 {
+ let dirrotaxis = a.cross(b).normalize();
+ let dirrotangle = a.angle_between(b);
+ let dirrotfull = glam::Quat::from_axis_angle(dirrotaxis, dirrotangle);
+ let dirrot = glam::Quat::IDENTITY.slerp(dirrotfull, t);
+ dirrot.mul_vec3(a.clone())
+}