Difference between revisions of "Geometry package"

From Octave
Jump to navigation Jump to search
Line 20: Line 20:
  
 
<!-- <syntaxhighlight lang="matlab"> -->
 
<!-- <syntaxhighlight lang="matlab"> -->
{{Code|Loading the file as polygon compatible with geometry package|<syntaxhighlight line="GESHI_FANCY_LINE_NUMBERS" lang="matlab">
+
{{Code|Loading the file as polygon compatible with geometry package|subst:{{SyntaxHighlight|
 
octavesvg = svg ("octave.svg").normalize();
 
octavesvg = svg ("octave.svg").normalize();
 
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));
</syntaxhighlight>}}
+
}}
 +
}}
 
Now we have our SVG as a polygon compatible with the Geometry package format. You can plot the polygon using the function {{Codeline|drawPolygon}}.  
 
Now we have our SVG as a polygon compatible with the Geometry package format. You can plot the polygon using the function {{Codeline|drawPolygon}}.  
{{Code|Plotting a polygon compatible with geometry package|<pre>
+
{{Code|Plotting a polygon compatible with geometry package|<syntaxhighlight line="GESHI_FANCY_LINE_NUMBERS" lang="matlab" style="font-size:13px">
 
drawPolygon (P, "-o");
 
drawPolygon (P, "-o");
</pre>}}
+
</syntaxhighlight>}}
 
As you can see the polygon has lots of points. We need to simplify the polygon in order to obtain a mesh of reasonable size. Otherwise gmsh will have problems meshing and the result could be huge (or a segmentation fault :( ). The package geometry comes with a simplification function but as of version 1.4.0, this function is very naïve and wont fix this problem. If you know how to simplify polygons you can contribute!
 
As you can see the polygon has lots of points. We need to simplify the polygon in order to obtain a mesh of reasonable size. Otherwise gmsh will have problems meshing and the result could be huge (or a segmentation fault :( ). The package geometry comes with a simplification function but as of version 1.4.0, this function is very naïve and wont fix this problem. If you know how to simplify polygons you can contribute!
 
We are going to reduce the amount of points of the polygon in a drastic and destructive way (i.e. the shape may be considerably damaged). The codes to do so follows, in the future a better procedure will be published here.
 
We are going to reduce the amount of points of the polygon in a drastic and destructive way (i.e. the shape may be considerably damaged). The codes to do so follows, in the future a better procedure will be published here.
{{Code|Symplification of a polygon compatible with geometry package|<pre>
+
{{Code|Symplification of a polygon compatible with geometry package|<syntaxhighlight line="GESHI_FANCY_LINE_NUMBERS" lang="matlab" style="font-size:13px">
 
Ps = P;  
 
Ps = P;  
 
n  = 1e6;
 
n  = 1e6;
Line 43: Line 44:
 
whos P Ps
 
whos P Ps
 
P  = Ps;  
 
P  = Ps;  
</pre>}}
+
</syntaxhighlight>}}
  
 
The next step is to mesh the interior of the polygon. To do this we could just call {{Codeline|delaunay}} 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 {{Forge|msh}}, which requires [http://geuz.org/gmsh/ Gmsh] installed in your system. The function {{Codeline|data2geo}} in the Geometry package makes our work very easy:
 
The next step is to mesh the interior of the polygon. To do this we could just call {{Codeline|delaunay}} 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 {{Forge|msh}}, which requires [http://geuz.org/gmsh/ Gmsh] installed in your system. The function {{Codeline|data2geo}} in the Geometry package makes our work very easy:
  
{{Code|Generating mesh for plot with msh package|<pre>
+
{{Code|Generating mesh for plot with msh package|<syntaxhighlight line="GESHI_FANCY_LINE_NUMBERS" lang="matlab" style="font-size:13px">
 
pkg load msh
 
pkg load msh
 
filename = tmpnam ();
 
filename = tmpnam ();
Line 53: Line 54:
 
data2geo (P, meshsize, "output", [filename ".geo"]);
 
data2geo (P, meshsize, "output", [filename ".geo"]);
 
T        = msh2m_gmsh (filename);
 
T        = msh2m_gmsh (filename);
</pre>}}
+
</syntaxhighlight>}}
  
 
<!-- </syntaxhighlight> -->
 
<!-- </syntaxhighlight> -->
 
After this code finishes, the structure T contains our mesh. To plot the generated mesh we use the function {{Codeline|pdemesh}} from the {{Forge|fpl}} package. In general is a good idea to use the openGL render (called ''fltk'') to plot meshes.
 
After this code finishes, the structure T contains our mesh. To plot the generated mesh we use the function {{Codeline|pdemesh}} from the {{Forge|fpl}} package. In general is a good idea to use the openGL render (called ''fltk'') to plot meshes.
  
{{Code|Plotting mesh with fpl package|<pre>
+
{{Code|Plotting mesh with fpl package|<syntaxhighlight line="GESHI_FANCY_LINE_NUMBERS" lang="matlab" style="font-size:13px">
 
pkg load fpl  
 
pkg load fpl  
 
graphics_toolkit ('fltk')
 
graphics_toolkit ('fltk')
Line 65: Line 66:
 
axis tight
 
axis tight
 
axis equal
 
axis equal
</pre>}}
+
</syntaxhighlight>}}
  
 
The output should look something like this
 
The output should look something like this

Revision as of 10:55, 2 April 2012

The geometry package is multipackage providing functions to manipulate geometrical entities in 2D and 3D. It is based in matGeom and extends it with several other functionalities, e.g. the package provides interfaces with SVG files and polygon meshing using Gmsh.

Relation to matGeom

octCLIP

Piece-wise 2D polynomial polygons

Tutorials

Loading SVG files

Meshing Octave

Under construction

This tutorial shows the workflow to generate a triangular mesh inside an arbitrary region. This tutorial requires that you install the package fpl and msh (which requires Gmsh installed in your system). Alternatively, the core function delaunay could be used but the result aren't so pretty.

This tutorial requires an interesting shape to mesh. If you have Inkscape you can create one, and use the previous tutorial to load it into octave. Here I will be using this SVG.

Octave.png
Code: Loading the file as polygon compatible with geometry package

subst: <syntaxhighlight line="GESHI_FANCY_LINE_NUMBERS" lang="matlab" style="font-size:13px"> {{{1}}} </syntaxhighlight>



Now we have our SVG as a polygon compatible with the Geometry package format. You can plot the polygon using the function drawPolygon.

Code: Plotting a polygon compatible with geometry package
1 drawPolygon (P, "-o");

As you can see the polygon has lots of points. We need to simplify the polygon in order to obtain a mesh of reasonable size. Otherwise gmsh will have problems meshing and the result could be huge (or a segmentation fault :( ). The package geometry comes with a simplification function but as of version 1.4.0, this function is very naïve and wont fix this problem. If you know how to simplify polygons you can contribute! We are going to reduce the amount of points of the polygon in a drastic and destructive way (i.e. the shape may be considerably damaged). The codes to do so follows, in the future a better procedure will be published here.

Code: Symplification of a polygon compatible with geometry package
 1 Ps = P; 
 2 n  = 1e6;
 3 thrsd = 0.38; 
 4 while n > size (Ps,1)
 5   n  = size (Ps,1); 
 6   ds = sqrt (sumsq (Ps(1:end,:) - Ps([2:end 1],:),2));
 7   Ps = Ps(ds > mean (ds)*(1-thrsd),:); 
 8 end
 9 whos P Ps
10 P  = Ps;

The next step is to mesh the interior of the polygon. To do this we could just call delaunay 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 msh, which requires Gmsh installed in your system. The function data2geo in the Geometry package makes our work very easy:

Code: Generating mesh for plot with msh package
1 pkg load msh
2 filename = tmpnam ();
3 meshsize = sqrt (mean (sumsq (diff (P, 1, 1), 2)))/2;
4 data2geo (P, meshsize, "output", [filename ".geo"]);
5 T        = msh2m_gmsh (filename);

After this code finishes, the structure T contains our mesh. To plot the generated mesh we use the function pdemesh from the fpl package. In general is a good idea to use the openGL render (called fltk) to plot meshes.

Code: Plotting mesh with fpl package
1 pkg load fpl 
2 graphics_toolkit ('fltk')
3 pdemesh (T.p, T.e, T.t);
4 view (2)
5 axis tight
6 axis equal

The output should look something like this

Octave meshed.png

Questions? Ideas? Join us in the mailing list or in the #octave IRC channel.

From piece-wise polynomial shapes to polygons

See also