← Back to team overview

dolfin team mailing list archive

[andy.terrel+launchpad@xxxxxxxxx: [Merge] lp:~andy-terrel/dolfin/dev into lp:dolfin]

 

Should there also be a check for choosing the Triangle format based on
file suffix (in meshconvert.py)?

--
Anders
--- Begin Message ---
Andy R Terrel has proposed merging lp:~andy-terrel/dolfin/dev into lp:dolfin.

Requested reviews:
  DOLFIN Core Team (dolfin-core)


The branch adds support for the Triangle mesh format for dolfin-convert.
-- 
https://code.launchpad.net/~andy-terrel/dolfin/dev/+merge/38023
You are subscribed to branch lp:dolfin.
=== modified file 'site-packages/dolfin/mesh/meshconvert.py'
--- site-packages/dolfin/mesh/meshconvert.py	2010-02-15 20:15:15 +0000
+++ site-packages/dolfin/mesh/meshconvert.py	2010-10-08 21:19:59 +0000
@@ -4,7 +4,7 @@
 # Modified by Garth N. Wells (gmsh function)
 # Modified by Alexander H. Jarosch (gmsh fix)
 # Modified by Angelo Simone (Gmsh and Medit fix)
-# Modified by Andy R. Terrel (gmsh fix)
+# Modified by Andy R. Terrel (gmsh fix and triangle function)
 # Modified by Magnus Vikstrom (metis and scotch function)
 # Modified by Bartosz Sawicki (diffpack function)
 # Modified by Gideon Simpson (Exodus II function)
@@ -408,6 +408,64 @@
     ifile.close()
     ofile.close()
 
+def triangle2xml(ifilename, ofilename):
+    """Convert between triangle format (http://www.cs.cmu.edu/~quake/triangle.html) and .xml.  The
+    given ifilename should be the prefix for the corresponding .node, and .ele files.
+    """
+
+    def get_next_line (fp):
+        """Helper function for skipping comments and blank lines"""
+        line = fp.readline()
+        if line == '':
+            _error("Hit end of file prematurely.")
+        line = line.strip()
+        if not (line.startswith('#') or line == ''):
+            return line
+        return get_next_line(fp)
+
+
+    print "Converting from Triangle format {.node, .ele} to DOLFIN XML format"
+
+    # Open files
+    node_file = open(ifilename+".node", "r")
+    ele_file =  open(ifilename+".ele", "r")
+    ofile = open(ofilename, "w")
+
+    # Read all the nodes
+    nodes = {}
+    num_nodes, dim, attr, bound = map(int, get_next_line(node_file).split())
+    while len(nodes) < num_nodes:
+        node, x, y = get_next_line(node_file).split()[:3]
+        nodes[int(node)] = (float(x), float(y))
+
+    # Read all the triangles
+    tris = {}
+    num_tris, n_per_tri, attrs = map(int, get_next_line(ele_file).split())
+    while len(tris) < num_tris:
+        tri, n1, n2, n3 = map(int, get_next_line(ele_file).split()[:4])
+        tris[tri] = (n1, n2, n3)
+
+    # Write everything out
+    write_header_mesh(ofile, "triangle", 2)
+    write_header_vertices(ofile, num_nodes)
+    node_off = 0 if nodes.has_key(0) else -1
+    for node, node_t in nodes.iteritems():
+        write_vertex(ofile, node+node_off, node_t[0], node_t[1], 0.0)
+    write_footer_vertices(ofile)
+    write_header_cells(ofile, num_tris)
+    tri_off = 0 if tris.has_key(0) else -1
+    for tri, tri_t in tris.iteritems():
+        write_cell_triangle(ofile, tri+tri_off, tri_t[0] + node_off,
+                            tri_t[1] + node_off, tri_t[2] + node_off)
+    write_footer_cells(ofile)
+    write_footer_mesh(ofile)
+
+    # Close files
+    node_file.close()
+    ele_file.close()
+    ofile.close()
+
+
 def xml_old2xml(ifilename, ofilename):
     "Convert from old DOLFIN XML format to new."
 
@@ -1212,6 +1270,9 @@
     elif iformat == "gmsh":
         # Convert from gmsh to xml format
         gmsh2xml(ifilename, ofilename)
+    elif iformat == "Triangle":
+        # Convert from Triangle to xml format
+        triangle2xml(ifilename, ofilename)
     elif iformat == "xml-old":
         # Convert from old to new xml format
         xml_old2xml(ifilename, ofilename)

=== modified file 'utils/convert/dolfin-convert'
--- utils/convert/dolfin-convert	2010-08-24 20:35:25 +0000
+++ utils/convert/dolfin-convert	2010-10-08 21:19:59 +0000
@@ -6,7 +6,7 @@
 # Modified by Garth N. Wells (gmsh function)
 # Modified by Alexander H. Jarosch (gmsh fix)
 # Modified by Angelo Simone (Gmsh and Medit fix)
-# Modified by Andy R. Terrel (gmsh fix)
+# Modified by Andy R. Terrel (gmsh fix and triangle function)
 # Modified by Magnus Vikstrom (metis and scotch function)
 # Modified by Bartosz Sawicki (diffpack function)
 # Modified by Gideon Simpson (Exodus II function)
@@ -88,6 +88,7 @@
   xml      - DOLFIN XML mesh format (current)
   xml-old  - DOLFIN XML mesh format (DOLFIN 0.6.2 and earlier)
   mesh     - Medit, generated by tetgen with option -g
+  Triangle - Triangle file format (input prefix of .ele and .node files)
   gmsh     - Gmsh, version 2.0 file format
   metis    - Metis graph file format
   scotch   - Scotch graph file format

=== added file 'utils/convert/test_Triangle.ele'
--- utils/convert/test_Triangle.ele	1970-01-01 00:00:00 +0000
+++ utils/convert/test_Triangle.ele	2010-10-08 21:19:59 +0000
@@ -0,0 +1,31 @@
+29  3  0
+   1      29     2     1
+   2       2    29    23
+   3      25    24    23
+   4      23    22     2
+   5      25    23    29
+   6       2    22     3
+   7       3    21     4
+   8      21     3    22
+   9       4    21    20
+  10       5     4    26
+  11      19    26     4
+  12      26    19    18
+  13      19     4    20
+  14       5    26    28
+  15      12    14    13
+  16      14    12    11
+  17      11    10     9
+  18       8    14     9
+  19       8    15    14
+  20       9    14    11
+  21       6    27     7
+  22      26    18    27
+  23       5    28     6
+  24      27    18     7
+  25      28    27     6
+  26      15     7    16
+  27       7    15     8
+  28      17     7    18
+  29       7    17    16
+# Generated by triangle pq A.poly

=== added file 'utils/convert/test_Triangle.node'
--- utils/convert/test_Triangle.node	1970-01-01 00:00:00 +0000
+++ utils/convert/test_Triangle.node	2010-10-08 21:19:59 +0000
@@ -0,0 +1,31 @@
+29  2  1  1
+   1    0.20000000000000001  -0.77639999999999998  -0.56999999999999995    1
+   2    0.22  -0.7732  -0.55000000000000004    1
+   3    0.24560000000000001  -0.75639999999999996  -0.51000000000000001    1
+   4    0.27760000000000001  -0.70199999999999996  -0.53000000000000003    1
+   5    0.48880000000000001  -0.20760000000000001  0.28000000000000003    1
+   6    0.50480000000000003  -0.20760000000000001  0.29999999999999999    1
+   7    0.74080000000000001  -0.73960000000000004  0    1
+   8    0.75600000000000001  -0.76119999999999999  -0.01    1
+   9    0.77439999999999998  -0.77239999999999998  0    1
+  10    0.80000000000000004  -0.77639999999999998  0.02    1
+  11    0.80000000000000004  -0.79239999999999999  0.01    1
+  12    0.57920000000000005  -0.79239999999999999  -0.20999999999999999    1
+  13    0.57920000000000005  -0.77639999999999998  -0.20000000000000001    1
+  14    0.62160000000000004  -0.77159999999999995  -0.14999999999999999    1
+  15    0.63360000000000005  -0.76280000000000003  -0.13    1
+  16    0.63919999999999999  -0.74439999999999995  -0.10000000000000001    1
+  17    0.62080000000000002  -0.68440000000000001  -0.059999999999999998    1
+  18    0.58720000000000006  -0.60440000000000005  -0.01    1
+  19    0.36080000000000001  -0.60440000000000005  -0.23999999999999999    1
+  20    0.31919999999999998  -0.70679999999999998  -0.39000000000000001    1
+  21    0.312  -0.73960000000000004  -0.42999999999999999    1
+  22    0.31840000000000002  -0.76119999999999999  -0.44    1
+  23    0.33439999999999998  -0.77159999999999995  -0.44    1
+  24    0.37119999999999997  -0.77639999999999998  -0.40999999999999998    1
+  25    0.37119999999999997  -0.79239999999999999  -0.41999999999999998    1
+  26    0.37440000000000001  -0.56999999999999995  -0.20000000000000001    1
+  27    0.57440000000000002  -0.56999999999999995  0    1
+  28    0.47360000000000002  -0.33079999999999998  0.14000000000000001    1
+  29    0.20000000000000001  -0.79239999999999999  -0.58999999999999997    1
+# Generated by triangle pq A.poly


--- End Message ---

Follow ups