]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vmesh.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vmesh.m
1 ## Copyright (C) 2002-2009 Etienne Grossmann <etienne@egdn.net>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## s = vmesh (x, y, z [, options] ) - Visualize a 3D surface
17 ## s = vmesh (z [, options] )
18 ##
19 ## Visualizes a 3D surface. Returns the VRML code.
20 ##
21 ## x : RxC or C  : X coordinates of the points on the surface
22 ## y : RxC or R  : Y "                                      "
23 ## z : RxC       : Z "                                      "
24 ##
25 ## s :   string  : The code
26 ##
27 ## If x and y are omitted, they are assumed to be linspace(-1,1,C or R).
28 ## Points presenting one or more 'inf' or 'nan' coordinates are ignored.
29 ##
30 ## Options : (all options of vrml_surf may be used too)
31 ##
32 ## "col" , col  : 3      : RGB Color,                Default = [0.3,0.4,0.9]
33 ##             or 3x(R*C): Color of vertices (vrml colorPerVertex is TRUE).
34 ##             or 3x((R-1)*(C-1))
35 ##                       : Color of facets
36 ##             or 1      : Reflectivity (equivalent to [col,col,col] in RGB)
37 ##             or R x C  : Reflectivity of vertices
38 ##             or 1x(R*C)
39 ##             or (R-1)x(C-1)
40 ##             or (R-1)*(C-1)
41 ##                       : Reflectivity of facets.
42 ##
43 ##        RGB and reflectivity values should be in the [0,1] interval.
44 ##
45 ## "checker", c : 1x2 : Color as a checker. If c(1) is positive, checker has
46 ##                      c(1) rows. If it is negative, each checker row is
47 ##                      c(1) facets high. c(2) does the same for columns.
48 ##             or 1x1 : Same as [c,c].
49 ##
50 ## "zgray"            : Color varies from black for lowest point to white
51 ##                      for highest.
52 ##
53 ## "zrb"              : Color varies from blue for lowest point to red for
54 ##                      highest.
55 ##
56 ## "zcol", zcol : Mx3 : Color is linearly interpolated between the RGB
57 ##                      values specified by the rows of zcol.
58 ##
59 ## "steps"            : Represent surface as a piecewise constant Z = f(X,Y)
60 ##                      function
61 ##
62 ## "bars"             : Represent surface as a bar plot
63 ## "bwid"             : Bar width, relative to point separation. Default = 2/3
64 ##
65 ## "level", l   : 1xN : Display one or more horizontal translucent plane(s)
66 ##
67 ##                        z == l(i)   (1 <= i <= length(l))
68 ## 
69 ## "lcol", lc   : Nx3 : Color of the plane(s).          Default = [.7 .7 .7]
70 ## "ltran",lt   : Nx1 : Transparency of the plane(s).   Default =        0.3
71 ## "tex", texFile
72 ##
73 ## "normalize"  :       Normalize z to [-1,1]
74 ##
75 ## See also: vrml_surf(), vrml_faces(), demo("vmesh")
76
77 function s = vmesh (x, y, z, varargin)
78
79 level =     [];
80 lcol =      [7 7 7]/10;
81 ltran =     0.3;
82 normalize = 0;
83
84 if (nargin <= 1) || ischar(y),  # Cruft to allow not passing x and y
85   zz = x ;
86   [R,C] = size (zz);
87   [xx,yy] = meshgrid (linspace (-1,1,C), linspace (-1,1,R)); 
88   
89   if nargin >= 3
90     varargin = {y, z, varargin{:}};
91   elseif nargin >= 2
92     varargin = {y, varargin{:}};
93   end
94 ##  if     nargin >=3,
95 ##    s = vmesh ( xx, yy, zz, y, z, varargin{:} );
96 ##    if ! nargout,  clear s; end;  return
97 ##  elseif nargin >=2,
98 ##    s = vmesh ( xx, yy, zz, y, varargin{:} );
99 ##    if ! nargout,  clear s; end;  return
100 ##  end
101   x = xx ; y = yy ; z = zz ;
102 end
103
104 frame = 1;
105
106 ## surf_args = list (x,y,z);    # Arguments that'll be passed to vrml_surf
107 surf_args = {x,y,z};    # Arguments that'll be passed to vrml_surf
108
109 if numel (varargin)
110
111   op1 = [" tran col checker creaseAngle emit colorPerVertex tex zcol  frame ",\
112          " level lcol ltran bwid "];
113   op0 = " smooth zgray zrb normalize steps bars ";
114
115   df = tars (level, lcol, ltran, normalize, frame);
116
117   opts = read_options (varargin,"op0",op0,"op1",op1,"default",df);
118
119                                 # Identify options for vrml_surf()
120 #   all_surf_opts  = list ("tran", "col", "checker", "creaseAngle", "emit", \
121 #                        "colorPerVertex", "smooth", "tex",\
122 #                        "zgray","zrb","zcol");
123   all_surf_opts  = {"tran", "col", "checker", "creaseAngle", "emit", \
124                     "colorPerVertex", "smooth", "steps", "bars", "bwid", "tex",\
125                     "zgray","zrb","zcol"};
126
127   for i = 1:length(all_surf_opts)
128     optname = all_surf_opts{i};
129     if isfield (opts, optname)
130       ## surf_args = append (surf_args, list (optname));
131       surf_args{length(surf_args)+1} = optname;
132       if index (op1,[" ",optname," "])
133         ## surf_args = append (surf_args, list(opts.(optname)));
134         surf_args{length(surf_args)+1} = opts.(optname);
135       end
136     end
137   end
138   lcol  =     opts.lcol;
139   level =     opts.level;
140   ltran =     opts.ltran;
141   frame =     opts.frame;
142   normalize = opts.normalize;
143 end
144
145 if normalize
146   x -= nanmean (x(:));
147   y -= nanmean (y(:));
148   z -= nanmean (z(:));
149
150   datascl = nanmax (abs([z(:);y(:);x(:)]));
151
152   x /= datascl;
153   y /= datascl;
154   z /= datascl;
155                                 # Put back z in surf_args
156   surf_args{1} = x;
157   surf_args{2} = y;
158   surf_args{3} = z;
159 end
160
161 s = vrml_surf (surf_args{:});
162
163 if numel (x) == columns (z)
164   x = ones(rows(z),1) * x(:)';
165 else
166   assert (numel (x) == numel (z));
167 end
168 if numel (y) == rows (z)
169   y = y(:) * ones(1,columns(z));
170 else
171   assert (numel (y) == numel (z));
172 end
173
174 pts = [x(:)';y(:)';z(:)'];
175 ii = find (all (isfinite (pts)));
176 pt2 = pts(:,ii); x2 = x(ii); y2 = y(ii); z2 = z(ii);
177
178 ## Add a point light
179
180 # scl = max (max(pt2') - min(pt2'));
181
182 # lpos = [min(x2) - 0.5*scl, mean(y2), max(z2)+scl]
183 # pl1 = vrml_PointLight ("location", lpos, "intensity", 0.7);
184
185 # lpos = [mean(x2), min(y2) - 0.5*scl, max(z2)+scl]
186 # pl2 = vrml_PointLight ("location", lpos, "intensity", 0.7);
187
188 # pl = [pl1 pl2];
189
190 pl = [vrml_DirectionalLight("direction",[-1,-1,-1],"intensity",0.75),\
191       vrml_DirectionalLight("direction",[-1, 1,-1],"intensity",0.5),\
192       vrml_DirectionalLight("direction",[ 1,-1,-1],"intensity",0.5),\
193       vrml_DirectionalLight("direction",[ 1, 1,-1],"intensity",0.33),\
194       vrml_DirectionalLight("direction",[ 0, 0, 1],"intensity",0.5)];
195
196 #  distance = max ([max (x(:)) - min (x(:)),\
197 #                max (y(:)) - min (y(:)),\
198 #                max (z(:)) - min (z(:))])
199 #  vp = vrml_Viewpoint  ("orientation", [1 0 0 -pi/6],\
200 #                     "position",    distance*[0 0 5]);
201
202 minpts = min (pt2');
203 maxpts = max (pt2');
204 medpts = (minpts + maxpts)/2;
205 ptssz  = (maxpts - minpts);
206 ptssz  = max (ptssz, max (ptssz/10));
207
208 if frame, fr = vrml_frame (minpts-ptssz/10,\
209                            "scale", ptssz * 1.2, "col",(ones(3)+eye(3))/2);
210 else      fr = "";
211 end
212
213 sbg = vrml_Background ("skyColor", [0.5 0.5 0.6]);
214
215 slevel = "";
216 if ! isempty (level)
217   level = level(:)';            # Make a row
218   nlev = length (level);
219   
220   xmin = min (x(:)); xmax = max (x(:));
221   ymin = min (y(:)); ymax = max (y(:));
222
223   if any (size (lcol) != [nlev,3])
224     nlc = prod (szlc = size (lcol));
225                                 # Transpose colors
226     if all (szlc == [3,nlev]), lcol = lcol'; 
227                                 # Single gray level
228     elseif nlc == 1          , lcol = lcol * ones (nlev,3);
229                                 # nlev gray levels
230     elseif nlc == nlev       , lcol = lcol(:)*[1 1 1];
231     elseif nlc == 3          , lcol = ones(nlev,1)*lcol(:)';
232     else error ("lcol has size %i x %i",szlc);
233     end
234   end
235   if prod (size (ltran)) == 1    , ltran = ltran*ones(1,nlev); end
236   
237   for i = 1:nlev
238     slevel = [slevel, \
239               vrml_parallelogram([xmin     xmin     xmax     xmax;\
240                                   ymin     ymax     ymax     ymin;\
241                                   level(i) level(i) level(i) level(i)],\
242                                  "col",lcol(i,:),"tran",ltran(i))];
243   end
244 end
245
246 s = [pl, sbg, s , fr, slevel];
247
248
249
250 if ! nargout,  
251   vrml_browse (s);
252   clear s; 
253 end
254
255 %!demo
256 %! % Test the vmesh and vrml_browse functions with the test_vmesh script
257 %! R = 41; C = 26; 
258 %! [x,y] = meshgrid (linspace (-8+eps,8+eps,C), linspace (-8+eps,8+eps,R));
259 %! z = sin (sqrt (x.^2 + y.^2)) ./ (sqrt (x.^2 + y.^2));
260 %! vmesh (z);
261 %! printf ("Press a key.\n"); pause;
262 %! 
263 %! ############## The same surface, with holes (NaN's) in it. ###############
264 %! z(3,3) = nan;                # Bore a hole
265 %!                              # Bore more holes
266 %! z(1+floor(rand(1,5+R*C/30)*R*C)) = nan;
267 %! vmesh (z);
268 %! printf ("Press a key.\n"); pause;
269 %!
270 %! ###### The same surface, with checkered stripes - 'checker' option ######
271 %! vmesh (z,"checker",-[6,5]);
272 %! printf ("Press a key.\n"); pause;
273 %! 
274 %! ##### With z-dependent coloring - 'zrb', 'zgray' and'zcol' options. #####
275 %! vmesh (z,"zrb");
276 %! printf ("That's it!\n");
277
278
279
280
281 ## %! test_vmesh
282
283 return
284
285 endfunction
286
287