Skip to content

Commit 1845055

Browse files
committed
cosmetics
1 parent 615d022 commit 1845055

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed

src/lib.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rand::seq::SliceRandom;
21
use rand::Rng;
32

43
pub mod geo;
@@ -24,19 +23,11 @@ impl Dla {
2423
attraction_radius: u16,
2524
seeds: impl IntoIterator<Item = Vec3>,
2625
) -> Option<Self> {
27-
let mut seeds = seeds.into_iter();
26+
let cells: Octree = seeds.into_iter().collect();
2827

29-
let mut cells = Octree::new();
30-
31-
let first = seeds.next()?;
32-
cells.add(first);
33-
34-
let mut bbox = Bbox::new(first);
35-
36-
for p in seeds {
37-
cells.add(p);
38-
bbox = bbox.expand(p);
39-
}
28+
let mut cells_it = cells.iter();
29+
let first_p = cells_it.next()?;
30+
let bbox = cells_it.fold(Bbox::new(*first_p), |b, p| b.expand(*p));
4031

4132
Some(Dla {
4233
cells,
@@ -91,14 +82,13 @@ impl Dla {
9182
break;
9283
}
9384
None => {
94-
let d = Vec3::new(
95-
rng.gen_range(1, self.attraction_radius / 2)
96-
* *[-1, 1].choose(rng).unwrap(),
97-
rng.gen_range(1, self.attraction_radius / 2)
98-
* *[-1, 1].choose(rng).unwrap(),
99-
rng.gen_range(1, self.attraction_radius / 2)
100-
* *[-1, 1].choose(rng).unwrap(),
101-
);
85+
let mut motion = || {
86+
let d = if rng.gen::<f32>() < 0.5 { -1 } else { 1 };
87+
rng.gen_range(1, self.attraction_radius / 2) * d
88+
};
89+
90+
let d = Vec3::new(motion(), motion(), motion());
91+
10292
cell = cell + d * self.attraction_radius;
10393

10494
if !spawn_bbox.contains(cell) {

src/main.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ use structopt::StructOpt;
99

1010
use dla::{Dla, Vec3};
1111

12-
pub type Rgb = [f64; 3];
13-
1412
/// Simulate 3D diffusion limited aggregation (DLA for short) and save the final
1513
/// system as a scene ready to be rendered using povray for example.
1614
#[derive(StructOpt, Debug)]
@@ -167,14 +165,14 @@ camera {{
167165
let mut cells = dla.cells().map(|cc| (cc, center.dist2(*cc))).collect::<Vec<_>>();
168166
cells.sort_by_key(|(_, d)| *d);
169167

170-
let d = cells.last().expect("empty dla, cannot happen since it should be seeded").1;
168+
let max_d = cells.last().expect("empty dla, cannot happen since it should be seeded").1;
171169
let mut cells = cells.into_iter();
172170

173171
let gradients = 3;
174172
let n = gradients * 2;
175173
for i in 0..n {
176174
writeln!(out, "\nunion {{")?;
177-
for (p, _) in cells.by_ref().take_while(|(_, dd)| *dd <= (i + 1) * d / n) {
175+
for (p, _) in cells.by_ref().take_while(|(_, dd)| *dd <= (i + 1) * max_d / n) {
178176
writeln!(out, " sphere {{ <{}, {}, {}>, 1 }}", p.x, p.y, p.z)?;
179177
}
180178

@@ -284,6 +282,7 @@ impl Scene {
284282
let scene_bbox = dla.bbox();
285283
let scene_dimensions = scene_bbox.dimensions();
286284
let away_dist = scene_dimensions.x.min(scene_dimensions.y).min(scene_dimensions.z);
285+
287286
let camera = Camera {
288287
position: Vec3::new(
289288
scene_bbox.center().x - away_dist,
@@ -292,8 +291,8 @@ impl Scene {
292291
),
293292
target: Vec3::new(0, 0, 0),
294293
};
295-
let mut lights = vec![];
296294

295+
let mut lights = vec![];
297296
let mut add_light = |pt: Vec3, intensity| {
298297
let position = pt + (pt - scene_bbox.center()).normalized() * away_dist;
299298
lights.push(Light { position, intensity })
@@ -342,6 +341,7 @@ impl Scene {
342341

343342
impl std::str::FromStr for SceneFormat {
344343
type Err = String;
344+
345345
fn from_str(s: &str) -> Result<Self, Self::Err> {
346346
match s {
347347
"povray" => Ok(SceneFormat::Povray),

src/octree.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,6 @@ pub struct Octree {
1717
enum Node {
1818
Branch { children: Box<[Node; 8]>, bbox: Bbox },
1919
Leaf { points: HashSet<Vec3>, bbox: Bbox },
20-
//
21-
// TODO: consider adding a Full{bbox: Bbox} variant which could be constructed when we know
22-
// that a bbox is completely full (remember we're living in a finite space since we're using
23-
// integer coordinates). This variant would drastically improve query time and also space
24-
// requirements.
25-
//
2620
}
2721

2822
#[derive(Debug)]
@@ -332,6 +326,12 @@ fn partition_pt(p: Vec3, c: Vec3) -> usize {
332326
}
333327
}
334328

329+
impl Default for Octree {
330+
fn default() -> Self {
331+
Octree::new()
332+
}
333+
}
334+
335335
#[cfg(test)]
336336
mod tests {
337337
use super::*;

0 commit comments

Comments
 (0)