]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/geom2d/cbezier2poly.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / geom2d / cbezier2poly.m
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>
4 %%
5 %% All rights reserved.
6 %% (simplified BSD License)
7 %%
8 %% Redistribution and use in source and binary forms, with or without
9 %% modification, are permitted provided that the following conditions are met:
10 %%
11 %% 1. Redistributions of source code must retain the above copyright notice, this
12 %%    list of conditions and the following disclaimer.
13 %%
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.
17 %%
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.
29 %%
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.
33
34 %% -*- texinfo -*-
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}.
38 %%
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].
50 %%
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
53 %% [0,1].
54 %%
55 %% @seealso{drawBezierCurve, polyval}
56 %% @end deftypefn
57
58 function varargout = cbezier2poly (points, ti=[])
59
60
61   % rename points
62   if size(points, 2)==2
63       % case of points given as a 4-by-2 array
64       p1 = points(1,:);
65       c1 = points(2,:);
66       c2 = points(3,:);
67       p2 = points(4,:);
68   elseif size(points,2) == 8
69       % case of points given as a 1-by-8 array, [X1 Y1 CX1 CX2..]
70       p1 = points(1:2);
71       c1 = points(3:4);
72       c2 = points(5:6);
73       p2 = points(7:8);
74   else
75     print_usage ;
76   end
77
78   % compute coefficients of Bezier Polynomial
79   pp = zeros(2,4);
80
81   pp(:,4) = [p1(1); ...
82              p1(2)];
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)];
89
90   if isempty (ti)
91     varargout{1} = pp;
92   else
93     varargout{1} = polyval (pp(1,:), ti);
94     varargout{2} = polyval (pp(2,:), ti);
95   end
96
97 endfunction
98
99 %!demo
100 %! points = [45.714286 483.79075; ...
101 %!           241.65656 110.40445; ...
102 %!           80.185847 741.77381; ...
103 %!           537.14286 480.93361];
104 %!
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');
113
114 %!demo
115 %! points = [0 0; ...
116 %!           1 1; ...
117 %!           1 1; ...
118 %!           2 0];
119 %!
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');
126
127 %!test
128 %! points = [0 0; ...
129 %!           1 1; ...
130 %!           1 1; ...
131 %!           2 0];
132 %! t = linspace(0,1,64);
133 %!
134 %! [x y] = cbezier2poly(points,t);
135 %! pp = cbezier2poly(points);
136 %! x2 = polyval(pp(1,:),t);
137 %! y2 = polyval(pp(2,:),t);
138 %! assert(x,x2);
139 %! assert(y,y2);
140
141 %!test
142 %! points = [0 0; ...
143 %!           1 1; ...
144 %!           1 1; ...
145 %!           2 0];
146 %! t = linspace(0,1,64);
147 %!
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);
153 %! assert(x,x2);
154 %! assert(y,y2);