MATLAB课程:代码示例之Graphics and Visualization(三)
Visualizing Volume Data
This example shows several methods for visualizing volume data in MATLAB®.
Display Isosurface
An isosurface is a surface where all the points within a volume of space have a constant value. Use the isosurface function to generate the faces and vertices for the outside of the surface and the isocaps function to generate the faces and vertices for the end caps of the volume. Use the patch command to draw the volume and its end caps.
load mri D % load dataD = squeeze(D); % remove singleton dimensionlimits = [NaN NaN NaN NaN NaN 10];[x, y, z, D] = subvolume(D, limits); % extract a subset of the volume data[fo,vo] = isosurface(x,y,z,D,5); % isosurface for the outside of the volume[fe,ve,ce] = isocaps(x,y,z,D,5); % isocaps for the end caps of the volumefigurep1 = patch('Faces', fo, 'Vertices', vo); % draw the outside of the volumep1.FaceColor = 'red';p1.EdgeColor = 'none';p2 = patch('Faces', fe, 'Vertices', ve, ... % draw the end caps of the volume 'FaceVertexCData', ce);p2.FaceColor = 'interp';p2.EdgeColor = 'none';view(-40,24)daspect([1 1 0.3]) % set the axes aspect ratiocolormap(gray(100))box oncamlight(40,40) % create two lightscamlight(-20,-10)lighting gouraud
The coneplot command plots velocity vectors as cones at x, y, z points in a volume. The cones represent the magnitude and direction of the vector field at each point.
cla % clear the current axesload wind u v w x y z % load data[m,n,p] = size(u);[Cx, Cy, Cz] = meshgrid(1:4:m,1:4:n,1:4:p); % calculate the location of the conesh = coneplot(u,v,w,Cx,Cy,Cz,y,4); % draw the cone plotset(h,'EdgeColor', 'none')axis tight equalview(37,32)box oncolormap(hsv)light
The streamline function plots streamlines for a velocity vector at x, y, z points in a volume to illustrate the flow of a 3-D vector field.
cla[m,n,p] = size(u);[Sx, Sy, Sz] = meshgrid(1,1:5:n,1:5:p); % calculate the starting points of the streamlinesstreamline(u,v,w,Sx,Sy,Sz) % draw the streamlinesaxis tight equalview(37,32)box on
The streamtube function plots streamtubes for a velocity vector at x, y, z points in a volume. The width of the tube is proportional to the normalized divergence of the vector field at each point.
cla[m,n,p] = size(u);[Sx, Sy, Sz] = meshgrid(1,1:5:n,1:5:p); % calculate the starting points of the streamlinesh = streamtube(u,v,w,Sx,Sy,Sz); % draw the streamtubes and return an array of surfacesset(h, 'FaceColor', 'cyan') % use 'set' to change properties for an array of objectsset(h, 'EdgeColor', 'none')axis tight equalview(37,32)box onlight
Combine volume visualization in a single plot to get a more comprehensive picture of a velocity field within a volume.
claspd = sqrt(u.*u + v.*v + w.*w); % wind speed at each point in the volume[fo,vo] = isosurface(x,y,z,spd,40); % isosurface for the outside of the volume[fe,ve,ce] = isocaps(x,y,z,spd,40); % isocaps for the end caps of the volumep1 = patch('Faces', fo, 'Vertices', vo); % draw the isosurface for the volumep1.FaceColor = 'red';p1.EdgeColor = 'none';p2 = patch('Faces', fe, 'Vertices', ve, ... % draw the end caps of the volume 'FaceVertexCData', ce);p2.FaceColor = 'interp';p2.EdgeColor = 'none' ;[fc, vc] = isosurface(x, y, z, spd, 30); % isosurface for the cones[fc, vc] = reducepatch(fc, vc, 0.2); % reduce the number of faces and verticesh1 = coneplot(x,y,z,u,v,w,vc(:,1),vc(:,2),vc(:,3),3); % draw the coneploth1.FaceColor = 'cyan';h1.EdgeColor = 'none';[sx, sy, sz] = meshgrid(80, 20:10:50, 0:5:15); % starting points for streamlineh2 = streamline(x,y,z,u,v,w,sx,sy,sz); % draw the streamlinesset(h2, 'Color', [.4 1 .4])axis tight equalview(37,32)box onlight



雷达卡





京公网安备 11010802022788号







