Skip to content
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

Add method to check if point is inside cell #2905

Merged
merged 25 commits into from
Jul 7, 2022
Merged

Conversation

MatthewFlamm
Copy link
Contributor

Overview

Add method to check if point is inside a specific cell. This was motivated partly by the discussion in #2703 (reply in thread)

Details

Related to #2897.

Should we allow a Sequence of points (if more than one is provided we can return a numpy ndarray)? If so, this might complicate any plans to allow for Iterables, Sequences, or similar of cell indices.

@tkoyama010 tkoyama010 added the enhancement Changes that enhance the library label Jun 29, 2022
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
Copy link
Member

@tkoyama010 tkoyama010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding a useful method.

@codecov
Copy link

codecov bot commented Jun 29, 2022

Codecov Report

Merging #2905 (8eb80f5) into main (7092937) will decrease coverage by 0.04%.
The diff coverage is 96.96%.

@@            Coverage Diff             @@
##             main    #2905      +/-   ##
==========================================
- Coverage   94.12%   94.08%   -0.05%     
==========================================
  Files          76       76              
  Lines       16525    16558      +33     
==========================================
+ Hits        15554    15578      +24     
- Misses        971      980       +9     

@MatthewFlamm MatthewFlamm mentioned this pull request Jun 29, 2022
11 tasks
@MatthewFlamm
Copy link
Contributor Author

Unlike DataSet.find_containing_cell, there is only minor efficiency gained by allowing multiple points to be supplied here. The inefficiency here is multiple calls to GetCell(ind) and GetPoints().GetNumberOfPoints() (I'm assuming the calls to vtk.mutable are cheap), which would be avoided if we allowed multiple points to be supplied to this function. Should we implement multiple points here? I'm not sure based on some discussions about also supporting multiple cells here #2897 (comment). If we support multiple points and multiple cells, then it becomes unclear, at least to me, what the 2D output structure should look like.

cc @adeak and @banesullivan

tkoyama010
tkoyama010 previously approved these changes Jun 29, 2022
Copy link
Member

@tkoyama010 tkoyama010 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to wait for a reply to the above, otherwise, it is LGTM.

Copy link
Member

@adeak adeak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike DataSet.find_containing_cell, there is only minor efficiency gained by allowing multiple points to be supplied here. The inefficiency here is multiple calls to GetCell(ind) and GetPoints().GetNumberOfPoints() (I'm assuming the calls to vtk.mutable are cheap), which would be avoided if we allowed multiple points to be supplied to this function. Should we implement multiple points here? I'm not sure based on some discussions about also supporting multiple cells here #2897 (comment). If we support multiple points and multiple cells, then it becomes unclear, at least to me, what the 2D output structure should look like.

My main reason for contemplating multiple points/cells as input is not that of performance (I don't typically associate VTK's API with vectorization), but rather convenience. If one has more than one point to check it would be nicer to call the method once with every point, rather than using something like a list comprehension outside the function, point by point.

This ties in with your question: if the API becomes confusing in the "multiple points, multiple cells" case then we should just forbid it. It's not convenient to have to look up the docs to see how it behaves.

That being said, I could imagine that if either points or cells are multiple then we return an array of bools (as large as the number of input points or cells), and if both are sequences then we return a 2d array of shape (n_points, n_indices) or (n_indices, n_points). But again, this might be too smart for its own good.

pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Show resolved Hide resolved
@MatthewFlamm
Copy link
Contributor Author

I agree on the convenience factor when using one or the other. I find the convenience factor will drop when using both together. I will never remember which comes first in the indices. But, maybe this is a rare enough use case that it isn't worth trying to over optimize the API. I'll implement multiple points

Co-authored-by: Andras Deak <adeak@users.noreply.github.com>
tkoyama010
tkoyama010 previously approved these changes Jun 30, 2022
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Show resolved Hide resolved
pyvista/core/dataset.py Show resolved Hide resolved
pyvista/utilities/common.py Outdated Show resolved Hide resolved
tests/utilities/test_common.py Show resolved Hide resolved
pyvista/utilities/common.py Outdated Show resolved Hide resolved
pyvista/utilities/common.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
pyvista/core/dataset.py Outdated Show resolved Hide resolved
@akaszynski akaszynski added this to the 0.36.0 milestone Jul 7, 2022
@akaszynski
Copy link
Member

Since 0.35 will be released soon, I'd like this to wait for the next minor (0.36) release. Best to not add any last second features.

Good news is we won't wait 3 months to get out the next release. There are a few critical features that we need much earlier, so expect 0.36.0 to be out in a month.

@adeak
Copy link
Member

adeak commented Jul 7, 2022

Oh, I forgot that this was waiting for me, I'm sorry @MatthewFlamm.

@akaszynski I think this is good to go, if you can give me ten minutes to make sure.

@akaszynski
Copy link
Member

Oh, I forgot that this was waiting for me, I'm sorry @MatthewFlamm.

@akaszynski I think this is good to go, if you can give me ten minutes to make sure.

Absolutely. Approve and I'll merge when you're ready.

Comment on lines +2635 to +2636
raise RuntimeError(
f"Computational difficulty encountered for point {node} in cell {ind}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two cosmetic remarks here that we could do:

  1. skip coverage for the esoteric error case to make coverage happy
  2. also add the point index i in the error.

I find it much more valuable to get this feature out in v0.35 than to fix this "should almost never happen" edge case and add a doc-build-length delay. We can follow up for the subsequent release if we want to.

Approving, thanks and sorry for forgetting about this.

pyvista/core/dataset.py Show resolved Hide resolved
@adeak adeak merged commit fdc66f3 into main Jul 7, 2022
@adeak adeak deleted the add_cell_is_point_inside branch July 7, 2022 21:56
@adeak adeak modified the milestones: 0.36.0, 0.35.0 Jul 7, 2022
@MatthewFlamm MatthewFlamm mentioned this pull request Jul 8, 2022
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Changes that enhance the library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants