Skip to content

API of class Voronoi

Source code in voromesh/voronoi/__init__.py
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class Voronoi:
    def __init__(self, points, buffer_size=1, boundary=None):
        """
        Bounded 2D Voronoi decomposition for given points.
        The boundary is either calculated from the buffered convex hull of the
        given points with the given buffer_size (default) or passed directly.

        Parameters
        ----------
        points : numpy.array of shape (npoints, ndim=2)
            Coordinates of points to construct a Voronoi diagram from.
        buffer_size : float, Optional
            Buffer for convex hull >= 0.
        boundary : shapely.geometry.polygon.Polygon, Optional
            Boundary for Voronoi decomposition.

        Returns
        -------
        None.

        """

        n = shape(points)

        if not n[1] == 2:
            raise TypeError("Points needs to be an array of shape Nx2, but is "
                            + str(n[0]) + "x" + str(n[1]) + "!")

        if buffer_size < 0:
            raise ValueError("Buffer_size needs to be >= 0!")

        if boundary is None:
            self.buffer_size = buffer_size
            boundary = convex_hull(points, buffer_size)
        elif type(boundary) is Polygon:
            self.buffer_size = None
        else:
            self.buffer_size = None
            raise Warning("Boundary is not of type"
                          + "shapely.geometry.polygon.Polygon.\n"
                          + "This can lead to unexpected results!")

        self.voronoi, self.points = voronoi_bound(points, boundary)

    def plot(self, center=False, tri=False):
        """
        plots the Voronoi diagramm.

        Parameters
        ----------
        center : Bool, optional
            Add center points to plot. The default is False.
        tri : Bool, optional
            Add Dilaunay triangulation to plot. The default is False.

        Returns
        -------
        None.

        """
        if tri:
            tri = Delaunay(self.points)
            plt.triplot(self.points[:, 0], self.points[:, 1], tri.simplices)

        if center:
            plt.plot(self.points[:, 0], self.points[:, 1], 'o')

        for r in self.voronoi.geoms:
            plt.fill(*zip(*array(list(
                zip(r.boundary.coords.xy[0][:-1],
                    r.boundary.coords.xy[1][:-1])))),
                alpha=0.4)

        plt.axis('equal')
        plt.show()

    def to_vtk(self, decimalplace=8, dim="Z"):
        """
        Converts the Voronoi diagram to a vtk unstructured grid.

        Parameters
        ----------
        decimalplace : int, optional
            _description_. Defaults to 8.
        dim : str, optional
            _description_. Defaults to "Z".

        Returns:
            vtkUnstructuredGrid: VTK unstructured grid
        """

        return to_vtk(self.voronoi, decimalplace, dim)

    def to_pyvista(self, decimalplace=8, dim="Z"):
        """
        Converts the Voronoi diagram to a pyvista unstructured grid.

        Returns
        -------
            pyvista.UnstructuredGrid: Pyvista unstructured grid

        """
        return to_pyvista(self.voronoi, decimalplace, dim)

__init__(points, buffer_size=1, boundary=None)

Bounded 2D Voronoi decomposition for given points. The boundary is either calculated from the buffered convex hull of the given points with the given buffer_size (default) or passed directly.

Parameters

points : numpy.array of shape (npoints, ndim=2) Coordinates of points to construct a Voronoi diagram from. buffer_size : float, Optional Buffer for convex hull >= 0. boundary : shapely.geometry.polygon.Polygon, Optional Boundary for Voronoi decomposition.

Returns

None.

Source code in voromesh/voronoi/__init__.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def __init__(self, points, buffer_size=1, boundary=None):
    """
    Bounded 2D Voronoi decomposition for given points.
    The boundary is either calculated from the buffered convex hull of the
    given points with the given buffer_size (default) or passed directly.

    Parameters
    ----------
    points : numpy.array of shape (npoints, ndim=2)
        Coordinates of points to construct a Voronoi diagram from.
    buffer_size : float, Optional
        Buffer for convex hull >= 0.
    boundary : shapely.geometry.polygon.Polygon, Optional
        Boundary for Voronoi decomposition.

    Returns
    -------
    None.

    """

    n = shape(points)

    if not n[1] == 2:
        raise TypeError("Points needs to be an array of shape Nx2, but is "
                        + str(n[0]) + "x" + str(n[1]) + "!")

    if buffer_size < 0:
        raise ValueError("Buffer_size needs to be >= 0!")

    if boundary is None:
        self.buffer_size = buffer_size
        boundary = convex_hull(points, buffer_size)
    elif type(boundary) is Polygon:
        self.buffer_size = None
    else:
        self.buffer_size = None
        raise Warning("Boundary is not of type"
                      + "shapely.geometry.polygon.Polygon.\n"
                      + "This can lead to unexpected results!")

    self.voronoi, self.points = voronoi_bound(points, boundary)

plot(center=False, tri=False)

plots the Voronoi diagramm.

Parameters

center : Bool, optional Add center points to plot. The default is False. tri : Bool, optional Add Dilaunay triangulation to plot. The default is False.

Returns

None.

Source code in voromesh/voronoi/__init__.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def plot(self, center=False, tri=False):
    """
    plots the Voronoi diagramm.

    Parameters
    ----------
    center : Bool, optional
        Add center points to plot. The default is False.
    tri : Bool, optional
        Add Dilaunay triangulation to plot. The default is False.

    Returns
    -------
    None.

    """
    if tri:
        tri = Delaunay(self.points)
        plt.triplot(self.points[:, 0], self.points[:, 1], tri.simplices)

    if center:
        plt.plot(self.points[:, 0], self.points[:, 1], 'o')

    for r in self.voronoi.geoms:
        plt.fill(*zip(*array(list(
            zip(r.boundary.coords.xy[0][:-1],
                r.boundary.coords.xy[1][:-1])))),
            alpha=0.4)

    plt.axis('equal')
    plt.show()

to_pyvista(decimalplace=8, dim='Z')

Converts the Voronoi diagram to a pyvista unstructured grid.

Returns

pyvista.UnstructuredGrid: Pyvista unstructured grid
Source code in voromesh/voronoi/__init__.py
102
103
104
105
106
107
108
109
110
111
def to_pyvista(self, decimalplace=8, dim="Z"):
    """
    Converts the Voronoi diagram to a pyvista unstructured grid.

    Returns
    -------
        pyvista.UnstructuredGrid: Pyvista unstructured grid

    """
    return to_pyvista(self.voronoi, decimalplace, dim)

to_vtk(decimalplace=8, dim='Z')

Converts the Voronoi diagram to a vtk unstructured grid.

Parameters

decimalplace : int, optional description. Defaults to 8. dim : str, optional description. Defaults to "Z".

Returns:

Name Type Description
vtkUnstructuredGrid

VTK unstructured grid

Source code in voromesh/voronoi/__init__.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
def to_vtk(self, decimalplace=8, dim="Z"):
    """
    Converts the Voronoi diagram to a vtk unstructured grid.

    Parameters
    ----------
    decimalplace : int, optional
        _description_. Defaults to 8.
    dim : str, optional
        _description_. Defaults to "Z".

    Returns:
        vtkUnstructuredGrid: VTK unstructured grid
    """

    return to_vtk(self.voronoi, decimalplace, dim)