BWAPI
|
00001 // Copyright (c) 2005 Tel-Aviv University (Israel). 00002 // All rights reserved. 00003 // 00004 // This file is part of CGAL (www.cgal.org); you may redistribute it under 00005 // the terms of the Q Public License version 1.0. 00006 // See the file LICENSE.QPL distributed with CGAL. 00007 // 00008 // Licensees holding a valid commercial license may use this file in 00009 // accordance with the commercial license agreement provided with the software. 00010 // 00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00013 // 00014 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Arrangement_on_surface_2/include/CGAL/Sweep_line_2/Sweep_line_event.h $ 00015 // $Id: Sweep_line_event.h 50366 2009-07-05 12:56:48Z efif $ 00016 // 00017 // 00018 // Author(s) : Tali Zvi <talizvi@post.tau.ac.il>, 00019 // Baruch Zukerman <baruchzu@post.tau.ac.il> 00020 // Ron Wein <wein@post.tau.ac.il> 00021 // Efi Fogel <efif@post.tau.ac.il> 00022 00023 #ifndef CGAL_SWEEP_LINE_EVENT_H 00024 #define CGAL_SWEEP_LINE_EVENT_H 00025 00030 #include <list> 00031 #include <CGAL/Sweep_line_2/Sweep_line_subcurve.h> 00032 00033 CGAL_BEGIN_NAMESPACE 00034 00051 template<class Traits_, class Subcurve_> 00052 class Sweep_line_event 00053 { 00054 public: 00055 00056 typedef Traits_ Traits_2; 00057 typedef typename Traits_2::X_monotone_curve_2 X_monotone_curve_2; 00058 typedef typename Traits_2::Point_2 Point_2; 00059 00060 // should be ok, as Traits_ has already extended by Basic_sweep_line 00061 typedef typename CGALi::Arr_complete_left_side_tag< Traits_2 >::Tag 00062 Arr_left_side_tag; 00063 typedef typename CGALi::Arr_complete_bottom_side_tag< Traits_2 >::Tag 00064 Arr_bottom_side_tag; 00065 typedef typename CGALi::Arr_complete_top_side_tag< Traits_2 >::Tag 00066 Arr_top_side_tag; 00067 typedef typename CGALi::Arr_complete_right_side_tag< Traits_2 >::Tag 00068 Arr_right_side_tag; 00069 00070 typedef Subcurve_ Subcurve; 00071 //template<typename SC> 00072 //struct SC_container { typedef std::list<SC> other; }; 00073 typedef std::list<Subcurve*> Subcurve_container; 00074 typedef typename Subcurve_container::iterator Subcurve_iterator; 00075 typedef typename Subcurve_container::reverse_iterator 00076 Subcurve_reverse_iterator; 00077 00079 enum Attribute 00080 { 00081 DEFAULT = 0, 00082 LEFT_END = 1, // A curve's left-end is on the event point. 00083 RIGHT_END = 2, // A curve's right-end is on the event point. 00084 ACTION = 4, // An action point. 00085 QUERY = 8, // A query point. 00086 INTERSECTION = 16, // Two curves intersects at their interior. 00087 WEAK_INTERSECTION = 32, // A curve's end-point is on the interior 00088 // of another curve (also may indicate overlap). 00089 OVERLAP = 64 // Endpoint of an overlap subcurve 00090 }; 00091 00092 protected: 00093 00094 // Data members: 00095 Point_2 m_point; // The point associated with the event. 00096 00097 Subcurve_container m_leftCurves; // The curves lying to the left of the 00098 // event and incident to it. 00099 00100 Subcurve_container m_rightCurves; // The curves lying to the right of the 00101 // event and incident to it, sorted by 00102 // their y-value to the right of the point. 00103 00104 char m_type; // The event type. 00105 char m_ps_x; // The boundary condition in x. 00106 char m_ps_y; // The boundary condition in y. 00107 char m_closed; // Is the event closed (associated with 00108 // a valid point. 00109 00110 public: 00111 00113 Sweep_line_event() : 00114 m_type (0), 00115 m_ps_x (static_cast<char> (ARR_INTERIOR)), 00116 m_ps_y (static_cast<char> (ARR_INTERIOR)), 00117 m_closed (1) 00118 {} 00119 00121 void init (const Point_2& point, Attribute type, 00122 Arr_parameter_space ps_x, Arr_parameter_space ps_y) 00123 { 00124 m_point = point; 00125 m_type = type; 00126 m_ps_x = static_cast<char> (ps_x); 00127 m_ps_y = static_cast<char> (ps_y); 00128 m_closed = 1; 00129 } 00130 00132 void init_at_open_boundary (Attribute type, 00133 Arr_parameter_space ps_x, 00134 Arr_parameter_space ps_y) 00135 { 00136 m_type = type; 00137 m_ps_x = ps_x; 00138 m_ps_y = ps_y; 00139 m_closed = 0; 00140 } 00141 00143 void add_curve_to_left (Subcurve *curve) 00144 { 00145 // Look for the subcurve. 00146 Subcurve_iterator iter; 00147 00148 for (iter = m_leftCurves.begin(); iter != m_leftCurves.end(); ++iter) 00149 { 00150 // Do nothing if the curve exists. 00151 if ((curve == *iter) || (*iter)->is_inner_node(curve)) 00152 return; 00153 00154 // Replace the existing curve in case of overlap. 00155 if (curve->is_inner_node(*iter)) 00156 { 00157 *iter = curve; 00158 return; 00159 } 00160 } 00161 00162 // The curve does not exist - insert it to the container. 00163 m_leftCurves.push_back (curve); 00164 return; 00165 } 00166 00168 void push_back_curve_to_left(Subcurve *curve) 00169 { 00170 m_leftCurves.push_back(curve); 00171 } 00172 00174 std::pair<bool, Subcurve_iterator> 00175 add_curve_to_right (Subcurve *curve, const Traits_2 *tr) 00176 { 00177 if (m_rightCurves.empty()) 00178 { 00179 m_rightCurves.push_back(curve); 00180 return (std::make_pair(false, m_rightCurves.begin())); 00181 } 00182 00183 // Check if its an event at open boundary, 00184 // and if so then there is no overlap 00185 //(there cannot be two non-overlap curves at the same event at open 00186 // boundary). 00187 if (!this->is_closed()) 00188 return (std::make_pair(true, m_rightCurves.begin())); 00189 00190 Subcurve_iterator iter = m_rightCurves.begin(); 00191 Comparison_result res; 00192 00193 while ((res = tr->compare_y_at_x_right_2_object() 00194 (curve->last_curve(), 00195 (*iter)->last_curve(), 00196 m_point)) == LARGER) 00197 { 00198 ++iter; 00199 if (iter == m_rightCurves.end()) 00200 { 00201 m_rightCurves.insert (iter, curve); 00202 return std::make_pair (false, --iter); 00203 } 00204 } 00205 00206 if (res == EQUAL) //overlap !! 00207 { 00208 return std::make_pair(true, iter); 00209 } 00210 00211 m_rightCurves.insert (iter, curve); 00212 return std::make_pair (false,--iter); 00213 } 00214 00220 std::pair<bool, Subcurve_iterator> 00221 add_curve_pair_to_right (Subcurve *sc1, Subcurve *sc2) 00222 { 00223 m_rightCurves.push_back(sc1); 00224 m_rightCurves.push_back(sc2); 00225 00226 Subcurve_iterator iter = m_rightCurves.end(); 00227 --iter; 00228 return (std::make_pair (false, iter)); 00229 } 00230 00232 void remove_curve_from_left (Subcurve* curve) 00233 { 00234 Subcurve_iterator iter; 00235 00236 for (iter = m_leftCurves.begin(); iter!= m_leftCurves.end(); ++iter) 00237 { 00238 if(curve->has_common_leaf (*iter)) 00239 { 00240 m_leftCurves.erase(iter); 00241 return; 00242 } 00243 } 00244 return; 00245 } 00246 00248 Subcurve_iterator left_curves_begin() 00249 { 00250 return (m_leftCurves.begin()); 00251 } 00252 00255 Subcurve_iterator left_curves_end() 00256 { 00257 return (m_leftCurves.end()); 00258 } 00259 00261 Subcurve_iterator right_curves_begin() 00262 { 00263 return (m_rightCurves.begin()); 00264 } 00265 00268 Subcurve_iterator right_curves_end() 00269 { 00270 return (m_rightCurves.end()); 00271 } 00272 00275 Subcurve_reverse_iterator right_curves_rbegin() 00276 { 00277 return (m_rightCurves.rbegin()); 00278 } 00279 00282 Subcurve_reverse_iterator right_curves_rend() 00283 { 00284 return (m_rightCurves.rend()); 00285 } 00286 00289 Subcurve_reverse_iterator left_curves_rbegin() 00290 { 00291 return (m_leftCurves.rbegin()); 00292 } 00293 00296 Subcurve_reverse_iterator left_curves_rend() 00297 { 00298 return (m_leftCurves.rend()); 00299 } 00300 00302 unsigned int number_of_left_curves() { 00303 return m_leftCurves.size(); 00304 } 00305 00307 unsigned int number_of_right_curves() 00308 { 00309 return (m_rightCurves.size()); 00310 } 00311 00313 bool has_left_curves() const 00314 { 00315 return (! m_leftCurves.empty()); 00316 } 00317 00319 bool has_right_curves() const 00320 { 00321 return (! m_rightCurves.empty()); 00322 } 00323 00328 const Point_2& point() const 00329 { 00330 CGAL_precondition (is_closed()); 00331 return (m_point); 00332 } 00333 00338 Point_2& point() 00339 { 00340 CGAL_precondition (is_closed()); 00341 return (m_point); 00342 } 00343 00348 const X_monotone_curve_2& curve () const 00349 { 00350 if (has_left_curves()) 00351 return (m_leftCurves.front()->last_curve()); 00352 00353 CGAL_assertion (has_right_curves()); 00354 return (m_rightCurves.front()->last_curve()); 00355 } 00356 00358 void set_point(const Point_2& pt) 00359 { 00360 m_point = pt; 00361 } 00362 00364 00365 bool is_left_end() const 00366 { 00367 return ((m_type & LEFT_END) != 0); 00368 } 00369 00370 bool is_right_end() const 00371 { 00372 return ((m_type & RIGHT_END) != 0); 00373 } 00374 00375 bool is_intersection() const 00376 { 00377 return ((m_type & INTERSECTION ) != 0); 00378 } 00379 00380 bool is_action() const 00381 { 00382 return ((m_type & ACTION ) != 0); 00383 } 00384 00385 bool is_query() const 00386 { 00387 return ((m_type & QUERY ) != 0); 00388 } 00389 00390 bool is_weak_intersection() const 00391 { 00392 return((m_type & WEAK_INTERSECTION) != 0); 00393 } 00394 00395 bool is_overlap() const 00396 { 00397 return ((m_type & OVERLAP ) != 0); 00398 } 00400 00402 00403 void set_left_end() 00404 { 00405 m_type |= LEFT_END; 00406 } 00407 00408 void set_right_end() 00409 { 00410 m_type |= RIGHT_END; 00411 } 00412 00413 void set_intersection() 00414 { 00415 m_type |= INTERSECTION; 00416 } 00417 00418 void set_action() 00419 { 00420 m_type |= ACTION; 00421 } 00422 00423 void set_query() 00424 { 00425 m_type |= QUERY; 00426 } 00427 00428 void set_weak_intersection() 00429 { 00430 m_type |= WEAK_INTERSECTION; 00431 } 00432 00433 void set_overlap() 00434 { 00435 m_type |= OVERLAP; 00436 } 00437 00438 void set_attribute (Attribute type) 00439 { 00440 m_type |= type; 00441 } 00443 00445 00446 inline bool is_closed() const 00447 { 00448 return (m_closed != 0); 00449 } 00450 00451 inline bool is_on_boundary () const 00452 { 00453 return (m_ps_x != static_cast<char> (ARR_INTERIOR) || 00454 m_ps_y != static_cast<char> (ARR_INTERIOR)); 00455 } 00456 00457 inline Arr_parameter_space parameter_space_in_x() const 00458 { 00459 return (Arr_parameter_space (m_ps_x)); 00460 } 00461 00462 inline Arr_parameter_space parameter_space_in_y() const 00463 { 00464 return (Arr_parameter_space (m_ps_y)); 00465 } 00467 00469 template <class InputIterator> 00470 void replace_left_curves (InputIterator begin, InputIterator end) 00471 { 00472 Subcurve_iterator left_iter = m_leftCurves.begin(); 00473 InputIterator iter; 00474 00475 for (iter = begin; iter != end; ++iter, ++left_iter) 00476 { 00477 *left_iter = static_cast<Subcurve*>(*iter); 00478 } 00479 00480 m_leftCurves.erase (left_iter, m_leftCurves.end()); 00481 return; 00482 } 00483 00484 bool is_right_curve_bigger (Subcurve* c1, Subcurve* c2) 00485 { 00486 Subcurve_iterator iter; 00487 for (iter = m_rightCurves.begin(); iter != m_rightCurves.end(); ++iter) 00488 { 00489 if (*iter == c1 || 00490 static_cast<Subcurve*>((*iter)->originating_subcurve1()) == c1 || 00491 static_cast<Subcurve*>((*iter)->originating_subcurve2()) == c1) 00492 return (false); 00493 00494 if (*iter == c2 || 00495 static_cast<Subcurve*>((*iter)->originating_subcurve1()) == c2 || 00496 static_cast<Subcurve*>((*iter)->originating_subcurve2()) == c2) 00497 return (true); 00498 } 00499 00500 return (true); 00501 } 00502 00504 bool are_left_neighbours (Subcurve* c1, Subcurve* c2) 00505 { 00506 Subcurve_iterator left_iter = m_leftCurves.begin(); 00507 00508 for( ; left_iter != m_leftCurves.end(); ++left_iter) 00509 { 00510 if (*left_iter == c1) 00511 { 00512 Subcurve_iterator temp = left_iter; 00513 ++temp; 00514 if (temp != m_leftCurves.end()) 00515 return (*temp == c2); 00516 00517 return (false); 00518 } 00519 00520 if(*left_iter == c2) 00521 { 00522 Subcurve_iterator temp = left_iter; 00523 ++temp; 00524 if(temp!=m_leftCurves.end()) 00525 return (*temp == c1); 00526 00527 return (false); 00528 } 00529 } 00530 00531 return (false); 00532 } 00533 00534 #ifdef CGAL_SL_VERBOSE 00535 void Print() ; 00536 #endif 00537 00538 }; 00539 00540 #ifdef CGAL_SL_VERBOSE 00541 template<class Traits, class Subcurve> 00542 void Sweep_line_event<Traits, Subcurve>::Print() 00543 { 00544 std::cout << "\tEvent info: " << "\n" ; 00545 if (this->is_closed()) 00546 std::cout << "\t" << m_point << "\n" ; 00547 else 00548 { 00549 std::cout << "\t"; 00550 Arr_parameter_space ps_x = this->parameter_space_in_x(); 00551 Arr_parameter_space ps_y = this->parameter_space_in_y(); 00552 00553 switch (ps_x) { 00554 case ARR_LEFT_BOUNDARY: std::cout << "left boundary"; break; 00555 case ARR_RIGHT_BOUNDARY: std::cout << "right boundary"; break; 00556 case ARR_INTERIOR: 00557 default: 00558 switch (ps_y) { 00559 case ARR_BOTTOM_BOUNDARY: std::cout << "bottom boundary"; break; 00560 case ARR_TOP_BOUNDARY: std::cout << "top boundary"; break; 00561 case ARR_INTERIOR: 00562 default: 00563 CGAL_error(); 00564 } 00565 } 00566 } 00567 std::cout<<"\n"; 00568 00569 std::cout << "\tLeft curves: \n" ; 00570 for ( Subcurve_iterator iter = m_leftCurves.begin() ; 00571 iter != m_leftCurves.end() ; ++iter ) 00572 { 00573 std::cout << "\t"; 00574 (*iter)->Print(); 00575 std::cout << "\n"; 00576 } 00577 std::cout << std::endl; 00578 std::cout << "\tRight curves: \n" ; 00579 for ( Subcurve_iterator iter1 = m_rightCurves.begin() ; 00580 iter1 != m_rightCurves.end() ; ++iter1 ) 00581 { 00582 std::cout << "\t"; 00583 (*iter1)->Print(); 00584 std::cout << "\n"; 00585 } 00586 00587 std::cout << std::endl; 00588 } 00589 #endif // CGAL_SL_VERBOSE 00590 00591 CGAL_END_NAMESPACE 00592 00593 #endif