Fix imports and Uniform creation usage in rand

Rand v0.9 made the `Uniform::new()` function fallible, but I'm going to
keep assuming these always work and just `.unwrap()` the Result.

It also renamed the distribution module, for some reason.
This commit is contained in:
2025-03-01 12:57:57 -06:00
parent 8548e4e322
commit 05add1efca
2 changed files with 9 additions and 9 deletions

View File

@@ -15,7 +15,7 @@ use std::fmt::Display;
use rand::Rng; use rand::Rng;
use rand::rngs::SmallRng; use rand::rngs::SmallRng;
use rand::distributions::Uniform; use rand::distr::Uniform;
pub type Vec2i = Vec2<i32>; pub type Vec2i = Vec2<i32>;
pub type Vec2f = Vec2<f32>; pub type Vec2f = Vec2<f32>;
@@ -144,7 +144,7 @@ impl Vec3{
} }
pub fn rand_in_unit_sphere(srng: &mut SmallRng) -> Vec3 { pub fn rand_in_unit_sphere(srng: &mut SmallRng) -> Vec3 {
let distrib = Uniform::new(-1.0, 1.0); let distrib = Uniform::new(-1.0, 1.0).unwrap();
loop { loop {
let p = Vec3::rand(srng, distrib); let p = Vec3::rand(srng, distrib);
if p.length_squared() >= 1.0 { continue; } if p.length_squared() >= 1.0 { continue; }
@@ -156,8 +156,8 @@ impl Vec3{
let distrib = Uniform::new(-1.0, 1.0); let distrib = Uniform::new(-1.0, 1.0);
loop { loop {
let p = Vec3 { let p = Vec3 {
x: srng.sample(distrib), x: srng.sample(distrib.unwrap()),
y: srng.sample(distrib), y: srng.sample(distrib.unwrap()),
z: 0.0, z: 0.0,
}; };
if p.length_squared() >= 1.0 { continue; } if p.length_squared() >= 1.0 { continue; }

View File

@@ -3,7 +3,7 @@ use crate::primitives::{Vec3, Ray};
use rand::Rng; use rand::Rng;
use rand::rngs::SmallRng; use rand::rngs::SmallRng;
use rand::distributions::Uniform; use rand::distr::Uniform;
pub struct HitRecord{ pub struct HitRecord{
pub p: Vec3, pub p: Vec3,
@@ -140,7 +140,7 @@ impl Material {
let sin_theta = (1.0 - cos_theta * cos_theta).sqrt(); let sin_theta = (1.0 - cos_theta * cos_theta).sqrt();
let cannot_refract = refraction_ratio * sin_theta > 1.0; let cannot_refract = refraction_ratio * sin_theta > 1.0;
let distrib_zero_one = Uniform::new(0.0, 1.0); let distrib_zero_one = Uniform::new(0.0, 1.0).unwrap();
let direction = if cannot_refract || Material::reflectance(cos_theta, refraction_ratio) > srng.sample(distrib_zero_one) { let direction = if cannot_refract || Material::reflectance(cos_theta, refraction_ratio) > srng.sample(distrib_zero_one) {
Vec3::reflect(unit_direction, rec.normal) Vec3::reflect(unit_direction, rec.normal)
} else { } else {
@@ -240,7 +240,7 @@ impl Scene {
world.push( Hittable::Sphere { center: Vec3::new(0.0, -1000.0, 0.0), radius: 1000.0, material: mat_ground }); world.push( Hittable::Sphere { center: Vec3::new(0.0, -1000.0, 0.0), radius: 1000.0, material: mat_ground });
let distrib_zero_one = Uniform::new(0.0, 1.0); let distrib_zero_one = Uniform::new(0.0, 1.0).unwrap();
for a in -11..11 { for a in -11..11 {
for b in -11..11 { for b in -11..11 {
let choose_mat = srng.sample(distrib_zero_one); let choose_mat = srng.sample(distrib_zero_one);
@@ -264,8 +264,8 @@ impl Scene {
); );
} else if choose_mat < 0.95 { } else if choose_mat < 0.95 {
// metal // metal
let distr_albedo = Uniform::new(0.5, 1.0); let distr_albedo = Uniform::new(0.5, 1.0).unwrap();
let distr_fuzz = Uniform::new(0.0, 0.5); let distr_fuzz = Uniform::new(0.0, 0.5).unwrap();
let albedo = Vec3::rand(srng, distr_albedo); let albedo = Vec3::rand(srng, distr_albedo);
let fuzz = srng.sample(distr_fuzz); let fuzz = srng.sample(distr_fuzz);