Skip to content

Commit

Permalink
Add some random helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkleiny committed Aug 10, 2024
1 parent 4673ba9 commit e6260fd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
6 changes: 3 additions & 3 deletions core/common/src/collections/grids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ impl<T> DenseGrid<T> {
/// A sparse grid of [`T`]s.
#[derive(Default, Clone, Debug)]
pub struct SparseGrid<T> {
entries: BTreeMap<usize, T>,
entries: BTreeMap<isize, T>,
}

/// A type that contains a position in 2-space.
Expand Down Expand Up @@ -308,8 +308,8 @@ impl<'a, T> IntoIterator for &'a mut SparseGrid<T> {

/// Gets the hash for the given position.
#[inline(always)]
const fn xy_hash(x: i32, y: i32) -> usize {
x as usize + y as usize
const fn xy_hash(x: i32, y: i32) -> isize {
x as isize | (y as isize) << 32
}

#[cfg(test)]
Expand Down
11 changes: 11 additions & 0 deletions core/common/src/maths/cameras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use super::*;

/// Represents a camera.
pub trait Camera {
/// Gets the position of this camera.
fn position(&self) -> Vec3;

/// Computes the projection matrix for this camera.
fn projection(&self) -> Mat4;

Expand Down Expand Up @@ -48,6 +51,10 @@ impl Default for OrthographicCamera {
}

impl Camera for OrthographicCamera {
fn position(&self) -> Vec3 {
self.position
}

fn projection(&self) -> Mat4 {
Mat4::orthographic_rh_gl(
0.,
Expand Down Expand Up @@ -91,6 +98,10 @@ impl Default for PerspectiveCamera {
}

impl Camera for PerspectiveCamera {
fn position(&self) -> Vec3 {
self.position
}

fn projection(&self) -> Mat4 {
Mat4::perspective_lh(self.fov, self.aspect_ratio, self.near_plane, self.far_plane)
}
Expand Down
30 changes: 30 additions & 0 deletions core/common/src/maths/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ impl Random {
range.start + (self.next::<T>() % (range.end - range.start))
}

/// Chooses a random value from the given slice.
pub fn choose_slice<'a, T: FromRandom>(&mut self, values: &'a [T]) -> Option<&'a T> {
if values.is_empty() {
return None;
}

Some(&values[self.next_range(0..values.len())])
}

/// Chooses a random value from the given iterator.
pub fn choose_iter<'a, T: FromRandom>(&mut self, values: &mut impl Iterator<Item = &'a T>) -> Option<&'a T> {
while let Some(value) = values.next() {
if self.next::<bool>() {
return Some(value);
}
}

None
}

/// Generates a random u64 number between 0 and u64::MAX, inclusive.
pub fn next_u64(&mut self) -> u64 {
self.state = self.state.wrapping_add(0xA0761D6478BD642F);
Expand Down Expand Up @@ -248,6 +268,16 @@ mod tests {
assert_eq!(a, b);
}

#[test]
fn test_choose_from_slice() {
let mut random = Random::with_seed(0);

let values = [1, 2, 3, 4, 5];
let a = random.choose_slice(&values).unwrap();

assert!(values.contains(a));
}

#[test]
fn test_generate_value_based_on_global_random() {
let a = u64::random();
Expand Down

0 comments on commit e6260fd

Please sign in to comment.