From 88bddce9542c10b26c8692433169446350ed62a9 Mon Sep 17 00:00:00 2001 From: rezural Date: Sun, 20 Dec 2020 00:19:21 +1100 Subject: harness make harness a public mod, make harness.state public --- src_testbed/harness/mod.rs | 205 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 src_testbed/harness/mod.rs (limited to 'src_testbed/harness/mod.rs') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs new file mode 100644 index 0000000..f8d3571 --- /dev/null +++ b/src_testbed/harness/mod.rs @@ -0,0 +1,205 @@ +use crate::physics::{PhysicsEvents, PhysicsState}; +use rapier::math::Vector; +use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; +use rapier::dynamics::{ + IntegrationParameters, JointSet, RigidBodySet, +}; +use rapier::geometry::{ + BroadPhase, ColliderSet, NarrowPhase, +}; + +// #[derive(PartialEq)] +// pub enum RunState { +// Initialized, +// Running, +// Pausing, +// Paused, +// Finished, +// } + +pub struct HarnessState { + #[cfg(feature = "parallel")] + pub thread_pool: rapier::rayon::ThreadPool, + pub timestep_id: usize, + // pub run_state: RunState, +} + + +pub struct Harness { + physics: PhysicsState, + max_steps: usize, + callbacks: Callbacks, + // plugins: Vec>, + time: f32, + events: PhysicsEvents, + event_handler: ChannelEventCollector, + pub state: HarnessState, +} + +type Callbacks = +Vec>; + +#[allow(dead_code)] +impl Harness { + pub fn new_empty() -> Self { + #[cfg(feature = "parallel")] + let num_threads = num_cpus::get_physical(); + + #[cfg(feature = "parallel")] + let thread_pool = rapier::rayon::ThreadPoolBuilder::new() + .num_threads(num_threads) + .build() + .unwrap(); + + let contact_channel = crossbeam::channel::unbounded(); + let proximity_channel = crossbeam::channel::unbounded(); + let event_handler = ChannelEventCollector::new(proximity_channel.0, contact_channel.0); + let events = PhysicsEvents { + contact_events: contact_channel.1, + proximity_events: proximity_channel.1, + }; + let physics = PhysicsState::new(); + let state = HarnessState { + #[cfg(feature = "parallel")] + thread_pool, + timestep_id: 0, + // run_state: RunState::Initialized + }; + + Self { + physics, + max_steps: 1000, + callbacks: Vec::new(), + time: 0.0, + events, + event_handler, + state, + } + } + + pub fn new(bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet) -> Self { + let mut res = Self::new_empty(); + res.set_world(bodies, colliders, joints); + res + } + + // pub fn pause_at_next_step(&mut self) { + // if self.state.run_state == RunState::Running { + // self.state.run_state = RunState::Pausing + // } + // } + + pub fn set_max_steps(&mut self, max_steps: usize) { + self.max_steps = max_steps + } + + pub fn integration_parameters_mut(&mut self) -> &mut IntegrationParameters { + &mut self.physics.integration_parameters + } + + pub fn physics_state_mut(&mut self) -> &mut PhysicsState { + &mut self.physics + } + + pub fn set_world(&mut self, bodies: RigidBodySet, colliders: ColliderSet, joints: JointSet) { + self.set_world_with_gravity(bodies, colliders, joints, Vector::y() * -9.81) + } + + pub fn set_world_with_gravity( + &mut self, + bodies: RigidBodySet, + colliders: ColliderSet, + joints: JointSet, + gravity: Vector, + ) { + // println!("Num bodies: {}", bodies.len()); + // println!("Num joints: {}", joints.len()); + self.physics.gravity = gravity; + self.physics.bodies = bodies; + self.physics.colliders = colliders; + self.physics.joints = joints; + self.physics.broad_phase = BroadPhase::new(); + self.physics.narrow_phase = NarrowPhase::new(); + self.time = 0.0; + // self.state.timestep_id = 0; + // self.state.highlighted_body = None; + self.physics.query_pipeline = QueryPipeline::new(); + self.physics.pipeline = PhysicsPipeline::new(); + self.physics.pipeline.counters.enable(); + + } + + // fn clear(&mut self) { + // self.callbacks.clear(); + // + // for plugin in &mut self.plugins { + // plugin.clear_graphics(window); + // } + // + // self.plugins.clear(); + // } + + // pub fn add_plugin(&mut self, plugin: impl TestbedPlugin + 'static) { + // self.plugins.push(Box::new(plugin)); + // } + + pub fn add_callback< + F: FnMut(&mut PhysicsState, &PhysicsEvents, f32) + 'static, + >( + &mut self, + callback: F, + ) { + self.callbacks.push(Box::new(callback)); + } + + pub fn step(&mut self) { + #[cfg(feature = "parallel")] + { + let physics = &mut self.physics; + let event_handler = &self.event_handler; + self.state.thread_pool.install(|| { + physics.pipeline.step( + &physics.gravity, + &physics.integration_parameters, + &mut physics.broad_phase, + &mut physics.narrow_phase, + &mut physics.bodies, + &mut physics.colliders, + &mut physics.joints, + None, + None, + event_handler, + ); + }); + } + + #[cfg(not(feature = "parallel"))] + self.physics.pipeline.step( + &self.physics.gravity, + &self.physics.integration_parameters, + &mut self.physics.broad_phase, + &mut self.physics.narrow_phase, + &mut self.physics.bodies, + &mut self.physics.colliders, + &mut self.physics.joints, + None, + None, + &self.event_handler, + ); + + self.physics + .query_pipeline + .update(&self.physics.bodies, &self.physics.colliders); + + // for plugin in &mut self.plugins { + // plugin.step(&mut self.physics) + // } + } + + pub fn run(&mut self) { + for _ in 0..self.max_steps { + self.step(); + self.state.timestep_id += 1; + } + } +} \ No newline at end of file -- cgit From 315b84a85e67077d5e5232f3c3f4f3d80b85db81 Mon Sep 17 00:00:00 2001 From: rezural Date: Sun, 20 Dec 2020 04:27:20 +1100 Subject: add plugins cargo fmt --- src_testbed/harness/mod.rs | 107 +++++++++++++++++++++++---------------------- 1 file changed, 54 insertions(+), 53 deletions(-) (limited to 'src_testbed/harness/mod.rs') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index f8d3571..af9a45d 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -1,12 +1,9 @@ use crate::physics::{PhysicsEvents, PhysicsState}; +use crate::HarnessPlugin; +use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet}; +use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase}; use rapier::math::Vector; use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; -use rapier::dynamics::{ - IntegrationParameters, JointSet, RigidBodySet, -}; -use rapier::geometry::{ - BroadPhase, ColliderSet, NarrowPhase, -}; // #[derive(PartialEq)] // pub enum RunState { @@ -24,33 +21,34 @@ pub struct HarnessState { // pub run_state: RunState, } - pub struct Harness { physics: PhysicsState, max_steps: usize, callbacks: Callbacks, - // plugins: Vec>, + plugins: Vec>, time: f32, events: PhysicsEvents, event_handler: ChannelEventCollector, pub state: HarnessState, } -type Callbacks = -Vec>; +type Callbacks = Vec>; #[allow(dead_code)] impl Harness { pub fn new_empty() -> Self { #[cfg(feature = "parallel")] - let num_threads = num_cpus::get_physical(); + let num_threads = num_cpus::get_physical(); #[cfg(feature = "parallel")] - let thread_pool = rapier::rayon::ThreadPoolBuilder::new() + let thread_pool = rapier::rayon::ThreadPoolBuilder::new() .num_threads(num_threads) .build() .unwrap(); + // #[cfg(feature = "parallel")] + // println!("Rapier Harness Parallel, num_threads: {}", num_threads); + let contact_channel = crossbeam::channel::unbounded(); let proximity_channel = crossbeam::channel::unbounded(); let event_handler = ChannelEventCollector::new(proximity_channel.0, contact_channel.0); @@ -70,6 +68,7 @@ impl Harness { physics, max_steps: 1000, callbacks: Vec::new(), + plugins: Vec::new(), time: 0.0, events, event_handler, @@ -126,7 +125,6 @@ impl Harness { self.physics.query_pipeline = QueryPipeline::new(); self.physics.pipeline = PhysicsPipeline::new(); self.physics.pipeline.counters.enable(); - } // fn clear(&mut self) { @@ -139,13 +137,11 @@ impl Harness { // self.plugins.clear(); // } - // pub fn add_plugin(&mut self, plugin: impl TestbedPlugin + 'static) { - // self.plugins.push(Box::new(plugin)); - // } + pub fn add_plugin(&mut self, plugin: impl HarnessPlugin + 'static) { + self.plugins.push(Box::new(plugin)); + } - pub fn add_callback< - F: FnMut(&mut PhysicsState, &PhysicsEvents, f32) + 'static, - >( + pub fn add_callback( &mut self, callback: F, ) { @@ -154,46 +150,51 @@ impl Harness { pub fn step(&mut self) { #[cfg(feature = "parallel")] - { - let physics = &mut self.physics; - let event_handler = &self.event_handler; - self.state.thread_pool.install(|| { - physics.pipeline.step( - &physics.gravity, - &physics.integration_parameters, - &mut physics.broad_phase, - &mut physics.narrow_phase, - &mut physics.bodies, - &mut physics.colliders, - &mut physics.joints, - None, - None, - event_handler, - ); - }); - } + { + let physics = &mut self.physics; + let event_handler = &self.event_handler; + self.state.thread_pool.install(|| { + physics.pipeline.step( + &physics.gravity, + &physics.integration_parameters, + &mut physics.broad_phase, + &mut physics.narrow_phase, + &mut physics.bodies, + &mut physics.colliders, + &mut physics.joints, + None, + None, + event_handler, + ); + }); + } #[cfg(not(feature = "parallel"))] - self.physics.pipeline.step( - &self.physics.gravity, - &self.physics.integration_parameters, - &mut self.physics.broad_phase, - &mut self.physics.narrow_phase, - &mut self.physics.bodies, - &mut self.physics.colliders, - &mut self.physics.joints, - None, - None, - &self.event_handler, - ); + self.physics.pipeline.step( + &self.physics.gravity, + &self.physics.integration_parameters, + &mut self.physics.broad_phase, + &mut self.physics.narrow_phase, + &mut self.physics.bodies, + &mut self.physics.colliders, + &mut self.physics.joints, + None, + None, + &self.event_handler, + ); self.physics .query_pipeline .update(&self.physics.bodies, &self.physics.colliders); - // for plugin in &mut self.plugins { - // plugin.step(&mut self.physics) - // } + for plugin in &mut self.plugins { + plugin.step(&mut self.physics) + } + + //FIXME: not sure if this makes sense here, basically copied from Testbed + for plugin in &mut self.plugins { + plugin.run_callbacks(&mut self.physics, self.time) + } } pub fn run(&mut self) { @@ -202,4 +203,4 @@ impl Harness { self.state.timestep_id += 1; } } -} \ No newline at end of file +} -- cgit From be07227e9455e1549f1fee2f278ab2a266c5c3c2 Mon Sep 17 00:00:00 2001 From: rezural Date: Sun, 20 Dec 2020 23:58:50 +1100 Subject: add HarnessState to callbacks, move HarnessPlugin to src_testbed/harness/plugin --- src_testbed/harness/mod.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) (limited to 'src_testbed/harness/mod.rs') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index af9a45d..7d6a939 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -1,10 +1,13 @@ use crate::physics::{PhysicsEvents, PhysicsState}; -use crate::HarnessPlugin; use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet}; use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase}; use rapier::math::Vector; use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; +pub mod plugin; + +use plugin::HarnessPlugin; + // #[derive(PartialEq)] // pub enum RunState { // Initialized, @@ -32,7 +35,7 @@ pub struct Harness { pub state: HarnessState, } -type Callbacks = Vec>; +type Callbacks = Vec>; #[allow(dead_code)] impl Harness { @@ -141,7 +144,8 @@ impl Harness { self.plugins.push(Box::new(plugin)); } - pub fn add_callback( + // type StepCallback = FnMut(&mut PhysicsState, &PhysicsEvents, f32); + pub fn add_callback( &mut self, callback: F, ) { @@ -191,10 +195,22 @@ impl Harness { plugin.step(&mut self.physics) } - //FIXME: not sure if this makes sense here, basically copied from Testbed + for f in &mut self.callbacks { + f( + &mut self.physics, + &self.events, + &self.state, + self.time, + ) + } + for plugin in &mut self.plugins { - plugin.run_callbacks(&mut self.physics, self.time) + plugin.run_callbacks(&mut self.physics, &self.events,&self.state, self.time) } + + self.events.poll_all(); + + self.time += self.physics.integration_parameters.dt(); } pub fn run(&mut self) { -- cgit From 16720918aac974a57f82c5bc0e840b36c8fa5970 Mon Sep 17 00:00:00 2001 From: rezural Date: Mon, 21 Dec 2020 04:33:25 +1100 Subject: cargo fmt --- src_testbed/harness/mod.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'src_testbed/harness/mod.rs') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 7d6a939..8ea141f 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -145,7 +145,9 @@ impl Harness { } // type StepCallback = FnMut(&mut PhysicsState, &PhysicsEvents, f32); - pub fn add_callback( + pub fn add_callback< + F: FnMut(&mut PhysicsState, &PhysicsEvents, &HarnessState, f32) + 'static, + >( &mut self, callback: F, ) { @@ -196,16 +198,11 @@ impl Harness { } for f in &mut self.callbacks { - f( - &mut self.physics, - &self.events, - &self.state, - self.time, - ) + f(&mut self.physics, &self.events, &self.state, self.time) } for plugin in &mut self.plugins { - plugin.run_callbacks(&mut self.physics, &self.events,&self.state, self.time) + plugin.run_callbacks(&mut self.physics, &self.events, &self.state, self.time) } self.events.poll_all(); -- cgit From 496f4e32588d4f5efd821e7834ddd0d7b0dd1acc Mon Sep 17 00:00:00 2001 From: rezural Date: Mon, 21 Dec 2020 14:56:05 +1100 Subject: remove some commented code --- src_testbed/harness/mod.rs | 37 ++----------------------------------- 1 file changed, 2 insertions(+), 35 deletions(-) (limited to 'src_testbed/harness/mod.rs') diff --git a/src_testbed/harness/mod.rs b/src_testbed/harness/mod.rs index 8ea141f..fa6c4c6 100644 --- a/src_testbed/harness/mod.rs +++ b/src_testbed/harness/mod.rs @@ -1,4 +1,5 @@ use crate::physics::{PhysicsEvents, PhysicsState}; +use plugin::HarnessPlugin; use rapier::dynamics::{IntegrationParameters, JointSet, RigidBodySet}; use rapier::geometry::{BroadPhase, ColliderSet, NarrowPhase}; use rapier::math::Vector; @@ -6,22 +7,10 @@ use rapier::pipeline::{ChannelEventCollector, PhysicsPipeline, QueryPipeline}; pub mod plugin; -use plugin::HarnessPlugin; - -// #[derive(PartialEq)] -// pub enum RunState { -// Initialized, -// Running, -// Pausing, -// Paused, -// Finished, -// } - pub struct HarnessState { #[cfg(feature = "parallel")] pub thread_pool: rapier::rayon::ThreadPool, pub timestep_id: usize, - // pub run_state: RunState, } pub struct Harness { @@ -49,9 +38,6 @@ impl Harness { .build() .unwrap(); - // #[cfg(feature = "parallel")] - // println!("Rapier Harness Parallel, num_threads: {}", num_threads); - let contact_channel = crossbeam::channel::unbounded(); let proximity_channel = crossbeam::channel::unbounded(); let event_handler = ChannelEventCollector::new(proximity_channel.0, contact_channel.0); @@ -64,7 +50,6 @@ impl Harness { #[cfg(feature = "parallel")] thread_pool, timestep_id: 0, - // run_state: RunState::Initialized }; Self { @@ -85,12 +70,6 @@ impl Harness { res } - // pub fn pause_at_next_step(&mut self) { - // if self.state.run_state == RunState::Running { - // self.state.run_state = RunState::Pausing - // } - // } - pub fn set_max_steps(&mut self, max_steps: usize) { self.max_steps = max_steps } @@ -123,28 +102,16 @@ impl Harness { self.physics.broad_phase = BroadPhase::new(); self.physics.narrow_phase = NarrowPhase::new(); self.time = 0.0; - // self.state.timestep_id = 0; - // self.state.highlighted_body = None; + self.state.timestep_id = 0; self.physics.query_pipeline = QueryPipeline::new(); self.physics.pipeline = PhysicsPipeline::new(); self.physics.pipeline.counters.enable(); } - // fn clear(&mut self) { - // self.callbacks.clear(); - // - // for plugin in &mut self.plugins { - // plugin.clear_graphics(window); - // } - // - // self.plugins.clear(); - // } - pub fn add_plugin(&mut self, plugin: impl HarnessPlugin + 'static) { self.plugins.push(Box::new(plugin)); } - // type StepCallback = FnMut(&mut PhysicsState, &PhysicsEvents, f32); pub fn add_callback< F: FnMut(&mut PhysicsState, &PhysicsEvents, &HarnessState, f32) + 'static, >( -- cgit