fix defaults for x any y (to be strings)
[PyX.git] / design / beziers.tex
blobad7a856c52884486c1258d630af8d8b3e6e1d12c
1 \documentclass{article}
2 \usepackage{amsmath}
3 \usepackage{array}
4 \usepackage{graphicx}
5 \newcommand{\sign}{\operatorname{sign}}
7 \begin{document}
9 \section{B\'ezier curves with prescribed tangent directions and curvatures at the endpoints}
11 % <<<
12 Parameterization of the B\'ezier curve, for $x(t)$ and $y(t)$ equivalently,
14 \begin{align}
15 x(t) &= x_0(1-t)^3 + 3x_1t(1-t)^2 + 3x_2t^2(1-t) + x_3 t^3\\
16 \dot x(t) &= 3(x_1-x_0)(1-t)^2 + 3(x_3-x_2)t^2 + 6(x_2-x_1)t(1-t) \\
17 \ddot x(t) &= 6(x_0 - 2x_1 + x_2)(1-t) + 6(x_1-2x_2+x_3) t \\
18 \dddot x(t) &= 6(x_3-x_0) + 18(x_1-x_2)
19 \end{align}
21 The curvature is
22 \begin{equation}
23 \kappa(t) = \frac{\dot x\ddot y - \ddot x\dot y}{[\dot x^2 + \dot y^2]^{3/2}}
24 \end{equation}
26 with the sign
28 \begin{equation}
29 \begin{aligned}
30 \sign\kappa &> 0 \quad\text{for left bendings}\\
31 \sign\kappa &< 0 \quad\text{for right bendings.}
32 \end{aligned}
33 \end{equation}
35 At the endpoints we can summarize:
37 \begin{align}
38 \dot x(0) &= 3(x_1 - x_0) \\
39 \dot x(1) &= 3(x_3 - x_2) \\
40 \ddot x(0) &= 6(x_0 - 2x_1 + x_2) = -4\dot x(0) - 2\dot x(1) + 6(x_3 - x_0) \\
41 \ddot x(1) &= 6(x_1 - 2x_2 + x_3) = +4\dot x(1) + 2\dot x(0) - 6(x_3 - x_0)
42 \end{align}
44 and the same for the $y$ coordinates.
45 The equations for the $\ddot x$ contain a convenient parameterization for this
46 problem, with given endpoints and tangent directions. We now introduce two
47 parameters for the distances between the first and the last pair of control
48 points:
50 \begingroup
51 \arraycolsep=0pt
52 \begin{align}
53 \left(\begin{array}{cc}
54 x_1-x_0 \\ y_1-y_0
55 \end{array}\right)
56 &= \frac{1}{3}
57 \left(\begin{array}{cc}
58 \dot x(0) \\ \dot y(0)
59 \end{array}\right)
60 =: \alpha
61 \left(\begin{array}{cc}
62 t_x(0) \\ t_y(0)
63 \end{array}\right) \\
64 \left(\begin{array}{cc}
65 x_3-x_2 \\ y_3-y_2
66 \end{array}\right)
67 &= \frac{1}{3}
68 \left(\begin{array}{cc}
69 \dot x(1) \\ \dot y(1)
70 \end{array}\right)
71 =: \beta
72 \left(\begin{array}{cc}
73 t_x(1) \\ t_y(1)
74 \end{array}\right) \qquad
75 \end{align}
76 \endgroup
78 Here, the externally prescribed tangent vectors are expected to be parallel to
79 the tangent vectors of the parameterization and normalized to 1. This implies
80 $\alpha>0$ and $\beta>0$ are the distances between the endpoints and their
81 corresponding control points.
83 The problem to now to find proper parameters $\alpha>0$ and $\beta>0$ for a given set of
84 endpoints $(x_0,y_0)$, $(x_3, y_3)$,
85 normalized tangent vectors $\mathbf{t}(0)$, $\mathbf{t}(1)$ and of
86 curvatures $\kappa(0), \kappa(1)$.
87 The rest of the control points is then given by
89 \begin{equation}
90 x_1 = x_0 + \alpha t_x(0) \quad\text{and}\quad
91 x_2 = x_3 - \beta t_x(1).
92 \end{equation}
94 For the curvatures at the endpoints we get a nonlinear equation,
96 \begin{align}
97 \kappa(0) (\dot x^2(0) + \dot y^2(0))^{3/2}
98 &= \dot x(0) \ddot y(0) - \ddot x(0) \dot y(0) \\
99 = 27 \kappa(0) |\alpha|^3
100 &= \begin{aligned}[t]
101 &+\dot x(0) \bigl[- 4\dot y(0) - 2\dot y(1) + 6(y_3-y_0)\bigr] \\
102 &-\dot y(0) \bigl[- 4\dot x(0) - 2\dot x(1) + 6(x_3-x_0)\bigr]
103 \end{aligned}\\
104 &= \begin{aligned}[t]
105 &-2 \bigl[\dot x(0) \dot y(1) - \dot y(0)\dot x(1)\bigr] \\
106 &+6 \bigl[\dot x(0) (y_3-y_0) - \dot y(0) (x_3-x_0)\bigr]
107 \end{aligned}\\
108 &= \begin{aligned}[t]
109 &-18 \alpha\beta \bigl[t_x(0)t_y(1) - t_y(0)t_x(1)\bigr] \\
110 &+18 \alpha \bigl[t_x(0) (y_3-y_0) - t_y(0) (x_3-x_0)\bigr]
111 \end{aligned}
112 \end{align}
114 And short,
116 \begin{equation}
117 0 = \frac{3}{2}\kappa(0) \alpha^2 \sign(\alpha)
118 + \beta \bigl[t_x(0)t_y(1) - t_y(0)t_x(1)\bigr]
119 - \bigl[t_x(0) (y_3-y_0) - t_y(0) (x_3-x_0)\bigr]
120 \end{equation}
122 A similar calculation can be done for the end curvature,
124 \begin{align}
125 \kappa(1) (\dot x^2(1) + \dot y^2(1))^{3/2}
126 &= \dot x(1) \ddot y(1) - \ddot x(1) \dot y(1) \\
127 = 27 \kappa(1) |\beta|^3
128 &= \begin{aligned}[t]
129 &+\dot x(1) \bigl[ 4\dot y(1) + 2\dot y(0) - 6(y_3-y_0)\bigr] \\
130 &-\dot y(1) \bigl[ 4\dot x(1) + 2\dot x(0) - 6(x_3-x_0)\bigr]
131 \end{aligned}\\
132 &= \begin{aligned}[t]
133 &-2 \bigl[\dot x(0) \dot y(1) - \dot y(0)\dot x(1)\bigr] \\
134 &-6 \bigl[\dot x(1) (y_3-y_0) - \dot y(1) (x_3-x_0)\bigr]
135 \end{aligned}\\
136 &= \begin{aligned}[t]
137 &-18 \alpha\beta \bigl[t_x(0)t_y(1) - t_y(0)t_x(1)\bigr] \\
138 &-18 \beta \bigl[t_x(1) (y_3-y_0) - t_y(1) (x_3-x_0)\bigr]
139 \end{aligned}
140 \end{align}
141 \begin{equation}
142 0 = \frac{3}{2}\kappa(1) \beta^2 \sign(\beta)
143 + \alpha \bigl[t_x(0)t_y(1) - t_y(0)t_x(1)\bigr]
144 + \bigl[t_x(1) (y_3-y_0) - t_y(1) (x_3-x_0)\bigr]
145 \end{equation}
147 Alltogether, we find the system of equations that is to be solved.
149 \begin{gather}
150 \begin{aligned}
151 0 &= \frac{3}{2} \kappa(0) \alpha^2 \sign(\alpha) + \beta T - D \\
152 0 &= \frac{3}{2} \kappa(1) \beta^2 \sign(\beta) + \alpha T - E
153 \end{aligned}\\[\medskipamount]
154 \begin{aligned}
155 T &:= t_x(0)t_y(1) - t_y(0)t_x(1) \\
156 D &:= \bigl[t_x(0) (y_3-y_0) - t_y(0) (x_3-x_0)\bigr]\\
157 E &:= \bigl[t_x(1) (y_0-y_3) - t_y(1) (x_0-x_3)\bigr]
158 \end{aligned}
159 \end{gather}
161 Decoupling the two equations causes problems with the absolute values,
163 \begin{align}
164 \alpha = \frac{1}{T}\left(E - \frac{3}{2}\kappa(1)\beta |\beta |\right)\quad\text{and}\quad
165 \beta = \frac{1}{T}\left(D - \frac{3}{2}\kappa(0)\alpha|\alpha|\right)
166 \end{align}
168 Thus, for $\alpha>0$ and $\beta>0$ we need also
170 \begin{align}
171 \frac{1}{T}\left(E - \frac{3}{2}\kappa(1)\beta |\beta |\right) > 0\quad\text{and}\quad
172 \frac{1}{T}\left(D - \frac{3}{2}\kappa(0)\alpha|\alpha|\right) > 0
173 \end{align}
175 which is not always guaranteed. E.g. the combination
177 \begin{equation}
178 T>0,\quad E<0\quad\text{and}\quad \kappa(1)>0
179 \end{equation}
181 is not compatible with a positive value of $\beta$. Under what circumstances do
182 we get such geometrically invalid results? Tests show that even allowing
183 arbitrary signs of the curvatures $\kappa(0)$~and $\kappa(1)$ does not help in
184 all cases.
186 The decoupled equations can be constructed by inserting $\alpha$~and $\beta$
187 into the equations for the curvatures. This reads
188 \begin{align}
189 0 &= \frac{3}{2}\kappa(0)\sign(\alpha)
190 \left(E - \frac{3}{2}\kappa(1)\beta |\beta |\right)^2
191 + \beta T^3 - D T^2 \\
192 &= \begin{aligned}[t]
193 \frac{27}{8} \kappa(0) \kappa^2(1) \sign(\alpha) \beta^4
194 - \frac{9}{2} E \kappa(0) \kappa(1) \sign(\alpha)\sign(\beta) \beta^2
195 + \beta T^3 \\
196 {}+ \frac{3}{2} \kappa(0) \sign(\alpha) E^2 - D T^2
197 \end{aligned}\\
198 0 &= \begin{aligned}[t]
199 \frac{27}{8} \kappa^2(0) \kappa(1) \sign(\beta) \alpha^4
200 - \frac{9}{2} D \kappa(0) \kappa(1) \sign(\alpha)\sign(\beta) \alpha^2
201 + \alpha T^3 \\
202 {}+ \frac{3}{2} \kappa(1) \sign(\beta) D^2 - E T^2
203 \end{aligned}
204 \end{align}
206 % >>>
208 \section{Bounding boxes for B\'ezier curves}
210 % <<<
211 The bounding box is defined by the minimal and maximal values of a
212 curve. Hence the problem decouples for the two coordintes $x$ and $y$.
213 We can search for the minima and maxima within the valid range for the
214 parameter $t$ together with taking into account the values at the
215 boundaries at $t=0$ and $t=1$.
217 For the $x$ coordinate we have:
218 \begin{align}
219 x(t) & = x_0(1-t)^3 + 3x_1t(1-t)^2 + 3x_2t^2(1-t) + x_3 t^3\\
220 & = (x_3-3x_2+3x_1-x_0)t^3 + (3x_0-6x_1+3x_2)t^2 + (3x_1-3x_0)t + x_0\\
221 & = a\,t^3 + \frac{3}{2}\,b\,t^2 + 3\,c\,t + x_0
222 \end{align}
224 The constants $a$, $b$, and $c$ are
226 \begin{align}
227 a & = x_3-3x_2+3x_1-x_0 \\
228 b & = 2x_0-4x_1+2x_2 \\
229 c & = x_1-x_0
230 \end{align}
232 Now $\dot x(t)$ is
234 \begin{equation}
235 \dot x(t) = 3\left[\,a\,t^2 + b\,t + c\,\right]
236 \end{equation}
238 For a numerically stable calculation of the roots of the function, we
239 first compute
241 \begin{equation}
242 q = -\frac{1}{2}\left[\,b+\mathrm{sgn}(b)\sqrt{b^2-4ac}\right]\, .
243 \end{equation}
245 In case the square root is negative, we only need to take into account
246 the values at the boundaries. Otherwise we need to take into account
247 the two solutions
249 \begin{equation}
250 t_1 = \frac{q}{a} \quad\text{and}\quad t_2 = \frac{c}{q}
251 \end{equation}
253 Again, for numerical failures (divisions by zero), we just need to skip
254 the particular solution. The minima and maxima for $x(t)$ can now
255 occur at $t=0$, $t=t_1$, $t=t_2$ and $t=1$.
257 % >>>
259 \section{Replacing B\'ezier curves by straight lines}
261 % <<<
262 \begin{figure}
263 \centerline{\includegraphics{beziertoline}}
264 \caption{Example for a replacement of a B\'ezier curve by a straight
265 line.}
266 \label{fig:beziertoline}
267 \end{figure}
269 To solve certain geometrical tasks like measuring the length of a
270 path or finding intersection points between paths, B\'ezier curves
271 are recusively reduced to smaller B\'ezier curves by splitting the
272 curves at the parameter value 0.5 until the parts become almost
273 straight. Using the notation shown in fig.~\ref{fig:beziertoline} the
274 straightness is expressed by a length measurement summing up the
275 distances $\overline{AB}=l_1$, $\overline{BC}=l_2$ and
276 $\overline{CD}=l_3$ and comparing this to the direct connection
277 $\overline{AD}$. When the difference becomes smaller than a threshold
278 $\epsilon$, the curve is adequately expressed by a straight line
279 either because the curve is almost straight or it is very short.
281 However, although the geometric changes are limited to distances of
282 $\epsilon$, the parametrization $t$ of the B\'ezier curve might be
283 mistakenly represented by the straight line on a much larger scale.
284 In the shown example, the point $X$ on the B\'ezier curve and $Y$ on
285 the straight line are both taken at the parameter value $t=0.5$, but
286 clearly are more separated from each other than one would expect from
287 the geometric distance of the two paths. While the parametrization on
288 a line is proportional to the arc length, a non-linear behaviour is
289 found on a B\'ezier curve. This non-linearity is originated in
290 considerably different lengths $l_1$, $l_2$ and $l_3$ and the mapping
291 of the non-linear parameter to a linear parametrization (in terms of
292 the arc length) can be reduced to a one-dimensional problem upon an
293 error $\epsilon$:
295 \begin{equation}
296 x_0(1-t')+x_3t' = x_0(1-t)^3 + 3x_1t(1-t)^2 + 3x_2t^2(1-t) + x_3 t^3\,.
297 \end{equation}
299 In this one-dimensional approximation the parameter $t'$ performs a
300 linear mapping as for any straight line while $t$ represents the usual
301 B\'ezier curve parametrization. It now becomes a matter of expressing
302 $t$ by $t'$. The polynomial in $t$ to be solved is:
304 \begin{equation}
305 0 = at^3+bt^2+ct+d
306 \end{equation}
308 with
310 \begin{align}
311 a & = x_3-3x_2+3x_1-x_0 = l_1-2l_2+l_3 \\
312 b & = 3x_0-6x_1+3x_2 = -3l_1+3l_2 \\
313 c & = 3x_1-3x_0 = 3l_1 \\
314 d & = t'(x_0-x_3) = -t'(l_1+l_2+l_3)\,.
315 \end{align}
317 For $0\le t'\le1$ there will be at least one solution $0\le t\le1$.
318 Several solutions are possible as well, although they should be close
319 to each other since otherwise the straight line approximation would
320 not be valid at all.
321 % >>>
323 \section{Relative coordinates}
325 % <<<
326 A B\'ezier curve is given by
327 \begin{equation}
328 \vec b(t) = (1-t)^3\vec p_0 + 3t(1-t)^2\vec p_1 + 3t^2(1-t)\vec p_2 + t^3\vec p_3
329 \end{equation}
330 where $\vec p_0 = (x_0, y_0)$, $\vec p_1 = (x_1, y_1)$, $\vec p_2 =
331 (x_2, y_2)$, and $\vec p_3 = (x_3, y_3)$ are the control points.
333 When $\vec p_0$ differs from $\vec p_3$ we can express the B\'ezier
334 curve in relative coordinates $r(t)$ and $s(t)$ by
335 \begin{equation}
336 \vec b(t) = \vec q + r(t)\vec r + s(t)\vec s
337 \end{equation}
338 with
339 \begin{align}
340 \vec q & = \vec p_0\\
341 \vec r & = \vec p_3 - \vec p_0 = \left(\begin{array}{c} x_3 - x_0 \\ y_3 - y_0 \end{array}\right)\\
342 \vec s & = \left(\begin{array}{c} y_3 - y_0 \\ x_0 - x_3 \end{array}\right)
343 \end{align}
345 Note that
346 \begin{equation}
347 \vec r\cdot\vec r = \vec s\cdot\vec s := l^2
348 \end{equation}
349 where $l$ is the distance between $\vec p_0$ and $\vec p_3$.
351 In addition $\vec r$ and $\vec s$ are perpendicular:
352 \begin{equation}
353 \vec r\cdot\vec s = 0
354 \end{equation}
356 We can express the control points in the new coordinates:
357 \begin{align}
358 \vec p_0 & = \vec q\\
359 \vec p_1 & = \vec q + r_1\vec r + s_1\vec s\\
360 \vec p_2 & = \vec q + r_2\vec r + s_2\vec s\\
361 \vec p_3 & = \vec q + \vec r
362 \end{align}
364 The coefficients $r_1$, $r_2$, $s_1$, $s_2$ are given by scalar
365 products due to the properties of $\vec r$ and $\vec s$ given above:
366 \begin{align}
367 r_1 l^2 & = (\vec p_1 - \vec p_0)\cdot\vec r\\
368 r_2 l^2 & = (\vec p_2 - \vec p_0)\cdot\vec r\\
369 s_1 l^2 & = (\vec p_1 - \vec p_0)\cdot\vec s\\
370 s_2 l^2 & = (\vec p_2 - \vec p_0)\cdot\vec s
371 \end{align}
373 The parametric functions $r(t)$ and $s(t)$ become
374 \begin{align}
375 r(t) & = 3t(1-t)^2 r_1 + 3t^2(1-t) r_2 + t^3\\
376 s(t) & = 3t(1-t)^2 s_1 + 3t^2(1-t) s_2
377 \end{align}
379 \emph{Note:} Originally the idea was to use relative coordinates to
380 remove unstabilities in B\'ezier curves (especially cusps) considering
381 all points with $\dot r(t)$ being zero or extremal. This idea works
382 very well in general, and could, for example, also remove
383 self-intersections. (You can get rid of any backwards directed curve
384 sections, i.e. where $\dot r(t)<0$.) However, the whole idea has been
385 discarded as $\dot r(t)$ could be close to zero for $t=0$ and $t=1$.
386 The pre-removal of such problems would introduce major defects to the
387 B\'ezier curve (like changing the direction of the tangential vector
388 at the beginning and the end point).
389 % >>>
391 \section{Invalid parametrisation}
393 % <<<
394 If the derivative of the parametrisation vanishes (both coordinates) at a
395 parameter~$t\in[0,1]$, the parametrisation is considered to be invalid at this
396 point. Under which circumstances can this occur? We search for solutions~$t$ for
397 the quadratic equations
399 \begin{equation}
400 \label{zeroderiv}
401 \begin{aligned}
402 0 = x(t)/3 &= t^2\bigl[(x_3-x_0) - 3(x_2-x_1)\bigr] + 2t\bigl[(x_2-x_1)-(x_1-x_0)\bigr] + (x_1-x_0) \\
403 0 = y(t)/3 &= t^2\bigl[(y_3-y_0) - 3(y_2-y_1)\bigr] + 2t\bigl[(y_2-y_1)-(y_1-y_0)\bigr] + (y_1-y_0)
404 \end{aligned}
405 \end{equation}
407 Using the usual formula for a quadratic equation $0=at^2 + bt + c$, we get the
408 same parameter for $x$- and $y$-coordinates, and the two inequalities from
409 $0\leq t\leq1$,
411 \begin{equation}
412 \label{samet}
413 t_{1,2} = \frac{1}{2a_x} \Bigl(-b_x \pm^1 \sqrt{b_x^2 - 4a_xc_x}\Bigr)
414 = \frac{1}{2a_y} \Bigl(-b_y \pm^2 \sqrt{b_y^2 - 4a_yc_y}\Bigr)
415 \end{equation}
417 The two sign choices are independent of each other.
419 For the moment, we put aside the special cases $p_3=p_0$ and $p_1=p_0$. As
420 affine transformations cannot change the degeneracy of the parametrisation, we
421 can fix three points without loss of generality:
423 \begin{equation}
424 \begin{aligned}
425 (x_0, y_0) = (0, 0),\quad
426 (x_1, y_1) = (0, 1),\quad
427 (x_3, y_3) = (1, 0).
428 \end{aligned}
429 \end{equation}
431 Thus, the only remaining true parameters are $(x_2, y_2)$. We will use $\Delta x
432 = x_2$ and $\Delta y = y_2 - 1$. Equation~\eqref{samet} now becomes
434 \begin{equation}
435 % a_y \Bigl(-b_x/2 \pm^1 \sqrt{(b_x/2)^2 - a_xc_x}\Bigr)
436 % = a_x \Bigl(-b_y/2 \pm^2 \sqrt{(b_y/2)^2 - a_yc_y}\Bigr)
438 % ( -3\Delta y) \Bigl(-(\Delta x ) \pm^1 \sqrt{(\Delta x )^2 }\Bigr)
439 % = (1-3\Delta x) \Bigl(-(\Delta y-1) \pm^2 \sqrt{(\Delta y-1)^2 + 3\Delta y}\Bigr)
441 \label{samet2}
442 ( -3\Delta y) \Bigl(-\Delta x \pm^1 \sqrt{(\Delta x )^2 }\Bigr)
443 = (1-3\Delta x) \Bigl(1-\Delta y \pm^2 \sqrt{1 + \Delta y + \Delta y^2}\Bigr)
444 \end{equation}
446 Special care has to be taken if $a_x=0$ or $a_y=0$. For the inequalities, we
447 have to consider the signs of $a_x$ and $a_y$ explicitly:
448 \arraycolsep=0pt
450 \begin{equation}
451 \begin{aligned}
452 0 \leq -\Delta x \pm^1 \sqrt{\Delta x^2} \leq 1-3\Delta x &\quad\text{if $\Delta x < 1/3$ (or $a_x > 0$)} \\
453 0 \leq \Delta x \mp^1 \sqrt{\Delta x^2} \leq 3\Delta x-1 &\quad\text{if $1/3 < \Delta x$ (or $a_x < 0$)}
454 \end{aligned}
455 \end{equation}
457 which can be reduced to
459 \begin{equation}
460 \label{constr1}
461 \begin{aligned}
462 \text{no constraint} &\quad\text{if $\Delta x \leq 0$} \\
463 \text{only the upper sign of $\pm^1$ is allowed} &\quad\text{if $0 < \Delta x < 1$} \\
464 \text{no constraint} &\quad\text{if $\Delta x \geq 1$}
465 \end{aligned}
466 \end{equation}
468 Between $0$ and $1$ the upper sign makes the inequality to be trivially
469 satisfied, because $-\Delta x + |\Delta x| = 0$.
471 The inequality constraint can equivalently be formulated for $\Delta y$,
473 \begin{equation}
474 \begin{aligned}
475 0 \leq 1-\Delta y \pm^2 \sqrt{1+\Delta y+\Delta y^2} \leq -3\Delta y &\quad\text{if $\Delta y < 0$ (or $a_y > 0$)} \\
476 0 \leq \Delta y-1 \mp^2 \sqrt{1+\Delta y+\Delta y^2} \leq 3\Delta y &\quad\text{if $\Delta y > 0$ (or $a_y < 0$)}
477 \end{aligned}
478 \end{equation}
480 In the first line, the comparison with~$0$ is always true. In the right
481 comparison, the lower sign is always possible and does not yield a constraint,
482 whereas the upper sign is only possible for $\Delta y \leq -1$. In the second
483 line, the upper sign is incompatible with the left comparison, and the lower
484 sign is always possible. We can reduce the conditions to
486 \begin{equation}
487 \label{constr2}
488 \begin{aligned}
489 \text{no constraint} &\quad\text{if $\Delta y \leq -1$} \\
490 \text{only the lower sign of $\pm^2$ is allowed} &\quad\text{if $\Delta y > -1$} \\
491 \end{aligned}
492 \end{equation}
495 Equation~\eqref{samet2} with the two constraints \eqref{constr1} and
496 \eqref{constr2} is the system of equations that gives us the values $\Delta x,
497 \Delta y$ for which the parametrisation could be invalid.
499 First, let us check that the classic case for a cusp is well described. It has
500 $\Delta x=-1, \Delta y=0$. We have no constraint on $x$, and we have to take the
501 lower sign on $y$. Indeed, the left-hand side of eq.~\eqref{samet2} vanishes due
502 for the lower sign, and the right one vanishes also for the lower sign. This
503 case is shown among the examples in figure~\ref{fig:invalid3}.
506 \begin{figure}[ht]
507 \centering
508 \includegraphics{invalid2}%
509 \caption{Absolute value of the difference between left- and right-hand side of
510 eq.~\eqref{samet2}. The dotted lines are given by eqs.~\eqref{leftcurve} and
511 \eqref{rightcurve}.}%
512 \label{fig:invalid2}%
513 \end{figure}%
515 \begin{figure}[ht]
516 \centering
517 \includegraphics{invalid3}%
518 \caption{Some examples of invalid parametrisations from the general
519 (non-degenerate) case described by eqs.~\eqref{samet2}, \eqref{constr1},
520 \eqref{constr2}.}%
521 \label{fig:invalid3}%
522 \end{figure}%
524 Figure~\ref{fig:invalid2} shows the difference between left- and right-hand side
525 of Eq.~\eqref{samet2} for all valid signs, respecting the constraints. The
526 horizontal and vertical lines are those special cases where $a_x=0$ or $a_y=0$
527 and need further considerations. The zero-level curve at $\Delta x < 0$ (dotted
528 curve in the figure) is well described by the equation
530 \begin{equation}
531 \label{leftcurve}
532 \Delta x = \frac{1}{3}\Bigl(-1-2\Delta y - \sqrt{(2\Delta y+1)^2 + 3}\Bigr)
533 \end{equation}
535 which is obtained by taking the upper sign of $\pm^1$ in eq.~\eqref{samet2}:
536 With negative values of $\Delta x$ the left-hand side becomes $6\Delta x\Delta
537 y$. Isolating the square-root and then taking the square leads to a quadratic
538 equation for $\Delta x$, of which eq.~\eqref{leftcurve} is the negative
539 solution. A numerical check shows that this solution corresponds to the lower
540 sign of $\pm^2$ and has thus no restriction in $\Delta y$. The second solution
541 (dotted curve in the figure for $\Delta x>0$) of the mentioned quadratic
542 equation,
544 \begin{equation}
545 \label{rightcurve}
546 \Delta x = \frac{1}{3}\Bigl(-1-2\Delta y + \sqrt{(2\Delta y+1)^2 + 3}\Bigr)
547 \end{equation}
549 corresponds to the upper sign of $\pm^2$ and is thus subject to the constraint
550 \hbox{$\Delta y \leq -1$}.
552 Apart from the general cases on the two curves, we must treat some special cases
553 separately:
555 \begin{itemize}
556 \item $a_y=0$: This appears as a zero-level line in figure~\ref{fig:invalid2},
557 but this is an artefact coming from both sides of eq.~\eqref{samet2} being
558 zero. We have to go back to the original set of
559 equations~\eqref{zeroderiv}: We have in the lower line $\Delta y=0$, thus
560 $t=1/2$. Going back to the upper line with this value, we obtain $\Delta
561 x=-1$. This parameter point is already covered by the general treatment.
562 \item $a_x=0$: This implies $\Delta x=1/3$, and the upper line gives $t=0$.
563 This, however, contradicts the lower line. We do not get an invalid
564 parametrisation point from $a_x=0$.
565 \item $p_1=p_0$: This is either an even more degenerate case (see below), or it
566 can be mapped to the reverse situation of $\Delta x=1, \Delta y=-1$ which
567 was $p_2=p_3$.
568 \item $p_1=p_0$ and $p_2=p_3$: This is a line.
569 \item $p_3=p_0$: There are invalid parametrisation points if all four
570 $p_0,p_1,p_2,p_3$ are collinear. Depending on the sign of
571 $(p_1-p_0)\cdot(p_3-p_2)$ there are one or two invalid points.
572 \item $p_1=p_0=p_2=p_3$: This is a point.
573 \end{itemize}
575 % >>>
577 \section{Self-intersections}
579 % <<<
580 In order to determine the control points which lead to self-intersections, we
581 could analyse the set of equations
583 \begin{equation}
584 x(t_1) = x(t_2)\quad\text{and}\quad
585 y(t_1) = y(t_2) \quad\text{for}\quad t_1\neq t_2.
586 \end{equation}
588 To solve the problem, we could express the solutions $t(\Delta x, \bar x)$ of
589 the cubic equation $\bar x = x(t)$, test their number via Cardano's equation,
590 and, if they are more than one, use a pair of them in the $y$-equation to see
591 whether we get two times the same $y$-value. This would be a formidable task.
593 We here take a different route. As for the invalid parametrisation above, we
594 exclude some degenerate cases and use affine transformations, such that only the
595 parameters $\Delta x=x_2$ and $\Delta y=y_2-1$ are left. It is geometrically
596 evident that a little left and right of the two relevant curves in
597 figure~\ref{fig:invalid2} we must have different situations as far as
598 self-intersections are concerned. Either we find one a little left or a little
599 right of the curves, and none on the respective other side. Further, a
600 self-intersection can clear away when parameters change such that the curve
601 passes through one of its endpoints. These two conditions boil down to the phase
602 diagram in figure~\ref{fig:selfinter2}. In the gray shaded areas,
603 self-intersections are possible.
605 \begin{figure}[t]
606 \includegraphics{selfinter2}%
607 \caption{Self-intersections are possible in the gray-shaded areas. The black,
608 red, green, blue curves are from eqs.~\eqref{leftcurve}, \eqref{rightcurve},
609 \eqref{pass00}, \eqref{pass10}, respectively.}%
610 \label{fig:selfinter2}%
611 \end{figure}
614 We first calculate the parameters $0<t<t$ for which the curve passes through
615 $(0,0)$. The $x$-coordinate leads to $t=3\Delta x/(3\Delta x - 1)$, and the
616 $y$-coordinate to $t=-1/\Delta y$. From the latter we know that again $\Delta
617 y\leq 1$. Setting both parameters equal allows to express $\Delta x(\Delta y)$
618 or inverse,
620 \begin{align}
621 \label{pass00}
622 \Delta x &= \frac{1}{3(\Delta y+1)} \\
623 \Delta y &= \frac{1-3\Delta x}{3\Delta x}
624 \end{align}
627 The calculation for passing through $(1,0)$ is a little more complicated, since
628 the cubic equation for $x$ (which had a double root at $t=0$ before) now reads
629 $0 = 3\Delta xt^2(1-t) + t^3 - 1$. The equation for~$y$ has not changed and
630 leads to the same solution with the same constraint. Using $t=-1/\Delta y$ in
631 the cubic equation for $x$ leads us to
633 \begin{align}
634 \label{pass10}
635 \Delta x &= \frac{1+\Delta y^3}{3(\Delta y+1)}.
636 \end{align}
638 % >>>
640 \section{Removing cusps}
642 A cusp is a point on a B\'ezier curve $\vec b(t)$, where
643 $\mathrm{d}\vec b(t)/\mathrm{d}t=\dot{\vec b}(t)$ vanishs.
644 For stability we don't want it to be almost zero either.
645 For comparison, a straight line
646 $\vec l(t)=\vec p_0 + (\vec p_3-\vec p_0)t$ has the constant derivative
647 $\mathrm{d}\vec l(t)/\mathrm{d}t = \dot{\vec l}(t) = \vec p_3-\vec p_0$.
648 As lines shorter than $\epsilon$ are forbidden, we have
649 $|\dot{\vec l}(t)| \ge \epsilon$. We want to enforce
650 this limit to B\'ezier curves as well, \emph{i.e.}
651 $|\dot{\vec b}(t)| \ge \epsilon$.
653 First we have to take into account the beginning ($t=0$) and end point
654 ($t=1$) of the B\'ezier curve. Here, the derivates are given by
655 $\dot{\vec b}(0) = 3(\vec p_1-\vec p_0)$ and
656 $\dot{\vec b}(1) = 3(\vec p_3-\vec p_2)$. In case the absolute value
657 of the derivatives are smaller than $\epsilon$, we need to shift
658 $\vec p_1$ and $\vec p_2$, respectively. For $\vec p_1$ a new value is
659 choosen in the direction of $\vec p_2$ with respect to $\vec p_0$ if
660 $\vec p_2$ is more distant than $\epsilon/3$, otherwise the direction
661 of $\vec p_3$ is used. The distance of the new point is choosen such
662 that the absolute value of the derivate becomes $\epsilon$. $\vec p_2$
663 is shifted similarly using the unshifted position for $\vec p_1$.
665 Once the derivatives at the boundary fulfil the requirement, we need
666 to search for minimal values of the absolute value (or the square) of
667 the derivate within the valid parameter range. If the absolute value
668 is smaller at an extrema, the curve is split at that point.
669 \end{document}
671 % vim:foldmethod=marker:foldmarker=<<<,>>>