Make all nodes thread safe

This commit is contained in:
Nova
2022-06-14 19:34:27 -04:00
parent 651fa5f012
commit 3aa691475c
6 changed files with 182 additions and 207 deletions

View File

@@ -7,7 +7,6 @@ use libstardustxr::fusion::flex::FlexBuffable;
use libstardustxr::{flex_to_quat, flex_to_vec3};
use parking_lot::Mutex;
use portable_atomic::AtomicF32;
use rccell::RcCell;
use std::ops::Deref;
use std::rc::Rc;
use std::sync::atomic::Ordering;
@@ -55,85 +54,87 @@ pub trait FieldTrait {
.transform_point3a(self.local_closest_point(local_p, r))
}
fn add_field_methods(&self, node: &RcCell<Node>) {
node.borrow_mut()
.add_local_method("distance", |node, calling_client, data| {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
let reference_space_path = flex_vec.idx(0).as_str();
let reference_space = calling_client
.get_scenegraph()
.get_node(reference_space_path)
.ok_or_else(|| anyhow!("Reference space node does not exist"))?
.borrow()
.spatial
.as_ref()
.ok_or_else(|| anyhow!("Reference space node does not have a spatial"))?
.clone();
let point =
flex_to_vec3!(flex_vec.idx(1)).ok_or_else(|| anyhow!("Point is invalid"))?;
let field = node
.field
.as_ref()
.ok_or_else(|| anyhow!("Node does not have a field!"))?;
let distance = field.distance(reference_space.as_ref(), point.into());
Ok(FlexBuffable::from(distance).build_singleton())
});
node.borrow_mut()
.add_local_method("normal", |node, calling_client, data| {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
let reference_space_path = flex_vec.idx(0).as_str();
let reference_space = calling_client
.get_scenegraph()
.get_node(reference_space_path)
.ok_or_else(|| anyhow!("Reference space node does not exist"))?
.borrow()
.spatial
.as_ref()
.ok_or_else(|| anyhow!("Reference space node does not have a spatial"))?
.clone();
let point =
flex_to_vec3!(flex_vec.idx(1)).ok_or_else(|| anyhow!("Point is invalid"))?;
let field = node
.field
.as_ref()
.ok_or_else(|| anyhow!("Node does not have a field!"))?;
let normal = field.normal(reference_space.as_ref(), point.into(), 0.001_f32);
Ok(FlexBuffable::from(mint::Vector3::from(normal)).build_singleton())
});
node.borrow_mut()
.add_local_method("closest_point", |node, calling_client, data| {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
let reference_space_path = flex_vec.idx(0).as_str();
let reference_space = calling_client
.get_scenegraph()
.get_node(reference_space_path)
.ok_or_else(|| anyhow!("Reference space node does not exist"))?
.borrow()
.spatial
.as_ref()
.ok_or_else(|| anyhow!("Reference space node does not have a spatial"))?
.clone();
let point =
flex_to_vec3!(flex_vec.idx(1)).ok_or_else(|| anyhow!("Point is invalid"))?;
let field = node
.field
.as_ref()
.ok_or_else(|| anyhow!("Node does not have a field!"))?;
let closest_point =
field.closest_point(reference_space.as_ref(), point.into(), 0.001_f32);
Ok(FlexBuffable::from(mint::Vector3::from(closest_point)).build_singleton())
});
fn add_field_methods(&self, node: &Arc<Node>) {
node.add_local_method("distance", field_distance_flex);
node.add_local_method("normal", field_normal_flex);
node.add_local_method("closest_point", field_closest_point_flex);
}
fn spatial_ref(&self) -> &Spatial;
}
fn field_distance_flex(node: &Node, calling_client: Rc<Client>, data: &[u8]) -> Result<Vec<u8>> {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
let reference_space_path = flex_vec.idx(0).as_str();
let reference_space = calling_client
.scenegraph
.get_node(reference_space_path)
.ok_or_else(|| anyhow!("Reference space node does not exist"))?
.spatial
.read()
.as_ref()
.ok_or_else(|| anyhow!("Reference space node does not have a spatial"))?
.clone();
let point = flex_to_vec3!(flex_vec.idx(1)).ok_or_else(|| anyhow!("Point is invalid"))?;
let distance = node
.field
.read()
.as_ref()
.unwrap()
.distance(reference_space.as_ref(), point.into());
Ok(FlexBuffable::from(distance).build_singleton())
}
fn field_normal_flex(node: &Node, calling_client: Rc<Client>, data: &[u8]) -> Result<Vec<u8>> {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
let reference_space_path = flex_vec.idx(0).as_str();
let reference_space = calling_client
.scenegraph
.get_node(reference_space_path)
.ok_or_else(|| anyhow!("Reference space node does not exist"))?
.spatial
.read()
.as_ref()
.ok_or_else(|| anyhow!("Reference space node does not have a spatial"))?
.clone();
let point = flex_to_vec3!(flex_vec.idx(1)).ok_or_else(|| anyhow!("Point is invalid"))?;
let normal = node.field.read().as_ref().unwrap().normal(
reference_space.as_ref(),
point.into(),
0.001_f32,
);
Ok(FlexBuffable::from(mint::Vector3::from(normal)).build_singleton())
}
fn field_closest_point_flex(
node: &Node,
calling_client: Rc<Client>,
data: &[u8],
) -> Result<Vec<u8>> {
let root = flexbuffers::Reader::get_root(data)?;
let flex_vec = root.get_vector()?;
let reference_space_path = flex_vec.idx(0).as_str();
let reference_space = calling_client
.scenegraph
.get_node(reference_space_path)
.ok_or_else(|| anyhow!("Reference space node does not exist"))?
.spatial
.read()
.as_ref()
.ok_or_else(|| anyhow!("Reference space node does not have a spatial"))?
.clone();
let point = flex_to_vec3!(flex_vec.idx(1)).ok_or_else(|| anyhow!("Point is invalid"))?;
let closest_point = node.field.read().as_ref().unwrap().closest_point(
reference_space.as_ref(),
point.into(),
0.001_f32,
);
Ok(FlexBuffable::from(mint::Vector3::from(closest_point)).build_singleton())
}
pub enum Field {
Box(BoxField),
Cylinder(CylinderField),
@@ -157,23 +158,22 @@ pub struct BoxField {
}
impl BoxField {
pub fn add_to(node: &RcCell<Node>, size: Vec3) -> Result<()> {
pub fn add_to(node: &Arc<Node>, size: Vec3) -> Result<()> {
ensure!(
node.borrow().spatial.is_some(),
node.spatial.read().is_some(),
"Internal: Node does not have a spatial attached!"
);
ensure!(
node.borrow().field.is_none(),
node.field.read().is_none(),
"Internal: Node already has a field attached!"
);
let box_field = BoxField {
space: node.borrow().spatial.as_ref().unwrap().clone(),
space: node.spatial.read().as_ref().unwrap().clone(),
size: Mutex::new(size),
};
box_field.add_field_methods(node);
node.borrow_mut()
.add_local_signal("setSize", BoxField::set_size_flex);
node.borrow_mut().field = Some(Arc::new(Field::Box(box_field)));
node.add_local_signal("setSize", BoxField::set_size_flex);
*node.field.write() = Some(Arc::new(Field::Box(box_field)));
Ok(())
}
@@ -184,11 +184,7 @@ impl BoxField {
pub fn set_size_flex(node: &Node, _calling_client: Rc<Client>, data: &[u8]) -> Result<()> {
let root = flexbuffers::Reader::get_root(data)?;
let size = flex_to_vec3!(root).ok_or_else(|| anyhow!("Size is invalid"))?;
let field = node
.field
.as_ref()
.ok_or_else(|| anyhow!("Node does not have a field"))?;
if let Field::Box(box_field) = field.as_ref() {
if let Field::Box(box_field) = node.field.read().as_ref().unwrap().as_ref() {
box_field.set_size(size.into());
}
Ok(())
@@ -218,24 +214,23 @@ pub struct CylinderField {
}
impl CylinderField {
pub fn add_to(node: &RcCell<Node>, length: f32, radius: f32) -> Result<()> {
pub fn add_to(node: &Arc<Node>, length: f32, radius: f32) -> Result<()> {
ensure!(
node.borrow().spatial.is_some(),
node.spatial.read().is_some(),
"Internal: Node does not have a spatial attached!"
);
ensure!(
node.borrow().field.is_none(),
node.field.read().is_none(),
"Internal: Node already has a field attached!"
);
let cylinder_field = CylinderField {
space: node.borrow().spatial.as_ref().unwrap().clone(),
space: node.spatial.read().as_ref().unwrap().clone(),
length: AtomicF32::new(length),
radius: AtomicF32::new(radius),
};
cylinder_field.add_field_methods(node);
node.borrow_mut()
.add_local_signal("setSize", CylinderField::set_size_flex);
node.borrow_mut().field = Some(Arc::new(Field::Cylinder(cylinder_field)));
node.add_local_signal("setSize", CylinderField::set_size_flex);
*node.field.write() = Some(Arc::new(Field::Cylinder(cylinder_field)));
Ok(())
}
@@ -249,11 +244,7 @@ impl CylinderField {
let flex_vec = root.get_vector()?;
let length = flex_vec.idx(0).as_f32();
let radius = flex_vec.idx(1).as_f32();
let field = node
.field
.as_ref()
.ok_or_else(|| anyhow!("Node does not have a field"))?;
if let Field::Cylinder(cylinder_field) = field.as_ref() {
if let Field::Cylinder(cylinder_field) = node.field.read().as_ref().unwrap().as_ref() {
cylinder_field.set_size(length, radius);
}
Ok(())
@@ -283,23 +274,22 @@ pub struct SphereField {
}
impl SphereField {
pub fn add_to(node: &RcCell<Node>, radius: f32) -> Result<()> {
pub fn add_to(node: &Arc<Node>, radius: f32) -> Result<()> {
ensure!(
node.borrow().spatial.is_some(),
node.spatial.read().is_some(),
"Internal: Node does not have a spatial attached!"
);
ensure!(
node.borrow().field.is_none(),
node.field.read().is_none(),
"Internal: Node already has a field attached!"
);
let sphere_field = SphereField {
space: node.borrow().spatial.as_ref().unwrap().clone(),
space: node.spatial.read().as_ref().unwrap().clone(),
radius: AtomicF32::new(radius),
};
sphere_field.add_field_methods(node);
node.borrow_mut()
.add_local_signal("setRadius", SphereField::set_radius_flex);
node.borrow_mut().field = Some(Arc::new(Field::Sphere(sphere_field)));
node.add_local_signal("setRadius", SphereField::set_radius_flex);
*node.field.write() = Some(Arc::new(Field::Sphere(sphere_field)));
Ok(())
}
@@ -309,11 +299,7 @@ impl SphereField {
pub fn set_radius_flex(node: &Node, _calling_client: Rc<Client>, data: &[u8]) -> Result<()> {
let root = flexbuffers::Reader::get_root(data)?;
let field = node
.field
.as_ref()
.ok_or_else(|| anyhow!("Node does not have a field"))?;
if let Field::Sphere(sphere_field) = field.as_ref() {
if let Field::Sphere(sphere_field) = node.field.read().as_ref().unwrap().as_ref() {
sphere_field.set_radius(root.as_f32());
}
Ok(())
@@ -336,11 +322,11 @@ impl FieldTrait for SphereField {
}
pub fn create_interface(client: Rc<Client>) {
let mut node = Node::create("", "field", false);
let node = Node::create("", "field", false);
node.add_local_signal("createBoxField", create_box_field_flex);
node.add_local_signal("createCylinderField", create_cylinder_field_flex);
node.add_local_signal("createSphereField", create_sphere_field_flex);
client.get_scenegraph().add_node(node);
client.scenegraph.add_node(node);
}
pub fn create_box_field_flex(_node: &Node, calling_client: Rc<Client>, data: &[u8]) -> Result<()> {
@@ -348,9 +334,9 @@ pub fn create_box_field_flex(_node: &Node, calling_client: Rc<Client>, data: &[u
let flex_vec = root.get_vector()?;
let node = Node::create("/field", flex_vec.idx(0).get_str()?, true);
let parent = calling_client
.get_scenegraph()
.scenegraph
.get_node(flex_vec.idx(1).as_str())
.and_then(|node| node.borrow().spatial.clone());
.and_then(|node| node.spatial.read().clone());
let transform = Mat4::from_rotation_translation(
flex_to_quat!(flex_vec.idx(3))
.ok_or_else(|| anyhow!("Rotation not found"))?
@@ -360,7 +346,7 @@ pub fn create_box_field_flex(_node: &Node, calling_client: Rc<Client>, data: &[u
.into(),
);
let size = flex_to_vec3!(flex_vec.idx(4)).ok_or_else(|| anyhow!("Size invalid"))?;
let node_rc = calling_client.get_scenegraph().add_node(node);
let node_rc = calling_client.scenegraph.add_node(node);
Spatial::add_to(&node_rc, parent, transform)?;
BoxField::add_to(&node_rc, size.into())?;
Ok(())
@@ -375,9 +361,9 @@ pub fn create_cylinder_field_flex(
let flex_vec = root.get_vector()?;
let node = Node::create("/field", flex_vec.idx(0).get_str()?, true);
let parent = calling_client
.get_scenegraph()
.scenegraph
.get_node(flex_vec.idx(1).as_str())
.and_then(|node| node.borrow().spatial.clone());
.and_then(|node| node.spatial.read().clone());
let transform = Mat4::from_rotation_translation(
flex_to_quat!(flex_vec.idx(3))
.ok_or_else(|| anyhow!("Rotation not found"))?
@@ -388,7 +374,7 @@ pub fn create_cylinder_field_flex(
);
let length = flex_vec.idx(0).as_f32();
let radius = flex_vec.idx(1).as_f32();
let node_rc = calling_client.get_scenegraph().add_node(node);
let node_rc = calling_client.scenegraph.add_node(node);
Spatial::add_to(&node_rc, parent, transform)?;
CylinderField::add_to(&node_rc, length, radius)?;
Ok(())
@@ -403,15 +389,15 @@ pub fn create_sphere_field_flex(
let flex_vec = root.get_vector()?;
let node = Node::create("/field", flex_vec.idx(0).get_str()?, true);
let parent = calling_client
.get_scenegraph()
.scenegraph
.get_node(flex_vec.idx(1).as_str())
.and_then(|node| node.borrow().spatial.clone());
.and_then(|node| node.spatial.read().clone());
let transform = Mat4::from_translation(
flex_to_vec3!(flex_vec.idx(2))
.ok_or_else(|| anyhow!("Position not found"))?
.into(),
);
let node_rc = calling_client.get_scenegraph().add_node(node);
let node_rc = calling_client.scenegraph.add_node(node);
Spatial::add_to(&node_rc, parent, transform)?;
SphereField::add_to(&node_rc, flex_vec.idx(3).as_f32())?;
Ok(())