do not keep filehandles around; instead use the same file open method at all places
[PyX/mjg.git] / design / beziers.tex
blob0a34bfd307588e9f99ac11ef633689afd5dffcf3
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 \end{document}
325 % vim:foldmethod=marker:foldmarker=<<<,>>>