3D Polygons in Python - python

As far as I am aware there is no inbuilt polygon functionality for Python. I want to create a 3D map and figured that polygons would be the best way to go about it.
Not wanting to reinvent the wheel I did some googling and found that there's a lot of Python stuff out there, but I couldn't find what I wanted. Thus before I reinvent the wheel (or invent it a whole), does anybody know of a Polygon system for Python?
Note that it does need to be 3D (I found quite a few 2D ones). Note also that I am not interested in the displaying of them but in storing them and the datastructure within Python.
Thanks

One of the most complete geography/mapping systems available for Python that I know about is GeoDjango. This works on top of the Django, an MVC framework. With it comes a large collection of polygon, line and distance calculation tools that can even take into account the curvature of the earth's surface if need be.
With that said, the quickest way I can think of to produce a 3D map is using a height map. Create a two dimensional list of tuples containing (x, y, z) coordinates. Each tuple represents an evenly spaced point on a grid, mapped out by the dimensions of the array. This creates a simple plane along the X and Z axes; the ground plane. The polygons that make up the plane are quads, a polygon with four sides.
Next, to produce the three dimensional height, simply give each point a Y value. This will create peaks and valleys in your ground plane.
How you render this will be up to you, and converting your grid of points into a polygon format that something like OpenGL can understand may take some work, but have a look at Visual Python, its the simplest 3D library I've seen for Python.

I think you mean Polyhedron, not Polygon .. and you might wanna look at vpython

CGAL is a C++ geometry library which, amongst other things, models Polyhedra (3D flat-surfaced shapes)
It has Python bindings available. The documentation for the Polygon3 class is here:
http://cgal-python.gforge.inria.fr/Manual/CGAL.Polyhedron.html#Polyhedron_3

Related

Finding coordinates of a triangle Mesh

I have a triangle mesh and look for a way to get programmatically for a given (x,z) 2D point all y coordinates which are represented by the mesh (x,y1,z),(x,y2,z) ..., preferable in python. I have the mesh stored in one of the common file formats (.stl , .obj ...)
The problem behind this question is that i convert a 2D face image into a 3D mesh of the face (using the marvelous https://github.com/sicxu/Deep3DFaceRecon_pytorch project) and then want to map the depth information of the 3D model back to the 2d image (to build something fancy in blender ...)
I finally found a solution for the problem which is both slow and inelegant but does the job for me for now.
I use section_multiplane function of the trimesh python library for that. Basically i use this function to intersect the mesh with 2d planes parallel to the y,z-plane and calculate the depth values by analyzing the resultant 2D Path. The library is very fast in calculating the intersections but the part i wrote - extracting the depth information from the 2D Paths - is painfully slow right now. (which doesnt matter in my particular application)
The code for that I have now is deeply interwoven in my particular application so it doesn't make sense to share it but if someone is interested in this approach there is a very helpful example included in the trimesh library which covers the crucial points: section_multiplane example
I am sure there are much more elegant solutions for this problem available but I wanted to share this approach in case somebody struggles finding a better approach too ...
Heatmap displaying the extracted depth information:
depth info:

Finding the intersection(s) of two 3D Polylines

I currently have 2 paths generated by a motion planning algorithm in Python. I interpolated into two 3D Polylines with equidistant points. Next I need to find whether these two paths of different lengths intersect (OR even ideally the point(s) at which they get near a certain distance of each other, if any). This is to eventually simulate a collision avoidance algorithm.
After much searching I have found multiple libraries and algorithms that can return points/line intersections of two Polylines, however they are all in 2D. I tried using the Bentley-Ottmann algorithm/python library, the Shapely library, the Geometry3D library, and other 2D solutions found on SO, but none have worked in a 3D environment. Next I tried using a 2D geometry library to find the intersections in the x-y, x-z, and y-z planes individually as shown in the graphs below showing a simple case, but I'm not sure where to go from there or whether that's even the right approach. The Polylines would usually contain 300+ points.
Any help pointing me towards a 3D algorithm or simple adjustments to make to the 2D libraries work in 3D would be really appreciated!
3D graph view, x-y view, z-y view.

Is there a Python library to do constrained triangulations in 3D?

I have a function whose range is a non-convex, simply connected region in R3. When I sample the function I know if the resulting point is on the surface of the region or not. I'd like to triangulate those samples subject to the surface constrains, i.e., the resulting tetrahedra should not
"hide" surface points. The hull would not be convex, of course.
I searched around for a library. So far I found Triangle, but it only works in R2. I also found TetGen, which works in R3, but it requires to provide
the surface triangulation (which I don't have). Also, as far as I can see, these C/C++ libraries do not have Python bindings.
Any suggestions? Thanks!
You may take a look at CGAL it has python-bindings.
One side note: If you need the surface triangulation (which seems to be a 2D problem), you may take each face, project it to 2D, triangulate it, and back to your 3D face.
EDIT due to comment: CGAL does "only" 3D triangulation. 3D constrained triangulation requires Steiner points. Since not every input can be triangulated in 3D (Schönhardt polyhedron as the classical counter-example).
Maybe you can take a look at MeshPy it looks like it has what you are looking for: "MeshPy provides Python interfaces to three well-regarded mesh generators, Triangle by J. Shewchuk, TetGen by Hang Si, and gmsh by Christophe Geuzaine and Jean-Francois Remacle."

Can someone explain how mypoly is used in Python with GPS coordinates?

I understand I can define a polygon in Python using "mypoly".
myPoly = Polygon(p1,p2,p3,…) #list of points
However, what I really want to do is to find a way to use Python to work with polygons that I define with GPS coordinates. The coordinates would show shapes similar to the following:
multiple polygons http://www.jvanderhook.info/images/slabs/drawing_1.png
(the image came from this question on Robotics Stack Exchange
I understand I should be able to work with a polygon once I have it defined, but this is new to me. How can I define one with GPS coordinates? Do I simply replace p1, p2, p3, ... above with the GPS coordinates?
Yes, you can use the gps coordinates to define your polygons. However, understand that these coordinates are typically in degrees (+/- 0-90 lat, +/- 0-180 lon).
Unfortunately, these things aren't as simple as they initially seem they should be. It might help to get comfortable with map projections, here's a good start:
http://kartoweb.itc.nl/geometrics/map%20projections/mappro.html
Then, you may be better off converting to a projection like 900913 (google maps projection) that is in meters and makes points easier to manage.
I tend to use django (which uses geos/gdal/proj4 and is a bit of a pain to get everything working right) for geometry manipulation and transforms. (shapely and pyproj seem like good alternatives if your not using django already)

Mapping a texture to a mayavi mesh

I'm using mayavi in python to visualize some data. I have a surface in 3D space and I want to map an image to it. I have looked at mayavi documentation and all I could find was this, which really doesn't help me much. Has anybody done something like this before?
I found that the mesh function actually does take a scalars argument, which then colors the corresponding point on the surface in accordance to the chosen colormap. I still however don't know how to map an arbitrary RGB image to the surface.
I guess you didn't saw the chapter "Python scripting for 3D plotting". There is a collection of the mayavi commands which can be used inside python. For example the section "3D Plotting functions for numpy arrays". If you follow the links you find some description about the functions and a small example for each of them.
For your case you should take mesh or surf. Both functions are kind of equal, but are different. For example the surf function is more a subfunction of mesh, which is my opinion. You don't really have to define x and y-coordinates, you just need the z-coordinates in a 2D array. If you want to use mesh you have to pass all three coordinates (x,y,z) as 2D arrays.
I would suggest to check the examples.
Is this an answer to your question? Maybe you should be more specific about what you want to do.
Have a nice day and Happy Holidays.

Categories

Resources