moab
moab::WriteSmf Class Reference

#include <WriteSmf.hpp>

Inheritance diagram for moab::WriteSmf:
moab::WriterIface

List of all members.

Public Member Functions

 WriteSmf (Interface *impl)
 Constructor.
virtual ~WriteSmf ()
 Destructor.
ErrorCode write_file (const char *file_name, const bool overwrite, const FileOptions &opts, const EntityHandle *output_list, const int num_sets, const std::vector< std::string > &qa_list, const Tag *tag_list=NULL, int num_tags=0, int export_dimension=3)
 writes out a file

Static Public Member Functions

static WriterIfacefactory (Interface *)

Private Attributes

InterfacembImpl
WriteUtilIfacewriteTool

Detailed Description

Definition at line 29 of file WriteSmf.hpp.


Constructor & Destructor Documentation

Constructor.

Definition at line 54 of file WriteSmf.cpp.

  : mbImpl(impl), writeTool(0)
{
  assert(impl != NULL);
  impl->query_interface(writeTool);
}

Destructor.

Definition at line 61 of file WriteSmf.cpp.


Member Function Documentation

Definition at line 49 of file WriteSmf.cpp.

{
  return new WriteSmf(iface);
}
ErrorCode moab::WriteSmf::write_file ( const char *  file_name,
const bool  overwrite,
const FileOptions opts,
const EntityHandle output_list,
const int  num_sets,
const std::vector< std::string > &  qa_list,
const Tag tag_list = NULL,
int  num_tags = 0,
int  export_dimension = 3 
) [virtual]

writes out a file

Implements moab::WriterIface.

Definition at line 66 of file WriteSmf.cpp.

{
  ErrorCode rval;

  // Get precision for node coordinates
  int precision;
  if (MB_SUCCESS != opts.get_int_option("PRECISION", precision))
    precision = DEFAULT_PRECISION;

  // Honor overwrite flag
  if (!overwrite) {
    rval = writeTool->check_doesnt_exist(file_name);
    if (MB_SUCCESS != rval)
      return rval;
  }

  // Create file
  std::ofstream file(file_name);
  if (!file) {
    writeTool->report_error("Could not open file: %s\n", file_name);
    return MB_FILE_WRITE_ERROR;
  }
  file.precision(precision);

  // Get entities to write
  Range triangles;
  if (!output_list || !num_sets) {
    rval = mbImpl->get_entities_by_type(0, MBTRI, triangles, false);
    if (MB_SUCCESS != rval)
      return rval;

  // Somehow get all the nodes from this range, order them, uniquify, then use binary search
  }
  else {
    // get all triangles from output sets
    for (int i = 0; i < num_sets; i++)
      rval = mbImpl->get_entities_by_type(output_list[i], MBTRI, triangles, false);
  }
  // Use an array with all the connectivities in the triangles; it will be converted later to ints
  int numTriangles = triangles.size();
  int array_alloc = 3 * numTriangles;       // allocated size of 'array'
  EntityHandle* array = new EntityHandle[array_alloc]; // ptr to working array of result handles
  // Fill up array with node handles; reorder and uniquify
  if (!array)
     return MB_MEMORY_ALLOCATION_FAILED;
  int fillA = 0;
  for (Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e) {
      const EntityHandle* conn;
      int conn_len;
      rval = mbImpl->get_connectivity(*e, conn, conn_len);
      if (MB_SUCCESS != rval) {
        delete[] array;
        return rval;
      }
      if (3 != conn_len) {
        delete[] array;
        return MB_INVALID_SIZE;
      }

      for (int i = 0; i < conn_len; ++i)
        array[fillA++] = conn[i];
  }
  if (fillA != array_alloc) {
    delete[] array;
    return MB_INVALID_SIZE;
  }

  std::sort(array, array + array_alloc);
  int numNodes = std::unique(array, array + array_alloc) - array;

  file << "#$SMF 1.0\n";
  file << "#$vertices " << numNodes << std::endl;
  file << "#$faces " << numTriangles << std::endl;
  file << "# \n";
  file << "# output from MOAB \n";
  file << "# \n";

  // output first the nodes
  // num nodes??
  // write the nodes 
  double coord[3];
  for (int i = 0; i < numNodes; i++) {
    EntityHandle node_handle = array[i];

    rval = mbImpl->get_coords(&node_handle, 1, coord);
    if (rval != MB_SUCCESS) {
      delete[] array;
      return rval;
    }

    file << "v " << coord[0] << " " << coord[1] << " " << coord[2] << std::endl;
  }
  // Write faces now
  // Leave a blank line for cosmetics
  file << " \n";
  for (Range::const_iterator e = triangles.begin(); e != triangles.end(); ++e) {
    const EntityHandle* conn;
    int conn_len;
    rval = mbImpl->get_connectivity(*e, conn, conn_len);
    if (MB_SUCCESS != rval) {
      delete[] array;
      return rval;
    }
    if (3!= conn_len) {
      delete[] array;
      return MB_INVALID_SIZE;
    }
    file << "f ";
    for (int i = 0; i < conn_len; ++i) {
      int indexInArray = std::lower_bound(array, array + numNodes, conn[i]) - array;
      file << indexInArray + 1 << " ";
    }
    file << std::endl;
  }

  file.close();
  delete[] array;
  return MB_SUCCESS;
}

Member Data Documentation

Definition at line 55 of file WriteSmf.hpp.

Definition at line 56 of file WriteSmf.hpp.


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