MOAB: Mesh Oriented datABase  (version 5.2.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 : public std::binary_function< T, T, T >
00079         {
00080             T operator()( const T a, const T b ) const
00081             {
00082                 return ( a + b ) / 2.0;
00083             }
00084         };  // Compute_center
00085     }       // namespace
00086 
00087     template < typename Vector >
00088     inline void compute_box_center( Vector& max, Vector& min, Vector& center )
00089     {
00090         typedef typename Vector::value_type Unit;
00091         center = min;
00092         std::transform( max.begin(), max.end(), center.begin(), center.begin(), Compute_center< Unit >() );
00093     }
00094 
00095     template < typename Box >
00096     inline typename Box::value_type compute_box_center( const Box& box, const int dim )
00097     {
00098         return ( box.max[dim] + box.min[dim] ) / 2.0;
00099     }
00100 
00101     template < typename T = float >
00102     class Box
00103     {
00104       public:
00105         typedef T value_type;
00106         typedef std::vector< T > Vector;
00107         Box() : max( 3, 0.0 ), min( 3, 0.0 ) {}
00108         Box( const Box& from ) : max( from.max ), min( from.min ) {}
00109         template < typename Iterator >
00110         Box( const Iterator begin, const Iterator end ) : max( begin, end ), min( begin, end )
00111         {
00112         }
00113         Box& operator=( const Box& from )
00114         {
00115             max = from.max;
00116             min = from.min;
00117             return *this;
00118         }
00119         Vector max;
00120         Vector min;
00121     };  // Box
00122 
00123     template < typename T >
00124     std::ostream& operator<<( std::ostream& out, const Box< T >& box )
00125     {
00126         out << "Max: ";
00127         print_vector( box.max, out );
00128         out << "Min: ";
00129         print_vector( box.min, out );
00130         return out;
00131     }
00132 
00133     // essentially a pair, but with an added constructor.
00134     template < typename T1, typename T2 >
00135     struct _Element_data
00136     {
00137         typedef T1 first_type;
00138         typedef T2 second_type;
00139         T1 first;
00140         T2 second;
00141         _Element_data() : first( T1() ), second( T2() ) {}
00142         _Element_data( const T1& x ) : first( x ), second( T2() ) {}
00143         _Element_data( const T1& x, T2& y ) : first( x ), second( y ) {}
00144         template < typename U, typename V >
00145         _Element_data( const _Element_data< U, V >& p ) : first( p.first ), second( p.second )
00146         {
00147         }
00148     };  // Element_data
00149 
00150     template < typename Entities, typename Iterator >
00151     void assign_entities( Entities& entities, const Iterator& begin, const Iterator& end )
00152     {
00153         entities.reserve( std::distance( begin, end ) );
00154         for( Iterator i = begin; i != end; ++i )
00155         {
00156             entities.push_back( std::make_pair( ( *i )->second.first, ( *i )->first ) );
00157         }
00158     }
00159 
00160     template < typename Coordinate, typename Coordinate_iterator >
00161     void update_bounding_max( Coordinate& max, Coordinate_iterator j )
00162     {
00163         typedef typename Coordinate::iterator Iterator;
00164         for( Iterator i = max.begin(); i != max.end(); ++i, ++j )
00165         {
00166             *i = std::max( *i, *j );
00167         }
00168     }
00169 
00170     template < typename Coordinate, typename Coordinate_iterator >
00171     void update_bounding_min( Coordinate& min, Coordinate_iterator j )
00172     {
00173         typedef typename Coordinate::iterator Iterator;
00174         for( Iterator i = min.begin(); i != min.end(); ++i, ++j )
00175         {
00176             *i = std::min( *i, *j );
00177         }
00178     }
00179 
00180     template < typename Box >
00181     void update_bounding_box( Box& a, const Box& b )
00182     {
00183         update_bounding_max( a.max, b.max.begin() );
00184         update_bounding_min( a.min, b.min.begin() );
00185 #ifdef COMMON_TREE_DEBUG
00186         if( !box_contains_box( a, b ) ) { std::cout << a << b << std::endl; }
00187 #endif
00188     }
00189 
00190     template < typename Entity_map, typename Ordering >
00191     void construct_ordering( Entity_map& entity_map, Ordering& entity_ordering )
00192     {
00193         entity_ordering.reserve( entity_map.size() );
00194         typedef typename Entity_map::iterator Map_iterator;
00195         for( Map_iterator i = entity_map.begin(); i != entity_map.end(); ++i )
00196         {
00197             entity_ordering.push_back( i );
00198         }
00199     }
00200 
00201     // Input: A bunch of entity handles
00202     // Output: A map from handle -> Data
00203     // Requirements: Data contains at least a bounding box.
00204     // And a non-default constructor which takes only a Box&
00205     template < typename Entity_handles, typename Element_map, typename Bounding_box, typename Moab >
00206     void construct_element_map( const Entity_handles& elements, Element_map& map, Bounding_box& bounding_box,
00207                                 Moab& moab )
00208     {
00209         typedef typename Element_map::mapped_type Box_data;
00210         typedef typename Entity_handles::value_type Entity_handle;
00211         typedef typename Entity_handles::iterator Entity_handles_iterator;
00212         typedef typename Box_data::first_type::value_type Unit;
00213         typedef typename std::vector< Unit > Coordinates;
00214         typedef typename Coordinates::iterator Coordinate_iterator;
00215 
00216         for( Entity_handles_iterator i = elements.begin(); i != elements.end(); ++i )
00217         {
00218             // TODO: not generic enough. Why dim != 3
00219             const int DIM    = 3;
00220             int num_vertices = 0;
00221             // Commence un-necessary deep copying.
00222             const Entity_handle* vertex_handle;
00223             moab.get_connectivity( *i, vertex_handle, num_vertices );
00224             Coordinates coordinate( DIM * num_vertices, 0.0 );
00225             moab.get_coords( vertex_handle, num_vertices, &coordinate[0] );
00226             Bounding_box box( coordinate.begin(), coordinate.begin() + 3 );
00227             if( i == elements.begin() ) { bounding_box = box; }
00228             for( Coordinate_iterator j = coordinate.begin() + DIM; j != coordinate.end(); j += DIM )
00229             {
00230                 update_bounding_max( box.max, j );
00231                 update_bounding_min( box.min, j );
00232             }
00233             update_bounding_box( bounding_box, box );
00234             map.insert( std::make_pair( *i, Box_data( box ) ) );
00235         }
00236     }
00237 
00238 }  // namespace common_tree
00239 
00240 }  // namespace moab
00241 
00242 #endif  // COMMON_TREE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines