1 ## Copyright (C) 2002-2012 Etienne Grossmann <etienne@egdn.net>
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
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
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/>.
16 ## s = vrml_faces(x,f,...) - VRML facet object (IndexedFaceSet node)
18 ## x : 3xP : The 3D points
19 ## f : 3xQ : The indexes of the points forming the faces. Indexes
20 ## should have values in 1:P.
22 ## Returns a Shape -> IndexedFaceSet vrml node.
24 ## No check is done on anything
28 ## "col" , col : 3 : Color, default = [0.3,0.4,0.9]
29 ## or 3xP : Color of vertices
30 ## or 3xQ : Color of facets (use "colorPerVertex" below to
31 ## disambiguate the case P==Q).
33 ## "emit", em : 3 : Emissive color of the surface
34 ## : 3XP : (same as color)
36 ## : 1 : Use color as emissive color too default = 0
38 ## "tran", tran : 1x1 : Transparency, default = 0
41 ## : 1 : vrml creaseAngle value. The browser may smoothe the
42 ## crease between facets whose angle is less than a.
45 ## : string : Name of file containing texture. default : none
47 ## "imsz", sz : 2 : Size of texture image
48 ## default is determined by imginfo()
51 ## : 2x3Q : Coordinates of vertices in texture image. Each 2x3
52 ## block contains coords of one facet's corners. The
53 ## coordinates should be in [0,1], as in a VRML
54 ## TextureCoordinate node.
55 ## default assumes faces are returned
58 ## "smooth" : same as "creaseAngle",pi.
60 ## "colorPerVertex", c: If 1, col specifies color of vertices. If 0,
61 ## col specifies color of facets. Default = 1
63 ## "DEFcoord",n : string : DEF the coord VRML node with name n. Default = ''
64 ## "DEFcol", n : string : DEF the color VRML node with name n. Default = ''
66 ## See also: vrml_surf(), vmesh(), test_vrml_faces()
68 function s = vrml_faces (x,f,varargin)
70 ## mytic; starttime = cputime();
74 error ("x is %i x %i, has neither 3 rows nor 3 columns.", size (x));
81 col = [0.3, 0.4, 0.9] ;
83 tcoord = imsz = tex = smooth = creaseAngle = nan ;
85 DEFcol = DEFcoord = "";
87 ## Read options ######################################################
88 opt1 = " tex DEFcoord DEFcol imsz tcoord tran col creaseAngle colorPerVertex emit " ;
89 opt0 = " smooth convex " ;
95 while numel(varargin)>=i
99 error ("vrml_faces : Non-string option : \n") ;
103 if index(opt1,[" ",tmp," "]) ,
105 tmp2 = varargin{i++} ;
107 eval([tmp,"=tmp2;"]) ;
109 if verbose , printf ("vrml_faces : Read option : %s.\n",tmp); end
111 elseif index(opt0,[" ",tmp," "]) ,
114 if verbose , printf ("vrml_faces : Read boolean option : %s\n",tmp); end
117 error ("vrml_faces : Unknown option : %s\n",tmp) ;
121 ## printf (" Options : %f\n",mytic()); ## Just for measuring time
122 ## End of reading options ############################################
124 if !isempty (DEFcol), col_def_str = ["DEF ",DEFcol];
125 else col_def_str = "";
129 if ! isnan (smooth), creaseAngle = pi ; end
131 ## printf ("creaseAngle = %8.3f\n",creaseAngle);
133 nfaces = columns (f);
136 error ("Faces matrix 'f' has %i < 3 rows, so it does not define faces",
139 if any (f > columns (x))
140 error ("Faces matrix 'f' has value %i greater than number of points %i",
141 max (f(:)), columns (x));
147 col_str_1 = sprintf ([" appearance Appearance {\n",...
148 " texture ImageTexture {\n",...
154 texcoord_point_str = sprintf (" %8.5f %8.5f\n", tcoord);
156 col_str_2 = sprintf ([" texCoord TextureCoordinate {\n",\
163 # If texture has been provided
164 elseif ischar (tex), # Assume triangles
166 ## printf ("Using texture\n");
168 col_str_1 = sprintf ([" appearance Appearance {\n",...
169 " texture ImageTexture {\n",...
175 # Eventually determine size of image
176 if isnan(imsz), imsz = imginfo (tex); end
180 nb = ceil (nfaces/2); # n of blocks
181 lo = [0:nb-1]/nb; hi = [1:nb]/nb;
182 on = ones (1,nb); ze = zeros (1,nb);
184 sm = (1/nb) /(imsz(2)+1);
185 tcoord = [lo; on; lo; ze; hi-sm; ze; lo+sm; on; hi-sm; on; hi-sm; ze];
186 tcoord = reshape (tcoord, 2, 6*nb);
187 tcoord = tcoord (:,1:3*nfaces);
190 col_str_2 = sprintf ([" texCoord TextureCoordinate {\n",\
193 " texCoordIndex [\n %s]\n",\
194 " coordIndex [\n %s]\n",\
196 sprintf ("%10.8f %10.8f,\n ",tcoord),\
197 sprintf ("%-4d, %-4d, %-4d, -1,\n ",0:3*nfaces-1),\
198 sprintf ("%-4d, %-4d, %-4d, -1,\n ",f-1)
201 ## TODO : f-1 abobe seems to not work if f is a cell or list (check
202 ## whether this is possible here)
204 elseif prod (size (col))==3, # One color has been specified for the whole
207 col_str_1 = [" appearance Appearance {\n",...
208 vrml_material(col, emit, tran,DEFcol),\
213 if (emit) # Color is emissive by default
217 else # If there's a material node, it is not
219 if tran, ts = sprintf ("transparency %8.3f",tran);
222 col_str_1 = ["appearance Appearance {\n",\
223 " material Material {",ts,"}\n}\n"];
225 if isnan (colorPerVertex)
226 if prod (size (col)) == 3*columns (x), colorPerVertex = 1;
227 elseif prod (size (col)) == 3*columns (f), colorPerVertex = 0;
230 if colorPerVertex, cPVs = "TRUE"; else cPVs = "FALSE"; end
232 col_str_2 = sprintf ([" colorPerVertex %s\n",\
233 " color %s Color {\n",\
239 sprintf(" %8.3f %8.3f %8.3f,\n",col));
242 ## printf (" Colors : %f\n",mytic()); ## Just for measuring time
245 if ! isnan (creaseAngle),
246 etc_str = [etc_str, sprintf(" creaseAngle %8.3f\n",creaseAngle)];
249 ## TODO : s/list/cell/g; Should put this code in sometime soon
250 ## Code below seems useless
252 if iscell (f), nfaces = length (f); else nfaces = columns (f); end
255 tpl0 = sprintf ("%%%dd, ",floor (log10 (max (1, columns (x))))+1);
256 ltpl0 = length (tpl0);
258 ptsface = zeros (1,nfaces);
260 # Determine total number of vertices, number
261 # of vertices per face and indexes of
262 # vertices of each face
265 for i = 1:length (f),
266 ptsface(i) = 1+length (f{i});
269 ii = [0, cumsum(ptsface)]';
270 all_indexes = -ones (1,npts);
271 for i = 1:length (f), all_indexes(ii(i)+1:ii(i+1)-1) = f{i} - 1; end
273 f = [f;-ones(1,columns(f))];
274 npts = sum (ptsface = (sum (!! f)));
275 all_indexes = nze (f) - 1;
276 all_indexes(find (all_indexes<0)) = -1;
278 ## printf (" Indexes : %f\n",mytic()); ## Just for measuring time
280 coord_str = sprintf (tpl0, all_indexes);
281 ## That's too slow coord_str = strrep (coord_str, "-1, ","-1,\n");
283 ## printf (" Faces : %f\n",mytic()); ## Just for measuring time
285 if ! convex, etc_str = [etc_str," convex FALSE\n"]; end
287 if !isempty (DEFcoord), coord_def_str = ["DEF ",DEFcoord];
288 else coord_def_str = "";
291 s = sprintf([... # string of indexed face set
294 " geometry IndexedFaceSet {\n",...
295 " solid FALSE # Show back of faces too\n",...
298 " coordIndex [\n%s]\n",...
299 " coord %s Coordinate {\n",...
300 " point [\n%s]\n",...
307 sprintf(" %8.3f %8.3f %8.3f,\n",x)) ;
308 ## printf (" Assembly : %f\n",mytic()); ## Just for measuring time
309 ## printf ("Total Time : %f\n",cputime() - starttime);
312 %! % Test the vrml_faces and vrml_browse functions with the test_vrml_faces script