-
Notifications
You must be signed in to change notification settings - Fork 307
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
Array bounds/contains check? #1247
Comments
You're working with |
Hm, good question. If it is okay to think out loud for a minute: A thing I often do is use ndarray with glam::IVec2. Then when I need to load from ndarray at a particular coordinate I call a function like However, I think I can reproduce this situation with pure NDarray. Imagine if I had a
And I could write:
Could I have written this code as only usizes, without using the isize indices? Not really, because I can't freely do math on those (for example I can't subtract 1 from the x axis without risking underflow). I'd have to add tests before subtracting, or something… it wouldn't be as convenient, in this case. So, I think that contains() with usize indices would make sense and be useful, because in a situation where you already have usize indexes (which you usually do) but you are for whatever reason not sure if they're in-bounds for all axes, you could call it (and it would be more apt than And a variant with isize-version indices, which is hardwired to just always return "false" if it finds less-than-zero components, would be situationally convenient, if someone happens to be on their own representing coordinates as isizes so they can do math on them. But I don't think this would be necessary, because the user could easily replicate it themselves by testing * Probably more information than you need: The way I normally do this is
But this requires me to keep, for each array2, a separate ivec containing the array2's size… with a hypothetical contains(), I would not need to keep the "size" vector around and could do:
|
Thank you for adding more details. Now that I think about it, we have a similar problem in medical imaging where we need to take a NxNxN patch around all voxels and apply some functions, or anything that works near the borders. |
This is not a counter argument to having a And of course, it does depend on whether the domain allows for sensible padding value. But for example, in a simulation where the array represented a map of cells, some of which were unreachable, it did change things at all to add some padding of unreachable cells outside which would never change the results. |
Is this still something that's wanted? It's still marked as "good first issue", and I'm looking for ways to contribute, so I could probably implement this. Also, it would seem like a natural extension to an API like this would be for it to handle checking if an index is inside a slice (regardless of whether it's inside the full array). Should I work on that as well? |
@dacid44 Imo, yes, it would be used by some people, including me at my job.
I'm not sure I understand. Can you please rephrase? |
I find myself very frequently having a coordinate and an ndarray Array2/Array3. I want to check whether the coordinate is within the bounds of the Array2/Array3 (IE, gte than 0,0,0, less than the array size). There doesn't appear to be a way to do this. There is "get" to return a value or None but that is not the same thing. Sometimes I want to know if the value is inbounds without, at that time, accessing the value.
It would be helpful to add a within(), contains() etc function to ArrayBase (I think adding it to Dim would also serve equivalent purposes).
(If this already exists and I just missed it, sorry)
The text was updated successfully, but these errors were encountered: