]> Creatis software - CreaPhase.git/blob - octave_packages/vrml-1.0.13/vrml_thick_surf.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / vrml-1.0.13 / vrml_thick_surf.m
1 ## Copyright (C) 2002 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 = vrml_thick_surf (x, y, z [, options] )
17 ##       s = vrml_thick_surf (z [, options] )
18 ##
19 ## Returns vrml97 code for a Shape -> IndexedFaceSet node representing a
20 ## surface passing through the given points.
21 ##
22 ## The surface may look smoother than that returned by  vrml_surf,  but it
23 ## has twice as many facets.
24 ##
25 ## x : RxC or C  : X coordinates of the points on the surface
26 ## y : RxC or R  : Y "                                      "
27 ## z : RxC       : Z "                                      "
28 ##
29 ## s :   string  : The code
30 ##
31 ## If x and y are omitted, they are assumed to be 1:C and 1:R, resp
32 ## Points presenting one or more 'inf' or 'nan' coordinates are ignored.
33 ##
34 ## Options :
35 ##
36 ## "col" , col  : 3   : Color,                      default = [0.3,0.4,0.9]
37 ##             or 3xP : color of vertices (vrml colorPerVertex is TRUE).
38 ## 
39 ## "tran", tran : 1x1 : Transparency,                           default = 0
40 ##
41 ## "creaseAngle", a 
42 ##              :  1  : vrml creaseAngle value. The browser may smoothe the
43 ##                      crease between facets whose angle is less than a.
44 ##                                                              default = 0
45 ## "smooth"           : same as "creaseAngle",pi.
46
47 function s = vrml_thick_surf (x, y, z, varargin)
48
49                                 # Default values
50 tran = 0 ;
51 col = [0.3, 0.4, 0.9] ;
52 smooth = creaseAngle = nan ;
53 nargin = nargin();
54
55 if (nargin <= 1) || ischar(y),  # Cruft to allow not passing x and y
56   zz = x ;
57   [R,C] = size (zz);
58   xx = ones(R,1)*[1:C] ;
59   yy = [1:R]'*ones(1,C) ;
60   if     nargin >=3,
61
62     s = vrml_surf ( xx, yy, zz, y, z, varargin{:} );
63     return
64   elseif nargin >=2,
65
66     s = vrml_surf ( xx, yy, zz, y, varargin{:} );
67     return
68   end
69   x = xx ; y = yy ; z = zz ;
70 end
71
72                                 # Read options
73 filename = "vrml_thick_surf" ;
74 verbose = 0 ;
75
76 if nargin > 3,
77
78   opt1 = " tran col creaseAngle " ;
79   opt0 = " smooth " ;
80
81   nargin -= 3 ;
82
83   read_options_old ;
84 end
85
86 if ! isnan (smooth), creaseAngle = pi ; end
87 [R,C] = size(z);
88 if any (size (x) == 1), x = ones(R,1)*x(:)' ; end
89 if any (size (y) == 1), y = y(:)*ones(1,C)  ; end
90
91 pts = [x(:)';y(:)';z(:)'];
92
93 keepp = all (!isnan(pts) & finite(pts)) ;
94
95 trgs = zeros(3,4*(R-1)*(C-1)) ;
96
97 tmp = 1:(R-1)*(C-1);
98
99 ## Points are numbered as
100 ##
101 ## 1  R+1 .. (C-1)*R+1
102 ## 2  R+2         :
103 ## :   :          :
104 ## R  2*R ..     C*R
105 ##
106
107 ## Triangles are numbered as :
108 ## 
109 ## X = (R-1)*(C-1)
110 ## _______________________________
111 ## |    /|    /|    /|    /|-R+1/|
112 ## | 1 / |R+1/ |   / |   / |R*C/ |
113 ## |  /  |  /  |  /  |  /  |  /  |
114 ## | /X+1| /X+R| /   | /   | /   |
115 ## |/    |/    |/    |/    |/    |
116 ## -------------------------------
117 ## |    /|    /|    /|    /|    /|
118 ## | 2 / |R+2/ |   / |   / |   / |
119 ## |  /  |  /  |  /  |  /  |  /  |
120 ## | /   | /   | /   | /   | /   |
121 ## |/    |/    |/    |/    |/    |
122 ## -------------------------------
123 ##    :           :           :
124 ##    :           :           :
125 ## -------------------------------
126 ## |    /|    /|    /|    /|    /|
127 ## | R / |2*R/ |   / |   / |C*R/ |
128 ## |  /  |  /  |  /  |  /  |  /  |
129 ## | /X+R| /   | /   | /   | /C*R|
130 ## |/    |/    |/    |/    |/ X+ |
131 ## -------------------------------
132
133 ## (x,y), (x,y+1), (x+1,y)  i.e. n, n+1, n+R
134
135 trgs(1,tmp) = ([1:R-1]'*ones(1,C-1) + R*ones(R-1,1)*[0:C-2])(:)';
136 trgs(2,tmp) = ([ 2:R ]'*ones(1,C-1) + R*ones(R-1,1)*[0:C-2])(:)';
137 trgs(3,tmp) = ([1:R-1]'*ones(1,C-1) + R*ones(R-1,1)*[1:C-1])(:)';
138
139 ## (x+1,y), (x,y+1), (x+1,y+1)  i.e. n+R, n+1, n+R+1
140 tmp += (R-1)*(C-1);
141
142 trgs(1,tmp) = ([1:R-1]'*ones(1,C-1) + R*ones(R-1,1)*[1:C-1])(:)';
143 trgs(2,tmp) = ([ 2:R ]'*ones(1,C-1) + R*ones(R-1,1)*[0:C-2])(:)';
144 trgs(3,tmp) = ([ 2:R ]'*ones(1,C-1) + R*ones(R-1,1)*[1:C-1])(:)';
145
146 ## (x,y), (x+1,y+1), (x+1,y)  i.e. n, n+1, n+R+1
147 tmp += (R-1)*(C-1);
148
149 trgs(1,tmp) = ([1:R-1]'*ones(1,C-1) + R*ones(R-1,1)*[0:C-2])(:)';
150 trgs(2,tmp) = ([2:R  ]'*ones(1,C-1) + R*ones(R-1,1)*[1:C-1])(:)';
151 trgs(3,tmp) = ([1:R-1]'*ones(1,C-1) + R*ones(R-1,1)*[1:C-1])(:)';
152
153 ## (x,y), (x,y+1), (x+1,y+1)  i.e. n, n+1, n+R+1
154 tmp += (R-1)*(C-1);
155
156 trgs(1,tmp) = ([1:R-1]'*ones(1,C-1) + R*ones(R-1,1)*[0:C-2])(:)';
157 trgs(2,tmp) = ([ 2:R ]'*ones(1,C-1) + R*ones(R-1,1)*[0:C-2])(:)';
158 trgs(3,tmp) = ([ 2:R ]'*ones(1,C-1) + R*ones(R-1,1)*[1:C-1])(:)';
159
160
161 if ! all(keepp),
162
163   keept = all (reshape(keepp(trgs),3,4*(R-1)*(C-1)));
164   ## keept = all (keepp(trgs)) ;
165   keepip = find (keepp);
166   keepit = find (keept);
167   renum = cumsum (keepp);
168
169   pts = pts (:,keepip) ;
170   trgs = reshape(renum (trgs (:,keepit)), 3, columns(keepit));
171
172 end
173 s = vrml_faces (pts, trgs, "col", col, "tran", tran, "creaseAngle", creaseAngle);
174
175 ## x=-1:0.1:1;y=(x=ones(columns(x),1)*x)';
176 ## r = sqrt (x.^2+y.^2); c = cos(2*pi*r.^2)./(2+r.^2);
177 ## Punch some holes
178 ## holes = ind2sub (size (c), [7,7,9,10,15,16;8,9,10,11,14,14]')
179 ## c(holes) = nan;
180 ##    vrml_browse ("tmp.wrl",vrml_thick_surf (x, y, c))
181 ## or save_vrml ("tmp.wrl", vrml_thick_surf (x, y, c)) 
182 endfunction
183