How to check if given 3d point is outside convex hull - convex-hull

I'm working on a science project. I have a list of xyz-coordinates of voronoi diagram vertices, and a list of points that create my protein's convex-hull (from triangulation). Now some of the vertices lie quite far from the hull and I'd like to filter them out. How can I do it in c++ ? For now it doesn't have to be super optimised, I'm only focused on removing those points.
visualization
I was also thinking to check if a line from voronoi vertex(red crosses) to center of protein(pink sphere) is intersecting with hull face at any point, but I'm not sure how to achive that.
I've read that you can check if a point is inside a polygon by counting the times an infinite line from the point is crossing the hull, but that was for two dimensions. Can similar approach be implemented to suit my needs ?
https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/

Let's start with the two-dimensional case. You can find the solution to that on CS.SX:
Determine whether a point lies in a convex hull of points in O(logn)
The idea is that each two consecutive points on the convex hull define a triangular slice of the plane (to some internal point of the hull); you can find the slice in which your point is located, and check whether it's closer to the internal point than the line segment bounding the slide.
For the three-dimensional case, I'm guessing you should be able to do something similar, but the search for the 3 points forming the relevant triangle might be a little tricky. In particular, it would also depend on how the convex hull is represented - as in the 2-d case the convex hull is just a sequence of consecutive points on a cycle.
I know this doesn't quite solve your problem but it's the best I've got given what you've written...

Related

Convex Hull with a predefined number of vertices

I am working on image segmentation and I thought the convex hull can provide me with a simple solution to my problem. Currently I have polygons with for sides (see image below). Due to image processing issues, the shape does not have clean straight sides and hence when I use the standard convex hull (in Matlab) I may get more than the four main corners to define it.
My goal is to force the convex hull algorithm to find the best 4 vertices that will enclose my polygons (i.e. 4 best enclosing vertices per polygon). Is this possible? An example code will be appreciated.
Thanks
The problem of the minimum area bounding polygon is briefly mentioned in "Geometric applications of a matrix-searching algorithm" (see Applications section). It is not simple and is probably not the way for you.
For an easier (but approximate) answer to your question, you can consider the four cardinal directions and find the farthest points in these, which define a quadrilateral. (Also consider the four intermediate directions, which are more appropriate for an axis-aligned rectangle.)
If you insist having an enclosing quadrilateral, you can translate the four edges to the farthest points in the respective perpendicular directions, and find the pairwise intersections.
If you insist having a rectangle, compute the convex hull and find the minimum area or minimum perimeter bounding rectangle by the Rotating Calipers method. https://geidav.wordpress.com/tag/rotating-calipers/

How to find closest points between two convex hull in MATLAB?

In part of an Artificial Neural Network matlab code, I want to find nearest points of two convex polygons.
I saw
dsearchn(X,T,XI)
command's description here, but that finds closest points between two sets of points, and polygons (like convexs) have infinites points.
So can you suggest any way/idea?
Notice: I'm using MATLAB 2014a. I have the coordinate of each convex's vertex point.
If you are not happy with what is provided by dsearchn, then, If I were you, I would do one of two following:
Find Nearest Neighbours on the vertices (for example which vertex of
polygon A is the NN of a given vertex of polygon B).
Pick a random point inside polygon A (you may want to compute the
convex hull of A, but you may skip that and take into account only
the vertices you know already). That random point is the query. Find
an NN of that point from the vertices of polygon B.
You may want to ask in Software recommendations for more.
Edit:
Another approach is this:
Create a representative dataset of polygon A. Set the size of the dataset yourself and fill it with samples of points that lie inside the polygon. Choose them uniformly randomly inside the polygon.
Then take a point of polygon B (either a vertex or a random point inside polygon B) and that's the query point, for which you will seek Nearest Neighbour(s) inside the representative dataset of polygon A.
Of course that's just an approximation, but I can't think of something else now.
Notice that you can of course do the same for polygon B.
With This File in File Exchange, I've find the solution.
With a little code modification, I have drawn a perpendicular bisector which I wanted. Of course, this way is time consuming.

Computing distance from a point to a triangulation in 3D with Matlab

I have a 3D point cloud that I've transformed into a Delaunay triangulation using Matlab's function DelaunayTri. Now I have a test point in 3D and want to compute, in Matlab, the smallest distance between this point and the triangulation.
So far, I've thought of using the nearestNeighbor(...) member function of the DelaunayTri class in Matlab to find the point in the triangulation closest to my test point and then computing the distance between them. That is something but it is not what I really want.
The closest point on the triangulation to my test point is, in general, not a vertex of the triangulation but somewhere on the face of a triangle. How can I find this point?
Thank you!!!
I've written codes for these things, but they are not on the File Exchange. I can be convinced to give them out by direct mail though.
It is relatively easy to find the distance to a convex hull, but not trivial. A delaunay tessellation is bounded by the convex hull anyway. So you can convert that tessellation into the convex hull easily enough, or just use the convex hull. Note that a convex hull is often a terribly poor approximation for many purposes, especially if you are using this for color mapping, which is perhaps the most common thing I see this used for. In that case, an alpha shape is a far better choice. Alpha shapes also will have a triangulated boundary surface, though in general it will not be convex.
So, to find the nearest point on a convex triangulation:
Convert to the convex boundary surface, i.e., the convex hull. This reduces to finding those triangles which are not shared between pairs of tetrahedra. Internal facets will always appear exactly twice in the list of all facets. This trick also works for non-convex tessellations of course, so for alpha shapes.
Compute a bounding circumcircle for each triangular surface facet. This allows you to know when to stop checking facets.
Get the distances to each point on the surface. Sort each facet by the distance to the nearest point in that facet. Start by looking at the nearest facet in that list.
Compute the distance to the apparently nearest facet found in step 3. A simple solution is found by least distance programming (LDP), which can be converted to a constrained linear least squares. Lawson & Hanson have an algorithm for this.
Repeat step 4 until the current best distance found is less than the distance, comparing it to any of the circumcircles from step 2. This loop will be quite short really, at least for a convex hull. For a more general non-convex hull from an alpha shape, it may take more time.
You can also reduce the search space a bit by excluding the facets from your search that point AWAY from the point in question. Use those facet normals for this test.
I wrote the tool point2trimesh for this problem. It's kind of a "brute force" solution which works also for non-convex surfaces.

I have a point set in image,how to find the External irregular shape circling all the points by Matlab

I got a point set after executing SIFT algorithm.Now I want to find the external irregular shape of those points.Is there anyone knowing the relevant functions in Matlab? Notice that I don't want the convex hall.Thank you!
If you want convex hull, (unclear based on your comment... you can edit questions btw), look up convhull.
If you don't want convex hull, a Delaunay triangulation will probably get you started since the result captures both the convex hull of the points, but also the internal structure such that you may be able to remove some edges from the outside of the returned triangulation.

How do I break a polyhedron into tetrahedra in MATLAB?

I have a polyhedron, with a list of vertices (v) and surfaces (s). How do I break this polyhedron into a series of tetrahedra?
I would particularly like to know if there are any built-in MATLAB commands for this.
For the convex case (no dents in the surface which cause surfaces to cover each other) and a triangle mesh, the simple solution is to calculate the center of the polyhedron and then connect the three corners of every face with the new center.
If you don't have a triangle mesh, then you must triangulate, first. Delaunay triangulation might help.
If there are holes or caves, this can be come arbitrarily complex.
I'm not sure the OP wanted a 'mesh' (Steiner points added) or a tetrahedralization (partition into tetrahedra, no Steiner points added). for a convex polyhedron, the addition of Steiner points (e.g. the 'center' point) is not necessary.
Stack overflow will not allow me to comment on gnovice's post (WTF, SO?), but the proof of the statement "the surfaces of a convex polyhedron are constraints in a Delaunay Tesselation" is rather simple: by definition, a simplex or subsimplex is a member in the Delaunay Tesselation if and only if there is a n-sphere circumscribing the simplex that strictly contains no point in the point set. for a surface triangle, construct the smallest circumscribing sphere, and 'puff' it outwards, away from the polyhedron, towards 'infinity'; eventually it will contain no other point. (in fact, the limit of the circumscribing sphere is a half-space; thus the convex hull is always a subset of the Delaunay Tesselation.)
for more on DT, see Okabe, et. al, 'Spatial Tesselations', or any of the papers by Shewchuk
(my thesis was on this stuff, but I remember less of it than I should...)
I would suggest trying the built-in function DELAUNAY3. The example given in the documentation link resembles Aaron's answer in that it uses the vertices plus the center point of the polyhedron to create a 3-D Delaunay tessellation, but shabbychef points out that you can still create a tessellation without including the extra point. You can then use TETRAMESH to visualize the resulting tetrahedral elements.
Your code might look something like this (assuming v is an N-by-3 matrix of vertex coordinate values):
v = [v; mean(v)]; %# Add an additional center point, if desired (this code
%# adds the mean of the vertices)
Tes = delaunay3(v(:,1),v(:,2),v(:,3)); %# Create the triangulation
tetramesh(Tes,v); %# Plot the tetrahedrons
Since you said in a comment that your polyhedron is convex, you shouldn't have to worry about specifying the surfaces as constraints in order to do the triangulation (shabbychef appears to give a more rigorous and terse proof of this than my comments below do).
NOTE: According to the documentation, DELAUNAY3 will be removed in a future release and DelaunayTri will effectively take its place (although currently it appears that defining constrained edges is still limited to only 2-D triangulations). For the sake of completeness, here is how you would use DelaunayTri and visualize the convex hull (i.e. polyhedral surface) as well:
DT = DelaunayTri(v); %# Using the same variable v as above
tetramesh(DT); %# Plot the tetrahedrons
figure; %# Make new figure window
ch = convexHull(DT); %# Get the convex hull
trisurf(ch,v(:,1),v(:,2),v(:,3),'FaceColor','cyan'); %# Plot the convex hull