Skip to content

Commit

Permalink
refactor: removed translate flag and replaced with translate objects …
Browse files Browse the repository at this point in the history
…function
  • Loading branch information
salam99823 committed Nov 17, 2024
1 parent 693ba9a commit 1bb608c
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 48 deletions.
10 changes: 5 additions & 5 deletions examples/bevy-image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ fn draw_png(image: &Image, img_path: &str) {

let scale = 8;
let (width, height) = (
i32::try_from(image.width()).expect("Image to wide.") * scale,
i32::try_from(image.height()).expect("Image to tall.") * scale,
i32::try_from(image.width() * scale).expect("Image to wide."),
i32::try_from(image.height() * scale).expect("Image to tall."),
);

// draw the edges to a png
let mut dt = DrawTarget::new(width, height);

let objects = edges.multi_image_edges_raw();
let objects = edges.multi_image_edge_raw();

for object in objects {
let mut pb = PathBuilder::new();
let mut edges_iter = object.into_iter();

if let Some(first_edge) = edges_iter.next() {
pb.move_to(first_edge.x * scale as f32, first_edge.y * scale as f32);
pb.move_to((first_edge.x * scale) as f32, (first_edge.y * scale) as f32);
for edge in edges_iter {
pb.line_to(edge.x * scale as f32, edge.y * scale as f32);
pb.line_to((edge.x * scale) as f32, (edge.y * scale) as f32);
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/dynamic-image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn draw_png(img_path: &str) {

let scale = 8;
let (width, height) = (
i32::try_from(image.width()).expect("Image to wide.") * scale,
i32::try_from(image.height()).expect("Image to tall.") * scale,
i32::try_from(image.width() * scale).expect("Image to wide."),
i32::try_from(image.height() * scale).expect("Image to tall."),
);

// draw the edges to a png
Expand All @@ -25,9 +25,9 @@ fn draw_png(img_path: &str) {

let mut edges_iter = edges.single_image_edge_raw().into_iter();
let first_edge = edges_iter.next().unwrap();
pb.move_to(first_edge.x * scale as f32, first_edge.y * scale as f32);
pb.move_to((first_edge.x * scale) as f32, (first_edge.y * scale) as f32);
for edge in edges_iter {
pb.line_to(edge.x * scale as f32, edge.y * scale as f32);
pb.line_to((edge.x * scale) as f32, (edge.y * scale) as f32);
}

let path = pb.finish();
Expand Down
8 changes: 4 additions & 4 deletions src/bin_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ impl BinImage {
/// # Returns
///
/// A new `Vec2` representing the translated coordinates
fn translate_point(&self, p: Vec2) -> Vec2 {
const fn translate_point(&self, p: UVec2) -> Vec2 {
Vec2::new(
p.x - ((self.width / 2) as f32 - 1.0),
((self.height / 2) as f32 - 1.0) - p.y,
(p.x - self.width / 2 - 1) as f32,
(self.height / 2 - p.y - 1) as f32,
)
}

Expand All @@ -147,7 +147,7 @@ impl BinImage {
/// # Returns
///
/// A vector of `Vec2` representing the translated coordinates.
pub fn translate(&self, v: Vec<Vec2>) -> Vec<Vec2> {
pub fn translate(&self, v: Vec<UVec2>) -> Vec<Vec2> {
v.into_par_iter().map(|p| self.translate_point(p)).collect()
}

Expand Down
93 changes: 62 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,70 @@ pub struct Edges {
}

impl Edges {
/// Creates a new `Edges` instance from the given dimensions and pixel data.
///
/// # Arguments
///
/// * `height` - The height of the image.
/// * `width` - The width of the image.
/// * `data` - A slice of bytes representing the pixel data of the image.
///
#[inline]
#[must_use]
pub fn new(height: u32, width: u32, data: &[u8]) -> Self {
Self {
image: BinImage::new(height, width, data),
}
}

/// If there's only one sprite / object in the image, this returns just one, with
/// coordinates translated to either side of (0, 0)
/// Translates the edges of a single image into a coordinate system centered at (0, 0).
///
/// # Returns
///
/// A vector of `Vec2` representing the translated edge points.
#[inline]
#[must_use]
pub fn single_image_edge_translated(&self) -> Vec<Vec2> {
self.image_edges(true).into_par_iter().flatten().collect()
self.translate(self.single_image_edge_raw())
}

/// If there's only one sprite / object in the image, this returns just one, with
/// coordinates left alone and all in positive x and y
/// Retrieves the raw edge points of a single image.
///
/// # Returns
///
/// A vector of `UVec2` representing the raw edge points.
#[inline]
#[must_use]
pub fn single_image_edge_raw(&self) -> Vec<Vec2> {
self.image_edges(false).into_par_iter().flatten().collect()
pub fn single_image_edge_raw(&self) -> Vec<UVec2> {
self.image_edges().into_par_iter().flatten().collect()
}

/// If there's more than one sprite / object in the image, this returns all it finds, with
/// coordinates translated to either side of (0, 0)
/// Translates the edges of multiple images into a coordinate system centered at (0, 0).
///
/// # Returns
///
/// A vector of vectors of `Vec2` representing the translated edge points of each image.
#[inline]
#[must_use]
pub fn multi_image_edge_translated(&self) -> Vec<Vec<Vec2>> {
self.image_edges(true)
self.translate_objects(self.multi_image_edge_raw())
}

/// If there's more than one sprite / object in the image, this returns all it finds, with
/// coordinates left alone and all in positive x and y
/// Retrieves the raw edge points of multiple images.
///
/// # Returns
///
/// A vector of vectors of `UVec2` representing the raw edge points of each image.
#[inline]
#[must_use]
pub fn multi_image_edges_raw(&self) -> Vec<Vec<Vec2>> {
self.image_edges(false)
pub fn multi_image_edge_raw(&self) -> Vec<Vec<UVec2>> {
self.image_edges()
}

/// Takes `Edges` and a boolean to indicate whether to translate
/// the points you get back to either side of (0, 0) instead of everything in positive x and y.
#[must_use]
pub fn image_edges(&self, translate: bool) -> Vec<Vec<Vec2>> {
pub fn image_edges(&self) -> Vec<Vec<UVec2>> {
let image = &self.image;
// Marching squares adjacent, walks all the pixels in the provided data and keeps track of
// any that have at least one transparent / zero value neighbor then, while sorting into drawing
Expand All @@ -69,19 +94,7 @@ impl Edges {
.filter(|p| image.get(*p) && image.is_corner(*p))
.collect();

let objects: Vec<_> = self
.collect_objects(&corners)
.into_par_iter()
.map(|object| object.into_par_iter().map(|p| p.as_vec2()).collect())
.collect();
if translate {
objects
.into_par_iter()
.map(|object| self.translate(object))
.collect()
} else {
objects
}
self.collect_objects(&corners)
}

fn collect_objects(&self, corners: &[UVec2]) -> Vec<Vec<UVec2>> {
Expand Down Expand Up @@ -145,10 +158,28 @@ impl Edges {
/// # Returns
///
/// A vector of `Vec2` representing the translated coordinates.
#[inline]
#[must_use]
pub fn translate(&self, v: Vec<Vec2>) -> Vec<Vec2> {
pub fn translate(&self, v: Vec<UVec2>) -> Vec<Vec2> {
self.image.translate(v)
}

/// Translates an `Vec` of `Vec` of points in positive (x, y) coordinates to a coordinate system centered at (0, 0).
///
/// # Arguments
///
/// * `v` - An `Vec` of `Vec2` points to translate.
///
/// # Returns
///
/// A vector of vector of `Vec2` representing the translated objects.
#[inline]
#[must_use]
pub fn translate_objects(&self, v: Vec<Vec<UVec2>>) -> Vec<Vec<Vec2>> {
v.into_par_iter()
.map(|v| self.translate(v))
.collect::<Vec<_>>()
}
}

#[cfg(feature = "bevy")]
Expand Down Expand Up @@ -184,8 +215,8 @@ impl fmt::Debug for Edges {
"Edges {{{}\n}}",
format!(
"\nraw: {:#?},\ntranslated: {:#?}",
self.image_edges(false),
self.image_edges(true),
self.image_edges(),
self.translate_objects(self.image_edges())
)
.replace('\n', "\n "),
)
Expand Down
8 changes: 4 additions & 4 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn same_image_same_edges() {
include_bytes!("../assets/car.png"), // buffer
ImageType::Extension("png"),
CompressedImageFormats::default(),
true, //
true,
ImageSampler::default(),
RenderAssetUsages::default(),
)
Expand All @@ -40,16 +40,16 @@ fn same_images_same_edges() {
include_bytes!("../assets/boulders.png"), // buffer
ImageType::Extension("png"),
CompressedImageFormats::default(),
true, //
true,
ImageSampler::default(),
RenderAssetUsages::default(),
)
.unwrap();
let bevy_edges = Edges::from(bevy_image);

assert_eq!(
dynamic_edges.multi_image_edges_raw(),
bevy_edges.multi_image_edges_raw()
dynamic_edges.multi_image_edge_raw(),
bevy_edges.multi_image_edge_raw()
);
assert_eq!(
dynamic_edges.multi_image_edge_translated(),
Expand Down

0 comments on commit 1bb608c

Please sign in to comment.