How to find any point lying inside polyhedron - python

I am trying to identify whether a point lies inside or outside of a polyhedron. How would I do this in Python?

Related

Make a condition if a polygon contains a point

I need to set up a condition using shapely that accuses whether or not a point belongs to a polygon.
If the value is true, a message "point belongs to polygon" should be displayed. If the point does not belong, then the message "the point does not belong to the polygon" should be displayed.
Also, if the point belongs to the polygon, it must be shown which polygon the point belongs to.
I tried to set up a condition using the "if" inside the "for" but it didn't work.

How to keep points inside a polygon and remove other points?

I have a 3d point cloud as shown in the first figure. I want to detect and draw polygons around all the boundaires present in the point cloud, I mean main polygon that represents the outer boundaries of the point cloud and another polygons that defines the inner boundaries as shown in figure 2. I tried convex hull but it only gets points representing the outer boundaries. The idea is that I want polygons defining all the boundaries such that later on if I have any points lying inside the inner boundaries or outside the outer boundaries I can remove them. Any ideas how I can do that in python?

How to determine whether a point is inside or outside a 3D model computationally

I have a .obj and .ply file of a 3D model.
What I want to do is read this 3D model file and see if a list of 3D coordinates are either inside or outside the 3D model space.
For example, if the 3D model is a sphere with radius 1, (0,0,0) would be inside (True) and (2,0,0) would be outside (False). Of course the 3D model I'm using is not as simple as a sphere.
I would like to add some of the methods I considered using.
Since I am using Python, I thought of using PyMesh, as their intersection feature looked promising. However the list of coordinates I have are not mesh files but just vectors, so it didn't seem to be the appropriate function to use.
I also found this method using ray casting. However, how to do this with PyMesh, or any other Python tool is something I need advice on.
Cast a ray from the 3D point along X-axis and check how many intersections with outer object you find.
Depending on the intersection number on each axis (even or odd) you can understand if your point is inside or outside.
You may want to repeat on Y and Z axes to improve the result (sometimes your ray is coincident with planar faces and intersection number is not reliable).
Converting my comment into an answer for future readers.
You can use a Convex Hull library to check whether a point is inside the hull. Most libraries use signed distance function to determine whether the point is inside. trimesh is one of the libraries that implements this feature.

Distance outside shapely.Polygon

How do I establish the distance a point is from a polygon? If the point is within the polygon the result should be 0.
I'm guessing something like:
def distance_from(poly,point):
if poly.contains(point): return 0
return poly.exterior.distance(point)
I'd like to clarify things up a bit:
The instances which are passed to the distance_from function are made of the classes shapely.geometry.polygon.Polygon and shapely.geometry.point.Point from the shapely.geometry module of the Shapely Python package for computational geometry. (For more info on shapely and geometric objects have a look in the manual : Shapely Python Package, Geometric Objects).
The distance method of a geometric object returns the minimum float distance to another geometric object as described here in the manual. The exterior of a polygon is an instance of shapely.geometry.polygon.LinearRing which makes up the boundary of a polygon (although it is not to be mixed up with the boundary attribute of a polygon which returns a LineString). So for the case above you don't need to to explicitly use poly.exterior.distance(point) because the minimum distance is the same for the whole polygon as it is for its boundary. They have exactly the same shape only a different geometry type.
It would be less error prone if you would just use poly.distance(point) because that way if the point of interest lies within the interior (more info here) of the polygon it is directly clear that it contains it, so you do not need an explicit check and also the distance from a point within the interior to itself is then 0. It would be different if you would try to get the distance from the exterior (which, as explained above, is a LinearRing, i.e. a closed LineString) to a point which lies in the "inner" part. In this case you would get the minimum distance between the point in the "inner" part and the surrounding linear ring.
For further info about binary relationships (i.e between a geometric object and another, which covers contains, within, touches etc.) refer to this part of the manual and for those interested in the underlying DE-9IM relationships (which are a way to clearly identify the way a geometric objects relates to another) refer to this part.
Shapely is a python wrapper for the GEOS suite which itself is a C++ port of JTS. On the JTS page under the link Feature sheet more information can be found about how spatial relationships are checked and computed

Indexing in python quadtree

I have a set of points with their coordinates (latitudes, longigutes), and I also have a region (a box). All the points are inside the box.
Now I want to distribute the points into small cells (rectangles) considering the density of points. Basically, a cell containing many points should be subdivided into smaller ones, while keeping larger cells with small number of points.
I have check this question, which has almost the same problem with me but I couldn't find a good answer. I think I should use a quad tree, but all implementations I found doesn't provide that.
For example, this library allows making a tree associated with a box, and then we can insert boxes as follows:
spindex = Index(bbox=(0, 0, 100, 100))
for item in items:
spindex.insert(item, item.bbox)
But it doens't allow inserting points. Moreover, I'll need to get the IDs of cells (or names), and check if a point belongs to a given cell.
Here is another lib I found. It does allow inserting points, but it doens't give me the cell's ID (or name), so I cannot check if a point belongs to a certain cell. Notice that the tree should do the decomposition automatically.
Could you please suggest me a solution?
Thanks.
Finally, I ended up using Google's s2-geometry-library with Python wrapper. In fact, each cell created by this lib is not a rectangle (it's a projection), but it satisfies my need. The lib already divided the earth surface into cells at different levels (quad tree). Given an point (lat,lng), I can easily get a corresponding cell at leaf level. From these leaf nodes, I go up and merge cells based on what I need (number of points in a cell).
This tutorial explains everything in details.
Here is my result:

Categories