MOAB: Mesh Oriented datABase  (version 5.4.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     [email protected]
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,
00126                         Mesh::TagType type,
00127                         unsigned length,
00128                         void* default_val                = 0,
00129                         TagDescription::VtkType vtk_type = TagDescription::NONE,
00130                         const std::string& field_member  = "" )
00131             : desc( name, type, vtk_type, length * size_from_tag_type( type ), field_member ), elementData( 0 ),
00132               elementCount( 0 ), vertexData( 0 ), vertexCount( 0 ), defaultValue( default_val )
00133         {
00134         }
00135 
00136         /** \brief Construct tag
00137          *\param desc Tag description object
00138          */
00139         inline TagData( const TagDescription& descr )
00140             : desc( descr ), elementData( 0 ), elementCount( 0 ), vertexData( 0 ), vertexCount( 0 ), defaultValue( 0 )
00141         {
00142         }
00143 
00144         ~TagData();
00145     };
00146 
00147     /** \brief Get the size of the passed data type */
00148     static size_t size_from_tag_type( Mesh::TagType type );
00149 
00150     /** \brief Clear all data */
00151     void clear();
00152 
00153     /** \brief Get tag index from name */
00154     size_t handle( const std::string& name, MsqError& err ) const;
00155 
00156     /** \brief Get tag properties */
00157     const TagDescription& properties( size_t tag_handle, MsqError& err ) const;
00158 
00159     /** \brief Create a new tag
00160      *
00161      * Create a new tag with the passed properties
00162      *\param name Tag name (must be unique)
00163      *\param type Tag data type
00164      *\param length Number of values in tag (array length, 1 for scalar)
00165      *\param defval Optional default value for tag
00166      */
00167     size_t create( const std::string& name, Mesh::TagType type, unsigned length, const void* defval, MsqError& err );
00168 
00169     /** \brief Create a new tag
00170      *
00171      * Create a new tag with the passed properties
00172      */
00173     size_t create( const TagDescription& desc, const void* defval, MsqError& err );
00174 
00175     /**\brief Remove a tag */
00176     void destroy( size_t tag_index, MsqError& err );
00177 
00178     /**\brief Set tag data on elements */
00179     void set_element_data( size_t tag_handle,
00180                            size_t num_indices,
00181                            const size_t* elem_indices,
00182                            const void* tag_data,
00183                            MsqError& err );
00184 
00185     /**\brief Set tag data on vertices */
00186     void set_vertex_data( size_t tag_handle,
00187                           size_t num_indices,
00188                           const size_t* elem_indices,
00189                           const void* tag_data,
00190                           MsqError& err );
00191 
00192     /**\brief Get tag data on elements */
00193     void get_element_data( size_t tag_handle,
00194                            size_t num_indices,
00195                            const size_t* elem_indices,
00196                            void* tag_data,
00197                            MsqError& err ) const;
00198 
00199     /**\brief Get tag data on vertices */
00200     void get_vertex_data( size_t tag_handle,
00201                           size_t num_indices,
00202                           const size_t* elem_indices,
00203                           void* tag_data,
00204                           MsqError& err ) const;
00205 
00206     /**\class TagIterator
00207      *
00208      * Iterate over list of valid tag handles
00209      */
00210     class TagIterator
00211     {
00212       public:
00213         TagIterator() : tags( 0 ), index( 0 ) {}
00214         TagIterator( MeshImplTags* d, size_t i ) : tags( d ), index( i ) {}
00215         size_t operator*() const
00216         {
00217             return index + 1;
00218         }
00219         TagIterator operator++();
00220         TagIterator operator--();
00221         TagIterator operator++( int );
00222         TagIterator operator--( int );
00223         bool operator==( TagIterator other ) const
00224         {
00225             return index == other.index;
00226         }
00227         bool operator!=( TagIterator other ) const
00228         {
00229             return index != other.index;
00230         }
00231 
00232       private:
00233         MeshImplTags* tags;
00234         size_t index;
00235     };
00236     TagIterator tag_begin();
00237     TagIterator tag_end()
00238     {
00239         return TagIterator( this, tagList.size() );
00240     }
00241 
00242     /**\brief Check if any vertices have tag */
00243     bool tag_has_vertex_data( size_t index, MsqError& err );
00244     /**\brief Check if any elements have tag */
00245     bool tag_has_element_data( size_t index, MsqError& err );
00246 
00247   private:
00248     friend class MeshImplTags::TagIterator;
00249 
00250     std::vector< TagData* > tagList;
00251 };  // class MeshImplTags
00252 
00253 }  // namespace MBMesquite
00254 
00255 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines