]> Creatis software - CreaPhase.git/blob - octave_packages/nurbs-1.3.6/nrbplot.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nurbs-1.3.6 / nrbplot.m
1 function nrbplot (nurbs, subd, varargin)
2
3 % NRBPLOT: Plot a NURBS curve or surface, or the boundary of a NURBS volume.
4
5 % Calling Sequence:
6
7 %   nrbplot (nrb, subd)
8 %   nrbplot (nrb, subd, p, v)
9
10 % INPUT:
11
12 %   nrb         : NURBS curve, surface or volume, see nrbmak.
13
14 %   npnts       : Number of evaluation points, for a surface or volume, a row 
15 %       vector with the number of points along each direction.
16
17 %   [p,v]       : property/value options
18 %
19 %               Valid property/value pairs include:
20 %
21 %               Property        Value/{Default}
22 %               -----------------------------------
23 %               light           {off} | on
24 %               colormap        {'copper'}
25 %
26 % Example:
27 %
28 %   Plot the test surface with 20 points along the U direction
29 %   and 30 along the V direction
30 %
31 %   nrbplot(nrbtestsrf, [20 30])
32 %
33 %    Copyright (C) 2000 Mark Spink
34 %    Copyright (C) 2010 Carlo de Falco, Rafael Vazquez
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 nargs = nargin;
50 if nargs < 2
51   error ('Need a NURBS to plot and the number of subdivisions!');
52 elseif rem(nargs+2,2)
53   error ('Param value pairs expected')
54 end
55
56 % Default values
57 light='off';
58 cmap='summer';
59
60 % Recover Param/Value pairs from argument list
61 for i=1:2:nargs-2
62   Param = varargin{i};
63   Value = varargin{i+1};
64   if (~ischar (Param))
65     error ('Parameter must be a string')
66   elseif size(Param,1)~=1
67     error ('Parameter must be a non-empty single row string.')
68   end
69   switch lower (Param)
70   case 'light'
71     light = lower (Value);
72     if (~ischar (light))
73       error ('light must be a string.')
74     elseif ~(strcmp(light,'off') | strcmp(light,'on'))
75       error ('light must be off | on')
76     end
77   case 'colormap'
78     if ischar (Value)
79       cmap = lower(Value);
80     elseif size (Value, 2) ~= 3
81       error ('colormap must be a string or have exactly three columns.')
82     else
83       cmap=Value;
84     end
85   otherwise
86     error ('Unknown parameter: %s', Param)
87   end
88 end
89
90 colormap (cmap);
91
92 % convert the number of subdivisions in number of points
93 subd = subd+1;
94
95 % plot the curve or surface
96 if (iscell (nurbs.knots))
97  if (size (nurbs.knots,2) == 2) % plot a NURBS surface
98   p = nrbeval (nurbs, {linspace(0.0,1.0,subd(1)) linspace(0.0,1.0,subd(2))});
99   if (strcmp (light,'on'))
100     % light surface
101     surfl (squeeze(p(1,:,:)), squeeze(p(2,:,:)), squeeze(p(3,:,:)));
102     shading interp;
103   else 
104     surf (squeeze (p(1,:,:)), squeeze (p(2,:,:)), squeeze (p(3,:,:)));
105     shading faceted;
106   end
107  elseif (size (nurbs.knots,2) == 3) % plot the boundaries of a NURBS volume
108   hold_flag = ishold;
109   px = nrbeval (nurbs, {[0 1] linspace(0.0,1.0,subd(2)) linspace(0.0,1.0,subd(3))});
110   py = nrbeval (nurbs, {linspace(0.0,1.0,subd(1)) [0 1] linspace(0.0,1.0,subd(3))});
111   pz = nrbeval (nurbs, {linspace(0.0,1.0,subd(1)) linspace(0.0,1.0,subd(2)) [0 1]});
112   if (strcmp (light, 'on'))
113     surfl (squeeze (pz(1,:,:,1)), squeeze (pz(2,:,:,1)), squeeze (pz(3,:,:,1)));
114     hold on
115     surfl (squeeze (pz(1,:,:,2)), squeeze (pz(2,:,:,2)), squeeze (pz(3,:,:,2)));
116     surfl (squeeze (py(1,:,1,:)), squeeze (py(2,:,1,:)), squeeze (py(3,:,1,:)));
117     surfl (squeeze (py(1,:,2,:)), squeeze (py(2,:,2,:)), squeeze (py(3,:,2,:)));
118     surfl (squeeze (px(1,1,:,:)), squeeze (px(2,1,:,:)), squeeze (px(3,1,:,:)));
119     surfl (squeeze (px(1,2,:,:)), squeeze (px(2,2,:,:)), squeeze (px(3,2,:,:)));
120     shading interp;
121   else
122     surf (squeeze (pz(1,:,:,1)), squeeze (pz(2,:,:,1)), squeeze (pz(3,:,:,1)));
123     hold on
124     surf (squeeze (pz(1,:,:,2)), squeeze (pz(2,:,:,2)), squeeze (pz(3,:,:,2)));
125     surf (squeeze (py(1,:,1,:)), squeeze (py(2,:,1,:)), squeeze (py(3,:,1,:)));
126     surf (squeeze (py(1,:,2,:)), squeeze (py(2,:,2,:)), squeeze (py(3,:,2,:)));
127     surf (squeeze (px(1,1,:,:)), squeeze (px(2,1,:,:)), squeeze (px(3,1,:,:)));
128     surf (squeeze (px(1,2,:,:)), squeeze (px(2,2,:,:)), squeeze (px(3,2,:,:)));
129     shading faceted;
130   end
131   
132   if (~hold_flag)
133     hold off
134   end
135  
136  else
137   error ('nrbplot: some argument is not correct')
138  end
139 else
140   % plot a NURBS curve
141   p = nrbeval (nurbs, linspace (0.0, 1.0, subd));
142
143   if (any (nurbs.coefs(3,:)))
144     % 3D curve
145     plot3 (p(1,:), p(2,:), p(3,:)); 
146     grid on;
147   else
148     % 2D curve
149     plot (p(1,:), p(2,:));
150   end
151 end
152 axis equal;
153
154 end
155
156 % plot the control surface
157 % hold on;
158 % mesh(squeeze(pnts(1,:,:)),squeeze(pnts(2,:,:)),squeeze(pnts(3,:,:)));
159 % hold off;
160
161 %!demo
162 %! crv = nrbtestcrv;
163 %! nrbplot(crv,100)
164 %! title('Test curve')
165 %! hold off
166
167 %!demo
168 %! coefs = [0.0 7.5 15.0 25.0 35.0 30.0 27.5 30.0;
169 %!          0.0 2.5  0.0 -5.0  5.0 15.0 22.5 30.0];
170 %! knots = [0.0 0.0 0.0 1/6 1/3 1/2 2/3 5/6 1.0 1.0 1.0];
171 %!
172 %! geom = [
173 %! nrbmak(coefs,knots)
174 %! nrbline([30.0 30.0],[20.0 30.0])
175 %! nrbline([20.0 30.0],[20.0 20.0])
176 %! nrbcirc(10.0,[10.0 20.0],1.5*pi,0.0)
177 %! nrbline([10.0 10.0],[0.0 10.0])
178 %! nrbline([0.0 10.0],[0.0 0.0])
179 %! nrbcirc(5.0,[22.5 7.5])
180 %! ];
181 %!
182 %! ng = length(geom);
183 %! for i = 1:ng
184 %!   nrbplot(geom(i),500);
185 %!   hold on;
186 %! end
187 %! hold off;
188 %! axis equal;
189 %! title('2D Geometry formed by a series of NURBS curves');
190
191 %!demo
192 %! sphere = nrbrevolve(nrbcirc(1,[],0.0,pi),[0.0 0.0 0.0],[1.0 0.0 0.0]);
193 %! nrbplot(sphere,[40 40],'light','on');
194 %! title('Ball and torus - surface construction by revolution');
195 %! hold on;
196 %! torus = nrbrevolve(nrbcirc(0.2,[0.9 1.0]),[0.0 0.0 0.0],[1.0 0.0 0.0]);
197 %! nrbplot(torus,[40 40],'light','on');
198 %! hold off
199
200 %!demo
201 %! knots = {[0 0 0 1/2 1 1 1] [0 0 0 1 1 1]...
202 %!          [0 0 0 1/6 2/6 1/2 1/2 4/6 5/6 1 1 1]};
203 %!
204 %! coefs = [-1.0000   -0.9734   -0.7071    1.4290    1.0000    3.4172
205 %!          0    2.4172         0    0.0148   -2.0000   -1.9734
206 %!          0    2.0000    4.9623    9.4508    4.0000    2.0000
207 %!     1.0000    1.0000    0.7071    1.0000    1.0000    1.0000
208 %!    -0.8536         0   -0.6036    1.9571    1.2071    3.5000
209 %!     0.3536    2.5000    0.2500    0.5429   -1.7071   -1.0000
210 %!          0    2.0000    4.4900    8.5444    3.4142    2.0000
211 %!     0.8536    1.0000    0.6036    1.0000    0.8536    1.0000
212 %!    -0.3536   -4.0000   -0.2500   -1.2929    1.7071    1.0000
213 %!     0.8536         0    0.6036   -2.7071   -1.2071   -5.0000
214 %!          0    2.0000    4.4900   10.0711    3.4142    2.0000
215 %!     0.8536    1.0000    0.6036    1.0000    0.8536    1.0000
216 %!          0   -4.0000         0    0.7071    2.0000    5.0000
217 %!     1.0000    4.0000    0.7071   -0.7071   -1.0000   -5.0000
218 %!          0    2.0000    4.9623   14.4142    4.0000    2.0000
219 %!     1.0000    1.0000    0.7071    1.0000    1.0000    1.0000
220 %!    -2.5000   -4.0000   -1.7678    0.7071    1.0000    5.0000
221 %!          0    4.0000         0   -0.7071   -3.5000   -5.0000
222 %!          0    2.0000    6.0418   14.4142    4.0000    2.0000
223 %!     1.0000    1.0000    0.7071    1.0000    1.0000    1.0000
224 %!    -2.4379         0   -1.7238    2.7071    1.9527    5.0000
225 %!     0.9527    4.0000    0.6737    1.2929   -3.4379   -1.0000
226 %!          0    2.0000    6.6827   10.0711    4.0000    2.0000
227 %!     1.0000    1.0000    0.7071    1.0000    1.0000    1.0000
228 %!    -0.9734   -1.0000   -0.6883    0.7071    3.4172    1.0000
229 %!     2.4172         0    1.7092   -1.4142   -1.9734   -2.0000
230 %!          0    4.0000    6.6827    4.9623    4.0000         0
231 %!     1.0000    1.0000    0.7071    0.7071    1.0000    1.0000
232 %!          0   -0.8536         0    0.8536    3.5000    1.2071
233 %!     2.5000    0.3536    1.7678   -1.2071   -1.0000   -1.7071
234 %!          0    3.4142    6.0418    4.4900    4.0000         0
235 %!     1.0000    0.8536    0.7071    0.6036    1.0000    0.8536
236 %!    -4.0000   -0.3536   -2.8284    1.2071    1.0000    1.7071
237 %!          0    0.8536         0   -0.8536   -5.0000   -1.2071
238 %!          0    3.4142    7.1213    4.4900    4.0000         0
239 %!     1.0000    0.8536    0.7071    0.6036    1.0000    0.8536
240 %!    -4.0000         0   -2.8284    1.4142    5.0000    2.0000
241 %!     4.0000    1.0000    2.8284   -0.7071   -5.0000   -1.0000
242 %!          0    4.0000   10.1924    4.9623    4.0000         0
243 %!     1.0000    1.0000    0.7071    0.7071    1.0000    1.0000
244 %!    -4.0000   -2.5000   -2.8284    0.7071    5.0000    1.0000
245 %!     4.0000         0    2.8284   -2.4749   -5.0000   -3.5000
246 %!          0    4.0000   10.1924    6.0418    4.0000         0
247 %!     1.0000    1.0000    0.7071    0.7071    1.0000    1.0000
248 %!          0   -2.4379         0    1.3808    5.0000    1.9527
249 %!     4.0000    0.9527    2.8284   -2.4309   -1.0000   -3.4379
250 %!          0    4.0000    7.1213    6.6827    4.0000         0
251 %!     1.0000    1.0000    0.7071    0.7071    1.0000    1.0000
252 %!    -1.0000   -0.9734    0.2071    2.4163    1.0000    3.4172
253 %!          0    2.4172   -1.2071   -1.3954   -2.0000   -1.9734
254 %!     2.0000    4.0000    7.0178    6.6827    2.0000         0
255 %!     1.0000    1.0000    1.0000    0.7071    1.0000    1.0000
256 %!    -0.8536         0    0.3536    2.4749    1.2071    3.5000
257 %!     0.3536    2.5000   -0.8536   -0.7071   -1.7071   -1.0000
258 %!     1.7071    4.0000    6.3498    6.0418    1.7071         0
259 %!     0.8536    1.0000    0.8536    0.7071    0.8536    1.0000
260 %!    -0.3536   -4.0000    0.8536    0.7071    1.7071    1.0000
261 %!     0.8536         0   -0.3536   -3.5355   -1.2071   -5.0000
262 %!     1.7071    4.0000    6.3498    7.1213    1.7071         0
263 %!     0.8536    1.0000    0.8536    0.7071    0.8536    1.0000
264 %!          0   -4.0000    1.2071    3.5355    2.0000    5.0000
265 %!     1.0000    4.0000   -0.2071   -3.5355   -1.0000   -5.0000
266 %!     2.0000    4.0000    7.0178   10.1924    2.0000         0
267 %!     1.0000    1.0000    1.0000    0.7071    1.0000    1.0000
268 %!    -2.5000   -4.0000   -0.5429    3.5355    1.0000    5.0000
269 %!          0    4.0000   -1.9571   -3.5355   -3.5000   -5.0000
270 %!     2.0000    4.0000    8.5444   10.1924    2.0000         0
271 %!     1.0000    1.0000    1.0000    0.7071    1.0000    1.0000
272 %!    -2.4379         0   -0.0355    3.5355    1.9527    5.0000
273 %!     0.9527    4.0000   -1.4497   -0.7071   -3.4379   -1.0000
274 %!     2.0000    4.0000    9.4508    7.1213    2.0000         0
275 %!     1.0000    1.0000    1.0000    0.7071    1.0000    1.0000];
276 %! coefs = reshape (coefs, 4, 4, 3, 9);
277 %! horseshoe = nrbmak (coefs, knots);
278 %! nrbplot (horseshoe, [6, 6, 50], 'light', 'on');