BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Polygon_2/Polygon_2_impl.h
Go to the documentation of this file.
00001 // Copyright (c) 1997  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Polygon/include/CGAL/Polygon_2/Polygon_2_impl.h $
00019 // $Id: Polygon_2_impl.h 50252 2009-07-01 07:27:31Z efif $
00020 // 
00021 //
00022 // Author(s)     : Wieger Wesselink <wieger@cs.ruu.nl>
00023 
00024 //-----------------------------------------------------------------------//
00025 //                          operator==
00026 //-----------------------------------------------------------------------//
00027 
00028 CGAL_BEGIN_NAMESPACE
00029 
00030 namespace i_polygon {
00031 template <class Equal_2, class Point_2>
00032 class Equal_pred {
00033     Equal_2 m_equal_2;
00034     Point_2 m_pt;
00035 public:
00036     Equal_pred(Equal_2 equal_2, Point_2 const &pt)
00037     : m_equal_2(equal_2), m_pt(pt) {}
00038     bool operator()(Point_2 const &pt) const
00039     { return m_equal_2(m_pt, pt); }
00040 };
00041 }
00042 
00043 template <class Traits_P, class Container1_P, class Container2_P>
00044 bool operator==( const Polygon_2<Traits_P,Container1_P> &x,
00045                  const Polygon_2<Traits_P,Container2_P> &y )
00046 {
00047   if (&x == &y)
00048     return true;
00049   typedef typename Traits_P::Equal_2 Equal_2;
00050   typedef typename Traits_P::Point_2 Point_2;
00051 //  CGAL_polygon_precondition( (x.size() != 0) || (y.size() != 0));
00052   if ((x.size() == 0) && (y.size() == 0)) return true;
00053 
00054   if (x.size() != y.size()) return false;
00055   Equal_2 equal_2 = x.traits_member().equal_2_object();
00056   typename Polygon_2<Traits_P,Container1_P>::Vertex_const_iterator x_iter =
00057     x.vertices_begin();
00058 
00059   typename Polygon_2<Traits_P,Container2_P>::Vertex_const_iterator y_iter =
00060     std::find_if(y.vertices_begin(), y.vertices_end(),
00061     i_polygon::Equal_pred<Equal_2, Point_2>(equal_2, *x.vertices_begin()));
00062 
00063   // if y doesn't contain the first point of x ...
00064   if (y_iter == y.vertices_end()) return false;
00065 
00066   ++x_iter;
00067   ++y_iter;
00068 
00069   while (y_iter != y.vertices_end()) {
00070     if (!equal_2(*x_iter, *y_iter)) return false;
00071     ++x_iter;
00072     ++y_iter;
00073   }
00074 
00075   y_iter = y.vertices_begin();
00076   while (x_iter != x.vertices_end()) {
00077     if (!equal_2(*x_iter, *y_iter)) return false;
00078     ++x_iter;
00079     ++y_iter;
00080   }
00081 
00082   return true;
00083 }
00084 
00085 //-----------------------------------------------------------------------//
00086 //                          operator>>
00087 //-----------------------------------------------------------------------//
00088 
00089 template <class Traits_P, class Container_P>
00090 std::istream &
00091 operator>>(std::istream &is, Polygon_2<Traits_P,Container_P>& p)
00092 {
00093   int n = 0; // number of vertices
00094   is >> n;
00095   typename Traits_P::Point_2 point;
00096  
00097   if (is) {
00098       p.erase(p.vertices_begin(),p.vertices_end());
00099       for (int i=0; i<n; i++) {
00100         is >> point;
00101         p.push_back(point);
00102       }
00103   }
00104  
00105   return is;
00106 }
00107 
00108 //-----------------------------------------------------------------------//
00109 //                          operator<<
00110 //-----------------------------------------------------------------------//
00111 
00112 template <class Traits_P, class Container_P>
00113 std::ostream&
00114 operator<<(std::ostream &os, const Polygon_2<Traits_P,Container_P>& p)
00115 {
00116   typename Polygon_2<Traits_P,Container_P>::Vertex_const_iterator i;
00117 
00118   switch(os.iword(IO::mode)) {
00119     case IO::ASCII :
00120       os << p.size() << ' ';
00121       for (i = p.vertices_begin(); i != p.vertices_end(); ++i) {
00122         os << *i << ' ';
00123       }
00124       return os;
00125 
00126     case IO::BINARY :
00127       os << p.size();
00128       for (i = p.vertices_begin(); i != p.vertices_end(); ++i) {
00129         os << *i;
00130       }
00131       return os;
00132 
00133     default:
00134       os << "Polygon_2(" << std::endl;
00135       for (i = p.vertices_begin(); i != p.vertices_end(); ++i) {
00136         os << "  " << *i << std::endl;
00137       }
00138       os << ")" << std::endl;
00139       return os;
00140   }
00141 }
00142 
00143 //-----------------------------------------------------------------------//
00144 //                          transform
00145 //-----------------------------------------------------------------------//
00146 
00147 template <class Transformation, class Traits_P, class Container_P>
00148 Polygon_2<Traits_P,Container_P>
00149 transform(const Transformation& t, const Polygon_2<Traits_P,Container_P>& p)
00150 {
00151   typedef typename Polygon_2<Traits_P,Container_P>::Vertex_const_iterator VI;
00152   Polygon_2<Traits_P,Container_P> result;
00153   for (VI i = p.vertices_begin(); i != p.vertices_end(); ++i)
00154     result.push_back(t(*i));
00155   return result;
00156 }
00157 
00158 CGAL_END_NAMESPACE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines