MOAB: Mesh Oriented datABase  (version 5.2.1)
MeshImplTags.hpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2004 Lawrence Livermore National Laboratory.  Under
00005     the terms of Contract B545069 with the University of Wisconsin --
00006     Madison, Lawrence Livermore National Laboratory 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     kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 #ifndef MESQUITE_MESH_IMPL_TAGS_HPP
00028 #define MESQUITE_MESH_IMPL_TAGS_HPP
00029 
00030 #include "Mesquite.hpp"
00031 #include "MeshInterface.hpp"
00032 
00033 #include <vector>
00034 
00035 namespace MBMesquite
00036 {
00037 
00038 struct TagDescription
00039 {
00040     // The VTK attribute type the data was read from, or NONE if not VTK data.
00041     // This property is kept only so that when a vtk file is read and subseqquently
00042     // written, it can be preserved for other potential readers that actually care
00043     // about it.
00044     enum VtkType
00045     {
00046         NONE = 0,
00047         SCALAR,
00048         COLOR,
00049         VECTOR,
00050         NORMAL,
00051         TEXTURE,
00052         TENSOR,
00053         FIELD
00054     };
00055 
00056     std::string name;    //!< Tag name
00057     Mesh::TagType type;  //!< Tag data type
00058     VtkType vtkType;     //!< Attribute type from VTK file
00059     size_t size;         //!< Size of tag data (sizeof(type)*array_length)
00060     std::string member;  //!< Field member name for 1-member fields.
00061 
00062     inline TagDescription( std::string n, Mesh::TagType t, VtkType v, size_t s, std::string m )
00063         : name( n ), type( t ), vtkType( v ), size( s ), member( m )
00064     {
00065     }
00066 
00067     inline TagDescription() : type( Mesh::BYTE ), vtkType( NONE ), size( 0 ) {}
00068 
00069     inline bool operator==( const TagDescription& o ) const
00070     {
00071         return name == o.name && type == o.type && vtkType == o.vtkType && size == o.size;
00072     }
00073     inline bool operator!=( const TagDescription& o ) const
00074     {
00075         return name != o.name || type != o.type || vtkType != o.vtkType || size != o.size;
00076     }
00077 };
00078 
00079 /**\class MeshImplTags
00080  *
00081  * Store tags and tag data for Mesquite's native mesh representation.
00082  * Stores for each tag: properties, element data, and vertex data.
00083  * The tag element and vertex data sets are maps between some element
00084  * or vertex index and a tag value.
00085  */
00086 class MeshImplTags
00087 {
00088   public:
00089     ~MeshImplTags()
00090     {
00091         clear();
00092     }
00093 
00094     /** \class TagData
00095      * Store data for a single tag
00096      */
00097     struct TagData
00098     {
00099 
00100         //! tag meta data
00101         const TagDescription desc;
00102 
00103         //! per-element data, or NULL if none has been set.
00104         void* elementData;
00105 
00106         //! number of entries in elementData
00107         size_t elementCount;
00108 
00109         //! per-vertex data, or NULL if none has been set.
00110         void* vertexData;
00111 
00112         //! number of entries in vertexData
00113         size_t vertexCount;
00114 
00115         //! Default value for tag
00116         void* defaultValue;
00117 
00118         /** \brief Construct tag
00119          *\param name Tag name
00120          *\param type Tag data type
00121          *\param length Tag array length (1 for scalar/non-array)
00122          *\param default_val Default value for tag
00123          *\param vtk_type Attribute type in VTK file
00124          */
00125         inline TagData( const std::string& name, Mesh::TagType type, unsigned length, void* default_val = 0,
00126                         TagDescription::VtkType vtk_type = TagDescription::NONE, const std::string& field_member = "" )
00127             : desc( name, type, vtk_type, length * size_from_tag_type( type ), field_member ), elementData( 0 ),
00128               elementCount( 0 ), vertexData( 0 ), vertexCount( 0 ), defaultValue( default_val )
00129         {
00130         }
00131 
00132         /** \brief Construct tag
00133          *\param desc Tag description object
00134          */
00135         inline TagData( const TagDescription& descr )
00136             : desc( descr ), elementData( 0 ), elementCount( 0 ), vertexData( 0 ), vertexCount( 0 ), defaultValue( 0 )
00137         {
00138         }
00139 
00140         ~TagData();
00141     };
00142 
00143     /** \brief Get the size of the passed data type */
00144     static size_t size_from_tag_type( Mesh::TagType type );
00145 
00146     /** \brief Clear all data */
00147     void clear();
00148 
00149     /** \brief Get tag index from name */
00150     size_t handle( const std::string& name, MsqError& err ) const;
00151 
00152     /** \brief Get tag properties */
00153     const TagDescription& properties( size_t tag_handle, MsqError& err ) const;
00154 
00155     /** \brief Create a new tag
00156      *
00157      * Create a new tag with the passed properties
00158      *\param name Tag name (must be unique)
00159      *\param type Tag data type
00160      *\param length Number of values in tag (array length, 1 for scalar)
00161      *\param defval Optional default value for tag
00162      */
00163     size_t create( const std::string& name, Mesh::TagType type, unsigned length, const void* defval, MsqError& err );
00164 
00165     /** \brief Create a new tag
00166      *
00167      * Create a new tag with the passed properties
00168      */
00169     size_t create( const TagDescription& desc, const void* defval, MsqError& err );
00170 
00171     /**\brief Remove a tag */
00172     void destroy( size_t tag_index, MsqError& err );
00173 
00174     /**\brief Set tag data on elements */
00175     void set_element_data( size_t tag_handle, size_t num_indices, const size_t* elem_indices, const void* tag_data,
00176                            MsqError& err );
00177 
00178     /**\brief Set tag data on vertices */
00179     void set_vertex_data( size_t tag_handle, size_t num_indices, const size_t* elem_indices, const void* tag_data,
00180                           MsqError& err );
00181 
00182     /**\brief Get tag data on elements */
00183     void get_element_data( size_t tag_handle, size_t num_indices, const size_t* elem_indices, void* tag_data,
00184                            MsqError& err ) const;
00185 
00186     /**\brief Get tag data on vertices */
00187     void get_vertex_data( size_t tag_handle, size_t num_indices, const size_t* elem_indices, void* tag_data,
00188                           MsqError& err ) const;
00189 
00190     /**\class TagIterator
00191      *
00192      * Iterate over list of valid tag handles
00193      */
00194     class TagIterator
00195     {
00196       public:
00197         TagIterator() : tags( 0 ), index( 0 ) {}
00198         TagIterator( MeshImplTags* d, size_t i ) : tags( d ), index( i ) {}
00199         size_t operator*() const
00200         {
00201             return index + 1;
00202         }
00203         TagIterator operator++();
00204         TagIterator operator--();
00205         TagIterator operator++( int );
00206         TagIterator operator--( int );
00207         bool operator==( TagIterator other ) const
00208         {
00209             return index == other.index;
00210         }
00211         bool operator!=( TagIterator other ) const
00212         {
00213             return index != other.index;
00214         }
00215 
00216       private:
00217         MeshImplTags* tags;
00218         size_t index;
00219     };
00220     TagIterator tag_begin();
00221     TagIterator tag_end()
00222     {
00223         return TagIterator( this, tagList.size() );
00224     }
00225 
00226     /**\brief Check if any vertices have tag */
00227     bool tag_has_vertex_data( size_t index, MsqError& err );
00228     /**\brief Check if any elements have tag */
00229     bool tag_has_element_data( size_t index, MsqError& err );
00230 
00231   private:
00232     friend class MeshImplTags::TagIterator;
00233 
00234     std::vector< TagData* > tagList;
00235 };  // class MeshImplTags
00236 
00237 }  // namespace MBMesquite
00238 
00239 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines