Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[c0a266]: inst / vrml_faces.m Maximize Restore History

Download this file

vrml_faces.m    316 lines (260 with data), 9.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
## Copyright (C) 2002-2012 Etienne Grossmann <etienne@egdn.net>
##
## This program is free software; you can redistribute it and/or modify it under
## the terms of the GNU General Public License as published by the Free Software
## Foundation; either version 3 of the License, or (at your option) any later
## version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
## details.
##
## You should have received a copy of the GNU General Public License along with
## this program; if not, see <http://www.gnu.org/licenses/>.
## s = vrml_faces(x,f,...) - VRML facet object (IndexedFaceSet node)
##
## x : 3xP : The 3D points
## f : 3xQ : The indexes of the points forming the faces. Indexes
## should have values in 1:P.
##
## Returns a Shape -> IndexedFaceSet vrml node.
##
## No check is done on anything
##
## Options :
##
## "col" , col : 3 : Color, default = [0.3,0.4,0.9]
## or 3xP : Color of vertices
## or 3xQ : Color of facets (use "colorPerVertex" below to
## disambiguate the case P==Q).
##
## "emit", em : 3 : Emissive color of the surface
## : 3XP : (same as color)
## : 3xQ :
## : 1 : Use color as emissive color too default = 0
##
## "tran", tran : 1x1 : Transparency, default = 0
##
## "creaseAngle", a
## : 1 : vrml creaseAngle value. The browser may smoothe the
## crease between facets whose angle is less than a.
## default = 0
## "tex", texfile
## : string : Name of file containing texture. default : none
##
## "imsz", sz : 2 : Size of texture image
## default is determined by imginfo()
##
## "tcoord", tcoord
## : 2x3Q : Coordinates of vertices in texture image. Each 2x3
## block contains coords of one facet's corners. The
## coordinates should be in [0,1], as in a VRML
## TextureCoordinate node.
## default assumes faces are returned
## by extex()
##
## "smooth" : same as "creaseAngle",pi.
## "convex"
## "colorPerVertex", c: If 1, col specifies color of vertices. If 0,
## col specifies color of facets. Default = 1
##
## "DEFcoord",n : string : DEF the coord VRML node with name n. Default = ''
## "DEFcol", n : string : DEF the color VRML node with name n. Default = ''
##
## See also: vrml_surf(), vmesh(), test_vrml_faces()
function s = vrml_faces (x,f,varargin)
## mytic; starttime = cputime();
if rows (x) != 3
if columns (x) != 3
error ("x is %i x %i, has neither 3 rows nor 3 columns.", size (x));
else
x = x';
end
end
tran = 0 ;
col = [0.3, 0.4, 0.9] ;
convex = emit = 0;
tcoord = imsz = tex = smooth = creaseAngle = nan ;
colorPerVertex = nan;
DEFcol = DEFcoord = "";
## Read options ######################################################
opt1 = " tex DEFcoord DEFcol imsz tcoord tran col creaseAngle colorPerVertex emit " ;
opt0 = " smooth convex " ;
verbose = 0 ;
i = 1;
while numel(varargin)>=i
tmp = varargin{i++};
if ! ischar(tmp) ,
error ("vrml_faces : Non-string option : \n") ;
## keyboard
end
if index(opt1,[" ",tmp," "]) ,
tmp2 = varargin{i++} ;
eval([tmp,"=tmp2;"]) ;
if verbose , printf ("vrml_faces : Read option : %s.\n",tmp); end
elseif index(opt0,[" ",tmp," "]) ,
eval([tmp,"=1;"]) ;
if verbose , printf ("vrml_faces : Read boolean option : %s\n",tmp); end
else
error ("vrml_faces : Unknown option : %s\n",tmp) ;
## keyboard
end
endwhile
## printf (" Options : %f\n",mytic()); ## Just for measuring time
## End of reading options ############################################
if !isempty (DEFcol), col_def_str = ["DEF ",DEFcol];
else col_def_str = "";
end
if ! isnan (smooth), creaseAngle = pi ; end
## printf ("creaseAngle = %8.3f\n",creaseAngle);
nfaces = columns (f);
if ismatrix(f)
if rows (f) < 3
error ("Faces matrix 'f' has %i < 3 rows, so it does not define faces",
rows (f));
end
if any (f > columns (x))
error ("Faces matrix 'f' has value %i greater than number of points %i",
max (f(:)), columns (x));
end
end
if ! isnan (tcoord)
col_str_1 = sprintf ([" appearance Appearance {\n",...
" texture ImageTexture {\n",...
" url \"%s\"\n",...
" }\n",...
" }\n"],...
tex);
texcoord_point_str = sprintf (" %8.5f %8.5f\n", tcoord);
col_str_2 = sprintf ([" texCoord TextureCoordinate {\n",...
" point [\n %s]\n",...
" }\n"...
],...
texcoord_point_str...
);
# If texture has been provided
elseif ischar (tex), # Assume triangles
## printf ("Using texture\n");
col_str_1 = sprintf ([" appearance Appearance {\n",...
" texture ImageTexture {\n",...
" url \"%s\"\n",...
" }\n",...
" }\n"],...
tex);
# Eventually determine size of image
if isnan(imsz), imsz = imginfo (tex); end
if isnan (tcoord),
nb = ceil (nfaces/2); # n of blocks
lo = [0:nb-1]/nb; hi = [1:nb]/nb;
on = ones (1,nb); ze = zeros (1,nb);
sm = (1/nb) /(imsz(2)+1);
tcoord = [lo; on; lo; ze; hi-sm; ze; lo+sm; on; hi-sm; on; hi-sm; ze];
tcoord = reshape (tcoord, 2, 6*nb);
tcoord = tcoord (:,1:3*nfaces);
end
col_str_2 = sprintf ([" texCoord TextureCoordinate {\n",...
" point [\n %s]\n",...
" }\n",...
" texCoordIndex [\n %s]\n",...
" coordIndex [\n %s]\n",...
],...
sprintf ("%10.8f %10.8f,\n ",tcoord),...
sprintf ("%-4d, %-4d, %-4d, -1,\n ",0:3*nfaces-1),...
sprintf ("%-4d, %-4d, %-4d, -1,\n ",f-1)
);
## TODO : f-1 abobe seems to not work if f is a cell or list (check
## whether this is possible here)
elseif prod (size (col))==3, # One color has been specified for the whole
# surface
col_str_1 = [" appearance Appearance {\n",...
vrml_material(col, emit, tran,DEFcol),...
" }\n"];
col_str_2 = "";
else
if (emit) # Color is emissive by default
col_str_1 = "";
else # If there's a material node, it is not
# emissive.
if tran, ts = sprintf ("transparency %8.3f",tran);
else ts = "";
end
col_str_1 = ["appearance Appearance {\n",...
" material Material {",ts,"}\n}\n"];
end
if isnan (colorPerVertex)
if prod (size (col)) == 3*columns (x), colorPerVertex = 1;
elseif prod (size (col)) == 3*columns (f), colorPerVertex = 0;
end
end
if colorPerVertex, cPVs = "TRUE"; else cPVs = "FALSE"; end
col_str_2 = sprintf ([" colorPerVertex %s\n",...
" color %s Color {\n",...
" color [\n%s\n",...
" ]\n",...
" }"],...
cPVs,...
col_def_str,...
sprintf(" %8.3f %8.3f %8.3f,\n",col));
end
## printf (" Colors : %f\n",mytic()); ## Just for measuring time
etc_str = "" ;
if ! isnan (creaseAngle),
etc_str = [etc_str, sprintf(" creaseAngle %8.3f\n",creaseAngle)];
end
## TODO : s/list/cell/g; Should put this code in sometime soon
## Code below seems useless
# Faces
if iscell (f), nfaces = length (f); else nfaces = columns (f); end
tpl0 = sprintf ("%%%dd, ",floor (log10 (max (1, columns (x))))+1);
ltpl0 = length (tpl0);
ptsface = zeros (1,nfaces);
# Determine total number of vertices, number
# of vertices per face and indexes of
# vertices of each face
if iscell (f)
npts = 0;
for i = 1:length (f),
ptsface(i) = 1+length (f{i});
npts += ptsface(i);
end
ii = [0, cumsum(ptsface)]';
all_indexes = -ones (1,npts);
for i = 1:length (f), all_indexes(ii(i)+1:ii(i+1)-1) = f{i} - 1; end
else
f = [f;-ones(1,columns(f))];
npts = sum (ptsface = (sum (!! f)));
all_indexes = nze (f) - 1;
all_indexes(find (all_indexes<0)) = -1;
end
## printf (" Indexes : %f\n",mytic()); ## Just for measuring time
coord_str = sprintf (tpl0, all_indexes);
## That's too slow coord_str = strrep (coord_str, "-1, ","-1,\n");
## printf (" Faces : %f\n",mytic()); ## Just for measuring time
if ! convex, etc_str = [etc_str," convex FALSE\n"]; end
if !isempty (DEFcoord), coord_def_str = ["DEF ",DEFcoord];
else coord_def_str = "";
end
s = sprintf([... # string of indexed face set
"Shape {\n",...
col_str_1,...
" geometry IndexedFaceSet {\n",...
" solid FALSE # Show back of faces too\n",...
col_str_2,...
etc_str,...
" coordIndex [\n%s]\n",...
" coord %s Coordinate {\n",...
" point [\n%s]\n",...
" }\n",...
" }\n",...
"}\n",...
],...
coord_str,...
coord_def_str,...
sprintf(" %8.3f %8.3f %8.3f,\n",x)) ;
## printf (" Assembly : %f\n",mytic()); ## Just for measuring time
## printf ("Total Time : %f\n",cputime() - starttime);
%!demo
%! % Test the vrml_faces and vrml_browse functions with the test_vrml_faces script
%! test_vrml_faces
endfunction