Realization of 3D visualization in matlab

Realization and processing of three-dimensional graphics visualization in Matlab

In addition to the commonly used methods such as grid map, surface map and contour line, Matlab also provides some three-dimensional visualization functions for drawing more complex three-dimensional and vector objects.

These functions usually construct scalar and vector graphics in three-dimensional space:

Since the [b][color=red] constructed by these functions is a solid rather than a simple surface[/color][/b], they require a three-dimensional array as Parameters, where each dimension of the three-dimensional array represents a coordinate axis, and the points in the three-dimensional array define the coordinate axis grid and the coordinate points on the coordinate axis.

[b][color=blue][size=4] The so-called scalar graphic[/size][/color][/b] is a three-dimensional graphic determined by a mathematical expression, such as V=sqrt(X^2 +cos(Y)^2+Z^2)+Z, the image when V takes different values. Implicit 3D graphics like this cannot be drawn directly in Matlab. Interested netizens can look at the discussion of implicit function drawing in this post.

If the function to be drawn is a scalar function, the drawing function requires 4 three-dimensional arrays, of which 3 arrays each represent a coordinate axis, and the fourth array represents In addition to the scalar data at these locations, these arrays are usually denoted as X, Y, Z, and V.

[b][size=4][color=blue] The so-called vector graph[/color][/size][/b] means that while the graph satisfies a certain equation, it also has directionality, such as divergence. , Curl, gradient, etc.

If the function to be drawn is a vector function, 6 three-dimensional arrays are required, of which 3 each represents a coordinate axis, and the remaining 3 are used to represent the vector at the coordinate point , These arrays are usually denoted as X, Y, Z, U, V, and W.


To use the stereo and vector visualization functions provided by Matlab correctly and reasonably, we must understand some terms related to stereo and vector.

For example, divergence and curl are used to describe the vector process, while isosurfaces and isocaps are used to describe the stereoscopic visual appearance.

If we want to generate and process more complex three-dimensional objects, we need to refer to the corresponding literature for an in-depth understanding of these terms.



[b][color=blue] Since vector stereo visualization has a considerable professional background, such as fluid mechanics, aerodynamics, field theory and vector, etc., we will only introduce [color=Red] scalar 3D visualization[/ color], interested netizens can directly view the help file of Matlab[/color][/b]

In the following content, we mainly introduce the following content:

2# How to generate three-dimensional data
3# dimensional three-dimensional section diagram
4# three-dimensional, etc.值面
5# Three-dimensional data smoothing

 

How to generate three-dimensional data

We examine the properties of the following three-dimensional graphics
V=v=f(x,y,z)=(x+y+z)(xy+xz+yz)-10xyz

When V takes different values, it will correspond to different [b ][color=red]Surface graphics[/color][/b], but if we take V as a continuous value in an interval of [b][color=red][/color][/b], then We got a [b][color=red]three-dimensional solid[/color][/b]

which is composed of many curved surfaces [b][color=red]Three-dimensional solid[/color][/b] which is composed of many curved surfaces[b][color=red] /color][/b], because Matlab provides them with related direct functions,

but sometimes we are not interested in the whole solid, [b][color=red] is only interested in a certain surface[/color][/b ], the problem lies here. [b][color=red] in Matlab does not provide the direct function of [/color][/b] three-dimensional implicit function drawing, but we can obtain it indirectly, that is, cut out from the three-dimensional A surface we are interested in, [color=blue] This means that Matlab's visualization of three-dimensional surfaces (especially implicit) must be processed in three-dimensional first, and then intercept the ones we are interested in. [/color]

Let’s look at one An example of using the scalar function mentioned above to construct a three-dimensional figure. First, we must generate a coordinate system for constructing a solid object:
[code]x=0:0.5:10;
y=x;
z=x;
[X,Y,Z]=meshgrid(x,y,z);[ /code]
The above code demonstrates the application of the meshgrid function in three-dimensional space. Among them, XYZ is the three three-dimensional arrays defining the grid, which are formed by the expansion of xyz through the three-dimensional grid. In other words, X is to copy and expand x into a three-dimensional array with length(y) rows and length(z) pages; Y is to first transpose y into a column vector, and then copy and expand to have length(x) columns and The three-dimensional array of length(z) page; Z is to first convert z into a three-dimensional vector of 1×1×length(z), and then expand it into a three-dimensional array of length(y) rows and length(x) columns.

Next, we also need to define a scalar function V with a three-dimensional array as an independent variable:
[code]V=(X+Y+Z).*(X.*Y+X.*Z+Y.*Z)-10* X.*Y.*Z;[/code]


OK, [b][color=blue] This will get the data X, Y, Z and V needed for stereo visualization [/color][/b], the following is Visualize and process the part or surface we are interested in

 

Three-dimensional cross-sectional view

In 2#, we use the scalar function v=f(x,y,z)=(x+y+z)(xy+xz+yz)-10xyz to define a three-dimensional object, and prepare all the required data, in order to make To visualize the three-dimensional object, we can use the following command to view some cross-sections of the three-dimensional object.

The figure of V=(x+y+z)(xy+xz+yz)-10xyz=0 is roughly as follows:

[align=center][attach]1187[/attach][/align]

[align=center][attach] 1188[/attach][/align]


[align=center][b][size=5][color=blue] Three-dimensional plane interception[/color][/size][/b][/align][code] slice(X,Y,Z,V,[6 8],[5 9],[3 5])% intercept x=6 and 8, y=5 and 9, z=3 and 5, etc. 6 cross-sectional views
xlabel ('x')
ylabel('y')
zlabel('z')[/code]
[align=center][attach]1184[/attach][/align]

From the figure we can see that the command intercepts x for us =6 and 8, y=5 and 9, z=3 and 5, etc. 6 cross-sectional views. Note that the colors in the figure are automatically drawn according to V.




[align=center][b][size=5][color=blue]Three-dimensional three-dimensional surface interception[/color][/size][/b][/align]

Above we demonstrated the plane section of the three-dimensional figure, in In a three-dimensional image, a curved cross-section of a three-dimensional figure can also be displayed. [code][xs,ys]=meshgrid(x,y);
zs=sin(-xs+ys/2);
slice(X,Y,Z,V,xs,ys,zs)% uses the sine function to intercept the section of the three-dimensional figure, that is, to intercept the surface
xlabel('x') that satisfies the equation z=sin(-x+y/2 )
ylabel('y')
zlabel('z')
[/code][align=center][attach]1185[/attach][/align]

[align=center][b][size=5][color=blue ]Section contour drawing[/color][/size][/b][/align]

In addition to the screenshot plane and surface, you can also use the contourslice function to add contours to the intercepted plane[code]slice(X,Y ,Z,V,[7 3],[5 9],[3 5])
hold on
h=contourslice(X,Y,Z,V,7,[5 9],5);% at x=7, y=5 and 9, z=5 add contour lines to the section
set(h,'edgecolor','k','linewidth',1.5)% set the color and line width of the contour lines
xlabel('x')
ylabel('y')
zlabel('z')[/code][align=center][attach]1189[/attach][/align]

 

Three-dimensional isosurface plot-three-dimensional implicit function plot

We always scold mathworks as a damn. There must be any function as long as we can think of it, but it does not provide a three-dimensional implicit function drawing.

In fact, although Matlab does not provide a direct function to draw a three-dimensional implicit function graph, we can use [ b][color=Red] isosurface[/color][/b] map to draw

[b][color=Blue][size=4] the so-called isosurface map[/size][/color][/b ], that is, V=f(X,Y,Z)=C, the surface figure when the value of C is determined, it is only a surface in three-dimensional V and =, which is equivalent to 3# intercepting a special surface nothing

except a cross-sectional view of a solid object, to find that the value of V is equal to a certain surface (isosurface), also common. In Matlab, this operation can be achieved by using the isosurface() function, which is similar to the delaunay function and also returns the vertices of several triangles. Pass the parameters returned by the isosurface function to the patch function, and the isosurface composed of these triangles can be drawn. [code][X,Y,Z,V]=flow(13);
fv=isosurface(X,Y,Z,V,-2);% display V=-2 equal value surface
subplot(121)
p= patch(fv);
set(p,'facecolor',[0.5 0.5 0.5],'edgecolor','k');
view(3)
axis equal tight
grid on
subplot(122)
p=patch(shrinkfaces(fv,0.3 ));%shrinkfaces function is to shrink the surface
set(p,'facecolor',[0.5 0.5 0.5],'edgecolor','k');
view(3)
axis equal tight
grid on[/code][align=center][attach]1190[/attach][ /align]

When we display the three-dimensional graphics only to observe the general structure, there is no need to plot all the data points, because too many data points will reduce the display speed. [color=Blue]Using the reducevolume() and reducepatch() functions in Matlab, you can use the graph to delete some data points or fragments that have little effect on the graph display before displaying, thereby improving the efficiency of graph display[/color].


For another example, we want to draw a sphere with a three-dimensional implicit function x^2+y^2+z^2=5. Since Matlab does not provide a direct function to draw a three-dimensional implicit function graph, we can only use three-dimensional Isosurfaces are used for interception. Although drawing the isosurfaces is troublesome and the obtained graphics are not very beautiful, it is at least better than nothing

[b]1, build a three-dimensional model[/b]
V=X^2+Y^2+Z ^2-5;
[b]2, get three-dimensional data[/b][code][
X,Y, Z] = meshgrid(-10:0.5:10); V=X.^2+Y.^2 +Z.^2-5;[/code][b]3, draw isosurface[/b][code]%x^2+y^2+z^2=5 is equivalent to the equivalent of V=0 Face
fv=isosurface(X,Y,Z,V,0);% display V=0 and so on face
p=patch(fv);
set(p,'FaceColor','red','EdgeColor','none ');

camlight
lighting gouraud
axis equal tight[/code][align=center][attach]1191[/attach][/align]

 

Three-dimensional data smoothing

Three-dimensional data can also be smoothed by filtering through the smooth3() function [code]data=rand(10,10,10);
datas=smooth3(data,'box',3);%box is the smoothing method, Matlab There are several methods available in the, see the help for specific
subplot(121)
p=patch(isosurface(data,0.5));
patch(isocaps(data,0.5));
isonormals(data,p);
camlight
lighting phong
axis vis3d off
view(3)
subplot(122)
p=patch(isosurface(datas,0.5));
patch(isocaps(datas,0.5));
isonormals(datas,p);
camlight
lighting phong
axis vis3d off
view(3)[/code ] The usage of the isocaps and isonormals functions used above is as follows:
isocaps generates the outer surface of the block diagram, and isonormals adjusts the attributes of the drawn fragments, so that the displayed graphics have the correct lighting effect

 

Guess you like

Origin blog.csdn.net/ccsss22/article/details/115217880