]> Creatis software - CreaPhase.git/blob - octave_packages/geometry-1.5.0/io/data2geo.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / geometry-1.5.0 / io / data2geo.m
1 %% Copyright (c) 2010 Juan Pablo Carbajal <carbajal@ifi.uzh.ch>
2 %%
3 %%    This program is free software: you can redistribute it and/or modify
4 %%    it under the terms of the GNU General Public License as published by
5 %%    the Free Software Foundation, either version 3 of the License, or
6 %%    any later version.
7 %%
8 %%    This program is distributed in the hope that it will be useful,
9 %%    but WITHOUT ANY WARRANTY; without even the implied warranty of
10 %%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 %%    GNU General Public License for more details.
12 %%
13 %%    You should have received a copy of the GNU General Public License
14 %%    along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 %% -*- texinfo -*-
17 %% @deftypefn {Function File} {@var{fileStr} =} data2geo (@var{data}, @var{lc})
18 %% @deftypefnx {Function File} {@var{fileStr} =} data2geo (@dots{}, @var{param}, @var{value})
19 %% Uses data to build a file compatible with Gmsh.
20 %%
21 %% @var{data} is assumed to describe a polygon in @code{polygon2d} format.
22 %% The argument @var{lc} specifies the edge size.
23 %%
24 %% The optional parameters can be 'output' followed with a string specifying a file
25 %% to write, and 'spherical' followed by a real number @var{r} indicating that the
26 %  polygon describes a spherical surface of radious @var{r}.
27 %%
28 %% @seealso{polygon2d, @@svg/path2polygon}
29 %% @end deftypefn
30
31 function strFile = data2geo(data,lc,varargin)
32
33     nl = @()sprintf('\n');
34
35     %% Parse options
36     filegiven = [];
37     spherical = [];
38     if nargin > 2
39       filegiven = find(cellfun(@(x)strcmpi(x,'output'),varargin));
40       spherical = find(cellfun(@(x)strcmpi(x,'spherical'),varargin));
41     end
42
43     [n dim] = size(data);
44     if dim == 2
45         data(:,3) = zeros(n,1);
46     end
47
48     header = ' // File created with Octave';
49     strFile = [];
50     strFile = [strFile header nl()];
51
52     % Points
53     strFile = [strFile '// Points' nl()];
54
55     for i=1:n
56         strFile = [strFile pointGeo(i,data(i,:),lc)];
57     end
58
59     % Lines
60     strFile = [strFile '// Lines' nl()];
61     for i=1:n-1
62         strFile = [strFile lineGeo(i,i,i+1)];
63     end
64     strFile = [strFile lineGeo(n,n,1)];
65
66     % Loop
67     strFile = [strFile lineLoopGeo(n+1,n,1:n)];
68
69     % Surface
70     if spherical
71         sphr = varargin{spherical+1};
72         if dim ==2
73             sphr(1,3) = 0;
74         end
75         strFile = [strFile pointGeo(n+1,sphr,lc)];
76         strFile = [strFile ruledSurfGeo(n+3,1,n+1,n+1)];
77     else
78         strFile = [strFile planeSurfGeo(n+2,1,n+1)];
79     end
80
81     if filegiven
82         outfile = varargin{filegiven+1};
83         fid = fopen(outfile,'w');
84         fprintf(fid,'%s',strFile);
85         fclose(fid);
86         disp(['DATA2GEO: Geometry file saved to ' outfile])
87     end
88 endfunction
89
90 %!demo
91 %! points  = [0 0 0; 0.1 0 0; 0.1 .3 0; 0 0.3 0];
92 %! strFile = data2geo(points,0.009);
93 %! disp(strFile)
94
95 %!demo
96 %! dc = svg('drawing6.svg');
97 %! ids = dc.pathid();
98 %! P = dc.path2polygon(ids{1},12)(1:end-1,:);
99 %! P = bsxfun(@minus, P, centroid(P));
100 %! P = simplifypolygon(P,'tol',5e-1);
101 %! filename = tmpnam ();
102 %! meshsize = sqrt(mean(sumsq(diff(P,1,1),2)))/2;
103 %! data2geo (P, meshsize, 'output', [filename '.geo']);
104 %!
105 %! pkg load msh fpl
106 %! T = msh2m_gmsh(filename);
107 %! pdemesh(T.p,T.e,T.t)
108 %!
109 %! % --------------------------------------------------------------------------
110 %! % We load the drawing6.svg file into Octave and transform it into a polygon.
111 %! % Then we create a temporary file where the .geo mesh will be written.
112 %! % If the packages msh and fpl are available, a mesh is created from the .geo
113 %! % file.