MOAB: Mesh Oriented datABase  (version 5.3.1)
common_tree.hpp
Go to the documentation of this file.
00001 /**
00002  * common_tree.hpp
00003  * Ryan H. Lewis
00004  * (C) 2012
00005  * Functionality common to all trees.
00006  */
00007 #include <vector>
00008 #include <set>
00009 #include <iostream>
00010 #include <map>
00011 #include <algorithm>
00012 #include <bitset>
00013 #include <numeric>
00014 #include <cmath>
00015 #include <tr1/unordered_map>
00016 #include <limits>
00017 
00018 #ifndef COMMON_TREE_HPP
00019 #define COMMON_TREE_HPP
00020 #define NUM_DIM 3
00021 #define TREE_DEBUG
00022 namespace moab
00023 {
00024 namespace common_tree
00025 {
00026 
00027     template < typename T, typename Stream >
00028     void print_vector( const T& v, Stream& out )
00029     {
00030         typedef typename T::const_iterator Iterator;
00031         out << "[ ";
00032         for( Iterator i = v.begin(); i != v.end(); ++i )
00033         {
00034             out << *i;
00035             if( i + 1 != v.end() ) { out << ", "; }
00036         }
00037         out << " ]" << std::endl;
00038     }
00039 
00040 #ifdef TREE_DEBUG
00041     template < typename T >
00042     void print_vector( const T& begin, const T& end )
00043     {
00044         std::cout << "[ ";
00045         for( T i = begin; i != end; ++i )
00046         {
00047             std::cout << ( *i )->second.second.to_ulong();
00048             if( i + 1 != end ) { std::cout << ", "; }
00049         }
00050         std::cout << " ]" << std::endl;
00051     }
00052 #endif
00053 
00054     template < typename _Box, typename _Point >
00055     bool box_contains_point( const _Box& box, const _Point& p, const double tol )
00056     {
00057         for( std::size_t i = 0; i < box.min.size(); ++i )
00058         {
00059             if( p[i] < ( box.min[i] - tol ) || p[i] > ( box.max[i] ) + tol ) { return false; }
00060         }
00061         return true;
00062     }
00063 
00064     template < typename _Box >
00065     bool box_contains_box( const _Box& a, const _Box& b, const double tol )
00066     {
00067         for( std::size_t i = 0; i < a.min.size(); ++i )
00068         {
00069             if( b.min[i] < ( a.min[i] - tol ) ) { return false; }
00070             if( b.max[i] > ( a.max[i] + tol ) ) { return false; }
00071         }
00072         return true;
00073     }
00074 
00075     namespace
00076     {
00077         template < typename T >
00078         struct Compute_center
00079         {
00080             // deprecation of binary_function
00081             typedef T first_argument_type;
00082             typedef T second_argument_type;
00083             typedef T result_type;
00084 
00085             T operator()( const T a, const T b ) const
00086             {
00087                 return ( a + b ) / 2.0;
00088             }
00089         };  // Compute_center
00090     }       // namespace
00091 
00092     template < typename Vector >
00093     inline void compute_box_center( Vector& max, Vector& min, Vector& center )
00094     {
00095         typedef typename Vector::value_type Unit;
00096         center = min;
00097         std::transform( max.begin(), max.end(), center.begin(), center.begin(), Compute_center< Unit >() );
00098     }
00099 
00100     template < typename Box >
00101     inline typename Box::value_type compute_box_center( const Box& box, const int dim )
00102     {
00103         return ( box.max[dim] + box.min[dim] ) / 2.0;
00104     }
00105 
00106     template < typename T = float >
00107     class Box
00108     {
00109       public:
00110         typedef T value_type;
00111         typedef std::vector< T > Vector;
00112         Box() : max( 3, 0.0 ), min( 3, 0.0 ) {}
00113         Box( const Box& from ) : max( from.max ), min( from.min ) {}
00114         template < typename Iterator >
00115         Box( const Iterator begin, const Iterator end ) : max( begin, end ), min( begin, end )
00116         {
00117         }
00118         Box& operator=( const Box& from )
00119         {
00120             max = from.max;
00121             min = from.min;
00122             return *this;
00123         }
00124         Vector max;
00125         Vector min;
00126     };  // Box
00127 
00128     template < typename T >
00129     std::ostream& operator<<( std::ostream& out, const Box< T >& box )
00130     {
00131         out << "Max: ";
00132         print_vector( box.max, out );
00133         out << "Min: ";
00134         print_vector( box.min, out );
00135         return out;
00136     }
00137 
00138     // essentially a pair, but with an added constructor.
00139     template < typename T1, typename T2 >
00140     struct _Element_data
00141     {
00142         typedef T1 first_type;
00143         typedef T2 second_type;
00144         T1 first;
00145         T2 second;
00146         _Element_data() : first( T1() ), second( T2() ) {}
00147         _Element_data( const T1& x ) : first( x ), second( T2() ) {}
00148         _Element_data( const T1& x, T2& y ) : first( x ), second( y ) {}
00149         template < typename U, typename V >
00150         _Element_data( const _Element_data< U, V >& p ) : first( p.first ), second( p.second )
00151         {
00152         }
00153     };  // Element_data
00154 
00155     template < typename Entities, typename Iterator >
00156     void assign_entities( Entities& entities, const Iterator& begin, const Iterator& end )
00157     {
00158         entities.reserve( std::distance( begin, end ) );
00159         for( Iterator i = begin; i != end; ++i )
00160         {
00161             entities.push_back( std::make_pair( ( *i )->second.first, ( *i )->first ) );
00162         }
00163     }
00164 
00165     template < typename Coordinate, typename Coordinate_iterator >
00166     void update_bounding_max( Coordinate& max, Coordinate_iterator j )
00167     {
00168         typedef typename Coordinate::iterator Iterator;
00169         for( Iterator i = max.begin(); i != max.end(); ++i, ++j )
00170         {
00171             *i = std::max( *i, *j );
00172         }
00173     }
00174 
00175     template < typename Coordinate, typename Coordinate_iterator >
00176     void update_bounding_min( Coordinate& min, Coordinate_iterator j )
00177     {
00178         typedef typename Coordinate::iterator Iterator;
00179         for( Iterator i = min.begin(); i != min.end(); ++i, ++j )
00180         {
00181             *i = std::min( *i, *j );
00182         }
00183     }
00184 
00185     template < typename Box >
00186     void update_bounding_box( Box& a, const Box& b )
00187     {
00188         update_bounding_max( a.max, b.max.begin() );
00189         update_bounding_min( a.min, b.min.begin() );
00190 #ifdef COMMON_TREE_DEBUG
00191         if( !box_contains_box( a, b ) ) { std::cout << a << b << std::endl; }
00192 #endif
00193     }
00194 
00195     template < typename Entity_map, typename Ordering >
00196     void construct_ordering( Entity_map& entity_map, Ordering& entity_ordering )
00197     {
00198         entity_ordering.reserve( entity_map.size() );
00199         typedef typename Entity_map::iterator Map_iterator;
00200         for( Map_iterator i = entity_map.begin(); i != entity_map.end(); ++i )
00201         {
00202             entity_ordering.push_back( i );
00203         }
00204     }
00205 
00206     // Input: A bunch of entity handles
00207     // Output: A map from handle -> Data
00208     // Requirements: Data contains at least a bounding box.
00209     // And a non-default constructor which takes only a Box&
00210     template < typename Entity_handles, typename Element_map, typename Bounding_box, typename Moab >
00211     void construct_element_map( const Entity_handles& elements, Element_map& map, Bounding_box& bounding_box,
00212                                 Moab& moab )
00213     {
00214         typedef typename Element_map::mapped_type Box_data;
00215         typedef typename Entity_handles::value_type Entity_handle;
00216         typedef typename Entity_handles::iterator Entity_handles_iterator;
00217         typedef typename Box_data::first_type::value_type Unit;
00218         typedef typename std::vector< Unit > Coordinates;
00219         typedef typename Coordinates::iterator Coordinate_iterator;
00220 
00221         for( Entity_handles_iterator i = elements.begin(); i != elements.end(); ++i )
00222         {
00223             // TODO: not generic enough. Why dim != 3
00224             const int DIM    = 3;
00225             int num_vertices = 0;
00226             // Commence un-necessary deep copying.
00227             const Entity_handle* vertex_handle;
00228             moab.get_connectivity( *i, vertex_handle, num_vertices );
00229             Coordinates coordinate( DIM * num_vertices, 0.0 );
00230             moab.get_coords( vertex_handle, num_vertices, &coordinate[0] );
00231             Bounding_box box( coordinate.begin(), coordinate.begin() + 3 );
00232             if( i == elements.begin() ) { bounding_box = box; }
00233             for( Coordinate_iterator j = coordinate.begin() + DIM; j != coordinate.end(); j += DIM )
00234             {
00235                 update_bounding_max( box.max, j );
00236                 update_bounding_min( box.min, j );
00237             }
00238             update_bounding_box( bounding_box, box );
00239             map.insert( std::make_pair( *i, Box_data( box ) ) );
00240         }
00241     }
00242 
00243 }  // namespace common_tree
00244 
00245 }  // namespace moab
00246 
00247 #endif  // COMMON_TREE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines