Skip to content

Commit

Permalink
Find by label
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Aug 16, 2024
1 parent d9f42e6 commit 92ccdb6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
1 change: 1 addition & 0 deletions examples/skeleton.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! A basic 2d humanoid skeleton with sliders powered by Cushy.
#![allow(clippy::too_many_lines)]
use core::f32;
use std::ops::RangeInclusive;

Expand Down
50 changes: 43 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![doc = include_str!(".crate-docs.md")]

use std::{
borrow::Borrow,
collections::{HashMap, HashSet},
f32::consts::PI,
fmt::{Debug, Display},
ops::{Add, Index, IndexMut, Neg, Sub},
ops::{Add, Deref, Index, IndexMut, Neg, Sub},
sync::Arc,
};

Expand Down Expand Up @@ -226,6 +227,29 @@ impl From<BoneKind> for LabeledBoneKind {
}
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
struct ArcString(Arc<String>);

impl PartialEq<str> for ArcString {
fn eq(&self, other: &str) -> bool {
&**self == other
}
}

impl Deref for ArcString {
type Target = str;

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

impl Borrow<str> for ArcString {
fn borrow(&self) -> &str {
self
}
}

/// A collection of [`Bone`]s. connected by [`Joint`]s.
#[derive(Default, Debug)]
pub struct Skeleton {
Expand All @@ -234,8 +258,8 @@ pub struct Skeleton {
joints: Vec<Joint>,
connections: HashMap<BoneAxis, Vec<JointId>>,
generation: usize,
bones_by_label: HashMap<Arc<String>, BoneId>,
joints_by_label: HashMap<Arc<String>, JointId>,
bones_by_label: HashMap<ArcString, BoneId>,
joints_by_label: HashMap<ArcString, JointId>,
}

impl Skeleton {
Expand All @@ -256,7 +280,7 @@ impl Skeleton {
let label = if bone.label.is_empty() {
None
} else {
let label = Arc::new(bone.label);
let label = ArcString(Arc::new(bone.label));
self.bones_by_label.insert(label.clone(), id);
Some(label)
};
Expand Down Expand Up @@ -289,6 +313,18 @@ impl Skeleton {
id
}

/// Finds an existing [`Joint`] by its label.
#[must_use]
pub fn find_joint_by_label(&self, label: &str) -> Option<JointId> {
self.joints_by_label.get(label).copied()
}

/// Finds an existing [`Bone`] by its label.
#[must_use]
pub fn find_bone_by_label(&self, label: &str) -> Option<BoneId> {
self.bones_by_label.get(label).copied()
}

/// Sets a translation to be applied to the entire skeleton.
pub fn set_translation(&mut self, translation: Vector) {
let bone = self.bones.first_mut().expect("root bone must be defined");
Expand Down Expand Up @@ -545,7 +581,7 @@ impl BoneAxis {
#[derive(Debug)]
pub struct Bone {
generation: usize,
label: Option<Arc<String>>,
label: Option<ArcString>,
kind: BoneKind,
start: Vector,
joint_pos: Option<Vector>,
Expand Down Expand Up @@ -599,7 +635,7 @@ impl Bone {
/// A connection between two bones.
#[derive(Debug)]
pub struct Joint {
label: Option<Arc<String>>,
label: Option<ArcString>,
bone_a: BoneAxis,
bone_b: BoneAxis,
calculated_position: Vector,
Expand All @@ -624,7 +660,7 @@ impl Joint {
pub fn with_label(mut self, label: impl Into<String>) -> Self {
let label = label.into();
if !label.is_empty() {
self.label = Some(Arc::new(label));
self.label = Some(ArcString(Arc::new(label)));
}
self
}
Expand Down

0 comments on commit 92ccdb6

Please sign in to comment.