python shapely intersection function: clock-wise or counter-clock-wise - python

I have used the Shapely polygon intersection function:
object.intersection(other)
and get inconsistency in the direction and order of the vertices of the output polygon.
Is there a way to have a systematic set of outputs, or should I run through the output polygon and sort it?

You may get better answers on https://gis.stackexchange.com/
Double check that you are using the right DE-9IM method. DE-9IM Wikipedia
If I understand you correctly with the systematic outputs you have multiple LinearRings inside your polygon and you want a separate result for each vs just having a single Boolean result for the entire polygon intersecting with other. The easiest way which is slower is to iterate through your polygon and compare each LinearRing. The faster way is to use sr-trees with the python package "rtrees". https://gis.stackexchange.com/a/119935/60045

Related

Merge two point clouds with known positions (python, OpenCV) - Transformation

I've two point clouds and the exact positions (coordinates, quaternions). Now I want to transform the second point clouds into the first.
I tried to get the difference of the two coordinates and quaternions and then I've translated the second coordinates with the difference to the first and the same with a rotation matrix of the quaternions. I can't get a useful outcome.
Is there any function in python / OpenCV that would do that for me automatically?
Thanks for help.
It's not completely clear to me want you're trying to do but it sounds like you're looking for scipy.spatial.transform.Rotation, specifically align_vectors().

Create a convex Polygon from unordered vertices

Short version:
Given: Vertices of a convex 3d-Polygon
Looking for: The edges connecting the vertices, s.t. the Polygon is convex.
Long version:
I started with a bunch of 3d-points and calculated the voronoi tessalation using this function from the scipy.spatial package. I'm actually are looking for the right edges to get the polygons created by it, but I do not understand how the package does it.
At the moment I just figured out how to get the vertices and to which polygon they belong, but knowing the vertices of a polygon alone would not be enough to recreate it unless I'd get the edges. So how do I get them? Is there maybe a simple way to do it or even better: a package which does it for me?
Since I know it must be convex I had an idea for the 2d-case (basically just circling around the centre (the mean) of the polygon and connecting the vertices following each other), but I don't know if this approach would work in 3d or if it would even create a convex polygon. I also think this approach would be probably not reliable and/or take a lot of time to compute.
I can supply code if wanted, but I don't think it would help.
Thanks bb1 for the answer :)
scipy.spatial.ConvexHull does the job. (Technically it didn't work 100%, but I think that might be a different problem which occurs in 3d.)

Finding a nonlinear mapping between two sets of data points

I have two sets of data points; effectively, one is from a preimage and the other from its image, but I do not know the rule between the two. This rule/function is nonlinear.
I've collected many data points of corresponding locations on both images, and I was wondering if anyone knew of a way to find a more complete mapping. That is, does anyone know the best way to find a mapping from R^2 to R^2 with an extensive set of sample points. This mapping is one-to-one and onto.
My goal is to use the data I've found to find a polynomial function that takes in some x,y coordinate from the preimage, and outputs the shifted coordinates.
edit: I have sample points along the domain and their corresponding points in the image, but not for every point in the domain. I want to be able to input any point (only integer values) in the domain and output the shifted point.
I don't think polynomial is easy (or easy to guarantee is a bijection). The obvious thing to do is to
Construct the delaunay triangulation of the known points in the domain.
For each delaunay triangle the mapping is just the linear mapping which interpolates the map on the vertices.
Then, when you have a random point, look up its delaunay triangle, and apply the requisite map.
I believe that all of the above can be done via scipy.spatial.delaunay.
The transformation you're trying to find sounds a lot like what's accomplished in Geographic Information Systems using a technique called rubber-sheeting https://en.wikipedia.org/wiki/Rubbersheeting
Igor Rivin's description of a process using a Delaunay triangulation is pretty much the solution that's used in such systems. Some systems will use a Barycentric coordinate system rather than a linear mapping to try to reduce the appearance of triangle-related artifacts in the transformed image.
What you are describing also sounds a bit like the "morphing" special effect used in video. Maybe a web search on that topic would turn up some leads for you.

Constructing convex hull object with known triangular faces

TLDR: I need to construct a python object for fast interior point testing, similar to a SciPy ConvexHull or DelaunayTriangulation. The catch is that I know ahead of time the order in which the triangulation of the points must be constructed: (6 points, 8 triangular faces, with a specific ordering of each face). In effect, I already know what the convex hull should be, but I need it in a form that I can use with existing (and optimised!) libraries (eg Scipy spatial). How can I do this?
Context:
I need to construct a triangular prism (imagine a Toblerone bar - 2 end faces, 6 side faces, all triangular) in order to do some interior point testing. As I will have many such prisms lying adjacent to each other (adjacent on their side faces, imagine many Toblerone bars stood on their ends and next to each other), I need to be careful to ensure that no region in space is contained by two adjacent prisms. The cross section of the prism will not generally be uniform, hence the possibility of overlap between adjacent prisms, as illustrated by this diagram of the approximately planar face between two adjacent prisms:
____
|\ /|
| \/ |
| /\ |
|/__\|
Note the two different diagonals constructed along the face - this is the problem. One prism may split the face into two triangles using the \ diagonal, and the neighbouring prism may instead use the /. In order to ensure no overlap between adjacent prisms, I need to explicitly control the order in which the triangles are formed so that they always use the same diagonal. This I can do: for each prism that I need to construct, I know ahead of time in what order the triangular faces should be constructed. Here's an illustration of two adjacent prisms, with the correct shared diagonal between them: neighbouring prisms, shared diagonal
My issue is with performing fast interior point testing with these prisms. Previously, I was using the approach linked in this answer: Delaunay(prism_points).find_simplex(test_points) >= 0. It's quick because it is using highly optimised library code, but I have no control over the construction of the triangulation, so there could be overlap.
If I construct the hulls as explicit np.array objects (vertices, faces) then I can use my own code to do the tests (there are numerous possible approaches, I'm projecting rays and testing for intersection with each triangular face). The problem is that this is around ~100x slower than the find_simplex() approach mentioned earlier. Whilst I'm sure I could get the code a bit quicker, it is worth pointing out this code is already fairly optimised from another use case with Cython - I am not sure if I can find all the extra speed I need here. As for the inevitable "do you really need the speed question", please take my word for it. This is turning a 5 minute job into many hours.
What I need is to construct an object I can use with external optimised libraries, whilst retaining control of the triangular faces. Adding extra Cython to my code is of course an option, but which such highly optimised code already out there, using that would be vastly preferable.
Thanks to anyone that can help.
Half a solution... Not an exact solution to the original question, but a different way of achieving the same outcome. Any triangular prism can be split into exactly three tetrahedra (see http://www.alecjacobson.com/weblog/?p=1888). This is a specific case of the fact that any polyhedron may be split into tetrahedra by connecting all faces to one vertex, if the faces does not already include it.
Knowing exactly how I would like the face triangles of my prism to be arranged, I can work out what three tetrahedra would reproduce the same configuration of triangles (with extra faces of course being added inside the original prism itself). I then form Delaunay triangulations around each of these three tetrahedra (ie, collections of 4 points) in turn and perform the original interior point tests: if it matches on any then I have a positive result for the whole prism. The key point is that by only giving four points to the Delaunay constructor at a time, I know exactly what triangulation it will return as there is only one way of forming such a tetrahedra (assuming no geometric degeneracy).
It's a bit longwinded, and involves 3x as many tests as I would like, but it's a start. If anyone in the future does know how I could do this better please do let me know.

Python line-plane collision detection

I need to work out whether a line segment will intersect a face of a 3D polygon (so a section of a plane confined by 4 points). Are there any well known python libraries that I could use? If not, how should I go about this?
I've tried looking at line-plane intersection equations but they seem over complicated as I only need to know if it will intersect, not find the point of intersection.
Sorry if this question has been asked before! Thanks
There's no way to do this that's simpler than the obvious one:
find the infinite plan that contains your polygon
determine the point where the line intersects that plane
determine whether this point is within the polygon
All but the very rare line will intersect the infinite plan somewhere, so the question isn't "does it intersect the plane", but "where"?
Each step in the above will be about as simple as a 3D geometry question can get, but still involve many the 3D mathematical tools (vectors, matrices, etc). There are lots of descriptions about how to do them, and I recommend that you search around for one that's presented at a level and in a notation that you're comfortable with. For example, for the line (infinite) plane intersection you could look here.

Categories

Resources