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/Arr_overlay_2.h $ 00015 // $Id: Arr_overlay_2.h 50366 2009-07-05 12:56:48Z efif $ 00016 // 00017 // 00018 // Author(s) : Baruch Zukerman <baruchzu@post.tau.ac.il> 00019 // Efi Fogel <efif@post.tau.ac.il> 00020 00021 #ifndef CGAL_ARR_OVERLAY_2_H 00022 #define CGAL_ARR_OVERLAY_2_H 00023 00028 #include <CGAL/Arrangement_on_surface_2.h> 00029 #include <CGAL/Sweep_line_2.h> 00030 #include <CGAL/Object.h> 00031 00032 #include <vector> 00033 #include <boost/mpl/if.hpp> 00034 #include <boost/mpl/or.hpp> 00035 #include <boost/type_traits.hpp> 00036 #include <boost/static_assert.hpp> 00037 00038 00039 CGAL_BEGIN_NAMESPACE 00040 00058 template <class GeomTraitsA, 00059 class GeomTraitsB, 00060 class GeomTraitsRes, 00061 class TopTraitsA, 00062 class TopTraitsB, 00063 class TopTraitsRes, 00064 class OverlayTraits> 00065 void overlay (const Arrangement_on_surface_2<GeomTraitsA, TopTraitsA>& arr1, 00066 const Arrangement_on_surface_2<GeomTraitsB, TopTraitsB>& arr2, 00067 Arrangement_on_surface_2<GeomTraitsRes, TopTraitsRes>& arr_res, 00068 OverlayTraits& ovl_tr) 00069 { 00070 typedef Arrangement_on_surface_2<GeomTraitsA, TopTraitsA> ArrA; 00071 typedef Arrangement_on_surface_2<GeomTraitsB, TopTraitsB> ArrB; 00072 typedef Arrangement_on_surface_2<GeomTraitsRes, TopTraitsRes> ArrRes; 00073 00074 // some type assertions (not all, but better then nothing). 00075 BOOST_STATIC_ASSERT((boost::is_convertible< \ 00076 typename GeomTraitsA::Point_2, \ 00077 typename GeomTraitsRes::Point_2 >::value)); 00078 BOOST_STATIC_ASSERT((boost::is_convertible< \ 00079 typename GeomTraitsB::Point_2, \ 00080 typename GeomTraitsRes::Point_2 >::value)); 00081 BOOST_STATIC_ASSERT((boost::is_convertible< \ 00082 typename GeomTraitsA::X_monotone_curve_2, \ 00083 typename GeomTraitsRes::X_monotone_curve_2 >::value)); 00084 BOOST_STATIC_ASSERT((boost::is_convertible< \ 00085 typename GeomTraitsB::X_monotone_curve_2, \ 00086 typename GeomTraitsRes::X_monotone_curve_2 >::value)); 00087 00088 typedef typename TopTraitsRes::template 00089 Sweep_line_overlay_visitor<ArrA, ArrB, OverlayTraits> 00090 Ovl_visitor; 00091 00092 typedef typename Ovl_visitor::Traits_2 Ovl_traits_2; 00093 typedef typename Ovl_traits_2::X_monotone_curve_2 Ovl_x_monotone_curve_2; 00094 typedef typename Ovl_traits_2::Point_2 Ovl_point_2; 00095 00096 // The result arrangement cannot be on of the input arrangements. 00097 CGAL_precondition(((void *)(&arr_res) != (void *)(&arr1)) && 00098 ((void *)(&arr_res) != (void *)(&arr2))); 00099 00100 // Prepare a vector of extended x-monotone curves that represent all edges 00101 // in both input arrangements. Each curve is associated with a halfedge 00102 // directed from right to left. 00103 typename ArrA::Edge_const_iterator eit1; 00104 typename ArrA::Halfedge_const_handle he1, invalid_he1; 00105 typename ArrB::Edge_const_iterator eit2; 00106 typename ArrB::Halfedge_const_handle he2, invalid_he2; 00107 std::vector<Ovl_x_monotone_curve_2> xcvs_vec (arr1.number_of_edges() + 00108 arr2.number_of_edges()); 00109 unsigned int i = 0; 00110 00111 for (eit1 = arr1.edges_begin(); eit1 != arr1.edges_end(); ++eit1, i++) 00112 { 00113 he1 = eit1; 00114 if (he1->direction() != ARR_RIGHT_TO_LEFT) 00115 he1 = he1->twin(); 00116 00117 xcvs_vec[i] = Ovl_x_monotone_curve_2 (eit1->curve(), 00118 he1, 00119 invalid_he2); 00120 } 00121 00122 for (eit2 = arr2.edges_begin(); eit2 != arr2.edges_end(); ++eit2, i++) 00123 { 00124 he2 = eit2; 00125 if (he2->direction() != ARR_RIGHT_TO_LEFT) 00126 he2 = he2->twin(); 00127 00128 xcvs_vec[i] = Ovl_x_monotone_curve_2 (eit2->curve(), 00129 invalid_he1, 00130 he2); 00131 } 00132 00133 // Obtain a extended traits-class object and define the sweep-line visitor. 00134 const typename ArrRes::Traits_adaptor_2 * traits_adaptor = 00135 arr_res.traits_adaptor(); 00136 00137 /* We would like to avoid copy construction of the geometry traits class. 00138 * Copy construction is undesired, because it may results with data 00139 * duplication or even data loss. 00140 * 00141 * If the type Ovl_traits_2 is the same as the type 00142 * GeomTraits, use a reference to GeomTraits to avoid constructing a new one. 00143 * Otherwise, instantiate a local variable of the former and provide 00144 * the later as a single parameter to the constructor. 00145 * 00146 * Use the form 'A a(*b);' and not ''A a = b;' to handle the case where A has 00147 * only an implicit constructor, (which takes *b as a parameter). 00148 */ 00149 typedef Arr_traits_basic_adaptor_2< GeomTraitsRes > Geom_traits_adaptor_2; 00150 typename boost::mpl::if_< 00151 boost::is_same< Geom_traits_adaptor_2, Ovl_traits_2>, 00152 const Ovl_traits_2&, Ovl_traits_2 >:: type 00153 ex_traits(*traits_adaptor); 00154 00155 Ovl_visitor visitor (&arr1, &arr2, &arr_res, &ovl_tr); 00156 Sweep_line_2<Ovl_traits_2, 00157 Ovl_visitor, 00158 typename Ovl_visitor::Subcurve, 00159 typename Ovl_visitor::Event> 00160 sweep_line (&ex_traits, &visitor); 00161 00162 // In case both arrangement do not contain isolated vertices, go on and 00163 // overlay them. 00164 const unsigned int total_iso_verts = arr1.number_of_isolated_vertices() + 00165 arr2.number_of_isolated_vertices(); 00166 00167 if (total_iso_verts == 0) 00168 { 00169 // Clear the result arrangement and perform the sweep to construct it. 00170 arr_res.clear(); 00171 00172 sweep_line.sweep (xcvs_vec.begin(), xcvs_vec.end()); 00173 return; 00174 } 00175 00176 // Prepare a vector of extended points that represent all isolated vertices 00177 // in both input arrangements. 00178 typename ArrA::Vertex_const_iterator vit1; 00179 typename ArrA::Vertex_const_handle v1; 00180 typename ArrB::Vertex_const_iterator vit2; 00181 typename ArrB::Vertex_const_handle v2; 00182 const CGAL::Object empty_obj; 00183 std::vector<Ovl_point_2> pts_vec (total_iso_verts); 00184 00185 i = 0; 00186 for (vit1 = arr1.vertices_begin(); vit1 != arr1.vertices_end(); ++vit1) 00187 { 00188 if (vit1->is_isolated()) 00189 { 00190 v1 = vit1; 00191 pts_vec[i++] = Ovl_point_2 (vit1->point(), 00192 CGAL::make_object (v1), 00193 empty_obj); 00194 } 00195 } 00196 00197 for (vit2 = arr2.vertices_begin(); vit2 != arr2.vertices_end(); ++vit2) 00198 { 00199 if (vit2->is_isolated()) 00200 { 00201 v2 = vit2; 00202 pts_vec[i++] = Ovl_point_2 (vit2->point(), 00203 empty_obj, 00204 CGAL::make_object (v2)); 00205 } 00206 } 00207 00208 // Clear the result arrangement and perform the sweep to construct it. 00209 arr_res.clear(); 00210 00211 sweep_line.sweep (xcvs_vec.begin(), xcvs_vec.end(), 00212 pts_vec.begin(), pts_vec.end()); 00213 return; 00214 } 00215 00216 CGAL_END_NAMESPACE 00217 00218 #endif