Main Page   Modules   Namespace List   Compound List   File List   Namespace Members   Compound Members  

Box_d_Box_d_batch_stream.C

00001 #include <algorithm>
00002 #include <CGAL/Random.h>
00003 
00004 #if BOX_D_BOX_D_BATCH_TRACE_EXECUTION
00005 #  include <CEP/intersection/Indentation.h>
00006 #  define RECURSE_ENTER  ++indent;
00007 #  define RECURSE_LEAVE  --indent;
00008 #else
00009 #  define RECURSE_ENTER  
00010 #  define RECURSE_LEAVE  
00011 #endif
00012 
00013 
00014 
00015 namespace CEP {
00016     namespace intersection {
00017 
00018 
00019 // When the point list is less than this amount, the base
00020 // case brute-force checking is invoked.  This must be >= 1.
00021 int base_case_threshold = 5000;
00022 
00023 
00024 template<class Box>
00025 inline
00026 bool does_contain( const Box& b, 
00027                    typename Box::FT low, 
00028                    typename Box::FT high, 
00029                    int dim )
00030 {
00031     return b.min(dim) <= low && high <= b.max(dim);
00032 }
00033 
00034 
00035 template <class Box>
00036 struct does_contain_pred : public std::unary_function<Box,bool>
00037 {
00038     typedef typename Box::FT FT;
00039 
00040     FT low, high;
00041     int dim;
00042 
00043     does_contain_pred( FT l, FT h, int d ) : low(l),high(h),dim(d) {};
00044 
00045     bool operator() ( const Box& b )
00046     {
00047         return does_contain( b, low, high, dim );
00048     }
00049 };
00050 
00051 
00052 
00053 /* True if (closed) d'th interval on box intersects closed interval
00054    [low,high] */
00055 template <class Box>
00056 inline 
00057 bool do_intersect( const Box& b,
00058                    typename Box::FT low, 
00059                    typename Box::FT high, 
00060                    int d )
00061 {
00062     return b.min(d) <= high && low <= b.max(d);
00063 }
00064 
00065 
00066 template <class Box>
00067 struct do_intersect_pred : public std::unary_function<Box,bool>
00068 {
00069     typedef typename Box::FT FT;
00070 
00071     FT low, high;
00072     int dim;
00073 
00074     do_intersect_pred( FT l, FT h, int d ) : low(l),high(h),dim(d) {};
00075 
00076     bool operator() ( const Box& b )
00077     {
00078         return do_intersect( b, low, high, dim );
00079     }
00080 };
00081 
00082 
00083 
00084 /* Compare cuboids' lower (leftmost) endpoint on d'th axis */
00085 template <class Box>
00086 struct less_than_fixed_value : public std::unary_function<Box,bool>
00087 {
00088     typedef typename Box::FT FT;
00089 
00090     FT bound;
00091     int dim;
00092 
00093     less_than_fixed_value( FT b, int d ) : bound(b),dim(d) {};
00094 
00095     bool operator() ( const Box& b )
00096     {
00097         return b.min(dim) < bound;
00098     }
00099 };
00100 
00101 template <class Box>
00102 struct equal_fixed_value : public std::unary_function<Box,bool>
00103 {
00104     typedef typename Box::FT FT;
00105 
00106     FT bound;
00107     int dim;
00108 
00109     equal_fixed_value( FT b, int d ) : bound(b),dim(d) {};
00110 
00111     bool operator() ( const Box& b )
00112     {
00113         return b.min(dim) == bound;
00114     }
00115 };
00116 
00117 
00118 
00119 
00120 // Computer interval that contains list of points
00121 template <class BoxInIter>
00122 void 
00123 get_interval( BoxInIter begin, BoxInIter end, int d,
00124               typename std::iterator_traits<BoxInIter>::value_type::FT& low,
00125               typename std::iterator_traits<BoxInIter>::value_type::FT& high )
00126 {
00127     typedef typename std::iterator_traits<BoxInIter>::value_type::FT FT;
00128 
00129     CGAL_assertion( begin != end );
00130 
00131     low = begin->min(d);
00132     high = begin->min(d);
00133 
00134     for( BoxInIter p = begin; p != end; ++p ) {
00135         if ( p->min(d) < low ) 
00136             low = p->min(d);
00137         else if ( p->min(d) > high )
00138             high = p->min(d);
00139     }
00140 }
00141 
00142 
00143 
00144 // Forward declaration of inner recursive procedure
00145 template< class BoxInIter, class BoxPairOutIter >
00146 void stream_inner( BoxInIter segments_begin,
00147                    BoxInIter segments_end,
00148                    typename std::iterator_traits<BoxInIter>::value_type::FT low,
00149                    typename std::iterator_traits<BoxInIter>::value_type::FT high,
00150                    BoxInIter points_begin,
00151                    BoxInIter points_end,
00152                    int d,
00153                    BoxPairOutIter out );
00154 
00155 
00156 // Outer recursive procedure
00157 template< class BoxInIter, class BoxPairOutIter >
00158 void stream_outer( BoxInIter begin1,
00159                    BoxInIter end1,
00160                    BoxInIter begin2,
00161                    BoxInIter end2,
00162                    int d,
00163                    BoxPairOutIter out )
00164 {
00165 #if BOX_D_BOX_D_BATCH_TRACE_EXECUTION
00166     RECURSE_ENTER
00167     cerr << indent << "stream_outer( d = " << d << " )" << endl;
00168     cerr << indent << "*** Box list 1 ***" << endl;
00169     for( BoxInIter i = begin1; i != end1; ++i )
00170         cerr << indent << *i << endl;
00171     cerr << indent << "*** Box list 2 ***" << endl;
00172     for( BoxInIter i = begin2; i != end2; ++i )
00173         cerr << indent << *i << endl;
00174 #endif
00175 
00176     typedef typename std::iterator_traits<BoxInIter>::value_type  Box_;
00177     typedef std::pair<Box_,Box_>                                  BoxPair;
00178     typedef typename Box_::FT                                     FT;
00179 
00180     if ( begin1 == end1 || begin2 == end2 ) {
00181         RECURSE_LEAVE
00182         return;
00183     }
00184 
00185     // On the last dimension, do a "sort and scan"
00186     if ( d == 0 ) {
00187         Box_d_Box_d_batch_scan( begin1, end1, begin2, end2, out );
00188         RECURSE_LEAVE
00189         return;
00190     }
00191 
00192     // Construct segment tree with the first set of boxes and query
00193     // with the second set, then vice-versa.
00194 
00195     FT low, high;
00196     get_interval( begin2, end2, d, low, high );
00197     stream_inner( begin1, end1,
00198                   low, high,
00199                   begin2, end2, d, out );
00200 
00201     get_interval( begin1, end1, d, low, high );
00202     stream_inner( begin2, end2,
00203                   low, high,
00204                   begin1, end1, d, out );
00205 
00206     RECURSE_LEAVE
00207 }
00208 
00209 
00210 
00211 
00212 template <class BoxInIter, class BoxPairOutIter>
00213 void Box_d_Box_d_batch_stream( BoxInIter begin1,
00214                                BoxInIter end1,
00215                                BoxInIter begin2,
00216                                BoxInIter end2,
00217                                BoxPairOutIter out )
00218 {
00219     typedef typename std::iterator_traits<BoxInIter>::value_type  Box_;
00220 
00221     stream_outer( begin1, end1, begin2, end2, 
00222                   Box_::dimension - 1, out );
00223 }
00224 
00225 
00226 // Median of three random values
00227 template<class BoxInIter>
00228 inline
00229 typename std::iterator_traits<BoxInIter>::value_type::FT
00230 approximate_median( BoxInIter begin,
00231                     int size, 
00232                     int d )
00233 {
00234     typedef typename std::iterator_traits<BoxInIter>::value_type  Box_;
00235     typedef typename Box_::FT        FT;
00236 
00237     CGAL_assertion( size > 0 );
00238 
00239     FT val0, val1, val2;
00240 
00241     if ( size > 3 ) {
00242         val0 = (begin + CGAL::default_random.get_int(0,size))->min(d);
00243         val1 = (begin + CGAL::default_random.get_int(0,size))->min(d);
00244         val2 = (begin + CGAL::default_random.get_int(0,size))->min(d);
00245     } else {
00246         if ( size == 3 ) {
00247             val0 = (begin+0)->min(d);
00248             val1 = (begin+1)->min(d);
00249             val2 = (begin+2)->min(d);
00250         } else {
00251             if ( size == 2 ) {
00252                 val0 = (begin+0)->min(d);
00253                 val1 = (begin+1)->min(d);
00254                 return std::max(val0,val1);
00255             } else {
00256                 return begin->min(d);
00257             }
00258         }
00259     }
00260 
00261     if ( val0 <= val1 ) {
00262         if ( val1 <= val2 )       // val0 <= val1 <= val2
00263             return val1;
00264         if ( val0 <= val2 )       // val0 <= val2 < val1
00265             return val2;
00266         return val0;              // val2 < val0 <= val1
00267     } else {
00268         if ( val0 <= val2 )       // val1 < val0 <= val2
00269             return val0;
00270         if ( val1 <= val2 )       // val1 <= val2 < val0
00271             return val2;
00272         return val1;              // val2 < val1 < val0
00273     }
00274     CGAL_assertion_msg(0,"should not reach here");
00275     return 0;
00276 }
00277 
00278 
00279 
00280 /* Preconditions: d > 0
00281  * All points lie in closed interval [low,high]
00282  *
00283  * Orderings of the input lists are not preserved.
00284  *
00285  * The algorithm is described in 
00286  *   Fast Software for Box Intersections
00287  *   ACM Symposium on Computational Geometry, 2000
00288  *   Afra Zomorodian and Herbert Edelsbrunner
00289  *
00290  * The first list of boxes is used as the segments (the d'th interval
00291  * of each box) that make up the tree, and the second list is treated
00292  * as the set of query points (the low point on the d'th interval).
00293  */
00294 template< class BoxInIter, class BoxPairOutIter >
00295 void 
00296 stream_inner( BoxInIter segments_begin,
00297               BoxInIter segments_end,
00298               typename std::iterator_traits<BoxInIter>::value_type::FT low,
00299               typename std::iterator_traits<BoxInIter>::value_type::FT high,
00300               BoxInIter points_begin,
00301               BoxInIter points_end,
00302               int d,
00303               BoxPairOutIter out )
00304 {
00305 #if BOX_D_BOX_D_BATCH_TRACE_EXECUTION
00306     RECURSE_ENTER
00307     cerr << indent << "stream_inner(" 
00308          << " d = " << d
00309          << " low = " << low
00310          << " high = " << high
00311          << " )" << endl;
00312     cerr << indent << "*** Segment list ***" << endl;
00313     for( BoxInIter i = segments_begin; i != segments_end; ++i )
00314         cerr << indent << *i << endl;
00315     cerr << indent << "*** Point list ***" << endl;
00316     for( BoxInIter i = points_begin; i != points_end; ++i )
00317         cerr << indent << *i << endl;
00318 #endif
00319 
00320     typedef typename std::iterator_traits<BoxInIter>::value_type  Box_;
00321     typedef typename Box_::FT        FT;
00322 
00323     int n_segments = segments_end - segments_begin;
00324     int n_points = points_end - points_begin;
00325 
00326     CGAL_assertion( low <= high );
00327 
00328     if ( n_segments == 0 || n_points == 0 ) {
00329         RECURSE_LEAVE
00330         return;
00331     }
00332 
00333     if ( n_points <= base_case_threshold ||
00334          n_segments <= base_case_threshold ) 
00335         {
00336             scan_oneway( segments_begin, segments_end,
00337                          points_begin, points_end,
00338                          d, out );
00339             RECURSE_LEAVE
00340             return;
00341         }
00342 
00343 
00344     // Compute the canonical set of segments for this "node"
00345     // of the segment tree.  All such segments are stabbed by
00346     // the point set, so recurse one dimension lower.
00347 
00348     BoxInIter cs_end = std::partition( segments_begin, segments_end,
00349                                        does_contain_pred<Box_>(low,high,d) );
00350     if ( cs_end != segments_begin ) {
00351         stream_outer( segments_begin, cs_end,
00352                       points_begin, points_end,
00353                       d-1, out );
00354     }
00355 
00356     // Consider the children of this node.  Intervals that appear
00357     // in the canonical set for this node will not appear in the set
00358     // of any successor node.  From now on, the set of intervals under
00359     // consideration is the range [cs_end,segments_end).  
00360 
00361     // Split the point set into 
00362     // [points_begin,points_mid) and 
00363     // [points_mid,points_end)
00364 
00365     FT mid = approximate_median( points_begin, n_points, d );
00366     BoxInIter points_mid = std::partition( points_begin, points_end,
00367                                            less_than_fixed_value<Box_>(mid,d) );
00368 
00369 #if BOX_D_BOX_D_BATCH_TRACE_EXECUTION
00370     cerr << indent << "midpoint value = " << mid << endl;
00371 #endif
00372 
00373     // The partition [points_begin,points_mid) contains elements
00374     // strictly < median value.  It can happen --- if there are
00375     // many points of median value --- that this partition is empty.
00376     // To correct this, we re-partition with up to n/2 points that
00377     // precisely equal the median value.
00378     if ( points_mid == points_begin ) {
00379         points_mid = std::partition( points_begin, points_end,
00380                                      equal_fixed_value<Box_>(mid,d) );
00381         CGAL_assertion( points_mid - points_begin > 0 );
00382         if ( points_mid - points_begin > n_points/2 )
00383             points_mid = points_begin + n_points/2;
00384     }
00385 
00386     BoxInIter split = std::partition( cs_end, segments_end,
00387                                       do_intersect_pred<Box_>(low,mid,d) );
00388     stream_inner( cs_end, split, low, mid,
00389                   points_begin, points_mid,
00390                   d, out );
00391 
00392     split = std::partition( cs_end, segments_end,
00393                             do_intersect_pred<Box_>(mid,high,d) );
00394     stream_inner( cs_end, split, mid, high,
00395                   points_mid, points_end,
00396                   d, out );
00397 
00398     RECURSE_LEAVE
00399 }
00400 
00401 
00402 }
00403 }
00404 

Generated on Sun Jun 8 17:38:22 2003 for CEP::intersection by doxygen1.3-rc3