Fix all clippy warnings or block them.

This commit is contained in:
Bill Thiede 2018-10-11 19:53:07 -07:00
parent fa7d79b112
commit 66d599b50d
13 changed files with 60 additions and 80 deletions

View File

@ -52,7 +52,7 @@ impl AABB {
if yd >= xd && yd >= zd { if yd >= xd && yd >= zd {
return 1; return 1;
} }
return 2; 2
} }
pub fn mid_point(&self) -> Vec3 { pub fn mid_point(&self) -> Vec3 {
@ -153,7 +153,7 @@ impl AABB {
tmax = tmax.min(t1.max(t2)); tmax = tmax.min(t1.max(t2));
} }
return tmax > tmin.max(0.0); tmax > tmin.max(0.0)
} }
} }

View File

@ -16,8 +16,6 @@ extern crate structopt;
extern crate rtiow; extern crate rtiow;
use actix_web::Path as WebPath; use actix_web::Path as WebPath;
use std::fs;
use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::time::SystemTime; use std::time::SystemTime;
@ -135,27 +133,6 @@ struct NoiseParamsMarble {
} }
impl NoiseParams { impl NoiseParams {
fn compact_string(&self) -> String {
let mut s = format!("w{}-h{}", self.width, self.height);
match self.noise {
NoiseType::Scale(scale) => s.push_str(&format!("-s{:.2}", scale)),
NoiseType::Turbulence(turbulence) => s.push_str(&format!("-t{}", turbulence)),
NoiseType::Marble {
period,
power,
size,
} => s.push_str(&format!(
"-m{}-{}-{}",
period.to_string().replace(" ", "-"),
power,
size
)),
}
s
}
fn url(&self) -> String { fn url(&self) -> String {
format!( format!(
"/noise/{}/{}/{}", "/noise/{}/{}/{}",
@ -168,15 +145,11 @@ impl NoiseParams {
fn big_url(&self) -> String { fn big_url(&self) -> String {
format!("/noise/1024/1024/{}", self.noise.to_url()) format!("/noise/1024/1024/{}", self.noise.to_url())
} }
fn image_name(&self) -> String {
format!("noise-{}.png", self.compact_string())
}
} }
fn render_noise(noise_params: &NoiseParams) -> image::GrayImage { fn render_noise(noise_params: &NoiseParams) -> image::GrayImage {
const SEED: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; const SEED: [u8; 16] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
let ref mut rng: XorShiftRng = SeedableRng::from_seed(SEED); let rng: &mut XorShiftRng = &mut SeedableRng::from_seed(SEED);
let mut img = image::GrayImage::new(noise_params.width, noise_params.height); let mut img = image::GrayImage::new(noise_params.width, noise_params.height);
let tex = Noise::new(rng); let tex = Noise::new(rng);
@ -210,18 +183,6 @@ fn render_noise(noise_params: &NoiseParams) -> image::GrayImage {
img img
} }
fn render_noise_to_disk<P>(output_dir: P, noise_params: &NoiseParams) -> Result<(), std::io::Error>
where
P: AsRef<Path>,
{
let img = render_noise(noise_params);
let ref output_dir: &Path = output_dir.as_ref();
fs::create_dir_all(output_dir)?;
let path = output_dir.join(noise_params.image_name());
info!("Saving {}", path.display());
img.save(path)
}
struct Specimen { struct Specimen {
title: String, title: String,
params: Vec<NoiseParams>, params: Vec<NoiseParams>,
@ -366,7 +327,7 @@ fn index(_req: &HttpRequest) -> Result<HttpResponse> {
.body(render_html(specimens).unwrap())) .body(render_html(specimens).unwrap()))
} }
fn noise(np: NoiseParams) -> Result<HttpResponse> { fn noise(np: &NoiseParams) -> Result<HttpResponse> {
let img = render_noise(&np); let img = render_noise(&np);
let mut buf = Vec::new(); let mut buf = Vec::new();
let img = image::DynamicImage::ImageLuma8(img); let img = image::DynamicImage::ImageLuma8(img);
@ -376,24 +337,28 @@ fn noise(np: NoiseParams) -> Result<HttpResponse> {
Ok(HttpResponse::Ok().content_type("image/png").body(buf)) Ok(HttpResponse::Ok().content_type("image/png").body(buf))
} }
// Making parameter breaks WebPath::From magic.
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
fn noise_scale(np: WebPath<NoiseParamsScale>) -> Result<HttpResponse> { fn noise_scale(np: WebPath<NoiseParamsScale>) -> Result<HttpResponse> {
noise(NoiseParams { noise(&NoiseParams {
width: np.width, width: np.width,
height: np.height, height: np.height,
noise: NoiseType::Scale(np.scale), noise: NoiseType::Scale(np.scale),
}) })
} }
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
fn noise_turbulence(np: WebPath<NoiseParamsTurbulence>) -> Result<HttpResponse> { fn noise_turbulence(np: WebPath<NoiseParamsTurbulence>) -> Result<HttpResponse> {
noise(NoiseParams { noise(&NoiseParams {
width: np.width, width: np.width,
height: np.height, height: np.height,
noise: NoiseType::Turbulence(np.turbulence), noise: NoiseType::Turbulence(np.turbulence),
}) })
} }
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
fn noise_marble(np: WebPath<NoiseParamsMarble>) -> Result<HttpResponse> { fn noise_marble(np: WebPath<NoiseParamsMarble>) -> Result<HttpResponse> {
noise(NoiseParams { noise(&NoiseParams {
width: np.width, width: np.width,
height: np.height, height: np.height,
noise: NoiseType::Marble { noise: NoiseType::Marble {

View File

@ -41,28 +41,32 @@ impl fmt::Display for BVHNode {
} }
} }
// Lint is wrong when dealing with Box<Trait>
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
fn box_x_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering { fn box_x_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) { match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) {
(Some(box_left), Some(box_right)) => { (Some(box_left), Some(box_right)) => {
return box_left.min().x.partial_cmp(&box_right.min().x).unwrap(); box_left.min().x.partial_cmp(&box_right.min().x).unwrap()
} }
_ => panic!("hit missing bounding box"), _ => panic!("hit missing bounding box"),
} }
} }
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
fn box_y_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering { fn box_y_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) { match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) {
(Some(box_left), Some(box_right)) => { (Some(box_left), Some(box_right)) => {
return box_left.min().y.partial_cmp(&box_right.min().y).unwrap(); box_left.min().y.partial_cmp(&box_right.min().y).unwrap()
} }
_ => panic!("hit missing bounding box"), _ => panic!("hit missing bounding box"),
} }
} }
#[cfg_attr(feature = "cargo-clippy", allow(borrowed_box))]
fn box_z_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering { fn box_z_compare(ah: &Box<Hit>, bh: &Box<Hit>) -> std::cmp::Ordering {
match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) { match (ah.bounding_box(0., 0.), bh.bounding_box(0., 0.)) {
(Some(box_left), Some(box_right)) => { (Some(box_left), Some(box_right)) => {
return box_left.min().z.partial_cmp(&box_right.min().z).unwrap(); box_left.min().z.partial_cmp(&box_right.min().z).unwrap()
} }
_ => panic!("hit missing bounding box"), _ => panic!("hit missing bounding box"),
} }
@ -78,10 +82,7 @@ fn vec_first_into<T>(v: Vec<T>) -> T {
v.len() v.len()
)); ));
} }
for i in v.into_iter() { v.into_iter().next().unwrap()
return i;
}
panic!("Unreachable");
} }
fn vec_split_into<T>(v: Vec<T>, offset: usize) -> (Vec<T>, Vec<T>) { fn vec_split_into<T>(v: Vec<T>, offset: usize) -> (Vec<T>, Vec<T>) {
@ -100,7 +101,7 @@ fn vec_split_into<T>(v: Vec<T>, offset: usize) -> (Vec<T>, Vec<T>) {
impl BVHNode { impl BVHNode {
fn new(l: Vec<Box<Hit>>, t_min: f32, t_max: f32) -> BVHNode { fn new(l: Vec<Box<Hit>>, t_min: f32, t_max: f32) -> BVHNode {
if l.len() == 1 { if l.len() == 1 {
return BVHNode::Leaf(vec_first_into(l)); BVHNode::Leaf(vec_first_into(l))
} else { } else {
let mut l: Vec<Box<Hit>> = l.into_iter().collect(); let mut l: Vec<Box<Hit>> = l.into_iter().collect();
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
@ -108,7 +109,7 @@ impl BVHNode {
0 => l.sort_by(box_x_compare), 0 => l.sort_by(box_x_compare),
1 => l.sort_by(box_y_compare), 1 => l.sort_by(box_y_compare),
2 => l.sort_by(box_z_compare), 2 => l.sort_by(box_z_compare),
val @ _ => panic!("unknown axis {}", val), val => panic!("unknown axis {}", val),
} }
let half = l.len() / 2; let half = l.len() / 2;
@ -116,7 +117,7 @@ impl BVHNode {
let left = Box::new(BVHNode::new(left_half, t_min, t_max)); let left = Box::new(BVHNode::new(left_half, t_min, t_max));
let right = Box::new(BVHNode::new(right_half, t_min, t_max)); let right = Box::new(BVHNode::new(right_half, t_min, t_max));
let bbox = BVHNode::surrounding_box(left.as_ref(), right.as_ref(), t_min, t_max); let bbox = BVHNode::surrounding_box(left.as_ref(), right.as_ref(), t_min, t_max);
return BVHNode::Branch { left, right, bbox }; BVHNode::Branch { left, right, bbox }
} }
} }
@ -144,7 +145,6 @@ impl Hit for BVHNode {
fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> { fn hit(&self, r: Ray, t_min: f32, t_max: f32) -> Option<HitRecord> {
match self { match self {
BVHNode::Leaf(ref hit) => hit.hit(r, t_min, t_max), BVHNode::Leaf(ref hit) => hit.hit(r, t_min, t_max),
// TODO(wathiede): call bbox.hit() before recursing down both paths of tree.
BVHNode::Branch { BVHNode::Branch {
ref left, ref left,
ref right, ref right,
@ -154,17 +154,16 @@ impl Hit for BVHNode {
if bbox.hit(r, t_min, t_max) { if bbox.hit(r, t_min, t_max) {
match (left.hit(r, t_min, t_max), right.hit(r, t_min, t_max)) { match (left.hit(r, t_min, t_max), right.hit(r, t_min, t_max)) {
(Some(hit_left), Some(hit_right)) => if hit_left.t < hit_right.t { (Some(hit_left), Some(hit_right)) => if hit_left.t < hit_right.t {
return Some(hit_left); Some(hit_left)
} else { } else {
return Some(hit_right); Some(hit_right)
}, },
(Some(hit_left), None) => return Some(hit_left), (Some(hit_left), None) => Some(hit_left),
(None, Some(hit_right)) => return Some(hit_right), (None, Some(hit_right)) => Some(hit_right),
(None, None) => return None, (None, None) => None,
}; };
} else {
return None;
} }
None
} }
None => None, None => None,
}, },
@ -203,7 +202,7 @@ impl BVH {
fn print_tree(f: &mut fmt::Formatter, depth: usize, bvhn: &BVHNode) -> fmt::Result { fn print_tree(f: &mut fmt::Formatter, depth: usize, bvhn: &BVHNode) -> fmt::Result {
let vol = bvhn.volume(); let vol = bvhn.volume();
write!(f, "{:.*}{}{}\n", 2, vol, " ".repeat(depth * 2), bvhn)?; writeln!(f, "{:.*}{}{}", 2, vol, " ".repeat(depth * 2), bvhn)?;
if let BVHNode::Branch { left, right, .. } = bvhn { if let BVHNode::Branch { left, right, .. } = bvhn {
print_tree(f, depth + 1, left)?; print_tree(f, depth + 1, left)?;
print_tree(f, depth + 1, right)?; print_tree(f, depth + 1, right)?;
@ -213,7 +212,7 @@ fn print_tree(f: &mut fmt::Formatter, depth: usize, bvhn: &BVHNode) -> fmt::Resu
impl fmt::Display for BVH { impl fmt::Display for BVH {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Root\n")?; writeln!(f, "Root")?;
print_tree(f, 1, &self.root) print_tree(f, 1, &self.root)
} }
} }

View File

@ -35,6 +35,8 @@ pub struct Camera {
} }
impl Camera { impl Camera {
// Parameters match the example C++ code.
#[cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))]
// vfov is top to bottom in degrees // vfov is top to bottom in degrees
pub fn new( pub fn new(
lookfrom: Vec3, lookfrom: Vec3,

View File

@ -19,6 +19,8 @@ pub struct Cuboid {
} }
impl Cuboid { impl Cuboid {
// This clippy doesn't work right with Arc.
#[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))]
pub fn new(p_min: Vec3, p_max: Vec3, material: Arc<Material>) -> Cuboid { pub fn new(p_min: Vec3, p_max: Vec3, material: Arc<Material>) -> Cuboid {
Cuboid { Cuboid {
p_min, p_min,

View File

@ -43,7 +43,7 @@ fn vec_split_into<T>(v: Vec<T>, offset: usize) -> (Vec<T>, Vec<T>) {
impl KDTree { impl KDTree {
pub fn new(l: Vec<Box<Hit>>, t_min: f32, t_max: f32) -> KDTree { pub fn new(l: Vec<Box<Hit>>, t_min: f32, t_max: f32) -> KDTree {
if l.len() == 0 { if l.is_empty() {
panic!("Attempt to build k-d tree with no Hit objects"); panic!("Attempt to build k-d tree with no Hit objects");
} }
@ -119,15 +119,15 @@ fn print_tree(f: &mut fmt::Formatter, depth: usize, kdt: &KDTree) -> fmt::Result
let vol = kdt.volume(); let vol = kdt.volume();
write!(f, "{:8.2}{}", vol, " ".repeat(depth * 2))?; write!(f, "{:8.2}{}", vol, " ".repeat(depth * 2))?;
match kdt { match kdt {
KDTree::Leaf(ref hit) => write!( KDTree::Leaf(ref hit) => writeln!(
f, f,
"Leaf: {}\n", "Leaf: {}",
hit.bounding_box(0., 0.) hit.bounding_box(0., 0.)
.map_or("NO BBOX".to_owned(), |bb| bb.to_string()) .map_or("NO BBOX".to_owned(), |bb| bb.to_string())
)?, )?,
KDTree::Branch { bbox, .. } => write!( KDTree::Branch { bbox, .. } => writeln!(
f, f,
"Branch: {}\n", "Branch: {}",
// TODO(wathiede): removing this .clone() results in a complaint about moving out // TODO(wathiede): removing this .clone() results in a complaint about moving out
// of a borrow. // of a borrow.
bbox.clone() bbox.clone()
@ -143,7 +143,7 @@ fn print_tree(f: &mut fmt::Formatter, depth: usize, kdt: &KDTree) -> fmt::Result
impl fmt::Display for KDTree { impl fmt::Display for KDTree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "kd-tree\n")?; writeln!(f, "kd-tree")?;
print_tree(f, 1, &self) print_tree(f, 1, &self)
} }
} }

View File

@ -17,6 +17,8 @@ impl Noise {
R: rand::Rng, R: rand::Rng,
{ {
let mut noise = vec![vec![vec![0.; NOISE_SIZE]; NOISE_SIZE]; NOISE_SIZE]; let mut noise = vec![vec![vec![0.; NOISE_SIZE]; NOISE_SIZE]; NOISE_SIZE];
// Squelch warning about only being used to index, as this way reads more consistently.
#[cfg_attr(feature = "cargo-clippy", allow(needless_range_loop))]
for x in 0..NOISE_SIZE { for x in 0..NOISE_SIZE {
for y in 0..NOISE_SIZE { for y in 0..NOISE_SIZE {
for z in 0..NOISE_SIZE { for z in 0..NOISE_SIZE {

View File

@ -1,3 +1,5 @@
// There are many math functions in this file, so we allow single letter variable names.
#![cfg_attr(feature = "cargo-clippy", allow(many_single_char_names))]
use rand::Rng; use rand::Rng;
use vec3::dot; use vec3::dot;
@ -131,6 +133,9 @@ impl Perlin {
let k = p.z.floor() as usize; let k = p.z.floor() as usize;
let mut c: [[[Vec3; 2]; 2]; 2] = Default::default(); let mut c: [[[Vec3; 2]; 2]; 2] = Default::default();
// Squelch warning about di only being used to index, as this way reads more consistently.
#[cfg_attr(feature = "cargo-clippy", allow(needless_range_loop))]
for di in 0..2 { for di in 0..2 {
for dj in 0..2 { for dj in 0..2 {
for dk in 0..2 { for dk in 0..2 {

View File

@ -1,3 +1,5 @@
// There are many math functions in this file, so we allow single letter variable names.
#![cfg_attr(feature = "cargo-clippy", allow(many_single_char_names))]
use aabb::AABB; use aabb::AABB;
use hitable::Hit; use hitable::Hit;
use hitable::HitRecord; use hitable::HitRecord;

View File

@ -48,7 +48,7 @@ pub fn new(opt: &Opt) -> Scene {
let nb = 20; let nb = 20;
let ref mut rng = rand::thread_rng(); let rng = &mut rand::thread_rng();
let mut boxlist: Vec<Box<Hit>> = Vec::with_capacity(10000); let mut boxlist: Vec<Box<Hit>> = Vec::with_capacity(10000);
let mut list: Vec<Box<Hit>> = Vec::new(); let mut list: Vec<Box<Hit>> = Vec::new();
let white: Arc<Material> = Arc::new(Lambertian::new(ConstantTexture::new([0.73, 0.73, 0.73]))); let white: Arc<Material> = Arc::new(Lambertian::new(ConstantTexture::new([0.73, 0.73, 0.73])));

View File

@ -33,7 +33,7 @@ pub fn new(opt: &Opt) -> Scene {
time_max, time_max,
); );
// TODO(wathiede): Use XOR rng for predictability? // TODO(wathiede): Use XOR rng for predictability?
let ref mut rng = rand::thread_rng(); let rng = &mut rand::thread_rng();
let pertext: Arc<Texture> = Arc::new(NoiseTexture::with_scale(rng, 10.)); let pertext: Arc<Texture> = Arc::new(NoiseTexture::with_scale(rng, 10.));
let objects: Vec<Box<Hit>> = vec![ let objects: Vec<Box<Hit>> = vec![

View File

@ -36,7 +36,7 @@ pub fn new(opt: &Opt) -> Scene {
time_min, time_min,
time_max, time_max,
); );
let ref mut rng = rand::thread_rng(); let rng = &mut rand::thread_rng();
let _ground_color = if opt.use_accel { let _ground_color = if opt.use_accel {
Box::new(ConstantTexture::new(Vec3::new(1.0, 0.4, 0.4))) Box::new(ConstantTexture::new(Vec3::new(1.0, 0.4, 0.4)))
} else { } else {

View File

@ -114,8 +114,8 @@ impl ImageTexture {
let (w, h) = img.dimensions(); let (w, h) = img.dimensions();
ImageTexture { ImageTexture {
img, img,
width: w as f32, width: f32::from(w.min(64000) as u16),
height: h as f32, height: f32::from(h.min(64000) as u16),
} }
} }
} }
@ -137,9 +137,12 @@ impl Texture for ImageTexture {
u, v, x, y, self.width, self.height u, v, x, y, self.width, self.height
)); ));
} }
let p = self.img.get_pixel(x, y); let pixel = self.img.get_pixel(x, y);
let rgb = Vec3::new(p[0] as f32 / 255., p[1] as f32 / 255., p[2] as f32 / 255.); Vec3::new(
rgb f32::from(pixel[0]) / 255.,
f32::from(pixel[1]) / 255.,
f32::from(pixel[2]) / 255.,
)
} }
} }