Skip to content

Commit

Permalink
a little cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
shnewto committed Mar 4, 2024
1 parent 1ca3449 commit 83d3680
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@

get the edges of objects in images with transparency

# using

```rust
use edges::Edges;
use std::path::Path;

fn main() {
let image = image::open(Path::new("assets/car.png"));
let edges = Edges::from(image.unwrap());
println!("{:#?}", edges.single_image_edge_translated());
}
```

## disclaimer

this existed first as implementation over in a crate called [bevy_collider_gen](<https://github.com/shnewto/bevy_collider_gen>). at the time, that's what it seemed most useful for. this crate represents me starting to wonder whether it's useful for something else.
Expand Down
2 changes: 1 addition & 1 deletion examples/bevy-image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ fn main() {

// get the image's edges
let edges = Edges::from(image.unwrap());
println!("{:#?}", edges);
println!("{:#?}", edges.single_image_edge_translated());
}
4 changes: 1 addition & 3 deletions examples/dynamic-image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ use std::path::Path;

fn main() {
let image = image::open(Path::new("assets/car.png"));

// get the image's edges
let edges = Edges::from(image.unwrap());
println!("{:#?}", edges);
println!("{:#?}", edges.single_image_edge_translated());
}
14 changes: 7 additions & 7 deletions src/edges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,30 @@ impl Edges {
/// If there's only one sprite / object in the image, this returns just one, with
/// coordinates translated to either side of (0, 0)
pub fn single_image_edge_translated(&self) -> Vec<Vec2> {
self.image_to_edges(true).into_iter().flatten().collect()
self.image_edges(true).into_iter().flatten().collect()
}

/// 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
pub fn single_image_edge_raw(&self) -> Vec<Vec2> {
self.image_to_edges(false).into_iter().flatten().collect()
self.image_edges(false).into_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)
pub fn multi_image_edge_translated(&self) -> Vec<Vec<Vec2>> {
self.image_to_edges(true)
self.image_edges(true)
}

/// 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
pub fn multi_image_edges_raw(&self) -> Vec<Vec<Vec2>> {
self.image_to_edges(false)
self.image_edges(false)
}

/// Takes a Bevy DynamicImage type and an 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
pub fn image_to_edges(&self, translate: bool) -> Vec<Vec<Vec2>> {
pub fn image_edges(&self, translate: bool) -> Vec<Vec<Vec2>> {
let rows = self.height();
let cols = self.width();
let data: &[u8] = self.bytes();
Expand Down Expand Up @@ -253,8 +253,8 @@ impl fmt::Debug for Edges {
}

let edges_display = EdgesDisplay {
raw: self.image_to_edges(false),
translated: self.image_to_edges(false),
raw: self.image_edges(false),
translated: self.image_edges(false),
};
write!(f, "{:#?}", edges_display)
}
Expand Down

0 comments on commit 83d3680

Please sign in to comment.