MOAB: Mesh Oriented datABase  (version 5.3.0)
MeshImpl.hpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2004 Sandia Corporation and Argonne National
00005     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
00006     with Sandia Corporation, the U.S. Government retains certain
00007     rights in this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     diachin2@llnl.gov, djmelan@sandia.gov, mbrewer@sandia.gov,
00024     pknupp@sandia.gov, tleurent@mcs.anl.gov, tmunson@mcs.anl.gov  ,
00025     kraftche@cae.wisc.edu
00026 
00027   ***************************************************************** */
00028 /*!
00029   \file   MeshImpl.hpp
00030   \brief
00031 
00032   \author Darryl Melander
00033   \author Thomas Leurent
00034   \author Jason Kraftcheck
00035   \date   2003-04-17
00036 */
00037 
00038 #ifndef MESQUITE_MESH_IMPL_HPP
00039 #define MESQUITE_MESH_IMPL_HPP
00040 
00041 #include "MeshInterface.hpp"
00042 
00043 #include <map>
00044 #include <iosfwd>
00045 
00046 namespace MBMesquite
00047 {
00048 class FileTokenizer;
00049 class MeshImplData;
00050 class MeshImplTags;
00051 struct TagDescription;
00052 
00053 /*!  \class MeshImpl
00054 
00055 \brief MeshImpl is a Mesquite implementation of the Mesh interface.
00056 Applications can also provide their own implementation of the interface.
00057 
00058 MeshImpl can read in mesh files in VTK format and ExodusII format.
00059 */
00060 class MESQUITE_EXPORT MeshImpl : public MBMesquite::Mesh
00061 {
00062   public:
00063     //********* Functions that are NOT inherited ************
00064 
00065     MeshImpl();
00066 
00067     virtual ~MeshImpl();
00068 
00069     /**\brief Initialize mesh by copying data from arrays.
00070      *
00071      *\param num_vertex  Number of vertices in the mesh
00072      *\param num_elem    Number of elements in the mesh
00073      *\param entity_topology Type of all elements in the mesh
00074      *\param fixed        Value of fixed flag for each vertex
00075      *\param coords      Interleaved vertex coordinates.
00076      *\param conn        Element corner vertices specified as indices into
00077      *                   vertex list
00078      */
00079     MeshImpl( int num_vertex, int num_elem, EntityTopology entity_topology, const bool* fixed, const double* coords,
00080               const int* conn );
00081 
00082     /**\brief Initialize mesh by copying data from arrays.
00083      *
00084      *\param num_vertex  Number of vertices in the mesh
00085      *\param num_elem    Number of elements in the mesh
00086      *\param entity_topologies The types of each element in the mesh
00087      *\param fixed       Value of fixed flag for each vertex
00088      *\param coords      Interleaved vertex coordinates.
00089      *\param conn        Element corner vertices specified as indices into
00090      *                   vertex list
00091      */
00092     MeshImpl( int num_vertex, int num_elem, const EntityTopology* element_topologies, const bool* fixed,
00093               const double* coords, const int* conn );
00094 
00095     /**\brief Read mesh from VTK file format version 3.0 or earlier */
00096     void read_vtk( const char* in_filename, MBMesquite::MsqError& err );
00097 
00098     /**\brief Write mesh to VTK file format version 3.0 */
00099     void write_vtk( const char* out_filename, MBMesquite::MsqError& err );
00100 
00101     /**\brief Read mesh from ExodusII file */
00102     void read_exodus( const char* in_filename, MBMesquite::MsqError& err );
00103 
00104     /**\brief Write mesh to ExodusII file */
00105     void write_exodus( const char* out_filename, MBMesquite::MsqError& err );
00106 
00107     /**\brief Set the value returned by vertices_get_fixed_flag for all vertices */
00108     void set_all_fixed_flags( bool value, MsqError& err );
00109 
00110     /**\brief Set the value returned by vertices_get_slaved_flag for all vertices
00111      *
00112      * Set value for vertices_get_slaved_flag for all corners nodes to false and
00113      * for all other nodes to the passed value.
00114      *
00115      *\param value Value to set on mid-edge, mid-face, or mid-region nodes.
00116      *
00117      *\NOTE You must change slave vertex mode to Settings::SLAVED_FLAG for the values
00118      *      returned by vertices_get_slaved_flag to be used during optimization.
00119      */
00120     void set_all_slaved_flags( bool value, MsqError& err );
00121 
00122     /**\brief Set values for vertices_get_fixed_flag and vertices_get_slaved_flag
00123      * on skin vertices
00124      *
00125      * Set flag values for vertices on the skin (i.e. boundary) of the mesh.
00126      * Does not modify flags on iterior vertices.  Call \c set_all_fixed _flags
00127      * and \c set_all_slaved_flags *before* calling this function to set values
00128      * on interior vertices.
00129      *\param corner_fixed_flag  Value for vertices_get_fixed_flag for vertices at element corners
00130      *\param midnode_fixed_flag  Value for vertices_get_fixed_flag for non-corner
00131      *                           vertices (i.e. mid-face or mid-element nodes)
00132      *\param midnode_slaved_flag  Value for vertices_get_slaved_flag for non-corner
00133      *                            vertices (i.e. mid-face or mid-element nodes)
00134      *
00135      *\NOTE You must change slave vertex mode to Settings::SLAVED_FLAG for the values
00136      *      returned by vertices_get_slaved_flag to be used during optimization.
00137      */
00138     void set_skin_flags( bool corner_fixed_flag, bool midnode_fixed_flag, bool midnode_slaved_flag, MsqError& err );
00139 
00140     /**\brief Find the vertices in the skin (i.e bounary) of the mesh and
00141      * mark them as 'fixed'.
00142      *\param clear_existing  If true only skin vertices are marked as fixed.
00143      *                       If false, skin vertices will be marked as fixed
00144      *                       in addition to any other vertices already marked
00145      *                       as fixed.
00146      */
00147     void mark_skin_fixed( MsqError& err, bool clear_existing = true );
00148 
00149     //********* Functions that ARE inherited ************
00150     // Returns whether this mesh lies in a 2D or 3D coordinate system.
00151     virtual int get_geometric_dimension( MsqError& err );
00152 
00153     /** \brief Get all elements in mesh
00154      *
00155      * Get the handles of every element in the active mesh.
00156      */
00157     virtual void get_all_elements( std::vector< ElementHandle >& handles, MsqError& err );
00158 
00159     /** \brief Get all vertices in mesh
00160      *
00161      * Get the handles of every vertex in the active mesh
00162      */
00163     virtual void get_all_vertices( std::vector< VertexHandle >& vertices, MsqError& err );
00164 
00165     // Returns a pointer to an iterator that iterates over the
00166     // set of all vertices in this mesh.  The calling code should
00167     // delete the returned iterator when it is finished with it.
00168     // If vertices are added or removed from the Mesh after obtaining
00169     // an iterator, the behavior of that iterator is undefined.
00170     virtual VertexIterator* vertex_iterator( MsqError& err );
00171 
00172     // Returns a pointer to an iterator that iterates over the
00173     // set of all top-level elements in this mesh.  The calling code should
00174     // delete the returned iterator when it is finished with it.
00175     // If elements are added or removed from the Mesh after obtaining
00176     // an iterator, the behavior of that iterator is undefined.
00177     virtual ElementIterator* element_iterator( MsqError& err );
00178 
00179     //************ Vertex Properties ********************
00180     //! Returns true or false, indicating whether the vertex
00181     //! is allowed to be repositioned.  True indicates that the vertex
00182     //! is fixed and cannot be moved.  Note that this is a read-only
00183     //! property; this flag can't be modified by users of the
00184     //! MBMesquite::Mesh interface.
00185     virtual void vertices_get_fixed_flag( const VertexHandle vert_array[], std::vector< bool >& fixed_flag_array,
00186                                           size_t num_vtx, MsqError& err );
00187 
00188     void vertices_set_fixed_flag( const VertexHandle vert_array[], const std::vector< bool >& fixed_flag_array,
00189                                   size_t num_vtx, MsqError& err );
00190 
00191     virtual void vertices_get_slaved_flag( const VertexHandle vert_array[], std::vector< bool >& slaved_flag_array,
00192                                            size_t num_vtx, MsqError& err );
00193 
00194     // Get/set location of a vertex
00195     virtual void vertices_get_coordinates( const Mesh::VertexHandle vert_array[], MBMesquite::MsqVertex* coordinates,
00196                                            size_t num_vtx, MsqError& err );
00197     virtual void vertex_set_coordinates( VertexHandle vertex, const Vector3D& coordinates, MsqError& err );
00198 
00199     // Each vertex has a byte-sized flag that can be used to store
00200     // flags.  This byte's value is neither set nor used by the mesh
00201     // implementation.  It is intended to be used by Mesquite algorithms.
00202     // Until a vertex's byte has been explicitly set, its value is 0.
00203     virtual void vertex_set_byte( VertexHandle vertex, unsigned char byte, MsqError& err );
00204     virtual void vertices_set_byte( const VertexHandle* vert_array, const unsigned char* byte_array, size_t array_size,
00205                                     MsqError& err );
00206 
00207     // Retrieve the byte value for the specified vertex or vertices.
00208     // The byte value is 0 if it has not yet been set via one of the
00209     // *_set_byte() functions.
00210     virtual void vertex_get_byte( const VertexHandle vertex, unsigned char* byte, MsqError& err );
00211     virtual void vertices_get_byte( const VertexHandle* vertex, unsigned char* byte_array, size_t array_size,
00212                                     MsqError& err );
00213 
00214     //**************** Vertex Topology *****************
00215 
00216     /** \brief get elements adjacent to vertices
00217      *
00218      * Get adjacency data for vertices
00219      *
00220      *\param vertex_array    Array of vertex handles specifying the
00221      *                       list of vertices to retrieve adjacency
00222      *                       data for.
00223      *\param num_vertex      Number of vertex handles in #vertex_array
00224      *\param elements     The array in which to place the handles of
00225      *                       elements adjacent to the input vertices.
00226      *\param offsets    For each vertex in #vertex_array, the
00227      *                       value in the corresponding position in this
00228      *                       array is the index into #elem_array at
00229      *                       which the adjacency list begins for that
00230      *                       vertex.
00231      */
00232     virtual void vertices_get_attached_elements( const VertexHandle* vertex_array, size_t num_vertex,
00233                                                  std::vector< ElementHandle >& elements, std::vector< size_t >& offsets,
00234                                                  MsqError& err );
00235 
00236     //*************** Element Topology *************
00237 
00238     /** \brief Get element connectivity
00239      *
00240      * Get the connectivity (ordered list of vertex handles) for
00241      * each element in the input array.
00242      *
00243      *\param elem_handles  The array of element handles for which to
00244      *                     retrieve the connectivity list.
00245      *\param num_elems     The length of #elem_handles
00246      *\param vert_handles  Array in which to place the vertex handles
00247      *                     in each elements connectivity.
00248      *\param offsets       For each element in #elem_handles, the
00249      *                     value in the same position in this array
00250      *                     is the index into #vert_handles at which
00251      *                     the connectivity list for that element begins.
00252      */
00253     virtual void elements_get_attached_vertices( const ElementHandle* elem_handles, size_t num_elems,
00254                                                  std::vector< VertexHandle >& vert_handles,
00255                                                  std::vector< size_t >& offsets, MsqError& err );
00256 
00257     // Returns the topologies of the given entities.  The "entity_topologies"
00258     // array must be at least "num_elements" in size.
00259     virtual void elements_get_topologies( const ElementHandle* element_handle_array, EntityTopology* element_topologies,
00260                                           size_t num_elements, MsqError& err );
00261 
00262     //*************** Tags  ***********
00263 
00264     /** \brief Create a tag
00265      *
00266      * Create a user-defined data type that can be attached
00267      * to any element or vertex in the mesh.  For an opaque or
00268      * undefined type, use type=BYTE and length=sizeof(..).
00269      *
00270      * \param tag_name  A unique name for the data object
00271      * \param type      The type of the data
00272      * \param length    Number of values per entity (1->scalar, >1 ->vector)
00273      * \param default_value Default value to assign to all entities - may be NULL
00274      * \return - Handle for tag definition
00275      */
00276     virtual TagHandle tag_create( const std::string& tag_name, TagType type, unsigned length, const void* default_value,
00277                                   MsqError& err );
00278 
00279     /** \brief Remove a tag and all corresponding data
00280      *
00281      * Delete a tag.
00282      */
00283     virtual void tag_delete( TagHandle handle, MsqError& err );
00284 
00285     /** \brief Get handle for existing tag, by name. */
00286     virtual TagHandle tag_get( const std::string& name, MsqError& err );
00287 
00288     /** \brief Get properites of tag
00289      *
00290      * Get data type and number of values per entity for tag.
00291      * \param handle     Tag to get properties of.
00292      * \param name_out   Passed back tag name.
00293      * \param type_out   Passed back tag type.
00294      * \param length_out Passed back number of values per entity.
00295      */
00296     virtual void tag_properties( TagHandle handle, std::string& name_out, TagType& type_out, unsigned& length_out,
00297                                  MsqError& err );
00298 
00299     /** \brief Set tag values on elements
00300      *
00301      * Set the value of a tag for a list of mesh elements.
00302      * \param handle     The tag
00303      * \param num_elems  Length of elem_array
00304      * \param elem_array Array of elements for which to set the tag value.
00305      * \param tag_data   Tag data for each element, contiguous in memory.
00306      *                   This data is expected to be
00307      *                   num_elems*tag_length*sizeof(tag_type) bytes.
00308      */
00309     virtual void tag_set_element_data( TagHandle handle, size_t num_elems, const ElementHandle* elem_array,
00310                                        const void* tag_data, MsqError& err );
00311 
00312     /** \brief Set tag values on vertices
00313      *
00314      * Set the value of a tag for a list of mesh vertices.
00315      * \param handle     The tag
00316      * \param num_elems  Length of node_array
00317      * \param node_array Array of vertices for which to set the tag value.
00318      * \param tag_data   Tag data for each element, contiguous in memory.
00319      *                   This data is expected to be
00320      *                   num_elems*tag_length*sizeof(tag_type) bytes.
00321      */
00322     virtual void tag_set_vertex_data( TagHandle handle, size_t num_elems, const VertexHandle* node_array,
00323                                       const void* tag_data, MsqError& err );
00324 
00325     /** \brief Get tag values on elements
00326      *
00327      * Get the value of a tag for a list of mesh elements.
00328      * \param handle     The tag
00329      * \param num_elems  Length of elem_array
00330      * \param elem_array Array of elements for which to get the tag value.
00331      * \param tag_data   Return buffer in which to copy tag data, contiguous
00332      *                   in memory.  This data is expected to be
00333      *                   num_elems*tag_length*sizeof(tag_type) bytes.
00334      */
00335     virtual void tag_get_element_data( TagHandle handle, size_t num_elems, const ElementHandle* elem_array,
00336                                        void* tag_data, MsqError& err );
00337 
00338     /** \brief Get tag values on vertices.
00339      *
00340      * Get the value of a tag for a list of mesh vertices.
00341      * \param handle     The tag
00342      * \param num_elems  Length of elem_array
00343      * \param elem_array Array of vertices for which to get the tag value.
00344      * \param tag_data   Return buffer in which to copy tag data, contiguous
00345      *                   in memory.  This data is expected to be
00346      *                   num_elems*tag_length*sizeof(tag_type) bytes.
00347      */
00348     virtual void tag_get_vertex_data( TagHandle handle, size_t num_elems, const VertexHandle* node_array,
00349                                       void* tag_data, MsqError& err );
00350 
00351     //**************** Memory Management ****************
00352     // Tells the mesh that the client is finished with a given
00353     // entity handle.
00354     virtual void release_entity_handles( const EntityHandle* handle_array, size_t num_handles, MsqError& err );
00355 
00356     // Instead of deleting a Mesh when you think you are done,
00357     // call release().  In simple cases, the implementation could
00358     // just call the destructor.  More sophisticated implementations
00359     // may want to keep the Mesh object to live longer than Mesquite
00360     // is using it.
00361     virtual void release();
00362 
00363     // Remove all data
00364     void clear();
00365 
00366   protected:
00367     /** Coordinate values per vertex */
00368     int numCoords;
00369 
00370     MeshImplData* myMesh;
00371     MeshImplTags* myTags;
00372 
00373   private:
00374     //**************** VTK Parsing ****************
00375 
00376     /** Read a data block from the file */
00377     void vtk_read_dataset( FileTokenizer& file, MsqError& err );
00378 
00379     /** Read structured point mesh */
00380     void vtk_read_structured_points( FileTokenizer& file, MsqError& err );
00381     /** Read structured grid mesh */
00382     void vtk_read_structured_grid( FileTokenizer& file, MsqError& err );
00383     /** Read rectilinear grid structured mesh */
00384     void vtk_read_rectilinear_grid( FileTokenizer& file, MsqError& err );
00385     /** Read polydata mesh */
00386     void vtk_read_polydata( FileTokenizer& file, MsqError& err );
00387     /** Read unstructured mesh */
00388     void vtk_read_unstructured_grid( FileTokenizer& file, MsqError& err );
00389     /** Read file-level field data */
00390     void vtk_read_field( FileTokenizer& file, MsqError& err );
00391 
00392     /** Helper function for vtk_read_polydata() - reads polygon subsection */
00393     void vtk_read_polygons( FileTokenizer& file, MsqError& err );
00394     /** Helper function for readers of structured mesh - create elements */
00395     void vtk_create_structured_elems( const long* dims, MsqError& err );
00396 
00397     /** Read attribute data for vertices */
00398     void vtk_read_point_data( FileTokenizer& file, MsqError& err );
00399     /** Read attribute data for elements */
00400     void vtk_read_cell_data( FileTokenizer& file, MsqError& err );
00401     /** Store data read in vtk_read_point_data into mesh tags */
00402     void vtk_store_point_data( const void* data, TagDescription& desc, MsqError& );
00403     /** Store data read in vtk_read_cell_data into mesh tags */
00404     void vtk_store_cell_data( const void* data, TagDescription& desc, MsqError& );
00405     /** Read actual data for both vtk_read_point_data() and vtk_read_cell_data()
00406      *  Initializes all fields of passed TagDescription
00407      *\return NULL if field data, otherwise pointer to malloc'd data.
00408      */
00409     void* vtk_read_attrib_data( FileTokenizer& file, long num_data_to_read, TagDescription& tag_out, MsqError& err );
00410     /** Read a 2-D array of data of the specified type from the file
00411      *  Initializes size and type fields of passed TagDescroption */
00412     void* vtk_read_typed_data( FileTokenizer& file, int type, size_t per_elem, size_t num_elem, TagDescription& tag_out,
00413                                MsqError& err );
00414     /** Read field data
00415      *\param count expected number of tuples, or zero if not known
00416      */
00417     void* vtk_read_field_data( FileTokenizer& file, size_t count, size_t field_count, const std::string& field_name,
00418                                TagDescription& tag, MsqError& err );
00419 
00420     /** Read scalar attribute data
00421      *  Initializes size and type fields of passed TagDescroption */
00422     void* vtk_read_scalar_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00423     /** Read color attribute data
00424      *  Initializes size and type fields of passed TagDescroption */
00425     void* vtk_read_color_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00426     /** Read vector or normal attribute data
00427      *  Initializes size and type fields of passed TagDescroption */
00428     void* vtk_read_vector_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00429     /** Read texture attribute data
00430      *  Initializes size and type fields of passed TagDescroption */
00431     void* vtk_read_texture_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00432     /** Read tensor (3x3 matrix) data
00433      *  Initializes size and type fields of passed TagDescroption */
00434     void* vtk_read_tensor_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00435 
00436     /** Write tag data to VTK attributes */
00437     void vtk_write_attrib_data( std::ostream& file, const TagDescription& desc, const void* data, size_t count,
00438                                 MsqError& err ) const;
00439 
00440     /** Convert tag data stored on vertices to boolean values.
00441      *  Deletes tag data.
00442      *  Passes back empty result vector if no tag.
00443      */
00444     void tag_to_bool( const char* tag_name, std::vector< bool >& vertex_vals, MsqError& err );
00445 
00446     //**************** End VTK Parsing ****************
00447 };
00448 }  // namespace MBMesquite
00449 
00450 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines