BWAPI
|
00001 // Copyright (c) 1999 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/Kernel_23/include/CGAL/Direction_2.h $ 00019 // $Id: Direction_2.h 49257 2009-05-09 16:08:19Z spion $ 00020 // 00021 // 00022 // Author(s) : Stefan Schirra 00023 00024 #ifndef CGAL_DIRECTION_2_H 00025 #define CGAL_DIRECTION_2_H 00026 00027 #include <boost/static_assert.hpp> 00028 #include <boost/type_traits.hpp> 00029 #include <CGAL/Kernel/Return_base_tag.h> 00030 #include <CGAL/representation_tags.h> 00031 #include <CGAL/Dimension.h> 00032 00033 CGAL_BEGIN_NAMESPACE 00034 00035 template <class R_> 00036 class Direction_2 : public R_::Kernel_base::Direction_2 00037 { 00038 typedef typename R_::RT RT; 00039 typedef typename R_::Vector_2 Vector_2; 00040 typedef typename R_::Line_2 Line_2; 00041 typedef typename R_::Ray_2 Ray_2; 00042 typedef typename R_::Segment_2 Segment_2; 00043 typedef typename R_::Aff_transformation_2 Aff_transformation_2; 00044 typedef typename R_::Kernel_base::Direction_2 RDirection_2; 00045 00046 typedef Direction_2 Self; 00047 BOOST_STATIC_ASSERT((boost::is_same<Self, typename R_::Direction_2>::value)); 00048 00049 public: 00050 00051 typedef Dimension_tag<2> Ambient_dimension; 00052 typedef Dimension_tag<0> Feature_dimension; 00053 00054 typedef RDirection_2 Rep; 00055 00056 const Rep& rep() const 00057 { 00058 return *this; 00059 } 00060 00061 Rep& rep() 00062 { 00063 return *this; 00064 } 00065 00066 typedef R_ R; 00067 00068 Direction_2() {} 00069 00070 Direction_2(const RDirection_2& d) 00071 : RDirection_2(d) {} 00072 00073 explicit Direction_2(const Vector_2& v) 00074 : RDirection_2(typename R::Construct_direction_2()(Return_base_tag(), v)) {} 00075 00076 explicit Direction_2(const Line_2& l) 00077 : RDirection_2(typename R::Construct_direction_2()(Return_base_tag(), l)) {} 00078 00079 explicit Direction_2(const Ray_2& r) 00080 : RDirection_2(typename R::Construct_direction_2()(Return_base_tag(), r)) {} 00081 00082 explicit Direction_2(const Segment_2& s) 00083 : RDirection_2(typename R::Construct_direction_2()(Return_base_tag(), s)) {} 00084 00085 Direction_2(const RT &x, const RT &y) 00086 : RDirection_2(typename R::Construct_direction_2()(Return_base_tag(), x,y)) {} 00087 00088 typename R::Boolean 00089 counterclockwise_in_between(const Direction_2 &d1, 00090 const Direction_2 &d2) const 00091 { 00092 return R().counterclockwise_in_between_2_object()(*this, d1, d2); 00093 } 00094 00095 Direction_2 perpendicular(const Orientation &o) const 00096 { 00097 return R().construct_perpendicular_direction_2_object()(*this,o); 00098 } 00099 00100 typename Qualified_result_of<typename R::Compute_dx_2, Direction_2>::type 00101 dx() const 00102 { 00103 return R().compute_dx_2_object()(*this); 00104 } 00105 00106 typename Qualified_result_of<typename R::Compute_dy_2, Direction_2>::type 00107 dy() const 00108 { 00109 return R().compute_dy_2_object()(*this); 00110 } 00111 00112 typename Qualified_result_of<typename R::Compute_dx_2, Direction_2>::type 00113 delta(int i) const 00114 { 00115 CGAL_kernel_precondition( ( i == 0 ) || ( i == 1 ) ); 00116 return (i==0) ? dx() : dy(); 00117 } 00118 00119 typename R::Boolean 00120 operator<(const Direction_2 &d) const 00121 { 00122 return R().compare_angle_with_x_axis_2_object()(*this, d) == SMALLER; 00123 } 00124 00125 00126 typename R::Boolean 00127 operator>(const Direction_2 &d) const 00128 { 00129 return d < *this; 00130 } 00131 00132 00133 typename R::Boolean 00134 operator>=(const Direction_2 &d) const 00135 { 00136 return R().compare_angle_with_x_axis_2_object()(*this, d) != SMALLER; 00137 } 00138 00139 00140 typename R::Boolean 00141 operator<=(const Direction_2 &d) const 00142 { 00143 return R().compare_angle_with_x_axis_2_object()(*this, d) != LARGER; 00144 } 00145 00146 Direction_2 00147 operator-() const 00148 { 00149 return R().construct_opposite_direction_2_object()(*this); 00150 } 00151 00152 Vector_2 vector() const 00153 { 00154 return R().construct_vector_2_object()(*this); 00155 } 00156 00157 Vector_2 to_vector() const 00158 { 00159 return this->vector(); 00160 } 00161 00162 typename R::Boolean 00163 operator==(const Direction_2& d) const 00164 { 00165 return R().equal_2_object()(*this, d); 00166 } 00167 00168 typename R::Boolean 00169 operator!=(const Direction_2& d) const 00170 { 00171 return !(*this == d); 00172 } 00173 00174 Direction_2 transform(const Aff_transformation_2 &t) const 00175 { 00176 return t.transform(*this); 00177 } 00178 00179 }; 00180 00181 00182 00183 template <class R > 00184 std::ostream& 00185 insert(std::ostream& os, const Direction_2<R>& d, const Cartesian_tag&) 00186 { 00187 typename R::Vector_2 v = d.to_vector(); 00188 switch(os.iword(IO::mode)) { 00189 case IO::ASCII : 00190 return os << v.x() << ' ' << v.y(); 00191 case IO::BINARY : 00192 write(os, v.x()); 00193 write(os, v.y()); 00194 return os; 00195 default: 00196 return os << "DirectionC2(" << v.x() << ", " << v.y() << ')'; 00197 } 00198 } 00199 00200 template <class R > 00201 std::ostream& 00202 insert(std::ostream& os, const Direction_2<R>& d, const Homogeneous_tag&) 00203 { 00204 switch(os.iword(IO::mode)) 00205 { 00206 case IO::ASCII : 00207 return os << d.dx() << ' ' << d.dy(); 00208 case IO::BINARY : 00209 write(os, d.dx()); 00210 write(os, d.dy()); 00211 return os; 00212 default: 00213 return os << "DirectionH2(" << d.dx() << ", " 00214 << d.dy() << ')'; 00215 } 00216 } 00217 00218 template < class R > 00219 std::ostream& 00220 operator<<(std::ostream& os, const Direction_2<R>& d) 00221 { 00222 return insert(os, d, typename R::Kernel_tag() ); 00223 } 00224 00225 00226 template <class R > 00227 std::istream& 00228 extract(std::istream& is, Direction_2<R>& d, const Cartesian_tag&) 00229 { 00230 typename R::FT x, y; 00231 switch(is.iword(IO::mode)) { 00232 case IO::ASCII : 00233 is >> x >> y; 00234 break; 00235 case IO::BINARY : 00236 read(is, x); 00237 read(is, y); 00238 break; 00239 default: 00240 std::cerr << std::endl << "Stream must be in ascii or binary mode" 00241 << std::endl; 00242 break; 00243 } 00244 if (is) 00245 d = Direction_2<R>(x, y); 00246 return is; 00247 } 00248 00249 template <class R > 00250 std::istream& 00251 extract(std::istream& is, Direction_2<R>& d, const Homogeneous_tag&) 00252 { 00253 typename R::RT x, y; 00254 switch(is.iword(IO::mode)) 00255 { 00256 case IO::ASCII : 00257 is >> x >> y; 00258 break; 00259 case IO::BINARY : 00260 read(is, x); 00261 read(is, y); 00262 break; 00263 default: 00264 std::cerr << "" << std::endl; 00265 std::cerr << "Stream must be in ascii or binary mode" << std::endl; 00266 break; 00267 } 00268 d = Direction_2<R>(x, y); 00269 return is; 00270 } 00271 00272 template < class R > 00273 std::istream& 00274 operator>>(std::istream& is, Direction_2<R>& d) 00275 { 00276 return extract(is, d, typename R::Kernel_tag() ); 00277 } 00278 00279 CGAL_END_NAMESPACE 00280 00281 #endif // CGAL_DIRECTION_2_H