Skip to content

Commit

Permalink
Index Buf with points rather than vecs
Browse files Browse the repository at this point in the history
  • Loading branch information
jdahlstrom committed Dec 7, 2024
1 parent d8c01f9 commit e4b0e2d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
1 change: 1 addition & 0 deletions core/src/math/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ where
Sc::Diff: Linear<Scalar = Sc::Diff> + Copy,
{
type Space = Sp;
// TODO Vectors always Linear once Point used for affine stuff
type Diff = Vector<[Sc::Diff; DIM], Sp>;

/// The dimension (number of components) of `Self`.
Expand Down
59 changes: 32 additions & 27 deletions core/src/util/buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
//! Useful for storing pixel data of any kind, among other things.
use alloc::{vec, vec::Vec};
use core::fmt::{self, Debug, Formatter};
use core::iter;
use core::ops::{Deref, DerefMut};
use core::{
fmt::{self, Debug, Formatter},
iter,
ops::{Deref, DerefMut},
};

use crate::util::Dims;

Expand Down Expand Up @@ -43,16 +45,16 @@ pub trait AsMutSlice2<T> {
///
/// # Examples
/// ```
/// # use retrofire_core::util::buf::*;
/// # use retrofire_core::math::vec::*;
/// # use retrofire_core::util::buf::Buf2;
/// # use retrofire_core::math::point::pt2;
/// // Elements initialized with `Default::default()`
/// let mut buf = Buf2::new((4, 4));
/// // Indexing with a 2D vector (x, y) yields element at row y, column x:
/// buf[vec2(2, 1)] = 123;
/// // Indexing with a 2D point (x, y) yields element at row y, column x:
/// buf[pt2(2, 1)] = 123;
/// // Indexing with an usize i yields row with index i as a slice:
/// assert_eq!(buf[1usize], [0, 0, 123, 0]);
/// assert_eq!(buf[1], [0, 0, 123, 0]);
/// // Thus you can also do this, row first, column second:
/// assert_eq!(buf[1usize][2], 123)
/// assert_eq!(buf[1][2], 123)
/// ```
#[derive(Clone)]
#[repr(transparent)]
Expand Down Expand Up @@ -203,8 +205,8 @@ impl<'a, T> Slice2<'a, T> {
/// # use retrofire_core::util::buf::Slice2;
/// let data = &[0, 1, 2, 3, 4, 5, 6];
/// let slice = Slice2::new((2, 2), 3, data);
/// assert_eq!(&slice[0usize], &[0, 1]);
/// assert_eq!(&slice[1usize], &[3, 4]);
/// assert_eq!(&slice[0], &[0, 1]);
/// assert_eq!(&slice[1], &[3, 4]);
/// ```
/// Above, `slice` represents a 2×2 rectangle with stride 3, such that
/// the first row maps to `data[0..2]` and the second to `data[3..5]`:
Expand Down Expand Up @@ -347,7 +349,10 @@ pub mod inner {
ops::{Deref, DerefMut, Index, IndexMut, Range},
};

use crate::{math::vec::Vec2u, util::rect::Rect, util::Dims};
use crate::{
math::point::Point2u,
util::{rect::Rect, Dims},
};

use super::{AsSlice2, MutSlice2, Slice2};

Expand Down Expand Up @@ -506,7 +511,7 @@ pub mod inner {

/// Returns a reference to the element at `pos`,
/// or `None` if `pos` is out of bounds.
pub fn get(&self, pos: impl Into<Vec2u>) -> Option<&T> {
pub fn get(&self, pos: impl Into<Point2u>) -> Option<&T> {
let [x, y] = pos.into().0;
self.to_index_checked(x, y).map(|i| &self.data[i])
}
Expand Down Expand Up @@ -607,7 +612,7 @@ pub mod inner {

/// Returns a mutable reference to the element at `pos`,
/// or `None` if `pos` is out of bounds.
pub fn get_mut(&mut self, pos: impl Into<Vec2u>) -> Option<&mut T> {
pub fn get_mut(&mut self, pos: impl Into<Point2u>) -> Option<&mut T> {
let [x, y] = pos.into().0;
self.to_index_checked(x, y)
.map(|i| &mut self.data[i])
Expand Down Expand Up @@ -662,7 +667,7 @@ pub mod inner {
impl<T, D, Pos> Index<Pos> for Inner<T, D>
where
D: Deref<Target = [T]>,
Pos: Into<Vec2u>,
Pos: Into<Point2u>,
{
type Output = T;

Expand All @@ -680,7 +685,7 @@ pub mod inner {
impl<T, D, Pos> IndexMut<Pos> for Inner<T, D>
where
D: DerefMut<Target = [T]>,
Pos: Into<Vec2u>,
Pos: Into<Point2u>,
{
/// Returns a mutable reference to the element at position `pos`.
///
Expand Down Expand Up @@ -734,8 +739,8 @@ mod tests {
assert_eq!(buf[2usize], [2, 12, 22, 32]);

assert_eq!(buf[[0, 0]], 0);
assert_eq!(buf[vec2(1, 0)], 10);
assert_eq!(buf[vec2(3, 4)], 34);
assert_eq!(buf[[1, 0]], 10);
assert_eq!(buf[[3, 4]], 34);

assert_eq!(buf.get([2, 3]), Some(&23));
assert_eq!(buf.get([4, 4]), None);
Expand All @@ -749,7 +754,7 @@ mod tests {
buf[2usize][1] = 123;
assert_eq!(buf[2usize], [2, 123, 22, 32]);

buf[vec2(2, 3)] = 234;
buf[[2, 3]] = 234;
assert_eq!(buf[[2, 3]], 234);

*buf.get_mut([3, 4]).unwrap() = 345;
Expand Down Expand Up @@ -952,12 +957,12 @@ mod tests {
let buf = Buf2::new_with((5, 4), |x, y| x * 10 + y);
let slice = buf.slice((2.., 1..3));

assert_eq!(slice[vec2(0, 0)], 21);
assert_eq!(slice[vec2(1, 0)], 31);
assert_eq!(slice[vec2(2, 1)], 42);
assert_eq!(slice[[0, 0]], 21);
assert_eq!(slice[[1, 0]], 31);
assert_eq!(slice[[2, 1]], 42);

assert_eq!(slice.get(vec2(2, 1)), Some(&42));
assert_eq!(slice.get(vec2(2, 2)), None);
assert_eq!(slice.get([2, 1]), Some(&42));
assert_eq!(slice.get([2, 2]), None);
}

#[test]
Expand All @@ -966,10 +971,10 @@ mod tests {
let mut slice = buf.slice_mut((2.., 1..3));

slice[[2, 1]] = 123;
assert_eq!(slice[vec2(2, 1)], 123);
assert_eq!(slice[[2, 1]], 123);

assert_eq!(slice.get_mut(vec2(2, 1)), Some(&mut 123));
assert_eq!(slice.get(vec2(2, 2)), None);
assert_eq!(slice.get_mut([2, 1]), Some(&mut 123));
assert_eq!(slice.get([2, 2]), None);

buf[[2, 2]] = 321;
let slice = buf.slice((1.., 2..));
Expand Down
16 changes: 9 additions & 7 deletions demos/src/bin/bezier.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::mem::swap;
use std::ops::ControlFlow::Continue;

use re::math::rand::{Distrib, Uniform, UnitDisk, Xorshift64};
use re::math::spline::BezierSpline;
use re::prelude::*;
use re::util::Dims;

use re_front::minifb::Window;
use re_front::Frame;
use re::math::{
point::Point2u,
rand::{Distrib, Uniform, UnitDisk, Xorshift64},
spline::BezierSpline,
};

fn line([mut p0, mut p1]: [Vec2; 2]) -> impl Iterator<Item = Vec2u> {
use re_front::{minifb::Window, Frame};

fn line([mut p0, mut p1]: [Vec2; 2]) -> impl Iterator<Item = Point2u> {
if p0.y() > p1.y() {
swap(&mut p0, &mut p1);
}
Expand All @@ -23,7 +25,7 @@ fn line([mut p0, mut p1]: [Vec2; 2]) -> impl Iterator<Item = Vec2u> {
};

p0.vary(step, Some(n as u32))
.map(|p| vec2(p.x() as u32, p.y() as u32))
.map(|p| p.map(|c| c as u32).to_pt())
}

fn main() {
Expand Down

0 comments on commit e4b0e2d

Please sign in to comment.