-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework of edges
#5
Conversation
Added: - function `new` to take `Edges` from any data. - `BinImage` structure to represent an image. - `Point` alias to represent a point in the image. Moved: - structure `Edges` moved to lib.rs. - tests to separate file. - `distance` function to utils. Removed: - `hashbrown`, `mashmap` and `thiserror` from dependencies.
Hi @salam99823 thanks for the PR! I'll be able to give it a proper look weekend. |
The example fails doc tests because `BinImage` is a private structure.
`width` and `height` fields are now private to ensure `BingImage` is immutable. To get `width` and `height` there are methods of the same name in `BinImage`. The `points_to_drawing_order` function has been moved out of the `Edges` implementation, as the usage of the `translate` argument has been moved to `image_edges`.
Hi @shnewto, I'm currently thinking about how to solve this problem. I think can add an |
I also think that the /// If there's only one sprite / object in the image, this returns just one, with
/// coordinates translated to either side of (0, 0)
#[must_use]
pub fn single_image_edge_translated<T: Into<BinImage>>(image: T) -> Vec<Vec2> {
image_edges(image, true).into_iter().flatten().collect()
}
// ...
/// 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<T: Into<BinImage>>(image: T, translate: bool) -> Vec<Vec<Vec2>> {
let image: BinImage = image.into();
// 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
// order, groups them into sets of connected pixels
let edge_points = (0..image.height() * image.width())
.map(|i| (i / image.height(), i % image.height()))
.map(|(x, y)| UVec2::new(x, y))
.filter(|p| image.get(*p))
.filter(|p| (0..8).contains(&image.get_neighbors(*p).iter().filter(|i| **i).count()))
.collect();
points_to_drawing_order(edge_points)
.into_iter()
.map(|group| {
let group = group.into_iter().map(|p| p.as_vec2()).collect();
if translate {
image.translate(group)
} else {
group
}
})
.collect()
}
#[must_use]
pub fn translate(image: impl Image, v: Vec<Vec2>) -> Vec<Vec2> {
image.translate(v)
}
impl Image for BinImage {
fn width(&self) -> u32 {
self.width()
}
fn height(&self) -> u32 {
self.height()
}
}
pub trait Image {
fn width(&self) -> u32;
fn height(&self) -> u32;
fn translate_point(&self, p: Vec2) -> Vec2 {
Vec2::new(
p.x - (self.width() as f32 / 2.0 - 1.0),
(self.height() as f32 / 2.0 - 1.0) - p.y,
)
}
fn translate(&self, v: Vec<Vec2>) -> Vec<Vec2> {
v.into_iter().map(|p| self.translate_point(p)).collect()
}
} |
@salam99823 I've been thinking some about that issue as well, I've been wondering whether we could get a little stricter about what constitutes a connected edge for drawing order by checking whether the on/off coordinates of a neighboring pixel continue the current pixel's edge in the correct coordinates. something like:
I think that approach might actually reduce the complexity of the current approach that's doing some acrobatics to get the edges it's able to currently. |
wrt whether to deprecate the |
Deprecating the |
Interesting solution, I may try to do this. |
Finally, the edge collection algorithm has been rewritten to work with diagonal lines and objects with holes.
Hey @salam99823 how are you feeling about the changes so far? i.e ready for earnest review or still working through some things? I'm in no rush, just don't want to let anything sit if it doesn't need to. Very interested in your revisions to edge collecting! |
I consider the work finished. |
This is a really fantastic contribution @salam99823. I really appreciate all the work you put into this crate and the quality of the code you committed. Thank you!! |
I am very glad that I was able to contribute to this crate. Now i work on |
Changes:
Added:
glam-latest
for those who useglam
.new
to takeEdges
from any data.BinImage
structure to represent an image.Moved:
Edges
moved to lib.rs.distance
function to utils.Moved to
BinImage
implementationtranslate_vec
renamed totranslate
.xy_translate
renamed totranslate_point
.get_at
renamed toget
.Removed:
hashbrown
,mashmap
andthiserror
from dependencies.march_edges
function.Error
and moduleerror
.translate_vec
,march_edges
functions from public API.EdgesDisplay
structure.Other
bevy
replaced withbevy_math
andbevy_render
.