MOAB: Mesh Oriented datABase  (version 5.4.1)
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     [email protected], [email protected], [email protected],
00024     [email protected], [email protected], [email protected]  ,
00025     [email protected]
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,
00080               int num_elem,
00081               EntityTopology entity_topology,
00082               const bool* fixed,
00083               const double* coords,
00084               const int* conn );
00085 
00086     /**\brief Initialize mesh by copying data from arrays.
00087      *
00088      *\param num_vertex  Number of vertices in the mesh
00089      *\param num_elem    Number of elements in the mesh
00090      *\param entity_topologies The types of each element in the mesh
00091      *\param fixed       Value of fixed flag for each vertex
00092      *\param coords      Interleaved vertex coordinates.
00093      *\param conn        Element corner vertices specified as indices into
00094      *                   vertex list
00095      */
00096     MeshImpl( int num_vertex,
00097               int num_elem,
00098               const EntityTopology* element_topologies,
00099               const bool* fixed,
00100               const double* coords,
00101               const int* conn );
00102 
00103     /**\brief Read mesh from VTK file format version 3.0 or earlier */
00104     void read_vtk( const char* in_filename, MBMesquite::MsqError& err );
00105 
00106     /**\brief Write mesh to VTK file format version 3.0 */
00107     void write_vtk( const char* out_filename, MBMesquite::MsqError& err );
00108 
00109     /**\brief Read mesh from ExodusII file */
00110     void read_exodus( const char* in_filename, MBMesquite::MsqError& err );
00111 
00112     /**\brief Write mesh to ExodusII file */
00113     void write_exodus( const char* out_filename, MBMesquite::MsqError& err );
00114 
00115     /**\brief Set the value returned by vertices_get_fixed_flag for all vertices */
00116     void set_all_fixed_flags( bool value, MsqError& err );
00117 
00118     /**\brief Set the value returned by vertices_get_slaved_flag for all vertices
00119      *
00120      * Set value for vertices_get_slaved_flag for all corners nodes to false and
00121      * for all other nodes to the passed value.
00122      *
00123      *\param value Value to set on mid-edge, mid-face, or mid-region nodes.
00124      *
00125      *\NOTE You must change slave vertex mode to Settings::SLAVED_FLAG for the values
00126      *      returned by vertices_get_slaved_flag to be used during optimization.
00127      */
00128     void set_all_slaved_flags( bool value, MsqError& err );
00129 
00130     /**\brief Set values for vertices_get_fixed_flag and vertices_get_slaved_flag
00131      * on skin vertices
00132      *
00133      * Set flag values for vertices on the skin (i.e. boundary) of the mesh.
00134      * Does not modify flags on iterior vertices.  Call \c set_all_fixed _flags
00135      * and \c set_all_slaved_flags *before* calling this function to set values
00136      * on interior vertices.
00137      *\param corner_fixed_flag  Value for vertices_get_fixed_flag for vertices at element corners
00138      *\param midnode_fixed_flag  Value for vertices_get_fixed_flag for non-corner
00139      *                           vertices (i.e. mid-face or mid-element nodes)
00140      *\param midnode_slaved_flag  Value for vertices_get_slaved_flag for non-corner
00141      *                            vertices (i.e. mid-face or mid-element nodes)
00142      *
00143      *\NOTE You must change slave vertex mode to Settings::SLAVED_FLAG for the values
00144      *      returned by vertices_get_slaved_flag to be used during optimization.
00145      */
00146     void set_skin_flags( bool corner_fixed_flag, bool midnode_fixed_flag, bool midnode_slaved_flag, MsqError& err );
00147 
00148     /**\brief Find the vertices in the skin (i.e bounary) of the mesh and
00149      * mark them as 'fixed'.
00150      *\param clear_existing  If true only skin vertices are marked as fixed.
00151      *                       If false, skin vertices will be marked as fixed
00152      *                       in addition to any other vertices already marked
00153      *                       as fixed.
00154      */
00155     void mark_skin_fixed( MsqError& err, bool clear_existing = true );
00156 
00157     //********* Functions that ARE inherited ************
00158     // Returns whether this mesh lies in a 2D or 3D coordinate system.
00159     virtual int get_geometric_dimension( MsqError& err );
00160 
00161     /** \brief Get all elements in mesh
00162      *
00163      * Get the handles of every element in the active mesh.
00164      */
00165     virtual void get_all_elements( std::vector< ElementHandle >& handles, MsqError& err );
00166 
00167     /** \brief Get all vertices in mesh
00168      *
00169      * Get the handles of every vertex in the active mesh
00170      */
00171     virtual void get_all_vertices( std::vector< VertexHandle >& vertices, MsqError& err );
00172 
00173     // Returns a pointer to an iterator that iterates over the
00174     // set of all vertices in this mesh.  The calling code should
00175     // delete the returned iterator when it is finished with it.
00176     // If vertices are added or removed from the Mesh after obtaining
00177     // an iterator, the behavior of that iterator is undefined.
00178     virtual VertexIterator* vertex_iterator( MsqError& err );
00179 
00180     // Returns a pointer to an iterator that iterates over the
00181     // set of all top-level elements in this mesh.  The calling code should
00182     // delete the returned iterator when it is finished with it.
00183     // If elements are added or removed from the Mesh after obtaining
00184     // an iterator, the behavior of that iterator is undefined.
00185     virtual ElementIterator* element_iterator( MsqError& err );
00186 
00187     //************ Vertex Properties ********************
00188     //! Returns true or false, indicating whether the vertex
00189     //! is allowed to be repositioned.  True indicates that the vertex
00190     //! is fixed and cannot be moved.  Note that this is a read-only
00191     //! property; this flag can't be modified by users of the
00192     //! MBMesquite::Mesh interface.
00193     virtual void vertices_get_fixed_flag( const VertexHandle vert_array[],
00194                                           std::vector< bool >& fixed_flag_array,
00195                                           size_t num_vtx,
00196                                           MsqError& err );
00197 
00198     void vertices_set_fixed_flag( const VertexHandle vert_array[],
00199                                   const std::vector< bool >& fixed_flag_array,
00200                                   size_t num_vtx,
00201                                   MsqError& err );
00202 
00203     virtual void vertices_get_slaved_flag( const VertexHandle vert_array[],
00204                                            std::vector< bool >& slaved_flag_array,
00205                                            size_t num_vtx,
00206                                            MsqError& err );
00207 
00208     // Get/set location of a vertex
00209     virtual void vertices_get_coordinates( const Mesh::VertexHandle vert_array[],
00210                                            MBMesquite::MsqVertex* coordinates,
00211                                            size_t num_vtx,
00212                                            MsqError& err );
00213     virtual void vertex_set_coordinates( VertexHandle vertex, const Vector3D& coordinates, MsqError& err );
00214 
00215     // Each vertex has a byte-sized flag that can be used to store
00216     // flags.  This byte's value is neither set nor used by the mesh
00217     // implementation.  It is intended to be used by Mesquite algorithms.
00218     // Until a vertex's byte has been explicitly set, its value is 0.
00219     virtual void vertex_set_byte( VertexHandle vertex, unsigned char byte, MsqError& err );
00220     virtual void vertices_set_byte( const VertexHandle* vert_array,
00221                                     const unsigned char* byte_array,
00222                                     size_t array_size,
00223                                     MsqError& err );
00224 
00225     // Retrieve the byte value for the specified vertex or vertices.
00226     // The byte value is 0 if it has not yet been set via one of the
00227     // *_set_byte() functions.
00228     virtual void vertex_get_byte( const VertexHandle vertex, unsigned char* byte, MsqError& err );
00229     virtual void vertices_get_byte( const VertexHandle* vertex,
00230                                     unsigned char* byte_array,
00231                                     size_t array_size,
00232                                     MsqError& err );
00233 
00234     //**************** Vertex Topology *****************
00235 
00236     /** \brief get elements adjacent to vertices
00237      *
00238      * Get adjacency data for vertices
00239      *
00240      *\param vertex_array    Array of vertex handles specifying the
00241      *                       list of vertices to retrieve adjacency
00242      *                       data for.
00243      *\param num_vertex      Number of vertex handles in #vertex_array
00244      *\param elements     The array in which to place the handles of
00245      *                       elements adjacent to the input vertices.
00246      *\param offsets    For each vertex in #vertex_array, the
00247      *                       value in the corresponding position in this
00248      *                       array is the index into #elem_array at
00249      *                       which the adjacency list begins for that
00250      *                       vertex.
00251      */
00252     virtual void vertices_get_attached_elements( const VertexHandle* vertex_array,
00253                                                  size_t num_vertex,
00254                                                  std::vector< ElementHandle >& elements,
00255                                                  std::vector< size_t >& offsets,
00256                                                  MsqError& err );
00257 
00258     //*************** Element Topology *************
00259 
00260     /** \brief Get element connectivity
00261      *
00262      * Get the connectivity (ordered list of vertex handles) for
00263      * each element in the input array.
00264      *
00265      *\param elem_handles  The array of element handles for which to
00266      *                     retrieve the connectivity list.
00267      *\param num_elems     The length of #elem_handles
00268      *\param vert_handles  Array in which to place the vertex handles
00269      *                     in each elements connectivity.
00270      *\param offsets       For each element in #elem_handles, the
00271      *                     value in the same position in this array
00272      *                     is the index into #vert_handles at which
00273      *                     the connectivity list for that element begins.
00274      */
00275     virtual void elements_get_attached_vertices( const ElementHandle* elem_handles,
00276                                                  size_t num_elems,
00277                                                  std::vector< VertexHandle >& vert_handles,
00278                                                  std::vector< size_t >& offsets,
00279                                                  MsqError& err );
00280 
00281     // Returns the topologies of the given entities.  The "entity_topologies"
00282     // array must be at least "num_elements" in size.
00283     virtual void elements_get_topologies( const ElementHandle* element_handle_array,
00284                                           EntityTopology* element_topologies,
00285                                           size_t num_elements,
00286                                           MsqError& err );
00287 
00288     //*************** Tags  ***********
00289 
00290     /** \brief Create a tag
00291      *
00292      * Create a user-defined data type that can be attached
00293      * to any element or vertex in the mesh.  For an opaque or
00294      * undefined type, use type=BYTE and length=sizeof(..).
00295      *
00296      * \param tag_name  A unique name for the data object
00297      * \param type      The type of the data
00298      * \param length    Number of values per entity (1->scalar, >1 ->vector)
00299      * \param default_value Default value to assign to all entities - may be NULL
00300      * \return - Handle for tag definition
00301      */
00302     virtual TagHandle tag_create( const std::string& tag_name,
00303                                   TagType type,
00304                                   unsigned length,
00305                                   const void* default_value,
00306                                   MsqError& err );
00307 
00308     /** \brief Remove a tag and all corresponding data
00309      *
00310      * Delete a tag.
00311      */
00312     virtual void tag_delete( TagHandle handle, MsqError& err );
00313 
00314     /** \brief Get handle for existing tag, by name. */
00315     virtual TagHandle tag_get( const std::string& name, MsqError& err );
00316 
00317     /** \brief Get properites of tag
00318      *
00319      * Get data type and number of values per entity for tag.
00320      * \param handle     Tag to get properties of.
00321      * \param name_out   Passed back tag name.
00322      * \param type_out   Passed back tag type.
00323      * \param length_out Passed back number of values per entity.
00324      */
00325     virtual void tag_properties( TagHandle handle,
00326                                  std::string& name_out,
00327                                  TagType& type_out,
00328                                  unsigned& length_out,
00329                                  MsqError& err );
00330 
00331     /** \brief Set tag values on elements
00332      *
00333      * Set the value of a tag for a list of mesh elements.
00334      * \param handle     The tag
00335      * \param num_elems  Length of elem_array
00336      * \param elem_array Array of elements for which to set the tag value.
00337      * \param tag_data   Tag data for each element, contiguous in memory.
00338      *                   This data is expected to be
00339      *                   num_elems*tag_length*sizeof(tag_type) bytes.
00340      */
00341     virtual void tag_set_element_data( TagHandle handle,
00342                                        size_t num_elems,
00343                                        const ElementHandle* elem_array,
00344                                        const void* tag_data,
00345                                        MsqError& err );
00346 
00347     /** \brief Set tag values on vertices
00348      *
00349      * Set the value of a tag for a list of mesh vertices.
00350      * \param handle     The tag
00351      * \param num_elems  Length of node_array
00352      * \param node_array Array of vertices for which to set the tag value.
00353      * \param tag_data   Tag data for each element, contiguous in memory.
00354      *                   This data is expected to be
00355      *                   num_elems*tag_length*sizeof(tag_type) bytes.
00356      */
00357     virtual void tag_set_vertex_data( TagHandle handle,
00358                                       size_t num_elems,
00359                                       const VertexHandle* node_array,
00360                                       const void* tag_data,
00361                                       MsqError& err );
00362 
00363     /** \brief Get tag values on elements
00364      *
00365      * Get the value of a tag for a list of mesh elements.
00366      * \param handle     The tag
00367      * \param num_elems  Length of elem_array
00368      * \param elem_array Array of elements for which to get the tag value.
00369      * \param tag_data   Return buffer in which to copy tag data, contiguous
00370      *                   in memory.  This data is expected to be
00371      *                   num_elems*tag_length*sizeof(tag_type) bytes.
00372      */
00373     virtual void tag_get_element_data( TagHandle handle,
00374                                        size_t num_elems,
00375                                        const ElementHandle* elem_array,
00376                                        void* tag_data,
00377                                        MsqError& err );
00378 
00379     /** \brief Get tag values on vertices.
00380      *
00381      * Get the value of a tag for a list of mesh vertices.
00382      * \param handle     The tag
00383      * \param num_elems  Length of elem_array
00384      * \param elem_array Array of vertices for which to get the tag value.
00385      * \param tag_data   Return buffer in which to copy tag data, contiguous
00386      *                   in memory.  This data is expected to be
00387      *                   num_elems*tag_length*sizeof(tag_type) bytes.
00388      */
00389     virtual void tag_get_vertex_data( TagHandle handle,
00390                                       size_t num_elems,
00391                                       const VertexHandle* node_array,
00392                                       void* tag_data,
00393                                       MsqError& err );
00394 
00395     //**************** Memory Management ****************
00396     // Tells the mesh that the client is finished with a given
00397     // entity handle.
00398     virtual void release_entity_handles( const EntityHandle* handle_array, size_t num_handles, MsqError& err );
00399 
00400     // Instead of deleting a Mesh when you think you are done,
00401     // call release().  In simple cases, the implementation could
00402     // just call the destructor.  More sophisticated implementations
00403     // may want to keep the Mesh object to live longer than Mesquite
00404     // is using it.
00405     virtual void release();
00406 
00407     // Remove all data
00408     void clear();
00409 
00410   protected:
00411     /** Coordinate values per vertex */
00412     int numCoords;
00413 
00414     MeshImplData* myMesh;
00415     MeshImplTags* myTags;
00416 
00417   private:
00418     //**************** VTK Parsing ****************
00419 
00420     /** Read a data block from the file */
00421     void vtk_read_dataset( FileTokenizer& file, MsqError& err );
00422 
00423     /** Read structured point mesh */
00424     void vtk_read_structured_points( FileTokenizer& file, MsqError& err );
00425     /** Read structured grid mesh */
00426     void vtk_read_structured_grid( FileTokenizer& file, MsqError& err );
00427     /** Read rectilinear grid structured mesh */
00428     void vtk_read_rectilinear_grid( FileTokenizer& file, MsqError& err );
00429     /** Read polydata mesh */
00430     void vtk_read_polydata( FileTokenizer& file, MsqError& err );
00431     /** Read unstructured mesh */
00432     void vtk_read_unstructured_grid( FileTokenizer& file, MsqError& err );
00433     /** Read file-level field data */
00434     void vtk_read_field( FileTokenizer& file, MsqError& err );
00435 
00436     /** Helper function for vtk_read_polydata() - reads polygon subsection */
00437     void vtk_read_polygons( FileTokenizer& file, MsqError& err );
00438     /** Helper function for readers of structured mesh - create elements */
00439     void vtk_create_structured_elems( const long* dims, MsqError& err );
00440 
00441     /** Read attribute data for vertices */
00442     void vtk_read_point_data( FileTokenizer& file, MsqError& err );
00443     /** Read attribute data for elements */
00444     void vtk_read_cell_data( FileTokenizer& file, MsqError& err );
00445     /** Store data read in vtk_read_point_data into mesh tags */
00446     void vtk_store_point_data( const void* data, TagDescription& desc, MsqError& );
00447     /** Store data read in vtk_read_cell_data into mesh tags */
00448     void vtk_store_cell_data( const void* data, TagDescription& desc, MsqError& );
00449     /** Read actual data for both vtk_read_point_data() and vtk_read_cell_data()
00450      *  Initializes all fields of passed TagDescription
00451      *\return NULL if field data, otherwise pointer to malloc'd data.
00452      */
00453     void* vtk_read_attrib_data( FileTokenizer& file, long num_data_to_read, TagDescription& tag_out, MsqError& err );
00454     /** Read a 2-D array of data of the specified type from the file
00455      *  Initializes size and type fields of passed TagDescroption */
00456     void* vtk_read_typed_data( FileTokenizer& file,
00457                                int type,
00458                                size_t per_elem,
00459                                size_t num_elem,
00460                                TagDescription& tag_out,
00461                                MsqError& err );
00462     /** Read field data
00463      *\param count expected number of tuples, or zero if not known
00464      */
00465     void* vtk_read_field_data( FileTokenizer& file,
00466                                size_t count,
00467                                size_t field_count,
00468                                const std::string& field_name,
00469                                TagDescription& tag,
00470                                MsqError& err );
00471 
00472     /** Read scalar attribute data
00473      *  Initializes size and type fields of passed TagDescroption */
00474     void* vtk_read_scalar_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00475     /** Read color attribute data
00476      *  Initializes size and type fields of passed TagDescroption */
00477     void* vtk_read_color_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00478     /** Read vector or normal attribute data
00479      *  Initializes size and type fields of passed TagDescroption */
00480     void* vtk_read_vector_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00481     /** Read texture attribute data
00482      *  Initializes size and type fields of passed TagDescroption */
00483     void* vtk_read_texture_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00484     /** Read tensor (3x3 matrix) data
00485      *  Initializes size and type fields of passed TagDescroption */
00486     void* vtk_read_tensor_attrib( FileTokenizer& file, long count, TagDescription& tag_out, MsqError& err );
00487 
00488     /** Write tag data to VTK attributes */
00489     void vtk_write_attrib_data( std::ostream& file,
00490                                 const TagDescription& desc,
00491                                 const void* data,
00492                                 size_t count,
00493                                 MsqError& err ) const;
00494 
00495     /** Convert tag data stored on vertices to boolean values.
00496      *  Deletes tag data.
00497      *  Passes back empty result vector if no tag.
00498      */
00499     void tag_to_bool( const char* tag_name, std::vector< bool >& vertex_vals, MsqError& err );
00500 
00501     //**************** End VTK Parsing ****************
00502 };
00503 }  // namespace MBMesquite
00504 
00505 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines