Newer
Older
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
####################################################################################################
#
# PyValentina - A Python implementation of Valentina Pattern Drafting Software
# Copyright (C) 2017 Fabrice Salvaire
#
# 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 math import log, sqrt # pow
from .Primitive import Primitive2D, bounding_box_from_points
from .Segment import Segment2D
from .Vector import Vector2D
####################################################################################################
class QuadraticBezier2D(Primitive2D):
""" 2D Quadratic Bezier Curve """
LineInterpolationPrecision = 0.05
##############################################
def __init__(self, p0, p1, p2):
""" Construct a :class:`Segment2D` from three points. """
self._p0 = Vector2D(p0)
self._p1 = Vector2D(p1)
self._p2 = Vector2D(p2)
##############################################
def __repr__(self):
return self.__class__.__name__ + '({0._p0}, {0._p1}, {0._p2})'.format(self)
##############################################
@property
def p0(self):
return self._p0
@p0.setter
def p0(self, value):
self._p0 = value
@property
def p1(self):
return self._p1
@p1.setter
def p1(self, value):
self._p1 = value
@property
def p2(self):
return self._p2
@p2.setter
def p2(self, value):
self._p2 = value
@property
def start_point(self):
return self._p0
@property
def end_point(self):
return self._p2
##############################################
@property
def length(self):
# calculation by Dave Eberly
# http://www.gamedev.net/topic/551455-length-of-a-generalized-quadratic-bezier-curve-in-3d
A0 = self._p1 - self._p0
A1 = self._p0 - self._p1 * 2 + self._p2
if A1.magnitude_square() != 0:
c = 4 * A1.dot(A1)
b = 8 * A0.dot(A1)
a = 4 * A0.dot(A0)
q = 4 * a * c - b * b
two_cb = 2 * c + b
sum_cba = c + b + a
m0 = 0.25 / c
m1 = q / (8 * c**1.5)
return (m0 * (two_cb * sqrt(sum_cba) - b * sqrt(a)) +
m1 * (log(2 * sqrt(c * sum_cba) + two_cb) - log(2 * sqrt(c * a) + b)))
else:
return 2 * A0.magnitude()
##############################################
def interpolated_length(self):
# Length of the curve obtained via line interpolation
dt = self.LineInterpolationPrecision / (self.end_point - self.start_point).magnitude()
length = 0
t = 0
while t < 1:
t0 = t
t = min(t + dt, 1)
length += (self.point_at_t(t) - self.point_at_t(t0)).magnitude()
return length
##############################################
def point_at_t(self, t):
# if 0 < t or 1 < t:
# raise ValueError()
one_minus_t = 1 - t
return self._p0 * one_minus_t**2 + self._p1 * 2 * t * one_minus_t + self._p2 * t**2
##############################################
def split_at_t(self, t):
# Splits the curve at given position
a = Segment2D(self._p0, self._p1).point_at_t(t)
b = Segment2D(self._p1, self._p2).point_at_t(t)
p = self.point_at_t(t)
return (QuadraticBezier2D(self._p0, a, p), QuadraticBezier2D(p, b, self._p2))
##############################################
def bounding_box(self):
return bounding_box_from_points((self._p0, self._p1, self._p2))
####################################################################################################
_Sqrt3 = sqrt(3)
_Div18Sqrt3 = 18 / _Sqrt3
_OneThird = 1 / 3
_Sqrt3Div36 = _Sqrt3 / 36
class CubicBezier2D(QuadraticBezier2D):
""" 2D Cubic Bezier Curve """
InterpolationPrecision = 0.001
#######################################
def __init__(self, p0, p1, p2, p3):
""" Construct a :class:`Segment2D` from three points. """
QuadraticBezier2D.__init__(self, p0, p1, p2)
self._p3 = Vector2D(p3)
##############################################
def __repr__(self):
return self.__class__.__name__ + '({0._p0}, {0._p1}, {0._p2}, {0._p3})'.format(self)
##############################################
@property
def p3(self):
return self._p3
@p3.setter
def p3(self, value):
self._p3 = value
@property
def end_point(self):
return self._p3
##############################################
@property
def length(self):
return self.adaptive_length_approximation()
##############################################
def point_at_t(self, t):
# if 0 < t or 1 < t:
# raise ValueError()
return (self._p0 +
(self._p1 - self._p0) * 3 * t +
(self._p2 - self._p1 * 2 + self._p0) * 3 * t**2 +
(self._p3 - self._p2 * 3 + self._p1 * 3 - self._p0) * t**3)
##############################################
def _q_point(self):
# Control point for mid-point quadratic approximation
return (self._p2 * 3 - self._p3 + self._p1 * 3 - self._p0) / 4
##############################################
def mid_point_quadratic_approximation(self):
# Mid-point quadratic approximation
p1 = self._q_point()
return QuadraticBezier2D(self._p0, p1, self._p3)
##############################################
def split_at_t(self, t):
# Splits the curve at given position
a = Segment2D(self._p0, self._p1).point_at_t(t)
b = Segment2D(self._p1, self._p2).point_at_t(t)
c = Segment2D(self._p2, self._p3).point_at_t(t)
m = Segment2D(a, b).point_at_t(t)
n = Segment2D(b, c).point_at_t(t)
p = self.point_at_t(t)
return (CubicBezier2D(self._p0, a, m, p), CubicBezier2D(p, n, c, self._p3))
##############################################
def _d01(self):
# The distance between 0 and 1 quadratic aproximations
return (self._p3 - self._p2 * 3 + self._p1 * 3 - self._p0).magnitude() / 2
##############################################
def _t_max(self):
# Split point for adaptive quadratic approximation
return (_Div18Sqrt3 * self.InterpolationPrecision / self._d01())**_OneThird
##############################################
def q_length(self):
# length of the mid-point quadratic approximation
return self.mid_point_quadratic_approximation().length
##############################################
def adaptive_length_approximation(self):
# Calculated length of adaptive quadratic approximation
segments = []
segment = self
t_max = segment._t_max()
while t_max < 1:
split = segment.split_at_t(t_max)
segments.append(split[0])
segment = split[1]
t_max = segment._t_max()
segments.append(segment)
return sum([segment.q_length() for segment in segments])
##############################################
def bounding_box(self):
return bounding_box_from_points((self._p0, self._p1, self._p2, self._p3))