-
Notifications
You must be signed in to change notification settings - Fork 3
/
triangulate.py
141 lines (112 loc) · 4.25 KB
/
triangulate.py
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
138
139
140
141
#!/usr/bin/env python
#-*- coding:utf-8 -*-
#
# This file is part of the PyNCulture project, which aims at providing tools to
# easily generate complex neuronal cultures.
# Copyright (C) 2017 SENeC Initiative
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
""" Triangulation and fast random point generation methods """
from OpenGL.GLU import *
from OpenGL.GL import *
from shapely.geometry import Polygon, MultiPolygon
import numpy as np
def triangulate(polygon):
"""
Returns a triangulation of `polygon`.
Parameters
----------
polygon : a :class:`Shape` object or a :class:`~shapely.geometry.Polygon`
or a :class:`~shapely.geometry.MultiPolygon` which will be decomposed
into triangles.
Returns
-------
triangles : generator containing triplets of triangle vertices.
"""
vertices = []
# opengl callbacks
def _edgeFlagCallback(param1, param2): pass
def _beginCallback(param=None):
vertices = []
def _vertexCallback(vertex, otherData=None):
vertices.append(vertex[:2])
def _combineCallback(vertex, neighbors, neighborWeights, out=None):
out = vertex
return out
def _endCallback(data=None): pass
# init tesselation
tess = gluNewTess()
gluTessProperty(tess, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_ODD)
# force triangulation of polygons (i.e. GL_TRIANGLES) rather than
# returning triangle fans or strips
gluTessCallback(tess, GLU_TESS_EDGE_FLAG_DATA, _edgeFlagCallback)
gluTessCallback(tess, GLU_TESS_BEGIN, _beginCallback)
gluTessCallback(tess, GLU_TESS_VERTEX, _vertexCallback)
gluTessCallback(tess, GLU_TESS_COMBINE, _combineCallback)
gluTessCallback(tess, GLU_TESS_END, _endCallback)
gluTessBeginPolygon(tess, 0)
# first handle the main polygon(s)
if isinstance(polygon, Polygon):
_tesselate(tess, polygon)
elif isinstance(polygon, MultiPolygon):
for p in polygon.geoms:
_tesselate(tess, p)
# finish polygon and remove tesselator
gluTessEndPolygon(tess)
gluDeleteTess(tess)
return ((vertices[i], vertices[i+1], vertices[i+2])
for i in range(0, len(vertices), 3))
def rnd_pts_in_tr(triangles, num_points):
'''
Generate random points in a set of triangles.
Parameters
----------
triangles : list of :class:`shapely.geometry.Polygon` triangles
num_points : number of points to generate.
Returns
-------
points : np.array of shape (`num_points`, 2)
'''
# normalized areas
areas = [t.area for t in triangles]
areas = np.array(areas) / np.sum(areas)
# vertices and triangle ids
verts = np.array([t.exterior.coords for t in triangles])
idx = np.arange(0, len(triangles), dtype=int)
# choose triangle based on its area
chosen_idx = np.random.choice(idx, size=num_points, p=areas)
chosen = verts[chosen_idx]
# generate random points inside these triangles
r1, r2 = np.random.rand(2, num_points)
As = chosen[:, 0, :]
Bs = chosen[:, 1, :]
Cs = chosen[:, 2, :]
points = (As.T*(1 - np.sqrt(r1))).T \
+ (Bs.T*(np.sqrt(r1)*(1 - r2))).T \
+ (Cs.T*(np.sqrt(r1)*r2)).T
return points
# polygon tesselation
def _tesselate(tess, polygon):
gluTessBeginContour(tess)
for point in polygon.exterior.coords:
point3d = (point[0], point[1], 0)
gluTessVertex(tess, point3d, point3d)
gluTessEndContour(tess)
# then handle each of the holes, if applicable
for hole in polygon.interiors:
gluTessBeginContour(tess)
for point in hole.coords:
point3d = (point[0], point[1], 0)
gluTessVertex(tess, point3d, point3d)
gluTessEndContour(tess)