Geometry package: Difference between revisions

using templates and octave guidelines for code formatting
(using templates and octave guidelines for code formatting)
Line 17: Line 17:
The first part of the tutorial requires an interesting shape. If you have Inkscape you can use the previous tutorial to load it into octave. Here I will be using [http://ubuntuone.com/5pNS12ZChUXeGNBniWNa3J this SVG].  
The first part of the tutorial requires an interesting shape. If you have Inkscape you can use the previous tutorial to load it into octave. Here I will be using [http://ubuntuone.com/5pNS12ZChUXeGNBniWNa3J this SVG].  
[[File:octave.png|200px|center]]
[[File:octave.png|200px|center]]
Lets load the file:


<!-- <syntaxhighlight lang="matlab"> -->
<!-- <syntaxhighlight lang="matlab"> -->
<pre>
{{Code|Loading the file as polygon compatible with geometry package|<pre>
octavesvg = svg('octave.svg');
octavesvg = svg ("octave.svg");
ids = octavesvg.pathid();
ids       = octavesvg.pathid();
P = octavesvg.path2polygon(ids{1},12)(1:end-1,:);
P         = octavesvg.path2polygon (ids{1}, 12)(1:end-1,:);
P = bsxfun(@minus, P, centroid(P));
P         = bsxfun (@minus, P, centroid (P));
</pre>
</pre>}}
Now we have our SVG as a polygon compatible with the Geometry package format. You can plot the polygon using the function <tt>drawPolygon</tt>. the next step is to mesh the interior of the polygon. To do this we could just call <tt>delaunay</tt> on the polygon and be done with it, but in general such mesh wont be so nice (you will need to add interior points). A very effective way of generating a good mesh is to use the package [http://octave.sourceforge.net/msh/index.html msh], which requires [http://geuz.org/gmsh/ Gmsh] installed in your system. The function data2geo in the Geometry package makes our work very easy:
Now we have our SVG as a polygon compatible with the Geometry package format. You can plot the polygon using the function <tt>drawPolygon</tt>. the next step is to mesh the interior of the polygon. To do this we could just call <tt>delaunay</tt> on the polygon and be done with it, but in general such mesh wont be so nice (you will need to add interior points). A very effective way of generating a good mesh is to use the package [http://octave.sourceforge.net/msh/index.html msh], which requires [http://geuz.org/gmsh/ Gmsh] installed in your system. The function data2geo in the Geometry package makes our work very easy:


<pre>
{{Code|Generating mesh for plot with msh package|<pre>
pkg load msh
pkg load msh
filename = tmpnam ();
filename = tmpnam ();
meshsize = sqrt(mean(sumsq(diff(P,1,1),2)))/2;
meshsize = sqrt (mean (sumsq (diff (P, 1, 1), 2)))/2;
data2geo (P, meshsize, 'output', [filename '.geo']);
data2geo (P, meshsize, "output", [filename ".geo"]);
T = msh2m_gmsh(filename);
T = msh2m_gmsh (filename);
</pre>
</pre>}}
 
<!-- </syntaxhighlight> -->
<!-- </syntaxhighlight> -->
To plot the generated mesh we use the function <tt>pdemesh</tt> from the [http://octave.sourceforge.net/fpl/index.html fpl] package.
To plot the generated mesh we use the function <tt>pdemesh</tt> from the [http://octave.sourceforge.net/fpl/index.html fpl] package.
<pre>
 
{{Code|Plotting mesh with fpl package|<pre>
pkg load fpl  
pkg load fpl  
pdemesh(T.p,T.e,T.t)
pdemesh (T.p, T.e, T.t)
</pre>
</pre>}}


[[File:octave_meshed.png|thumb|200px]]
[[File:octave_meshed.png|thumb|200px]]