Skip to main content

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 <= 1 and 0 <= y <= 1
  • the planeWidth and planeHeight to 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>}..},
    "regions": [
        ..
        {
            "siteIdentifier": "<pointUUID>",
            "edges": [
                ..
                {
                    "vertexIdentifier0": <vertexUUID0>,
                    "vertexIdentifier1": <vertexUUID1>,
                    "neighborSiteIdentifier": <pointUUID>
                }
                ..
            ]
        }
        ..
    ]
}

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:

  • siteId indicates which point the region was computed with respect to
  • edges is the edges bounding the region
    • Each edge indicates the two vertices composing it and, via neighborSiteId, the region immediately opposite to it

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

voronout_5_points

.. or this (numBasePoints = 20)..

voronout_20_points

.. or this (numBasePoints = 100):

voronout_100_points

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

voronout-0.0.4.0.1.tar.gz (226.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

voronout-0.0.4.0.1-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

File details

Details for the file voronout-0.0.4.0.1.tar.gz.

File metadata

  • Download URL: voronout-0.0.4.0.1.tar.gz
  • Upload date:
  • Size: 226.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for voronout-0.0.4.0.1.tar.gz
Algorithm Hash digest
SHA256 ca619bf217aac200fc1124a740e55379bd00d808c7e4d8af4ce04d5a9d5045c2
MD5 083087824b1b44022290fa8ab2eaa6a7
BLAKE2b-256 1797fe238a0d1a59ea5c71221dd0dcd47916663be0f89539ed58a57923f03594

See more details on using hashes here.

File details

Details for the file voronout-0.0.4.0.1-py3-none-any.whl.

File metadata

  • Download URL: voronout-0.0.4.0.1-py3-none-any.whl
  • Upload date:
  • Size: 21.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for voronout-0.0.4.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 729cd52c14b1c951240c70a7f322aa2bd5f26b410cc842998213fe1469c28f6b
MD5 d79ed8f41872880515c009591f5345dd
BLAKE2b-256 9fdb617381dd8144c499ee25525d9d1ea9a07aa4d731ce90b348a9b0749261fa

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page