[Index for tmp_for_tar/vrml.doc] [Return to Master Index]

vmesh

(tmp_for_tar/vrml.doc/vmesh.m)


Function Synopsis

s = vmesh (x, y, z, ...)

Help text

 s = vmesh (x, y, z [, options] ) - Visualize a 3D surface
 s = vmesh (z [, options] )

 Visualizes a 3D surface. Returns the VRML code.

 x : RxC or C  : X coordinates of the points on the surface
 y : RxC or R  : Y "                                      "
 z : RxC       : Z "                                      "

 s :   string  : The code

 If x and y are omitted, they are assumed to be linspace(-1,1,C or R).
 Points presenting one or more 'inf' or 'nan' coordinates are ignored.

 Options :

 "col" , col  : 3   : Color,                      default = [0.3,0.4,0.9]
             or 3xP : color of vertices (vrml colorPerVertex is TRUE).

 "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
 "smooth"           : same as "creaseAngle",pi.

 "tex", img   : string : image file name 
                matrix : greylevel image 
                struct : an image structure as returned by ims_load

 Last modified: December 2000



Cross-Reference Information

This function calls

Listing of function file tmp_for_tar/vrml.doc/vmesh.m

## s = vmesh (x, y, z [, options] ) - Visualize a 3D surface
## s = vmesh (z [, options] )
##
## Visualizes a 3D surface. Returns the VRML code.
##
## x : RxC or C  : X coordinates of the points on the surface
## y : RxC or R  : Y "                                      "
## z : RxC       : Z "                                      "
##
## s :   string  : The code
##
## If x and y are omitted, they are assumed to be linspace(-1,1,C or R).
## Points presenting one or more 'inf' or 'nan' coordinates are ignored.
##
## Options :
##
## "col" , col  : 3   : Color,                      default = [0.3,0.4,0.9]
##             or 3xP : color of vertices (vrml colorPerVertex is TRUE).
## 
## "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
## "smooth"           : same as "creaseAngle",pi.
##
## "tex", img   : string : image file name 
##                matrix : greylevel image 
##                struct : an image structure as returned by ims_load

## Author:        Etienne Grossmann  <etienne@isr.ist.utl.pt>
## Last modified: December 2000

function s = vmesh (x, y, z, ...)

				# Default values
tran = 0 ;
col = [0.3, 0.4, 0.9] ;
tcoord = tex = smooth = creaseAngle = nan ;
frame = 0;

if (nargin <= 1) || isstr(y),	# Cruft to allow not passing x and y
  zz = x ;
  [R,C] = size (zz);
  [xx,yy] = meshgrid (linspace (-1,1,C), linspace (-1,1,R)); 
  ## ones(R,1)*[1:C] ;
  ## yy = ## [1:R]'*ones(1,C) ;
  if     nargin >=3,
    s = vmesh ( xx, yy, zz, y, z, all_va_args );
    return
  elseif nargin >=2,
    s = vmesh ( xx, yy, zz, y, all_va_args );
    return
  end
  x = xx ; y = yy ; z = zz ;
end

				# Read options
filename = "vmesh" ;
verbose = 0 ;

if nargin > 3,

  opt1 = " tran col creaseAngle tex " ;
  opt0 = " smooth " ;

  nargin -= 3 ;

  read_options ;
end

if ! isnan (smooth), creaseAngle = pi ; end


[R,C] = size(z);
P = R*C;
if any (size (x) == 1), x = ones(R,1)*x(:)' ; end
if any (size (y) == 1), y = y(:)*ones(1,C)  ; end

pts = [x(:)';y(:)';z(:)'];

keepp = all (!isnan(pts) & finite(pts)) ;

trgs = zeros(3,2*(R-1)*(C-1)) ;

tmp = 1:(R-1)*(C-1);

## Points are numbered as
##
## 1  R+1 .. (C-1)*R+1
## 2  R+2         :
## :   :          :
## R  2*R ..     C*R
##

## Triangles are numbered as :
## 
## X = (R-1)*(C-1)
## _______________________________
## |    /|    /|    /|    /|-R+1/|
## | 1 / |R+1/ |   / |   / |R*C/ |
## |  /  |  /  |  /  |  /  |  /  |
## | /X+1| /X+R| /   | /   | /   |
## |/    |/    |/    |/    |/    |
## -------------------------------
## |    /|    /|    /|    /|    /|
## | 2 / |R+2/ |   / |   / |   / |
## |  /  |  /  |  /  |  /  |  /  |
## | /   | /   | /   | /   | /   |
## |/    |/    |/    |/    |/    |
## -------------------------------
##    :           :           :
##    :           :           :
## -------------------------------
## |    /|    /|    /|    /|    /|
## | R / |2*R/ |   / |   / |C*R/ |
## |  /  |  /  |  /  |  /  |  /  |
## | /X+R| /   | /   | /   | /C*R|
## |/    |/    |/    |/    |/ X+ |
## -------------------------------

## (x,y), (x,y+1), (x+1,y)  i.e. n, n+1, n+R

trgs(1,tmp) = ([1:R-1]'*ones(1,C-1) + R*ones(R-1,1)*[0:C-2])(:)';
trgs(2,tmp) = ([ 2:R ]'*ones(1,C-1) + R*ones(R-1,1)*[0:C-2])(:)';
trgs(3,tmp) = ([1:R-1]'*ones(1,C-1) + R*ones(R-1,1)*[1:C-1])(:)';

tmp += (R-1)*(C-1);

trgs(1,tmp) = ([1:R-1]'*ones(1,C-1) + R*ones(R-1,1)*[1:C-1])(:)';
trgs(2,tmp) = ([ 2:R ]'*ones(1,C-1) + R*ones(R-1,1)*[0:C-2])(:)';
trgs(3,tmp) = ([ 2:R ]'*ones(1,C-1) + R*ones(R-1,1)*[1:C-1])(:)';


if ! all(keepp),

				# Try to toggle some triangles to fill in
				# holes
  nt = (R-1)*(C-1) ;
  tmp = ! reshape(keepp(trgs),3,2*nt);
  tmp = all( tmp == kron([0,0;0,1;1,0],ones(1,nt)) );
  trgs(3,     find (    tmp(1:nt)   & rem (trgs(3,1:nt),R)) )++ ;
  trgs(2, nt+ find ( tmp(nt+1:2*nt) & (rem (trgs(3,nt+1:2*nt),R)!=1)) )-- ;

				# Remove whatever can't be kept
  keept = all (reshape(keepp(trgs),3,2*(R-1)*(C-1)));
  ## keept = all (keepp(trgs)) ;
  keepip = find (keepp);	# Ind. of Kept points
  keepit = find (keept);	# Ind. of Kept triangles
  renum = cumsum (keepp);

  pts = pts (:,keepip) ;	# Kept points
  Q = columns (keepit);		# Number of triangles
				# Kept triangles
  trgs = reshape(renum (trg0 = trgs (:,keepit)), 3, Q);
else 
  trg0 = trgs;
  Q = 2*(R-1)*(C-1);		# Number of triangles
  ## renum = keepip = 1:P;		# All points are kept
end

				# Received a matrix for texture
if isnumeric (tex) && !isnan (tex), 
  img = tex;
  tex = "/tmp/octave_vrml_texture.pgm";
  ims_save(mat2ims (img), tex);
elseif is_struct (tex),		# assume image structure
  img = tex;
  tex = "/tmp/octave_vrml_texture";
  if img.rgb, tex = [tex,".ppm"]; else tex = [tex,".pgm"]; end
  ims_save (img,tex);
end

if isstr (tex),
  if ! strcmp (tex(1), "/"), tex = [pwd(),"/", tex]; end
  tcoord = zeros(6,Q);
  [jj,ii] = sub2ind ([R,C], 1:P);
  ii = (ii-1)/(C-1);
  jj = (jj-1)/(R-1);
  ## keyboard
  tcoord(1,:) = ii(trg0(1,:));
  tcoord(2,:) = jj(trg0(1,:));
  tcoord(3,:) = ii(trg0(2,:));
  tcoord(4,:) = jj(trg0(2,:));
  tcoord(5,:) = ii(trg0(3,:));
  tcoord(6,:) = jj(trg0(3,:));
end

s = vrml_faces (pts, trgs, "col", col, "creaseAngle", creaseAngle,\
		"tran", tran,\
		"tex",tex,"tcoord",tcoord);

## Addd a point light
scl = sqrt (msq (pts-mean (pts')'*ones(1,columns (pts))));
lpos = [min (x(:)) - 1.1*scl* max (max(x(:))-min(x(:)), 1),
	mean(y(:)),
	max(z(:))];

pl = vrml_PointLight ("location", lpos);
vp = vrml_Viewpoint  ("orientation", [1 0 0 -pi/6]);

if frame, fr = vrml_frame (min (pts'), "col",eye(3)) ;
else      fr = "";
end

s = [s,pl,fr];

vrml_browse (s);

## R=5; C=11;
## x = ones(R,1)*[1:C]; y = [1:R]'*ones(1,C);
## zz = z = sin(x)+(2*y/R-1).^2 ;
## ## Punch some holes
## holes = ind2sub ([R,C],[3,3,3,1,2,3,2,3;1,2,3,5,7,7,9,10]')
## z(holes) = nan
## save_vrml("tmp.wrl",vmesh(x,y,z+1))
## save_vrml("tmp.wrl",vmesh(z,"col",[0.5,0,0],"tran",0.5),vmesh(z+1))


Produced by oct2html on Sat Dec 2 19:08:39 2000
Cross-Directory links are: ON