Skip to content

Commit 209010e

Browse files
committed
Rustfmt most of the code
But don't accept the suggestions that make the code harder to read.
1 parent 35bd0b1 commit 209010e

17 files changed

+210
-168
lines changed

src/aabb.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ impl Aabb {
5353

5454
/// Returns the smalles axis-aligned bounding box that contains all input
5555
/// points.
56-
pub fn enclose_points<'a, I>(points: I) -> Aabb where I: IntoIterator<Item = &'a SVector3> {
56+
pub fn enclose_points<'a, I>(points: I) -> Aabb
57+
where I: IntoIterator<Item = &'a SVector3>
58+
{
5759
let mut it = points.into_iter();
5860
let &first = it.next().expect("enclosure must encluse at least one point");
5961

@@ -69,7 +71,9 @@ impl Aabb {
6971
}
7072

7173
/// Returns the smallest bounding box that contains all input boxes.
72-
pub fn enclose_aabbs<'a, I>(aabbs: I) -> Aabb where I: IntoIterator<Item = &'a Aabb> {
74+
pub fn enclose_aabbs<'a, I>(aabbs: I) -> Aabb
75+
where I: IntoIterator<Item = &'a Aabb>
76+
{
7377
let mut it = aabbs.into_iter();
7478
let first = it.next().expect("enclosure must enclose at least one AABB");
7579

src/bench.rs

+24-22
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ pub fn unit_squaternions(n: usize) -> Vec<SQuaternion> {
8989
// Use rejection sampling because I do not know how to sample a 4D unit
9090
// sphere uniformly.
9191
let norm_squared = a * a + b * b + c * c + d * d;
92-
if norm_squared > 1.0 { continue }
92+
if norm_squared > 1.0 {
93+
continue;
94+
}
9395

9496
let norm = norm_squared.sqrt();
9597
let q = SQuaternion::new(a / norm, b / norm, c / norm, d / norm);
@@ -175,33 +177,33 @@ pub fn triangles(n: usize) -> Vec<Triangle> {
175177
let v1s = svectors_on_unit_sphere(n);
176178
let v2s = svectors_on_unit_sphere(n);
177179
v0s.iter()
178-
.zip(v1s.iter().zip(v2s.iter()))
179-
.map(|(&v0, (&v1, &v2))| Triangle::new(v0, v1, v2, SMaterial::white()))
180-
.collect()
180+
.zip(v1s.iter().zip(v2s.iter()))
181+
.map(|(&v0, (&v1, &v2))| Triangle::new(v0, v1, v2, SMaterial::white()))
182+
.collect()
181183
}
182184

183185
/// Generates n bounding boxes with two vertices on the unit sphere.
184186
pub fn aabbs(n: usize) -> Vec<Aabb> {
185187
let v0s = svectors_on_unit_sphere(n);
186188
let v1s = svectors_on_unit_sphere(n);
187189
v0s.iter()
188-
.zip(v1s.iter())
189-
.map(|(&v0, &v1)| Aabb::new(SVector3::min(v0, v1), SVector3::max(v0, v1)))
190-
.collect()
190+
.zip(v1s.iter())
191+
.map(|(&v0, &v1)| Aabb::new(SVector3::min(v0, v1), SVector3::max(v0, v1)))
192+
.collect()
191193
}
192194

193195
/// Generates n mrays originating from a sphere of radius 10, pointing inward.
194196
pub fn mrays_inward(n: usize) -> Vec<MRay> {
195197
let origins = mvectors_on_unit_sphere(n);
196198
let dests = mvectors_on_unit_sphere(n);
197199
origins.iter()
198-
.zip(dests.iter())
199-
.map(|(&from, &to)| {
200-
let origin = from * Mf32::broadcast(10.0);
201-
let direction = (to - origin).normalized();
202-
MRay::new(origin, direction)
203-
})
204-
.collect()
200+
.zip(dests.iter())
201+
.map(|(&from, &to)| {
202+
let origin = from * Mf32::broadcast(10.0);
203+
let direction = (to - origin).normalized();
204+
MRay::new(origin, direction)
205+
})
206+
.collect()
205207
}
206208

207209
/// Generates n mrays originating from a sphere of radius 10, pointing inward.
@@ -210,14 +212,14 @@ pub fn mrays_inward_coherent(n: usize) -> Vec<MRay> {
210212
let origins = svectors_on_unit_sphere(n);
211213
let dests = mvectors_on_unit_sphere(n);
212214
origins.iter()
213-
.zip(dests.iter())
214-
.map(|(&from, &to)| {
215-
let origin = MVector3::broadcast(from * 10.0);
216-
let dest = to * Mf32::broadcast(0.5);
217-
let direction = (dest - origin).normalized();
218-
MRay::new(origin, direction)
219-
})
220-
.collect()
215+
.zip(dests.iter())
216+
.map(|(&from, &to)| {
217+
let origin = MVector3::broadcast(from * 10.0);
218+
let dest = to * Mf32::broadcast(0.5);
219+
let direction = (dest - origin).normalized();
220+
MRay::new(origin, direction)
221+
})
222+
.collect()
221223
}
222224

223225
#[test]

src/bvh.rs

+48-32
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,9 @@ impl InterimNode {
162162
let num_tris = self.triangles.len();
163163
if bins[index].triangles.len() > num_tris / 8 && num_tris > bins.len() {
164164
// Clear the bins before trying again.
165-
for bin in &mut bins[..] { bin.clear(); }
165+
for bin in &mut bins[..] {
166+
bin.clear();
167+
}
166168
return self.bin_triangles_uniform(bins, axis);
167169
}
168170
}
@@ -201,8 +203,8 @@ impl InterimNode {
201203
/// Returs the bounding box enclosing the bin bounding boxes.
202204
fn enclose_bins(bins: &[Bin]) -> Aabb {
203205
let aabbs = bins.iter()
204-
.filter(|bin| bin.triangles.len() > 0)
205-
.map(|bin| bin.aabb.as_ref().unwrap());
206+
.filter(|bin| bin.triangles.len() > 0)
207+
.map(|bin| bin.aabb.as_ref().unwrap());
206208

207209
Aabb::enclose_aabbs(aabbs)
208210
}
@@ -213,9 +215,12 @@ impl InterimNode {
213215
}
214216

215217
/// Returns the cheapest split and its cost.
216-
fn find_cheapest_split<'a, H>(&self, heuristic: &H, bins: &[Bin<'a>])
217-
-> (f32, Vec<&'a TriangleRef>, Vec<&'a TriangleRef>)
218-
where H: Heuristic {
218+
fn find_cheapest_split<'a, H>(&self,
219+
heuristic: &H,
220+
bins: &[Bin<'a>])
221+
-> (f32, Vec<&'a TriangleRef>, Vec<&'a TriangleRef>)
222+
where H: Heuristic
223+
{
219224
let mut best_split_at = 0;
220225
let mut best_split_cost = 0.0;
221226
let mut is_first = true;
@@ -257,10 +262,12 @@ impl InterimNode {
257262

258263
/// Splits the node if that is would be beneficial according to the
259264
/// heuristic.
260-
fn split<H>(&mut self, heuristic: &H) where H: Heuristic {
265+
fn split<H>(&mut self, heuristic: &H)
266+
where H: Heuristic
267+
{
261268
// If there is only one triangle, splitting does not make sense.
262269
if self.triangles.len() <= 1 {
263-
return
270+
return;
264271
}
265272

266273
let (best_split_cost, left_tris, right_tris) = {
@@ -286,7 +293,9 @@ impl InterimNode {
286293
}
287294
}
288295

289-
for bin in &mut bins[..] { bin.clear(); }
296+
for bin in &mut bins[..] {
297+
bin.clear();
298+
}
290299
}
291300

292301
// Something must have set the cost.
@@ -301,7 +310,7 @@ impl InterimNode {
301310
// one.
302311
let no_split_cost = heuristic.tris_cost(self.triangles.len());
303312
if no_split_cost < best_split_cost {
304-
return
313+
return;
305314
}
306315

307316
let left_node = InterimNode::from_triangle_refs(left_tris);
@@ -313,7 +322,9 @@ impl InterimNode {
313322
}
314323

315324
/// Recursively splits the node, constructing the BVH.
316-
fn split_recursive<H>(&mut self, heuristic: &H) where H: Heuristic {
325+
fn split_recursive<H>(&mut self, heuristic: &H)
326+
where H: Heuristic
327+
{
317328
use rayon;
318329
self.split(heuristic);
319330

@@ -396,8 +407,10 @@ impl InterimNode {
396407
nodes.push(BvhNode::new());
397408

398409
// Recursively crystallize the child nodes.
399-
self.children[0].crystallize(source_triangles, nodes, sorted_triangles, child_index + 0);
400-
self.children[1].crystallize(source_triangles, nodes, sorted_triangles, child_index + 1);
410+
self.children[0]
411+
.crystallize(source_triangles, nodes, sorted_triangles, child_index + 0);
412+
self.children[1]
413+
.crystallize(source_triangles, nodes, sorted_triangles, child_index + 1);
401414

402415
nodes[into_index].index = child_index as u32;
403416
nodes[into_index].len = 0;
@@ -474,9 +487,10 @@ impl BvhNode {
474487
impl Bvh {
475488
pub fn build(source_triangles: &[Triangle]) -> Bvh {
476489
// Actual triangles are not important to the BVH, convert them to AABBs.
477-
let trirefs = (0..).zip(source_triangles.iter())
478-
.map(|(i, tri)| TriangleRef::from_triangle(i, tri))
479-
.collect();
490+
let trirefs = (0..)
491+
.zip(source_triangles.iter())
492+
.map(|(i, tri)| TriangleRef::from_triangle(i, tri))
493+
.collect();
480494

481495
let mut root = InterimNode::from_triangle_refs(trirefs);
482496

@@ -534,20 +548,19 @@ impl Bvh {
534548
let mut triangles = Vec::new();
535549

536550
for mesh in meshes {
537-
let mesh_triangles = mesh.triangles.iter().map(
538-
|ref tri| {
539-
let (i0, i1, i2) = tri.vertices;
540-
let v0 = mesh.vertices[i0 as usize];
541-
let v1 = mesh.vertices[i1 as usize];
542-
let v2 = mesh.vertices[i2 as usize];
543-
let mut triangle = Triangle::new(v0, v1, v2, tri.material);
544-
if let Some((tx0, tx1, tx2)) = tri.tex_coords {
545-
triangle.uv0 = mesh.tex_coords[tx0 as usize];
546-
triangle.uv1 = mesh.tex_coords[tx1 as usize];
547-
triangle.uv2 = mesh.tex_coords[tx2 as usize];
548-
}
549-
triangle
550-
});
551+
let mesh_triangles = mesh.triangles.iter().map(|ref tri| {
552+
let (i0, i1, i2) = tri.vertices;
553+
let v0 = mesh.vertices[i0 as usize];
554+
let v1 = mesh.vertices[i1 as usize];
555+
let v2 = mesh.vertices[i2 as usize];
556+
let mut triangle = Triangle::new(v0, v1, v2, tri.material);
557+
if let Some((tx0, tx1, tx2)) = tri.tex_coords {
558+
triangle.uv0 = mesh.tex_coords[tx0 as usize];
559+
triangle.uv1 = mesh.tex_coords[tx1 as usize];
560+
triangle.uv2 = mesh.tex_coords[tx2 as usize];
561+
}
562+
triangle
563+
});
551564
triangles.extend(mesh_triangles);
552565
}
553566

@@ -573,7 +586,10 @@ impl Bvh {
573586
/// Also returns the number of AABBs intersected and the number of triangles
574587
/// intersected.
575588
#[inline(always)]
576-
pub fn intersect_nearest_impl(&self, ray: &MRay, mut isect: MIntersection) -> (MIntersection, u32, u32) {
589+
pub fn intersect_nearest_impl(&self,
590+
ray: &MRay,
591+
mut isect: MIntersection)
592+
-> (MIntersection, u32, u32) {
577593
// Keep a stack of nodes that still need to be intersected. This does
578594
// involve a heap allocation, but that is not so bad. Using a small
579595
// on-stack vector from the smallvec crate (which falls back to heap
@@ -610,7 +626,7 @@ impl Bvh {
610626
// intersection, then nothing inside the node can yield
611627
// a closer intersection, so we can skip the node.
612628
if aabb_isect.is_further_away_than(isect.distance, ray.active) {
613-
continue
629+
continue;
614630
}
615631

616632
if node.len == 0 {

src/main.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,12 @@ fn main() {
144144
// blur.
145145
renderer.set_time(time, 0.0);
146146
}
147-
Action::None => { }
147+
Action::None => {}
148148
}
149149

150-
if render_realtime { renderer.set_time(time, time_delta); }
150+
if render_realtime {
151+
renderer.set_time(time, time_delta);
152+
}
151153
renderer.update_scene();
152154

153155
// When rendering in accumulation mode, first copy the current state

src/material.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ fn diffuse_brdf(isect: &MIntersection, ray_in: &MRay) -> MVector3 {
225225
let modulation = Mf32::broadcast(0.5 / consts::PI) * cos_theta;
226226

227227
debug_assert!(modulation.all_finite());
228-
debug_assert!(modulation.all_sign_bits_positive(), "color modulation cannot be negative");
228+
debug_assert!(modulation.all_sign_bits_positive(),
229+
"color modulation cannot be negative");
229230

230231
MVector3::new(modulation, modulation, modulation)
231232
}
@@ -241,9 +242,7 @@ fn diffuse_brdf(isect: &MIntersection, ray_in: &MRay) -> MVector3 {
241242
/// so the cosine distribution still does a decent job, and it is much cheaper
242243
/// to sample from than a distribution specific for the Blinn-Phong BRDF.
243244
#[inline(always)]
244-
fn continue_path_brdf(isect: &MIntersection,
245-
rng: &mut Rng)
246-
-> MRay {
245+
fn continue_path_brdf(isect: &MIntersection, rng: &mut Rng) -> MRay {
247246
// Bounce in a random direction in the hemisphere around the surface
248247
// normal, with a cosine-weighted distribution, for a diffuse bounce.
249248
let dir_z = rng.sample_hemisphere_vector();
@@ -268,10 +267,7 @@ fn pd_brdf(isect: &MIntersection, ray: &MRay) -> Mf32 {
268267
}
269268

270269
/// Continues the path of a photon by sampling a point on a surface.
271-
fn continue_path_direct_sample(scene: &Scene,
272-
isect: &MIntersection,
273-
rng: &mut Rng)
274-
-> MRay {
270+
fn continue_path_direct_sample(scene: &Scene, isect: &MIntersection, rng: &mut Rng) -> MRay {
275271
let ds = scene.get_direct_sample(rng);
276272
let direction = (ds.position - isect.position).normalized();
277273

@@ -329,7 +325,8 @@ fn pd_direct_sample(scene: &Scene, ray: &MRay) -> Mf32 {
329325
pd_total = (pd_total + pd).pick(pd_total, sample_isect.mask);
330326

331327
debug_assert!(pd_total.all_finite());
332-
debug_assert!(pd_total.all_sign_bits_positive(), "probability density must be positive");
328+
debug_assert!(pd_total.all_sign_bits_positive(),
329+
"probability density must be positive");
333330
});
334331

335332
// So far we computed the probability density per triangle, but we picked
@@ -421,7 +418,7 @@ pub fn continue_path(material: MMaterial,
421418
let new_ray = MRay {
422419
origin: new_ray.origin.pick(ray.origin, active),
423420
direction: new_ray.direction.pick(ray.direction, active),
424-
active: active
421+
active: active,
425422
};
426423

427424
let white = MVector3::new(Mf32::one(), Mf32::one(), Mf32::one());
@@ -456,7 +453,8 @@ fn microfacet_brdf(material: MMaterial,
456453
f_color
457454
};
458455

459-
debug_assert!(d.all_sign_bits_positive(), "surface normal density must not be negative");
456+
debug_assert!(d.all_sign_bits_positive(),
457+
"surface normal density must not be negative");
460458

461459
// Compute the final microfacet transmission. The factor
462460
// 4 * dot(n, l) * dot(n, v) has been absorbed into the geometry factor,
@@ -493,7 +491,8 @@ fn microfacet_fresnel(incoming: MVector3, half_way: MVector3, color: MVector3) -
493491
/// distribution".)
494492
fn microfacet_normal_dist(half_way: MVector3,
495493
isect: &MIntersection,
496-
glossiness_exponent_index: Mi32) -> Mf32 {
494+
glossiness_exponent_index: Mi32)
495+
-> Mf32 {
497496
// Compute powers of the cosine.
498497
let c1 = half_way.dot(isect.normal);
499498
let c2 = c1 * c1;

src/quaternion.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,9 @@ fn bench_interpolate_1000(b: &mut test::Bencher) {
264264
let ((q0, q1), &t) = it.next().unwrap();
265265
for _ in 0..100 {
266266
unroll_10! {{
267-
test::black_box(test::black_box(q0).interpolate(test::black_box(q1), test::black_box(t)));
267+
test::black_box(
268+
test::black_box(q0)
269+
.interpolate(test::black_box(q1), test::black_box(t)));
268270
}};
269271
}
270272
});

src/random.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub struct Rng {
3535
}
3636

3737
impl Rng {
38-
3938
/// Creates a new random number generator.
4039
///
4140
/// The generator is seeded from three 32-bit integers, suggestively called
@@ -62,11 +61,12 @@ impl Rng {
6261
// powers of two.
6362
let seed = seed.wrapping_add(seed % 9358246936573323101);
6463

65-
let primes = Mu64(14491630826648200009, 13149596372461506851, 6119410235796056053, 14990141545859273719);
64+
let primes = Mu64(14491630826648200009,
65+
13149596372461506851,
66+
6119410235796056053,
67+
14990141545859273719);
6668

67-
Rng {
68-
state: Mu64(seed, seed, seed, seed) * primes,
69-
}
69+
Rng { state: Mu64(seed, seed, seed, seed) * primes }
7070
}
7171

7272
/// Updates the state and returns the old state.

0 commit comments

Comments
 (0)