File: polygon.py

package info (click to toggle)
python-vispy 0.14.3-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 8,840 kB
  • sloc: python: 59,436; javascript: 6,800; makefile: 69; sh: 6
file content (137 lines) | stat: -rw-r--r-- 4,201 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) Vispy Development Team. All Rights Reserved.
# Distributed under the (new) BSD License. See LICENSE.txt for more info.
# -----------------------------------------------------------------------------

import numpy as np

from .triangulation import Triangulation


class PolygonData(object):
    """Polygon class for data handling

    Parameters
    ----------
    vertices : (Nv, 3) array
        Vertex coordinates. If faces is not specified, then this will instead
        be interpreted as (Nf, 3, 3) array of coordinates.
    edges : (Nv, 2) array
        Constraining edges specified by vertex indices.
    faces : (Nf, 3) array
        Indexes into the vertex array.

    Notes
    -----
    All arguments are optional.
    """

    def __init__(self, vertices=None, edges=None, faces=None):
        self._vertices = vertices
        self._edges = edges
        self._faces = faces
        self._convex_hull = None

    @property
    def faces(self):
        """Return an array (Nf, 3) of vertex indexes, three per triangular
        face in the mesh.

        If faces have not been computed for this mesh, the function
        computes them.
        If no vertices or faces are specified, the function returns None.
        """
        if self._faces is None:
            if self._vertices is None:
                return None
            self.triangulate()
        return self._faces

    @faces.setter
    def faces(self, f):
        """If vertices and faces are incompatible, this will generate vertices
        from these faces and set them.
        """
        self._faces = f

    @property
    def vertices(self):
        """Return an array (Nf, 3) of vertices.

        If only faces exist, the function computes the vertices and
        returns them.
        If no vertices or faces are specified, the function returns None.
        """
        if self._faces is None:
            if self._vertices is None:
                return None
            self.triangulate()
        return self._vertices

    @vertices.setter
    def vertices(self, v):
        """If vertices and faces are incompatible, this will generate faces
        from these vertices and set them.
        """
        self._vertices = v

    @property
    def edges(self):
        """Return an array (Nv, 2) of vertex indices.

        If no vertices or faces are specified, the function returns None.
        """
        return self._edges

    @edges.setter
    def edges(self, e):
        """Ensures that all edges are valid."""
        self._edges = e

    @property
    def convex_hull(self):
        """Return an array of vertex indexes representing the convex hull.

        If faces have not been computed for this mesh, the function
        computes them.
        If no vertices or faces are specified, the function returns None.
        """
        if self._faces is None:
            if self._vertices is None:
                return None
            self.triangulate()
        return self._convex_hull

    def triangulate(self):
        """
        Triangulates the set of vertices and stores the triangles in faces and
        the convex hull in convex_hull.
        """
        npts = self._vertices.shape[0]
        if np.any(self._vertices[0] != self._vertices[1]):
            # start != end, so edges must wrap around to beginning.
            edges = np.empty((npts, 2), dtype=np.uint32)
            edges[:, 0] = np.arange(npts)
            edges[:, 1] = edges[:, 0] + 1
            edges[-1, 1] = 0
        else:
            # start == end; no wrapping required.
            edges = np.empty((npts-1, 2), dtype=np.uint32)
            edges[:, 0] = np.arange(npts)
            edges[:, 1] = edges[:, 0] + 1

        tri = Triangulation(self._vertices, edges)
        tri.triangulate()
        return tri.pts, tri.tris

    def add_vertex(self, vertex):
        """
        Adds given vertex and retriangulates to generate new faces.

        Parameters
        ----------
        vertex : array-like
            The vertex to add.
        """
        raise NotImplementedError