Python: Minimum Convex Polygon? - python

I've accidentally found an interesting way of representing data in the book: Comparative Vertebrate Neuroanatomy by Ann B. Butler. Below is the image with its description from the book. Does anyone know how to code such a plot in python?

This is called the Convex Hull. Many libraries including, for example, scipy, have algorithms to find the Convex Hull. And then Python has many different libraries for plotting a polygon, including, for example, matplotlib.

Related

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.

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.)

Why does scipy.spatial.Voronoi() output zero regions even if there are no duplicate input points?

Why are some Voronoi regions empty?
I have a point cloud of the surface of a human that I'm trying to find the Voronoi diagram of. So I found scipy.spatial.Voronoi(), including some pretty good documentation. But sometimes vor.regions includes empty regions, and the documentation doesn't quite explain why they're there. At best it explains the relation to its Delaunay calculation "Note here that due to similar numerical precision issues as in Delaunay triangulation above, there may be fewer Voronoi regions than input points." But if so, it's only because qhull calculates Voronoi using a similar (or the exact same) algorithm as it uses for Delaunay Triangulation that this happens. Mathematically, the only reason a Voronoi region should be empty is if its points are duplicated
Specific goals:
I'm looking to replicate the results from this paper that use the Voronoi diagrams to calculate normal vectors at each input point. So I could do a workaround where I use a similar normal to a nearby point's properly calculated normal, or I could ignore any point that gives a zero Voronoi region. But I'd really like to know how to solve these empty regions so I can use as much of the input data as possible.
Background and other research
This answer suggests Voronoi(points, qhull_options='Qbb Qc Qx') and in the docs scipy suggests qhull_options="QJ Pp"
input data: http://columbia.edu/~nxb2101/minimal_stickfig_.npy
Code:
import numpy as np; np.set_err(all='raise')
import scipy.spatial
pt_cloud=np.load('minimal_stickfig_.npy')
vor=scipy.spatial.Voronoi(pt_cloud)
for idx in range(len(vor.regions)):
region=vor.regions[idx]
vertices = vor.vertices[region]
hull=scipy.spatial.ConvexHull(vertices)
volume=hull.volume
triangle_mesh_hull=vertices[hull.simplices] # (n,3,3)
# triang mesh calculation taken from
# https://stackoverflow.com/questions/26434726/return-surface-triangle-of-3d-scipy-spatial-delaunay/26516915
inner_pt = np.mean(vertices[:2],axis=0).reshape((1,3))
CoM=np.zeros((3,))
pif("inner_pt is {0}".format(inner_pt))
for triangle in triangle_mesh_hull:
pif("triangle is: \n{0}".format(triangle))
tetra=np.concatenate((inner_pt,triangle),axis=0)
CoM_tetra=np.mean(tetra,axis=0)
vol_tetra=volume_tetra(tetra)
CoM+=(CoM_tetra*vol_tetra)
CoM /= volume
# do more with CoM, and volume
Any kind of help is appreciated; if you're not sure why qhull does this or how to get around it, please let me know what you did to make a high-quality mesh from a point cloud

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."

create equations from lines in image in python 2.7

What python library would be useful in creating an equation from an image of straight edges. Something that would take an image, divide it up into a xy grid, recognize that there is a straight line and find the linear equation of that line with respect to the xy grid.
What I plan to do is detect the two sides of a rectangular shape and calculate the equations of the perpendicular lines. I don't need any code but the names of some relevant imaging libraries would be helpful.
You could look at OpenCV's Cascade Qualifier. It is a C library, runs on most platforms, has a python interface and can be used to detect different shapes. I believe it is what many websites use to detect faces in photos. I had looked into it as a way of automatically generating 3d building models from photos.

Categories

Resources