1 %% Copyright (c) 2011, INRA
2 %% 2007-2011, David Legland <david.legland@grignon.inra.fr>
3 %% 2011 Adapted to Octave by Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
5 %% All rights reserved.
6 %% (simplified BSD License)
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %% list of conditions and the following disclaimer.
14 %% 2. Redistributions in binary form must reproduce the above copyright notice,
15 %% this list of conditions and the following disclaimer in the documentation
16 %% and/or other materials provided with the distribution.
18 %% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 %% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 %% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 %% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 %% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 %% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 %% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 %% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 %% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 %% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 %% POSSIBILITY OF SUCH DAMAGE.
30 %% The views and conclusions contained in the software and documentation are
31 %% those of the authors and should not be interpreted as representing official
32 %% policies, either expressed or implied, of copyright holder.
35 %% @deftypefn {Function File} {@var{pp} =} cbezier2poly (@var{points})
36 %% @deftypefnx {Command} {Function File} {[@var{x} @var{y}] =} cbezier2poly (@var{points},@var{t})
37 %% Returns the polynomial representation of the cubic Bezier defined by the control points @var{points}.
39 %% With only one input argument, calculates the polynomial @var{pp} of the cubic
40 %% Bezier curve defined by the 4 control points stored in @var{points}. The first
41 %% point is the inital point of the curve. The segment joining the first point
42 %% with the second point (first center) defines the tangent of the curve at the initial point.
43 %% The segment that joints the third point (second center) with the fourth defines the tanget at
44 %% the end-point of the curve, which is defined in the fourth point.
45 %% @var{points} is either a 4-by-2 array (vertical concatenation of point
46 %% coordinates), or a 1-by-8 array (horizotnal concatenation of point
47 %% coordinates). @var{pp} is a 2-by-3 array, 1st row is the polynomial for the
48 %% x-coordinate and the 2nd row for the y-coordinate. Each row can be evaluated
49 %% with @code{polyval}. The polynomial @var{pp}(t) is defined for t in [0,1].
51 %% When called with a second input argument @var{t}, it returns the coordinates
52 %% @var{x} and @var{y} corresponding to the polynomial evaluated at @var{t} in
55 %% @seealso{drawBezierCurve, polyval}
58 function varargout = cbezier2poly (points, ti=[])
63 % case of points given as a 4-by-2 array
68 elseif size(points,2) == 8
69 % case of points given as a 1-by-8 array, [X1 Y1 CX1 CX2..]
78 % compute coefficients of Bezier Polynomial
83 pp(:,3) = [3 * c1(1) - 3 * p1(1); ...
84 3 * c1(2) - 3 * p1(2)];
85 pp(:,2) = [3 * p1(1) - 6 * c1(1) + 3 * c2(1); ...
86 3 * p1(2) - 6 * c1(2) + 3 * c2(2)];
87 pp(:,1) = [p2(1) - 3 * c2(1) + 3 * c1(1) - p1(1); ...
88 p2(2) - 3 * c2(2) + 3 * c1(2) - p1(2)];
93 varargout{1} = polyval (pp(1,:), ti);
94 varargout{2} = polyval (pp(2,:), ti);
100 %! points = [45.714286 483.79075; ...
101 %! 241.65656 110.40445; ...
102 %! 80.185847 741.77381; ...
103 %! 537.14286 480.93361];
105 %! pp = cbezier2poly(points);
106 %! t = linspace(0,1,64);
107 %! x = polyval(pp(1,:),t);
108 %! y = polyval(pp(2,:),t);
109 %! plot (x,y,'b-',points([1 4],1),points([1 4],2),'s',...
110 %! points([2 3],1),points([2 3],2),'o');
111 %! line(points([2 1],1),points([2 1],2),'color','r');
112 %! line(points([3 4],1),points([3 4],2),'color','r');
115 %! points = [0 0; ...
120 %! t = linspace(0,1,64);
121 %! [x y] = cbezier2poly(points,t);
122 %! plot (x,y,'b-',points([1 4],1),points([1 4],2),'s',...
123 %! points([2 3],1),points([2 3],2),'o');
124 %! line(points([2 1],1),points([2 1],2),'color','r');
125 %! line(points([3 4],1),points([3 4],2),'color','r');
128 %! points = [0 0; ...
132 %! t = linspace(0,1,64);
134 %! [x y] = cbezier2poly(points,t);
135 %! pp = cbezier2poly(points);
136 %! x2 = polyval(pp(1,:),t);
137 %! y2 = polyval(pp(2,:),t);
142 %! points = [0 0; ...
146 %! t = linspace(0,1,64);
148 %! p = reshape(points,1,8);
149 %! [x y] = cbezier2poly(p,t);
150 %! pp = cbezier2poly(p);
151 %! x2 = polyval(pp(1,:),t);
152 %! y2 = polyval(pp(2,:),t);