update upload data
[PyX.git] / pyx / deformer.py
blob97f4143b65902b199c3c90d49912467e415b7c30
1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2003-2006 Michael Schindler <m-schindler@users.sourceforge.net>
5 # Copyright (C) 2003-2005 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import math, warnings
24 import attr, mathutils, path, normpath, unit, color
26 # specific exception for an invalid parameterization point
27 # used in parallel
28 class InvalidParamException(Exception):
30 def __init__(self, param):
31 self.normsubpathitemparam = param
33 def curvescontrols_from_endlines_pt(B, tangent1, tangent2, r1, r2, softness): # <<<
34 # calculates the parameters for two bezier curves connecting two lines (curvature=0)
35 # starting at B - r1*tangent1
36 # ending at B + r2*tangent2
38 # Takes the corner B
39 # and two tangent vectors heading to and from B
40 # and two radii r1 and r2:
41 # All arguments must be in Points
42 # Returns the seven control points of the two bezier curves:
43 # - start d1
44 # - control points g1 and f1
45 # - midpoint e
46 # - control points f2 and g2
47 # - endpoint d2
49 # make direction vectors d1: from B to A
50 # d2: from B to C
51 d1 = -tangent1[0] / math.hypot(*tangent1), -tangent1[1] / math.hypot(*tangent1)
52 d2 = tangent2[0] / math.hypot(*tangent2), tangent2[1] / math.hypot(*tangent2)
54 # 0.3192 has turned out to be the maximum softness available
55 # for straight lines ;-)
56 f = 0.3192 * softness
57 g = (15.0 * f + math.sqrt(-15.0*f*f + 24.0*f))/12.0
59 # make the control points of the two bezier curves
60 f1 = B[0] + f * r1 * d1[0], B[1] + f * r1 * d1[1]
61 f2 = B[0] + f * r2 * d2[0], B[1] + f * r2 * d2[1]
62 g1 = B[0] + g * r1 * d1[0], B[1] + g * r1 * d1[1]
63 g2 = B[0] + g * r2 * d2[0], B[1] + g * r2 * d2[1]
64 d1 = B[0] + r1 * d1[0], B[1] + r1 * d1[1]
65 d2 = B[0] + r2 * d2[0], B[1] + r2 * d2[1]
66 e = 0.5 * (f1[0] + f2[0]), 0.5 * (f1[1] + f2[1])
68 return (d1, g1, f1, e, f2, g2, d2)
69 # >>>
71 def controldists_from_endgeometry_pt(A, B, tangA, tangB, curvA, curvB, allownegative=0): # <<<
73 """For a curve with given tangents and curvatures at the endpoints this gives the distances between the controlpoints
75 This helper routine returns a list of two distances between the endpoints and the
76 corresponding control points of a (cubic) bezier curve that has
77 prescribed tangents tangentA, tangentB and curvatures curvA, curvB at the
78 end points.
80 Note: The returned distances are not always positive.
81 But only positive values are geometrically correct, so please check!
82 The outcome is sorted so that the first entry is expected to be the
83 most reasonable one
84 """
85 debug = 0
87 def test_divisions(T, D, E, AB, curvA, curvB, debug):# <<<
89 def is_zero(x):
90 try:
91 1.0 / x
92 except ZeroDivisionError:
93 return 1
94 return 0
96 T_is_zero = is_zero(T)
97 curvA_is_zero = is_zero(curvA)
98 curvB_is_zero = is_zero(curvB)
100 if T_is_zero:
101 if curvA_is_zero:
102 assert abs(D) < 1.0e-10
103 a = AB / 3.0
104 if curvB_is_zero:
105 assert abs(E) < 1.0e-10
106 b = AB / 3.0
107 else:
108 b = math.sqrt(abs(E / (1.5 * curvB))) * mathutils.sign(E*curvB)
109 else:
110 a = math.sqrt(abs(D / (1.5 * curvA))) * mathutils.sign(D*curvA)
111 if curvB_is_zero:
112 assert abs(E) < 1.0e-10
113 b = AB / 3.0
114 else:
115 b = math.sqrt(abs(E / (1.5 * curvB))) * mathutils.sign(E*curvB)
116 else:
117 if curvA_is_zero:
118 b = D / T
119 a = (E - 1.5*curvB*b*abs(b)) / T
120 elif curvB_is_zero:
121 a = E / T
122 b = (D - 1.5*curvA*a*abs(a)) / T
123 else:
124 return []
126 if debug:
127 print "fallback with exact zero value"
128 return [(a, b)]
129 # >>>
130 def fallback_smallT(T, D, E, AB, curvA, curvB, threshold, debug):# <<<
131 a = math.sqrt(abs(D / (1.5 * curvA))) * mathutils.sign(D*curvA)
132 b = math.sqrt(abs(E / (1.5 * curvB))) * mathutils.sign(E*curvB)
133 q1 = min(abs(1.5*a*a*curvA), abs(D))
134 q2 = min(abs(1.5*b*b*curvB), abs(E))
135 if (a >= 0 and b >= 0 and
136 abs(b*T) < threshold * q1 and abs(1.5*a*abs(a)*curvA - D) < threshold * q1 and
137 abs(a*T) < threshold * q2 and abs(1.5*b*abs(b)*curvB - E) < threshold * q2):
138 if debug:
139 print "fallback with T approx 0"
140 return [(a, b)]
141 return []
142 # >>>
143 def fallback_smallcurv(T, D, E, AB, curvA, curvB, threshold, debug):# <<<
144 result = []
146 # is curvB approx zero?
147 a = E / T
148 b = (D - 1.5*curvA*a*abs(a)) / T
149 if (a >= 0 and b >= 0 and
150 abs(1.5*b*b*curvB) < threshold * min(abs(a*T), abs(E)) and
151 abs(a*T - E) < threshold * min(abs(a*T), abs(E))):
152 if debug:
153 print "fallback with curvB approx 0"
154 result.append((a, b))
156 # is curvA approx zero?
157 b = D / T
158 a = (E - 1.5*curvB*b*abs(b)) / T
159 if (a >= 0 and b >= 0 and
160 abs(1.5*a*a*curvA) < threshold * min(abs(b*T), abs(D)) and
161 abs(b*T - D) < threshold * min(abs(b*T), abs(D))):
162 if debug:
163 print "fallback with curvA approx 0"
164 result.append((a, b))
166 return result
167 # >>>
168 def findnearest(x, ys): # <<<
169 I = 0
170 Y = ys[I]
171 mindist = abs(x - Y)
173 # find the value in ys which is nearest to x
174 for i, y in enumerate(ys[1:]):
175 dist = abs(x - y)
176 if dist < mindist:
177 I, Y, mindist = i, y, dist
179 return I, Y
180 # >>>
182 # some shortcuts
183 T = tangA[0] * tangB[1] - tangA[1] * tangB[0]
184 D = tangA[0] * (B[1]-A[1]) - tangA[1] * (B[0]-A[0])
185 E = tangB[0] * (A[1]-B[1]) - tangB[1] * (A[0]-B[0])
186 AB = math.hypot(A[0] - B[0], A[1] - B[1])
188 # try if one of the prefactors is exactly zero
189 testsols = test_divisions(T, D, E, AB, curvA, curvB, debug)
190 if testsols:
191 return testsols
193 # The general case:
194 # we try to find all the zeros of the decoupled 4th order problem
195 # for the combined problem:
196 # The control points of a cubic Bezier curve are given by a, b:
197 # A, A + a*tangA, B - b*tangB, B
198 # for the derivation see /design/beziers.tex
199 # 0 = 1.5 a |a| curvA + b * T - D
200 # 0 = 1.5 b |b| curvB + a * T - E
201 # because of the absolute values we get several possibilities for the signs
202 # in the equation. We test all signs, also the invalid ones!
203 if allownegative:
204 signs = [(+1, +1), (-1, +1), (+1, -1), (-1, -1)]
205 else:
206 signs = [(+1, +1)]
208 candidates_a = []
209 candidates_b = []
210 for sign_a, sign_b in signs:
211 coeffs_a = (sign_b*3.375*curvA*curvA*curvB, 0.0, -sign_b*sign_a*4.5*curvA*curvB*D, T**3, sign_b*1.5*curvB*D*D - T*T*E)
212 coeffs_b = (sign_a*3.375*curvA*curvB*curvB, 0.0, -sign_a*sign_b*4.5*curvA*curvB*E, T**3, sign_a*1.5*curvA*E*E - T*T*D)
213 candidates_a += [root for root in mathutils.realpolyroots(*coeffs_a) if sign_a*root >= 0]
214 candidates_b += [root for root in mathutils.realpolyroots(*coeffs_b) if sign_b*root >= 0]
215 solutions = []
216 if candidates_a and candidates_b:
217 for a in candidates_a:
218 i, b = findnearest((D - 1.5*curvA*a*abs(a))/T, candidates_b)
219 solutions.append((a, b))
221 # try if there is an approximate solution
222 for thr in [1.0e-2, 1.0e-1]:
223 if not solutions:
224 solutions = fallback_smallT(T, D, E, AB, curvA, curvB, thr, debug)
225 if not solutions:
226 solutions = fallback_smallcurv(T, D, E, AB, curvA, curvB, thr, debug)
228 # sort the solutions: the more reasonable values at the beginning
229 def mycmp(x,y): # <<<
230 # first the pairs that are purely positive, then all the pairs with some negative signs
231 # inside the two sets: sort by magnitude
232 sx = (x[0] > 0 and x[1] > 0)
233 sy = (y[0] > 0 and y[1] > 0)
235 # experimental stuff:
236 # what criterion should be used for sorting ?
238 #errx = abs(1.5*curvA*x[0]*abs(x[0]) + x[1]*T - D) + abs(1.5*curvB*x[1]*abs(x[1]) + x[0]*T - E)
239 #erry = abs(1.5*curvA*y[0]*abs(y[0]) + y[1]*T - D) + abs(1.5*curvB*y[1]*abs(y[1]) + y[0]*T - E)
240 # # For each equation, a value like
241 # # abs(1.5*curvA*y[0]*abs(y[0]) + y[1]*T - D) / abs(curvA*(D - y[1]*T))
242 # # indicates how good the solution is. In order to avoid the division,
243 # # we here multiply with all four denominators:
244 # errx = max(abs( (1.5*curvA*y[0]*abs(y[0]) + y[1]*T - D) * (curvB*(E - y[0]*T))*(curvA*(D - x[1]*T))*(curvB*(E - x[0]*T)) ),
245 # abs( (1.5*curvB*y[1]*abs(y[1]) + y[0]*T - E) * (curvA*(D - y[1]*T))*(curvA*(D - x[1]*T))*(curvB*(E - x[0]*T)) ))
246 # errx = max(abs( (1.5*curvA*x[0]*abs(x[0]) + x[1]*T - D) * (curvA*(D - y[1]*T))*(curvB*(E - y[0]*T))*(curvB*(E - x[0]*T)) ),
247 # abs( (1.5*curvB*x[1]*abs(x[1]) + x[0]*T - E) * (curvA*(D - y[1]*T))*(curvB*(E - y[0]*T))*(curvA*(D - x[1]*T)) ))
248 #errx = (abs(curvA*x[0]) - 1.0)**2 + (abs(curvB*x[1]) - 1.0)**2
249 #erry = (abs(curvA*y[0]) - 1.0)**2 + (abs(curvB*y[1]) - 1.0)**2
251 errx = x[0]**2 + x[1]**2
252 erry = y[0]**2 + y[1]**2
254 if sx == 1 and sy == 1:
255 # try to use longer solutions if there are any crossings in the control-arms
256 # the following combination yielded fewest sorting errors in test_bezier.py
257 t, s = intersection(A, B, tangA, tangB)
258 t, s = abs(t), abs(s)
259 if (t > 0 and t < x[0] and s > 0 and s < x[1]):
260 if (t > 0 and t < y[0] and s > 0 and s < y[1]):
261 # use the shorter one
262 return cmp(errx, erry)
263 else:
264 # use the longer one
265 return -1
266 else:
267 if (t > 0 and t < y[0] and s > 0 and s < y[1]):
268 # use the longer one
269 return 1
270 else:
271 # use the shorter one
272 return cmp(errx, erry)
273 #return cmp(x[0]**2 + x[1]**2, y[0]**2 + y[1]**2)
274 else:
275 return cmp(sy, sx)
276 # >>>
277 solutions.sort(mycmp)
279 return solutions
280 # >>>
282 def normcurve_from_endgeometry_pt(A, B, tangA, tangB, curvA, curvB): # <<<
283 a, b = controldists_from_endgeometry_pt(A, B, tangA, tangB, curvA, curvB)[0]
284 return normpath.normcurve_pt(A[0], A[1],
285 A[0] + a * tangA[0], A[1] + a * tangA[1],
286 B[0] - b * tangB[0], B[1] - b * tangB[1], B[0], B[1])
287 # >>>
289 def intersection(A, D, tangA, tangD): # <<<
291 """returns the intersection parameters of two evens
293 they are defined by:
294 x(t) = A + t * tangA
295 x(s) = D + s * tangD
297 det = -tangA[0] * tangD[1] + tangA[1] * tangD[0]
298 try:
299 1.0 / det
300 except ArithmeticError:
301 return None, None
303 DA = D[0] - A[0], D[1] - A[1]
305 t = (-tangD[1]*DA[0] + tangD[0]*DA[1]) / det
306 s = (-tangA[1]*DA[0] + tangA[0]*DA[1]) / det
308 return t, s
309 # >>>
311 class deformer(attr.attr):
313 def deform (self, basepath):
314 return basepath
316 class cycloid(deformer): # <<<
317 """Wraps a cycloid around a path.
319 The outcome looks like a spring with the originalpath as the axis.
320 radius: radius of the cycloid
321 halfloops: number of halfloops
322 skipfirst/skiplast: undeformed end lines of the original path
323 curvesperhloop:
324 sign: start left (1) or right (-1) with the first halfloop
325 turnangle: angle of perspective on a (3D) spring
326 turnangle=0 will produce a sinus-like cycloid,
327 turnangle=90 will procude a row of connected circles
331 def __init__(self, radius=0.5*unit.t_cm, halfloops=10,
332 skipfirst=1*unit.t_cm, skiplast=1*unit.t_cm, curvesperhloop=3, sign=1, turnangle=45):
333 self.skipfirst = skipfirst
334 self.skiplast = skiplast
335 self.radius = radius
336 self.halfloops = halfloops
337 self.curvesperhloop = curvesperhloop
338 self.sign = sign
339 self.turnangle = turnangle
341 def __call__(self, radius=None, halfloops=None,
342 skipfirst=None, skiplast=None, curvesperhloop=None, sign=None, turnangle=None):
343 if radius is None:
344 radius = self.radius
345 if halfloops is None:
346 halfloops = self.halfloops
347 if skipfirst is None:
348 skipfirst = self.skipfirst
349 if skiplast is None:
350 skiplast = self.skiplast
351 if curvesperhloop is None:
352 curvesperhloop = self.curvesperhloop
353 if sign is None:
354 sign = self.sign
355 if turnangle is None:
356 turnangle = self.turnangle
358 return cycloid(radius=radius, halfloops=halfloops, skipfirst=skipfirst, skiplast=skiplast,
359 curvesperhloop=curvesperhloop, sign=sign, turnangle=turnangle)
361 def deform(self, basepath):
362 resultnormsubpaths = [self.deformsubpath(nsp) for nsp in basepath.normpath().normsubpaths]
363 return normpath.normpath(resultnormsubpaths)
365 def deformsubpath(self, normsubpath):
367 skipfirst = abs(unit.topt(self.skipfirst))
368 skiplast = abs(unit.topt(self.skiplast))
369 radius = abs(unit.topt(self.radius))
370 turnangle = math.radians(self.turnangle)
371 sign = mathutils.sign(self.sign)
373 cosTurn = math.cos(turnangle)
374 sinTurn = math.sin(turnangle)
376 # make list of the lengths and parameters at points on normsubpath
377 # where we will add cycloid-points
378 totlength = normsubpath.arclen_pt()
379 if totlength <= skipfirst + skiplast + 2*radius*sinTurn:
380 warnings.warn("normsubpath is too short for deformation with cycloid -- skipping...")
381 return normsubpath
383 # parameterization is in rotation-angle around the basepath
384 # differences in length, angle ... between two basepoints
385 # and between basepoints and controlpoints
386 Dphi = math.pi / self.curvesperhloop
387 phis = [i * Dphi for i in range(self.halfloops * self.curvesperhloop + 1)]
388 DzDphi = (totlength - skipfirst - skiplast - 2*radius*sinTurn) * 1.0 / (self.halfloops * math.pi * cosTurn)
389 # Dz = (totlength - skipfirst - skiplast - 2*radius*sinTurn) * 1.0 / (self.halfloops * self.curvesperhloop * cosTurn)
390 # zs = [i * Dz for i in range(self.halfloops * self.curvesperhloop + 1)]
391 # from path._arctobcurve:
392 # optimal relative distance along tangent for second and third control point
393 L = 4 * radius * (1 - math.cos(Dphi/2)) / (3 * math.sin(Dphi/2))
395 # Now the transformation of z into the turned coordinate system
396 Zs = [ skipfirst + radius*sinTurn # here the coordinate z starts
397 - sinTurn*radius*math.cos(phi) + cosTurn*DzDphi*phi # the transformed z-coordinate
398 for phi in phis]
399 params = normsubpath._arclentoparam_pt(Zs)[0]
401 # get the positions of the splitpoints in the cycloid
402 points = []
403 for phi, param in zip(phis, params):
404 # the cycloid is a circle that is stretched along the normsubpath
405 # here are the points of that circle
406 basetrafo = normsubpath.trafo([param])[0]
408 # The point on the cycloid, in the basepath's local coordinate system
409 baseZ, baseY = 0, radius*math.sin(phi)
411 # The tangent there, also in local coords
412 tangentX = -cosTurn*radius*math.sin(phi) + sinTurn*DzDphi
413 tangentY = radius*math.cos(phi)
414 tangentZ = sinTurn*radius*math.sin(phi) + DzDphi*cosTurn
415 norm = math.sqrt(tangentX*tangentX + tangentY*tangentY + tangentZ*tangentZ)
416 tangentY, tangentZ = tangentY/norm, tangentZ/norm
418 # Respect the curvature of the basepath for the cycloid's curvature
419 # XXX this is only a heuristic, not a "true" expression for
420 # the curvature in curved coordinate systems
421 pathradius = normsubpath.curveradius_pt([param])[0]
422 if pathradius is not normpath.invalid:
423 factor = (pathradius - baseY) / pathradius
424 factor = abs(factor)
425 else:
426 factor = 1
427 l = L * factor
429 # The control points prior and after the point on the cycloid
430 preeZ, preeY = baseZ - l * tangentZ, baseY - l * tangentY
431 postZ, postY = baseZ + l * tangentZ, baseY + l * tangentY
433 # Now put everything at the proper place
434 points.append(basetrafo.apply_pt(preeZ, sign * preeY) +
435 basetrafo.apply_pt(baseZ, sign * baseY) +
436 basetrafo.apply_pt(postZ, sign * postY))
438 if len(points) <= 1:
439 warnings.warn("normsubpath is too short for deformation with cycloid -- skipping...")
440 return normsubpath
442 # Build the path from the pointlist
443 # containing (control x 2, base x 2, control x 2)
444 if skipfirst > normsubpath.epsilon:
445 normsubpathitems = normsubpath.segments([0, params[0]])[0]
446 normsubpathitems.append(normpath.normcurve_pt(*(points[0][2:6] + points[1][0:4])))
447 else:
448 normsubpathitems = [normpath.normcurve_pt(*(points[0][2:6] + points[1][0:4]))]
449 for i in range(1, len(points)-1):
450 normsubpathitems.append(normpath.normcurve_pt(*(points[i][2:6] + points[i+1][0:4])))
451 if skiplast > normsubpath.epsilon:
452 for nsp in normsubpath.segments([params[-1], len(normsubpath)]):
453 normsubpathitems.extend(nsp.normsubpathitems)
455 # That's it
456 return normpath.normsubpath(normsubpathitems, epsilon=normsubpath.epsilon)
457 # >>>
459 cycloid.clear = attr.clearclass(cycloid)
461 class smoothed(deformer): # <<<
463 """Bends corners in a normpath.
465 This decorator replaces corners in a normpath with bezier curves. There are two cases:
466 - If the corner lies between two lines, _two_ bezier curves will be used
467 that are highly optimized to look good (their curvature is to be zero at the ends
468 and has to have zero derivative in the middle).
469 Additionally, it can controlled by the softness-parameter.
470 - If the corner lies between curves then _one_ bezier is used that is (except in some
471 special cases) uniquely determined by the tangents and curvatures at its end-points.
472 In some cases it is necessary to use only the absolute value of the curvature to avoid a
473 cusp-shaped connection of the new bezier to the old path. In this case the use of
474 "obeycurv=0" allows the sign of the curvature to switch.
475 - The radius argument gives the arclength-distance of the corner to the points where the
476 old path is cut and the beziers are inserted.
477 - Path elements that are too short (shorter than the radius) are skipped
480 def __init__(self, radius, softness=1, obeycurv=0, relskipthres=0.01):
481 self.radius = radius
482 self.softness = softness
483 self.obeycurv = obeycurv
484 self.relskipthres = relskipthres
486 def __call__(self, radius=None, softness=None, obeycurv=None, relskipthres=None):
487 if radius is None:
488 radius = self.radius
489 if softness is None:
490 softness = self.softness
491 if obeycurv is None:
492 obeycurv = self.obeycurv
493 if relskipthres is None:
494 relskipthres = self.relskipthres
495 return smoothed(radius=radius, softness=softness, obeycurv=obeycurv, relskipthres=relskipthres)
497 def deform(self, basepath):
498 return normpath.normpath([self.deformsubpath(normsubpath)
499 for normsubpath in basepath.normpath().normsubpaths])
501 def deformsubpath(self, normsubpath):
502 radius_pt = unit.topt(self.radius)
503 epsilon = normsubpath.epsilon
505 # remove too short normsubpath items (shorter than self.relskipthres*radius_pt or epsilon)
506 pertinentepsilon = max(epsilon, self.relskipthres*radius_pt)
507 pertinentnormsubpath = normpath.normsubpath(normsubpath.normsubpathitems,
508 epsilon=pertinentepsilon)
509 pertinentnormsubpath.flushskippedline()
510 pertinentnormsubpathitems = pertinentnormsubpath.normsubpathitems
512 # calculate the splitting parameters for the pertinentnormsubpathitems
513 arclens_pt = []
514 params = []
515 for pertinentnormsubpathitem in pertinentnormsubpathitems:
516 arclen_pt = pertinentnormsubpathitem.arclen_pt(epsilon)
517 arclens_pt.append(arclen_pt)
518 l1_pt = min(radius_pt, 0.5*arclen_pt)
519 l2_pt = max(0.5*arclen_pt, arclen_pt - radius_pt)
520 params.append(pertinentnormsubpathitem.arclentoparam_pt([l1_pt, l2_pt], epsilon))
522 # handle the first and last pertinentnormsubpathitems for a non-closed normsubpath
523 if not normsubpath.closed:
524 l1_pt = 0
525 l2_pt = max(0, arclens_pt[0] - radius_pt)
526 params[0] = pertinentnormsubpathitems[0].arclentoparam_pt([l1_pt, l2_pt], epsilon)
527 l1_pt = min(radius_pt, arclens_pt[-1])
528 l2_pt = arclens_pt[-1]
529 params[-1] = pertinentnormsubpathitems[-1].arclentoparam_pt([l1_pt, l2_pt], epsilon)
531 newnormsubpath = normpath.normsubpath(epsilon=normsubpath.epsilon)
532 for i in range(len(pertinentnormsubpathitems)):
533 this = i
534 next = (i+1) % len(pertinentnormsubpathitems)
535 thisparams = params[this]
536 nextparams = params[next]
537 thisnormsubpathitem = pertinentnormsubpathitems[this]
538 nextnormsubpathitem = pertinentnormsubpathitems[next]
539 thisarclen_pt = arclens_pt[this]
540 nextarclen_pt = arclens_pt[next]
542 # insert the middle segment
543 newnormsubpath.append(thisnormsubpathitem.segments(thisparams)[0])
545 # insert replacement curves for the corners
546 if next or normsubpath.closed:
548 t1 = thisnormsubpathitem.rotation([thisparams[1]])[0].apply_pt(1, 0)
549 t2 = nextnormsubpathitem.rotation([nextparams[0]])[0].apply_pt(1, 0)
550 # TODO: normpath.invalid
552 if (isinstance(thisnormsubpathitem, normpath.normline_pt) and
553 isinstance(nextnormsubpathitem, normpath.normline_pt)):
555 # case of two lines -> replace by two curves
556 d1, g1, f1, e, f2, g2, d2 = curvescontrols_from_endlines_pt(
557 thisnormsubpathitem.atend_pt(), t1, t2,
558 thisarclen_pt*(1-thisparams[1]), nextarclen_pt*(nextparams[0]), softness=self.softness)
560 p1 = thisnormsubpathitem.at_pt([thisparams[1]])[0]
561 p2 = nextnormsubpathitem.at_pt([nextparams[0]])[0]
563 newnormsubpath.append(normpath.normcurve_pt(*(d1 + g1 + f1 + e)))
564 newnormsubpath.append(normpath.normcurve_pt(*(e + f2 + g2 + d2)))
566 else:
568 # generic case -> replace by a single curve with prescribed tangents and curvatures
569 p1 = thisnormsubpathitem.at_pt([thisparams[1]])[0]
570 p2 = nextnormsubpathitem.at_pt([nextparams[0]])[0]
571 c1 = thisnormsubpathitem.curvature_pt([thisparams[1]])[0]
572 c2 = nextnormsubpathitem.curvature_pt([nextparams[0]])[0]
573 # TODO: normpath.invalid
575 # TODO: more intelligent fallbacks:
576 # circle -> line
577 # circle -> circle
579 if not self.obeycurv:
580 # do not obey the sign of the curvature but
581 # make the sign such that the curve smoothly passes to the next point
582 # this results in a discontinuous curvature
583 # (but the absolute value is still continuous)
584 s1 = +mathutils.sign(t1[0] * (p2[1]-p1[1]) - t1[1] * (p2[0]-p1[0]))
585 s2 = -mathutils.sign(t2[0] * (p2[1]-p1[1]) - t2[1] * (p2[0]-p1[0]))
586 c1 = s1 * abs(c1)
587 c2 = s2 * abs(c2)
589 # get the length of the control "arms"
590 controldists = controldists_from_endgeometry_pt(p1, p2, t1, t2, c1, c2)
592 if controldists and (controldists[0][0] >= 0 and controldists[0][1] >= 0):
593 # use the first entry in the controldists
594 # this should be the "smallest" pair
595 a, d = controldists[0]
596 # avoid curves with invalid parameterization
597 a = max(a, epsilon)
598 d = max(d, epsilon)
600 # avoid overshooting at the corners:
601 # this changes not only the sign of the curvature
602 # but also the magnitude
603 if not self.obeycurv:
604 t, s = intersection(p1, p2, t1, t2)
605 if (t is not None and s is not None and
606 t > 0 and s < 0):
607 a = min(a, abs(t))
608 d = min(d, abs(s))
610 else:
611 # use a fallback
612 t, s = intersection(p1, p2, t1, t2)
613 if t is not None and s is not None:
614 a = 0.65 * abs(t)
615 d = 0.65 * abs(s)
616 else:
617 # if there is no useful result:
618 # take an arbitrary smoothing curve that does not obey
619 # the curvature constraints
620 dist = math.hypot(p1[0] - p2[0], p1[1] - p2[1])
621 a = dist / (3.0 * math.hypot(*t1))
622 d = dist / (3.0 * math.hypot(*t2))
624 # calculate the two missing control points
625 q1 = p1[0] + a * t1[0], p1[1] + a * t1[1]
626 q2 = p2[0] - d * t2[0], p2[1] - d * t2[1]
628 newnormsubpath.append(normpath.normcurve_pt(*(p1 + q1 + q2 + p2)))
630 if normsubpath.closed:
631 newnormsubpath.close()
632 return newnormsubpath
634 # >>>
636 smoothed.clear = attr.clearclass(smoothed)
638 class parallel(deformer): # <<<
640 """creates a parallel normpath with constant distance to the original normpath
642 A positive 'distance' results in a curve left of the original one -- and a
643 negative 'distance' in a curve at the right. Left/Right are understood in
644 terms of the parameterization of the original curve. For each path element
645 a parallel curve/line is constructed. At corners, either a circular arc is
646 drawn around the corner, or, if possible, the parallel curve is cut in
647 order to also exhibit a corner.
649 distance: the distance of the parallel normpath
650 relerr: distance*relerr is the maximal allowed error in the parallel distance
651 sharpoutercorners: make the outer corners not round but sharp.
652 The inner corners (corners after inflection points) will stay round
653 dointersection: boolean for doing the intersection step (default: 1).
654 Set this value to 0 if you want the whole parallel path
655 checkdistanceparams: a list of parameter values in the interval (0,1) where the
656 parallel distance is checked on each normpathitem
657 lookforcurvatures: number of points per normpathitem where is looked for
658 a critical value of the curvature
661 # TODO:
662 # * do testing for curv=0, T=0, D=0, E=0 cases
663 # * do testing for several random curves
664 # -- does the recursive deformnicecurve converge?
667 def __init__(self, distance, relerr=0.05, sharpoutercorners=0, dointersection=1,
668 checkdistanceparams=[0.5], lookforcurvatures=11, debug=None):
669 self.distance = distance
670 self.relerr = relerr
671 self.sharpoutercorners = sharpoutercorners
672 self.checkdistanceparams = checkdistanceparams
673 self.lookforcurvatures = lookforcurvatures
674 self.dointersection = dointersection
675 self.debug = debug
677 def __call__(self, distance=None, relerr=None, sharpoutercorners=None, dointersection=None,
678 checkdistanceparams=None, lookforcurvatures=None, debug=None):
679 # returns a copy of the deformer with different parameters
680 if distance is None:
681 distance = self.distance
682 if relerr is None:
683 relerr = self.relerr
684 if sharpoutercorners is None:
685 sharpoutercorners = self.sharpoutercorners
686 if dointersection is None:
687 dointersection = self.dointersection
688 if checkdistanceparams is None:
689 checkdistanceparams = self.checkdistanceparams
690 if lookforcurvatures is None:
691 lookforcurvatures = self.lookforcurvatures
692 if debug is None:
693 debug = self.debug
695 return parallel(distance=distance, relerr=relerr,
696 sharpoutercorners=sharpoutercorners,
697 dointersection=dointersection,
698 checkdistanceparams=checkdistanceparams,
699 lookforcurvatures=lookforcurvatures,
700 debug=debug)
702 def deform(self, basepath):
703 self.dist_pt = unit.topt(self.distance)
704 resultnormsubpaths = []
705 for nsp in basepath.normpath().normsubpaths:
706 parallel_normpath = self.deformsubpath(nsp)
707 resultnormsubpaths += parallel_normpath.normsubpaths
708 result = normpath.normpath(resultnormsubpaths)
709 return result
711 def deformsubpath(self, orig_nsp): # <<<
713 """returns a list of normsubpaths building the parallel curve"""
715 dist = self.dist_pt
716 epsilon = orig_nsp.epsilon
718 # avoid too small dists: we would run into instabilities
719 if abs(dist) < abs(epsilon):
720 return normpath.normpath([orig_nsp])
722 result = normpath.normpath()
724 # iterate over the normsubpath in the following manner:
725 # * for each item first append the additional arc / intersect
726 # and then add the next parallel piece
727 # * for the first item only add the parallel piece
728 # (because this is done for next_orig_nspitem, we need to start with next=0)
729 for i in range(len(orig_nsp.normsubpathitems)):
730 prev = i-1
731 next = i
732 prev_orig_nspitem = orig_nsp.normsubpathitems[prev]
733 next_orig_nspitem = orig_nsp.normsubpathitems[next]
735 stepsize = 0.01
736 prev_param, prev_rotation = self.valid_near_rotation(prev_orig_nspitem, 1, 0, stepsize, 0.5*epsilon)
737 next_param, next_rotation = self.valid_near_rotation(next_orig_nspitem, 0, 1, stepsize, 0.5*epsilon)
738 # TODO: eventually shorten next_orig_nspitem
740 prev_tangent = prev_rotation.apply_pt(1, 0)
741 next_tangent = next_rotation.apply_pt(1, 0)
743 # get the next parallel piece for the normpath
744 try:
745 next_parallel_normpath = self.deformsubpathitem(next_orig_nspitem, epsilon)
746 except InvalidParamException, e:
747 invalid_nspitem_param = e.normsubpathitemparam
748 # split the nspitem apart and continue with pieces that do not contain
749 # the invalid point anymore. At the end, simply take one piece, otherwise two.
750 stepsize = 0.01
751 if self.length_pt(next_orig_nspitem, invalid_nspitem_param, 0) > epsilon:
752 if self.length_pt(next_orig_nspitem, invalid_nspitem_param, 1) > epsilon:
753 p1, foo = self.valid_near_rotation(next_orig_nspitem, invalid_nspitem_param, 0, stepsize, 0.5*epsilon)
754 p2, foo = self.valid_near_rotation(next_orig_nspitem, invalid_nspitem_param, 1, stepsize, 0.5*epsilon)
755 segments = next_orig_nspitem.segments([0, p1, p2, 1])
756 segments = segments[0], segments[2].modifiedbegin_pt(*(segments[0].atend_pt()))
757 else:
758 p1, foo = self.valid_near_rotation(next_orig_nspitem, invalid_nspitem_param, 0, stepsize, 0.5*epsilon)
759 segments = next_orig_nspitem.segments([0, p1])
760 else:
761 p2, foo = self.valid_near_rotation(next_orig_nspitem, invalid_nspitem_param, 1, stepsize, 0.5*epsilon)
762 segments = next_orig_nspitem.segments([p2, 1])
764 next_parallel_normpath = self.deformsubpath(normpath.normsubpath(segments, epsilon=epsilon))
766 if not (next_parallel_normpath.normsubpaths and next_parallel_normpath[0].normsubpathitems):
767 continue
769 # this starts the whole normpath
770 if not result.normsubpaths:
771 result = next_parallel_normpath
772 continue
774 # sinus of the angle between the tangents
775 # sinangle > 0 for a left-turning nexttangent
776 # sinangle < 0 for a right-turning nexttangent
777 sinangle = prev_tangent[0]*next_tangent[1] - prev_tangent[1]*next_tangent[0]
778 cosangle = prev_tangent[0]*next_tangent[0] + prev_tangent[1]*next_tangent[1]
779 if cosangle < 0 or abs(dist*math.asin(sinangle)) >= epsilon:
780 if self.sharpoutercorners and dist*sinangle < 0:
781 A1, A2 = result.atend_pt(), next_parallel_normpath.atbegin_pt()
782 t1, t2 = intersection(A1, A2, prev_tangent, next_tangent)
783 B = A1[0] + t1 * prev_tangent[0], A1[1] + t1 * prev_tangent[1]
784 arc_normpath = normpath.normpath([normpath.normsubpath([
785 normpath.normline_pt(A1[0], A1[1], B[0], B[1]),
786 normpath.normline_pt(B[0], B[1], A2[0], A2[1])
787 ])])
788 else:
789 # We must append an arc around the corner
790 arccenter = next_orig_nspitem.atbegin_pt()
791 arcbeg = result.atend_pt()
792 arcend = next_parallel_normpath.atbegin_pt()
793 angle1 = math.atan2(arcbeg[1] - arccenter[1], arcbeg[0] - arccenter[0])
794 angle2 = math.atan2(arcend[1] - arccenter[1], arcend[0] - arccenter[0])
796 # depending on the direction we have to use arc or arcn
797 if dist > 0:
798 arcclass = path.arcn_pt
799 else:
800 arcclass = path.arc_pt
801 arc_normpath = path.path(arcclass(
802 arccenter[0], arccenter[1], abs(dist),
803 math.degrees(angle1), math.degrees(angle2))).normpath(epsilon=epsilon)
805 # append the arc to the parallel path
806 result.join(arc_normpath)
807 # append the next parallel piece to the path
808 result.join(next_parallel_normpath)
809 else:
810 # The path is quite straight between prev and next item:
811 # normpath.normpath.join adds a straight line if necessary
812 result.join(next_parallel_normpath)
815 # end here if nothing has been found so far
816 if not (result.normsubpaths and result[-1].normsubpathitems):
817 return result
819 # the curve around the closing corner may still be missing
820 if orig_nsp.closed:
821 # TODO: normpath.invalid
822 stepsize = 0.01
823 prev_param, prev_rotation = self.valid_near_rotation(result[-1][-1], 1, 0, stepsize, 0.5*epsilon)
824 next_param, next_rotation = self.valid_near_rotation(result[0][0], 0, 1, stepsize, 0.5*epsilon)
825 # TODO: eventually shorten next_orig_nspitem
827 prev_tangent = prev_rotation.apply_pt(1, 0)
828 next_tangent = next_rotation.apply_pt(1, 0)
829 sinangle = prev_tangent[0]*next_tangent[1] - prev_tangent[1]*next_tangent[0]
830 cosangle = prev_tangent[0]*next_tangent[0] + prev_tangent[1]*next_tangent[1]
832 if cosangle < 0 or abs(dist*math.asin(sinangle)) >= epsilon:
833 # We must append an arc around the corner
834 # TODO: avoid the code dublication
835 if self.sharpoutercorners and dist*sinangle < 0:
836 A1, A2 = result.atend_pt(), result.atbegin_pt()
837 t1, t2 = intersection(A1, A2, prev_tangent, next_tangent)
838 B = A1[0] + t1 * prev_tangent[0], A1[1] + t1 * prev_tangent[1]
839 arc_normpath = normpath.normpath([normpath.normsubpath([
840 normpath.normline_pt(A1[0], A1[1], B[0], B[1]),
841 normpath.normline_pt(B[0], B[1], A2[0], A2[1])
842 ])])
843 else:
844 arccenter = orig_nsp.atend_pt()
845 arcbeg = result.atend_pt()
846 arcend = result.atbegin_pt()
847 angle1 = math.atan2(arcbeg[1] - arccenter[1], arcbeg[0] - arccenter[0])
848 angle2 = math.atan2(arcend[1] - arccenter[1], arcend[0] - arccenter[0])
850 # depending on the direction we have to use arc or arcn
851 if dist > 0:
852 arcclass = path.arcn_pt
853 else:
854 arcclass = path.arc_pt
855 arc_normpath = path.path(arcclass(
856 arccenter[0], arccenter[1], abs(dist),
857 math.degrees(angle1), math.degrees(angle2))).normpath(epsilon=epsilon)
859 # append the arc to the parallel path
860 if (result.normsubpaths and result[-1].normsubpathitems and
861 arc_normpath.normsubpaths and arc_normpath[-1].normsubpathitems):
862 result.join(arc_normpath)
864 if len(result) == 1:
865 result[0].close()
866 else:
867 # if the parallel normpath is split into several subpaths anyway,
868 # then use the natural beginning and ending
869 # closing is not possible anymore
870 for nspitem in result[0]:
871 result[-1].append(nspitem)
872 result.normsubpaths = result.normsubpaths[1:]
874 if self.dointersection:
875 result = self.rebuild_intersected_normpath(result, normpath.normpath([orig_nsp]), epsilon)
877 return result
878 # >>>
879 def deformsubpathitem(self, nspitem, epsilon): # <<<
881 """Returns a parallel normpath for a single normsubpathitem
883 Analyzes the curvature of a normsubpathitem and returns a normpath with
884 the appropriate number of normsubpaths. This must be a normpath because
885 a normcurve can be strongly curved, such that the parallel path must
886 contain a hole"""
888 dist = self.dist_pt
890 # for a simple line we return immediately
891 if isinstance(nspitem, normpath.normline_pt):
892 normal = nspitem.rotation([0])[0].apply_pt(0, 1)
893 start = nspitem.atbegin_pt()
894 end = nspitem.atend_pt()
895 return path.line_pt(
896 start[0] + dist * normal[0], start[1] + dist * normal[1],
897 end[0] + dist * normal[0], end[1] + dist * normal[1]).normpath(epsilon=epsilon)
899 # for a curve we have to check if the curvatures
900 # cross the singular value 1/dist
901 crossings = self.distcrossingparameters(nspitem, epsilon)
903 # depending on the number of crossings we must consider
904 # three different cases:
905 if crossings:
906 # The curvature crosses the borderline 1/dist
907 # the parallel curve contains points with infinite curvature!
908 result = normpath.normpath()
910 # we need the endpoints of the nspitem
911 if self.length_pt(nspitem, crossings[0], 0) > epsilon:
912 crossings.insert(0, 0)
913 if self.length_pt(nspitem, crossings[-1], 1) > epsilon:
914 crossings.append(1)
916 for i in range(len(crossings) - 1):
917 middleparam = 0.5*(crossings[i] + crossings[i+1])
918 middlecurv = nspitem.curvature_pt([middleparam])[0]
919 if middlecurv is normpath.invalid:
920 raise InvalidParamException(middleparam)
921 # the radius is good if
922 # - middlecurv and dist have opposite signs or
923 # - middlecurv is "smaller" than 1/dist
924 if middlecurv*dist < 0 or abs(dist*middlecurv) < 1:
925 parallel_nsp = self.deformnicecurve(nspitem.segments(crossings[i:i+2])[0], epsilon)
926 # never append empty normsubpaths
927 if parallel_nsp.normsubpathitems:
928 result.append(parallel_nsp)
930 return result
932 else:
933 # the curvature is either bigger or smaller than 1/dist
934 middlecurv = nspitem.curvature_pt([0.5])[0]
935 if dist*middlecurv < 0 or abs(dist*middlecurv) < 1:
936 # The curve is everywhere less curved than 1/dist
937 # We can proceed finding the parallel curve for the whole piece
938 parallel_nsp = self.deformnicecurve(nspitem, epsilon)
939 # never append empty normsubpaths
940 if parallel_nsp.normsubpathitems:
941 return normpath.normpath([parallel_nsp])
942 else:
943 return normpath.normpath()
944 else:
945 # the curve is everywhere stronger curved than 1/dist
946 # There is nothing to be returned.
947 return normpath.normpath()
949 # >>>
950 def deformnicecurve(self, normcurve, epsilon, startparam=0.0, endparam=1.0): # <<<
952 """Returns a parallel normsubpath for the normcurve.
954 This routine assumes that the normcurve is everywhere
955 'less' curved than 1/dist and contains no point with an
956 invalid parameterization
958 dist = self.dist_pt
959 T_threshold = 1.0e-5
961 # normalized tangent directions
962 tangA, tangD = normcurve.rotation([startparam, endparam])
963 # if we find an unexpected normpath.invalid we have to
964 # parallelise this normcurve on the level of split normsubpaths
965 if tangA is normpath.invalid:
966 raise InvalidParamException(startparam)
967 if tangD is normpath.invalid:
968 raise InvalidParamException(endparam)
969 tangA = tangA.apply_pt(1, 0)
970 tangD = tangD.apply_pt(1, 0)
972 # the new starting points
973 orig_A, orig_D = normcurve.at_pt([startparam, endparam])
974 A = orig_A[0] - dist * tangA[1], orig_A[1] + dist * tangA[0]
975 D = orig_D[0] - dist * tangD[1], orig_D[1] + dist * tangD[0]
977 # we need to end this _before_ we will run into epsilon-problems
978 # when creating curves we do not want to calculate the length of
979 # or even split it for recursive calls
980 if (math.hypot(A[0] - D[0], A[1] - D[1]) < epsilon and
981 math.hypot(tangA[0] - tangD[0], tangA[1] - tangD[1]) < T_threshold):
982 return normpath.normsubpath([normpath.normline_pt(A[0], A[1], D[0], D[1])])
984 result = normpath.normsubpath(epsilon=epsilon)
985 # is there enough space on the normals before they intersect?
986 a, d = intersection(orig_A, orig_D, (-tangA[1], tangA[0]), (-tangD[1], tangD[0]))
987 # a,d are the lengths to the intersection points:
988 # for a (and equally for b) we can proceed in one of the following cases:
989 # a is None (means parallel normals)
990 # a and dist have opposite signs (and the same for b)
991 # a has the same sign but is bigger
992 if ( (a is None or a*dist < 0 or abs(a) > abs(dist) + epsilon) or
993 (d is None or d*dist < 0 or abs(d) > abs(dist) + epsilon) ):
994 # the original path is long enough to draw a parallel piece
995 # this is the generic case. Get the parallel curves
996 orig_curvA, orig_curvD = normcurve.curvature_pt([startparam, endparam])
997 # normpath.invalid may not appear here because we have asked
998 # for this already at the tangents
999 assert orig_curvA is not normpath.invalid
1000 assert orig_curvD is not normpath.invalid
1001 curvA = orig_curvA / (1.0 - dist*orig_curvA)
1002 curvD = orig_curvD / (1.0 - dist*orig_curvD)
1004 # first try to approximate the normcurve with a single item
1005 controldistpairs = controldists_from_endgeometry_pt(A, D, tangA, tangD, curvA, curvD)
1007 if controldistpairs:
1008 # TODO: is it good enough to get the first entry here?
1009 # from testing: this fails if there are loops in the original curve
1010 a, d = controldistpairs[0]
1011 if a >= 0 and d >= 0:
1012 if a < epsilon and d < epsilon:
1013 result = normpath.normsubpath([normpath.normline_pt(A[0], A[1], D[0], D[1])], epsilon=epsilon)
1014 else:
1015 # we avoid curves with invalid parameterization
1016 a = max(a, epsilon)
1017 d = max(d, epsilon)
1018 result = normpath.normsubpath([normpath.normcurve_pt(
1019 A[0], A[1],
1020 A[0] + a * tangA[0], A[1] + a * tangA[1],
1021 D[0] - d * tangD[0], D[1] - d * tangD[1],
1022 D[0], D[1])], epsilon=epsilon)
1024 # then try with two items, recursive call
1025 if ((not result.normsubpathitems) or
1026 (self.checkdistanceparams and result.normsubpathitems
1027 and not self.distchecked(normcurve, result, epsilon, startparam, endparam))):
1028 # TODO: does this ever converge?
1029 # TODO: what if this hits epsilon?
1030 firstnsp = self.deformnicecurve(normcurve, epsilon, startparam, 0.5*(startparam+endparam))
1031 secondnsp = self.deformnicecurve(normcurve, epsilon, 0.5*(startparam+endparam), endparam)
1032 if not (firstnsp.normsubpathitems and secondnsp.normsubpathitems):
1033 result = normpath.normsubpath(
1034 [normpath.normline_pt(A[0], A[1], D[0], D[1])], epsilon=epsilon)
1035 else:
1036 # we will get problems if the curves are too short:
1037 result = firstnsp.joined(secondnsp)
1039 return result
1040 # >>>
1042 def distchecked(self, orig_normcurve, parallel_normsubpath, epsilon, tstart, tend): # <<<
1044 """Checks the distances between orig_normcurve and parallel_normsubpath
1046 The checking is done at parameters self.checkdistanceparams of orig_normcurve."""
1048 dist = self.dist_pt
1049 # do not look closer than epsilon:
1050 dist_relerr = mathutils.sign(dist) * max(abs(self.relerr*dist), epsilon)
1052 checkdistanceparams = [tstart + (tend-tstart)*t for t in self.checkdistanceparams]
1054 for param, P, rotation in zip(checkdistanceparams,
1055 orig_normcurve.at_pt(checkdistanceparams),
1056 orig_normcurve.rotation(checkdistanceparams)):
1057 # check if the distance is really the wanted distance
1058 # measure the distance in the "middle" of the original curve
1059 if rotation is normpath.invalid:
1060 raise InvalidParamException(param)
1062 normal = rotation.apply_pt(0, 1)
1064 # create a short cutline for intersection only:
1065 cutline = normpath.normsubpath([normpath.normline_pt (
1066 P[0] + (dist - 2*dist_relerr) * normal[0],
1067 P[1] + (dist - 2*dist_relerr) * normal[1],
1068 P[0] + (dist + 2*dist_relerr) * normal[0],
1069 P[1] + (dist + 2*dist_relerr) * normal[1])], epsilon=epsilon)
1071 cutparams = parallel_normsubpath.intersect(cutline)
1072 distances = [math.hypot(P[0] - cutpoint[0], P[1] - cutpoint[1])
1073 for cutpoint in cutline.at_pt(cutparams[1])]
1075 if (not distances) or (abs(min(distances) - abs(dist)) > abs(dist_relerr)):
1076 return 0
1078 return 1
1079 # >>>
1080 def distcrossingparameters(self, normcurve, epsilon, tstart=0, tend=1): # <<<
1082 """Returns a list of parameters where the curvature is 1/distance"""
1084 dist = self.dist_pt
1086 # we _need_ to do this with the curvature, not with the radius
1087 # because the curvature is continuous at the straight line and the radius is not:
1088 # when passing from one slightly curved curve to the other with opposite curvature sign,
1089 # via the straight line, then the curvature changes its sign at curv=0, while the
1090 # radius changes its sign at +/-infinity
1091 # this causes instabilities for nearly straight curves
1093 # include tstart and tend
1094 params = [tstart + i * (tend - tstart) * 1.0 / (self.lookforcurvatures - 1)
1095 for i in range(self.lookforcurvatures)]
1096 curvs = normcurve.curvature_pt(params)
1098 # break everything at invalid curvatures
1099 for param, curv in zip(params, curvs):
1100 if curv is normpath.invalid:
1101 raise InvalidParamException(param)
1103 parampairs = zip(params[:-1], params[1:])
1104 curvpairs = zip(curvs[:-1], curvs[1:])
1106 crossingparams = []
1107 for parampair, curvpair in zip(parampairs, curvpairs):
1108 begparam, endparam = parampair
1109 begcurv, endcurv = curvpair
1110 if (endcurv*dist - 1)*(begcurv*dist - 1) < 0:
1111 # the curvature crosses the value 1/dist
1112 # get the parmeter value by linear interpolation:
1113 middleparam = (
1114 (begparam * abs(begcurv*dist - 1) + endparam * abs(endcurv*dist - 1)) /
1115 (abs(begcurv*dist - 1) + abs(endcurv*dist - 1)))
1116 middleradius = normcurve.curveradius_pt([middleparam])[0]
1118 if middleradius is normpath.invalid:
1119 raise InvalidParamException(middleparam)
1121 if abs(middleradius - dist) < epsilon:
1122 # get the parmeter value by linear interpolation:
1123 crossingparams.append(middleparam)
1124 else:
1125 # call recursively:
1126 cps = self.distcrossingparameters(normcurve, epsilon, tstart=begparam, tend=endparam)
1127 crossingparams += cps
1129 return crossingparams
1130 # >>>
1131 def valid_near_rotation(self, nspitem, param, otherparam, stepsize, epsilon): # <<<
1132 p = param
1133 rot = nspitem.rotation([p])[0]
1134 # run towards otherparam searching for a valid rotation
1135 while rot is normpath.invalid:
1136 p = (1-stepsize)*p + stepsize*otherparam
1137 rot = nspitem.rotation([p])[0]
1138 # walk back to param until near enough
1139 # but do not go further if an invalid point is hit
1140 end, new = nspitem.at_pt([param, p])
1141 far = math.hypot(end[0]-new[0], end[1]-new[1])
1142 pnew = p
1143 while far > epsilon:
1144 pnew = (1-stepsize)*pnew + stepsize*param
1145 end, new = nspitem.at_pt([param, pnew])
1146 far = math.hypot(end[0]-new[0], end[1]-new[1])
1147 if nspitem.rotation([pnew])[0] is normpath.invalid:
1148 break
1149 else:
1150 p = pnew
1151 return p, nspitem.rotation([p])[0]
1152 # >>>
1153 def length_pt(self, path, param1, param2): # <<<
1154 point1, point2 = path.at_pt([param1, param2])
1155 return math.hypot(point1[0] - point2[0], point1[1] - point2[1])
1156 # >>>
1158 def normpath_selfintersections(self, np, epsilon): # <<<
1160 """return all self-intersection points of normpath np.
1162 This does not include the intersections of a single normcurve with itself,
1163 but all intersections of one normpathitem with a different one in the path"""
1165 n = len(np)
1166 linearparams = []
1167 parampairs = []
1168 paramsriap = {}
1169 for nsp_i in range(n):
1170 for nsp_j in range(nsp_i, n):
1171 for nspitem_i in range(len(np[nsp_i])):
1172 if nsp_j == nsp_i:
1173 nspitem_j_range = range(nspitem_i+1, len(np[nsp_j]))
1174 else:
1175 nspitem_j_range = range(len(np[nsp_j]))
1176 for nspitem_j in nspitem_j_range:
1177 intsparams = np[nsp_i][nspitem_i].intersect(np[nsp_j][nspitem_j], epsilon)
1178 if intsparams:
1179 for intsparam_i, intsparam_j in intsparams:
1180 if ( (abs(intsparam_i) < epsilon and abs(1-intsparam_j) < epsilon) or
1181 (abs(intsparam_j) < epsilon and abs(1-intsparam_i) < epsilon) ):
1182 continue
1183 npp_i = normpath.normpathparam(np, nsp_i, float(nspitem_i)+intsparam_i)
1184 npp_j = normpath.normpathparam(np, nsp_j, float(nspitem_j)+intsparam_j)
1185 linearparams.append(npp_i)
1186 linearparams.append(npp_j)
1187 paramsriap[id(npp_i)] = len(parampairs)
1188 paramsriap[id(npp_j)] = len(parampairs)
1189 parampairs.append((npp_i, npp_j))
1190 linearparams.sort()
1191 return linearparams, parampairs, paramsriap
1193 # >>>
1194 def can_continue(self, par_np, param1, param2): # <<<
1195 dist = self.dist_pt
1197 rot1, rot2 = par_np.rotation([param1, param2])
1198 if rot1 is normpath.invalid or rot2 is normpath.invalid:
1199 return 0
1200 curv1, curv2 = par_np.curvature_pt([param1, param2])
1201 tang2 = rot2.apply_pt(1, 0)
1202 norm1 = rot1.apply_pt(0, -1)
1203 norm1 = (dist*norm1[0], dist*norm1[1])
1205 # the self-intersection is valid if the tangents
1206 # point into the correct direction or, for parallel tangents,
1207 # if the curvature is such that the on-going path does not
1208 # enter the region defined by dist
1209 mult12 = norm1[0]*tang2[0] + norm1[1]*tang2[1]
1210 eps = 1.0e-6
1211 if abs(mult12) > eps:
1212 return (mult12 < 0)
1213 else:
1214 # tang1 and tang2 are parallel
1215 if curv2 is normpath.invalid or curv1 is normpath.invalid:
1216 return 0
1217 if dist > 0:
1218 return (curv2 <= curv1)
1219 else:
1220 return (curv2 >= curv1)
1221 # >>>
1222 def rebuild_intersected_normpath(self, par_np, orig_np, epsilon): # <<<
1224 dist = self.dist_pt
1226 # calculate the self-intersections of the par_np
1227 selfintparams, selfintpairs, selfintsriap = self.normpath_selfintersections(par_np, epsilon)
1228 # calculate the intersections of the par_np with the original path
1229 origintparams = par_np.intersect(orig_np)[0]
1231 # visualize the intersection points: # <<<
1232 if self.debug is not None:
1233 for param1, param2 in selfintpairs:
1234 point1, point2 = par_np.at([param1, param2])
1235 self.debug.fill(path.circle(point1[0], point1[1], 0.05), [color.rgb.red])
1236 self.debug.fill(path.circle(point2[0], point2[1], 0.03), [color.rgb.black])
1237 for param in origintparams:
1238 point = par_np.at([param])[0]
1239 self.debug.fill(path.circle(point[0], point[1], 0.05), [color.rgb.green])
1240 # >>>
1242 result = normpath.normpath()
1243 if not selfintparams:
1244 if origintparams:
1245 return result
1246 else:
1247 return par_np
1249 beginparams = []
1250 endparams = []
1251 for i in range(len(par_np)):
1252 beginparams.append(normpath.normpathparam(par_np, i, 0))
1253 endparams.append(normpath.normpathparam(par_np, i, len(par_np[i])))
1255 allparams = selfintparams + origintparams + beginparams + endparams
1256 allparams.sort()
1257 allparamindices = {}
1258 for i, param in enumerate(allparams):
1259 allparamindices[id(param)] = i
1261 done = {}
1262 for param in allparams:
1263 done[id(param)] = 0
1265 def otherparam(p): # <<<
1266 pair = selfintpairs[selfintsriap[id(p)]]
1267 if (p is pair[0]):
1268 return pair[1]
1269 else:
1270 return pair[0]
1271 # >>>
1272 def trial_parampairs(startp): # <<<
1273 tried = {}
1274 for param in allparams:
1275 tried[id(param)] = done[id(param)]
1277 lastp = startp
1278 currentp = allparams[allparamindices[id(startp)] + 1]
1279 result = []
1281 while 1:
1282 if currentp is startp:
1283 result.append((lastp, currentp))
1284 return result
1285 if currentp in selfintparams and otherparam(currentp) is startp:
1286 result.append((lastp, currentp))
1287 return result
1288 if currentp in endparams:
1289 result.append((lastp, currentp))
1290 return result
1291 if tried[id(currentp)]:
1292 return []
1293 if currentp in origintparams:
1294 return []
1295 # follow the crossings on valid startpairs until
1296 # the normsubpath is closed or the end is reached
1297 if (currentp in selfintparams and
1298 self.can_continue(par_np, currentp, otherparam(currentp))):
1299 # go to the next pair on the curve, seen from currentpair[1]
1300 result.append((lastp, currentp))
1301 lastp = otherparam(currentp)
1302 tried[id(currentp)] = 1
1303 tried[id(otherparam(currentp))] = 1
1304 currentp = allparams[allparamindices[id(otherparam(currentp))] + 1]
1305 else:
1306 # go to the next pair on the curve, seen from currentpair[0]
1307 tried[id(currentp)] = 1
1308 tried[id(otherparam(currentp))] = 1
1309 currentp = allparams[allparamindices[id(currentp)] + 1]
1310 assert 0
1311 # >>>
1313 # first the paths that start at the beginning of a subnormpath:
1314 for startp in beginparams + selfintparams:
1315 if done[id(startp)]:
1316 continue
1318 parampairs = trial_parampairs(startp)
1319 if not parampairs:
1320 continue
1322 # collect all the pieces between parampairs
1323 add_nsp = normpath.normsubpath(epsilon=epsilon)
1324 for begin, end in parampairs:
1325 # check that trial_parampairs works correctly
1326 assert begin is not end
1327 # we do not cross the border of a normsubpath here
1328 assert begin.normsubpathindex is end.normsubpathindex
1329 for item in par_np[begin.normsubpathindex].segments(
1330 [begin.normsubpathparam, end.normsubpathparam])[0].normsubpathitems:
1331 # TODO: this should be obsolete with an improved intersection algorithm
1332 # guaranteeing epsilon
1333 if add_nsp.normsubpathitems:
1334 item = item.modifiedbegin_pt(*(add_nsp.atend_pt()))
1335 add_nsp.append(item)
1337 if begin in selfintparams:
1338 done[id(begin)] = 1
1339 #done[otherparam(begin)] = 1
1340 if end in selfintparams:
1341 done[id(end)] = 1
1342 #done[otherparam(end)] = 1
1344 # eventually close the path
1345 if add_nsp and (parampairs[0][0] is parampairs[-1][-1] or
1346 (parampairs[0][0] in selfintparams and otherparam(parampairs[0][0]) is parampairs[-1][-1])):
1347 add_nsp.normsubpathitems[-1] = add_nsp.normsubpathitems[-1].modifiedend_pt(*add_nsp.atbegin_pt())
1348 add_nsp.close()
1350 result.extend([add_nsp])
1352 return result
1354 # >>>
1356 # >>>
1358 parallel.clear = attr.clearclass(parallel)
1360 # vim:foldmethod=marker:foldmarker=<<<,>>>