Spatial Data Structures and Algorithms (scipy.spatial)
scipy.spatial can compute triangulations, Voronoi diagrams, and
convex hulls of a set of points, by leveraging the Qhull library.
Moreover, it contains KDTree implementations for nearest-neighbor point
queries, and utilities for distance computations in various metrics.
Delaunay triangulations
The Delaunay triangulation is a subdivision of a set of points into a
non-overlapping set of triangles, such that no point is inside the
circumcircle of any triangle. In practice, such triangulations tend to
avoid triangles with small angles.
Delaunay triangulation can be computed using as follows:
The structure of the triangulation is encoded in the following way:
the simplices attribute contains the indices of the points in the
points array that make up the triangle. For instance:
What this tells us is that this triangle has triangle #0 as a neighbor,
but no other neighbors. Moreover, it tells us that neighbor 0 is
opposite the vertex 1 of the triangle:
>>> points[tri.simplices[i,1]]array([ 0. , 1.1])
Indeed, from the figure, we see that this is the case.
Qhull can also perform tessellations to simplices for
higher-dimensional point sets (for instance, subdivision into
tetrahedra in 3-D).
Coplanar points
It is important to note that not all points necessarily appear as
vertices of the triangulation, due to numerical precision issues in
forming the triangulation. Consider the above with a duplicated
point:
Observe that point #4, which is a duplicate, does not occur as a
vertex of the triangulation. That this happened is recorded:
>>> tri.coplanararray([[4, 0, 3]], dtype=int32)
This means that point 4 resides near triangle 0 and vertex 3, but is
not included in the triangulation.
Note that such degeneracies can occur not only because of duplicated
points, but also for more complicated geometrical reasons, even in
point sets that at first sight seem well-behaved.
However, Qhull has the “QJ” option, which instructs it to perturb the
input data randomly until degeneracies are resolved:
Two new triangles appeared. However, we see that they are degenerate
and have zero area.
Convex hulls
A convex hull is the smallest convex object containing all points in a
given point set.
These can be computed via the Qhull wrappers in scipy.spatial as
follows:
>>> fromscipy.spatialimportConvexHull>>> rng=np.random.default_rng()>>> points=rng.random((30,2))# 30 random points in 2-D>>> hull=ConvexHull(points)
The convex hull is represented as a set of N 1-D simplices,
which in 2-D means line segments. The storage scheme is exactly the
same as for the simplices in the Delaunay triangulation discussed
above.
A Voronoi diagram is a subdivision of the space into the nearest
neighborhoods of a given set of points.
There are two ways to approach this object using scipy.spatial.
First, one can use the KDTree to answer the question “which of the
points is closest to this one”, and define the regions that way:
Negative value -1 again indicates a point at infinity. Indeed,
only one of the regions, [0,1,3,2], is bounded. Note here that
due to similar numerical precision issues as in Delaunay triangulation
above, there may be fewer Voronoi regions than input points.
The ridges (lines in 2-D) separating the regions are described as a
similar collection of simplices as the convex hull pieces:
These numbers present the indices of the Voronoi vertices making up the
line segments. -1 is again a point at infinity — only 4 of
the 12 lines are a bounded line segment, while others extend to
infinity.
The Voronoi ridges are perpendicular to the lines drawn between the
input points. To which two points each ridge corresponds is also
recorded:
>>> defmandala(n_iter,n_points,radius):... """Creates a mandala figure using Voronoi tessellations....... Parameters... ----------... n_iter : int... Number of iterations, i.e. how many times the equidistant points will... be generated.... n_points : int... Number of points to draw per iteration.... radius : scalar... The radial expansion factor.
>>> # Modify the following parameters in order to get different figures>>> n_iter=3>>> n_points=6>>> radius=4