Implement basic variation support
CI / cargo fmt (push) Successful in 24s
CI / cargo test (push) Failing after 26s
CI / cargo test (GPU) (push) Successful in 17m57s

This commit is contained in:
2026-06-27 15:11:23 -04:00
parent df747855b6
commit 6671475c75
4 changed files with 127 additions and 8 deletions
+18 -4
View File
@@ -13,6 +13,7 @@
//! This algorithm is also known as the ["chaos game"](https://en.wikipedia.org/wiki/Chaos_game),
//! and it forms the basic system for producing images.
use crate::transform::Transform;
use crate::variation::Variation;
use glam::{Vec2, vec2};
use rand::distr::{Distribution, StandardUniform};
use rand::{Rng, RngExt};
@@ -37,6 +38,7 @@ pub fn step_chaos_game<R: Rng>(
rng: &mut R,
transforms: &[Transform],
weights: &[f32],
variations: &[Variation],
) -> (Vec2, u32) {
let mut choice_weight = rng.sample::<f32, _>(StandardUniform);
let mut transform_index: u32 = 0;
@@ -51,7 +53,7 @@ pub fn step_chaos_game<R: Rng>(
}
(
transforms[transform_index as usize].transform_point(point),
transforms[transform_index as usize].transform_point(rng, variations, point),
transform_index,
)
}
@@ -65,17 +67,24 @@ pub struct ChaosGame<'a, R: Rng> {
rng: &'a mut R,
transforms: &'a [Transform],
weights: &'a [f32],
variations: &'a [Variation],
}
impl<'a, R: Rng> ChaosGame<'a, R> {
/// Create a new chaos game iterator
pub fn new(rng: &'a mut R, transforms: &'a [Transform], weights: &'a [f32]) -> Self {
pub fn new(
rng: &'a mut R,
transforms: &'a [Transform],
weights: &'a [f32],
variations: &'a [Variation],
) -> Self {
let current_point = vec2(rng.sample(BiUnit), rng.sample(BiUnit));
ChaosGame {
current_point,
rng,
transforms,
weights,
variations,
}
}
}
@@ -84,8 +93,13 @@ impl<'a, R: Rng> Iterator for ChaosGame<'a, R> {
type Item = Vec2;
fn next(&mut self) -> Option<Self::Item> {
let (next_point, _) =
step_chaos_game(self.current_point, self.rng, self.transforms, self.weights);
let (next_point, _) = step_chaos_game(
self.current_point,
self.rng,
self.transforms,
self.weights,
self.variations,
);
self.current_point = next_point;
Some(next_point)