refactor: use typemap for aspects!
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
use super::{get_field, BoxFieldAspect, Field, FieldTrait, Node};
|
||||
use crate::core::client::Client;
|
||||
use super::{BoxFieldAspect, FieldTrait, Node};
|
||||
use crate::nodes::fields::FieldAspect;
|
||||
use crate::nodes::spatial::Spatial;
|
||||
use color_eyre::eyre::{ensure, Result};
|
||||
use crate::{core::client::Client, nodes::fields::Field};
|
||||
use color_eyre::eyre::Result;
|
||||
use glam::{vec3, vec3a, Vec3, Vec3A};
|
||||
use mint::Vector3;
|
||||
use parking_lot::Mutex;
|
||||
@@ -14,24 +14,14 @@ pub struct BoxField {
|
||||
}
|
||||
|
||||
impl BoxField {
|
||||
pub fn add_to(node: &Arc<Node>, size: Vector3<f32>) -> Result<Arc<Field>> {
|
||||
ensure!(
|
||||
node.spatial.get().is_some(),
|
||||
"Internal: Node does not have a spatial attached!"
|
||||
);
|
||||
ensure!(
|
||||
node.field.get().is_none(),
|
||||
"Internal: Node already has a field attached!"
|
||||
);
|
||||
pub fn add_to(node: &Arc<Node>, size: Vector3<f32>) {
|
||||
let box_field = BoxField {
|
||||
space: node.spatial.get().unwrap().clone(),
|
||||
space: node.get_aspect::<Spatial>().unwrap().clone(),
|
||||
size: Mutex::new(size.into()),
|
||||
};
|
||||
<BoxField as FieldAspect>::add_node_members(node);
|
||||
<BoxField as BoxFieldAspect>::add_node_members(node);
|
||||
let field = Arc::new(Field::Box(box_field));
|
||||
let _ = node.field.set(field.clone());
|
||||
Ok(field)
|
||||
node.add_aspect(Field::Box(box_field));
|
||||
}
|
||||
|
||||
pub fn set_size(&self, size: Vector3<f32>) {
|
||||
@@ -60,7 +50,8 @@ impl BoxFieldAspect for BoxField {
|
||||
_calling_client: Arc<Client>,
|
||||
size: mint::Vector3<f32>,
|
||||
) -> Result<()> {
|
||||
let Field::Box(this_field) = &*get_field(&node)? else {
|
||||
let this_field = node.get_aspect::<Field>()?;
|
||||
let Field::Box(this_field) = &*this_field else {
|
||||
return Ok(());
|
||||
};
|
||||
this_field.set_size(size.into());
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use super::{get_field, CylinderFieldAspect, Field, FieldTrait, Node};
|
||||
use super::{CylinderFieldAspect, Field, FieldTrait, Node};
|
||||
use crate::core::client::Client;
|
||||
use crate::nodes::fields::FieldAspect;
|
||||
use crate::nodes::spatial::Spatial;
|
||||
use color_eyre::eyre::{ensure, Result};
|
||||
use color_eyre::eyre::Result;
|
||||
use glam::{swizzles::*, vec2, Vec3A};
|
||||
use portable_atomic::AtomicF32;
|
||||
|
||||
@@ -16,24 +16,15 @@ pub struct CylinderField {
|
||||
}
|
||||
|
||||
impl CylinderField {
|
||||
pub fn add_to(node: &Arc<Node>, length: f32, radius: f32) -> Result<()> {
|
||||
ensure!(
|
||||
node.spatial.get().is_some(),
|
||||
"Internal: Node does not have a spatial attached!"
|
||||
);
|
||||
ensure!(
|
||||
node.field.get().is_none(),
|
||||
"Internal: Node already has a field attached!"
|
||||
);
|
||||
pub fn add_to(node: &Arc<Node>, length: f32, radius: f32) {
|
||||
let cylinder_field = CylinderField {
|
||||
space: node.spatial.get().unwrap().clone(),
|
||||
space: node.get_aspect::<Spatial>().unwrap().clone(),
|
||||
length: AtomicF32::new(length.abs()),
|
||||
radius: AtomicF32::new(radius.abs()),
|
||||
};
|
||||
<CylinderField as FieldAspect>::add_node_members(node);
|
||||
<CylinderField as CylinderFieldAspect>::add_node_members(node);
|
||||
let _ = node.field.set(Arc::new(Field::Cylinder(cylinder_field)));
|
||||
Ok(())
|
||||
node.add_aspect(Field::Cylinder(cylinder_field));
|
||||
}
|
||||
|
||||
pub fn set_size(&self, length: f32, radius: f32) {
|
||||
@@ -60,7 +51,8 @@ impl CylinderFieldAspect for CylinderField {
|
||||
length: f32,
|
||||
radius: f32,
|
||||
) -> Result<()> {
|
||||
let Field::Cylinder(this_field) = &*get_field(&node)? else {
|
||||
let this_field = node.get_aspect::<Field>()?;
|
||||
let Field::Cylinder(this_field) = &*this_field else {
|
||||
return Ok(());
|
||||
};
|
||||
this_field.set_size(length, radius);
|
||||
|
||||
@@ -9,8 +9,8 @@ use self::sphere::SphereField;
|
||||
use self::torus::TorusField;
|
||||
|
||||
use super::alias::AliasInfo;
|
||||
use super::spatial::{get_spatial, Spatial};
|
||||
use super::Node;
|
||||
use super::spatial::Spatial;
|
||||
use super::{Aspect, Node};
|
||||
use crate::core::client::Client;
|
||||
use crate::create_interface;
|
||||
use crate::nodes::spatial::Transform;
|
||||
@@ -30,7 +30,7 @@ pub static FIELD_ALIAS_INFO: Lazy<AliasInfo> = Lazy::new(|| AliasInfo {
|
||||
|
||||
stardust_xr_server_codegen::codegen_field_protocol!();
|
||||
|
||||
pub trait FieldTrait {
|
||||
pub trait FieldTrait: Send + Sync + 'static {
|
||||
fn spatial_ref(&self) -> &Spatial;
|
||||
|
||||
fn local_distance(&self, p: Vec3A) -> f32;
|
||||
@@ -114,9 +114,9 @@ impl<Fi: FieldTrait + 'static> FieldAspect for Fi {
|
||||
space: Arc<Node>,
|
||||
point: mint::Vector3<f32>,
|
||||
) -> Result<f32> {
|
||||
let reference_space = get_spatial(&space, "Reference space")?;
|
||||
let this_field = node.field.get().unwrap();
|
||||
Ok(this_field.distance(reference_space.as_ref(), point.into()))
|
||||
let reference_space = space.get_aspect::<Spatial>()?;
|
||||
let this_field = node.get_aspect::<Field>()?;
|
||||
Ok((*this_field).distance(reference_space.as_ref(), point.into()))
|
||||
}
|
||||
|
||||
async fn normal(
|
||||
@@ -125,8 +125,8 @@ impl<Fi: FieldTrait + 'static> FieldAspect for Fi {
|
||||
space: Arc<Node>,
|
||||
point: mint::Vector3<f32>,
|
||||
) -> Result<Vector3<f32>> {
|
||||
let reference_space = get_spatial(&space, "Reference space")?;
|
||||
let this_field = node.field.get().unwrap();
|
||||
let reference_space = space.get_aspect::<Spatial>()?;
|
||||
let this_field = node.get_aspect::<Field>()?;
|
||||
Ok(this_field
|
||||
.normal(reference_space.as_ref(), point.into(), 0.001)
|
||||
.into())
|
||||
@@ -138,8 +138,8 @@ impl<Fi: FieldTrait + 'static> FieldAspect for Fi {
|
||||
space: Arc<Node>,
|
||||
point: mint::Vector3<f32>,
|
||||
) -> Result<Vector3<f32>> {
|
||||
let reference_space = get_spatial(&space, "Reference space")?;
|
||||
let this_field = node.field.get().unwrap();
|
||||
let reference_space = space.get_aspect::<Spatial>()?;
|
||||
let this_field = node.get_aspect::<Field>()?;
|
||||
Ok(this_field
|
||||
.closest_point(reference_space.as_ref(), point.into(), 0.001)
|
||||
.into())
|
||||
@@ -152,12 +152,12 @@ impl<Fi: FieldTrait + 'static> FieldAspect for Fi {
|
||||
ray_origin: mint::Vector3<f32>,
|
||||
ray_direction: mint::Vector3<f32>,
|
||||
) -> Result<RayMarchResult> {
|
||||
let reference_space = get_spatial(&space, "Reference space")?;
|
||||
let this_field = node.field.get().unwrap();
|
||||
let reference_space = space.get_aspect::<Spatial>()?;
|
||||
let this_field = node.get_aspect::<Field>()?;
|
||||
Ok(this_field.ray_march(Ray {
|
||||
origin: ray_origin.into(),
|
||||
direction: ray_direction.into(),
|
||||
space: reference_space,
|
||||
space: reference_space.clone(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
@@ -183,6 +183,9 @@ pub enum Field {
|
||||
Sphere(SphereField),
|
||||
Torus(TorusField),
|
||||
}
|
||||
impl Aspect for Field {
|
||||
const NAME: &'static str = "Field";
|
||||
}
|
||||
impl Deref for Field {
|
||||
type Target = dyn FieldTrait;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@@ -194,6 +197,72 @@ impl Deref for Field {
|
||||
}
|
||||
}
|
||||
}
|
||||
// impl FieldTrait for Field {
|
||||
// fn spatial_ref(&self) -> &Spatial {
|
||||
// match self {
|
||||
// Field::Box(field) => field.spatial_ref(),
|
||||
// Field::Cylinder(field) => field.spatial_ref(),
|
||||
// Field::Sphere(field) => field.spatial_ref(),
|
||||
// Field::Torus(field) => field.spatial_ref(),
|
||||
// }
|
||||
// }
|
||||
// fn local_distance(&self, p: Vec3A) -> f32 {
|
||||
// match self {
|
||||
// Field::Box(field) => field.local_distance(p),
|
||||
// Field::Cylinder(field) => field.local_distance(p),
|
||||
// Field::Sphere(field) => field.local_distance(p),
|
||||
// Field::Torus(field) => field.local_distance(p),
|
||||
// }
|
||||
// }
|
||||
// fn local_normal(&self, p: Vec3A, r: f32) -> Vec3A {
|
||||
// match self {
|
||||
// Field::Box(field) => field.local_normal(p, r),
|
||||
// Field::Cylinder(field) => field.local_normal(p, r),
|
||||
// Field::Sphere(field) => field.local_normal(p, r),
|
||||
// Field::Torus(field) => field.local_normal(p, r),
|
||||
// }
|
||||
// }
|
||||
// fn local_closest_point(&self, p: Vec3A, r: f32) -> Vec3A {
|
||||
// match self {
|
||||
// Field::Box(field) => field.local_closest_point(p, r),
|
||||
// Field::Cylinder(field) => field.local_closest_point(p, r),
|
||||
// Field::Sphere(field) => field.local_closest_point(p, r),
|
||||
// Field::Torus(field) => field.local_closest_point(p, r),
|
||||
// }
|
||||
// }
|
||||
// fn distance(&self, reference_space: &Spatial, p: Vec3A) -> f32 {
|
||||
// match self {
|
||||
// Field::Box(field) => field.distance(reference_space, p),
|
||||
// Field::Cylinder(field) => field.distance(reference_space, p),
|
||||
// Field::Sphere(field) => field.distance(reference_space, p),
|
||||
// Field::Torus(field) => field.distance(reference_space, p),
|
||||
// }
|
||||
// }
|
||||
// fn normal(&self, reference_space: &Spatial, p: Vec3A, r: f32) -> Vec3A {
|
||||
// match self {
|
||||
// Field::Box(field) => field.normal(reference_space, p, r),
|
||||
// Field::Cylinder(field) => field.normal(reference_space, p, r),
|
||||
// Field::Sphere(field) => field.normal(reference_space, p, r),
|
||||
// Field::Torus(field) => field.normal(reference_space, p, r),
|
||||
// }
|
||||
// }
|
||||
// fn closest_point(&self, reference_space: &Spatial, p: Vec3A, r: f32) -> Vec3A {
|
||||
// match self {
|
||||
// Field::Box(field) => field.closest_point(reference_space, p, r),
|
||||
// Field::Cylinder(field) => field.closest_point(reference_space, p, r),
|
||||
// Field::Sphere(field) => field.closest_point(reference_space, p, r),
|
||||
// Field::Torus(field) => field.closest_point(reference_space, p, r),
|
||||
// }
|
||||
// }
|
||||
// fn ray_march(&self, ray: Ray) -> RayMarchResult {
|
||||
// match self {
|
||||
// Field::Box(field) => field.ray_march(ray),
|
||||
// Field::Cylinder(field) => field.ray_march(ray),
|
||||
// Field::Sphere(field) => field.ray_march(ray),
|
||||
// Field::Torus(field) => field.ray_march(ray),
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
create_interface!(FieldInterface, FieldInterfaceAspect, "/field");
|
||||
pub struct FieldInterface;
|
||||
@@ -207,7 +276,7 @@ impl FieldInterfaceAspect for FieldInterface {
|
||||
size: mint::Vector3<f32>,
|
||||
) -> Result<()> {
|
||||
let transform = transform.to_mat4(true, true, false);
|
||||
let parent = get_spatial(&parent, "Spatial parent")?;
|
||||
let parent = parent.get_aspect::<Spatial>()?;
|
||||
let node = Node::create_parent_name(
|
||||
&calling_client,
|
||||
Self::CREATE_BOX_FIELD_PARENT_PATH,
|
||||
@@ -215,8 +284,8 @@ impl FieldInterfaceAspect for FieldInterface {
|
||||
true,
|
||||
)
|
||||
.add_to_scenegraph()?;
|
||||
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||
BoxField::add_to(&node, size)?;
|
||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
||||
BoxField::add_to(&node, size);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -230,7 +299,7 @@ impl FieldInterfaceAspect for FieldInterface {
|
||||
radius: f32,
|
||||
) -> Result<()> {
|
||||
let transform = transform.to_mat4(true, true, false);
|
||||
let parent = get_spatial(&parent, "Spatial parent")?;
|
||||
let parent = parent.get_aspect::<Spatial>()?;
|
||||
let node = Node::create_parent_name(
|
||||
&calling_client,
|
||||
Self::CREATE_CYLINDER_FIELD_PARENT_PATH,
|
||||
@@ -238,8 +307,8 @@ impl FieldInterfaceAspect for FieldInterface {
|
||||
true,
|
||||
)
|
||||
.add_to_scenegraph()?;
|
||||
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||
CylinderField::add_to(&node, length, radius)?;
|
||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
||||
CylinderField::add_to(&node, length, radius);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -251,7 +320,7 @@ impl FieldInterfaceAspect for FieldInterface {
|
||||
position: mint::Vector3<f32>,
|
||||
radius: f32,
|
||||
) -> Result<()> {
|
||||
let parent = get_spatial(&parent, "Spatial parent")?;
|
||||
let parent = parent.get_aspect::<Spatial>()?;
|
||||
let node = Node::create_parent_name(
|
||||
&calling_client,
|
||||
Self::CREATE_SPHERE_FIELD_PARENT_PATH,
|
||||
@@ -261,11 +330,11 @@ impl FieldInterfaceAspect for FieldInterface {
|
||||
.add_to_scenegraph()?;
|
||||
Spatial::add_to(
|
||||
&node,
|
||||
Some(parent),
|
||||
Some(parent.clone()),
|
||||
Mat4::from_translation(position.into()),
|
||||
false,
|
||||
)?;
|
||||
SphereField::add_to(&node, radius)?;
|
||||
);
|
||||
SphereField::add_to(&node, radius);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -279,7 +348,7 @@ impl FieldInterfaceAspect for FieldInterface {
|
||||
radius_b: f32,
|
||||
) -> Result<()> {
|
||||
let transform = transform.to_mat4(true, true, false);
|
||||
let parent = get_spatial(&parent, "Spatial parent")?;
|
||||
let parent = parent.get_aspect::<Spatial>()?;
|
||||
let node = Node::create_parent_name(
|
||||
&calling_client,
|
||||
Self::CREATE_TORUS_FIELD_PARENT_PATH,
|
||||
@@ -287,18 +356,12 @@ impl FieldInterfaceAspect for FieldInterface {
|
||||
true,
|
||||
)
|
||||
.add_to_scenegraph()?;
|
||||
Spatial::add_to(&node, Some(parent), transform, false)?;
|
||||
TorusField::add_to(&node, radius_a, radius_b)?;
|
||||
Spatial::add_to(&node, Some(parent.clone()), transform, false);
|
||||
TorusField::add_to(&node, radius_a, radius_b);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_field(client: &Client, path: &str) -> Result<Arc<Field>> {
|
||||
client
|
||||
.get_node("Field", path)?
|
||||
.get_aspect("Field", "info", |n| &n.field)
|
||||
.cloned()
|
||||
}
|
||||
pub fn get_field(node: &Node) -> Result<Arc<Field>> {
|
||||
node.get_aspect("Field", "info", |n| &n.field).cloned()
|
||||
client.get_node("Field", path)?.get_aspect::<Field>()
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use super::{get_field, Field, FieldTrait, Node, SphereFieldAspect};
|
||||
use super::{Field, FieldTrait, Node, SphereFieldAspect};
|
||||
use crate::core::client::Client;
|
||||
use crate::nodes::fields::FieldAspect;
|
||||
use crate::nodes::spatial::Spatial;
|
||||
use color_eyre::eyre::{ensure, Result};
|
||||
use color_eyre::eyre::Result;
|
||||
use glam::Vec3A;
|
||||
use portable_atomic::AtomicF32;
|
||||
use std::sync::atomic::Ordering;
|
||||
@@ -14,23 +14,14 @@ pub struct SphereField {
|
||||
}
|
||||
|
||||
impl SphereField {
|
||||
pub fn add_to(node: &Arc<Node>, radius: f32) -> Result<()> {
|
||||
ensure!(
|
||||
node.spatial.get().is_some(),
|
||||
"Internal: Node does not have a spatial attached!"
|
||||
);
|
||||
ensure!(
|
||||
node.field.get().is_none(),
|
||||
"Internal: Node already has a field attached!"
|
||||
);
|
||||
pub fn add_to(node: &Arc<Node>, radius: f32) {
|
||||
let sphere_field = SphereField {
|
||||
space: node.spatial.get().unwrap().clone(),
|
||||
space: node.get_aspect::<Spatial>().unwrap().clone(),
|
||||
radius: AtomicF32::new(radius),
|
||||
};
|
||||
<SphereField as FieldAspect>::add_node_members(node);
|
||||
<SphereField as SphereFieldAspect>::add_node_members(node);
|
||||
let _ = node.field.set(Arc::new(Field::Sphere(sphere_field)));
|
||||
Ok(())
|
||||
node.add_aspect(Field::Sphere(sphere_field));
|
||||
}
|
||||
|
||||
pub fn set_radius(&self, radius: f32) {
|
||||
@@ -54,7 +45,8 @@ impl FieldTrait for SphereField {
|
||||
}
|
||||
impl SphereFieldAspect for SphereField {
|
||||
fn set_radius(node: Arc<Node>, _calling_client: Arc<Client>, radius: f32) -> Result<()> {
|
||||
let Field::Sphere(this_field) = &*get_field(&node)? else {
|
||||
let this_field = node.get_aspect::<Field>()?;
|
||||
let Field::Sphere(this_field) = &*this_field else {
|
||||
return Ok(());
|
||||
};
|
||||
this_field.set_radius(radius);
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
use super::{get_field, Field, FieldTrait, Node, TorusFieldAspect};
|
||||
use super::{Field, FieldTrait, Node, TorusFieldAspect};
|
||||
use crate::core::client::Client;
|
||||
use crate::nodes::fields::FieldAspect;
|
||||
use crate::nodes::spatial::Spatial;
|
||||
use crate::nodes::Message;
|
||||
use color_eyre::eyre::{ensure, Result};
|
||||
use color_eyre::eyre::Result;
|
||||
use glam::{swizzles::*, vec2, Vec3A};
|
||||
use portable_atomic::AtomicF32;
|
||||
use stardust_xr::schemas::flex::deserialize;
|
||||
|
||||
use std::sync::atomic::Ordering;
|
||||
use std::sync::Arc;
|
||||
@@ -18,45 +16,21 @@ pub struct TorusField {
|
||||
}
|
||||
|
||||
impl TorusField {
|
||||
pub fn add_to(node: &Arc<Node>, radius_a: f32, radius_b: f32) -> Result<()> {
|
||||
ensure!(
|
||||
node.spatial.get().is_some(),
|
||||
"Internal: Node does not have a spatial attached!"
|
||||
);
|
||||
ensure!(
|
||||
node.field.get().is_none(),
|
||||
"Internal: Node already has a field attached!"
|
||||
);
|
||||
pub fn add_to(node: &Arc<Node>, radius_a: f32, radius_b: f32) {
|
||||
let torus_field = TorusField {
|
||||
space: node.spatial.get().unwrap().clone(),
|
||||
space: node.get_aspect::<Spatial>().unwrap().clone(),
|
||||
radius_a: AtomicF32::new(radius_a.abs()),
|
||||
radius_b: AtomicF32::new(radius_b.abs()),
|
||||
};
|
||||
<TorusField as FieldAspect>::add_node_members(node);
|
||||
<TorusField as TorusFieldAspect>::add_node_members(node);
|
||||
node.add_local_signal("set_size", TorusField::set_size_flex);
|
||||
let _ = node.field.set(Arc::new(Field::Torus(torus_field)));
|
||||
Ok(())
|
||||
node.add_aspect(Field::Torus(torus_field));
|
||||
}
|
||||
|
||||
pub fn set_size(&self, radius_a: f32, radius_b: f32) {
|
||||
self.radius_a.store(radius_a.abs(), Ordering::Relaxed);
|
||||
self.radius_b.store(radius_b.abs(), Ordering::Relaxed);
|
||||
}
|
||||
|
||||
pub fn set_size_flex(
|
||||
node: Arc<Node>,
|
||||
_calling_client: Arc<Client>,
|
||||
message: Message,
|
||||
) -> Result<()> {
|
||||
let Field::Torus(torus_field) = node.field.get().unwrap().as_ref() else {
|
||||
return Ok(());
|
||||
};
|
||||
let (radius_a, radius_b) = deserialize(message.as_ref())?;
|
||||
torus_field.set_size(radius_a, radius_b);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl FieldTrait for TorusField {
|
||||
fn local_distance(&self, p: Vec3A) -> f32 {
|
||||
@@ -76,7 +50,8 @@ impl TorusFieldAspect for TorusField {
|
||||
radius_a: f32,
|
||||
radius_b: f32,
|
||||
) -> Result<()> {
|
||||
let Field::Torus(this_field) = &*get_field(&node)? else {
|
||||
let this_field = node.get_aspect::<Field>()?;
|
||||
let Field::Torus(this_field) = &*this_field else {
|
||||
return Ok(());
|
||||
};
|
||||
this_field.set_size(radius_a, radius_b);
|
||||
|
||||
Reference in New Issue
Block a user