Skip to content

Commit

Permalink
Converted to package.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yatoom committed Mar 28, 2018
1 parent 0db15f2 commit 1328029
Show file tree
Hide file tree
Showing 36 changed files with 107 additions and 65 deletions.
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@

A Python implementation of Fortune's algorithm based on the description of "Computational Geometry: Algorithms and Applications" by de Berg et al. The algorithm handles the special cases described in the book. The bounding box is generalized to handle a convex polygon.

## Manual Installation

First, clone the repository and then install the package.
```bash
git clone https://github.com/Yatoom/voronoi.git
cd voronoi
python setup.py install
```

## Example usage

Example that uses a polygon as a bounding box.

```python
from algorithm import Algorithm
from graph import Polygon, Point
from voronoi import Voronoi, Polygon, Point, BoundingBox

# Define a set of points
voronoi_points = [
points = [
Point(2.5, 2.5),
Point(4, 7.5),
Point(7.5, 2.5),
Expand All @@ -23,7 +31,7 @@ voronoi_points = [
]

# Define a bounding box
polygon_points = [
polygon = Polygon([
Point(2.5, 10),
Point(5, 10),
Point(10, 5),
Expand All @@ -32,13 +40,13 @@ polygon_points = [
Point(2.5, 0),
Point(0, 2.5),
Point(0, 5),
]
])

# Initalize the algorithm
v = Algorithm(Polygon(polygon_points))
# Initialize the algorithm
v = Voronoi(polygon)

# Create the diagram
v.create_diagram(points=voronoi_points, vis_steps=False, verbose=False, vis_result=True, vis_tree=True)
v.create_diagram(points=points, vis_steps=False, verbose=False, vis_result=True, vis_tree=True)

# Get properties
edges = v.edges
Expand Down
3 changes: 0 additions & 3 deletions events/__init__.py

This file was deleted.

6 changes: 0 additions & 6 deletions graph/__init__.py

This file was deleted.

6 changes: 0 additions & 6 deletions graph/test.py

This file was deleted.

4 changes: 0 additions & 4 deletions nodes/__init__.py

This file was deleted.

2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
16 changes: 16 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from setuptools import setup
setup(
name='voronoi',
packages=['voronoi', 'voronoi.visualization', 'voronoi.tree', 'voronoi.nodes', "voronoi.graph", "voronoi.events", "voronoi.tests"],
version='0.9',
description="Fortune's algorithm for Voronoi diagrams with polygon bounding boxes.",
author='Jeroen van Hoof',
author_email='[email protected]',
url='https://github.com/Yatoom/voronoi',
download_url='',
keywords=['voronoi', 'polygon', 'fortune', 'algorithm'],
classifiers=[],
install_requires=[
"numpy", "matplotlib",
]
)
2 changes: 0 additions & 2 deletions tree/__init__.py

This file was deleted.

2 changes: 0 additions & 2 deletions visualization/__init__.py

This file was deleted.

2 changes: 2 additions & 0 deletions voronoi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from voronoi.algorithm import Algorithm as Voronoi
from voronoi.graph import Point, Polygon, BoundingBox
21 changes: 15 additions & 6 deletions algorithm.py → voronoi/algorithm.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from queue import PriorityQueue
from graph import HalfEdge, Vertex, Algebra
from nodes import LeafNode, Arc, Breakpoint, InternalNode
from events import CircleEvent, SiteEvent
from tree import SmartTree, SmartNode
from visualization import visualize
from visualization import Tell
from voronoi.graph import HalfEdge, Vertex, Algebra
from voronoi.nodes import LeafNode, Arc, Breakpoint, InternalNode
from voronoi.events import CircleEvent, SiteEvent
from voronoi.tree import SmartTree, SmartNode
from voronoi.visualization import visualize
from voronoi.visualization import Tell


class Algorithm:
Expand Down Expand Up @@ -51,6 +51,15 @@ def initialize(self, points):
return self.event_queue

def create_diagram(self, points: list, vis_steps=False, vis_result=False, vis_tree=False, verbose=False):
"""
Create the Voronoi diagram.
:param points: (list) The list of cell points to make the diagram for
:param vis_steps: (bool) Visualize intermediate steps
:param vis_result: (bool) Visualize the final result
:param vis_tree: (bool) Visualize the status of the binary tree (text-based)
:param verbose: (bool) Print debug information
"""

# Initialize all points
self.initialize(points)
Expand Down
3 changes: 3 additions & 0 deletions voronoi/events/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from voronoi.events.circle_event import CircleEvent
from voronoi.events.event import Event
from voronoi.events.site_event import SiteEvent
10 changes: 5 additions & 5 deletions events/circle_event.py → voronoi/events/circle_event.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import math
from decimal import *

from events.event import Event
from graph.point import Point
from nodes.leaf_node import LeafNode
from nodes.arc import Arc
from visualization import Tell
from voronoi.events.event import Event
from voronoi.graph.point import Point
from voronoi.nodes.leaf_node import LeafNode
from voronoi.nodes.arc import Arc
from voronoi.visualization import Tell


class CircleEvent(Event):
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions events/site_event.py → voronoi/events/site_event.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from events.event import Event
from graph.point import Point
from voronoi.events.event import Event
from voronoi.graph.point import Point


class SiteEvent(Event):
Expand Down
6 changes: 6 additions & 0 deletions voronoi/graph/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from voronoi.graph.half_edge import HalfEdge
from voronoi.graph.point import Point
from voronoi.graph.vertex import Vertex
from voronoi.graph.polygon import Polygon
from voronoi.graph.algebra import Algebra
from voronoi.graph.bounding_box import BoundingBox
2 changes: 1 addition & 1 deletion graph/algebra.py → voronoi/graph/algebra.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
from graph import Point
from voronoi.graph import Point


class Algebra:
Expand Down
2 changes: 1 addition & 1 deletion graph/bounding_box.py → voronoi/graph/bounding_box.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from graph import Polygon, Point
from voronoi.graph import Polygon, Point


class BoundingBox(Polygon):
Expand Down
2 changes: 1 addition & 1 deletion graph/half_edge.py → voronoi/graph/half_edge.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from graph.vertex import Vertex
from voronoi.graph.vertex import Vertex


class HalfEdge:
Expand Down
15 changes: 13 additions & 2 deletions graph/point.py → voronoi/graph/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@ class Point:
A simple point
"""

def __init__(self, x=None, y=None, player: int = None, name=None, first_edge=None):
def __init__(self, x=None, y=None, metadata=None, name=None, first_edge=None):
"""
A point in 2D space.
:param x: (float) The x-coordinate
:param y: (float) The y-coordinate
:param metadata: (dict) Optional metadata stored in a dictionary
:param name: (str) A one-letter string (assigned automatically by algorithm)
:param first_edge: (HalfEdge) Pointer to the first edge (assigned automatically by the algorithm)
"""
if metadata is None:
metadata = {}

self.metadata = metadata
self.x: float = x
self.y: float = y
self.player = player
self.name = name
self.first_edge = first_edge

Expand Down
6 changes: 3 additions & 3 deletions graph/polygon.py → voronoi/graph/polygon.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from graph import Point, Vertex, HalfEdge
from graph.algebra import Algebra
from voronoi.graph import Point, Vertex, HalfEdge
from voronoi.graph.algebra import Algebra
import numpy as np

from visualization import Tell
from voronoi.visualization import Tell


class Polygon:
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions voronoi/nodes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from voronoi.nodes.arc import Arc
from voronoi.nodes.breakpoint import Breakpoint
from voronoi.nodes.internal_node import InternalNode
from voronoi.nodes.leaf_node import LeafNode
2 changes: 1 addition & 1 deletion nodes/arc.py → voronoi/nodes/arc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from graph.point import Point
from voronoi.graph.point import Point


class Arc:
Expand Down
2 changes: 1 addition & 1 deletion nodes/breakpoint.py → voronoi/nodes/breakpoint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
from graph.point import Point
from voronoi.graph.point import Point


class Breakpoint:
Expand Down
2 changes: 1 addition & 1 deletion nodes/internal_node.py → voronoi/nodes/internal_node.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tree.smart_node import SmartNode
from voronoi.tree.smart_node import SmartNode


class InternalNode(SmartNode):
Expand Down
4 changes: 2 additions & 2 deletions nodes/leaf_node.py → voronoi/nodes/leaf_node.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from nodes import Arc
from tree.smart_node import SmartNode
from voronoi.nodes import Arc
from voronoi.tree.smart_node import SmartNode


class LeafNode(SmartNode):
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/random-triangle.py → voronoi/tests/random-triangle.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import random

from algorithm import Algorithm
from graph import Polygon, Point
from voronoi.algorithm import Algorithm
from voronoi.graph import Polygon, Point

x = 100
y = 100
Expand Down
8 changes: 4 additions & 4 deletions tests/unit.py → voronoi/tests/unit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from algorithm import Algorithm
from graph import Polygon
from graph.bounding_box import BoundingBox
from graph.point import Point
from voronoi.algorithm import Algorithm
from voronoi.graph import Polygon
from voronoi.graph.bounding_box import BoundingBox
from voronoi.graph.point import Point


# -----------------
Expand Down
2 changes: 2 additions & 0 deletions voronoi/tree/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from voronoi.tree.smart_node import SmartNode
from voronoi.tree.smart_tree import SmartTree
File renamed without changes.
2 changes: 1 addition & 1 deletion tree/smart_tree.py → voronoi/tree/smart_tree.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tree.smart_node import SmartNode
from voronoi.tree.smart_node import SmartNode


class SmartTree:
Expand Down
2 changes: 2 additions & 0 deletions voronoi/visualization/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from voronoi.visualization.tell import Tell
from voronoi.visualization.visual import visualize
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import matplotlib.patches as patches
import matplotlib.pyplot as plt
import numpy as np
from events import CircleEvent
from voronoi.events import CircleEvent


def visualize(y, current_event, bounding_poly, points, vertices, edges, arc_list, event_queue, calc_cell_sizes=True):
Expand Down

0 comments on commit 1328029

Please sign in to comment.