MOAB: Mesh Oriented datABase
(version 5.2.1)
|
00001 /** 00002 * \class ReadTemplate 00003 * \brief Template for writing a new reader in MOAB 00004 * 00005 */ 00006 00007 #include "ReadTemplate.hpp" 00008 #include "moab/Interface.hpp" 00009 #include "moab/ReadUtilIface.hpp" 00010 #include "moab/Range.hpp" 00011 #include "moab/FileOptions.hpp" 00012 00013 #include <cstdio> 00014 #include <assert.h> 00015 00016 namespace moab 00017 { 00018 00019 ReaderIface* ReadTemplate::factory( Interface* iface ) 00020 { 00021 return new ReadTemplate( iface ); 00022 } 00023 00024 ReadTemplate::ReadTemplate( Interface* impl ) : mbImpl( impl ), fileName( NULL ) 00025 { 00026 mbImpl->query_interface( readMeshIface ); 00027 } 00028 00029 ReadTemplate::~ReadTemplate() 00030 { 00031 if( readMeshIface ) 00032 { 00033 mbImpl->release_interface( readMeshIface ); 00034 readMeshIface = 0; 00035 } 00036 } 00037 00038 ErrorCode ReadTemplate::read_tag_values( const char* /* file_name */, const char* /* tag_name */, 00039 const FileOptions& /* opts */, std::vector< int >& /* tag_values_out */, 00040 const SubsetList* /* subset_list */ ) 00041 { 00042 return MB_NOT_IMPLEMENTED; 00043 } 00044 00045 ErrorCode ReadTemplate::load_file( const char* filename, const EntityHandle* file_set, const FileOptions& opts, 00046 const ReaderIface::SubsetList* subset_list, const Tag* /*file_id_tag*/ ) 00047 { 00048 if( subset_list ) 00049 { 00050 // See src/moab/ReaderIface.hpp, definition of SubsetList struct; this basically specifies 00051 // an integer tag and tag values for sets to read on this proc, or a part number and total # 00052 // parts for reading a trivial partition of entities 00053 } 00054 00055 // Save filename to member variable so we don't need to pass as an argument 00056 // to called functions 00057 fileName = filename; 00058 00059 // Process options; see src/FileOptions.hpp for API for FileOptions class, and 00060 // doc/metadata_info.doc for a description of various options used by some of the readers in 00061 // MOAB 00062 ErrorCode result = process_options( opts );MB_CHK_SET_ERR( result, fileName << ": problem reading options" ); 00063 00064 // Open file; filePtr is member of ReadTemplate, change to whatever mechanism is used to 00065 // identify file 00066 FILE* filePtr = fopen( fileName, "r" ); 00067 if( !filePtr ) { MB_SET_ERR( MB_FILE_DOES_NOT_EXIST, fileName << ": fopen returned error" ); } 00068 00069 // Read number of verts, elements, sets 00070 long num_verts = 0, num_elems = 0, num_sets = 0; 00071 00072 // read_ents keeps a running set of entities read from this file, including vertices, elements, 00073 // and sets; these will get added to file_set (if input) at the end of the read 00074 Range read_ents; 00075 00076 // start_vertex is passed back so we know how to convert indices from the file into vertex 00077 // handles; most of the time this is done by adding start_vertex to the (0-based) index; if the 00078 // index is 1-based, you also need to subtract one; see read_elements for details 00079 EntityHandle start_vertex; 00080 result = read_vertices( num_verts, start_vertex, read_ents ); 00081 if( MB_SUCCESS != result ) 00082 { 00083 fclose( filePtr ); 00084 return result; 00085 } 00086 00087 // Create/read elements; this template assumes that all elements are the same type, so can be 00088 // read in a single call to read_elements, and kept track of with a single start_elem handle. If 00089 // there are more entity types, might have to keep these start handles in an array/vector. 00090 // start_elem is only really needed if you're reading sets later, and need to convert some 00091 // file-based index to an entity handle 00092 EntityHandle start_elem; 00093 result = read_elements( num_elems, start_vertex, start_elem, read_ents ); 00094 if( MB_SUCCESS != result ) 00095 { 00096 fclose( filePtr ); 00097 return result; 00098 } 00099 00100 // Read/create entity sets; typically these sets have some tag identifying what they're for, see 00101 // doc/metadata_info.doc for examples of different kinds of sets and how they're marked 00102 result = create_sets( num_sets, start_vertex, num_verts, start_elem, num_elems, read_ents ); 00103 if( MB_SUCCESS != result ) 00104 { 00105 fclose( filePtr ); 00106 return result; 00107 } 00108 00109 // Finally, add all read_ents into the file set, if one was input 00110 if( file_set && *file_set ) 00111 { 00112 result = mbImpl->add_entities( *file_set, read_ents ); 00113 if( MB_SUCCESS != result ) 00114 { 00115 fclose( filePtr ); 00116 return result; 00117 } 00118 } 00119 00120 fclose( filePtr ); 00121 00122 return result; 00123 } 00124 00125 ErrorCode ReadTemplate::read_vertices( int num_verts, EntityHandle& start_vertex, Range& read_ents ) 00126 { 00127 // Allocate nodes; these are allocated in one shot, get contiguous handles starting with 00128 // start_handle, and the reader is passed back double*'s pointing to MOAB's native storage for 00129 // vertex coordinates for those verts 00130 std::vector< double* > coord_arrays; 00131 ErrorCode result = readMeshIface->get_node_coords( 3, num_verts, 1, start_vertex, coord_arrays );MB_CHK_SET_ERR( result, fileName << ": Trouble reading vertices" ); 00132 00133 // Fill in vertex coordinate arrays 00134 double *x = coord_arrays[0], *y = coord_arrays[1], *z = coord_arrays[2]; 00135 for( long i = 0; i < num_verts; ++i ) 00136 { 00137 // Read x/y/z; do something with them for now to avoid warning 00138 if( x || y || z ) {} 00139 } 00140 00141 if( num_verts ) read_ents.insert( start_vertex, start_vertex + num_verts - 1 ); 00142 00143 return result; 00144 } 00145 00146 //! Read/create elements 00147 ErrorCode ReadTemplate::read_elements( int num_elems, EntityHandle start_vertex, EntityHandle& start_elem, 00148 Range& read_ents ) 00149 { 00150 // Get the entity type being read 00151 EntityType ent_type = MBHEX; 00152 00153 // Get the number of vertices per entity 00154 int verts_per_elem = 8; 00155 00156 // Create the element sequence; passes back a pointer to the internal storage for connectivity 00157 // and the starting entity handle 00158 EntityHandle* conn_array; 00159 ErrorCode result = 00160 readMeshIface->get_element_connect( num_elems, verts_per_elem, ent_type, 1, start_elem, conn_array );MB_CHK_SET_ERR( result, fileName << ": Trouble reading elements" ); 00161 00162 // Read connectivity into conn_array directly 00163 for( long i = 0; i < num_elems; i++ ) 00164 { 00165 // Read connectivity 00166 } 00167 00168 // Convert file-based connectivity indices to vertex handles in-place; be careful, if indices 00169 // are smaller than handles, need to do from the end of the list so we don't overwrite data 00170 // 00171 // Here, we assume indices are smaller than handles, just for demonstration; create an 00172 // integer-type pointer to connectivity initialized to same start of connectivity array 00173 int* ind_array = reinterpret_cast< int* >( conn_array ); 00174 // OFFSET is value of first vertex index in file; most files are 1-based, but some might be 00175 // 0-based 00176 int OFFSET = 1; 00177 for( long i = num_elems * verts_per_elem - 1; i >= 0; i-- ) 00178 { 00179 conn_array[i] = ind_array[i] + start_vertex + OFFSET; 00180 00181 // This assert assumes last handle in read_ents is highest vertex handle in this file 00182 assert( conn_array[i] >= start_vertex && conn_array[i] <= *read_ents.rbegin() ); 00183 } 00184 00185 // Notify MOAB of the new elements 00186 result = readMeshIface->update_adjacencies( start_elem, num_elems, verts_per_elem, conn_array ); 00187 if( MB_SUCCESS != result ) return result; 00188 00189 // Add elements to read_ents 00190 if( num_elems ) read_ents.insert( start_elem, start_elem + num_elems - 1 ); 00191 00192 return MB_SUCCESS; 00193 } 00194 00195 //! Read/create sets 00196 ErrorCode ReadTemplate::create_sets( int num_sets, EntityHandle /*start_vertex*/, int /*num_verts*/, 00197 EntityHandle /*start_elem*/, int /*num_elems*/, Range& read_ents ) 00198 { 00199 ErrorCode result = MB_SUCCESS; 00200 EntityHandle this_set; 00201 00202 for( int i = 0; i < num_sets; i++ ) 00203 { 00204 // Create set 00205 result = mbImpl->create_meshset( MESHSET_SET, this_set );MB_CHK_SET_ERR( result, fileName << ": Trouble creating set" ); 00206 00207 Range set_ents; 00208 // Read/compute what's in this set; REMEMBER TO CONVERT THESE TO MOAB HANDLES 00209 00210 // Add them to the set 00211 result = mbImpl->add_entities( this_set, set_ents );MB_CHK_SET_ERR( result, fileName << ": Trouble putting entities in set" ); 00212 00213 // Add the new set to read_ents 00214 read_ents.insert( this_set ); 00215 } 00216 00217 return MB_SUCCESS; 00218 } 00219 00220 ErrorCode ReadTemplate::process_options( const FileOptions& opts ) 00221 { 00222 // Mark all options seen, to avoid compile warning on unused variable 00223 opts.mark_all_seen(); 00224 return MB_SUCCESS; 00225 } 00226 00227 } // namespace moab