Find all integer values contained within an irregular volume
5 次查看(过去 30 天)
显示 更早的评论
I'm setting up a 3D volume within which points will be distributed, and want to mark some areas as "inaccessible". The areas are generated using e.g. a parametric description of ellipsoids:
% starting parameters
axes = randi(10,1,3) ; % generate axes lengths
theta = linspace(-0.5*pi,0.5*pi) ; % eccentric anomaly
lambda = linspace(0,2*pi) ; % azimuthal angle
angles = combvec(theta,lambda) ; % all angles for evaluation
% evaluate ellipsoid points
points = axes .* [cos(angles(1,:)) .* cos(angles(2,:)) ; ...
cos(angles(1,:)) .* sin(angles(2,:)) ; ...
sin(angles(1,:))] ;
% convert to integers, find unique rows
unique_points = unique(floor(points), 'rows') ;
At this point, I have a set of integer points for an ellipsoid centred on the origin. But I would also like all integers contained within these points. Additionally, in the real version, these ellipsoids can be asymmetric across the origin, are rotated, and offset. They might also be cylinders. The shape is determined by the user at initialisation.
Lacking a precise equation to describe such an irregular shape, how can I find all integer values contained within the ellipsoid? The only way I can think of at the moment requires nested for-loops, which takes TIME. I suspect there is a way to vectorise this code, but I can't see it at the moment.
EDIT: clarified use of "irregular shape/volume" whilst giving an example ellipsoid.
2 个评论
John D'Errico
2020-8-11
"I'm setting up a 3D volume within which points will be distributed"
That seems to imply you want to eventually generate random numbers, or something like that. What are you really looking to do in the end with this?
Will the volume ALWAYS be an ellipsoid? Or will it be irregular? Thus anything? It seems like you are using an ellipsoid, so admit it is so, instead of just calling it irregular. Something that is truly irregular is vastly different from an ellipsoid, and you may be able to use the information about it being an ellipsoid if it truly is one.
So... Are you looking to generate the set of all points on an integer lattice that lie within a fully general ellipsoid in 3-d?
If so, then what you are doing does not seem to achieve that, thus working with floating point grids in spherical coordinates, then converting to cartesian, then rounding. If you don't want to miss points inside that volume, then you need to work in cartesian coordinates from the start.
And so, my next question is, how large is the ellipsoid? That is, unless it has a very large volume, the simplest and probably fastest solution really is to just use meshgrid/ndgrid, and then test the set of all lattice points in that volume, accepting that you will be testing many points. Since the test will be fully vectorized, it will still be fast even if the sampling is overly done. 3-d is not that huge of a space to search, and 10-1 is not that big of an aspect ratio, if that is as large as it gets.
采纳的回答
Cris LaPierre
2020-8-10
I wonder if the functions inpolygon and inpolyhedron might be helpful here. See this post about finding the points inside a 3D volume as well.
3 个评论
Cris LaPierre
2020-8-11
I found the following pages describing inpolyhedron. The challenge would be getting your shapes to be triangulated meshes.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Volume Visualization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!