moab
moab::ReadTemplate Class Reference

Template for implementing new file readers in MOAB This class is a template for writing new file readers in MOAB. This shows how to efficiently create vertices and elements using the ReadUtilIface class, and to translate indices in connectivity lists into vertex handles created during the read. More...

#include <ReadTemplate.hpp>

Inheritance diagram for moab::ReadTemplate:
moab::ReaderIface

List of all members.

Public Member Functions

ErrorCode load_file (const char *file_name, const EntityHandle *file_set, const FileOptions &opts, const SubsetList *subset_list=0, const Tag *file_id_tag=0)
 Load mesh from a file.
ErrorCode read_tag_values (const char *file_name, const char *tag_name, const FileOptions &opts, std::vector< int > &tag_values_out, const SubsetList *subset_list=0)
 Read tag values from a file.
 ReadTemplate (Interface *impl=NULL)
 Constructor.
virtual ~ReadTemplate ()
 Destructor.

Static Public Member Functions

static ReaderIfacefactory (Interface *)
 factory method

Private Member Functions

ErrorCode read_vertices (int num_verts, EntityHandle &start_vertex, Range &read_ents)
 Read vertex data and create vertices in MOAB database.
ErrorCode read_elements (int num_elems, EntityHandle start_vertex, EntityHandle &start_elem, Range &read_ents)
 Read element data and create elements in MOAB database.
ErrorCode create_sets (int num_sets, EntityHandle start_vertex, int num_verts, EntityHandle start_elem, int num_elems, Range &read_ents)
 Read entity set data and create/populate sets in MOAB database.
ErrorCode process_options (const FileOptions &opts)
 Process options passed into the reader.

Private Attributes

ReadUtilIfacereadMeshIface
InterfacembImpl
 interface instance
const char * fileName

Detailed Description

Template for implementing new file readers in MOAB This class is a template for writing new file readers in MOAB. This shows how to efficiently create vertices and elements using the ReadUtilIface class, and to translate indices in connectivity lists into vertex handles created during the read.

After writing the new reader class, you should also modify src/ReaderWriterSet.cpp, to register the new reader along with the file extensions that it reads. This will turn on automatic creating of this reader based on file extension, which is done in Core::serial_load_file.

Definition at line 22 of file ReadTemplate.hpp.


Constructor & Destructor Documentation

Constructor.

Definition at line 21 of file ReadTemplate.cpp.

Destructor.

Definition at line 27 of file ReadTemplate.cpp.


Member Function Documentation

ErrorCode ReadTemplate::create_sets ( int  num_sets,
EntityHandle  start_vertex,
int  num_verts,
EntityHandle  start_elem,
int  num_elems,
Range read_ents 
) [private]

Read entity set data and create/populate sets in MOAB database.

read/create sets

Parameters:
num_setsNumber of sets to be read
start_vertexStarting vertex handle
num_vertsTotal number of vertices read from file
start_elemStarting element handle
num_elemsTotal number of elements read from file
read_entsRange storing all entities read from this file

Definition at line 192 of file ReadTemplate.cpp.

{ 
  ErrorCode result = MB_SUCCESS;
  EntityHandle this_set;
  
  for (int i = 0; i < num_sets; i++) {
      // create set
    result = mbImpl->create_meshset(MESHSET_SET, this_set);
    if (MB_SUCCESS != result) {
      readMeshIface->report_error("%s: Trouble creating set.\n", fileName);
      return result;
    }

    Range set_ents;
      // read/compute what's in this set; REMEMBER TO CONVERT THESE TO MOAB HANDLES

      // add them to the set
    result = mbImpl->add_entities(this_set, set_ents);
    if (MB_SUCCESS != result) {
      readMeshIface->report_error("%s: Trouble putting entities in set.\n", fileName);
      return result;
    }

      // add the new set to read_ents
    read_ents.insert(this_set);
  }
    
  return MB_SUCCESS;
}
ReaderIface * ReadTemplate::factory ( Interface iface) [static]

factory method

Definition at line 18 of file ReadTemplate.cpp.

  { return new ReadTemplate(iface); }
ErrorCode ReadTemplate::load_file ( const char *  file_name,
const EntityHandle file_set,
const FileOptions opts,
const SubsetList subset_list = 0,
const Tag file_id_tag = 0 
) [virtual]

Load mesh from a file.

Method all readers must provide to import a mesh.

Parameters:
file_nameThe file to read.
file_setOptional pointer to entity set representing file. If this is not NULL, reader may optionally tag the pointed-to set with format-specific meta-data.
subset_listAn optional struct pointer specifying the tags identifying entity sets to be read.
file_id_tagIf specified, reader should store for each entity it reads, a unique integer ID for this tag.
Author:
Jason Kraftcheck

Implements moab::ReaderIface.

Definition at line 46 of file ReadTemplate.cpp.

{
  if (subset_list) {
      // see src/moab/ReaderIface.hpp, definition of SubsetList struct; this basically specifies
      // an integer tag and tag values for sets to read on this proc, or a part number and total # parts
      // for reading a trivial partition of entities
  }

    // save filename to member variable so we don't need to pass as an argument
    // to called functions
  fileName = filename;

    // process options; see src/FileOptions.hpp for API for FileOptions class, and doc/metadata_info.doc for
    // a description of various options used by some of the readers in MOAB
  ErrorCode result = process_options(opts);
  if (MB_SUCCESS != result) {
    readMeshIface->report_error( "%s: problem reading options\n", fileName);
    return result;
  }

    // Open file; filePtr is member of ReadTemplate, change to whatever mechanism is used to identify file
  FILE* filePtr = fopen( fileName, "r" );
  if (!filePtr)
  {
    readMeshIface->report_error( "%s: fopen returned error.\n", fileName);
    return MB_FILE_DOES_NOT_EXIST;
  }
  
    // read number of verts, elements, sets
  long num_verts = 0, num_elems = 0, num_sets = 0;

    // read_ents keeps a running set of entities read from this file, including vertices, elements, and sets;
    // these will get added to file_set (if input) at the end of the read
  Range read_ents;
  
    // start_vertex is passed back so we know how to convert indices from the file into vertex handles; most
    // of the time this is done by adding start_vertex to the (0-based) index; if the index is 1-based, you also
    // need to subtract one; see read_elements for details
  EntityHandle start_vertex;
  result = read_vertices(num_verts, start_vertex, read_ents);
  if (MB_SUCCESS != result) return result;

    // create/read elements; this template assumes that all elements are the same type, so can be read in a single
    // call to read_elements, and kept track of with a single start_elem handle.  If there are more entity types,
    // might have to keep these start handles in an array/vector.  start_elem is only really needed if you're reading
    // sets later, and need to convert some file-based index to an entity handle
  EntityHandle start_elem;
  result = read_elements(num_elems, start_vertex, start_elem, read_ents);
  if (MB_SUCCESS != result) return result;

    // read/create entity sets; typically these sets have some tag identifying what they're for, see doc/metadata_info.doc
    // for examples of different kinds of sets and how they're marked
  result = create_sets(num_sets, start_vertex, num_verts, start_elem, num_elems, read_ents);
  if (MB_SUCCESS != result) return result;

    // finally, add all read_ents into the file set, if one was input
  if (file_set && *file_set) {
    result = mbImpl->add_entities(*file_set, read_ents);
    if (MB_SUCCESS != result) return result;
  }
  
  return result;
}

Process options passed into the reader.

Parameters:
optsOptions passed into this read

Definition at line 223 of file ReadTemplate.cpp.

{
    // mark all options seen, to avoid compile warning on unused variable
  opts.mark_all_seen();
  return MB_SUCCESS;
}
ErrorCode ReadTemplate::read_elements ( int  num_elems,
EntityHandle  start_vertex,
EntityHandle start_elem,
Range read_ents 
) [private]

Read element data and create elements in MOAB database.

read/create elements

Parameters:
num_elemsNumber of elements to be read
start_vertexStarting vertex handle; used to offset connectivity indices
start_elemStarting element handle; may be used later to offset set entities
read_entsRange storing all entities read from this file

Definition at line 142 of file ReadTemplate.cpp.

{
    // get the entity type being read
  EntityType ent_type = MBHEX;

    // get the number of vertices per entity
  int verts_per_elem = 8;
  
    // Create the element sequence; passes back a pointer to the internal storage for connectivity and the
    // starting entity handle
  EntityHandle* conn_array;
  ErrorCode result = readMeshIface->get_element_connect( num_elems, verts_per_elem, ent_type,
                                                         1, start_elem, conn_array );
  if (MB_SUCCESS != result) {
    readMeshIface->report_error("%s: Trouble reading elements\n", fileName);
    return result;
  }

    // read connectivity into conn_array directly
  for (long i = 0; i < num_elems; i++) {
      // read connectivity
  }

    // convert file-based connectivity indices to vertex handles in-place; be careful, if indices are smaller than handles,
    // need to do from the end of the list so we don't overwrite data
    //
    // here, we assume indices are smaller than handles, just for demonstration; create an integer-type pointer to connectivity
    // initialized to same start of connectivity array
  int *ind_array = reinterpret_cast<int*>(conn_array);
    // OFFSET is value of first vertex index in file; most files are 1-based, but some might be 0-based
  int OFFSET = 1;
  for (long i = num_elems*verts_per_elem-1; i >= 0; i--) {
    conn_array[i] = ind_array[i] + start_vertex + OFFSET;

      // this assert assumes last handle in read_ents is highest vertex handle in this file
    assert(conn_array[i] >= start_vertex && conn_array[i] <= *read_ents.rbegin());
  }
  
    // notify MOAB of the new elements
  result = readMeshIface->update_adjacencies(start_elem, num_elems, verts_per_elem, conn_array);
  if (MB_SUCCESS != result) return result;

    // add elements to read_ents
  if (num_elems) read_ents.insert(start_elem, start_elem+num_elems-1);
  
  return MB_SUCCESS;
}
ErrorCode ReadTemplate::read_tag_values ( const char *  file_name,
const char *  tag_name,
const FileOptions opts,
std::vector< int > &  tag_values_out,
const SubsetList subset_list = 0 
) [virtual]

Read tag values from a file.

Read the list if all integer tag values from the file for a tag that is a single integer value per entity.

Parameters:
file_nameThe file to read.
tag_nameThe tag for which to read values
tag_values_outOutput: The list of tag values.
subset_listAn array of tag name and value sets specifying the subset of the file to read. If multiple tags are specified, the sets that match all tags (intersection) should be read.
subset_list_lengthThe length of the 'subset_list' array.

Implements moab::ReaderIface.

Definition at line 36 of file ReadTemplate.cpp.

{
  return MB_NOT_IMPLEMENTED;
}
ErrorCode ReadTemplate::read_vertices ( int  num_verts,
EntityHandle start_vertex,
Range read_ents 
) [private]

Read vertex data and create vertices in MOAB database.

Parameters:
num_vertsNumber of vertices to be read
start_vertexStarting vertex handle; used later to offset connectivity indices
read_entsRange storing all entities read from this file

Definition at line 114 of file ReadTemplate.cpp.

{
    // allocate nodes; these are allocated in one shot, get contiguous handles starting with start_handle,
    // and the reader is passed back double*'s pointing to MOAB's native storage for vertex coordinates
    // for those verts
  std::vector<double*> coord_arrays;
  ErrorCode result = readMeshIface->get_node_coords( 3, num_verts, 1, start_vertex, coord_arrays );
  if (MB_SUCCESS != result) {
    readMeshIface->report_error("%s: Trouble reading vertices\n", fileName);
    return result;
  }

    // fill in vertex coordinate arrays
  double *x = coord_arrays[0], 
         *y = coord_arrays[1],
         *z = coord_arrays[2];
  for(long i = 0; i < num_verts; ++i) {
      // read x/y/z; do something with them for now to avoid warning
    if (x || y || z) {}
    
  }

  if (num_verts) read_ents.insert(start_vertex, start_vertex + num_verts - 1);
  
  return result;
}

Member Data Documentation

const char* moab::ReadTemplate::fileName [private]

Definition at line 88 of file ReadTemplate.hpp.

interface instance

Definition at line 86 of file ReadTemplate.hpp.

Definition at line 83 of file ReadTemplate.hpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines