]> Creatis software - CreaPhase.git/blob - octave_packages/nurbs-1.3.6/nrbcirc.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nurbs-1.3.6 / nrbcirc.m
1 function curve = nrbcirc(radius,center,sang,eang)
2
3 % NRBCIRC: Construct a circular arc.
4
5 % Calling Sequence:
6
7 %   crv = nrbcirc()
8 %   crv = nrbcirc(radius)
9 %   crv = nrbcirc(radius,center)
10 %   crv = nrbcirc(radius,center,sang,eang)
11
12 % INPUT:
13
14 %   radius      : Radius of the circle, default 1.0
15
16 %   center      : Center of the circle, default (0,0,0)
17
18 %   sang        : Start angle, default 0 radians (0 degrees)
19
20 %   eang        : End angle, default 2*pi radians (360 degrees)
21
22 % OUTPUT:
23 %
24 %   crv         : NURBS curve for a circular arc.
25
26 % Description:
27
28 %   Constructs NURBS data structure for a circular arc in the x-y plane. If
29 %   no rhs arguments are supplied a unit circle with center (0.0,0.0) is
30 %   constructed. 
31
32 %   Angles are defined as positive in the anti-clockwise direction.
33 %
34 %    Copyright (C) 2000 Mark Spink
35 %
36 %    This program is free software: you can redistribute it and/or modify
37 %    it under the terms of the GNU General Public License as published by
38 %    the Free Software Foundation, either version 2 of the License, or
39 %    (at your option) any later version.
40
41 %    This program is distributed in the hope that it will be useful,
42 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
43 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44 %    GNU General Public License for more details.
45 %
46 %    You should have received a copy of the GNU General Public License
47 %    along with this program.  If not, see <http://www.gnu.org/licenses/>.
48
49 if nargin < 1
50   radius = 1;
51 end
52
53 if nargin < 2
54   center = [];
55 end
56    
57 if nargin < 4
58   sang = 0;
59   eang = 2*pi;
60 end
61
62 sweep = eang - sang;       % sweep angle of arc
63 if sweep < 0
64   sweep = 2*pi + sweep;
65 end
66      
67 if abs(sweep) <= pi/2
68   narcs = 1;                % number of arc segments
69   knots = [0 0 0 1 1 1];
70 elseif abs(sweep) <= pi
71   narcs = 2;
72   knots = [0 0 0 0.5 0.5 1 1 1];
73 elseif abs(sweep) <= 3*pi/2
74   narcs = 3;
75   knots = [0 0 0 1/3 1/3 2/3 2/3 1 1 1];
76 else
77   narcs = 4;
78   knots = [0 0 0 0.25 0.25 0.5 0.5 0.75 0.75 1 1 1];
79 end
80
81 dsweep = sweep/(2*narcs);     % arc segment sweep angle/2
82
83 % determine middle control point and weight
84 wm = cos(dsweep);
85 x  = radius*wm;
86 y  = radius*sin(dsweep);
87 xm = x+y*tan(dsweep);
88
89 % arc segment control points
90 ctrlpt = [ x wm*xm x;    % w*x - coordinate
91           -y 0     y;    % w*y - coordinate
92            0 0     0;    % w*z - coordinate
93            1 wm    1];   % w   - coordinate
94       
95 % build up complete arc from rotated segments
96 coefs = zeros(4,2*narcs+1);   % nurbs control points of arc
97 xx = vecrotz(sang + dsweep);
98 coefs(:,1:3) = xx*ctrlpt;     % rotate to start angle
99 xx = vecrotz(2*dsweep);
100 for n = 2:narcs
101    m = 2*n+[0 1];
102    coefs(:,m) = xx*coefs(:,m-2);
103 end
104
105 % vectrans arc if necessary
106 if ~isempty(center) 
107   xx = vectrans(center);
108   coefs = xx*coefs;
109 end
110
111 curve = nrbmak(coefs,knots);
112
113 end
114
115 %!demo
116 %! for r = 1:9
117 %! crv = nrbcirc(r,[],deg2rad(45),deg2rad(315));
118 %!   nrbplot(crv,50);
119 %!   hold on;
120 %! end
121 %! hold off;
122 %! axis equal;
123 %! title('NURBS construction of several 2D arcs.');