sketchkit 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of sketchkit might be problematic. Click here for more details.
- sketchkit/__init__.py +9 -0
- sketchkit/core/__init__.py +3 -0
- sketchkit/core/sketch.py +661 -0
- sketchkit/core/sketch3d.py +0 -0
- sketchkit/datasets/__init__.py +23 -0
- sketchkit/datasets/controlsketch.py +403 -0
- sketchkit/datasets/creative_sketch.py +370 -0
- sketchkit/datasets/differsketching.py +261 -0
- sketchkit/datasets/fscoco.py +265 -0
- sketchkit/datasets/gmu_sketch_cleanup.py +317 -0
- sketchkit/datasets/hzy_sketch.py +238 -0
- sketchkit/datasets/opensketch.py +323 -0
- sketchkit/datasets/photosketching.py +416 -0
- sketchkit/datasets/quickdraw.py +356 -0
- sketchkit/datasets/sketchx_pris.py +299 -0
- sketchkit/datasets/sketchy.py +386 -0
- sketchkit/datasets/tracing_vs_freehand.py +311 -0
- sketchkit/datasets/tu_berlin.py +381 -0
- sketchkit/models/__init__.py +0 -0
- sketchkit/renderer/cairo_renderer.py +315 -0
- sketchkit/utils/__init__.py +0 -0
- sketchkit/utils/dataset.py +194 -0
- sketchkit/utils/file.py +147 -0
- sketchkit/utils/geometry.py +150 -0
- sketchkit/utils/transform.py +32 -0
- sketchkit-0.0.1.dist-info/METADATA +121 -0
- sketchkit-0.0.1.dist-info/RECORD +29 -0
- sketchkit-0.0.1.dist-info/WHEEL +5 -0
- sketchkit-0.0.1.dist-info/top_level.txt +1 -0
sketchkit/__init__.py
ADDED
sketchkit/core/sketch.py
ADDED
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
from sketchkit.utils.geometry import bezier_lengths, is_line
|
|
3
|
+
import svgpathtools
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Point:
|
|
7
|
+
"""2D point.
|
|
8
|
+
|
|
9
|
+
Represents a 2D point with x and y coordinates stored as 32-bit floats.
|
|
10
|
+
|
|
11
|
+
Attributes:
|
|
12
|
+
x (np.float32): X coordinate.
|
|
13
|
+
y (np.float32): Y coordinate.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
x: float | np.float32,
|
|
19
|
+
y: float | np.float32,
|
|
20
|
+
):
|
|
21
|
+
self.x = np.float32(x)
|
|
22
|
+
self.y = np.float32(y)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Vertex(Point):
|
|
26
|
+
"""Vertex point with drawing attributes.
|
|
27
|
+
|
|
28
|
+
Extends Point with curve-specific properties: pressure, thickness, color,
|
|
29
|
+
and opacity for rendering.
|
|
30
|
+
|
|
31
|
+
Attributes:
|
|
32
|
+
pressure (np.float32 | None): Normalized pressure value.
|
|
33
|
+
thickness (np.float32 | None): Stroke thickness value.
|
|
34
|
+
color (np.ndarray | None): RGB color array with float32 dtype.
|
|
35
|
+
opacity (np.float32 | None): Opacity value (0.0-1.0).
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
x: float | np.float32,
|
|
41
|
+
y: float | np.float32,
|
|
42
|
+
pressure: None | float | np.float32 = None,
|
|
43
|
+
thickness: None | float | np.float32 = None,
|
|
44
|
+
color: None | tuple[float, float, float] | np.ndarray = None,
|
|
45
|
+
opacity: None | float | np.float32 = None,
|
|
46
|
+
):
|
|
47
|
+
"""Initialize a Vertex.
|
|
48
|
+
|
|
49
|
+
Args:
|
|
50
|
+
x (float | np.float32): X coordinate.
|
|
51
|
+
y (float | np.float32): Y coordinate.
|
|
52
|
+
pressure (float | np.float32 | None, optional): Pressure at this
|
|
53
|
+
vertex. Defaults to None.
|
|
54
|
+
thickness (float | np.float32 | None, optional): Stroke thickness at
|
|
55
|
+
this vertex. Defaults to None.
|
|
56
|
+
color (tuple[float, float, float] | np.ndarray | None, optional):
|
|
57
|
+
RGB color as tuple or numpy array. Defaults to None.
|
|
58
|
+
opacity (float | np.float32 | None, optional): Opacity (0.0-1.0).
|
|
59
|
+
Defaults to None.
|
|
60
|
+
"""
|
|
61
|
+
super().__init__(x, y)
|
|
62
|
+
self.pressure = np.float32(pressure) if pressure is not None else None
|
|
63
|
+
self.thickness = np.float32(thickness) if thickness is not None else None
|
|
64
|
+
self.color = np.array(color, dtype=np.float32) if color is not None else None
|
|
65
|
+
self.opacity = np.float32(opacity) if opacity is not None else None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class Curve:
|
|
69
|
+
"""Curve defined by two vertices and two control points.
|
|
70
|
+
|
|
71
|
+
Connects two vertices (start and end) with optional control points for a
|
|
72
|
+
curved path. Visual properties (color, pressure, thickness, opacity) are
|
|
73
|
+
applied to both vertices together.
|
|
74
|
+
|
|
75
|
+
Attributes:
|
|
76
|
+
p_start (Vertex): Starting vertex.
|
|
77
|
+
p_end (Vertex): Ending vertex.
|
|
78
|
+
p_crtl1 (Point): First control point for curve definition.
|
|
79
|
+
p_crtl2 (Point): Second control point for curve definition.
|
|
80
|
+
|
|
81
|
+
Notes:
|
|
82
|
+
When setting properties, the same value is applied to both start and end
|
|
83
|
+
vertices. Numeric properties are converted to np.float32 automatically.
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
def __init__(
|
|
87
|
+
self,
|
|
88
|
+
p_start: Vertex,
|
|
89
|
+
p_end: Vertex,
|
|
90
|
+
p_crtl1: Point,
|
|
91
|
+
p_crtl2: Point,
|
|
92
|
+
):
|
|
93
|
+
"""Initialize a Curve.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
p_start (Vertex): Starting vertex.
|
|
97
|
+
p_end (Vertex): Ending vertex.
|
|
98
|
+
p_crtl1 (Point): First control point.
|
|
99
|
+
p_crtl2 (Point): Second control point.
|
|
100
|
+
"""
|
|
101
|
+
self.p_start = p_start
|
|
102
|
+
self.p_end = p_end
|
|
103
|
+
self.p_crtl1 = p_crtl1
|
|
104
|
+
self.p_crtl2 = p_crtl2
|
|
105
|
+
|
|
106
|
+
def _get_vertices_attribute(self, attribute: str):
|
|
107
|
+
return [
|
|
108
|
+
getattr(self.p_start, attribute),
|
|
109
|
+
getattr(self.p_end, attribute),
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
def _set_vertices_attribute(self, attribute: str, value):
|
|
113
|
+
setattr(self.p_start, attribute, value)
|
|
114
|
+
setattr(self.p_end, attribute, value)
|
|
115
|
+
|
|
116
|
+
@property
|
|
117
|
+
def color(self):
|
|
118
|
+
"""Colors of the start and end vertices.
|
|
119
|
+
|
|
120
|
+
Returns:
|
|
121
|
+
list[np.ndarray | None]: A two-element list of RGB colors for the
|
|
122
|
+
start and end vertices (each as float32 arrays) or None.
|
|
123
|
+
"""
|
|
124
|
+
return self._get_vertices_attribute("color")
|
|
125
|
+
|
|
126
|
+
@color.setter
|
|
127
|
+
def color(self, value: None | tuple[float, float, float] | np.ndarray):
|
|
128
|
+
"""Set color for both vertices.
|
|
129
|
+
|
|
130
|
+
Args:
|
|
131
|
+
value (tuple[float, float, float] | np.ndarray | None): RGB color as
|
|
132
|
+
tuple/ndarray, or None to clear.
|
|
133
|
+
"""
|
|
134
|
+
color_value = np.array(value, dtype=np.float32) if value is not None else None
|
|
135
|
+
self._set_vertices_attribute("color", color_value)
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def pressure(self):
|
|
139
|
+
"""Pressures of the start and end vertices.
|
|
140
|
+
|
|
141
|
+
Returns:
|
|
142
|
+
list[np.float32 | None]: Two-element list of pressures or None.
|
|
143
|
+
"""
|
|
144
|
+
return self._get_vertices_attribute("pressure")
|
|
145
|
+
|
|
146
|
+
@pressure.setter
|
|
147
|
+
def pressure(self, value: None | float | np.float32):
|
|
148
|
+
"""Set pressure for both vertices.
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
value (float | np.float32 | None): Pressure value or None to clear.
|
|
152
|
+
"""
|
|
153
|
+
pressure_value = np.float32(value) if value is not None else None
|
|
154
|
+
self._set_vertices_attribute("pressure", pressure_value)
|
|
155
|
+
|
|
156
|
+
@property
|
|
157
|
+
def thickness(self):
|
|
158
|
+
"""Thicknesses of the start and end vertices.
|
|
159
|
+
|
|
160
|
+
Returns:
|
|
161
|
+
list[np.float32 | None]: Two-element list of thicknesses or None.
|
|
162
|
+
"""
|
|
163
|
+
return self._get_vertices_attribute("thickness")
|
|
164
|
+
|
|
165
|
+
@thickness.setter
|
|
166
|
+
def thickness(self, value: None | float | np.float32):
|
|
167
|
+
"""Set thickness for both vertices.
|
|
168
|
+
|
|
169
|
+
Args:
|
|
170
|
+
value (float | np.float32 | None): Thickness value or None to clear.
|
|
171
|
+
"""
|
|
172
|
+
thickness_value = np.float32(value) if value is not None else None
|
|
173
|
+
self._set_vertices_attribute("thickness", thickness_value)
|
|
174
|
+
|
|
175
|
+
@property
|
|
176
|
+
def opacity(self):
|
|
177
|
+
"""Opacities of the start and end vertices.
|
|
178
|
+
|
|
179
|
+
Returns:
|
|
180
|
+
list[np.float32 | None]: Two-element list of opacities or None.
|
|
181
|
+
"""
|
|
182
|
+
return self._get_vertices_attribute("opacity")
|
|
183
|
+
|
|
184
|
+
@opacity.setter
|
|
185
|
+
def opacity(self, value: None | float | np.float32):
|
|
186
|
+
"""Set opacity for both vertices.
|
|
187
|
+
|
|
188
|
+
Args:
|
|
189
|
+
value (float | np.float32 | None): Opacity (0.0–1.0) or None to clear.
|
|
190
|
+
"""
|
|
191
|
+
opacity_value = np.float32(value) if value is not None else None
|
|
192
|
+
self._set_vertices_attribute("opacity", opacity_value)
|
|
193
|
+
|
|
194
|
+
def is_line(self) -> bool:
|
|
195
|
+
"""Check if the curve is a line.
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
bool: True if the curve is a line, False otherwise.
|
|
199
|
+
"""
|
|
200
|
+
return is_line(self.np_points.reshape(1, 4, 2))[0]
|
|
201
|
+
|
|
202
|
+
@property
|
|
203
|
+
def np_points(self) -> np.ndarray:
|
|
204
|
+
"""Get the points of the curve.
|
|
205
|
+
|
|
206
|
+
Returns:
|
|
207
|
+
np.ndarray: Array of points (shape: [4, 2]).
|
|
208
|
+
"""
|
|
209
|
+
return np.array([
|
|
210
|
+
[self.p_start.x, self.p_start.y],
|
|
211
|
+
[self.p_crtl1.x, self.p_crtl1.y],
|
|
212
|
+
[self.p_crtl2.x, self.p_crtl2.y],
|
|
213
|
+
[self.p_end.x, self.p_end.y],
|
|
214
|
+
])
|
|
215
|
+
|
|
216
|
+
def to_svg_path(self) -> str:
|
|
217
|
+
"""Convert the curve to an SVG path string.
|
|
218
|
+
|
|
219
|
+
Returns:
|
|
220
|
+
str: SVG path representation of the curve.
|
|
221
|
+
"""
|
|
222
|
+
p0, p1, p2, p3 = self.p_start, self.p_crtl1, self.p_crtl2, self.p_end
|
|
223
|
+
return f'M {p0.x} {p0.y} C {p1.x} {p1.y}, {p2.x} {p2.y}, {p3.x} {p3.y}'
|
|
224
|
+
|
|
225
|
+
@property
|
|
226
|
+
def length(self) -> float:
|
|
227
|
+
"""Calculate the length of the curve.
|
|
228
|
+
|
|
229
|
+
Returns:
|
|
230
|
+
float: Length of the curve.
|
|
231
|
+
"""
|
|
232
|
+
|
|
233
|
+
# 将p1,p2,p3,p4整合成[1,4,2]
|
|
234
|
+
points = self.np_points.reshape(1, 4, 2)
|
|
235
|
+
return bezier_lengths(points)[0]
|
|
236
|
+
|
|
237
|
+
def svg_length(self) -> float:
|
|
238
|
+
"""Calculate the length of the curve in SVG path units.
|
|
239
|
+
|
|
240
|
+
Returns:
|
|
241
|
+
float: Length of the curve in SVG path units.
|
|
242
|
+
"""
|
|
243
|
+
path = svgpathtools.parse_path(self.to_svg_path())
|
|
244
|
+
return path.length()
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def get_division_points(self, ts: np.ndarray) -> np.ndarray:
|
|
248
|
+
"""Get points on the curve at specified parameter values.
|
|
249
|
+
|
|
250
|
+
Args:
|
|
251
|
+
ts (np.ndarray): Array of parameter values between 0 and 1.
|
|
252
|
+
|
|
253
|
+
Returns:
|
|
254
|
+
np.ndarray: Array of points on the curve at the specified parameter values.
|
|
255
|
+
"""
|
|
256
|
+
svg_path = svgpathtools.parse_path(self.to_svg_path())
|
|
257
|
+
return np.array([[svg_path.point(t).real, svg_path.point(t).imag] for t in ts], dtype=np.float32)
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
class Path:
|
|
261
|
+
"""Collection of curves.
|
|
262
|
+
|
|
263
|
+
Manages multiple curves and applies common operations to all of them.
|
|
264
|
+
|
|
265
|
+
Attributes:
|
|
266
|
+
curves (list[Curve]): Curves in this path.
|
|
267
|
+
|
|
268
|
+
Examples:
|
|
269
|
+
>>> path = Path([curve1, curve2, curve3])
|
|
270
|
+
>>> path.color = (1.0, 0.0, 0.0) # Set all curves to red
|
|
271
|
+
>>> path.thickness = 2.5 # Set all curves to thickness 2.5
|
|
272
|
+
>>> path.curve_num
|
|
273
|
+
3
|
|
274
|
+
"""
|
|
275
|
+
|
|
276
|
+
def __init__(self, curves: list[Curve] | None = None):
|
|
277
|
+
"""Initialize a Path.
|
|
278
|
+
|
|
279
|
+
Args:
|
|
280
|
+
curves (list[Curve] | None, optional): Initial curves. Defaults to None.
|
|
281
|
+
"""
|
|
282
|
+
self.curves = curves if curves else []
|
|
283
|
+
|
|
284
|
+
@property
|
|
285
|
+
def curve_num(self):
|
|
286
|
+
"""Number of curves in the path.
|
|
287
|
+
|
|
288
|
+
Returns:
|
|
289
|
+
int: Count of curves.
|
|
290
|
+
"""
|
|
291
|
+
return len(self.curves)
|
|
292
|
+
|
|
293
|
+
def _get_curves_attribute(self, attribute: str):
|
|
294
|
+
return [getattr(curve, attribute) for curve in self.curves]
|
|
295
|
+
|
|
296
|
+
def _set_curves_attribute(self, attribute: str, value):
|
|
297
|
+
for curve in self.curves:
|
|
298
|
+
setattr(curve, attribute, value)
|
|
299
|
+
|
|
300
|
+
@property
|
|
301
|
+
def color(self):
|
|
302
|
+
"""Colors for each curve.
|
|
303
|
+
|
|
304
|
+
Returns:
|
|
305
|
+
list[list[np.ndarray | None]]: For each curve, a two-element list of
|
|
306
|
+
vertex colors (RGB arrays) or None.
|
|
307
|
+
"""
|
|
308
|
+
return self._get_curves_attribute("color")
|
|
309
|
+
|
|
310
|
+
@color.setter
|
|
311
|
+
def color(self, value: None | tuple[float, float, float] | np.ndarray):
|
|
312
|
+
"""Set color for all curves.
|
|
313
|
+
|
|
314
|
+
Args:
|
|
315
|
+
value (tuple[float, float, float] | np.ndarray | None): RGB color as
|
|
316
|
+
tuple/ndarray, or None to clear.
|
|
317
|
+
"""
|
|
318
|
+
color_value = np.array(value, dtype=np.float32) if value is not None else None
|
|
319
|
+
self._set_curves_attribute("color", color_value)
|
|
320
|
+
|
|
321
|
+
@property
|
|
322
|
+
def pressure(self):
|
|
323
|
+
"""Pressures for each curve.
|
|
324
|
+
|
|
325
|
+
Returns:
|
|
326
|
+
list[list[np.float32 | None]]: For each curve, a two-element list of
|
|
327
|
+
vertex pressures or None.
|
|
328
|
+
"""
|
|
329
|
+
return self._get_curves_attribute("pressure")
|
|
330
|
+
|
|
331
|
+
@pressure.setter
|
|
332
|
+
def pressure(self, value: None | float | np.float32):
|
|
333
|
+
"""Set pressure for all curves.
|
|
334
|
+
|
|
335
|
+
Args:
|
|
336
|
+
value (float | np.float32 | None): Pressure value or None to clear.
|
|
337
|
+
"""
|
|
338
|
+
pressure_value = np.float32(value) if value is not None else None
|
|
339
|
+
self._set_curves_attribute("pressure", pressure_value)
|
|
340
|
+
|
|
341
|
+
@property
|
|
342
|
+
def thickness(self):
|
|
343
|
+
"""Thicknesses for each curve.
|
|
344
|
+
|
|
345
|
+
Returns:
|
|
346
|
+
list[list[np.float32 | None]]: For each curve, a two-element list of
|
|
347
|
+
vertex thicknesses or None.
|
|
348
|
+
"""
|
|
349
|
+
return self._get_curves_attribute("thickness")
|
|
350
|
+
|
|
351
|
+
@thickness.setter
|
|
352
|
+
def thickness(self, value: None | float | np.float32):
|
|
353
|
+
"""Set thickness for all curves.
|
|
354
|
+
|
|
355
|
+
Args:
|
|
356
|
+
value (float | np.float32 | None): Thickness value or None to clear.
|
|
357
|
+
"""
|
|
358
|
+
thickness_value = np.float32(value) if value is not None else None
|
|
359
|
+
self._set_curves_attribute("thickness", thickness_value)
|
|
360
|
+
|
|
361
|
+
@property
|
|
362
|
+
def opacity(self):
|
|
363
|
+
"""Opacities for each curve.
|
|
364
|
+
|
|
365
|
+
Returns:
|
|
366
|
+
list[list[np.float32 | None]]: For each curve, a two-element list of
|
|
367
|
+
vertex opacities or None.
|
|
368
|
+
"""
|
|
369
|
+
return self._get_curves_attribute("opacity")
|
|
370
|
+
|
|
371
|
+
@opacity.setter
|
|
372
|
+
def opacity(self, value: None | float | np.float32):
|
|
373
|
+
"""Set opacity for all curves.
|
|
374
|
+
|
|
375
|
+
Args:
|
|
376
|
+
value (float | np.float32 | None): Opacity (0.0–1.0) or None to clear.
|
|
377
|
+
"""
|
|
378
|
+
opacity_value = np.float32(value) if value is not None else None
|
|
379
|
+
self._set_curves_attribute("opacity", opacity_value)
|
|
380
|
+
|
|
381
|
+
def curve_lengths(self):
|
|
382
|
+
"""Calculate the length of each curve in the path.
|
|
383
|
+
|
|
384
|
+
Returns:
|
|
385
|
+
float: Total length of the path.
|
|
386
|
+
"""
|
|
387
|
+
points = np.array([curve.np_points for curve in self.curves])
|
|
388
|
+
lengths = bezier_lengths(points)
|
|
389
|
+
return lengths
|
|
390
|
+
|
|
391
|
+
@property
|
|
392
|
+
def length(self):
|
|
393
|
+
"""Calculate the total length of all curves in the path.
|
|
394
|
+
|
|
395
|
+
Returns:
|
|
396
|
+
float: Total length of the path.
|
|
397
|
+
"""
|
|
398
|
+
return sum(self.curve_lengths())[0]
|
|
399
|
+
|
|
400
|
+
def svg_curve_lengths(self) -> list[float]:
|
|
401
|
+
"""Calculate the lengths of each curve in SVG path units.
|
|
402
|
+
|
|
403
|
+
Returns:
|
|
404
|
+
np.ndarray: Array of lengths for each curve in SVG path units.
|
|
405
|
+
"""
|
|
406
|
+
return np.array([curve.svg_length() for curve in self.curves])
|
|
407
|
+
|
|
408
|
+
def svg_length(self) -> float:
|
|
409
|
+
"""Calculate the total length of all curves in the path in SVG path units.
|
|
410
|
+
|
|
411
|
+
Returns:
|
|
412
|
+
float: Total length of the path in SVG path units.
|
|
413
|
+
"""
|
|
414
|
+
return sum(self.svg_curve_lengths())
|
|
415
|
+
|
|
416
|
+
def get_division_points(self, ts: np.ndarray) -> np.ndarray:
|
|
417
|
+
"""Get points on the path at parameters ts.
|
|
418
|
+
|
|
419
|
+
Args:
|
|
420
|
+
ts (np.ndarray): Array of parameter values between 0 and 1.
|
|
421
|
+
|
|
422
|
+
Returns:
|
|
423
|
+
np.ndarray: Array of points on the path at parameters ts.
|
|
424
|
+
"""
|
|
425
|
+
assert np.all((0.0 <= ts) & (ts <= 1.0)), "All t values must be in [0, 1]"
|
|
426
|
+
lengths = self.svg_curve_lengths()
|
|
427
|
+
pre_sum_lengths = np.cumsum(lengths)
|
|
428
|
+
total_length = pre_sum_lengths[-1]
|
|
429
|
+
target_lengths = ts * total_length
|
|
430
|
+
curve_indices = np.searchsorted(pre_sum_lengths, target_lengths, side='right')
|
|
431
|
+
points = np.zeros((len(ts), 2), dtype=np.float32)
|
|
432
|
+
for i, curve_index in enumerate(curve_indices):
|
|
433
|
+
if curve_index == 0:
|
|
434
|
+
t_on_curve = target_lengths[i] / lengths[0]
|
|
435
|
+
points[i] = self.curves[0].point(t_on_curve)
|
|
436
|
+
elif curve_index < len(self.curves):
|
|
437
|
+
t_on_curve = (target_lengths[i] - pre_sum_lengths[curve_index - 1]) / lengths[curve_index]
|
|
438
|
+
points[i] = self.curves[curve_index].point(t_on_curve)
|
|
439
|
+
else:
|
|
440
|
+
points[i] = self.curves[-1].point(t_on_curve)
|
|
441
|
+
return points
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
class Sketch:
|
|
446
|
+
"""Sketch composed of multiple paths.
|
|
447
|
+
|
|
448
|
+
A Sketch collects Path objects to form a complete drawing and exposes
|
|
449
|
+
convenient accessors for color, pressure, thickness, and opacity.
|
|
450
|
+
|
|
451
|
+
Attributes:
|
|
452
|
+
paths (list[Path]): Paths in the sketch.
|
|
453
|
+
|
|
454
|
+
Examples:
|
|
455
|
+
>>> sketch = Sketch([path1, path2, path3])
|
|
456
|
+
>>> sketch.color = (1.0, 0.0, 0.0) # Set all paths to red
|
|
457
|
+
>>> sketch.thickness = 2.5 # Set all paths to thickness 2.5
|
|
458
|
+
"""
|
|
459
|
+
|
|
460
|
+
def __init__(self, paths: list[Path] | None = None):
|
|
461
|
+
"""Initialize a Sketch.
|
|
462
|
+
|
|
463
|
+
Args:
|
|
464
|
+
paths (list[Path] | None, optional): Initial paths. Defaults to None.
|
|
465
|
+
"""
|
|
466
|
+
self.paths = paths if paths else []
|
|
467
|
+
|
|
468
|
+
@property
|
|
469
|
+
def path_num(self):
|
|
470
|
+
"""Number of paths in the sketch.
|
|
471
|
+
|
|
472
|
+
Returns:
|
|
473
|
+
int: Count of paths.
|
|
474
|
+
"""
|
|
475
|
+
return len(self.paths)
|
|
476
|
+
|
|
477
|
+
@property
|
|
478
|
+
def curve_num(self):
|
|
479
|
+
"""Total number of curves across all paths.
|
|
480
|
+
|
|
481
|
+
Returns:
|
|
482
|
+
int: Sum of curves in all paths.
|
|
483
|
+
"""
|
|
484
|
+
return sum(path.curve_num for path in self.paths)
|
|
485
|
+
|
|
486
|
+
def _get_paths_attribute(self, attribute: str):
|
|
487
|
+
return [getattr(path, attribute) for path in self.paths]
|
|
488
|
+
|
|
489
|
+
def _set_paths_attribute(self, attribute: str, value):
|
|
490
|
+
for path in self.paths:
|
|
491
|
+
setattr(path, attribute, value)
|
|
492
|
+
|
|
493
|
+
@property
|
|
494
|
+
def color(self):
|
|
495
|
+
"""Colors per path.
|
|
496
|
+
|
|
497
|
+
Returns:
|
|
498
|
+
list[list[list[np.ndarray | None]]]: For each path, a list of curve
|
|
499
|
+
colors where each curve has a two-element list of vertex colors.
|
|
500
|
+
"""
|
|
501
|
+
return self._get_paths_attribute("color")
|
|
502
|
+
|
|
503
|
+
@color.setter
|
|
504
|
+
def color(self, value: None | tuple[float, float, float] | np.ndarray):
|
|
505
|
+
"""Set color for all paths.
|
|
506
|
+
|
|
507
|
+
Args:
|
|
508
|
+
value (tuple[float, float, float] | np.ndarray | None): RGB color as
|
|
509
|
+
tuple/ndarray, or None to clear.
|
|
510
|
+
"""
|
|
511
|
+
color_value = np.array(value, dtype=np.float32) if value is not None else None
|
|
512
|
+
self._set_paths_attribute("color", color_value)
|
|
513
|
+
|
|
514
|
+
@property
|
|
515
|
+
def pressure(self):
|
|
516
|
+
"""Pressures per path.
|
|
517
|
+
|
|
518
|
+
Returns:
|
|
519
|
+
list[list[list[np.float32 | None]]]: For each path, a list of curve
|
|
520
|
+
pressures where each curve has a two-element list of vertex pressures.
|
|
521
|
+
"""
|
|
522
|
+
return self._get_paths_attribute("pressure")
|
|
523
|
+
|
|
524
|
+
@pressure.setter
|
|
525
|
+
def pressure(self, value: None | float | np.float32):
|
|
526
|
+
"""Set pressure for all paths.
|
|
527
|
+
|
|
528
|
+
Args:
|
|
529
|
+
value (float | np.float32 | None): Pressure value or None to clear.
|
|
530
|
+
"""
|
|
531
|
+
pressure_value = np.float32(value) if value is not None else None
|
|
532
|
+
self._set_paths_attribute("pressure", pressure_value)
|
|
533
|
+
|
|
534
|
+
@property
|
|
535
|
+
def thickness(self):
|
|
536
|
+
"""Thicknesses per path.
|
|
537
|
+
|
|
538
|
+
Returns:
|
|
539
|
+
list[list[list[np.float32 | None]]]: For each path, a list of curve
|
|
540
|
+
thicknesses where each curve has a two-element list of vertex values.
|
|
541
|
+
"""
|
|
542
|
+
return self._get_paths_attribute("thickness")
|
|
543
|
+
|
|
544
|
+
@thickness.setter
|
|
545
|
+
def thickness(self, value: None | float | np.float32):
|
|
546
|
+
"""Set thickness for all paths.
|
|
547
|
+
|
|
548
|
+
Args:
|
|
549
|
+
value (float | np.float32 | None): Thickness value or None to clear.
|
|
550
|
+
"""
|
|
551
|
+
thickness_value = np.float32(value) if value is not None else None
|
|
552
|
+
self._set_paths_attribute("thickness", thickness_value)
|
|
553
|
+
|
|
554
|
+
@property
|
|
555
|
+
def opacity(self):
|
|
556
|
+
"""Opacities per path.
|
|
557
|
+
|
|
558
|
+
Returns:
|
|
559
|
+
list[list[list[np.float32 | None]]]: For each path, a list of curve
|
|
560
|
+
opacities where each curve has a two-element list of vertex values.
|
|
561
|
+
"""
|
|
562
|
+
return self._get_paths_attribute("opacity")
|
|
563
|
+
|
|
564
|
+
@opacity.setter
|
|
565
|
+
def opacity(self, value: None | float | np.float32):
|
|
566
|
+
"""Set opacity for all paths.
|
|
567
|
+
|
|
568
|
+
Args:
|
|
569
|
+
value (float | np.float32 | None): Opacity (0.0–1.0) or None to clear.
|
|
570
|
+
"""
|
|
571
|
+
opacity_value = np.float32(value) if value is not None else None
|
|
572
|
+
self._set_paths_attribute("opacity", opacity_value)
|
|
573
|
+
|
|
574
|
+
def to_svg(self, size: int = 512, filename: str = None, fit_size: bool = False) -> str:
|
|
575
|
+
"""Convert the sketch to an SVG string.
|
|
576
|
+
|
|
577
|
+
Args:
|
|
578
|
+
size: Size of the SVG canvas (width and height in pixels).
|
|
579
|
+
filename: Optional filename to save the SVG.
|
|
580
|
+
fit_size: If True, scale and center the sketch while preserving aspect ratio.
|
|
581
|
+
|
|
582
|
+
Returns:
|
|
583
|
+
str: SVG representation of the sketch.
|
|
584
|
+
"""
|
|
585
|
+
svg_parts = []
|
|
586
|
+
svg_parts.append('<?xml version="1.0"?>')
|
|
587
|
+
svg_parts.append(f'<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="{size}" height="{size}">')
|
|
588
|
+
svg_parts.append('<defs/>')
|
|
589
|
+
svg_parts.append('<g>')
|
|
590
|
+
|
|
591
|
+
# 计算缩放和偏移参数
|
|
592
|
+
scale = 1.0
|
|
593
|
+
offset_x = 0.0
|
|
594
|
+
offset_y = 0.0
|
|
595
|
+
|
|
596
|
+
if fit_size:
|
|
597
|
+
# 收集所有点以确定边界
|
|
598
|
+
all_points = []
|
|
599
|
+
for path in self.paths:
|
|
600
|
+
for curve in path.curves:
|
|
601
|
+
if not isinstance(curve, Curve):
|
|
602
|
+
raise ValueError("All strokes must be of type `Curve`.")
|
|
603
|
+
|
|
604
|
+
p0, p1, p2, p3 = curve.p_start, curve.p_crtl1, curve.p_crtl2, curve.p_end
|
|
605
|
+
all_points.append([p0.x, p0.y])
|
|
606
|
+
all_points.append([p1.x, p1.y])
|
|
607
|
+
all_points.append([p2.x, p2.y])
|
|
608
|
+
all_points.append([p3.x, p3.y])
|
|
609
|
+
|
|
610
|
+
if all_points:
|
|
611
|
+
# 计算边界
|
|
612
|
+
points_array = np.array(all_points)
|
|
613
|
+
min_x, min_y = np.min(points_array, axis=0)
|
|
614
|
+
max_x, max_y = np.max(points_array, axis=0)
|
|
615
|
+
|
|
616
|
+
# 计算大小和中心
|
|
617
|
+
width = max_x - min_x
|
|
618
|
+
height = max_y - min_y
|
|
619
|
+
center_x = (min_x + max_x) / 2
|
|
620
|
+
center_y = (min_y + max_y) / 2
|
|
621
|
+
|
|
622
|
+
# 计算缩放因子以适应画布并留出边距
|
|
623
|
+
padding = 0.05 # 5%边距
|
|
624
|
+
scale = min((size * (1 - padding * 2)) / width, (size * (1 - padding * 2)) / height)
|
|
625
|
+
|
|
626
|
+
# 计算平移以居中草图
|
|
627
|
+
offset_x = size / 2 - center_x * scale
|
|
628
|
+
offset_y = size / 2 - center_y * scale
|
|
629
|
+
|
|
630
|
+
for path in self.paths:
|
|
631
|
+
for curve in path.curves:
|
|
632
|
+
if not isinstance(curve, Curve):
|
|
633
|
+
raise ValueError("All strokes must be of type `Curve`.")
|
|
634
|
+
|
|
635
|
+
p0, p1, p2, p3 = curve.p_start, curve.p_crtl1, curve.p_crtl2, curve.p_end
|
|
636
|
+
color = curve.color[0] if curve.color[0] is not None else (0, 0, 0)
|
|
637
|
+
color_hex = f'#{int(color[0]*255):02x}{int(color[1]*255):02x}{int(color[2]*255):02x}'
|
|
638
|
+
thickness = curve.thickness[0] if curve.thickness[0] is not None else 1.0
|
|
639
|
+
opacity = curve.opacity[0] if curve.opacity[0] is not None else 1.0
|
|
640
|
+
|
|
641
|
+
# 直接应用缩放和偏移到每个点的坐标
|
|
642
|
+
x0 = p0.x * scale + offset_x
|
|
643
|
+
y0 = p0.y * scale + offset_y
|
|
644
|
+
x1 = p1.x * scale + offset_x
|
|
645
|
+
y1 = p1.y * scale + offset_y
|
|
646
|
+
x2 = p2.x * scale + offset_x
|
|
647
|
+
y2 = p2.y * scale + offset_y
|
|
648
|
+
x3 = p3.x * scale + offset_x
|
|
649
|
+
y3 = p3.y * scale + offset_y
|
|
650
|
+
|
|
651
|
+
path_d = f'M {x0} {y0} C {x1} {y1}, {x2} {y2}, {x3} {y3}'
|
|
652
|
+
svg_parts.append(f'<path d="{path_d}" stroke="{color_hex}" stroke-width="{thickness}" fill="none" stroke-opacity="{opacity}"/>')
|
|
653
|
+
|
|
654
|
+
svg_parts.append('</g>')
|
|
655
|
+
svg_parts.append('</svg>')
|
|
656
|
+
final_str = '\n'.join(svg_parts)
|
|
657
|
+
|
|
658
|
+
if filename:
|
|
659
|
+
with open(filename, 'w') as f:
|
|
660
|
+
f.write(final_str)
|
|
661
|
+
return final_str
|
|
File without changes
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from .tu_berlin import TUBerlin
|
|
2
|
+
from .quickdraw import QuickDraw
|
|
3
|
+
from .sketchy import Sketchy
|
|
4
|
+
from .hzy_sketch import hzySketch
|
|
5
|
+
from .tracing_vs_freehand import TracingVsFreehand
|
|
6
|
+
from .controlsketch import ControlSketch
|
|
7
|
+
from .opensketch import OpenSketch
|
|
8
|
+
from .sketchx_pris import SketchXPRIS
|
|
9
|
+
from .photosketching import PhotoSketching
|
|
10
|
+
from .gmu_sketch_cleanup import GMUSketchCleanup
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"TUBerlin",
|
|
14
|
+
"QuickDraw",
|
|
15
|
+
"hzySketch",
|
|
16
|
+
"TracingVsFreehand",
|
|
17
|
+
"ControlSketch",
|
|
18
|
+
"OpenSketch",
|
|
19
|
+
"SketchXPRIS",
|
|
20
|
+
"Sketchy",
|
|
21
|
+
"PhotoSketching",
|
|
22
|
+
"GMUSketchCleanup"
|
|
23
|
+
]
|