Translating scipy.spatial's Voronoi diagrams into a set of cells bounded by a specified containing box, in easy-to-parse JSON.
Project description
Voronout is..
.. a Python module that, given..
- a set of points on a 2D plane bounded by
0 <= x <= 1and0 <= y <= 1 - the
planeWidthandplaneHeightto scale those points to
..outputs JSON describing the Voronoi diagram in that 2D plan.
The Voronoi computation is SciPy's. Voronout translates that into more easily parsible JSON:
{
"points": {.."<pointUUID>": {"x": <point.x>, "y": <point.y>}..},
"vertices": {.."<vertexUUID>": {"x": <vertex.x>, "y": <vertex.y>}..},
"edges": {.. "<edgeUUID>": {"vertex0Id": "<vertexUUID>", "vertex1Id": "<vertexUUID>"}..}
"regions": [
..
{
"siteIdentifier": "<UUID>",
"edges": [<edgeUUID>s composing the region]
}
..
],
"regionNeighbors: {
"<regionUUID>": [<regionUUID>s neighboring the region]
}
}
points are the points provided to compute the diagram. Each point (site) is associated with a region, a section of the 2D plane containing all points closer to the region's particular site than to any other.
points, like all coordinate data in this JSON, are indexed by unique UUID. This allows us to describe the region in terms of those UUIDs.
The primary use of that is with vertices - the vertices of the edges that bound the regions. Since any given Voronoi edge vertex is likely to be part of multiple edges, it looks better to describe that vertex by its associated UUID than to copy the same coordinate data multiple times.
vertices consist of vertices calculated when the diagram + vertices calculated when processing it. The latter case defines vertices that were found to fall outside the plane - x > 1 or < 0, y > 1 or < 0 - and consequently bounded within it.
We keep the diagram within the plane by..
- Determining which of its four boundaries it would intersect with
- Figuring out where the boundary and the edge, two lines, would intersect
- Replacing the " outside the plane " vertice with that point of intersection
regions combines the above information:
siteIdindicates whichpointthe region was computed with respect toedgesis the UUIDs of the edges bounding the region, mapping toedgesone level above
regionNeighbors indicates the regions that neighbor each region.
How do we generate a diagram?
We first determine our list of points, taking (0, 0) as the top left corner of the plane:
basePoints = tuple((
Point(.25, .25),
Point(.40, .75),
Point(.75, .25),
Point(.60, .75),
Point(.40, .40),
Point(.30, .30),
Point(.60, .30)
))
(The 0/1 bounding allows for intuitive specification of points. Instead of calculating the exact x and y coords in terms of the space width height you want, you can come up with points like (x = <25% of width>, y = <25% of width>) and scale the diagram data up appropriately after generating it.)
We then generate the diagram.
from voronout.VoronoiDiagram import VoronoiDiagram
voronoiDiagram = VoronoiDiagram(basePoints = basePoints, planeWidth = <plane width>, planeHeight = <plane height>)
From there, we can either process the info ourselves..
for voronoiRegion in voronoiDiagram.voronoiRegions.values():
for voronoiRegionEdge in voronoiRegion.edges:
# Do whatever you want with the borders of the region..
.. or write it out as JSON for something else to process:
from voronout.VoronoiDiagramToJSON import toJson
toJson(voronoiDiagram = voronoiDiagram, voronoiJsonPath = "voronoi.json")
How can we process a diagram?
Many ways - to quickly illustrate Voronout here, we'll draw generated diagrams with Matplotlib.
With code like..
planeWidth = 600
planeHeight = 600
basePoints = tuple((Point(x = random.random(), y = random.random()) for _ in range(<numBasePoints>)))
voronoiDiagram = VoronoiDiagram(basePoints = basePoints, planeWidth = 600, planeHeight = 600)
pyplot.ylim(bottom = planeHeight, top = 0)
for voronoiRegion in voronoiDiagram.voronoiRegions.values():
for voronoiRegionEdge in voronoiRegion.edges:
vertexIdentifier0 = voronoiRegionEdge.vertexIdentifier0
vertexIdentifier1 = voronoiRegionEdge.vertexIdentifier1
vertex0 = diagramVertices[vertexIdentifier0] if vertexIdentifier0 in diagramVertices else diagramVertices[vertexIdentifier0]
vertex1 = diagramVertices[vertexIdentifier1] if vertexIdentifier1 in diagramVertices else boundaryVertices[vertexIdentifier1]
pyplot.plot([vertex0.x, vertex1.x], [vertex0.y, vertex1.y])
.. we can create diagrams like this (numBasePoints = 5)..
.. or this (numBasePoints = 20)..
.. or this (numBasePoints = 100):
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file voronout-0.0.4.1.1.tar.gz.
File metadata
- Download URL: voronout-0.0.4.1.1.tar.gz
- Upload date:
- Size: 653.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b6c940edfc3113b032c36fbe466eb9c673d76ff9d7aeae2ba889824bf8cfdcb
|
|
| MD5 |
db29c52844b122120d3b373638be769b
|
|
| BLAKE2b-256 |
249f74414579275917cbc405be647bb479cc3988409919b053696430559bd9f5
|
File details
Details for the file voronout-0.0.4.1.1-py3-none-any.whl.
File metadata
- Download URL: voronout-0.0.4.1.1-py3-none-any.whl
- Upload date:
- Size: 20.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e73c31676209aa09714bf8e96ca32f80c765174d60d6c790de5b12982202785f
|
|
| MD5 |
9a5c2c6cb6772ec607f01b425f1f15b1
|
|
| BLAKE2b-256 |
7c991d5b464928985b20554ba23c144678fddd59ace76f2d62499b2802af432b
|