1
- # BézierBuilder
2
- #
3
- # Copyright (c) 2013, Juan Luis Cano Rodríguez <juanlu001@gmail.com>
4
- # All rights reserved.
5
- #
6
- # Redistribution and use in source and binary forms, with or without modification,
7
- # are permitted provided that the following conditions are met:
8
- #
9
- # * Redistributions of source code must retain the above copyright notice,
10
- # this list of conditions and the following disclaimer.
11
- # * Redistributions in binary form must reproduce the above copyright notice,
12
- # this list of conditions and the following disclaimer in the documentation
13
- # and/or other materials provided with the distribution.
14
- #
15
- # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
- # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
- # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
- # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
19
- # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
- # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
- # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
- # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
- # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
- # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
- # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
-
27
1
"""BézierBuilder, an interactive Bézier curve explorer.
28
2
29
- Just run it with
30
-
31
- $ python bezier_builder.py
32
-
3
+ Copyright (c) 2013, Juan Luis Cano Rodríguez <juanlu001@gmail.com>
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without modification,
7
+ are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright notice,
10
+ this list of conditions and the following disclaimer.
11
+ * Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
19
+ OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
26
"""
34
27
35
- from math import factorial
36
-
37
28
import numpy as np
38
29
from matplotlib .backends .qt_compat import QtCore
39
30
from matplotlib .lines import Line2D
40
31
41
- from .minimvc import Trigger
32
+ from viscm .bezierbuilder .curve import curve_method
33
+ from viscm .minimvc import Trigger
42
34
43
35
44
36
class ControlPointModel :
@@ -193,7 +185,7 @@ def compute_arc_length(xp, yp, method, t=None, grid=256):
193
185
194
186
class SingleBezierCurveModel :
195
187
def __init__ (self , control_point_model , method = "CatmulClark" ):
196
- self .method = eval ( method )
188
+ self .method = curve_method [ method ]
197
189
self .control_point_model = control_point_model
198
190
x , y = self .get_bezier_points ()
199
191
self .bezier_curve = Line2D (x , y )
@@ -215,7 +207,7 @@ def _refresh(self):
215
207
216
208
class TwoBezierCurveModel :
217
209
def __init__ (self , control_point_model , method = "CatmulClark" ):
218
- self .method = eval ( method )
210
+ self .method = curve_method [ method ]
219
211
self .control_point_model = control_point_model
220
212
x , y = self .get_bezier_points ()
221
213
self .bezier_curve = Line2D (x , y )
@@ -286,49 +278,3 @@ def _refresh(self):
286
278
x , y = self .bezier_curve_model .get_bezier_points ()
287
279
self .bezier_curve .set_data (x , y )
288
280
self .canvas .draw ()
289
-
290
-
291
- # We used to use scipy.special.binom here,
292
- # but reimplementing it ourself lets us avoid pulling in a dependency
293
- # scipy just for that one function.
294
- def binom (n , k ):
295
- return factorial (n ) * 1.0 / (factorial (k ) * factorial (n - k ))
296
-
297
-
298
- def Bernstein (n , k ):
299
- """Bernstein polynomial."""
300
- coeff = binom (n , k )
301
-
302
- def _bpoly (x ):
303
- return coeff * x ** k * (1 - x ) ** (n - k )
304
-
305
- return _bpoly
306
-
307
-
308
- def Bezier (points , at ):
309
- """Build Bézier curve from points.
310
- Deprecated. CatmulClark builds nicer splines
311
- """
312
- at = np .asarray (at )
313
- at_flat = at .ravel ()
314
- N = len (points )
315
- curve = np .zeros ((at_flat .shape [0 ], 2 ))
316
- for ii in range (N ):
317
- curve += np .outer (Bernstein (N - 1 , ii )(at_flat ), points [ii ])
318
- return curve .reshape ((* at .shape , 2 ))
319
-
320
-
321
- def CatmulClark (points , at ):
322
- points = np .asarray (points )
323
-
324
- while len (points ) < len (at ):
325
- new_p = np .zeros ((2 * len (points ), 2 ))
326
- new_p [0 ] = points [0 ]
327
- new_p [- 1 ] = points [- 1 ]
328
- new_p [1 :- 2 :2 ] = 3 / 4.0 * points [:- 1 ] + 1 / 4.0 * points [1 :]
329
- new_p [2 :- 1 :2 ] = 1 / 4.0 * points [:- 1 ] + 3 / 4.0 * points [1 :]
330
- points = new_p
331
- xp , yp = zip (* points )
332
- xp = np .interp (at , np .linspace (0 , 1 , len (xp )), xp )
333
- yp = np .interp (at , np .linspace (0 , 1 , len (yp )), yp )
334
- return np .asarray (list (zip (xp , yp )))
0 commit comments