forked from fangq/iso2mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsavejmesh.m
More file actions
135 lines (121 loc) · 3.44 KB
/
savejmesh.m
File metadata and controls
135 lines (121 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
function savejmesh(node,face,elem,fname,varargin)
%
% savejmesh(node,face,elem,fname,opt)
%
% export a mesh to the JMesh format
%
% author: Qianqian Fang (fangq<at> nmr.mgh.harvard.edu)
% date: 2011/10/06
%
% input:
% node: input, node list, dimension (nn,3)
% face: input, optional, surface face element list, dimension (be,3)
% elem: input, tetrahedral element list, dimension (ne,4)
% fname: output file name
% opt: additional parameters in the form of 'parameter',value pairs
% valid parameters include:
% 'Dimension': 0 - a user defined mesh, 2- a 2D mesh, 3- a 3D mesh
% 'Author': a string to set the author of the mesh
% 'MeshTitle': a string to set the title of the mesh
% 'MeshTag': a value as the tag of the mesh data
% 'MeshGroup': a value to group this mesh with other mesh data
% 'Comment': a string as the additional note for the mesh data
%
% -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net)
%
if(nargin==2)
fname=face;
face=[];
elem=[];
end
if(nargin==3)
fname=elem;
elem=[];
end
if(length(varargin)==1 && ischar(varargin{1}))
opt=struct('FileName',varargin{1});
else
opt=varargin2struct(varargin{:});
end
meshdim=jsonopt('Dimension',0,opt);
fid=fopen(fname,'wt');
if(fid==-1)
error('You do not have permission to save mesh files.');
end
mesh.MeshVersion=0.5;
mesh.CreationTime=datestr(now);
mesh.Comment=['Created by iso2mesh ' iso2meshver '(http://iso2mesh.sf.net)'];
if(meshdim==0) % a user-defined mesh
mesh.MeshNode=node;
if(~isempty(face))
mesh.MeshSurf=face;
end
if(~isempty(elem))
mesh.MeshElem=elem;
end
elseif(meshdim==3) % a 3D mesh
nd=size(node);
if(nd(2)<3) error('expecting 3 or more columns in node'); end
mesh.MeshPoint3D=node(:,1:3);
if(nd(2)>3)
mesh.MeshNodeVal=node(:,4:end);
end
if(~isempty(face))
nd=size(face);
if(nd(2)<3) error('expecting 3 or more columns in face'); end
mesh.MeshTri=face(:,1:3);
if(nd(2)>3)
mesh.MeshTriVal=face(:,4:end);
end
end
if(~isempty(elem))
nd=size(elem);
if(nd(2)<4) error('expecting 4 or more columns in elem'); end
mesh.MeshTetra=elem(:,1:4);
if(nd(2)>4)
mesh.MeshTetraVal=elem(:,5:end);
end
end
elseif(meshdim==2) % a 2D mesh
nd=size(node);
if(nd(2)<2) error('expecting 2 or more columns in node'); end
mesh.MeshPoint2D=node(:,1:2);
if(nd(2)>2)
mesh.MeshNodeVal=node(:,3:end);
end
if(~isempty(face))
nd=size(face);
if(nd(2)<3) error('expecting 3 or more columns in face'); end
mesh.MeshTri=face(:,1:3);
if(nd(2)>3)
mesh.MeshTriVal=face(:,4:end);
end
end
if(~isempty(elem))
warning('elem is redundant in a 2D mesh, skip');
end
else
error('the specified Dimension is not supported, please remove to save data to a general format');
end
author=jsonopt('Author','',opt);
if(~isempty(author))
mesh.Author=author;
end
title=jsonopt('MeshTitle','',opt);
if(~isempty(title))
mesh.MeshTitle=title;
end
tag=jsonopt('MeshTag',[],opt);
if(~isempty(tag))
mesh.MeshTag=tag;
end
group=jsonopt('MeshGroup',[],opt);
if(~isempty(group))
mesh.MeshTag=group;
end
comm=jsonopt('Comment','',opt);
if(~isempty(comm))
mesh.Comment=comm;
end
fprintf(fid,'%s\n',savejson('',mesh,varargin{:}));
fclose(fid);