• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Matlab处理数据导出Paraview可读的vtk文件(一)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

Paraview是一个开源的可视化软件。

用到matlab子程序从这里下载

或者到博客末尾复制粘贴

子程序名为 vtkwrite

示例1:

1 load mri  
2 D = squeeze(D);  
3 vtkwrite(\'mri.vtk\', \'structured_points\', \'mri\', D) 

 

 

示例2:云图

1 load wind   
2 [cu,cv,cw] = curl(x, y, z, u, v, w);       
3 div = divergence(x, y, z, u, v, w);     
4 vtkwrite(\'wind.vtk\', \'structured_grid\', x, y, z, ...   
5 \'vectors\', \'vector_field\', u, v, w, \'vectors\', \'vorticity\', cu, cv, cw, \'scalars\', \'divergence\', div);

示例3:二维曲线

1 x = 1:100; 
2 y = sin(x); 
3 z = sqrt(x);     
4 vtkwrite(\'execute\',\'polydata\',\'lines\',x,y,z); 

示例4:三角形

1 [x,y,z] = peaks(100);     
2 z = .4*z;     
3 tri = delaunay(x,y);     
4 vtkwrite(\'peaks.vtk\',\'polydata\',\'triangle\',x,y,z,tri); 

 

 示例5:四面体

1 d = [-1 1];     
2 [x, y, z] = meshgrid(d, d, d);     
3 DT = delaunayTriangulation(x(:), y(:), z(:));     
4 vtkwrite(\'execute\', \'polydata\',\'tetrahedron\', x, y, z, DT.ConnectivityList);  

 

 

vtkwrite

  1 function vtkwrite( filename,dataType,varargin )
  2 % VTKWRITE Writes 3D Matlab array into VTK file format.
  3 %  vtkwrite(filename,\'structured_grid\',x,y,z,\'vectors\',title,u,v,w) writes
  4 %  a structured 3D vector data into VTK file, with name specified by the string
  5 %  filename. (u,v,w) are the vector components at the points (x,y,z). x,y,z
  6 %  should be 3-D matrices like those generated by meshgrid, where
  7 %  point(ijk) is specified by x(i,j,k), y(i,j,k) and z(i,j,k).
  8 %  The matrices x,y,z,u,v,w must all be the same size and contain
  9 %  corrresponding position and vector component. The string title specifies
 10 %  the name of the vector field to be saved. 
 11 %
 12 %  vtkwrite(filename,\'structured_grid\',x,y,z,\'scalars\',title,r) writes a 3D
 13 %  scalar data into VTK file whose name is specified by the string
 14 %  filename. r is the scalar value at the points (x,y,z). The matrices
 15 %  x,y,z,r must all be the same size and contain the corresponding position
 16 %  and scalar values. 
 17 %
 18 %  vtkwrite(filename,\'structured_grid\',x,y,z,\'vectors\',title,u,v,w,\'scalars\',
 19 %  title2,r) writes a 3D structured grid that contains both vector and scalar values.
 20 %  x,y,z,u,v,w,r must all be the same size and contain the corresponding
 21 %  positon, vector and scalar values.
 22 %
 23 %  vtkwrite(filename, \'structured_points\', title, m) saves matrix m (could
 24 %  be 1D, 2D or 3D array) into vtk as structured points.
 25 %
 26 %  vtkwrite(filename, \'structured_points\', title, m, \'spacing\', sx, sy, sz)
 27 %  allows user to specify spacing. (default: 1, 1, 1). This is the aspect
 28 %  ratio of a single voxel. 
 29 %
 30 %  vtkwrite(filename, \'structured_points\', title, m, \'origin\', ox, oy, oz)
 31 %  allows user to speicify origin of dataset. (default: 0, 0, 0).
 32 %
 33 %  vtkwrite(filename,\'unstructured_grid\',x,y,z,\'vectors\',title,u,v,w,\'scalars\',
 34 %  title2,r) writes a 3D unstructured grid that contains both vector and scalar values.
 35 %  x,y,z,u,v,w,r must all be the same size and contain the corresponding
 36 %  positon, vector and scalar values.
 37 %  
 38 %  vtkwrite(filename, \'polydata\', \'lines\', x, y, z) exports a 3D line where
 39 %  x,y,z are coordinates of the points that make the line. x, y, z are
 40 %  vectors containing the coordinates of points of the line, where point(n)
 41 %  is specified by x(n), y(n) and z(n).
 42 %
 43 %  vtkwrite(filename,\'polydata\',\'lines\',x,y,z,\'Precision\',n) allows you to
 44 %  specify precision of the exported number up to n digits after decimal
 45 %  point. Default precision is 3 digits. 
 46 %
 47 %  vtkwrite(filename,\'polydata\',\'triangle\',x,y,z,tri) exports a list of
 48 %  triangles where x,y,z are the coordinates of the points and tri is an
 49 %  m*3 matrix whose rows denote the points of the individual triangles.
 50 %
 51 %  vtkwrite(filename,\'polydata\',\'tetrahedron\',x,y,z,tetra) exports a list
 52 %  of tetrahedrons where x,y,z are the coordinates of the points
 53 %  and tetra is an m*4 matrix whose rows denote the points of individual
 54 %  tetrahedrons. 
 55 %  
 56 %  vtkwrite(\'execute\',\'polydata\',\'lines\',x,y,z) will save data with default
 57 %  filename \'\'matlab_export.vtk\' and automatically loads data into
 58 %  ParaView. 
 59 %  
 60 %  Version 2.3
 61 %  Copyright, Chaoyuan Yeh, 2016
 62 %  Codes are modified from William Thielicke and David Gingras\'s submission.    
 63 
 64 if strcmpi(filename,\'execute\'), filename = \'matlab_export.vtk\'; end
 65 fid = fopen(filename, \'w\'); 
 66 % VTK files contain five major parts
 67 % 1. VTK DataFile Version
 68 fprintf(fid, \'# vtk DataFile Version 2.0\n\');
 69 % 2. Title
 70 fprintf(fid, \'VTK from Matlab\n\');
 71 
 72 
 73 binaryflag = any(strcmpi(varargin, \'BINARY\'));
 74 if any(strcmpi(varargin, \'PRECISION\'))
 75     precision = num2str(varargin{find(strcmpi(vin, \'PRECISION\'))+1});
 76 else
 77     precision = \'2\';
 78 end
 79 
 80 switch upper(dataType)
 81     case \'STRUCTURED_POINTS\'
 82         title = varargin{1};
 83         m = varargin{2};
 84         if any(strcmpi(varargin, \'spacing\'))
 85             sx = varargin{find(strcmpi(varargin, \'spacing\'))+1};
 86             sy = varargin{find(strcmpi(varargin, \'spacing\'))+2};
 87             sz = varargin{find(strcmpi(varargin, \'spacing\'))+3};
 88         else
 89             sx = 1;
 90             sy = 1;
 91             sz = 1;
 92         end
 93         if any(strcmpi(varargin, \'origin\'))
 94             ox = varargin{find(strcmpi(varargin, \'origin\'))+1};
 95             oy = varargin{find(strcmpi(varargin, \'origin\'))+2};
 96             oz = varargin{find(strcmpi(varargin, \'origin\'))+3};
 97         else
 98             ox = 0;
 99             oy = 0;
100             oz = 0;
101         end
102         [nx, ny, nz] = size(m);
103         setdataformat(fid, binaryflag);
104         
105         fprintf(fid, \'DATASET STRUCTURED_POINTS\n\');
106         fprintf(fid, \'DIMENSIONS %d %d %d\n\', nx, ny, nz);
107         fprintf(fid, [\'SPACING \', num2str(sx), \' \', num2str(sy), \' \',...
108             num2str(sz), \'\n\']);
109         fprintf(fid, [\'ORIGIN \', num2str(ox), \' \', num2str(oy), \' \',...
110             num2str(oz), \'\n\']); 
111         fprintf(fid, \'POINT_DATA %d\n\', nx*ny*nz);
112         fprintf(fid, [\'SCALARS \', title, \' float 1\n\']);
113         fprintf(fid,\'LOOKUP_TABLE default\n\');
114         if ~binaryflag 
115             spec = [\'%0.\', precision, \'f \'];
116             fprintf(fid, spec, m(:)\');
117         else
118             fwrite(fid, m(:)\', \'float\', \'b\');
119         end
120         
121     case {\'STRUCTURED_GRID\',\'UNSTRUCTURED_GRID\'}
122         % 3. The format data proper is saved in (ASCII or Binary). Use
123         % fprintf to write data in the case of ASCII and fwrite for binary.
124         if numel(varargin)<6, error(\'Not enough input arguments\'); end
125         setdataformat(fid, binaryflag);
126 %         fprintf(fid, \'BINARY\n\');
127         x = varargin{1};
128         y = varargin{2};
129         z = varargin{3};
130         if sum(size(x)==size(y) & size(y)==size(z))~=length(size(x))
131             error(\'Input dimesions do not match\')
132         end
133         n_elements = numel(x);
134         % 4. Type of Dataset ( can be STRUCTURED_POINTS, STRUCTURED_GRID,
135         % UNSTRUCTURED_GRID, POLYDATA, RECTILINEAR_GRID or FIELD )
136         % This part, dataset structure, begins with a line containing the
137         % keyword \'DATASET\' followed by a keyword describing the type of dataset.
138         % Then the geomettry part describes geometry and topology of the dataset.
139         if strcmpi(dataType,\'STRUCTURED_GRID\')
140             fprintf(fid, \'DATASET STRUCTURED_GRID\n\');
141             fprintf(fid, \'DIMENSIONS %d %d %d\n\', size(x,1), size(x,2), size(x,3));
142         else
143             fprintf(fid, \'DATASET UNSTRUCTURED_GRID\n\');
144         end
145         fprintf(fid, [\'POINTS \' num2str(n_elements) \' float\n\']);
146         output = [x(:)\'; y(:)\'; z(:)\'];
147         
148         if ~binaryflag
149             spec = [\'%0.\', precision, \'f \'];
150             fprintf(fid, spec, output);
151         else
152             fwrite(fid, output, \'float\', \'b\');
153         end
154         % 5.This final part describe the dataset attributes and begins with the
155         % keywords \'POINT_DATA\' or \'CELL_DATA\', followed by an integer number
156         % specifying the number of points of cells. Other keyword/data combination
157         % then define the actual dataset attribute values.
158         fprintf(fid, [\'\nPOINT_DATA \' num2str(n_elements)]);
159         % Parse remaining argument.
160         vidx = find(strcmpi(varargin,\'VECTORS\'));
161         sidx = find(strcmpi(varargin,\'SCALARS\'));
162         if vidx~=0
163             for ii = 1:length(vidx)
164                 title = varargin{vidx(ii)+1};
165                 % Data enteries begin with a keyword specifying data type
166                 % and numeric format.
167                 fprintf(fid, [\'\nVECTORS \', title,\' float\n\']);
168                 output = [varargin{ vidx(ii) + 2 }(:)\';...
169                           varargin{ vidx(ii) + 3 }(:)\';...
170                           varargin{ vidx(ii) + 4 }(:)\'];
171 
172                 if ~binaryflag
173                     spec = [\'%0.\', precision, \'f \'];
174                     fprintf(fid, spec, output);
175                 else
176                     fwrite(fid, output, \'float\', \'b\');
177                 end
178 %                 fwrite(fid, [reshape(varargin{vidx(ii)+2},1,n_elements);...
179 %                 reshape(varargin{vidx(ii)+3},1,n_elements);...
180 %                 reshape(varargin{vidx(ii)+4},1,n_elements)],\'float\',\'b\');
181             end
182         end
183         if sidx~=0
184             for ii = 1:length(sidx)
185                 title = varargin{sidx(ii)+1};
186                 fprintf(fid, [\'\nSCALARS \', title,\' float\n\']);
187                 fprintf(fid, \'LOOKUP_TABLE default\n\');
188                 if ~binaryflag
189                     spec = [\'%0.\', precision, \'f \'];
190                     fprintf(fid, spec, varargin{ sidx(ii) + 2});
191                 else
192                     fwrite(fid, varargin{ sidx(ii) + 2}, \'float\', \'b\');
193                 end
194 %                 fwrite(fid, reshape(varargin{sidx(ii)+2},1,n_elements),\'float\',\'b\');
195             end
196         end
197         
198     case \'POLYDATA\'
199 
200         fprintf(fid, \'ASCII\n\');
201         if numel(varargin)<4, error(\'Not enough input arguments\'); end
202         x = varargin{2}(:);
203         y = varargin{3}(:);
204         z = varargin{4}(:);
205         if numel(varargin)<4, error(\'Not enough input arguments\'); end
206         if sum(size(x)==size(y) & size(y)==size(z))~= length(size(x))
207             error(\'Input dimesions do not match\')
208         end
209         n_elements = numel(x);
210         fprintf(fid, \'DATASET POLYDATA\n\');
211         if mod(n_elements,3)==1
212             x(n_elements+1:n_elements+2,1)=[0;0];
213             y(n_elements+1:n_elements+2,1)=[0;0];
214             z(n_elements+1:n_elements+2,1)=[0;0];
215         elseif mod(n_elements,3)==2
216             x(n_elements+1,1)=0;
217             y(n_elements+1,1)=0;
218             z(n_elements+1,1)=0;
219         end
220         nbpoint = numel(x);
221         fprintf(fid, [\'POINTS \' num2str(nbpoint) \' float\n\']);
222         
223         spec = [repmat([\'%0.\', precision, \'f \'], 1, 9), \'\n\'];
224         
225         output = [x(1:3:end-2), y(1:3:end-2), z(1:3:end-2),...
226                   x(2:3:end-1), y(2:3:end-1), z(2:3:end-1),...
227                   x(3:3:end), y(3:3:end), z(3:3:end)]\';
228               
229         fprintf(fid, spec, output);
230         
231         switch upper(varargin{1})
232             case \'LINES\'
233                 if mod(n_elements,2)==0
234                     nbLine = 2*n_elements-2;
235                 else
236                     nbLine = 2*(n_elements-1);
237                 end
238                 conn1 = zeros(nbLine,1);
239                 conn2 = zeros(nbLine,1);
240                 conn2(1:nbLine/2) = 1:nbLine/2;
241                 conn1(1:nbLine/2) = conn2(1:nbLine/2)-1;
242                 conn1(nbLine/2+1:end) = 1:nbLine/2;
243                 conn2(nbLine/2+1:end) = conn1(nbLine/2+1:end)-1;
244                 fprintf(fid,\'\nLINES %d %d\n\' 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
matlab学习笔记4--MAT文件的保存和读取发布时间:2022-07-18
下一篇:
Matlab产生波形数据文件 - limanjihe发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap