Sierra Toolkit Version of the Day
Relation.hpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 
00010 #ifndef stk_mesh_Relation_hpp
00011 #define stk_mesh_Relation_hpp
00012 
00013 #include <iosfwd>
00014 #include <limits>
00015 
00016 #include <stk_mesh/base/EntityKey.hpp>
00017 
00018 namespace stk {
00019 namespace mesh {
00020 
00024 //----------------------------------------------------------------------
00047 class Relation {
00048 public:
00049   typedef uint32_t raw_relation_id_type ;
00050   typedef uint32_t attribute_type;
00051 
00053   ~Relation() {}
00054 
00056   Relation() : m_raw_relation(), m_attribute(), m_entity(NULL) {}
00057 
00059   Relation( const Relation & r )
00060     : m_raw_relation( r.m_raw_relation ), m_attribute(r.m_attribute), m_entity(r.m_entity) {}
00061 
00063   Relation & operator = ( const Relation & r )
00064   {
00065     if( this != &r ) {
00066       m_raw_relation = r.m_raw_relation ;
00067       m_attribute    = r.m_attribute ;
00068       m_entity       = r.m_entity ;
00069     }
00070     return *this ;
00071   }
00072 
00076   Relation( Entity & entity , RelationIdentifier identifier );
00077 
00078   attribute_type   attribute() const { return m_attribute; }
00079   void set_attribute(attribute_type attr) const  { m_attribute = attr; }
00080 
00082   static raw_relation_id_type raw_relation_id( unsigned rank , unsigned id );
00083 
00085   raw_relation_id_type raw_relation_id() const { return m_raw_relation.value ; }
00086 
00088   unsigned entity_rank() const ;
00089 
00091   RelationIdentifier identifier() const ;
00092 
00094   Entity * entity() const { return m_entity ; }
00095 
00097   bool operator == ( const Relation & r ) const
00098     { return m_raw_relation.value == r.m_raw_relation.value && m_entity == r.m_entity ; }
00099 
00101   bool operator != ( const Relation & r ) const
00102     { return m_raw_relation.value != r.m_raw_relation.value || m_entity != r.m_entity ; }
00103 
00105   bool operator < ( const Relation & r ) const ;
00106 
00107 private:
00108 
00109   enum { entity_rank_ok = 1 / (!!(EntityKey::rank_digits == 8)) };
00110   enum {
00111     rank_digits = 8  ,
00112     id_digits   = 24 ,
00113     id_mask     = ~(0u) >> rank_digits
00114   };
00115 
00116   union RawRelationType {
00117   public:
00118     raw_relation_id_type value ;
00119 
00120     struct {
00121       raw_relation_id_type identifier  : id_digits ;
00122       raw_relation_id_type entity_rank : rank_digits ;
00123     } normal_view ;
00124 
00125     struct {
00126       raw_relation_id_type entity_rank : rank_digits ;
00127       raw_relation_id_type identifier  : id_digits ;
00128     } reverse_view ;
00129 
00130     RawRelationType( raw_relation_id_type v ) : value(v) {}
00131     RawRelationType() : value(0) {}
00132     RawRelationType( const RawRelationType & rhs ) : value( rhs.value ) {}
00133     RawRelationType & operator = ( const RawRelationType & rhs )
00134       { value = rhs.value ; return *this ; }
00135   };
00136 
00137   RawRelationType   m_raw_relation ;
00138   mutable attribute_type    m_attribute ;
00139   Entity          * m_entity ;
00140 };
00141 
00142 //----------------------------------------------------------------------
00143 
00144 inline
00145 Relation::raw_relation_id_type
00146 Relation::raw_relation_id( unsigned rank , unsigned id )
00147 {
00148   ThrowAssertMsg( id <= id_mask,
00149                   "For args rank " << rank << ", id " << id << ": " <<
00150                   "id " << " > id_mask=" << id_mask );
00151 
00152   return ( raw_relation_id_type(rank) << id_digits ) | id ;
00153 }
00154 
00155 inline
00156 unsigned Relation::entity_rank() const
00157 { return m_raw_relation.value >> id_digits; }
00158 
00159 inline
00160 RelationIdentifier Relation::identifier() const
00161 { return unsigned( m_raw_relation.value & id_mask ); }
00162 
00163 struct LessRelation {
00164   bool operator() ( const Relation & lhs , const Relation & rhs ) const
00165     { return lhs < rhs ; }
00166 
00167   bool operator() ( const Relation & lhs , Relation::raw_relation_id_type rhs ) const
00168     { return lhs.raw_relation_id() < rhs ; }
00169 };
00170 
00171 //----------------------------------------------------------------------
00175 void get_entities_through_relations(
00176   const std::vector<Entity*> & entities ,
00177         std::vector<Entity*> & entities_related );
00178 
00183 void get_entities_through_relations(
00184   const std::vector<Entity*> & entities ,
00185         EntityRank             entities_related_rank ,
00186         std::vector<Entity*> & entities_related );
00187 
00188 //----------------------------------------------------------------------
00192 bool membership_is_induced( const Part & part , unsigned entity_rank );
00193 
00197 void induced_part_membership( Part & part ,
00198                               unsigned entity_rank_from ,
00199                               unsigned entity_rank_to ,
00200                               RelationIdentifier relation_identifier ,
00201                               PartVector & induced_parts );
00202 
00206 void induced_part_membership( const Entity           & entity_from ,
00207                               const PartVector       & omit ,
00208                                     unsigned           entity_rank_to ,
00209                                     RelationIdentifier relation_identifier ,
00210                                     PartVector       & induced_parts );
00211 
00215 void induced_part_membership( const Entity     & entity ,
00216                               const PartVector & omit ,
00217                                     PartVector & induced_parts );
00218 
00219 
00220 //----------------------------------------------------------------------
00221 
00222 #if 0
00223 
00224 std::ostream &
00225 print_relation( std::ostream & , Relation::raw__attr_type );
00226 
00228 std::ostream &
00229 print_relation( std::ostream & , const MetaData & ,
00230                 Relation::raw__attr_type , EntityKey );
00231 #endif
00232 
00234 std::ostream & operator << ( std::ostream & , const Relation & );
00235 
00238 } // namespace mesh
00239 } // namespace stk
00240 
00241 //----------------------------------------------------------------------
00242 //----------------------------------------------------------------------
00243 
00244 #endif /* stk_mesh_Relation_hpp */
00245 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends