[MOAB-dev] r3629 - in MOAB/trunk/examples: . FileReader
iulian at mcs.anl.gov
iulian at mcs.anl.gov
Mon Mar 15 17:32:40 CDT 2010
Author: iulian
Date: 2010-03-15 17:32:40 -0500 (Mon, 15 Mar 2010)
New Revision: 3629
Added:
MOAB/trunk/examples/FileReader/
MOAB/trunk/examples/FileReader/A.1.ele
MOAB/trunk/examples/FileReader/A.1.node
MOAB/trunk/examples/FileReader/FileRead.cpp
MOAB/trunk/examples/FileReader/Makefile
MOAB/trunk/examples/FileReader/README
Log:
add example read
Added: MOAB/trunk/examples/FileReader/A.1.ele
===================================================================
--- MOAB/trunk/examples/FileReader/A.1.ele (rev 0)
+++ MOAB/trunk/examples/FileReader/A.1.ele 2010-03-15 22:32:40 UTC (rev 3629)
@@ -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 A.poly
Added: MOAB/trunk/examples/FileReader/A.1.node
===================================================================
--- MOAB/trunk/examples/FileReader/A.1.node (rev 0)
+++ MOAB/trunk/examples/FileReader/A.1.node 2010-03-15 22:32:40 UTC (rev 3629)
@@ -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 A.poly
Added: MOAB/trunk/examples/FileReader/FileRead.cpp
===================================================================
--- MOAB/trunk/examples/FileReader/FileRead.cpp (rev 0)
+++ MOAB/trunk/examples/FileReader/FileRead.cpp 2010-03-15 22:32:40 UTC (rev 3629)
@@ -0,0 +1,125 @@
+#include <iostream>
+#include <fstream>
+#include <vector>
+#include <string>
+#include <sstream>
+
+#include "MBCore.hpp"
+#include "MBReadUtilIface.hpp"
+
+using namespace std;
+
+MBErrorCode ReadTriangleOutput( MBCore *mb, std::string fileBase ) {
+
+ //
+ // get the read iface from moab
+ void* ptr = 0;
+ mb->query_interface("MBReadUtilIface", &ptr);
+ MBReadUtilIface *iface = reinterpret_cast<MBReadUtilIface*>(ptr);
+ //
+ string nodeFileName = fileBase+".node";
+ ifstream nodeFile (nodeFileName.c_str());
+ if (!nodeFile.is_open())
+ {
+ cout<<"can't open node file .\n";
+ return MB_FILE_DOES_NOT_EXIST;
+ }
+
+ string eleFileName = fileBase+".ele";
+ ifstream eleFile (eleFileName.c_str());
+ if (!eleFile.is_open())
+ {
+ cout<<"can't open node file .\n";
+ return MB_FILE_DOES_NOT_EXIST;
+ }
+
+ string line;
+
+ // ignore comment lines that start with #
+
+ int num_nodes=0, num_triangles=0;
+ while(num_nodes==0)
+ {
+ getline(nodeFile, line);
+ if ('#' == line[0])
+ continue;
+ stringstream tks(line);
+ tks >> num_nodes; // ignore the rest of the line
+ cout << "num nodes:" << num_nodes << endl;
+ }
+
+ // allocate a block of vertex handles and read xyz’s into them
+ vector<double*> arrays;
+ MBEntityHandle startv, *starth;
+ MBErrorCode rval = iface->get_node_arrays(2, num_nodes, 0, startv, arrays);
+ for (int i = 0; i < num_nodes; i++)
+ {
+ getline(nodeFile, line);
+ if ('#' == line[0])
+ continue;
+ stringstream tokens(line);
+ int nodeId;
+ tokens >> nodeId >> arrays[0][i] >> arrays[1][i] ;
+ }
+
+
+ while(num_triangles==0)
+ {
+ getline(eleFile, line);
+ if ('#' == line[0])
+ continue;
+ stringstream tks(line);
+ tks >> num_triangles; // ignore the rest of the line
+ cout << "num triangles:" << num_nodes << endl;
+ }
+
+ MBEntityHandle starte;
+ // allocate block of triangle handles and read connectivity into them
+ rval = iface->get_element_array(num_triangles, 3, MBTRI, 0, starte, starth);
+
+ for (int j = 0; j < num_triangles; j++)
+ {
+ getline(eleFile, line);
+ if ('#' == line[0])
+ continue;
+ stringstream tokens(line);
+ int eleId, node;
+ tokens >> eleId;
+ for (int k=0; k<3; k++)
+ {
+ tokens >> node;
+ // vertex starts from 0
+ starth[3*j+k] = (MBEntityHandle)(node + (int)startv-1 );
+ }
+ }
+
+ //
+ return MB_SUCCESS;
+}
+
+
+// .
+// Read Triangle output files
+// Assume that the format is <filename>.node and <filename>.ele
+// see http://www.cs.cmu.edu/~quake/triangle.html for details
+//
+int main(int argc, char **argv) {
+ if (3!=argc) {
+ cout << "Usage: " << argv[0] << " <filename> <outFile> " << endl;
+ cout << " <filename> is the base file name; *.ele and *.node file are read; outFile is a file with an extension recognized by MOAB " << endl;
+ return 0;
+ }
+
+ string filename = argv[1];
+ char * outfile = argv[2];
+
+
+ // get MOAB instance and read the file
+ MBCore *mb = new MBCore();
+
+ MBErrorCode rval = ReadTriangleOutput(mb, filename);
+
+ if (rval==MB_SUCCESS)
+ mb->write_file(outfile);
+ return 0;
+}
Added: MOAB/trunk/examples/FileReader/Makefile
===================================================================
--- MOAB/trunk/examples/FileReader/Makefile (rev 0)
+++ MOAB/trunk/examples/FileReader/Makefile 2010-03-15 22:32:40 UTC (rev 3629)
@@ -0,0 +1,10 @@
+MOAB_LIB_DIR = /homes/iulian/lib/MOAB/lib
+CXX=/homes/sharp/3rdparty/mpich2/mpich2-1.1.1p1/gcc/bin/mpicxx
+include ${MOAB_LIB_DIR}/moab.make
+
+fread : FileRead.o
+ ${CXX} -g $< ${MOAB_LIBS_LINK} -o $@
+
+.cpp.o :
+ ${CXX} -g ${MOAB_INCLUDES} -c $<
+
Added: MOAB/trunk/examples/FileReader/README
===================================================================
--- MOAB/trunk/examples/FileReader/README (rev 0)
+++ MOAB/trunk/examples/FileReader/README 2010-03-15 22:32:40 UTC (rev 3629)
@@ -0,0 +1,14 @@
+Mesh readers and writers communicate mesh into/out of MOAB from/to disk files.
+Reading a mesh often involves importing large sets of data, for example coordinates of all the nodes in the mesh. Normally, this process would involve reading data from the file into a temporary data buffer, then copying data from there into its destination in MOAB. To avoid the expense of copying data, MOAB has implemented a reader/writer interface that provides direct access to blocks of memory used to represent mesh.
+The reader interface, declared in MBReadUtilIface, is used to request blocks of memory for storing coordinate positions and element connectivity. The pointers returned from these functions point to the actual memory used to represent those data in MOAB. Once data is written to that memory, no further copying is done. This not only saves time, but it also eliminates the need to allocate a large memory buffer for intermediate storage of these data.
+
+In this example, Triangle output files are read into MOAB. The database is saved into a file specified by the user, extension decides the type of the file.
+
+usage
+fread <triangle_file_base> <output_file>
+example provided
+$> fread A.1 out.vtk
+
+It is assumed that the <>.node file and <>.ele files are existing.
+
+
More information about the moab-dev
mailing list