$extrastylesheet
off_io.C
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 // C++ includes
00019 #include <fstream>
00020 
00021 // Local includes
00022 #include "libmesh/off_io.h"
00023 #include "libmesh/mesh_base.h"
00024 #include "libmesh/edge_edge2.h"
00025 #include "libmesh/face_tri3.h"
00026 
00027 namespace libMesh
00028 {
00029 
00030 
00031 
00032 // ------------------------------------------------------------
00033 // OFFIO class members
00034 
00035 void OFFIO::read(const std::string& name)
00036 {
00037   std::ifstream in (name.c_str());
00038 
00039   read_stream(in);
00040 }
00041 
00042 
00043 
00044 void OFFIO::read_stream(std::istream& in)
00045 {
00046   // This is a serial-only process for now;
00047   // the Mesh should be read on processor 0 and
00048   // broadcast later
00049   libmesh_assert_equal_to (this->mesh().processor_id(), 0);
00050 
00051   // Get a reference to the mesh
00052   MeshBase& the_mesh = MeshInput<MeshBase>::mesh();
00053 
00054   // Clear any existing mesh data
00055   the_mesh.clear();
00056 
00057   // Check the input buffer
00058   libmesh_assert (in.good());
00059 
00060   unsigned int nn, ne, nf;
00061 
00062   std::string label;
00063 
00064   // Read the first string.  It should say "OFF"
00065   in >> label;
00066 
00067   libmesh_assert_equal_to (label, "OFF");
00068 
00069   // read the number of nodes, faces, and edges
00070   in >> nn >> nf >> ne;
00071 
00072 
00073   Real x=0., y=0., z=0.;
00074 
00075   // Read the nodes
00076   for (unsigned int n=0; n<nn; n++)
00077     {
00078       libmesh_assert (in.good());
00079 
00080       in >> x
00081          >> y
00082          >> z;
00083 
00084       the_mesh.add_point ( Point(x,y,z), n );
00085     }
00086 
00087   unsigned int nv, nid;
00088 
00089   // Read the elements
00090   for (unsigned int e=0; e<nf; e++)
00091     {
00092       libmesh_assert (in.good());
00093 
00094       // The number of vertices in the element
00095       in >> nv;
00096 
00097       libmesh_assert(nv == 2 || nv == 3);
00098       if (e == 0)
00099         {
00100           the_mesh.set_mesh_dimension(cast_int<unsigned char>(nv-1));
00101           if (nv == 3)
00102             {
00103 #if LIBMESH_DIM < 2
00104               libmesh_error_msg("Cannot open dimension 2 mesh file when configured without 2D support.");
00105 #endif
00106             }
00107         }
00108 
00109       Elem* elem;
00110       switch (nv)
00111         {
00112         case 2:
00113           elem = new Edge2;
00114           break;
00115 
00116         case 3:
00117           elem = new Tri3;
00118           break;
00119 
00120         default:
00121           libmesh_error_msg("Unsupported nv = " << nv);
00122         }
00123 
00124       elem->set_id(e);
00125       the_mesh.add_elem (elem);
00126 
00127       for (unsigned int i=0; i<nv; i++)
00128         {
00129           in >> nid;
00130           elem->set_node(i) = the_mesh.node_ptr(nid);
00131         }
00132     }
00133 }
00134 
00135 } // namespace libMesh