-
Notifications
You must be signed in to change notification settings - Fork 3
/
svgtools.py
executable file
·265 lines (205 loc) · 7.82 KB
/
svgtools.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/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/>.
from xml.dom.minidom import parse
from svg.path import (parse_path, CubicBezier, QuadraticBezier, Arc,
Move, Close, Path)
from shapely.affinity import scale, affine_transform, translate
from shapely.geometry import Point, Polygon
import numpy as np
'''
Shape generation from SVG files.
'''
__all__ = ["polygons_from_svg"]
# predefined svg shapes and their parameters
_predefined = {
'path': None,
'ellipse': ("cx", "cy", "rx", "ry"),
'circle': ("cx", "cy", "r"),
'rect': ("x", "y", "width", "height")
}
_valid_nodes = _predefined.keys()
def polygons_from_svg(filename, interpolate_curve=50, parent=None,
return_points=False):
'''
Generate :class:`shapely.geometry.Polygon` objects from an SVG file.
'''
svg = parse(filename)
elt_structs = {k: [] for k in _valid_nodes}
elt_points = {k: [] for k in _valid_nodes}
# get the properties of all predefined elements
for elt_type, elt_prop in _predefined.items():
_build_struct(svg, elt_structs[elt_type], elt_type, elt_prop)
# build all shapes
polygons = []
for elt_type, instructions in elt_structs.items():
for struct in instructions:
polygon, points = _make_polygon(
elt_type, struct, parent=parent, return_points=True)
polygons.append(polygon)
elt_points[elt_type].append(points)
if return_points:
return polygons, elt_points
return polygons
# ----- #
# Tools #
# ----- #
def _get_closed_subpaths(path):
'''
Generates all closed subpaths raises error if open subpaths exist.
Credit to @tatarize:
https://github.com/regebro/svg.path/issues/54#issuecomment-570101018
'''
segments = None
for p in path:
if isinstance(p, Move):
if segments is not None:
raise RuntimeError("Only closed shapes accepted.")
segments = []
segments.append(p)
if isinstance(p, Close):
yield Path(*segments)
segments = None
def _get_points(path, interpolate_curve=50):
''' Get points from path. '''
points = []
for item in path:
if isinstance(item, (Arc, CubicBezier, QuadraticBezier)):
istart = 1. / interpolate_curve
for frac in np.linspace(istart, 1, interpolate_curve):
points.append(
(item.point(frac).real, item.point(frac).imag))
else:
points.append((item.start.real, item.start.imag))
return points
def _get_outer_shell(paths_points):
''' Returns the index of the container subpath '''
minx, maxx, miny, maxy = np.inf, -np.inf, np.inf, -np.inf
container_idx = None
for i, pp in enumerate(paths_points):
arr = np.array(pp)
x_min = np.min(arr[:, 0])
x_max = np.max(arr[:, 0])
y_min = np.min(arr[:, 1])
y_max = np.max(arr[:, 1])
winner = True
if x_min <= minx:
minx = x_min
else:
winner = False
if x_max >= maxx:
maxx = x_max
else:
winner = False
if y_min <= miny:
miny = y_min
else:
winner = False
if y_max >= maxy:
maxy = y_max
else:
winner = False
if winner:
container_idx = i
return container_idx
def _build_struct(svg, container, elt_type, elt_properties):
root = svg.documentElement
for elt in root.getElementsByTagName(elt_type):
struct = {
"transf": [],
"transfdata": []
}
parent = elt.parentNode
while parent is not None:
_get_transform(parent, struct)
parent = parent.parentNode
_get_transform(elt, struct)
if elt_type == 'path':
path, _ = elt.getAttribute('d'), None
struct["path"] = path
else:
for item in elt_properties:
struct[item] = float(elt.getAttribute(item))
container.append(struct)
def _make_polygon(elt_type, instructions, parent=None, interpolate_curve=50,
return_points=False):
container = None
shell = [] # outer points defining the polygon's outer shell
holes = [] # inner points defining holes
if elt_type == "path": # build polygons from custom paths
path_data = parse_path(instructions["path"])
subpaths = [
subpath for subpath in _get_closed_subpaths(path_data)
]
points = [_get_points(subpath) for subpath in subpaths]
# get the container
idx_container = _get_outer_shell(points)
shell = np.array(points[idx_container])
# get the holes and make the shape
holes = [pp for i, pp in enumerate(points) if i != idx_container]
container = Polygon(shell, holes=holes)
elif elt_type == "ellipse": # build ellipses
circle = Point((instructions["cx"], instructions["cy"])).buffer(1)
rx, ry = instructions["rx"], instructions["ry"]
container = scale(circle, rx, ry)
elif elt_type == "circle": # build circles
r = instructions["r"]
container = Point((instructions["cx"], instructions["cy"])).buffer(r)
elif elt_type == "rect": # build rectangles
x, y = instructions["x"], instructions["y"]
w, h = instructions["width"], instructions["height"]
shell = np.array([(x, y), (x + w, y), (x + w, y + h), (x, y + h)])
container = Polygon(shell)
else:
raise RuntimeError("Unexpected element type: '{}'.".format(elt_type))
# transforms
nn, dd = instructions["transf"][::-1], instructions["transfdata"][::-1]
for name, data in zip(nn, dd):
if name == "matrix":
container = affine_transform(container, data)
elif name == "translate":
container = translate(container, *data)
# y axis is inverted in SVG, so make mirror transform
container = affine_transform(container, (1, 0, 0, -1, 0, 0))
shell = np.array(container.exterior.coords)
if return_points:
return container, shell
return container
def _get_transform(obj, tdict):
''' Get the transformation properties and name into `tdict` '''
try:
if obj.hasAttribute("transform"):
trans = obj.getAttribute('transform')
if trans.startswith("translate"):
start = trans.find("(") + 1
stop = trans.find(")")
tdict["transf"].append("translate")
tdict["transfdata"].append(
[float(f) for f in trans[start:stop].split(",")])
elif trans.startswith("matrix"):
start = trans.find("(") + 1
stop = trans.find(")")
trans = [float(f)
for f in trans[start:stop].split(",")]
tdict["transf"].append("matrix")
tdict["transfdata"].append(trans)
else:
raise RuntimeError("Uknown transform: " + trans)
except Exception:
pass