Archived
1
0
Fork 0

Noob steps

This commit is contained in:
Mo 2023-07-06 19:42:19 +02:00
parent 04d31e992a
commit 973647e817

View file

@ -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 nalgebra::base::{Matrix3, Vector3};
use rand::{distributions::Uniform, prelude::Distribution, rngs::SmallRng, Rng, SeedableRng}; use rand::{distributions::Uniform, prelude::Distribution, rngs::SmallRng, Rng, SeedableRng};
use std::collections::HashSet; use std::collections::HashSet;
@ -52,11 +59,14 @@ impl Transformations {
} }
} }
#[derive(Resource)]
struct Walk { struct Walk {
points: Vec<Point>, points: Vec<Point>,
new_points: Vec<Point>, new_points: Vec<Point>,
pivot_dist: Uniform<usize>, pivot_dist: Uniform<usize>,
set: HashSet<Point>, set: HashSet<Point>,
rng: SmallRng,
transformations: Transformations,
} }
impl Walk { impl Walk {
@ -71,22 +81,28 @@ impl Walk {
let set = HashSet::with_capacity(n); let set = HashSet::with_capacity(n);
let rng = SmallRng::seed_from_u64(42);
let transformations = Transformations::new();
Self { Self {
points, points,
new_points, new_points,
pivot_dist, pivot_dist,
set, set,
rng,
transformations,
} }
} }
pub fn pivot(&mut self, rng: &mut impl Rng, transformations: &Transformations) { pub fn pivot(&mut self) {
loop { loop {
self.set.clear(); self.set.clear();
self.new_points.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 (s1, s2) = self.points.split_at_mut(pivot_ind + 1);
let pivot = unsafe { s1.get_unchecked(pivot_ind) }; let pivot = unsafe { s1.get_unchecked(pivot_ind) };
@ -110,15 +126,49 @@ impl Walk {
} }
} }
fn main() { fn setup(mut commands: Commands) {
let transformations = Transformations::new(); commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(80.0, 80.0, 80.0)
let n = 1_000; .looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
..Default::default()
let seed = 42; });
let mut rng = SmallRng::seed_from_u64(seed); }
let mut walk = Walk::new(n); fn pivot(
mut commands: Commands,
walk.pivot(&mut rng, &transformations); mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut walk: ResMut<Walk>,
) {
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::<Vec<_>>(),
);
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();
} }