Matlab reads .wrl 3D image files
1. wrl file
- wrl (Virtual Reality Language): A wrl file is a virtual reality text format file. Also the extension for VRML scene model files.
- wrl files are pure ASCII files, so they can be opened and edited with a text editor. Even so, for large 3D files. It is not advisable to use a purely manual method. You should choose to use 3D software (maya, max, etc.) for scene modeling and output.
- Can be produced and exported by SolidWorks
- Visualization can be opened by matlab
For example, scene.wrl file, its content is as follows:
#VRML V2.0 utf8
Transform {
children [
DEF model1 Transform {
children [
DEF body1 Transform {
translation -1 0 0
children [
DEF shape1 Shape {
appearance Appearance {
material Material {
diffuseColor 1.0 1.0 0.0
}
}
geometry Box {
size 1.5 1.5 1.5
}
}
]
}
]
}
DEF model2 Transform {
children [
DEF body2 Transform {
translation 1 0 0
children [
DEF shape2 Shape {
appearance Appearance {
material Material {
diffuseColor 1.0 1.0 0.0
}
}
geometry Box {
size 1.5 1.5 1.5
}
}
]
}
]
}
]
}
2. Open the .wrl file in matlab
%********************打开.wrl文件*************
vrsetpref('DefaultViewer', 'internalv5')
w = vrworld('scene.wrl');
open(w);
view(w);
3. Matlab draws three-dimensional coordinate points in wrl
Open the .wrl file in txt format, directly copy the coordinates of the point set to the matlab code, and add after each line of coordinates...
%绘制三维点
%%base_link
P=[ -0.075000 -0.100000 0.000000, -0.075000 -0.100000 0.570000, -0.075000 -0.075000 0.436008, -0.075000 -0.073559 0.421376, -0.075000 -0.073559 0.450639,...
-0.075000 -0.069291 0.407306, -0.075000 -0.069291 0.464709, -0.075000 -0.062360 0.394340, -0.075000 -0.062360 0.477675, -0.075000 -0.053033 0.382975,...
-0.075000 -0.053033 0.489041, -0.075000 -0.041668 0.373647, -0.075000 -0.041668 0.498368, -0.075000 -0.028701 0.366717, -0.075000 -0.028701 0.505299,...
-0.075000 -0.014632 0.362449, -0.075000 -0.014632 0.509567, -0.075000 0.000000 0.361008, -0.075000 0.000000 0.511008, -0.075000 0.014632 0.362449,...
-0.075000 0.014632 0.509567, -0.075000 0.028701 0.366717, -0.075000 0.028701 0.505299, -0.075000 0.041668 0.373647, -0.075000 0.041668 0.498368,...
-0.075000 0.053033 0.382975, -0.075000 0.053033 0.489041, -0.075000 0.062360 0.394340, -0.075000 0.062360 0.477675, -0.075000 0.069291 0.407306,...
-0.075000 0.069291 0.464709, -0.075000 0.073559 0.421376, -0.075000 0.073559 0.450639, -0.075000 0.075000 0.436008, -0.075000 0.100000 0.000000,...
-0.075000 0.100000 0.570000, -0.025000 -0.075000 0.436008, -0.025000 -0.073559 0.421376, -0.025000 -0.073559 0.450639, -0.025000 -0.069291 0.407306,...
-0.025000 -0.069291 0.464709, -0.025000 -0.062360 0.394340, -0.025000 -0.062360 0.477675, -0.025000 -0.053033 0.382975, -0.025000 -0.053033 0.489041,...
-0.025000 -0.041668 0.373647, -0.025000 -0.041668 0.498368, -0.025000 -0.028701 0.366717, -0.025000 -0.028701 0.505299, -0.025000 -0.014632 0.362449,...
-0.025000 -0.014632 0.509567, -0.025000 0.000000 0.361008, -0.025000 0.000000 0.436008, -0.025000 -0.000000 0.511008, -0.025000 0.014632 0.362449,...
-0.025000 0.014632 0.509567, -0.025000 0.028701 0.366717, -0.025000 0.028701 0.505299, -0.025000 0.041668 0.373647, -0.025000 0.041668 0.498368,...
-0.025000 0.053033 0.382975, -0.025000 0.053033 0.489041, -0.025000 0.062360 0.394340, -0.025000 0.062360 0.477675, -0.025000 0.069291 0.407306,...
-0.025000 0.069291 0.464709, -0.025000 0.073559 0.421376, -0.025000 0.073559 0.450639, -0.025000 0.075000 0.436008, 0.075000 -0.100000 0.000000,...
0.075000 -0.100000 0.570000, 0.075000 0.100000 0.000000, 0.075000 0.100000 0.570000 ]
i=1
while i<length(P)
scatter3(P(i),P(i+1),P(i+2));
hold on
i=i+3;
end
4. Perform spatial rotation transformation on 3D points
%旋转矩阵:
%绕x\y\z轴旋转,输入弧度
function [R]=getR(a,flg)
R=[]
if(flg==0)
R=[ 1 0 0;
0 cos(a) -sin(a);
0 sin(a) cos(a)];
elseif(flg==1)
R=[ cos(a) 0 sin(a);
0 1 0 ;
-sin(a) 0 cos(a)];
elseif(flg==2)
R=[ cos(a) -sin(a) 0;
sin(a) cos(a) 0 ;
0 0 1];
end
end
Following the point set format in Heading 3, the calculation is as follows:
rx=getR(-pi/2,0);
points0=points*rx
ry=getR(-pi/2,1);
points1=points*ry
rz=getR(-pi/2,2)
points2=point*rz