diff --git a/src/main.rs b/src/main.rs index 3d4e241..d80622e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,10 @@ +use bevy::{ + prelude::{ + App, Assets, Camera3dBundle, Color, Commands, DefaultPlugins, Mesh, PbrBundle, ResMut, + Resource, StandardMaterial, Transform, Vec3, + }, + render::render_resource::PrimitiveTopology, +}; use nalgebra::base::{Matrix3, Vector3}; use rand::{distributions::Uniform, prelude::Distribution, rngs::SmallRng, Rng, SeedableRng}; use std::collections::HashSet; @@ -52,11 +59,14 @@ impl Transformations { } } +#[derive(Resource)] struct Walk { points: Vec, new_points: Vec, pivot_dist: Uniform, set: HashSet, + rng: SmallRng, + transformations: Transformations, } impl Walk { @@ -71,22 +81,28 @@ impl Walk { let set = HashSet::with_capacity(n); + let rng = SmallRng::seed_from_u64(42); + + let transformations = Transformations::new(); + Self { points, new_points, pivot_dist, set, + rng, + transformations, } } - pub fn pivot(&mut self, rng: &mut impl Rng, transformations: &Transformations) { + pub fn pivot(&mut self) { loop { self.set.clear(); self.new_points.clear(); - let m = transformations.rand(rng); + let m = self.transformations.rand(&mut self.rng); - let pivot_ind = self.pivot_dist.sample(rng); + let pivot_ind = self.pivot_dist.sample(&mut self.rng); let (s1, s2) = self.points.split_at_mut(pivot_ind + 1); let pivot = unsafe { s1.get_unchecked(pivot_ind) }; @@ -110,15 +126,49 @@ impl Walk { } } -fn main() { - let transformations = Transformations::new(); - - let n = 1_000; - - let seed = 42; - let mut rng = SmallRng::seed_from_u64(seed); - - let mut walk = Walk::new(n); - - walk.pivot(&mut rng, &transformations); +fn setup(mut commands: Commands) { + commands.spawn(Camera3dBundle { + transform: Transform::from_xyz(80.0, 80.0, 80.0) + .looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y), + ..Default::default() + }); +} + +fn pivot( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, + mut walk: ResMut, +) { + let blue = materials.add(Color::BLUE.into()); + + let mut mesh = Mesh::new(PrimitiveTopology::LineStrip); + mesh.insert_attribute( + Mesh::ATTRIBUTE_POSITION, + walk.points + .iter() + .map(|p| Vec3::new(p.x as f32, p.y as f32, p.z as f32)) + .collect::>(), + ); + + commands.spawn(PbrBundle { + mesh: meshes.add(mesh), + material: blue, + ..Default::default() + }); + + walk.pivot(); +} + +fn main() { + let n = 250; + + let walk = Walk::new(n); + + App::new() + .add_plugins(DefaultPlugins) + .insert_resource(walk) + .add_startup_system(setup) + .add_system(pivot) + .run(); }