Skip to content

Commit bcf7b92

Browse files
committed
stick to upstream format
1 parent 0e3c380 commit bcf7b92

File tree

4 files changed

+116
-29
lines changed

4 files changed

+116
-29
lines changed

rustfmt.toml

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/geo.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ impl Bbox {
6262
}
6363

6464
pub fn expand(&self, p: Vec3) -> Bbox {
65-
Bbox { lower: self.lower.min(p), upper: self.upper.max(p) }
65+
Bbox {
66+
lower: self.lower.min(p),
67+
upper: self.upper.max(p),
68+
}
6669
}
6770

6871
pub fn contains(&self, p: Vec3) -> bool {
@@ -89,11 +92,17 @@ impl Bbox {
8992
}
9093

9194
pub fn scale(&self, f: i64) -> Self {
92-
Bbox { lower: self.lower * f, upper: self.upper * f }
95+
Bbox {
96+
lower: self.lower * f,
97+
upper: self.upper * f,
98+
}
9399
}
94100

95101
pub fn union(&self, b: &Bbox) -> Self {
96-
Bbox { lower: self.lower.min(b.lower), upper: self.upper.max(b.upper) }
102+
Bbox {
103+
lower: self.lower.min(b.lower),
104+
upper: self.upper.max(b.upper),
105+
}
97106
}
98107
}
99108

src/main.rs

Lines changed: 74 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,11 @@ fn main() -> io::Result<()> {
7474
let mut rng = rand::thread_rng();
7575
for i in 0..args.particles {
7676
if i % 100 == 0 {
77-
print!("\rgenerated {} particles, progress: {}%", i, i * 100 / args.particles);
77+
print!(
78+
"\rgenerated {} particles, progress: {}%",
79+
i,
80+
i * 100 / args.particles
81+
);
7882
io::stdout().flush()?;
7983
}
8084

@@ -117,7 +121,14 @@ It contains {} particles and its bounding box goes from
117121
Ok(())
118122
}
119123

120-
fn save_pov_scene(path: &Path, Scene { dla, camera, lights }: &Scene) -> io::Result<()> {
124+
fn save_pov_scene(
125+
path: &Path,
126+
Scene {
127+
dla,
128+
camera,
129+
lights,
130+
}: &Scene,
131+
) -> io::Result<()> {
121132
let path = path.with_extension("pov");
122133
let mut out = BufWriter::new(File::create(&path)?);
123134

@@ -161,17 +172,26 @@ camera {{
161172
}
162173

163174
let center = bbox.center();
164-
let mut cells = dla.cells().map(|cc| (cc, center.dist2(*cc))).collect::<Vec<_>>();
175+
let mut cells = dla
176+
.cells()
177+
.map(|cc| (cc, center.dist2(*cc)))
178+
.collect::<Vec<_>>();
165179
cells.sort_by_key(|(_, d)| *d);
166180

167-
let max_d = cells.last().expect("empty dla, cannot happen since it should be seeded").1;
181+
let max_d = cells
182+
.last()
183+
.expect("empty dla, cannot happen since it should be seeded")
184+
.1;
168185
let mut cells = cells.into_iter();
169186

170187
let gradients = 3;
171188
let n = gradients * 2;
172189
for i in 0..n {
173190
writeln!(out, "\nunion {{")?;
174-
for (p, _) in cells.by_ref().take_while(|(_, dd)| *dd <= (i + 1) * max_d / n) {
191+
for (p, _) in cells
192+
.by_ref()
193+
.take_while(|(_, dd)| *dd <= (i + 1) * max_d / n)
194+
{
175195
writeln!(out, " sphere {{ <{}, {}, {}>, 1 }}", p.x, p.y, p.z)?;
176196
}
177197

@@ -211,7 +231,14 @@ render with a command like the following
211231
Ok(())
212232
}
213233

214-
fn save_js_scene(path: &Path, Scene { dla, camera, lights }: &Scene) -> io::Result<()> {
234+
fn save_js_scene(
235+
path: &Path,
236+
Scene {
237+
dla,
238+
camera,
239+
lights,
240+
}: &Scene,
241+
) -> io::Result<()> {
215242
let path = path.with_extension("js");
216243
let mut out = BufWriter::new(File::create(&path)?);
217244

@@ -299,7 +326,10 @@ impl Scene {
299326
fn new(dla: Dla) -> Self {
300327
let scene_bbox = dla.bbox();
301328
let scene_dimensions = scene_bbox.dimensions();
302-
let away_dist = scene_dimensions.x.min(scene_dimensions.y).min(scene_dimensions.z);
329+
let away_dist = scene_dimensions
330+
.x
331+
.min(scene_dimensions.y)
332+
.min(scene_dimensions.z);
303333

304334
let camera = Camera {
305335
position: Vec3::new(
@@ -313,12 +343,19 @@ impl Scene {
313343
let mut lights = vec![];
314344
let mut add_light = |pt: Vec3, intensity| {
315345
let position = pt + (pt - scene_bbox.center()).normalized() * away_dist;
316-
lights.push(Light { position, intensity })
346+
lights.push(Light {
347+
position,
348+
intensity,
349+
})
317350
};
318351

319352
// key light
320353
add_light(
321-
Vec3::new(scene_bbox.lower().x, scene_bbox.center().y, scene_bbox.lower().z),
354+
Vec3::new(
355+
scene_bbox.lower().x,
356+
scene_bbox.center().y,
357+
scene_bbox.lower().z,
358+
),
322359
1.0,
323360
);
324361

@@ -334,26 +371,49 @@ impl Scene {
334371

335372
// fill light
336373
add_light(
337-
Vec3::new(scene_bbox.upper().x, scene_bbox.lower().y, scene_bbox.lower().z),
374+
Vec3::new(
375+
scene_bbox.upper().x,
376+
scene_bbox.lower().y,
377+
scene_bbox.lower().z,
378+
),
338379
0.75,
339380
);
340381

341382
// background light
342-
add_light(Vec3::new(scene_bbox.lower().x, scene_bbox.upper().y, scene_bbox.upper().z), 0.5);
383+
add_light(
384+
Vec3::new(
385+
scene_bbox.lower().x,
386+
scene_bbox.upper().y,
387+
scene_bbox.upper().z,
388+
),
389+
0.5,
390+
);
343391

344392
// bottom light
345393
add_light(
346-
Vec3::new(scene_bbox.center().x, scene_bbox.lower().y, scene_bbox.center().z),
394+
Vec3::new(
395+
scene_bbox.center().x,
396+
scene_bbox.lower().y,
397+
scene_bbox.center().z,
398+
),
347399
0.75,
348400
);
349401

350402
// top light
351403
add_light(
352-
Vec3::new(scene_bbox.center().x, scene_bbox.upper().y, scene_bbox.center().z),
404+
Vec3::new(
405+
scene_bbox.center().x,
406+
scene_bbox.upper().y,
407+
scene_bbox.center().z,
408+
),
353409
0.5,
354410
);
355411

356-
Scene { camera, lights, dla }
412+
Scene {
413+
camera,
414+
lights,
415+
dla,
416+
}
357417
}
358418
}
359419

src/octree.rs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@ pub struct Octree {
1515

1616
#[derive(Clone, Debug, PartialEq, Eq)]
1717
enum Node {
18-
Branch { children: Box<[Node; 8]>, bbox: Bbox },
19-
Leaf { points: HashSet<Vec3>, bbox: Bbox },
18+
Branch {
19+
children: Box<[Node; 8]>,
20+
bbox: Bbox,
21+
},
22+
Leaf {
23+
points: HashSet<Vec3>,
24+
bbox: Bbox,
25+
},
2026
}
2127

2228
#[derive(Debug)]
@@ -60,7 +66,12 @@ impl Octree {
6066
pub fn iter(&self) -> impl Iterator<Item = &Vec3> {
6167
let stack = self.root.as_ref().map_or_else(Vec::new, |c| vec![c]);
6268

63-
OctreeIter { stack, current: None, len: self.len }.chain(self.outside.iter())
69+
OctreeIter {
70+
stack,
71+
current: None,
72+
len: self.len,
73+
}
74+
.chain(self.outside.iter())
6475
}
6576

6677
pub fn add(&mut self, p: Vec3) {
@@ -75,7 +86,9 @@ impl Octree {
7586
let outside_bbox = self
7687
.outside
7788
.iter()
78-
.fold(Bbox::new(*self.outside.iter().next().unwrap()), |b, p| b.expand(*p));
89+
.fold(Bbox::new(*self.outside.iter().next().unwrap()), |b, p| {
90+
b.expand(*p)
91+
});
7992

8093
let b = match &self.root {
8194
None => outside_bbox,
@@ -85,8 +98,10 @@ impl Octree {
8598
// scale bounding box to avoid rebuilding the tree in the near future
8699
let b = b.scale(2);
87100

88-
self.root =
89-
Some(Node::new(b, self.iter().chain(self.outside.iter()).cloned().collect()));
101+
self.root = Some(Node::new(
102+
b,
103+
self.iter().chain(self.outside.iter()).cloned().collect(),
104+
));
90105
self.outside.clear();
91106
self.rebuilt_count += 1;
92107
}
@@ -102,8 +117,11 @@ impl Octree {
102117
pub fn nearest(&self, p: Vec3) -> Option<(Vec3, i64)> {
103118
let closest = self.root.as_ref().and_then(|n| n.nearest(p));
104119

105-
let closest_outside =
106-
self.outside.iter().map(|pt| (*pt, pt.dist2(p))).min_by_key(|(_, d)| *d);
120+
let closest_outside = self
121+
.outside
122+
.iter()
123+
.map(|pt| (*pt, pt.dist2(p)))
124+
.min_by_key(|(_, d)| *d);
107125

108126
match (closest, closest_outside) {
109127
(None, None) => None,
@@ -188,9 +206,10 @@ impl Node {
188206

189207
pub fn nearest(&self, p: Vec3) -> Option<(Vec3, i64)> {
190208
match self {
191-
Node::Leaf { points, .. } => {
192-
points.iter().map(|pt| (*pt, pt.dist2(p))).min_by_key(|(_, d)| *d)
193-
}
209+
Node::Leaf { points, .. } => points
210+
.iter()
211+
.map(|pt| (*pt, pt.dist2(p)))
212+
.min_by_key(|(_, d)| *d),
194213
Node::Branch { children, bbox } => {
195214
let enclosing_bbox_id = partition_pt(p, bbox.center());
196215

0 commit comments

Comments
 (0)